|
@@ -121,7 +121,7 @@ int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
|
|
|
const EVP_MD *mgf1md)
|
|
|
{
|
|
|
int i, dblen = 0, mlen = -1, one_index = 0, msg_index;
|
|
|
- unsigned int good, found_one_byte;
|
|
|
+ unsigned int good = 0, found_one_byte, mask;
|
|
|
const unsigned char *maskedseed, *maskeddb;
|
|
|
/*
|
|
|
* |em| is the encoded message, zero-padded to exactly |num| bytes: em =
|
|
@@ -144,12 +144,15 @@ int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
|
|
|
* |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 * mdlen + 2 must hold for the modulus irrespective of
|
|
|
+ * |num| >= 2 * |mdlen| + 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 * mdlen + 2)
|
|
|
- goto decoding_err;
|
|
|
+ if (num < flen || num < 2 * mdlen + 2) {
|
|
|
+ RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1,
|
|
|
+ RSA_R_OAEP_DECODING_ERROR);
|
|
|
+ return -1;
|
|
|
+ }
|
|
|
|
|
|
dblen = num - mdlen - 1;
|
|
|
db = OPENSSL_malloc(dblen);
|
|
@@ -158,25 +161,24 @@ int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
|
|
|
goto cleanup;
|
|
|
}
|
|
|
|
|
|
- if (flen != num) {
|
|
|
- em = OPENSSL_malloc(num);
|
|
|
- if (em == NULL) {
|
|
|
- RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1,
|
|
|
- ERR_R_MALLOC_FAILURE);
|
|
|
- goto cleanup;
|
|
|
- }
|
|
|
+ em = OPENSSL_malloc(num);
|
|
|
+ if (em == NULL) {
|
|
|
+ RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1,
|
|
|
+ ERR_R_MALLOC_FAILURE);
|
|
|
+ goto cleanup;
|
|
|
+ }
|
|
|
|
|
|
- /*
|
|
|
- * Caller is encouraged to pass zero-padded message created with
|
|
|
- * BN_bn2binpad, but if it doesn't, we do this zero-padding copy
|
|
|
- * 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|.
|
|
|
- */
|
|
|
- memset(em, 0, num);
|
|
|
- memcpy(em + num - flen, from, flen);
|
|
|
- from = em;
|
|
|
+ /*
|
|
|
+ * Caller is encouraged to pass zero-padded message created with
|
|
|
+ * BN_bn2binpad. Trouble is that since we can't read out of |from|'s
|
|
|
+ * bounds, it's impossible to have an invariant memory access pattern
|
|
|
+ * in case |from| was not zero-padded in advance.
|
|
|
+ */
|
|
|
+ for (from += flen, em += num, i = 0; i < num; i++) {
|
|
|
+ mask = ~constant_time_is_zero(flen);
|
|
|
+ flen -= 1 & mask;
|
|
|
+ from -= 1 & mask;
|
|
|
+ *--em = *from & mask;
|
|
|
}
|
|
|
|
|
|
/*
|
|
@@ -184,10 +186,10 @@ int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
|
|
|
* true. See James H. Manger, "A Chosen Ciphertext Attack on RSA
|
|
|
* Optimal Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001).
|
|
|
*/
|
|
|
- good = constant_time_is_zero(from[0]);
|
|
|
+ good = constant_time_is_zero(em[0]);
|
|
|
|
|
|
- maskedseed = from + 1;
|
|
|
- maskeddb = from + 1 + mdlen;
|
|
|
+ maskedseed = em + 1;
|
|
|
+ maskeddb = em + 1 + mdlen;
|
|
|
|
|
|
if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md))
|
|
|
goto cleanup;
|
|
@@ -224,37 +226,51 @@ int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
|
|
|
* 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_MGF1, RSA_R_DATA_TOO_LARGE);
|
|
|
- mlen = -1;
|
|
|
- } else {
|
|
|
- memcpy(to, db + msg_index, mlen);
|
|
|
- goto cleanup;
|
|
|
+ /*
|
|
|
+ * For good measure, do this check in constant time as well.
|
|
|
+ */
|
|
|
+ good &= constant_time_ge(tlen, mlen);
|
|
|
+
|
|
|
+ /*
|
|
|
+ * Move the result in-place by |dblen|-|mdlen|-1-|mlen| bytes to the left.
|
|
|
+ * Then if |good| move |mlen| bytes from |db|+|mdlen|+1 to |to|.
|
|
|
+ * Otherwise leave |to| unchanged.
|
|
|
+ * Copy the memory back in a way that does not reveal the size of
|
|
|
+ * the data being copied via a timing side channel. This requires copying
|
|
|
+ * parts of the buffer multiple times based on the bits set in the real
|
|
|
+ * length. Clear bits do a non-copy with identical access pattern.
|
|
|
+ * The loop below has overall complexity of O(N*log(N)).
|
|
|
+ */
|
|
|
+ tlen = constant_time_select_int(constant_time_lt(dblen - mdlen - 1, tlen),
|
|
|
+ dblen - mdlen - 1, tlen);
|
|
|
+ for (msg_index = 1; msg_index < dblen - mdlen - 1; msg_index <<= 1) {
|
|
|
+ mask = ~constant_time_eq(msg_index & (dblen - mdlen - 1 - mlen), 0);
|
|
|
+ for (i = mdlen + 1; i < dblen - msg_index; i++)
|
|
|
+ db[i] = constant_time_select_8(mask, db[i + msg_index], db[i]);
|
|
|
+ }
|
|
|
+ for (i = 0; i < tlen; i++) {
|
|
|
+ mask = good & constant_time_lt(i, mlen);
|
|
|
+ to[i] = constant_time_select_8(mask, db[i + mdlen + 1], to[i]);
|
|
|
}
|
|
|
|
|
|
- decoding_err:
|
|
|
/*
|
|
|
* 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_MGF1,
|
|
|
RSA_R_OAEP_DECODING_ERROR);
|
|
|
+ err_clear_last_constant_time(1 & good);
|
|
|
cleanup:
|
|
|
- if (db != NULL) {
|
|
|
- OPENSSL_cleanse(db, dblen);
|
|
|
- OPENSSL_free(db);
|
|
|
- }
|
|
|
- if (em != NULL) {
|
|
|
- OPENSSL_cleanse(em, num);
|
|
|
- OPENSSL_free(em);
|
|
|
- }
|
|
|
- return mlen;
|
|
|
+ OPENSSL_cleanse(seed, sizeof(seed));
|
|
|
+ OPENSSL_cleanse(db, dblen);
|
|
|
+ OPENSSL_free(db);
|
|
|
+ OPENSSL_cleanse(em, num);
|
|
|
+ OPENSSL_free(em);
|
|
|
+
|
|
|
+ return constant_time_select_int(good, mlen, -1);
|
|
|
}
|
|
|
|
|
|
int PKCS1_MGF1(unsigned char *mask, long len,
|