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