@@ -666,8 +666,8 @@ static int wp_aead_set_ctx_params(wp_AeadCtx* ctx, const OSSL_PARAM params[])
666666 ok = wp_aead_set_param_tls1_iv_fixed (ctx , params );
667667 }
668668 else if (ok && (ctx -> mode == EVP_CIPH_GCM_MODE ) &&
669- (XMEMCMP (params -> key , OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED ,
670- sizeof (OSSL_CIPHER_PARAM_AEAD_TLS1_IV_FIXED )) == 0 )) {
669+ (XMEMCMP (params -> key , OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV ,
670+ sizeof (OSSL_CIPHER_PARAM_AEAD_TLS1_SET_IV_INV )) == 0 )) {
671671 ok = wp_aead_set_param_tls1_iv_rand (ctx , params );
672672 }
673673
@@ -925,7 +925,12 @@ static int wp_aesgcm_set_rand_iv(wp_AeadCtx *ctx, unsigned char *in,
925925 XMEMCPY (ctx -> origIv , ctx -> iv , ctx -> ivLen );
926926#endif
927927 XMEMCPY (ctx -> iv + ctx -> ivLen - inLen , in , inLen );
928+ #ifdef WOLFSSL_AESGCM_STREAM
929+ /* Stream update initializes AES-GCM when IV state is buffered. */
930+ ctx -> ivState = IV_STATE_BUFFERED ;
931+ #else
928932 ctx -> ivState = IV_STATE_COPIED ;
933+ #endif
929934 }
930935
931936 WOLFPROV_LEAVE (WP_LOG_COMP_AES , __FILE__ ":" WOLFPROV_STRINGIZE (__LINE__ ), ok );
@@ -997,69 +1002,72 @@ static int wp_aesgcm_tls_iv_set_fixed(wp_AeadCtx* ctx, unsigned char* iv,
9971002}
9981003
9991004/**
1000- * Initialize AES GCM cipher for encryption.
1001- *
1002- * Sets the parameters as well as key and IV/nonce.
1005+ * Initialize AES GCM key and IV/nonce state.
10031006 *
10041007 * @param [in, out] ctx AEAD context object.
1005- * @param [in] key Private key to initialize with. May be NULL.
1008+ * @param [in] key Key to initialize with. May be NULL.
10061009 * @param [in] keyLen Length of key in bytes.
10071010 * @param [in] iv IV/nonce to initialize with. May be NULL.
10081011 * @param [in] ivLen Length of IV/nonce in bytes.
1009- * @param [in] params Array of parameters and values .
1012+ * @param [in] enc 1 for encryption, 0 for decryption .
10101013 * @return 1 on success.
10111014 * @return 0 on failure.
10121015 */
1013- static int wp_aesgcm_einit (wp_AeadCtx * ctx , const unsigned char * key ,
1014- size_t keyLen , const unsigned char * iv , size_t ivLen ,
1015- const OSSL_PARAM params [])
1016+ static int wp_aesgcm_init_key_iv (wp_AeadCtx * ctx , const unsigned char * key ,
1017+ size_t keyLen , const unsigned char * iv , size_t ivLen , int enc )
10161018{
10171019 Aes * aes = & ctx -> aes ;
10181020 int ok = 1 ;
1021+ int rc ;
10191022
1020- WOLFPROV_ENTER (WP_LOG_COMP_AES , "wp_aesgcm_einit" );
1021-
1022- if (!wolfssl_prov_is_running ()) {
1023- ok = 0 ;
1024- }
1025- if (ok ) {
1026- WP_CHECK_FIPS_ALGO (WP_CAST_ALGO_AES );
1027- }
10281023#ifdef WOLFSSL_AESGCM_STREAM
1029- if (ok ) {
1030- int rc ;
1024+ if (iv != NULL ) {
1025+ if (ivLen == 0 ) {
1026+ ok = 0 ;
1027+ }
1028+ if (ok ) {
1029+ XMEMCPY (ctx -> iv , iv , ivLen );
1030+ ctx -> ivState = IV_STATE_BUFFERED ;
1031+ ctx -> ivSet = 0 ;
1032+ ctx -> ivLen = ivLen ;
1033+ }
1034+ }
10311035
1032- if (iv != NULL ) {
1033- if (ivLen == 0 ) {
1036+ if (ok && (key != NULL )) {
1037+ if ((iv == NULL ) || (ivLen == 0 )) {
1038+ rc = wc_AesGcmSetKey (aes , key , (word32 )keyLen );
1039+ if (rc != 0 ) {
1040+ WOLFPROV_MSG_DEBUG_RETCODE (WP_LOG_LEVEL_DEBUG ,
1041+ "wc_AesGcmSetKey" , rc );
10341042 ok = 0 ;
10351043 }
1036- if (ok ) {
1037- XMEMCPY (ctx -> iv , iv , ivLen );
1038- ctx -> ivState = IV_STATE_BUFFERED ;
1039- ctx -> ivSet = 0 ;
1040- ctx -> ivLen = ivLen ;
1041- }
10421044 }
1043- if ((ivLen == 0 ) && (key != NULL )) {
1044- rc = wc_AesGcmSetKey (aes , key , (word32 )keyLen );
1045+ else if (enc ) {
1046+ rc = wc_AesGcmEncryptInit (aes , key , (word32 )keyLen , iv ,
1047+ (word32 )ivLen );
10451048 if (rc != 0 ) {
1046- WOLFPROV_MSG_DEBUG_RETCODE (WP_LOG_LEVEL_DEBUG , "wc_AesGcmSetKey" , rc );
1049+ WOLFPROV_MSG_DEBUG_RETCODE (WP_LOG_LEVEL_DEBUG ,
1050+ "wc_AesGcmEncryptInit" , rc );
10471051 ok = 0 ;
10481052 }
10491053 }
1050- else if (key != NULL ) {
1051- rc = wc_AesGcmEncryptInit (aes , key , (word32 )keyLen , iv , (word32 )ivLen );
1054+ else {
1055+ rc = wc_AesGcmDecryptInit (aes , key , (word32 )keyLen , iv ,
1056+ (word32 )ivLen );
10521057 if (rc != 0 ) {
1053- WOLFPROV_MSG_DEBUG_RETCODE (WP_LOG_LEVEL_DEBUG , "wc_AesGcmEncryptInit" , rc );
1058+ WOLFPROV_MSG_DEBUG_RETCODE (WP_LOG_LEVEL_DEBUG ,
1059+ "wc_AesGcmDecryptInit" , rc );
10541060 ok = 0 ;
10551061 }
10561062 }
10571063 }
10581064#else
1059- if (ok && (key != NULL )) {
1060- int rc = wc_AesGcmSetKey (aes , key , (word32 )keyLen );
1065+ (void )enc ;
1066+ if (key != NULL ) {
1067+ rc = wc_AesGcmSetKey (aes , key , (word32 )keyLen );
10611068 if (rc != 0 ) {
1062- WOLFPROV_MSG_DEBUG_RETCODE (WP_LOG_LEVEL_DEBUG , "wc_AesGcmSetKey" , rc );
1069+ WOLFPROV_MSG_DEBUG_RETCODE (WP_LOG_LEVEL_DEBUG , "wc_AesGcmSetKey" ,
1070+ rc );
10631071 ok = 0 ;
10641072 }
10651073 }
@@ -1074,6 +1082,41 @@ static int wp_aesgcm_einit(wp_AeadCtx* ctx, const unsigned char *key,
10741082 }
10751083 }
10761084#endif
1085+
1086+ return ok ;
1087+ }
1088+
1089+ /**
1090+ * Initialize AES GCM cipher for encryption.
1091+ *
1092+ * Sets the parameters as well as key and IV/nonce.
1093+ *
1094+ * @param [in, out] ctx AEAD context object.
1095+ * @param [in] key Private key to initialize with. May be NULL.
1096+ * @param [in] keyLen Length of key in bytes.
1097+ * @param [in] iv IV/nonce to initialize with. May be NULL.
1098+ * @param [in] ivLen Length of IV/nonce in bytes.
1099+ * @param [in] params Array of parameters and values.
1100+ * @return 1 on success.
1101+ * @return 0 on failure.
1102+ */
1103+ static int wp_aesgcm_einit (wp_AeadCtx * ctx , const unsigned char * key ,
1104+ size_t keyLen , const unsigned char * iv , size_t ivLen ,
1105+ const OSSL_PARAM params [])
1106+ {
1107+ int ok = 1 ;
1108+
1109+ WOLFPROV_ENTER (WP_LOG_COMP_AES , "wp_aesgcm_einit" );
1110+
1111+ if (!wolfssl_prov_is_running ()) {
1112+ ok = 0 ;
1113+ }
1114+ if (ok ) {
1115+ WP_CHECK_FIPS_ALGO (WP_CAST_ALGO_AES );
1116+ }
1117+ if (ok ) {
1118+ ok = wp_aesgcm_init_key_iv (ctx , key , keyLen , iv , ivLen , 1 );
1119+ }
10771120 if (ok ) {
10781121 ctx -> enc = 1 ;
10791122 ctx -> keySet |= (key != NULL );
@@ -1103,7 +1146,6 @@ static int wp_aesgcm_dinit(wp_AeadCtx *ctx, const unsigned char *key,
11031146 size_t keyLen , const unsigned char * iv , size_t ivLen ,
11041147 const OSSL_PARAM params [])
11051148{
1106- Aes * aes = & ctx -> aes ;
11071149 int ok = 1 ;
11081150
11091151 WOLFPROV_ENTER (WP_LOG_COMP_AES , "wp_aesgcm_dinit" );
@@ -1114,38 +1156,9 @@ static int wp_aesgcm_dinit(wp_AeadCtx *ctx, const unsigned char *key,
11141156 if (ok ) {
11151157 WP_CHECK_FIPS_ALGO (WP_CAST_ALGO_AES );
11161158 }
1117- #ifdef WOLFSSL_AESGCM_STREAM
1118- if (ok && key != NULL ) {
1119- int rc = wc_AesGcmDecryptInit (aes , key , (word32 )keyLen , iv , (word32 )ivLen );
1120- if (rc != 0 ) {
1121- WOLFPROV_MSG_DEBUG_RETCODE (WP_LOG_LEVEL_DEBUG , "wc_AesGcmDecryptInit" , rc );
1122- ok = 0 ;
1123- }
1124- }
11251159 if (ok ) {
1126- XMEMCPY (ctx -> iv , iv , ivLen );
1127- ctx -> ivState = IV_STATE_BUFFERED ;
1128- ctx -> ivSet = 0 ;
1129- }
1130- #else
1131- if (ok && (key != NULL )) {
1132- int rc = wc_AesGcmSetKey (aes , key , (word32 )keyLen );
1133- if (rc != 0 ) {
1134- WOLFPROV_MSG_DEBUG_RETCODE (WP_LOG_LEVEL_DEBUG , "wc_AesGcmSetKey" , rc );
1135- ok = 0 ;
1136- }
1137- }
1138- if (ok && (iv != NULL )) {
1139- if (ivLen != ctx -> ivLen ) {
1140- ok = 0 ;
1141- }
1142- if (ok ) {
1143- XMEMCPY (ctx -> iv , iv , ivLen );
1144- ctx -> ivState = IV_STATE_BUFFERED ;
1145- ctx -> ivSet = 0 ;
1146- }
1160+ ok = wp_aesgcm_init_key_iv (ctx , key , keyLen , iv , ivLen , 0 );
11471161 }
1148- #endif
11491162 if (ok ) {
11501163 ctx -> enc = 0 ;
11511164 ctx -> keySet |= (key != NULL );
0 commit comments