@@ -23,6 +23,8 @@ use std::thread;
23
23
use cryptoki:: mechanism:: ekdf:: AesCbcDeriveParams ;
24
24
use testresult:: TestResult ;
25
25
26
+ const AES128_BLOCK_SIZE : usize = 128 / 8 ;
27
+
26
28
#[ test]
27
29
#[ serial]
28
30
fn sign_verify ( ) -> TestResult {
@@ -228,10 +230,12 @@ fn sign_verify_multipart() -> TestResult {
228
230
229
231
let pub_key_template = vec ! [
230
232
Attribute :: Token ( true ) ,
233
+ Attribute :: Private ( false ) ,
231
234
Attribute :: PublicExponent ( public_exponent) ,
232
235
Attribute :: ModulusBits ( modulus_bits. into( ) ) ,
236
+ Attribute :: Verify ( true ) ,
233
237
] ;
234
- let priv_key_template = vec ! [ Attribute :: Token ( true ) ] ;
238
+ let priv_key_template = vec ! [ Attribute :: Token ( true ) , Attribute :: Sign ( true ) ] ;
235
239
236
240
// Generate keypair
237
241
let ( pub_key, priv_key) = session. generate_key_pair (
@@ -281,36 +285,40 @@ fn sign_verify_multipart_not_initialized() -> TestResult {
281
285
let result = session. sign_update ( & data) ;
282
286
283
287
assert ! ( result. is_err( ) ) ;
288
+ // The exact error returned is inconsistent between backends, so we only match on the function
284
289
assert ! ( matches!(
285
290
result. unwrap_err( ) ,
286
- Error :: Pkcs11 ( RvError :: OperationNotInitialized , Function :: SignUpdate )
291
+ Error :: Pkcs11 ( _ , Function :: SignUpdate )
287
292
) ) ;
288
293
289
294
// Attempt to finalize signing without an operation having been initialized
290
295
let result = session. sign_final ( ) ;
291
296
292
297
assert ! ( result. is_err( ) ) ;
298
+ // The exact error returned is inconsistent between backends, so we only match on the function
293
299
assert ! ( matches!(
294
300
result. unwrap_err( ) ,
295
- Error :: Pkcs11 ( RvError :: OperationNotInitialized , Function :: SignFinal )
301
+ Error :: Pkcs11 ( _ , Function :: SignFinal )
296
302
) ) ;
297
303
298
304
// Attempt to update verification without an operation having been initialized
299
305
let result = session. verify_update ( & data) ;
300
306
301
307
assert ! ( result. is_err( ) ) ;
308
+ // The exact error returned is inconsistent between backends, so we only match on the function
302
309
assert ! ( matches!(
303
310
result. unwrap_err( ) ,
304
- Error :: Pkcs11 ( RvError :: OperationNotInitialized , Function :: VerifyUpdate )
311
+ Error :: Pkcs11 ( _ , Function :: VerifyUpdate )
305
312
) ) ;
306
313
307
314
// Attempt to finalize verification without an operation having been initialized
308
315
let result = session. verify_final ( & signature) ;
309
316
310
317
assert ! ( result. is_err( ) ) ;
318
+ // The exact error returned is inconsistent between backends, so we only match on the function
311
319
assert ! ( matches!(
312
320
result. unwrap_err( ) ,
313
- Error :: Pkcs11 ( RvError :: OperationNotInitialized , Function :: VerifyFinal )
321
+ Error :: Pkcs11 ( _ , Function :: VerifyFinal )
314
322
) ) ;
315
323
316
324
Ok ( ( ) )
@@ -331,10 +339,12 @@ fn sign_verify_multipart_already_initialized() -> TestResult {
331
339
332
340
let pub_key_template = vec ! [
333
341
Attribute :: Token ( true ) ,
342
+ Attribute :: Private ( false ) ,
334
343
Attribute :: PublicExponent ( public_exponent) ,
335
344
Attribute :: ModulusBits ( modulus_bits. into( ) ) ,
345
+ Attribute :: Verify ( true ) ,
336
346
] ;
337
- let priv_key_template = vec ! [ Attribute :: Token ( true ) ] ;
347
+ let priv_key_template = vec ! [ Attribute :: Token ( true ) , Attribute :: Sign ( true ) ] ;
338
348
339
349
// Generate keypair
340
350
let ( pub_key, priv_key) = session. generate_key_pair (
@@ -353,8 +363,10 @@ fn sign_verify_multipart_already_initialized() -> TestResult {
353
363
Error :: Pkcs11 ( RvError :: OperationActive , Function :: SignInit )
354
364
) ) ;
355
365
356
- // Make sure signing operation is over before trying same with verification
357
- session. sign_final ( ) ?;
366
+ // Make sure signing operation is over before trying same with verification.
367
+ // Some backends will reset the ongoing operation after the failed 2nd call to
368
+ // sign_init(), so we should not unwrap the result of this call.
369
+ let _ = session. sign_final ( ) ;
358
370
359
371
// Initialize verification operation twice in a row
360
372
session. verify_init ( & Mechanism :: Sha256RsaPkcs , pub_key) ?;
@@ -437,21 +449,26 @@ fn encrypt_decrypt_multipart() -> TestResult {
437
449
// Generate key (currently SoftHSM only supports multi-part encrypt/decrypt for symmetric crypto)
438
450
let template = vec ! [
439
451
Attribute :: Token ( true ) ,
440
- Attribute :: ValueLen ( ( 128 / 8 ) . into( ) ) ,
452
+ Attribute :: Private ( false ) ,
453
+ Attribute :: ValueLen ( ( AES128_BLOCK_SIZE as u64 ) . into( ) ) ,
454
+ Attribute :: Encrypt ( true ) ,
455
+ Attribute :: Decrypt ( true ) ,
441
456
] ;
442
457
let key = session. generate_key ( & Mechanism :: AesKeyGen , & template) ?;
443
458
444
459
// Data to encrypt
445
460
let data = vec ! [
446
461
0xFF , 0x55 , 0xDD , 0x11 , 0xBB , 0x33 , 0x99 , 0x77 , 0xFF , 0x55 , 0xDD , 0x11 , 0xBB , 0x33 , 0x99 ,
447
- 0x77 ,
462
+ 0x77 , 0xFF , 0x55 , 0xDD , 0x11 , 0xBB , 0x33 , 0x99 , 0x77 , 0xFF , 0x55 , 0xDD , 0x11 , 0xBB , 0x33 ,
463
+ 0x99 , 0x77 , 0xFF , 0x55 , 0xDD , 0x11 , 0xBB , 0x33 , 0x99 , 0x77 , 0xFF , 0x55 , 0xDD , 0x11 , 0xBB ,
464
+ 0x33 , 0x99 , 0x77 ,
448
465
] ;
449
466
450
467
// Encrypt data in parts
451
468
session. encrypt_init ( & Mechanism :: AesEcb , key) ?;
452
469
453
470
let mut encrypted_data = vec ! [ ] ;
454
- for part in data. chunks ( 3 ) {
471
+ for part in data. chunks ( AES128_BLOCK_SIZE ) {
455
472
encrypted_data. extend ( session. encrypt_update ( part) ?) ;
456
473
}
457
474
encrypted_data. extend ( session. encrypt_final ( ) ?) ;
@@ -460,7 +477,7 @@ fn encrypt_decrypt_multipart() -> TestResult {
460
477
session. decrypt_init ( & Mechanism :: AesEcb , key) ?;
461
478
462
479
let mut decrypted_data = vec ! [ ] ;
463
- for part in encrypted_data. chunks ( 3 ) {
480
+ for part in encrypted_data. chunks ( AES128_BLOCK_SIZE ) {
464
481
decrypted_data. extend ( session. decrypt_update ( part) ?) ;
465
482
}
466
483
decrypted_data. extend ( session. decrypt_final ( ) ?) ;
@@ -492,36 +509,40 @@ fn encrypt_decrypt_multipart_not_initialized() -> TestResult {
492
509
let result = session. encrypt_update ( & data) ;
493
510
494
511
assert ! ( result. is_err( ) ) ;
512
+ // The exact error returned is inconsistent between backends, so we only match on the function
495
513
assert ! ( matches!(
496
514
result. unwrap_err( ) ,
497
- Error :: Pkcs11 ( RvError :: OperationNotInitialized , Function :: EncryptUpdate )
515
+ Error :: Pkcs11 ( _ , Function :: EncryptUpdate )
498
516
) ) ;
499
517
500
518
// Attempt to finalize encryption without an operation having been initialized
501
519
let result = session. encrypt_final ( ) ;
502
520
503
521
assert ! ( result. is_err( ) ) ;
522
+ // The exact error returned is inconsistent between backends, so we only match on the function
504
523
assert ! ( matches!(
505
524
result. unwrap_err( ) ,
506
- Error :: Pkcs11 ( RvError :: OperationNotInitialized , Function :: EncryptFinal )
525
+ Error :: Pkcs11 ( _ , Function :: EncryptFinal )
507
526
) ) ;
508
527
509
528
// Attempt to update decryption without an operation having been initialized
510
529
let result = session. decrypt_update ( & data) ;
511
530
512
531
assert ! ( result. is_err( ) ) ;
532
+ // The exact error returned is inconsistent between backends, so we only match on the function
513
533
assert ! ( matches!(
514
534
result. unwrap_err( ) ,
515
- Error :: Pkcs11 ( RvError :: OperationNotInitialized , Function :: DecryptUpdate )
535
+ Error :: Pkcs11 ( _ , Function :: DecryptUpdate )
516
536
) ) ;
517
537
518
538
// Attempt to finalize decryption without an operation having been initialized
519
539
let result = session. decrypt_final ( ) ;
520
540
521
541
assert ! ( result. is_err( ) ) ;
542
+ // The exact error returned is inconsistent between backends, so we only match on the function
522
543
assert ! ( matches!(
523
544
result. unwrap_err( ) ,
524
- Error :: Pkcs11 ( RvError :: OperationNotInitialized , Function :: DecryptFinal )
545
+ Error :: Pkcs11 ( _ , Function :: DecryptFinal )
525
546
) ) ;
526
547
527
548
Ok ( ( ) )
@@ -539,7 +560,10 @@ fn encrypt_decrypt_multipart_already_initialized() -> TestResult {
539
560
// Generate key (currently SoftHSM only supports multi-part encrypt/decrypt for symmetric crypto)
540
561
let template = vec ! [
541
562
Attribute :: Token ( true ) ,
542
- Attribute :: ValueLen ( ( 128 / 8 ) . into( ) ) ,
563
+ Attribute :: Private ( false ) ,
564
+ Attribute :: ValueLen ( ( AES128_BLOCK_SIZE as u64 ) . into( ) ) ,
565
+ Attribute :: Encrypt ( true ) ,
566
+ Attribute :: Decrypt ( true ) ,
543
567
] ;
544
568
let key = session. generate_key ( & Mechanism :: AesKeyGen , & template) ?;
545
569
@@ -553,8 +577,10 @@ fn encrypt_decrypt_multipart_already_initialized() -> TestResult {
553
577
Error :: Pkcs11 ( RvError :: OperationActive , Function :: EncryptInit )
554
578
) ) ;
555
579
556
- // Make sure encryption operation is over before trying same with decryption
557
- session. encrypt_final ( ) ?;
580
+ // Make sure encryption operation is over before trying same with decryption.
581
+ // Some backends will reset the ongoing operation after the failed 2nd call to
582
+ // encrypt_init(), so we should not unwrap the result of this call.
583
+ let _ = session. encrypt_final ( ) ;
558
584
559
585
// Initialize encryption operation twice in a row
560
586
session. decrypt_init ( & Mechanism :: AesEcb , key) ?;
@@ -1646,6 +1672,11 @@ fn sha256_digest_multipart() -> TestResult {
1646
1672
#[ test]
1647
1673
#[ serial]
1648
1674
fn sha256_digest_multipart_with_key ( ) -> TestResult {
1675
+ // FIXME: Getting value from sensitive objects is now broken in Kryoptic: https://github.com/latchset/kryoptic/issues/193
1676
+ if !is_softhsm ( ) {
1677
+ return Ok ( ( ) ) ;
1678
+ }
1679
+
1649
1680
let ( pkcs11, slot) = init_pins ( ) ;
1650
1681
1651
1682
// Open a session and log in
@@ -1655,7 +1686,8 @@ fn sha256_digest_multipart_with_key() -> TestResult {
1655
1686
// Create a key to add to the digest
1656
1687
let key_template = vec ! [
1657
1688
Attribute :: Token ( true ) ,
1658
- Attribute :: ValueLen ( ( 256 / 8 ) . into( ) ) ,
1689
+ Attribute :: Private ( false ) ,
1690
+ Attribute :: ValueLen ( ( AES128_BLOCK_SIZE as u64 ) . into( ) ) ,
1659
1691
// Key must be non-sensitive and extractable to get its bytes and digest them directly, for comparison
1660
1692
Attribute :: Sensitive ( false ) ,
1661
1693
Attribute :: Extractable ( true ) ,
@@ -1707,18 +1739,20 @@ fn sha256_digest_multipart_not_initialized() -> TestResult {
1707
1739
let result = session. digest_update ( & data) ;
1708
1740
1709
1741
assert ! ( result. is_err( ) ) ;
1742
+ // The exact error returned is inconsistent between backends, so we only match on the function
1710
1743
assert ! ( matches!(
1711
1744
result. unwrap_err( ) ,
1712
- Error :: Pkcs11 ( RvError :: OperationNotInitialized , Function :: DigestUpdate )
1745
+ Error :: Pkcs11 ( _ , Function :: DigestUpdate )
1713
1746
) ) ;
1714
1747
1715
1748
// Attempt to finalize digest without an operation having been initialized
1716
1749
let result = session. digest_final ( ) ;
1717
1750
1718
1751
assert ! ( result. is_err( ) ) ;
1752
+ // The exact error returned is inconsistent between backends, so we only match on the function
1719
1753
assert ! ( matches!(
1720
1754
result. unwrap_err( ) ,
1721
- Error :: Pkcs11 ( RvError :: OperationNotInitialized , Function :: DigestFinal )
1755
+ Error :: Pkcs11 ( _ , Function :: DigestFinal )
1722
1756
) ) ;
1723
1757
1724
1758
Ok ( ( ) )
0 commit comments