From dcde0d0559d6eaa08aa02edf6d4e4f3eae9692d0 Mon Sep 17 00:00:00 2001 From: Jukka Laitinen Date: Wed, 9 Feb 2022 10:05:48 +0200 Subject: [PATCH] src/drivers/sw_crypto: Late initialize tomcypt This saves a lot of flash space, in case functions from libtomcrypt are not used (currently only RSA related). When RSA is not used, the linker can now drop all libtomcrypt related things. This is especially relevant for bootloaders using the SW crypto. Signed-off-by: Jukka Laitinen --- src/drivers/sw_crypto/crypto.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/drivers/sw_crypto/crypto.c b/src/drivers/sw_crypto/crypto.c index 05ecc47d26..13786b87ab 100644 --- a/src/drivers/sw_crypto/crypto.c +++ b/src/drivers/sw_crypto/crypto.c @@ -64,6 +64,12 @@ extern void libtomcrypt_init(void); */ static int crypto_open_count = 0; +/* + * Status of libtomcrypt initialization. This is a large library, which + * is initialized & pulled in by linker only when it is actually used + */ +static bool tomcrypt_initialized = false; + typedef struct { size_t key_size; uint8_t *key; @@ -76,6 +82,14 @@ typedef struct { uint64_t ctr; } chacha20_context_t; +static inline void initialize_tomcrypt(void) +{ + if (!tomcrypt_initialized) { + libtomcrypt_init(); + tomcrypt_initialized = true; + } +} + /* Clear key cache */ static void clear_key_cache(void) { @@ -135,7 +149,6 @@ void crypto_init() { keystore_init(); clear_key_cache(); - libtomcrypt_init(); } crypto_session_handle_t crypto_open(px4_crypto_algorithm_t algorithm) @@ -269,6 +282,8 @@ bool crypto_encrypt_data(crypto_session_handle_t handle, uint8_t *public_key = (uint8_t *)crypto_get_key_ptr(handle.keystore_handle, key_idx, &key_sz); *cipher_size = 0; + initialize_tomcrypt(); + if (public_key && rsa_import(public_key, key_sz, &key) == CRYPT_OK) { if (outlen >= ltc_mp.unsigned_size(key.N) && @@ -413,6 +428,8 @@ size_t crypto_get_min_blocksize(crypto_session_handle_t handle, uint8_t key_idx) 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);