From d9a8c579b60938372d1b4076ef7d272ecd6ba296 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Thu, 12 Mar 2026 12:06:18 +0100 Subject: [PATCH] Optimize random data generation. In the process of trying to bring down the time of sending an OMEMO encrypted message I noticed that a lot of time was spent in generating the random key and IV (~0.54s on my machine). By changing this to a single call to `gcry_random_bytes_secure()` I have been able to slash the time by 50% to ~0.27s. Signed-off-by: Steffen Jaeckel --- src/omemo/omemo.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/omemo/omemo.c b/src/omemo/omemo.c index 2c678fc1..bfd984a3 100644 --- a/src/omemo/omemo.c +++ b/src/omemo/omemo.c @@ -781,8 +781,8 @@ omemo_on_message_send(ProfWin* win, const char* const message, gboolean request_ tag = gcry_malloc_secure(tag_len); key_tag = gcry_malloc_secure(AES128_GCM_KEY_LENGTH + AES128_GCM_TAG_LENGTH); - key = gcry_random_bytes_secure(AES128_GCM_KEY_LENGTH, GCRY_VERY_STRONG_RANDOM); - iv = gcry_random_bytes_secure(AES128_GCM_IV_LENGTH, GCRY_VERY_STRONG_RANDOM); + key = gcry_random_bytes_secure(AES128_GCM_KEY_LENGTH + AES128_GCM_IV_LENGTH, GCRY_VERY_STRONG_RANDOM); + iv = key + AES128_GCM_KEY_LENGTH; res = aes128gcm_encrypt(ciphertext, &ciphertext_len, tag, &tag_len, (const unsigned char* const)message, strlen(message), iv, key); if (res != 0) { @@ -994,7 +994,6 @@ out: g_list_free_full(keys, (GDestroyNotify)omemo_key_free); free(ciphertext); gcry_free(key); - gcry_free(iv); gcry_free(tag); gcry_free(key_tag);