Fix modes.h so that indent doesn't complain
[openssl.git] / crypto / rsa / rsa_oaep.c
index 70bacf850e36cfddce20c076a1fd79400a8d80e7..c363331992e0c75111882843447f8bd1ac858ccb 100644 (file)
@@ -18,6 +18,7 @@
  * an equivalent notion.
  */
 
+#include "constant_time_locl.h"
 
 #if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA1)
 #include <stdio.h>
@@ -52,18 +53,12 @@ int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
                return 0;
                }
 
-       dbmask = OPENSSL_malloc(emlen - SHA_DIGEST_LENGTH);
-       if (dbmask == NULL)
-               {
-               RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
-               return 0;
-               }
-
        to[0] = 0;
        seed = to + 1;
        db = to + SHA_DIGEST_LENGTH + 1;
 
-       EVP_Digest((void *)param, plen, db, NULL, EVP_sha1(), NULL);
+       if (!EVP_Digest((void *)param, plen, db, NULL, EVP_sha1(), NULL))
+               return 0;
        memset(db + SHA_DIGEST_LENGTH, 0,
                emlen - flen - 2 * SHA_DIGEST_LENGTH - 1);
        db[emlen - flen - SHA_DIGEST_LENGTH - 1] = 0x01;
@@ -76,6 +71,13 @@ int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
           20);
 #endif
 
+       dbmask = OPENSSL_malloc(emlen - SHA_DIGEST_LENGTH);
+       if (dbmask == NULL)
+               {
+               RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
+               return 0;
+               }
+
        if (MGF1(dbmask, emlen - SHA_DIGEST_LENGTH, seed, SHA_DIGEST_LENGTH) < 0)
                return 0;
        for (i = 0; i < emlen - SHA_DIGEST_LENGTH; i++)
@@ -94,91 +96,117 @@ int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
        const unsigned char *from, int flen, int num,
        const unsigned char *param, int plen)
        {
-       int i, dblen, mlen = -1;
-       const unsigned char *maskeddb;
-       int lzero;
-       unsigned char *db = NULL, seed[SHA_DIGEST_LENGTH], phash[SHA_DIGEST_LENGTH];
-       unsigned char *padded_from;
-       int bad = 0;
-
-       if (--num < 2 * SHA_DIGEST_LENGTH + 1)
-               /* 'num' is the length of the modulus, i.e. does not depend on the
-                * particular ciphertext. */
-               goto decoding_err;
+       int i, dblen, mlen = -1, one_index = 0, msg_index;
+       unsigned int good, found_one_byte;
+       const unsigned char *maskedseed, *maskeddb;
+       /* |em| is the encoded message, zero-padded to exactly |num| bytes:
+        * em = Y || maskedSeed || maskedDB */
+       unsigned char *db = NULL, *em = NULL, seed[EVP_MAX_MD_SIZE],
+               phash[EVP_MAX_MD_SIZE];
 
-       lzero = num - flen;
-       if (lzero < 0)
-               {
-               /* signalling this error immediately after detection might allow
-                * for side-channel attacks (e.g. timing if 'plen' is huge
-                * -- cf. James H. Manger, "A Chosen Ciphertext Attack on RSA Optimal
-                * Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001),
-                * so we use a 'bad' flag */
-               bad = 1;
-               lzero = 0;
-               flen = num; /* don't overflow the memcpy to padded_from */
-               }
+        if (tlen <= 0 || flen <= 0)
+               return -1;
+
+       /*
+        * |num| is the length of the modulus; |flen| is the length of the
+        * encoded message. Therefore, for any |from| that was obtained by
+        * decrypting a ciphertext, we must have |flen| <= |num|. Similarly,
+        * num < 2 * SHA_DIGEST_LENGTH + 2 must hold for the modulus
+        * irrespective of the ciphertext, see PKCS #1 v2.2, section 7.1.2.
+        * This does not leak any side-channel information.
+        */
+       if (num < flen || num < 2 * SHA_DIGEST_LENGTH + 2)
+               goto decoding_err;
 
-       dblen = num - SHA_DIGEST_LENGTH;
-       db = OPENSSL_malloc(dblen + num);
-       if (db == NULL)
+       dblen = num - SHA_DIGEST_LENGTH - 1;
+       db = OPENSSL_malloc(dblen);
+       em = OPENSSL_malloc(num);
+       if (db == NULL || em == NULL)
                {
                RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
-               return -1;
+               goto cleanup;
                }
 
-       /* Always do this zero-padding copy (even when lzero == 0)
-        * to avoid leaking timing info about the value of lzero. */
-       padded_from = db + dblen;
-       memset(padded_from, 0, lzero);
-       memcpy(padded_from + lzero, from, flen);
+       /*
+        * Always do this zero-padding copy (even when num == flen) to avoid
+        * leaking that information. The copy still leaks some side-channel
+        * information, but it's impossible to have a fixed  memory access
+        * pattern since we can't read out of the bounds of |from|.
+        *
+        * TODO(emilia): Consider porting BN_bn2bin_padded from BoringSSL.
+        */
+       memset(em, 0, num);
+       memcpy(em + num - flen, from, flen);
+
+       /*
+        * The first byte must be zero, however we must not leak if this is
+        * true. See James H. Manger, "A Chosen Ciphertext  Attack on RSA
+        * Optimal Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001).
+        */
+       good = constant_time_is_zero(em[0]);
 
-       maskeddb = padded_from + SHA_DIGEST_LENGTH;
+       maskedseed = em + 1;
+       maskeddb = em + 1 + SHA_DIGEST_LENGTH;
 
        if (MGF1(seed, SHA_DIGEST_LENGTH, maskeddb, dblen))
-               return -1;
+               goto cleanup;
        for (i = 0; i < SHA_DIGEST_LENGTH; i++)
-               seed[i] ^= padded_from[i];
-  
+               seed[i] ^= maskedseed[i];
+
        if (MGF1(db, dblen, seed, SHA_DIGEST_LENGTH))
-               return -1;
+               goto cleanup;
        for (i = 0; i < dblen; i++)
                db[i] ^= maskeddb[i];
 
-       EVP_Digest((void *)param, plen, phash, NULL, EVP_sha1(), NULL);
+       if (!EVP_Digest((void *)param, plen, phash, NULL, EVP_sha1(), NULL))
+               goto cleanup;
+
+       good &= constant_time_is_zero(CRYPTO_memcmp(db, phash, SHA_DIGEST_LENGTH));
+
+       found_one_byte = 0;
+       for (i = SHA_DIGEST_LENGTH; i < dblen; i++)
+               {
+               /* Padding consists of a number of 0-bytes, followed by a 1. */
+               unsigned int equals1 = constant_time_eq(db[i], 1);
+               unsigned int equals0 = constant_time_is_zero(db[i]);
+               one_index = constant_time_select_int(~found_one_byte & equals1,
+                       i, one_index);
+               found_one_byte |= equals1;
+               good &= (found_one_byte | equals0);
+               }
+
+       good &= found_one_byte;
 
-       if (memcmp(db, phash, SHA_DIGEST_LENGTH) != 0 || bad)
+       /*
+        * At this point |good| is zero unless the plaintext was valid,
+        * so plaintext-awareness ensures timing side-channels are no longer a
+        * concern.
+        */
+       if (!good)
                goto decoding_err;
+
+       msg_index = one_index + 1;
+       mlen = dblen - msg_index;
+
+       if (tlen < mlen)
+               {
+               RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_DATA_TOO_LARGE);
+               mlen = -1;
+               }
        else
                {
-               for (i = SHA_DIGEST_LENGTH; i < dblen; i++)
-                       if (db[i] != 0x00)
-                               break;
-               if (i == dblen || db[i] != 0x01)
-                       goto decoding_err;
-               else
-                       {
-                       /* everything looks OK */
-
-                       mlen = dblen - ++i;
-                       if (tlen < mlen)
-                               {
-                               RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_DATA_TOO_LARGE);
-                               mlen = -1;
-                               }
-                       else
-                               memcpy(to, db + i, mlen);
-                       }
+               memcpy(to, db + msg_index, mlen);
+               goto cleanup;
                }
