summaryrefslogtreecommitdiffstats
path: root/crypto
diff options
context:
space:
mode:
authorBen Laurie <ben@links.org>2013-01-28 17:30:38 +0000
committerDr. Stephen Henson <steve@openssl.org>2013-02-06 14:16:55 +0000
commit7c770d572a719fa40fa9c82807a0bd3840baf4a0 (patch)
treebc4d2be0bd12ef55460d16c760d87ff9ae954aa0 /crypto
parentea34a58385058748c51037bfb2c3208ee639f5f1 (diff)
Add and use a constant-time memcmp.
This change adds CRYPTO_memcmp, which compares two vectors of bytes in an amount of time that's independent of their contents. It also changes several MAC compares in the code to use this over the standard memcmp, which may leak information about the size of a matching prefix. (cherry picked from commit 2ee798880a246d648ecddadc5b91367bee4a5d98)
Diffstat (limited to 'crypto')
-rw-r--r--crypto/cryptlib.c13
-rw-r--r--crypto/crypto.h7
-rw-r--r--crypto/rsa/rsa_oaep.c2
3 files changed, 21 insertions, 1 deletions
diff --git a/crypto/cryptlib.c b/crypto/cryptlib.c
index 07b0a66217..56d82adfcd 100644
--- a/crypto/cryptlib.c
+++ b/crypto/cryptlib.c
@@ -397,3 +397,16 @@ void OpenSSLDie(const char *file,int line,const char *assertion)
#ifndef OPENSSL_FIPSCANISTER
void *OPENSSL_stderr(void) { return stderr; }
#endif
+
+int CRYPTO_memcmp(const void *in_a, const void *in_b, size_t len)
+ {
+ size_t i;
+ const unsigned char *a = in_a;
+ const unsigned char *b = in_b;
+ unsigned char x = 0;
+
+ for (i = 0; i < len; i++)
+ x |= a[i] ^ b[i];
+
+ return x;
+ }
diff --git a/crypto/crypto.h b/crypto/crypto.h
index d3da3c881f..3a43803a1e 100644
--- a/crypto/crypto.h
+++ b/crypto/crypto.h
@@ -567,6 +567,13 @@ int FIPS_mode_set(int r);
void OPENSSL_init(void);
+/* CRYPTO_memcmp returns zero iff the |len| bytes at |a| and |b| are equal. It
+ * takes an amount of time dependent on |len|, but independent of the contents
+ * of |a| and |b|. Unlike memcmp, it cannot be used to put elements into a
+ * defined order as the return value when a != b is undefined, other than to be
+ * non-zero. */
+int CRYPTO_memcmp(const void *a, const void *b, size_t len);
+
/* BEGIN ERROR CODES */
/* The following lines are auto generated by the script mkerr.pl. Any changes
* made after this point may be overwritten when the script is next run.
diff --git a/crypto/rsa/rsa_oaep.c b/crypto/rsa/rsa_oaep.c
index eaae712236..c57507d214 100644
--- a/crypto/rsa/rsa_oaep.c
+++ b/crypto/rsa/rsa_oaep.c
@@ -151,7 +151,7 @@ int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
if (!EVP_Digest((void *)param, plen, phash, NULL, EVP_sha1(), NULL))
return -1;
- if (memcmp(db, phash, SHA_DIGEST_LENGTH) != 0 || bad)
+ if (CRYPTO_memcmp(db, phash, SHA_DIGEST_LENGTH) != 0 || bad)
goto decoding_err;
else
{