-
-
Notifications
You must be signed in to change notification settings - Fork 34
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question: Encrypting/Decrypting across restarts #145
Comments
Here is my example Key generationconst label = "test_key";
const softHsm = graphene.Module.load("/usr/local/lib/softhsm/libsofthsm2.so");
softHsm.initialize();
try {
const slot = softHsm.getSlots(0);
const session = slot.open(graphene.SessionFlag.RW_SESSION | graphene.SessionFlag.SERIAL_SESSION);
session.login("12345", graphene.UserType.USER);
const keys = session.generateKeyPair(graphene.KeyGenMechanism.RSA, {
keyType: graphene.KeyType.RSA,
token: true,
label,
publicExponent: Buffer.from([1, 0, 1]),
modulusBits: 2048,
encrypt: true,
}, {
keyType: graphene.KeyType.RSA,
private: true,
sensitive: true,
token: true,
label,
decrypt: true,
});
console.log(keys);
} catch (e) {
console.error(e);
softHsm.finalize();
} Encryptionconst label = "test_key";
const softHsm = graphene.Module.load("/usr/local/lib/softhsm/libsofthsm2.so");
softHsm.initialize();
try {
const slot = softHsm.getSlots(0);
const session = slot.open(graphene.SessionFlag.SERIAL_SESSION);
const publicKey = session.find({
class: graphene.ObjectClass.PUBLIC_KEY,
label,
}).items(0).toType();
const cipher = session.createCipher({
name: "RSA_PKCS_OAEP",
params: new graphene.RsaOaepParams(graphene.MechanismEnum.SHA1, graphene.RsaMgf.MGF1_SHA1),
}, publicKey);
encrypted = cipher.once(data, Buffer.alloc(2048));
console.log(encrypted);
} catch (e) {
console.error(e);
softHsm.finalize();
} Decryptionconst label = "test_key";
const softHsm = graphene.Module.load("/usr/local/lib/softhsm/libsofthsm2.so");
softHsm.initialize();
try {
const slot = softHsm.getSlots(0);
const session = slot.open(graphene.SessionFlag.SERIAL_SESSION);
session.login("12345", graphene.UserType.USER);
const privateKey = session.find({
class: graphene.ObjectClass.PRIVATE_KEY,
label,
}).items(0).toType();
const decipher = session.createDecipher({
name: "RSA_PKCS_OAEP",
params: new graphene.RsaOaepParams(graphene.MechanismEnum.SHA1, graphene.RsaMgf.MGF1_SHA1),
}, privateKey);
const decrypted = decipher.once(encrypted, Buffer.alloc(2048));
console.log(decrypted);
} catch (e) {
console.error(e);
softHsm.finalize();
} |
@microshine Thank you for the detailed example. I can confirm what you have shared works as expected, however without a an application restart between encryption and decryption functions. So the question I still have is, what could be a recommended way to store the Public/Private key pair in order to ensure that the data encrypted using a set of keys can be decrypted in a deterministic way even after the application restarts. An interesting side note on something that I observed:
Thank you for the clarification and further help. |
Have you managed to solve this issue by any chance and decrypt data separately from the encryption ? @santosh-shaastry |
Hi,
I am a newbie to SoftHSM/PKCS 11. I am trying to:
Step 1
session
session
'scipher
methods as described in your README documentationStep 2
Step 3
session
session
'scipher
methods as described in your README documentationThough I am able to login successfully using the same token and seem to obtain the same
session
, I run into the following error when performingBuffer.concat([dec, decipher.final()]).toString();
From what I understand about the PKCS 11 Session as described in the spec, unless a
session
is closed/logged out the application should be able to access the same session and hence the token/slot.Appreciate any direction, inputs and help on this one. Thank you.
The text was updated successfully, but these errors were encountered: