Add missing error logs in crypto 57/305857/6
authorDariusz Michaluk <d.michaluk@samsung.com>
Fri, 9 Feb 2024 13:11:31 +0000 (14:11 +0100)
committerKrzysztof Małysa <k.malysa@samsung.com>
Mon, 12 Feb 2024 13:10:46 +0000 (13:10 +0000)
Change-Id: I736dcaaade26a3255b56a701b0c9cc2ef595ae94

srcs/crypto/encryptor.cpp
srcs/crypto/hkdf.cpp
srcs/crypto/hmac.cpp
srcs/crypto/random.cpp
srcs/crypto/sha2.cpp

index 9bf15788370e3b8e989ad7adad4b2692d6a00083..07bceb29845d5ff759141023a9a81a2bfcd09b67 100644 (file)
@@ -32,16 +32,26 @@ CryptoBuffer Crypt(bool encrypt, const CryptoBuffer &key, const CryptoBuffer &in
     int len;
 
     ctx = EVP_CIPHER_CTX_new();
-    if (!ctx)
+    if (!ctx) {
+        TRY_LOG_ERROR("EVP_CIPHER_CTX_new() error");
         goto opensslError;
-    if (EVP_CipherInit_ex(ctx, EVP_aes_256_ecb(), nullptr, key.data(), nullptr, encrypt) != 1)
+    }
+    if (EVP_CipherInit_ex(ctx, EVP_aes_256_ecb(), nullptr, key.data(), nullptr, encrypt) != 1) {
+        TRY_LOG_ERROR("EVP_CipherInit_ex() error");
         goto opensslError;
-    if (EVP_CIPHER_CTX_set_padding(ctx, 0) != 1)
+    }
+    if (EVP_CIPHER_CTX_set_padding(ctx, 0) != 1) {
+        TRY_LOG_ERROR("EVP_CIPHER_CTX_set_padding() error");
         goto opensslError;
-    if (EVP_CipherUpdate(ctx, output.data(), &len, input.data(), input.size()) != 1)
+    }
+    if (EVP_CipherUpdate(ctx, output.data(), &len, input.data(), input.size()) != 1) {
+        TRY_LOG_ERROR("EVP_CipherUpdate() error");
         goto opensslError;
-    if (EVP_CipherFinal_ex(ctx, output.data() + len, &len) != 1)
+    }
+    if (EVP_CipherFinal_ex(ctx, output.data() + len, &len) != 1) {
+        TRY_LOG_ERROR("EVP_CipherFinal_ex() error");
         goto opensslError;
+    }
 
     EVP_CIPHER_CTX_free(ctx);
     return output;
index b5e40cedef4b52288fc85fd9354cd8b9f67b26a0..d44e61bbb3e1c893094bd15038da51ace70e9718 100644 (file)
@@ -17,6 +17,7 @@
 #include "crypto/common.h"
 #include "crypto/hkdf.h"
 #include "crypto/openssl_error.h"
+#include "log/log.h"
 
 #include <openssl/evp.h>
 #include <openssl/hmac.h>
@@ -39,33 +40,53 @@ CryptoBuffer HkdfSha256(const CryptoBuffer &secret,
     size_t outLen = derivedKeySize;
 
     ctx = EVP_PKEY_CTX_new_id(EVP_PKEY_HKDF, nullptr);
-    if (!ctx)
+    if (!ctx) {
+        TRY_LOG_ERROR("EVP_PKEY_CTX_new_id() error");
         goto opensslError;
-    if (EVP_PKEY_derive_init(ctx) <= 0)
+    }
+    if (EVP_PKEY_derive_init(ctx) <= 0) {
+        TRY_LOG_ERROR("EVP_PKEY_derive_init() error");
         goto opensslError;
-    if (EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()) <= 0)
+    }
+    if (EVP_PKEY_CTX_set_hkdf_md(ctx, EVP_sha256()) <= 0) {
+        TRY_LOG_ERROR("EVP_PKEY_CTX_set_hkdf_md() error");
         goto opensslError;
+    }
     if (secret.empty()) {
         uint8_t key[EVP_MAX_MD_SIZE];
         unsigned int len;
         // TODO: before openssl 3.0 EVP_PKEY_CTX_set1_hkdf_key() does not allow zero-length argument
         // See issue: https://github.com/openssl/openssl/issues/8531
-        if (EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY) != 1)
+        if (EVP_PKEY_CTX_hkdf_mode(ctx, EVP_PKEY_HKDEF_MODE_EXPAND_ONLY) != 1) {
+            TRY_LOG_ERROR("EVP_PKEY_CTX_hkdf_mode() error");
             goto opensslError;
-        if (!HMAC(EVP_sha256(), salt.data(), salt.size(), nullptr, 0, key, &len))
+        }
+        if (!HMAC(EVP_sha256(), salt.data(), salt.size(), nullptr, 0, key, &len)) {
+            TRY_LOG_ERROR("HMAC() error");
             goto opensslError;
-        if (EVP_PKEY_CTX_set1_hkdf_key(ctx, key, len) != 1)
+        }
+        if (EVP_PKEY_CTX_set1_hkdf_key(ctx, key, len) != 1) {
+            TRY_LOG_ERROR("EVP_PKEY_CTX_set1_hkdf_key() error");
             goto opensslError;
+        }
     } else {
-        if (EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt.data(), salt.size()) <= 0)
+        if (EVP_PKEY_CTX_set1_hkdf_salt(ctx, salt.data(), salt.size()) <= 0) {
+            TRY_LOG_ERROR("EVP_PKEY_CTX_set1_hkdf_salt() error");
             goto opensslError;
-        if (EVP_PKEY_CTX_set1_hkdf_key(ctx, secret.data(), secret.size()) <= 0)
+        }
+        if (EVP_PKEY_CTX_set1_hkdf_key(ctx, secret.data(), secret.size()) <= 0) {
+            TRY_LOG_ERROR("EVP_PKEY_CTX_set1_hkdf_key() error");
             goto opensslError;
+        }
     }
