@@ -1042,6 +1042,160 @@ int test_aes128_gcm_tls(void *data)
10421042 EVP_GCM_TLS_FIXED_IV_LEN , 0 );
10431043}
10441044
1045+ /******************************************************************************/
1046+
1047+ /* Test that OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV correctly sets the
1048+ * explicit/random portion of the IV on the decrypt side via the OSSL_PARAM
1049+ * interface. This exercises the fix in wp_aead_set_ctx_params where the
1050+ * parameter key comparison was corrected from AEAD_TLS1_IV_FIXED to
1051+ * AEAD_TLS1_SET_IV_INV. */
1052+ static int test_aes_gcm_set_iv_inv_dec (const EVP_CIPHER * cipher ,
1053+ unsigned char * key , unsigned char * iv , int ivFixedLen , int ivLen ,
1054+ unsigned char * aad , unsigned char * msg , int len ,
1055+ unsigned char * enc , unsigned char * tag , unsigned char * dec )
1056+ {
1057+ int err ;
1058+ EVP_CIPHER_CTX * ctx ;
1059+ int decLen ;
1060+ unsigned int tagLen = 16 ;
1061+ OSSL_PARAM params [2 ];
1062+
1063+ err = (ctx = EVP_CIPHER_CTX_new ()) == NULL ;
1064+ /* Init decrypt with key. */
1065+ if (err == 0 ) {
1066+ err = EVP_DecryptInit (ctx , cipher , key , NULL ) != 1 ;
1067+ }
1068+ /* Set the fixed IV portion - this also sets ivGen. */
1069+ if (err == 0 ) {
1070+ err = EVP_CIPHER_CTX_ctrl (ctx , EVP_CTRL_GCM_SET_IV_FIXED ,
1071+ ivFixedLen , iv ) != 1 ;
1072+ }
1073+ /* Use OSSL_PARAM AEAD_TLS1_SET_IV_INV to set the explicit/random part
1074+ * of the IV from the encrypt side. This is the code path fixed by the
1075+ * commit. */
1076+ if (err == 0 ) {
1077+ params [0 ] = OSSL_PARAM_construct_octet_string (
1078+ OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV ,
1079+ (void * )(iv + ivFixedLen ), ivLen - ivFixedLen );
1080+ params [1 ] = OSSL_PARAM_construct_end ();
1081+ err = EVP_CIPHER_CTX_set_params (ctx , params ) != 1 ;
1082+ }
1083+ /* Set tag for verification. */
1084+ if (err == 0 ) {
1085+ err = EVP_CIPHER_CTX_ctrl (ctx , EVP_CTRL_AEAD_SET_TAG , tagLen ,
1086+ (void * )tag ) != 1 ;
1087+ }
1088+ /* AAD. */
1089+ if (err == 0 ) {
1090+ err = EVP_DecryptUpdate (ctx , NULL , & decLen , aad ,
1091+ (int )strlen ((char * )aad )) != 1 ;
1092+ }
1093+ /* Decrypt. */
1094+ if (err == 0 ) {
1095+ err = EVP_DecryptUpdate (ctx , dec , & decLen , enc , len ) != 1 ;
1096+ }
1097+ if (err == 0 ) {
1098+ err = EVP_DecryptFinal_ex (ctx , dec + decLen , & decLen ) != 1 ;
1099+ }
1100+ if (err == 0 && dec != NULL && msg != NULL ) {
1101+ PRINT_BUFFER ("Decrypted" , dec , len );
1102+ if (memcmp (dec , msg , len ) != 0 ) {
1103+ err = 1 ;
1104+ }
1105+ }
1106+
1107+ EVP_CIPHER_CTX_free (ctx );
1108+ return err ;
1109+ }
1110+
1111+ static int test_aes_gcm_set_iv_inv (void * data , const char * cipher ,
1112+ int keyLen , int ivFixedLen , int ivLen )
1113+ {
1114+ int err = 0 ;
1115+ unsigned char msg [] = "Test pattern" ;
1116+ unsigned char key [32 ];
1117+ unsigned char iv [12 ];
1118+ unsigned char aad [] = "AAD" ;
1119+ unsigned char enc [sizeof (msg )];
1120+ unsigned char tag [AES_BLOCK_SIZE ];
1121+ unsigned char dec [sizeof (msg )];
1122+ EVP_CIPHER * ocipher ;
1123+ EVP_CIPHER * wcipher ;
1124+
1125+ (void )data ;
1126+
1127+ ocipher = EVP_CIPHER_fetch (osslLibCtx , cipher , "" );
1128+ wcipher = EVP_CIPHER_fetch (wpLibCtx , cipher , "" );
1129+
1130+ if (RAND_bytes (key , keyLen ) == 0 ) {
1131+ err = 1 ;
1132+ }
1133+ if (err == 0 ) {
1134+ if (RAND_bytes (iv , sizeof (iv )) == 0 ) {
1135+ err = 1 ;
1136+ }
1137+ }
1138+
1139+ if (err == 0 ) {
1140+ PRINT_BUFFER ("Key" , key , keyLen );
1141+ PRINT_BUFFER ("IV" , iv , ivLen );
1142+ PRINT_BUFFER ("Message" , msg , sizeof (msg ));
1143+ }
1144+
1145+ /* Encrypt with OpenSSL using fixed IV, decrypt with wolfProvider
1146+ * using OSSL_PARAM SET_IV_INV. */
1147+ if (err == 0 ) {
1148+ PRINT_MSG ("Encrypt with OpenSSL (fixed IV)" );
1149+ err = test_aes_tag_fixed_enc (ocipher , key , iv , ivFixedLen , ivLen ,
1150+ aad , msg , sizeof (msg ), enc , tag );
1151+ }
1152+ if (err == 0 ) {
1153+ PRINT_MSG ("Decrypt with wolfprovider (SET_IV_INV via OSSL_PARAM)" );
1154+ err = test_aes_gcm_set_iv_inv_dec (wcipher , key , iv , ivFixedLen , ivLen ,
1155+ aad , msg , sizeof (msg ), enc , tag ,
1156+ dec );
1157+ }
1158+
1159+ /* Encrypt with wolfProvider using fixed IV, decrypt with wolfProvider
1160+ * using OSSL_PARAM SET_IV_INV. */
1161+ if (err == 0 ) {
1162+ PRINT_MSG ("Encrypt with wolfprovider (fixed IV)" );
1163+ err = test_aes_tag_fixed_enc (wcipher , key , iv , ivFixedLen , ivLen ,
1164+ aad , msg , sizeof (msg ), enc , tag );
1165+ }
1166+ if (err == 0 ) {
1167+ PRINT_MSG ("Decrypt with wolfprovider (SET_IV_INV via OSSL_PARAM)" );
1168+ err = test_aes_gcm_set_iv_inv_dec (wcipher , key , iv , ivFixedLen , ivLen ,
1169+ aad , msg , sizeof (msg ), enc , tag ,
1170+ dec );
1171+ }
1172+
1173+ /* Encrypt with wolfProvider using fixed IV, decrypt with OpenSSL
1174+ * using OSSL_PARAM SET_IV_INV. */
1175+ if (err == 0 ) {
1176+ PRINT_MSG ("Encrypt with wolfprovider (fixed IV)" );
1177+ err = test_aes_tag_fixed_enc (wcipher , key , iv , ivFixedLen , ivLen ,
1178+ aad , msg , sizeof (msg ), enc , tag );
1179+ }
1180+ if (err == 0 ) {
1181+ PRINT_MSG ("Decrypt with OpenSSL (SET_IV_INV via OSSL_PARAM)" );
1182+ err = test_aes_gcm_set_iv_inv_dec (ocipher , key , iv , ivFixedLen , ivLen ,
1183+ aad , msg , sizeof (msg ), enc , tag ,
1184+ dec );
1185+ }
1186+
1187+ EVP_CIPHER_free (wcipher );
1188+ EVP_CIPHER_free (ocipher );
1189+
1190+ return err ;
1191+ }
1192+
1193+ int test_aes128_gcm_set_iv_inv (void * data )
1194+ {
1195+ return test_aes_gcm_set_iv_inv (data , "AES-128-GCM" , 16 ,
1196+ EVP_GCM_TLS_FIXED_IV_LEN , 12 );
1197+ }
1198+
10451199#endif /* WP_HAVE_AESGCM */
10461200
10471201/******************************************************************************/
0 commit comments