diff --git a/src/drivers/sw_crypto/crypto.c b/src/drivers/sw_crypto/crypto.c index f528ebc45000..7d228b03c385 100644 --- a/src/drivers/sw_crypto/crypto.c +++ b/src/drivers/sw_crypto/crypto.c @@ -327,44 +327,51 @@ bool crypto_signature_check(crypto_session_handle_t handle, initialize_tomcrypt(); - if (public_key && rsa_import(public_key, keylen, &key) == CRYPT_OK) { + if (rsa_import(public_key, keylen, &key) == CRYPT_OK) { // Register hash algorithm. const struct ltc_hash_descriptor *hash_desc = &sha256_desc; const int hash_idx = register_hash(hash_desc); - if (hash_idx < 0) { - return false; - } + if (hash_idx >= 0) { + // Hash message. + unsigned char hash[32]; + hash_state md; + + if (hash_desc->init(&md) == CRYPT_OK + && hash_desc->process(&md, + (const unsigned char *) message, + (unsigned long) message_size) + == CRYPT_OK + && hash_desc->done(&md, hash) == CRYPT_OK) { + // Define padding scheme. + const int padding = LTC_PKCS_1_V1_5; + const unsigned long saltlen = 0; + + // Verify signature. + int stat = 0; + + if (rsa_verify_hash_ex(signature, + 256, + hash, + hash_desc->hashsize, + padding, + hash_idx, + saltlen, + &stat, + &key) + == CRYPT_OK + && stat) { + ret = true; + } + } - // Hash message. - unsigned char hash[32]; - hash_state md; - - hash_desc->init(&md); - hash_desc->process(&md, (const unsigned char *) message, (unsigned long) message_size); - hash_desc->done(&md, hash); - - // Define padding scheme. - const int padding = LTC_PKCS_1_V1_5; - const unsigned long saltlen = 0; - - // Verify signature. - int stat = 0; - - if (rsa_verify_hash_ex(signature, - 256, - hash, - hash_desc->hashsize, - padding, - hash_idx, - saltlen, - &stat, - &key) - == CRYPT_OK - && stat) { - ret = true; + // Clean up. + memset(hash, 0, sizeof(hash)); + memset(&md, 0, sizeof(md)); + unregister_hash(hash_desc); } + // Free RSA key. rsa_free(&key); } } @@ -690,7 +697,7 @@ bool crypto_decrypt_data(crypto_session_handle_t handle, if (key_sz == 32 && mac_size == 16 && *message_size >= cipher_size) { uint8_t sub_key[32]; crypto_hchacha20(sub_key, key, context->nonce); - bool mac_verified{false}; + bool mac_verified = false; if (mac) { uint8_t auth_key[64]; // "Wasting" the whole Chacha block is faster diff --git a/src/lib/crypto/CMakeLists.txt b/src/lib/crypto/CMakeLists.txt index 69302083d2a6..f9c454957b7d 100644 --- a/src/lib/crypto/CMakeLists.txt +++ b/src/lib/crypto/CMakeLists.txt @@ -72,6 +72,7 @@ libtomcrypt_wrappers.c libtomcrypt/src/misc/crypt/crypt_hash_is_valid.c libtomcrypt/src/misc/crypt/crypt_prng_is_valid.c libtomcrypt/src/misc/crypt/crypt_register_hash.c + libtomcrypt/src/misc/crypt/crypt_unregister_hash.c libtomcrypt/src/misc/mem_neq.c libtomcrypt/src/misc/zeromem.c )