Skip to content

Commit 189f88e

Browse files
Added tests for multi-part decryption/encryption
Signed-off-by: Jacob Prud'homme <[email protected]>
1 parent 58eec0a commit 189f88e

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed

cryptoki/tests/basic.rs

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,172 @@ fn encrypt_decrypt() -> TestResult {
265265
Ok(())
266266
}
267267

268+
#[test]
269+
#[serial]
270+
// Currently SoftHSM doesn't support EncryptUpdate/DecryptUpdate
271+
#[ignore]
272+
fn encrypt_decrypt_multipart() -> TestResult {
273+
let (pkcs11, slot) = init_pins();
274+
275+
// Open a session and log in
276+
let session = pkcs11.open_rw_session(slot)?;
277+
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
278+
279+
// Define parameters for keypair
280+
let public_exponent = vec![0x01, 0x00, 0x01];
281+
let modulus_bits = 1024;
282+
283+
let pub_key_template = vec![
284+
Attribute::Token(true),
285+
Attribute::Private(false),
286+
Attribute::PublicExponent(public_exponent),
287+
Attribute::ModulusBits(modulus_bits.into()),
288+
Attribute::Encrypt(true),
289+
];
290+
let priv_key_template = vec![Attribute::Token(true), Attribute::Decrypt(true)];
291+
292+
// Generate keypair
293+
let (pub_key, priv_key) =
294+
session.generate_key_pair(&Mechanism::RsaPkcsKeyPairGen, &pub_key_template, &priv_key_template)?;
295+
296+
// Data to encrypt
297+
let data = vec![0xFF, 0x55, 0xDD, 0x11, 0xBB, 0x33];
298+
299+
// Encrypt data in parts
300+
session.encrypt_initialize(&Mechanism::RsaPkcs, pub_key)?;
301+
302+
let mut encrypted_data = vec![];
303+
for part in data.chunks(3) {
304+
encrypted_data.extend(session.encrypt_update(part)?);
305+
}
306+
encrypted_data.extend(session.encrypt_finalize()?);
307+
308+
// Decrypt data in parts
309+
session.decrypt_initialize(&Mechanism::RsaPkcs, priv_key)?;
310+
311+
let mut decrypted_data = vec![];
312+
for part in encrypted_data.chunks(3) {
313+
decrypted_data.extend(session.decrypt_update(part)?);
314+
}
315+
decrypted_data.extend(session.decrypt_finalize()?);
316+
317+
assert_eq!(data, decrypted_data);
318+
319+
// Delete keys
320+
session.destroy_object(pub_key)?;
321+
session.destroy_object(priv_key)?;
322+
323+
Ok(())
324+
}
325+
326+
#[test]
327+
#[serial]
328+
// Currently SoftHSM doesn't support EncryptUpdate/DecryptUpdate
329+
#[ignore]
330+
fn encrypt_decrypt_multipart_not_initialized() -> TestResult {
331+
let (pkcs11, slot) = init_pins();
332+
333+
// Open a session and log in
334+
let session = pkcs11.open_rw_session(slot)?;
335+
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
336+
337+
// Data to encrypt/decrypt
338+
let data = vec![0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF];
339+
340+
// Attempt to update encryption without an operation having been initialized
341+
let result = session.encrypt_update(&data);
342+
343+
assert!(result.is_err());
344+
assert!(matches!(
345+
result.unwrap_err(),
346+
Error::Pkcs11(RvError::OperationNotInitialized, Function::EncryptUpdate)
347+
));
348+
349+
// Attempt to finalize encryption without an operation having been initialized
350+
let result = session.encrypt_finalize();
351+
352+
assert!(result.is_err());
353+
assert!(matches!(
354+
result.unwrap_err(),
355+
Error::Pkcs11(RvError::OperationNotInitialized, Function::EncryptFinal)
356+
));
357+
358+
// Attempt to update decryption without an operation having been initialized
359+
let result = session.decrypt_update(&data);
360+
361+
assert!(result.is_err());
362+
assert!(matches!(
363+
result.unwrap_err(),
364+
Error::Pkcs11(RvError::OperationNotInitialized, Function::DecryptUpdate)
365+
));
366+
367+
// Attempt to finalize decryption without an operation having been initialized
368+
let result = session.decrypt_finalize();
369+
370+
assert!(result.is_err());
371+
assert!(matches!(
372+
result.unwrap_err(),
373+
Error::Pkcs11(RvError::OperationNotInitialized, Function::DecryptFinal)
374+
));
375+
376+
Ok(())
377+
}
378+
379+
#[test]
380+
#[serial]
381+
// Currently SoftHSM doesn't support EncryptUpdate/DecryptUpdate
382+
#[ignore]
383+
fn encrypt_decrypt_multipart_already_initialized() -> TestResult {
384+
let (pkcs11, slot) = init_pins();
385+
386+
// Open a session and log in
387+
let session = pkcs11.open_rw_session(slot)?;
388+
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
389+
390+
// Define parameters for keypair
391+
let public_exponent = vec![0x01, 0x00, 0x01];
392+
let modulus_bits = 1024;
393+
394+
let pub_key_template = vec![
395+
Attribute::Token(true),
396+
Attribute::Private(false),
397+
Attribute::PublicExponent(public_exponent),
398+
Attribute::ModulusBits(modulus_bits.into()),
399+
Attribute::Encrypt(true),
400+
];
401+
let priv_key_template = vec![Attribute::Token(true), Attribute::Decrypt(true)];
402+
403+
// Generate keypair
404+
let (pub_key, priv_key) =
405+
session.generate_key_pair(&Mechanism::RsaPkcsKeyPairGen, &pub_key_template, &priv_key_template)?;
406+
407+
// Initialize encryption operation twice in a row
408+
session.encrypt_initialize(&Mechanism::RsaPkcs, pub_key)?;
409+
let result = session.encrypt_initialize(&Mechanism::RsaPkcs, pub_key);
410+
411+
assert!(result.is_err());
412+
assert!(matches!(
413+
result.unwrap_err(),
414+
Error::Pkcs11(RvError::OperationActive, Function::EncryptInit)
415+
));
416+
417+
// Initialize encryption operation twice in a row
418+
session.decrypt_initialize(&Mechanism::RsaPkcs, priv_key)?;
419+
let result = session.decrypt_initialize(&Mechanism::RsaPkcs, priv_key);
420+
421+
assert!(result.is_err());
422+
assert!(matches!(
423+
result.unwrap_err(),
424+
Error::Pkcs11(RvError::OperationActive, Function::DecryptInit)
425+
));
426+
427+
// Delete keys
428+
session.destroy_object(pub_key)?;
429+
session.destroy_object(priv_key)?;
430+
431+
Ok(())
432+
}
433+
268434
#[test]
269435
#[serial]
270436
fn derive_key() -> TestResult {

0 commit comments

Comments
 (0)