-       OPENSSL_free(db);
-       return mlen;
 
 decoding_err:
-       /* to avoid chosen ciphertext attacks, the error message should not reveal
-        * which kind of decoding error happened */
+       /* To avoid chosen ciphertext attacks, the error message should not reveal
+        * which kind of decoding error happened. */
        RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_OAEP_DECODING_ERROR);
+cleanup:
        if (db != NULL) OPENSSL_free(db);
-       return -1;
+       if (em != NULL) OPENSSL_free(em);
+       return mlen;
        }
 
 int PKCS1_MGF1(unsigned char *mask, long len,
@@ -189,34 +217,40 @@ int PKCS1_MGF1(unsigned char *mask, long len,
        EVP_MD_CTX c;
        unsigned char md[EVP_MAX_MD_SIZE];
        int mdlen;
+       int rv = -1;
 
        EVP_MD_CTX_init(&c);
        mdlen = EVP_MD_size(dgst);
        if (mdlen < 0)
-               return -1;
+               goto err;
        for (i = 0; outlen < len; i++)
                {
                cnt[0] = (unsigned char)((i >> 24) & 255);
                cnt[1] = (unsigned char)((i >> 16) & 255);
                cnt[2] = (unsigned char)((i >> 8)) & 255;
                cnt[3] = (unsigned char)(i & 255);
-               EVP_DigestInit_ex(&c,dgst, NULL);
-               EVP_DigestUpdate(&c, seed, seedlen);
-               EVP_DigestUpdate(&c, cnt, 4);
+               if (!EVP_DigestInit_ex(&c,dgst, NULL)
+                       || !EVP_DigestUpdate(&c, seed, seedlen)
+                       || !EVP_DigestUpdate(&c, cnt, 4))
+                       goto err;
                if (outlen + mdlen <= len)
                        {
-                       EVP_DigestFinal_ex(&c, mask + outlen, NULL);
+                       if (!EVP_DigestFinal_ex(&c, mask + outlen, NULL))
+                               goto err;
                        outlen += mdlen;
                        }
                else
                        {
-                       EVP_DigestFinal_ex(&c, md, NULL);
+                       if (!EVP_DigestFinal_ex(&c, md, NULL))
+                               goto err;
                        memcpy(mask + outlen, md, len - outlen);
                        outlen = len;
                        }
                }
+       rv = 0;
+       err:
        EVP_MD_CTX_cleanup(&c);
-       return 0;
+       return rv;
        }
 
 static int MGF1(unsigned char *mask, long len, const unsigned char *seed,