]> git.ipfire.org Git - thirdparty/openvpn.git/commitdiff
Use constant time memcmp when comparing HMACs in openvpn_decrypt.
authorSteffan Karger <steffan.karger@fox-it.com>
Tue, 19 Mar 2013 12:01:50 +0000 (13:01 +0100)
committerGert Doering <gert@greenie.muc.de>
Sat, 29 Nov 2014 13:57:11 +0000 (14:57 +0100)
Signed-off-by: Steffan Karger <steffan.karger@fox-it.com>
Acked-by: Gert Doering <gert@greenie.muc.de>
Signed-off-by: Gert Doering <gert@greenie.muc.de>
(cherry picked from commit 11d21349a4e7e38a025849479b36ace7c2eec2ee)

buffer.h
crypto.c

index 3b24d09e94ab4ee4537260d7f1e4f6e86bedd8db..037191134704b8ce02222d2926d677941f74dd5d 100644 (file)
--- a/buffer.h
+++ b/buffer.h
@@ -615,6 +615,10 @@ buf_read_u32 (struct buffer *buf, bool *good)
     }
 }
 
+/**
+ * Compare src buffer contents with match.
+ * *NOT* constant time. Do not use when comparing HMACs.
+ */
 static inline bool
 buf_string_match (const struct buffer *src, const void *match, int size)
 {
@@ -623,6 +627,10 @@ buf_string_match (const struct buffer *src, const void *match, int size)
   return memcmp (BPTR (src), match, size) == 0;
 }
 
+/**
+ * Compare first size bytes of src buffer contents with match.
+ * *NOT* constant time. Do not use when comparing HMACs.
+ */
 static inline bool
 buf_string_match_head (const struct buffer *src, const void *match, int size)
 {
index 5cfc34aceedacb2ad7bc9e230a8f93196ad794ee..6dd6a5f2a2631ca6ab665f48cbefda8ad23bd409 100644 (file)
--- a/crypto.c
+++ b/crypto.c
 #define CRYPT_ERROR(format) \
   do { msg (D_CRYPT_ERRORS, "%s: " format, error_prefix); goto error_exit; } while (false)
 
+/**
+ * As memcmp(), but constant-time.
+ * Returns 0 when data is equal, non-zero otherwise.
+ */
+static int
+memcmp_constant_time (const void *a, const void *b, size_t size) {
+  const uint8_t * a1 = a;
+  const uint8_t * b1 = b;
+  int ret = 0;
+  size_t i;
+
+  for (i = 0; i < size; i++) {
+      ret |= *a1++ ^ *b1++;
+  }
+
+  return ret;
+}
+
 void
 openvpn_encrypt (struct buffer *buf, struct buffer work,
                 const struct crypto_options *opt,
@@ -254,7 +272,7 @@ openvpn_decrypt (struct buffer *buf, struct buffer work,
          ASSERT (hmac_len == in_hmac_len);
 
          /* Compare locally computed HMAC with packet HMAC */
-         if (memcmp (local_hmac, BPTR (buf), hmac_len))
+         if (memcmp_constant_time (local_hmac, BPTR (buf), hmac_len))
            CRYPT_ERROR ("packet HMAC authentication failed");
 
          ASSERT (buf_advance (buf, hmac_len));