@@ -186,6 +186,10 @@ int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
186186 if ((sz / WC_AES_BLOCK_SIZE ) > 0 ) {
187187 /* update IV */
188188 cmsg = CMSG_FIRSTHDR (& (aes -> msg ));
189+ if (cmsg == NULL ) {
190+ WOLFSSL_MSG ("CMSG_FIRSTHDR() in wc_AesCbcEncrypt() returned NULL unexpectedly." );
191+ return SYSLIB_FAILED_E ;
192+ }
189193 ret = wc_Afalg_SetIv (CMSG_NXTHDR (& (aes -> msg ), cmsg ),
190194 (byte * )(aes -> reg ), AES_IV_SIZE );
191195 if (ret < 0 ) {
@@ -245,6 +249,10 @@ int wc_AesSetKey(Aes* aes, const byte* userKey, word32 keylen,
245249 if ((sz / WC_AES_BLOCK_SIZE ) > 0 ) {
246250 /* update IV */
247251 cmsg = CMSG_FIRSTHDR (& (aes -> msg ));
252+ if (cmsg == NULL ) {
253+ WOLFSSL_MSG ("CMSG_FIRSTHDR() in wc_AesCbcDecrypt() returned NULL unexpectedly." );
254+ return SYSLIB_FAILED_E ;
255+ }
248256 ret = wc_Afalg_SetIv (CMSG_NXTHDR (& (aes -> msg ), cmsg ),
249257 (byte * )(aes -> reg ), AES_IV_SIZE );
250258 if (ret != 0 ) {
@@ -397,6 +405,10 @@ int wc_AesSetKeyDirect(Aes* aes, const byte* userKey, word32 keylen,
397405
398406 /* update IV */
399407 cmsg = CMSG_FIRSTHDR (& (aes -> msg ));
408+ if (cmsg == NULL ) {
409+ WOLFSSL_MSG ("CMSG_FIRSTHDR() in wc_AesCtrEncrypt() returned NULL unexpectedly." );
410+ return SYSLIB_FAILED_E ;
411+ }
400412 ret = wc_Afalg_SetIv (CMSG_NXTHDR (& (aes -> msg ), cmsg ),
401413 (byte * )(aes -> reg ), AES_IV_SIZE );
402414 if (ret < 0 ) {
@@ -613,7 +625,15 @@ int wc_AesGcmEncrypt(Aes* aes, byte* out, const byte* in, word32 sz,
613625
614626 msg = & (aes -> msg );
615627 cmsg = CMSG_FIRSTHDR (msg );
628+ if (cmsg == NULL ) {
629+ WOLFSSL_MSG ("CMSG_FIRSTHDR() in wc_AesGcmEncrypt() returned NULL unexpectedly." );
630+ return SYSLIB_FAILED_E ;
631+ }
616632 cmsg = CMSG_NXTHDR (msg , cmsg );
633+ if (cmsg == NULL ) {
634+ WOLFSSL_MSG ("CMSG_NEXTHDR() in wc_AesGcmEncrypt() returned NULL unexpectedly." );
635+ return SYSLIB_FAILED_E ;
636+ }
617637
618638 /* set IV and AAD size */
619639 ret = wc_Afalg_SetIv (cmsg , (byte * )iv , ivSz );
0 commit comments