Fix coverity CID #1452770 - Dereference before NULL check in CRYPTO_siv128_init()
authorShane Lontis <shane.lontis@oracle.com>
Tue, 11 Aug 2020 07:50:13 +0000 (17:50 +1000)
committerShane Lontis <shane.lontis@oracle.com>
Mon, 24 Aug 2020 01:19:28 +0000 (11:19 +1000)
Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/12628)

crypto/modes/siv128.c

index 27e29c3cc614c4ca89d6907570123f178fb8f4fd..aa924efae7ee41fb63069aa319443795e7fe93f7 100644 (file)
@@ -168,13 +168,10 @@ int CRYPTO_siv128_init(SIV128_CONTEXT *ctx, const unsigned char *key, int klen,
     size_t out_len = SIV_LEN;
     EVP_MAC_CTX *mac_ctx = NULL;
     OSSL_PARAM params[3];
-    const char *cbc_name = EVP_CIPHER_name(cbc);
+    const char *cbc_name;
 
-    params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
-                                                 (char *)cbc_name, 0);
-    params[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
-                                                  (void *)key, klen);
-    params[2] = OSSL_PARAM_construct_end();
+    if (ctx == NULL)
+        return 0;
 
     memset(&ctx->d, 0, sizeof(ctx->d));
     EVP_CIPHER_CTX_free(ctx->cipher_ctx);
@@ -184,8 +181,17 @@ int CRYPTO_siv128_init(SIV128_CONTEXT *ctx, const unsigned char *key, int klen,
     ctx->cipher_ctx = NULL;
     ctx->mac_ctx_init = NULL;
 
-    if (key == NULL || cbc == NULL || ctr == NULL
-            || (ctx->cipher_ctx = EVP_CIPHER_CTX_new()) == NULL
+    if (key == NULL || cbc == NULL || ctr == NULL)
+        return 0;
+
+    cbc_name = EVP_CIPHER_name(cbc);
+    params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_CIPHER,
+                                                 (char *)cbc_name, 0);
+    params[1] = OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY,
+                                                  (void *)key, klen);
+    params[2] = OSSL_PARAM_construct_end();
+
+    if ((ctx->cipher_ctx = EVP_CIPHER_CTX_new()) == NULL
             || (ctx->mac =
                 EVP_MAC_fetch(libctx, OSSL_MAC_NAME_CMAC, propq)) == NULL
             || (ctx->mac_ctx_init = EVP_MAC_CTX_new(ctx->mac)) == NULL