@@ -6,10 +6,10 @@ use crate::common::{get_pkcs11, is_softhsm, SO_PIN, USER_PIN};
6
6
use common:: init_pins;
7
7
use cryptoki:: context:: Function ;
8
8
use cryptoki:: error:: { Error , RvError } ;
9
- use cryptoki:: mechanism:: aead:: GcmParams ;
9
+ use cryptoki:: mechanism:: aead:: { GcmMessageParams , GcmParams , GeneratorFunction } ;
10
10
use cryptoki:: mechanism:: eddsa:: { EddsaParams , EddsaSignatureScheme } ;
11
11
use cryptoki:: mechanism:: rsa:: { PkcsMgfType , PkcsOaepParams , PkcsOaepSource } ;
12
- use cryptoki:: mechanism:: { Mechanism , MechanismType } ;
12
+ use cryptoki:: mechanism:: { Mechanism , MechanismType , MessageParam } ;
13
13
use cryptoki:: object:: {
14
14
Attribute , AttributeInfo , AttributeType , KeyType , ObjectClass , ObjectHandle ,
15
15
} ;
@@ -1862,6 +1862,125 @@ fn aes_gcm_with_aad() -> TestResult {
1862
1862
Ok ( ( ) )
1863
1863
}
1864
1864
1865
+ #[ test]
1866
+ #[ serial]
1867
+ fn encrypt_decrypt_gcm_message_no_aad ( ) -> TestResult {
1868
+ let ( pkcs11, slot) = init_pins ( ) ;
1869
+ // PKCS#11 3.0 API is not supported by this token. Skip
1870
+ if !pkcs11. is_fn_supported ( Function :: MessageEncryptInit ) {
1871
+ /* return Ignore(); */
1872
+ print ! ( "SKIP: The PKCS#11 module does not support message based encryption" ) ;
1873
+ return Ok ( ( ) ) ;
1874
+ }
1875
+
1876
+ let session = pkcs11. open_rw_session ( slot) ?;
1877
+ session. login ( UserType :: User , Some ( & AuthPin :: new ( USER_PIN . into ( ) ) ) ) ?;
1878
+
1879
+ // The same input as in aes_gcm_no_aad()
1880
+ let key = vec ! [ 0 ; 16 ] ;
1881
+ let mut iv = [ 0 ; 12 ] ;
1882
+ let mut tag = [ 0 ; 12 ] ;
1883
+ let aad = [ ] ;
1884
+ let plain = [ 0 ; 32 ] ;
1885
+ let expected_cipher = [
1886
+ 0x03 , 0x88 , 0xda , 0xce , 0x60 , 0xb6 , 0xa3 , 0x92 , 0xf3 , 0x28 , 0xc2 , 0xb9 , 0x71 , 0xb2 , 0xfe ,
1887
+ 0x78 , 0xf7 , 0x95 , 0xaa , 0xab , 0x49 , 0x4b , 0x59 , 0x23 , 0xf7 , 0xfd , 0x89 , 0xff , 0x94 , 0x8b ,
1888
+ 0xc1 , 0xe0 ,
1889
+ ] ;
1890
+ let expected_tag = [
1891
+ 0x40 , 0x49 , 0x0a , 0xf4 , 0x80 , 0x56 , 0x06 , 0xb2 , 0xa3 , 0xa2 , 0xe7 , 0x93 ,
1892
+ ] ;
1893
+
1894
+ let template = [
1895
+ Attribute :: Class ( ObjectClass :: SECRET_KEY ) ,
1896
+ Attribute :: KeyType ( KeyType :: AES ) ,
1897
+ Attribute :: Value ( key) ,
1898
+ Attribute :: Encrypt ( true ) ,
1899
+ Attribute :: Decrypt ( true ) ,
1900
+ ] ;
1901
+ let key_handle = session. create_object ( & template) ?;
1902
+
1903
+ let param = GcmMessageParams :: new ( & mut iv, 96 . into ( ) , GeneratorFunction :: NoGenerate , & mut tag) ?;
1904
+ let mechanism = Mechanism :: AesGcmMessage ( param) ;
1905
+ session. message_encrypt_init ( & mechanism, key_handle) ?;
1906
+
1907
+ let param2 = MessageParam :: AesGcmMessage ( param) ;
1908
+ let cipher = session. encrypt_message ( & param2, & aad, & plain) ?;
1909
+ assert_eq ! ( expected_cipher[ ..] , cipher[ ..] ) ;
1910
+ assert_eq ! ( expected_tag[ ..] , tag[ ..] ) ;
1911
+ session. message_encrypt_final ( ) ?;
1912
+
1913
+ /* Do also decryption */
1914
+ let param = GcmMessageParams :: new ( & mut iv, 96 . into ( ) , GeneratorFunction :: NoGenerate , & mut tag) ?;
1915
+ let mechanism = Mechanism :: AesGcmMessage ( param) ;
1916
+ session. message_decrypt_init ( & mechanism, key_handle) ?;
1917
+
1918
+ let param2 = MessageParam :: AesGcmMessage ( param) ;
1919
+ let plain_decrypted = session. decrypt_message ( & param2, & aad, & cipher) ?;
1920
+ assert_eq ! ( plain_decrypted[ ..] , plain[ ..] ) ;
1921
+ session. message_decrypt_final ( ) ?;
1922
+ Ok ( ( ) )
1923
+ }
1924
+
1925
+ #[ test]
1926
+ #[ serial]
1927
+ fn encrypt_decrypt_gcm_message_with_aad ( ) -> TestResult {
1928
+ let ( pkcs11, slot) = init_pins ( ) ;
1929
+ // PKCS#11 3.0 API is not supported by this token. Skip
1930
+ if !pkcs11. is_fn_supported ( Function :: MessageEncryptInit ) {
1931
+ /* return Ignore(); */
1932
+ print ! ( "SKIP: The PKCS#11 module does not support message based encryption" ) ;
1933
+ return Ok ( ( ) ) ;
1934
+ }
1935
+
1936
+ let session = pkcs11. open_rw_session ( slot) ?;
1937
+ session. login ( UserType :: User , Some ( & AuthPin :: new ( USER_PIN . into ( ) ) ) ) ?;
1938
+
1939
+ // The same input as in aes_gcm_with_aad()
1940
+ let key = vec ! [ 0 ; 16 ] ;
1941
+ let mut iv = [ 0 ; 12 ] ;
1942
+ let mut tag = [ 0 ; 12 ] ;
1943
+ let aad = [ 0 ; 16 ] ;
1944
+ let plain = [ 0 ; 16 ] ;
1945
+ let expected_cipher = [
1946
+ 0x03 , 0x88 , 0xda , 0xce , 0x60 , 0xb6 , 0xa3 , 0x92 , 0xf3 , 0x28 , 0xc2 , 0xb9 , 0x71 , 0xb2 , 0xfe ,
1947
+ 0x78 ,
1948
+ ] ;
1949
+ let expected_tag = [
1950
+ 0xd2 , 0x4e , 0x50 , 0x3a , 0x1b , 0xb0 , 0x37 , 0x07 , 0x1c , 0x71 , 0xb3 , 0x5d ,
1951
+ ] ;
1952
+
1953
+ let template = [
1954
+ Attribute :: Class ( ObjectClass :: SECRET_KEY ) ,
1955
+ Attribute :: KeyType ( KeyType :: AES ) ,
1956
+ Attribute :: Value ( key) ,
1957
+ Attribute :: Encrypt ( true ) ,
1958
+ Attribute :: Decrypt ( true ) ,
1959
+ ] ;
1960
+ let key_handle = session. create_object ( & template) ?;
1961
+
1962
+ let param = GcmMessageParams :: new ( & mut iv, 96 . into ( ) , GeneratorFunction :: NoGenerate , & mut tag) ?;
1963
+ let mechanism = Mechanism :: AesGcmMessage ( param) ;
1964
+ session. message_encrypt_init ( & mechanism, key_handle) ?;
1965
+
1966
+ let param2 = MessageParam :: AesGcmMessage ( param) ;
1967
+ let cipher = session. encrypt_message ( & param2, & aad, & plain) ?;
1968
+ assert_eq ! ( expected_cipher[ ..] , cipher[ ..] ) ;
1969
+ assert_eq ! ( expected_tag[ ..] , tag[ ..] ) ;
1970
+ session. message_encrypt_final ( ) ?;
1971
+
1972
+ /* Do also decryption */
1973
+ let param = GcmMessageParams :: new ( & mut iv, 96 . into ( ) , GeneratorFunction :: NoGenerate , & mut tag) ?;
1974
+ let mechanism = Mechanism :: AesGcmMessage ( param) ;
1975
+ session. message_decrypt_init ( & mechanism, key_handle) ?;
1976
+
1977
+ let param2 = MessageParam :: AesGcmMessage ( param) ;
1978
+ let plain_decrypted = session. decrypt_message ( & param2, & aad, & cipher) ?;
1979
+ assert_eq ! ( plain_decrypted[ ..] , plain[ ..] ) ;
1980
+ session. message_decrypt_final ( ) ?;
1981
+ Ok ( ( ) )
1982
+ }
1983
+
1865
1984
#[ test]
1866
1985
#[ serial]
1867
1986
fn rsa_pkcs_oaep_empty ( ) -> TestResult {
0 commit comments