AES CTR-DRGB: do not leak timing information
authorPatrick Steuer <patrick.steuer@de.ibm.com>
Sat, 22 Feb 2020 00:20:09 +0000 (01:20 +0100)
committerPauli <paul.dale@oracle.com>
Wed, 8 Apr 2020 00:58:07 +0000 (10:58 +1000)
Signed-off-by: Patrick Steuer <patrick.steuer@de.ibm.com>
Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
Reviewed-by: Paul Dale <paul.dale@oracle.com>
(Merged from https://github.com/openssl/openssl/pull/11147)

(cherry picked from commit 069165d10646a22000c596095cc04d43bbf1f807)

crypto/rand/drbg_ctr.c

index 93b82f34ceda1290f5ff6d073c6df56501e6d7d2..f41484e9d548bdc551c50393566cd8d6ecf3b78a 100644 (file)
 
 static void inc_128(RAND_DRBG_CTR *ctr)
 {
-    int i;
-    unsigned char c;
-    unsigned char *p = &ctr->V[15];
-
-    for (i = 0; i < 16; i++, p--) {
-        c = *p;
-        c++;
-        *p = c;
-        if (c != 0) {
-            /* If we didn't wrap around, we're done. */
-            break;
-        }
-    }
+    unsigned char *p = &ctr->V[0];
+    u32 n = 16, c = 1;
+
+    do {
+        --n;
+        c += p[n];
+        p[n] = (u8)c;
+        c >>= 8;
+    } while (n);
 }
 
 static void ctr_XOR(RAND_DRBG_CTR *ctr, const unsigned char *in, size_t inlen)