Skip to content

Commit 498c5ee

Browse files
Re-enabled prior tests for multi-part decryption/encryption
I found out it was not that multi-part encryption/decryption weren't supported *at all*, it's that they're just supported for symmetric schemes Signed-off-by: Jacob Prud'homme <[email protected]>
1 parent feb07ed commit 498c5ee

File tree

1 file changed

+35
-46
lines changed

1 file changed

+35
-46
lines changed

cryptoki/tests/basic.rs

Lines changed: 35 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -431,37 +431,31 @@ fn encrypt_decrypt() -> TestResult {
431431

432432
#[test]
433433
#[serial]
434-
// Currently SoftHSM doesn't support EncryptUpdate/DecryptUpdate
435-
#[ignore]
436434
fn encrypt_decrypt_multipart() -> TestResult {
437435
let (pkcs11, slot) = init_pins();
438436

439437
// Open a session and log in
440438
let session = pkcs11.open_rw_session(slot)?;
441439
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
442440

443-
// Define parameters for keypair
444-
let public_exponent = vec![0x01, 0x00, 0x01];
445-
let modulus_bits = 1024;
446-
447-
let pub_key_template = vec![
441+
// Generate key (currently SoftHSM only supports multi-part encrypt/decrypt for symmetric crypto)
442+
let template = vec![
448443
Attribute::Token(true),
449444
Attribute::Private(false),
450-
Attribute::PublicExponent(public_exponent),
451-
Attribute::ModulusBits(modulus_bits.into()),
445+
Attribute::ValueLen((128 / 8).into()),
452446
Attribute::Encrypt(true),
447+
Attribute::Decrypt(true),
453448
];
454-
let priv_key_template = vec![Attribute::Token(true), Attribute::Decrypt(true)];
455-
456-
// Generate keypair
457-
let (pub_key, priv_key) =
458-
session.generate_key_pair(&Mechanism::RsaPkcsKeyPairGen, &pub_key_template, &priv_key_template)?;
449+
let key = session.generate_key(&Mechanism::AesKeyGen, &template)?;
459450

460451
// Data to encrypt
461-
let data = vec![0xFF, 0x55, 0xDD, 0x11, 0xBB, 0x33];
452+
let data = vec![
453+
0xFF, 0x55, 0xDD, 0x11, 0xBB, 0x33, 0x99, 0x77, 0xFF, 0x55, 0xDD, 0x11, 0xBB, 0x33, 0x99,
454+
0x77,
455+
];
462456

463457
// Encrypt data in parts
464-
session.encrypt_initialize(&Mechanism::RsaPkcs, pub_key)?;
458+
session.encrypt_initialize(&Mechanism::AesEcb, key)?;
465459

466460
let mut encrypted_data = vec![];
467461
for part in data.chunks(3) {
@@ -470,7 +464,7 @@ fn encrypt_decrypt_multipart() -> TestResult {
470464
encrypted_data.extend(session.encrypt_finalize()?);
471465

472466
// Decrypt data in parts
473-
session.decrypt_initialize(&Mechanism::RsaPkcs, priv_key)?;
467+
session.decrypt_initialize(&Mechanism::AesEcb, key)?;
474468

475469
let mut decrypted_data = vec![];
476470
for part in encrypted_data.chunks(3) {
@@ -480,26 +474,26 @@ fn encrypt_decrypt_multipart() -> TestResult {
480474

481475
assert_eq!(data, decrypted_data);
482476

483-
// Delete keys
484-
session.destroy_object(pub_key)?;
485-
session.destroy_object(priv_key)?;
477+
// Delete key
478+
session.destroy_object(key)?;
486479

487480
Ok(())
488481
}
489482

490483
#[test]
491484
#[serial]
492-
// Currently SoftHSM doesn't support EncryptUpdate/DecryptUpdate
493-
#[ignore]
494485
fn encrypt_decrypt_multipart_not_initialized() -> TestResult {
495486
let (pkcs11, slot) = init_pins();
496487

497488
// Open a session and log in
498-
let session = pkcs11.open_rw_session(slot)?;
489+
let session = pkcs11.open_ro_session(slot)?;
499490
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
500491

501492
// Data to encrypt/decrypt
502-
let data = vec![0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF];
493+
let data = vec![
494+
0xFF, 0x55, 0xDD, 0x11, 0xBB, 0x33, 0x99, 0x77, 0xFF, 0x55, 0xDD, 0x11, 0xBB, 0x33, 0x99,
495+
0x77,
496+
];
503497

504498
// Attempt to update encryption without an operation having been initialized
505499
let result = session.encrypt_update(&data);
@@ -542,55 +536,48 @@ fn encrypt_decrypt_multipart_not_initialized() -> TestResult {
542536

543537
#[test]
544538
#[serial]
545-
// Currently SoftHSM doesn't support EncryptUpdate/DecryptUpdate
546-
#[ignore]
547539
fn encrypt_decrypt_multipart_already_initialized() -> TestResult {
548540
let (pkcs11, slot) = init_pins();
549541

550542
// Open a session and log in
551543
let session = pkcs11.open_rw_session(slot)?;
552544
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
553545

554-
// Define parameters for keypair
555-
let public_exponent = vec![0x01, 0x00, 0x01];
556-
let modulus_bits = 1024;
557-
558-
let pub_key_template = vec![
546+
// Generate key (currently SoftHSM only supports multi-part encrypt/decrypt for symmetric crypto)
547+
let template = vec![
559548
Attribute::Token(true),
560549
Attribute::Private(false),
561-
Attribute::PublicExponent(public_exponent),
562-
Attribute::ModulusBits(modulus_bits.into()),
550+
Attribute::ValueLen((128 / 8).into()),
563551
Attribute::Encrypt(true),
552+
Attribute::Decrypt(true),
564553
];
565-
let priv_key_template = vec![Attribute::Token(true), Attribute::Decrypt(true)];
566-
567-
// Generate keypair
568-
let (pub_key, priv_key) =
569-
session.generate_key_pair(&Mechanism::RsaPkcsKeyPairGen, &pub_key_template, &priv_key_template)?;
554+
let key = session.generate_key(&Mechanism::AesKeyGen, &template)?;
570555

571556
// Initialize encryption operation twice in a row
572-
session.encrypt_initialize(&Mechanism::RsaPkcs, pub_key)?;
573-
let result = session.encrypt_initialize(&Mechanism::RsaPkcs, pub_key);
557+
session.encrypt_initialize(&Mechanism::AesEcb, key)?;
558+
let result = session.encrypt_initialize(&Mechanism::AesEcb, key);
574559

575560
assert!(result.is_err());
576561
assert!(matches!(
577562
result.unwrap_err(),
578563
Error::Pkcs11(RvError::OperationActive, Function::EncryptInit)
579564
));
580565

566+
// Make sure encryption operation is over before trying same with decryption
567+
session.encrypt_finalize()?;
568+
581569
// Initialize encryption operation twice in a row
582-
session.decrypt_initialize(&Mechanism::RsaPkcs, priv_key)?;
583-
let result = session.decrypt_initialize(&Mechanism::RsaPkcs, priv_key);
570+
session.decrypt_initialize(&Mechanism::AesEcb, key)?;
571+
let result = session.decrypt_initialize(&Mechanism::AesEcb, key);
584572

585573
assert!(result.is_err());
586574
assert!(matches!(
587575
result.unwrap_err(),
588576
Error::Pkcs11(RvError::OperationActive, Function::DecryptInit)
589577
));
590578

591-
// Delete keys
592-
session.destroy_object(pub_key)?;
593-
session.destroy_object(priv_key)?;
579+
// Delete key
580+
session.destroy_object(key)?;
594581

595582
Ok(())
596583
}
@@ -1644,7 +1631,9 @@ fn sha256_digest_multipart() -> TestResult {
16441631
session.login(UserType::User, Some(&AuthPin::new(USER_PIN.into())))?;
16451632

16461633
// Data to digest
1647-
let data = vec![0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11];
1634+
let data = vec![
1635+
0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11,
1636+
];
16481637

16491638
// Digest data in parts
16501639
session.digest_initialize(&Mechanism::Sha256)?;

0 commit comments

Comments
 (0)