-    if (EVP_PKEY_CTX_add1_hkdf_info(ctx, info.data(), info.size()) <= 0)
+    if (EVP_PKEY_CTX_add1_hkdf_info(ctx, info.data(), info.size()) <= 0) {
+        TRY_LOG_ERROR("EVP_PKEY_CTX_add1_hkdf_info() error");
         goto opensslError;
-    if (EVP_PKEY_derive(ctx, derivedKey.data(), &outLen) <= 0)
+    }
+    if (EVP_PKEY_derive(ctx, derivedKey.data(), &outLen) <= 0) {
+        TRY_LOG_ERROR("EVP_PKEY_derive() error");
         goto opensslError;
+    }
 
     EVP_PKEY_CTX_free(ctx);
     return derivedKey;
index 51ae5e1def797d6b4eb49d53440893da5036ec3a..84e0165d742823531481f8d7b34817488d4cd90e 100644 (file)
@@ -16,6 +16,7 @@
 #include "crypto/common.h"
 #include "crypto/hmac.h"
 #include "crypto/openssl_error.h"
+#include "log/log.h"
 
 #include <openssl/hmac.h>
 
@@ -33,6 +34,7 @@ CryptoBuffer HmacSha256(const CryptoBuffer &key, const CryptoBuffer &data)
               data.size(),
               digest.data(),
               &digestLen)) {
+        LogError("HMAC() error");
         throw OpensslError{};
     }
 
index e39afa0465b402771f4885407019dc57e59004b5..75567e46318af13808539ac339f549e6cc3ae2a2 100644 (file)
@@ -17,6 +17,7 @@
 #include "crypto/common.h"
 #include "crypto/openssl_error.h"
 #include "crypto/random.h"
+#include "log/log.h"
 
 #include <openssl/rand.h>
 
@@ -27,6 +28,7 @@ CryptoBuffer RandomBytes(size_t bytes)
     CryptoBuffer random(bytes);
 
     if (RAND_bytes(random.data(), random.size()) <= 0) {
+        LogError("RAND_bytes() error");
         throw OpensslError{};
     }
 
index c66d85700e7f6a88f3351a0c38460dc5856c678a..f1f4bdda28ffddb8eaeaf63c6b95e3243262c628 100644 (file)
@@ -17,6 +17,7 @@
 #include "crypto/common.h"
 #include "crypto/openssl_error.h"
 #include "crypto/sha2.h"
+#include "log/log.h"
 
 #include <openssl/sha.h>
 
@@ -27,6 +28,7 @@ CryptoBuffer Sha256Hash(const CryptoBuffer &data)
     CryptoBuffer digest(SHA256_DIGEST_LENGTH);
 
     if (!SHA256(data.data(), data.size(), digest.data())) {
+        LogError("SHA256() error");
         throw OpensslError{};
     }