Skip to content

Commit

Permalink
Sw_crypto signature check: cleanup and free resources
Browse files Browse the repository at this point in the history
  • Loading branch information
kjyrinki-unikie committed Jan 13, 2025
1 parent 094b59d commit 09ca1e0
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
71 changes: 39 additions & 32 deletions src/drivers/sw_crypto/crypto.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/lib/crypto/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand Down

0 comments on commit 09ca1e0

Please sign in to comment.