@@ -5,6 +5,7 @@ mod common;
5
5
use crate :: common:: { SO_PIN , USER_PIN } ;
6
6
use common:: init_pins;
7
7
use cryptoki:: error:: { Error , RvError } ;
8
+ use cryptoki:: mechanism:: aead:: GcmParams ;
8
9
use cryptoki:: mechanism:: Mechanism ;
9
10
use cryptoki:: object:: { Attribute , AttributeInfo , AttributeType , KeyType , ObjectClass } ;
10
11
use cryptoki:: session:: { SessionState , UserType } ;
@@ -938,3 +939,67 @@ fn sha256_digest() -> TestResult {
938
939
939
940
Ok ( ( ) )
940
941
}
942
+
943
+ #[ test]
944
+ #[ serial]
945
+ // Currently empty AAD crashes SoftHSM, see: https://github.com/opendnssec/SoftHSMv2/issues/605
946
+ #[ ignore]
947
+ fn aes_gcm_no_aad ( ) -> TestResult {
948
+ // Encrypt two blocks of zeros with AES-128-GCM
949
+ let key = vec ! [ 0 ; 16 ] ;
950
+ let iv = [ 0 ; 12 ] ;
951
+ let aad = [ ] ;
952
+ let plain = [ 0 ; 32 ] ;
953
+ let expected_cipher_and_tag = [
954
+ 0x03 , 0x88 , 0xda , 0xce , 0x60 , 0xb6 , 0xa3 , 0x92 , 0xf3 , 0x28 , 0xc2 , 0xb9 , 0x71 , 0xb2 , 0xfe ,
955
+ 0x78 , 0xf7 , 0x95 , 0xaa , 0xab , 0x49 , 0x4b , 0x59 , 0x23 , 0xf7 , 0xfd , 0x89 , 0xff , 0x94 , 0x8b ,
956
+ 0xc1 , 0xe0 , 0x40 , 0x49 , 0x0a , 0xf4 , 0x80 , 0x56 , 0x06 , 0xb2 , 0xa3 , 0xa2 , 0xe7 , 0x93 ,
957
+ ] ;
958
+
959
+ let ( pkcs11, slot) = init_pins ( ) ;
960
+ let session = pkcs11. open_rw_session ( slot) ?;
961
+ session. login ( UserType :: User , Some ( & AuthPin :: new ( USER_PIN . into ( ) ) ) ) ?;
962
+
963
+ let template = [
964
+ Attribute :: Class ( ObjectClass :: SECRET_KEY ) ,
965
+ Attribute :: KeyType ( KeyType :: AES ) ,
966
+ Attribute :: Value ( key) ,
967
+ Attribute :: Encrypt ( true ) ,
968
+ ] ;
969
+ let key_handle = session. create_object ( & template) ?;
970
+ let mechanism = Mechanism :: AesGcm ( GcmParams :: new ( & iv, & aad, 96 . into ( ) ) ) ;
971
+ let cipher_and_tag = session. encrypt ( & mechanism, key_handle, & plain) ?;
972
+ assert_eq ! ( expected_cipher_and_tag[ ..] , cipher_and_tag[ ..] ) ;
973
+ Ok ( ( ) )
974
+ }
975
+
976
+ #[ test]
977
+ #[ serial]
978
+ fn aes_gcm_with_aad ( ) -> TestResult {
979
+ // Encrypt a block of zeros with AES-128-GCM.
980
+ // Use another block of zeros for AAD.
981
+ let key = vec ! [ 0 ; 16 ] ;
982
+ let iv = [ 0 ; 12 ] ;
983
+ let aad = [ 0 ; 16 ] ;
984
+ let plain = [ 0 ; 16 ] ;
985
+ let expected_cipher_and_tag = [
986
+ 0x03 , 0x88 , 0xda , 0xce , 0x60 , 0xb6 , 0xa3 , 0x92 , 0xf3 , 0x28 , 0xc2 , 0xb9 , 0x71 , 0xb2 , 0xfe ,
987
+ 0x78 , 0xd2 , 0x4e , 0x50 , 0x3a , 0x1b , 0xb0 , 0x37 , 0x07 , 0x1c , 0x71 , 0xb3 , 0x5d ,
988
+ ] ;
989
+
990
+ let ( pkcs11, slot) = init_pins ( ) ;
991
+ let session = pkcs11. open_rw_session ( slot) ?;
992
+ session. login ( UserType :: User , Some ( & AuthPin :: new ( USER_PIN . into ( ) ) ) ) ?;
993
+
994
+ let template = [
995
+ Attribute :: Class ( ObjectClass :: SECRET_KEY ) ,
996
+ Attribute :: KeyType ( KeyType :: AES ) ,
997
+ Attribute :: Value ( key) ,
998
+ Attribute :: Encrypt ( true ) ,
999
+ ] ;
1000
+ let key_handle = session. create_object ( & template) ?;
1001
+ let mechanism = Mechanism :: AesGcm ( GcmParams :: new ( & iv, & aad, 96 . into ( ) ) ) ;
1002
+ let cipher_and_tag = session. encrypt ( & mechanism, key_handle, & plain) ?;
1003
+ assert_eq ! ( expected_cipher_and_tag[ ..] , cipher_and_tag[ ..] ) ;
1004
+ Ok ( ( ) )
1005
+ }
0 commit comments