]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
siv: return error if key is not set
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 9 Sep 2020 12:00:32 +0000 (14:00 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Thu, 10 Sep 2020 07:36:35 +0000 (09:36 +0200)
Avoid encryption or decryption using uninitialized data, or causing a
crash, if a key was not set for the SIV instance.

siv_gnutls.c
siv_nettle.c
test/unit/siv.c

index bc93f015de75b53b1e4bb83bb87399d04427f2fb..77942ece60c5adb02a37beadcd1d9bbb681659d0 100644 (file)
@@ -204,6 +204,9 @@ SIV_Encrypt(SIV_Instance instance,
 {
   size_t clen = ciphertext_length;
 
+  if (!instance->cipher)
+    return 0;
+
   if (nonce_length < 1 || assoc_length < 0 ||
       plaintext_length < 0 || ciphertext_length < 0)
     return 0;
@@ -232,6 +235,9 @@ SIV_Decrypt(SIV_Instance instance,
 {
   size_t plen = plaintext_length;
 
+  if (!instance->cipher)
+    return 0;
+
   if (nonce_length < 1 || assoc_length < 0 ||
       plaintext_length < 0 || ciphertext_length < 0)
     return 0;
index 43a84b808e04b046d2ece0125613103703bd6bca..d8f8b23e7e78c1f223e7380c9585123682133f40 100644 (file)
@@ -39,6 +39,7 @@
 
 struct SIV_Instance_Record {
   struct siv_cmac_aes128_ctx siv;
+  int key_set;
 };
 
 /* ================================================== */
@@ -52,6 +53,7 @@ SIV_CreateInstance(SIV_Algorithm algorithm)
     return NULL;
 
   instance = MallocNew(struct SIV_Instance_Record);
+  instance->key_set = 0;
 
   return instance;
 }
@@ -86,6 +88,8 @@ SIV_SetKey(SIV_Instance instance, const unsigned char *key, int length)
 
   siv_cmac_aes128_set_key(&instance->siv, key);
 
+  instance->key_set = 1;
+
   return 1;
 }
 
@@ -108,6 +112,9 @@ SIV_Encrypt(SIV_Instance instance,
             const void *plaintext, int plaintext_length,
             unsigned char *ciphertext, int ciphertext_length)
 {
+  if (!instance->key_set)
+    return 0;
+
   if (nonce_length < SIV_MIN_NONCE_SIZE || assoc_length < 0 ||
       plaintext_length < 0 || plaintext_length > ciphertext_length ||
       plaintext_length + SIV_DIGEST_SIZE != ciphertext_length)
@@ -130,6 +137,9 @@ SIV_Decrypt(SIV_Instance instance,
             const unsigned char *ciphertext, int ciphertext_length,
             void *plaintext, int plaintext_length)
 {
+  if (!instance->key_set)
+    return 0;
+
   if (nonce_length < SIV_MIN_NONCE_SIZE || assoc_length < 0 ||
       plaintext_length < 0 || plaintext_length > ciphertext_length ||
       plaintext_length + SIV_DIGEST_SIZE != ciphertext_length)
index 5425de02e0acbb495be6821b47f8bd05a7725dc5..76846c8bc0215f89c3e30914b494adc2181eee86 100644 (file)
@@ -149,6 +149,17 @@ test_unit(void)
 
     TEST_CHECK(SIV_GetKeyLength(tests[i].algorithm) == tests[i].key_length);
 
+    r = SIV_Encrypt(siv, tests[i].nonce, tests[i].nonce_length,
+                    tests[i].assoc, tests[i].assoc_length,
+                    tests[i].plaintext, tests[i].plaintext_length,
+                    ciphertext, tests[i].ciphertext_length);
+    TEST_CHECK(!r);
+    r = SIV_Decrypt(siv, tests[i].nonce, tests[i].nonce_length,
+                    tests[i].assoc, tests[i].assoc_length,
+                    tests[i].ciphertext, tests[i].ciphertext_length,
+                    plaintext, tests[i].plaintext_length);
+    TEST_CHECK(!r);
+
     for (j = -1; j < 1024; j++) {
       r = SIV_SetKey(siv, tests[i].key, j);
       TEST_CHECK(r == (j == tests[i].key_length));