From 631046b96227988ce8efeb30997781844474b9cf Mon Sep 17 00:00:00 2001 From: Jukka Laitinen Date: Mon, 20 Mar 2023 11:22:41 +0200 Subject: [PATCH] src/drivers/sw_crypto: Correct RSA lengths in the example sw crypto backend driver Maximum plaintext length was wrong, it is just k - 2 * hLen -2, where k is RSA key modulus length and hLen is hash length Also minimum block that can be encrypted didn't make sense for RSA-OAEP, it is just 1 byte, the rest will be padded. Signed-off-by: Jukka Laitinen --- src/drivers/sw_crypto/crypto.c | 42 +++++++++++++++------------------- 1 file changed, 19 insertions(+), 23 deletions(-) diff --git a/src/drivers/sw_crypto/crypto.c b/src/drivers/sw_crypto/crypto.c index e4ced4fd1a..ca0a23de8e 100644 --- a/src/drivers/sw_crypto/crypto.c +++ b/src/drivers/sw_crypto/crypto.c @@ -59,6 +59,10 @@ extern void libtomcrypt_init(void); #define SECMEM_FREE XFREE #endif +#define SHA256_HASHLEN 32 +#define OAEP_MAX_RSA_MODLEN 256 /* RSA2048 */ +#define OAEP_MAX_MSGLEN (OAEP_MAX_RSA_MODLEN - 2 * SHA256_HASHLEN - 2) + /* * For now, this is just a dummy up/down counter for tracking open/close calls */ @@ -382,12 +386,22 @@ bool crypto_get_encrypted_key(crypto_session_handle_t handle, max_len); } else { - // The key size, encrypted, is a multiple of minimum block size for the algorithm+key - size_t min_block = crypto_get_min_blocksize(handle, encryption_key_idx); - *max_len = key_sz / min_block * min_block; + switch (handle.algorithm) { - if (key_sz % min_block) { - *max_len += min_block; + case CRYPTO_RSA_OAEP: + /* The length is the RSA key modulus length, and the maximum plaintext + * length is calculated from that. This is now just fixed for RSA2048, + * but one could also parse the RSA key + * (encryption_key_idx) here and calculate the lengths. + */ + + *max_len = key_sz <= OAEP_MAX_MSGLEN ? OAEP_MAX_RSA_MODLEN : 0; + ret = true; + break; + + default: + *max_len = 0; + break; } } @@ -427,24 +441,6 @@ size_t crypto_get_min_blocksize(crypto_session_handle_t handle, uint8_t key_idx) ret = 64; break; - case CRYPTO_RSA_OAEP: { - rsa_key enc_key; - size_t pub_key_sz; - uint8_t *pub_key = (uint8_t *)crypto_get_key_ptr(handle.keystore_handle, key_idx, &pub_key_sz); - - initialize_tomcrypt(); - - if (pub_key && - rsa_import(pub_key, pub_key_sz, &enc_key) == CRYPT_OK) { - ret = ltc_mp.unsigned_size(enc_key.N); - rsa_free(&enc_key); - - } else { - ret = 0; - } - } - break; - default: ret = 1; }