]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
siv: set key directly with gnutls
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 11 May 2022 06:57:22 +0000 (08:57 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Wed, 11 May 2022 10:22:33 +0000 (12:22 +0200)
A new function is provided by the latest gnutls (should be in 3.7.5) to
set the key of an AEAD cipher. If available, use it to avoid destroying
and creating a new SIV instance with each key change.

This improves the server NTS-NTP performance if using gnutls for SIV.

configure
siv_gnutls.c

index 8041d4e4631d0eb7c7af1518ed0e8d976c300595..73d186735676c42e55ebdb7a837b1fc6880182bb 100755 (executable)
--- a/configure
+++ b/configure
@@ -988,6 +988,12 @@ if [ $feat_ntp = "1" ] && [ $feat_nts = "1" ] && [ $try_gnutls = "1" ]; then
       then
         EXTRA_OBJECTS="$EXTRA_OBJECTS siv_gnutls.o"
         add_def HAVE_SIV
+        if test_code 'gnutls_aead_cipher_set_key()' 'gnutls/crypto.h' \
+          "$test_cflags" "$test_link $LIBS" '
+            return gnutls_aead_cipher_set_key(NULL, NULL);'
+        then
+          add_def HAVE_GNUTLS_AEAD_CIPHER_SET_KEY
+        fi
       else
         if test_code 'AES128 in nettle' 'nettle/aes.h' '' "$LIBS" \
           'aes128_set_encrypt_key(NULL, NULL);'
index 437f7151ebe77314bc5312ee835b8352921523f0..aba2babf430dd83f4707aae845267d69b5baabe5 100644 (file)
@@ -165,17 +165,29 @@ SIV_SetKey(SIV_Instance instance, const unsigned char *key, int length)
   datum.data = (unsigned char *)key;
   datum.size = length;
 
-  /* Initialise a new cipher with the provided key (gnutls does not seem to
-     have a function to change the key directly) */
+#ifdef HAVE_GNUTLS_AEAD_CIPHER_SET_KEY
+  if (instance->cipher) {
+    r = gnutls_aead_cipher_set_key(instance->cipher, &datum);
+    if (r < 0) {
+      DEBUG_LOG("Could not set cipher key : %s", gnutls_strerror(r));
+      return 0;
+    }
+
+    return 1;
+  }
+#endif
+
+  /* Initialise a new cipher with the provided key */
   r = gnutls_aead_cipher_init(&cipher, instance->algorithm, &datum);
   if (r < 0) {
     DEBUG_LOG("Could not initialise %s : %s", "cipher", gnutls_strerror(r));
     return 0;
   }
 
-  /* Replace the previous cipher */
+  /* Destroy the previous cipher (if its key could not be changed directly) */
   if (instance->cipher)
     gnutls_aead_cipher_deinit(instance->cipher);
+
   instance->cipher = cipher;
 
   return 1;