156083Skris/* crypto/rsa/rsa_oaep.c */
2296465Sdelphij/*
3296465Sdelphij * Written by Ulf Moeller. This software is distributed on an "AS IS" basis,
4296465Sdelphij * WITHOUT WARRANTY OF ANY KIND, either express or implied.
5296465Sdelphij */
656083Skris
789837Skris/* EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0) */
856083Skris
9296465Sdelphij/*
10296465Sdelphij * See Victor Shoup, "OAEP reconsidered," Nov. 2000, <URL:
11296465Sdelphij * http://www.shoup.net/papers/oaep.ps.Z> for problems with the security
12296465Sdelphij * proof for the original OAEP scheme, which EME-OAEP is based on. A new
13296465Sdelphij * proof can be found in E. Fujisaki, T. Okamoto, D. Pointcheval, J. Stern,
14296465Sdelphij * "RSA-OEAP is Still Alive!", Dec. 2000, <URL:
15296465Sdelphij * http://eprint.iacr.org/2000/061/>. The new proof has stronger requirements
16296465Sdelphij * for the underlying permutation: "partial-one-wayness" instead of
17296465Sdelphij * one-wayness.  For the RSA function, this is an equivalent notion.
1889837Skris */
1989837Skris
20279265Sdelphij#include "constant_time_locl.h"
2189837Skris
22109998Smarkm#if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA1)
23296465Sdelphij# include <stdio.h>
24296465Sdelphij# include "cryptlib.h"
25296465Sdelphij# include <openssl/bn.h>
26296465Sdelphij# include <openssl/rsa.h>
27296465Sdelphij# include <openssl/evp.h>
28296465Sdelphij# include <openssl/rand.h>
29296465Sdelphij# include <openssl/sha.h>
3056083Skris
3189837Skrisint MGF1(unsigned char *mask, long len,
32296465Sdelphij         const unsigned char *seed, long seedlen);
3356083Skris
3456083Skrisint RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
35296465Sdelphij                               const unsigned char *from, int flen,
36296465Sdelphij                               const unsigned char *param, int plen)
37296465Sdelphij{
38296465Sdelphij    int i, emlen = tlen - 1;
39296465Sdelphij    unsigned char *db, *seed;
40296465Sdelphij    unsigned char *dbmask, seedmask[SHA_DIGEST_LENGTH];
4156083Skris
42296465Sdelphij    if (flen > emlen - 2 * SHA_DIGEST_LENGTH - 1) {
43296465Sdelphij        RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP,
44296465Sdelphij               RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
45296465Sdelphij        return 0;
46296465Sdelphij    }
4756083Skris
48296465Sdelphij    if (emlen < 2 * SHA_DIGEST_LENGTH + 1) {
49296465Sdelphij        RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, RSA_R_KEY_SIZE_TOO_SMALL);
50296465Sdelphij        return 0;
51296465Sdelphij    }
5256083Skris
53296465Sdelphij    to[0] = 0;
54296465Sdelphij    seed = to + 1;
55296465Sdelphij    db = to + SHA_DIGEST_LENGTH + 1;
5689837Skris
57296465Sdelphij    EVP_Digest((void *)param, plen, db, NULL, EVP_sha1(), NULL);
58296465Sdelphij    memset(db + SHA_DIGEST_LENGTH, 0,
59296465Sdelphij           emlen - flen - 2 * SHA_DIGEST_LENGTH - 1);
60296465Sdelphij    db[emlen - flen - SHA_DIGEST_LENGTH - 1] = 0x01;
61296465Sdelphij    memcpy(db + emlen - flen - SHA_DIGEST_LENGTH, from, (unsigned int)flen);
62296465Sdelphij    if (RAND_bytes(seed, SHA_DIGEST_LENGTH) <= 0)
63296465Sdelphij        return 0;
64296465Sdelphij# ifdef PKCS_TESTVECT
65296465Sdelphij    memcpy(seed,
66296465Sdelphij           "\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2\xf0\x6c\xb5\x8f",
67296465Sdelphij           20);
68296465Sdelphij# endif
6956083Skris
70296465Sdelphij    dbmask = OPENSSL_malloc(emlen - SHA_DIGEST_LENGTH);
71296465Sdelphij    if (dbmask == NULL) {
72296465Sdelphij        RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
73296465Sdelphij        return 0;
74296465Sdelphij    }
75205128Ssimon
76296465Sdelphij    MGF1(dbmask, emlen - SHA_DIGEST_LENGTH, seed, SHA_DIGEST_LENGTH);
77296465Sdelphij    for (i = 0; i < emlen - SHA_DIGEST_LENGTH; i++)
78296465Sdelphij        db[i] ^= dbmask[i];
7956083Skris
80296465Sdelphij    MGF1(seedmask, SHA_DIGEST_LENGTH, db, emlen - SHA_DIGEST_LENGTH);
81296465Sdelphij    for (i = 0; i < SHA_DIGEST_LENGTH; i++)
82296465Sdelphij        seed[i] ^= seedmask[i];
8356083Skris
84296465Sdelphij    OPENSSL_free(dbmask);
85296465Sdelphij    return 1;
86296465Sdelphij}
8756083Skris
8856083Skrisint RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
89296465Sdelphij                                 const unsigned char *from, int flen, int num,
90296465Sdelphij                                 const unsigned char *param, int plen)
91296465Sdelphij{
92296465Sdelphij    int i, dblen, mlen = -1, one_index = 0, msg_index;
93296465Sdelphij    unsigned int good, found_one_byte;
94296465Sdelphij    const unsigned char *maskedseed, *maskeddb;
95296465Sdelphij    /*
96296465Sdelphij     * |em| is the encoded message, zero-padded to exactly |num| bytes: em =
97296465Sdelphij     * Y || maskedSeed || maskedDB
98296465Sdelphij     */
99296465Sdelphij    unsigned char *db = NULL, *em = NULL, seed[EVP_MAX_MD_SIZE],
100296465Sdelphij        phash[EVP_MAX_MD_SIZE];
10156083Skris
102296465Sdelphij    if (tlen <= 0 || flen <= 0)
103296465Sdelphij        return -1;
104279265Sdelphij
105296465Sdelphij    /*
106296465Sdelphij     * |num| is the length of the modulus; |flen| is the length of the
107296465Sdelphij     * encoded message. Therefore, for any |from| that was obtained by
108296465Sdelphij     * decrypting a ciphertext, we must have |flen| <= |num|. Similarly,
109296465Sdelphij     * num < 2 * SHA_DIGEST_LENGTH + 2 must hold for the modulus
110296465Sdelphij     * irrespective of the ciphertext, see PKCS #1 v2.2, section 7.1.2.
111296465Sdelphij     * This does not leak any side-channel information.
112296465Sdelphij     */
113296465Sdelphij    if (num < flen || num < 2 * SHA_DIGEST_LENGTH + 2)
114296465Sdelphij        goto decoding_err;
11556083Skris
116296465Sdelphij    dblen = num - SHA_DIGEST_LENGTH - 1;
117296465Sdelphij    db = OPENSSL_malloc(dblen);
118296465Sdelphij    em = OPENSSL_malloc(num);
119296465Sdelphij    if (db == NULL || em == NULL) {
120296465Sdelphij        RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, ERR_R_MALLOC_FAILURE);
121296465Sdelphij        goto cleanup;
122296465Sdelphij    }
12356083Skris
124296465Sdelphij    /*
125296465Sdelphij     * Always do this zero-padding copy (even when num == flen) to avoid
126296465Sdelphij     * leaking that information. The copy still leaks some side-channel
127296465Sdelphij     * information, but it's impossible to have a fixed  memory access
128296465Sdelphij     * pattern since we can't read out of the bounds of |from|.
129296465Sdelphij     *
130296465Sdelphij     * TODO(emilia): Consider porting BN_bn2bin_padded from BoringSSL.
131296465Sdelphij     */
132296465Sdelphij    memset(em, 0, num);
133296465Sdelphij    memcpy(em + num - flen, from, flen);
134194206Ssimon
135296465Sdelphij    /*
136296465Sdelphij     * The first byte must be zero, however we must not leak if this is
137296465Sdelphij     * true. See James H. Manger, "A Chosen Ciphertext  Attack on RSA
138296465Sdelphij     * Optimal Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001).
139296465Sdelphij     */
140296465Sdelphij    good = constant_time_is_zero(em[0]);
141194206Ssimon
142296465Sdelphij    maskedseed = em + 1;
143296465Sdelphij    maskeddb = em + 1 + SHA_DIGEST_LENGTH;
144279265Sdelphij
145296465Sdelphij    MGF1(seed, SHA_DIGEST_LENGTH, maskeddb, dblen);
146296465Sdelphij    for (i = 0; i < SHA_DIGEST_LENGTH; i++)
147296465Sdelphij        seed[i] ^= maskedseed[i];
148279265Sdelphij
149296465Sdelphij    MGF1(db, dblen, seed, SHA_DIGEST_LENGTH);
150296465Sdelphij    for (i = 0; i < dblen; i++)
151296465Sdelphij        db[i] ^= maskeddb[i];
15289837Skris
153296465Sdelphij    EVP_Digest((void *)param, plen, phash, NULL, EVP_sha1(), NULL);
15489837Skris
155296465Sdelphij    good &=
156296465Sdelphij        constant_time_is_zero(CRYPTO_memcmp(db, phash, SHA_DIGEST_LENGTH));
157279265Sdelphij
158296465Sdelphij    found_one_byte = 0;
159296465Sdelphij    for (i = SHA_DIGEST_LENGTH; i < dblen; i++) {
160296465Sdelphij        /*
161296465Sdelphij         * Padding consists of a number of 0-bytes, followed by a 1.
162296465Sdelphij         */
163296465Sdelphij        unsigned int equals1 = constant_time_eq(db[i], 1);
164296465Sdelphij        unsigned int equals0 = constant_time_is_zero(db[i]);
165296465Sdelphij        one_index = constant_time_select_int(~found_one_byte & equals1,
166296465Sdelphij                                             i, one_index);
167296465Sdelphij        found_one_byte |= equals1;
168296465Sdelphij        good &= (found_one_byte | equals0);
169296465Sdelphij    }
170279265Sdelphij
171296465Sdelphij    good &= found_one_byte;
172279265Sdelphij
173296465Sdelphij    /*
174296465Sdelphij     * At this point |good| is zero unless the plaintext was valid,
175296465Sdelphij     * so plaintext-awareness ensures timing side-channels are no longer a
176296465Sdelphij     * concern.
177296465Sdelphij     */
178296465Sdelphij    if (!good)
179296465Sdelphij        goto decoding_err;
180279265Sdelphij
181296465Sdelphij    msg_index = one_index + 1;
182296465Sdelphij    mlen = dblen - msg_index;
183279265Sdelphij
184296465Sdelphij    if (tlen < mlen) {
185296465Sdelphij        RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_DATA_TOO_LARGE);
186296465Sdelphij        mlen = -1;
187296465Sdelphij    } else {
188296465Sdelphij        memcpy(to, db + msg_index, mlen);
189296465Sdelphij        goto cleanup;
190296465Sdelphij    }
19179998Skris
192296465Sdelphij decoding_err:
193296465Sdelphij    /*
194296465Sdelphij     * To avoid chosen ciphertext attacks, the error message should not
195296465Sdelphij     * reveal which kind of decoding error happened.
196296465Sdelphij     */
197296465Sdelphij    RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP, RSA_R_OAEP_DECODING_ERROR);
198296465Sdelphij cleanup:
199296465Sdelphij    if (db != NULL)
200296465Sdelphij        OPENSSL_free(db);
201296465Sdelphij    if (em != NULL)
202296465Sdelphij        OPENSSL_free(em);
203296465Sdelphij    return mlen;
204296465Sdelphij}
20556083Skris
206160814Ssimonint PKCS1_MGF1(unsigned char *mask, long len,
207296465Sdelphij               const unsigned char *seed, long seedlen, const EVP_MD *dgst)
208296465Sdelphij{
209296465Sdelphij    long i, outlen = 0;
210296465Sdelphij    unsigned char cnt[4];
211296465Sdelphij    EVP_MD_CTX c;
212296465Sdelphij    unsigned char md[EVP_MAX_MD_SIZE];
213296465Sdelphij    int mdlen;
21456083Skris
215296465Sdelphij    EVP_MD_CTX_init(&c);
216296465Sdelphij    mdlen = M_EVP_MD_size(dgst);
217296465Sdelphij    for (i = 0; outlen < len; i++) {
218296465Sdelphij        cnt[0] = (unsigned char)((i >> 24) & 255);
219296465Sdelphij        cnt[1] = (unsigned char)((i >> 16) & 255);
220296465Sdelphij        cnt[2] = (unsigned char)((i >> 8)) & 255;
221296465Sdelphij        cnt[3] = (unsigned char)(i & 255);
222296465Sdelphij        EVP_DigestInit_ex(&c, dgst, NULL);
223296465Sdelphij        EVP_DigestUpdate(&c, seed, seedlen);
224296465Sdelphij        EVP_DigestUpdate(&c, cnt, 4);
225296465Sdelphij        if (outlen + mdlen <= len) {
226296465Sdelphij            EVP_DigestFinal_ex(&c, mask + outlen, NULL);
227296465Sdelphij            outlen += mdlen;
228296465Sdelphij        } else {
229296465Sdelphij            EVP_DigestFinal_ex(&c, md, NULL);
230296465Sdelphij            memcpy(mask + outlen, md, len - outlen);
231296465Sdelphij            outlen = len;
232296465Sdelphij        }
233296465Sdelphij    }
234296465Sdelphij    EVP_MD_CTX_cleanup(&c);
235296465Sdelphij    return 0;
236296465Sdelphij}
237160814Ssimon
238296465Sdelphijint MGF1(unsigned char *mask, long len, const unsigned char *seed,
239296465Sdelphij         long seedlen)
240296465Sdelphij{
241296465Sdelphij    return PKCS1_MGF1(mask, len, seed, seedlen, EVP_sha1());
242296465Sdelphij}
24356083Skris#endif
244