2015-01-22 03:40:55 +00:00
|
|
|
/*
|
2019-02-28 09:08:18 +00:00
|
|
|
* Copyright 1999-2019 The OpenSSL Project Authors. All Rights Reserved.
|
2016-05-17 18:51:34 +00:00
|
|
|
*
|
2018-12-06 12:54:02 +00:00
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
2016-05-17 18:51:34 +00:00
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
2015-01-22 03:40:55 +00:00
|
|
|
*/
|
1999-02-17 21:11:08 +00:00
|
|
|
|
2000-12-05 10:30:21 +00:00
|
|
|
/* EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0) */
|
|
|
|
|
2015-01-22 03:40:55 +00:00
|
|
|
/*
|
|
|
|
* See Victor Shoup, "OAEP reconsidered," Nov. 2000, <URL:
|
|
|
|
* http://www.shoup.net/papers/oaep.ps.Z> for problems with the security
|
|
|
|
* proof for the original OAEP scheme, which EME-OAEP is based on. A new
|
|
|
|
* proof can be found in E. Fujisaki, T. Okamoto, D. Pointcheval, J. Stern,
|
|
|
|
* "RSA-OEAP is Still Alive!", Dec. 2000, <URL:
|
|
|
|
* http://eprint.iacr.org/2000/061/>. The new proof has stronger requirements
|
|
|
|
* for the underlying permutation: "partial-one-wayness" instead of
|
|
|
|
* one-wayness. For the RSA function, this is an equivalent notion.
|
2000-12-05 10:30:21 +00:00
|
|
|
*/
|
|
|
|
|
2015-05-14 12:54:49 +00:00
|
|
|
#include "internal/constant_time_locl.h"
|
1999-02-17 21:11:08 +00:00
|
|
|
|
2015-01-27 17:34:45 +00:00
|
|
|
#include <stdio.h>
|
2015-05-14 14:56:48 +00:00
|
|
|
#include "internal/cryptlib.h"
|
2015-01-27 17:34:45 +00:00
|
|
|
#include <openssl/bn.h>
|
|
|
|
#include <openssl/evp.h>
|
|
|
|
#include <openssl/rand.h>
|
|
|
|
#include <openssl/sha.h>
|
2016-04-02 13:12:58 +00:00
|
|
|
#include "rsa_locl.h"
|
1999-02-17 21:11:08 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char *from, int flen,
|
|
|
|
const unsigned char *param, int plen)
|
|
|
|
{
|
|
|
|
return RSA_padding_add_PKCS1_OAEP_mgf1(to, tlen, from, flen,
|
|
|
|
param, plen, NULL, NULL);
|
|
|
|
}
|
2013-05-21 22:55:50 +00:00
|
|
|
|
|
|
|
int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char *from, int flen,
|
|
|
|
const unsigned char *param, int plen,
|
|
|
|
const EVP_MD *md, const EVP_MD *mgf1md)
|
|
|
|
{
|
2018-09-03 01:39:50 +00:00
|
|
|
int rv = 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
int i, emlen = tlen - 1;
|
|
|
|
unsigned char *db, *seed;
|
2018-09-03 01:39:50 +00:00
|
|
|
unsigned char *dbmask = NULL;
|
|
|
|
unsigned char seedmask[EVP_MAX_MD_SIZE];
|
|
|
|
int mdlen, dbmask_len = 0;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
if (md == NULL)
|
|
|
|
md = EVP_sha1();
|
|
|
|
if (mgf1md == NULL)
|
|
|
|
mgf1md = md;
|
|
|
|
|
|
|
|
mdlen = EVP_MD_size(md);
|
|
|
|
|
|
|
|
if (flen > emlen - 2 * mdlen - 1) {
|
|
|
|
RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1,
|
|
|
|
RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (emlen < 2 * mdlen + 1) {
|
|
|
|
RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1,
|
|
|
|
RSA_R_KEY_SIZE_TOO_SMALL);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
to[0] = 0;
|
|
|
|
seed = to + 1;
|
|
|
|
db = to + mdlen + 1;
|
|
|
|
|
|
|
|
if (!EVP_Digest((void *)param, plen, db, NULL, md, NULL))
|
2018-09-03 01:39:50 +00:00
|
|
|
goto err;
|
2015-01-22 03:40:55 +00:00
|
|
|
memset(db + mdlen, 0, emlen - flen - 2 * mdlen - 1);
|
|
|
|
db[emlen - flen - mdlen - 1] = 0x01;
|
|
|
|
memcpy(db + emlen - flen - mdlen, from, (unsigned int)flen);
|
|
|
|
if (RAND_bytes(seed, mdlen) <= 0)
|
2018-09-03 01:39:50 +00:00
|
|
|
goto err;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2018-09-03 01:39:50 +00:00
|
|
|
dbmask_len = emlen - mdlen;
|
|
|
|
dbmask = OPENSSL_malloc(dbmask_len);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (dbmask == NULL) {
|
|
|
|
RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE);
|
2018-09-03 01:39:50 +00:00
|
|
|
goto err;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
2018-09-03 01:39:50 +00:00
|
|
|
if (PKCS1_MGF1(dbmask, dbmask_len, seed, mdlen, mgf1md) < 0)
|
2016-12-23 13:35:16 +00:00
|
|
|
goto err;
|
2018-09-03 01:39:50 +00:00
|
|
|
for (i = 0; i < dbmask_len; i++)
|
2015-01-22 03:40:55 +00:00
|
|
|
db[i] ^= dbmask[i];
|
|
|
|
|
2018-09-03 01:39:50 +00:00
|
|
|
if (PKCS1_MGF1(seedmask, mdlen, db, dbmask_len, mgf1md) < 0)
|
2016-12-23 13:35:16 +00:00
|
|
|
goto err;
|
2015-01-22 03:40:55 +00:00
|
|
|
for (i = 0; i < mdlen; i++)
|
|
|
|
seed[i] ^= seedmask[i];
|
2018-09-03 01:39:50 +00:00
|
|
|
rv = 1;
|
2016-12-23 13:35:16 +00:00
|
|
|
|
|
|
|
err:
|
2018-09-03 01:39:50 +00:00
|
|
|
OPENSSL_cleanse(seedmask, sizeof(seedmask));
|
|
|
|
OPENSSL_clear_free(dbmask, dbmask_len);
|
|
|
|
return rv;
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1999-02-17 21:11:08 +00:00
|
|
|
|
1999-04-19 21:31:43 +00:00
|
|
|
int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char *from, int flen, int num,
|
|
|
|
const unsigned char *param, int plen)
|
|
|
|
{
|
|
|
|
return RSA_padding_check_PKCS1_OAEP_mgf1(to, tlen, from, flen, num,
|
|
|
|
param, plen, NULL, NULL);
|
|
|
|
}
|
2013-05-21 22:55:50 +00:00
|
|
|
|
|
|
|
int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char *from, int flen,
|
|
|
|
int num, const unsigned char *param,
|
|
|
|
int plen, const EVP_MD *md,
|
|
|
|
const EVP_MD *mgf1md)
|
|
|
|
{
|
2017-07-31 18:52:43 +00:00
|
|
|
int i, dblen = 0, mlen = -1, one_index = 0, msg_index;
|
2018-09-06 19:54:23 +00:00
|
|
|
unsigned int good = 0, found_one_byte, mask;
|
2015-01-22 03:40:55 +00:00
|
|
|
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];
|
|
|
|
int mdlen;
|
|
|
|
|
|
|
|
if (md == NULL)
|
|
|
|
md = EVP_sha1();
|
|
|
|
if (mgf1md == NULL)
|
|
|
|
mgf1md = md;
|
|
|
|
|
|
|
|
mdlen = EVP_MD_size(md);
|
|
|
|
|
|
|
|
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,
|
2019-02-28 09:08:18 +00:00
|
|
|
* |num| >= 2 * |mdlen| + 2 must hold for the modulus irrespective of
|
2015-01-22 03:40:55 +00:00
|
|
|
* the ciphertext, see PKCS #1 v2.2, section 7.1.2.
|
|
|
|
* This does not leak any side-channel information.
|
|
|
|
*/
|
2018-09-06 19:54:23 +00:00
|
|
|
if (num < flen || num < 2 * mdlen + 2) {
|
|
|
|
RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1,
|
|
|
|
RSA_R_OAEP_DECODING_ERROR);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
dblen = num - mdlen - 1;
|
|
|
|
db = OPENSSL_malloc(dblen);
|
2018-02-04 14:24:54 +00:00
|
|
|
if (db == NULL) {
|
2015-01-22 03:40:55 +00:00
|
|
|
RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2018-09-06 19:54:23 +00:00
|
|
|
em = OPENSSL_malloc(num);
|
|
|
|
if (em == NULL) {
|
|
|
|
RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1,
|
|
|
|
ERR_R_MALLOC_FAILURE);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2018-02-04 14:24:54 +00:00
|
|
|
|
2018-09-06 19:54:23 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2018-02-04 14:24:54 +00:00
|
|
|
}
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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).
|
|
|
|
*/
|
2019-02-28 09:08:18 +00:00
|
|
|
good = constant_time_is_zero(em[0]);
|
2015-01-22 03:40:55 +00:00
|
|
|
|
2019-02-28 09:08:18 +00:00
|
|
|
maskedseed = em + 1;
|
|
|
|
maskeddb = em + 1 + mdlen;
|
2015-01-22 03:40:55 +00:00
|
|
|
|
|
|
|
if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md))
|
|
|
|
goto cleanup;
|
|
|
|
for (i = 0; i < mdlen; i++)
|
|
|
|
seed[i] ^= maskedseed[i];
|
|
|
|
|
|
|
|
if (PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md))
|
|
|
|
goto cleanup;
|
|
|
|
for (i = 0; i < dblen; i++)
|
|
|
|
db[i] ^= maskeddb[i];
|
|
|
|
|
|
|
|
if (!EVP_Digest((void *)param, plen, phash, NULL, md, NULL))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
good &= constant_time_is_zero(CRYPTO_memcmp(db, phash, mdlen));
|
|
|
|
|
|
|
|
found_one_byte = 0;
|
|
|
|
for (i = mdlen; 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;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* At this point |good| is zero unless the plaintext was valid,
|
|
|
|
* so plaintext-awareness ensures timing side-channels are no longer a
|
|
|
|
* concern.
|
|
|
|
*/
|
|
|
|
msg_index = one_index + 1;
|
|
|
|
mlen = dblen - msg_index;
|
|
|
|
|
2018-09-06 19:54:23 +00:00
|
|
|
/*
|
2019-02-28 09:08:18 +00:00
|
|
|
* For good measure, do this check in constant time as well.
|
2018-09-06 19:54:23 +00:00
|
|
|
*/
|
|
|
|
good &= constant_time_ge(tlen, mlen);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Even though we can't fake result's length, we can pretend copying
|
|
|
|
* |tlen| bytes where |mlen| bytes would be real. Last |tlen| of |dblen|
|
|
|
|
* bytes are viewed as circular buffer with start at |tlen|-|mlen'|,
|
|
|
|
* where |mlen'| is "saturated" |mlen| value. Deducing information
|
|
|
|
* about failure or |mlen| would take attacker's ability to observe
|
|
|
|
* memory access pattern with byte granularity *as it occurs*. It
|
|
|
|
* should be noted that failure is indistinguishable from normal
|
|
|
|
* operation if |tlen| is fixed by protocol.
|
|
|
|
*/
|
2019-02-28 09:08:18 +00:00
|
|
|
tlen = constant_time_select_int(constant_time_lt(dblen - mdlen - 1, tlen),
|
|
|
|
dblen - mdlen - 1, tlen);
|
2018-09-06 19:54:23 +00:00
|
|
|
msg_index = constant_time_select_int(good, msg_index, dblen - tlen);
|
|
|
|
mlen = dblen - msg_index;
|
2019-02-28 09:08:18 +00:00
|
|
|
for (mask = good, i = 0; i < tlen; i++) {
|
|
|
|
unsigned int equals = constant_time_eq(msg_index, dblen);
|
2018-09-06 19:54:23 +00:00
|
|
|
|
2019-02-28 09:08:18 +00:00
|
|
|
msg_index -= tlen & equals; /* rewind at EOF */
|
|
|
|
mask &= ~equals; /* mask = 0 at EOF */
|
|
|
|
to[i] = constant_time_select_8(mask, db[msg_index++], to[i]);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
2018-09-06 19:54:23 +00:00
|
|
|
err_clear_last_constant_time(1 & good);
|
2015-01-22 03:40:55 +00:00
|
|
|
cleanup:
|
2018-09-03 01:39:50 +00:00
|
|
|
OPENSSL_cleanse(seed, sizeof(seed));
|
2017-07-31 18:52:43 +00:00
|
|
|
OPENSSL_clear_free(db, dblen);
|
|
|
|
OPENSSL_clear_free(em, num);
|
2018-09-06 19:54:23 +00:00
|
|
|
|
|
|
|
return constant_time_select_int(good, mlen, -1);
|
2015-01-22 03:40:55 +00:00
|
|
|
}
|
1999-02-17 21:11:08 +00:00
|
|
|
|
2005-05-28 20:44:02 +00:00
|
|
|
int PKCS1_MGF1(unsigned char *mask, long len,
|
2015-01-22 03:40:55 +00:00
|
|
|
const unsigned char *seed, long seedlen, const EVP_MD *dgst)
|
|
|
|
{
|
|
|
|
long i, outlen = 0;
|
|
|
|
unsigned char cnt[4];
|
2015-12-01 23:49:35 +00:00
|
|
|
EVP_MD_CTX *c = EVP_MD_CTX_new();
|
2015-01-22 03:40:55 +00:00
|
|
|
unsigned char md[EVP_MAX_MD_SIZE];
|
|
|
|
int mdlen;
|
|
|
|
int rv = -1;
|
|
|
|
|
2015-11-27 13:02:12 +00:00
|
|
|
if (c == NULL)
|
|
|
|
goto err;
|
|
|
|
mdlen = EVP_MD_size(dgst);
|
2015-01-22 03:40:55 +00:00
|
|
|
if (mdlen < 0)
|
|
|
|
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);
|
2015-11-27 13:02:12 +00:00
|
|
|
if (!EVP_DigestInit_ex(c, dgst, NULL)
|
|
|
|
|| !EVP_DigestUpdate(c, seed, seedlen)
|
|
|
|
|| !EVP_DigestUpdate(c, cnt, 4))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
if (outlen + mdlen <= len) {
|
2015-11-27 13:02:12 +00:00
|
|
|
if (!EVP_DigestFinal_ex(c, mask + outlen, NULL))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
outlen += mdlen;
|
|
|
|
} else {
|
2015-11-27 13:02:12 +00:00
|
|
|
if (!EVP_DigestFinal_ex(c, md, NULL))
|
2015-01-22 03:40:55 +00:00
|
|
|
goto err;
|
|
|
|
memcpy(mask + outlen, md, len - outlen);
|
|
|
|
outlen = len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rv = 0;
|
|
|
|
err:
|
2018-09-03 01:39:50 +00:00
|
|
|
OPENSSL_cleanse(md, sizeof(md));
|
2015-12-01 23:49:35 +00:00
|
|
|
EVP_MD_CTX_free(c);
|
2015-01-22 03:40:55 +00:00
|
|
|
return rv;
|
|
|
|
}
|