Coverage Report

Created: 2022-11-30 06:20

/src/openssl/crypto/rsa/rsa_oaep.c
Line
Count
Source (jump to first uncovered line)
1
/* crypto/rsa/rsa_oaep.c */
2
/*
3
 * Written by Ulf Moeller. This software is distributed on an "AS IS" basis,
4
 * WITHOUT WARRANTY OF ANY KIND, either express or implied.
5
 */
6
7
/* EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0) */
8
9
/*
10
 * See Victor Shoup, "OAEP reconsidered," Nov. 2000, <URL:
11
 * http://www.shoup.net/papers/oaep.ps.Z> for problems with the security
12
 * proof for the original OAEP scheme, which EME-OAEP is based on. A new
13
 * proof can be found in E. Fujisaki, T. Okamoto, D. Pointcheval, J. Stern,
14
 * "RSA-OEAP is Still Alive!", Dec. 2000, <URL:
15
 * http://eprint.iacr.org/2000/061/>. The new proof has stronger requirements
16
 * for the underlying permutation: "partial-one-wayness" instead of
17
 * one-wayness.  For the RSA function, this is an equivalent notion.
18
 */
19
20
#include "constant_time_locl.h"
21
22
#if !defined(OPENSSL_NO_SHA) && !defined(OPENSSL_NO_SHA1)
23
# include <stdio.h>
24
# include "cryptlib.h"
25
# include <openssl/bn.h>
26
# include <openssl/rsa.h>
27
# include <openssl/evp.h>
28
# include <openssl/rand.h>
29
# include <openssl/sha.h>
30
31
int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
32
                               const unsigned char *from, int flen,
33
                               const unsigned char *param, int plen)
34
0
{
35
0
    return RSA_padding_add_PKCS1_OAEP_mgf1(to, tlen, from, flen,
36
0
                                           param, plen, NULL, NULL);
37
0
}
38
39
int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
40
                                    const unsigned char *from, int flen,
41
                                    const unsigned char *param, int plen,
42
                                    const EVP_MD *md, const EVP_MD *mgf1md)
43
0
{
44
0
    int i, emlen = tlen - 1;
45
0
    unsigned char *db, *seed;
46
0
    unsigned char *dbmask, seedmask[EVP_MAX_MD_SIZE];
47
0
    int mdlen;
48
49
0
    if (md == NULL)
50
0
        md = EVP_sha1();
51
0
    if (mgf1md == NULL)
52
0
        mgf1md = md;
53
54
0
    mdlen = EVP_MD_size(md);
55
56
0
    if (flen > emlen - 2 * mdlen - 1) {
57
0
        RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1,
58
0
               RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
59
0
        return 0;
60
0
    }
61
62
0
    if (emlen < 2 * mdlen + 1) {
63
0
        RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1,
64
0
               RSA_R_KEY_SIZE_TOO_SMALL);
65
0
        return 0;
66
0
    }
67
68
0
    to[0] = 0;
69
0
    seed = to + 1;
70
0
    db = to + mdlen + 1;
71
72
0
    if (!EVP_Digest((void *)param, plen, db, NULL, md, NULL))
73
0
        return 0;
74
0
    memset(db + mdlen, 0, emlen - flen - 2 * mdlen - 1);
75
0
    db[emlen - flen - mdlen - 1] = 0x01;
76
0
    memcpy(db + emlen - flen - mdlen, from, (unsigned int)flen);
77
0
    if (RAND_bytes(seed, mdlen) <= 0)
78
0
        return 0;
79
# ifdef PKCS_TESTVECT
80
    memcpy(seed,
81
           "\xaa\xfd\x12\xf6\x59\xca\xe6\x34\x89\xb4\x79\xe5\x07\x6d\xde\xc2\xf0\x6c\xb5\x8f",
82
           20);
83
# endif
84
85
0
    dbmask = OPENSSL_malloc(emlen - mdlen);
86
0
    if (dbmask == NULL) {
87
0
        RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE);
88
0
        return 0;
89
0
    }
90
91
0
    if (PKCS1_MGF1(dbmask, emlen - mdlen, seed, mdlen, mgf1md) < 0)
92
0
        goto err;
93
0
    for (i = 0; i < emlen - mdlen; i++)
94
0
        db[i] ^= dbmask[i];
95
96
0
    if (PKCS1_MGF1(seedmask, mdlen, db, emlen - mdlen, mgf1md) < 0)
97
0
        goto err;
98
0
    for (i = 0; i < mdlen; i++)
99
0
        seed[i] ^= seedmask[i];
100
101
0
    OPENSSL_free(dbmask);
102
0
    return 1;
103
104
0
 err:
105
0
    OPENSSL_free(dbmask);
106
0
    return 0;
107
0
}
108
109
int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
110
                                 const unsigned char *from, int flen, int num,
111
                                 const unsigned char *param, int plen)
112
0
{
113
0
    return RSA_padding_check_PKCS1_OAEP_mgf1(to, tlen, from, flen, num,
114
0
                                             param, plen, NULL, NULL);
115
0
}
116
117
int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
118
                                      const unsigned char *from, int flen,
119
                                      int num, const unsigned char *param,
120
                                      int plen, const EVP_MD *md,
121
                                      const EVP_MD *mgf1md)
122
0
{
123
0
    int i, dblen, mlen = -1, one_index = 0, msg_index;
124
0
    unsigned int good, found_one_byte;
125
0
    const unsigned char *maskedseed, *maskeddb;
126
    /*
127
     * |em| is the encoded message, zero-padded to exactly |num| bytes: em =
128
     * Y || maskedSeed || maskedDB
129
     */
130
0
    unsigned char *db = NULL, *em = NULL, seed[EVP_MAX_MD_SIZE],
131
0
        phash[EVP_MAX_MD_SIZE];
132
0
    int mdlen;
133
134
0
    if (md == NULL)
135
0
        md = EVP_sha1();
136
0
    if (mgf1md == NULL)
137
0
        mgf1md = md;
138
139
0
    mdlen = EVP_MD_size(md);
140
141
0
    if (tlen <= 0 || flen <= 0)
142
0
        return -1;
143
    /*
144
     * |num| is the length of the modulus; |flen| is the length of the
145
     * encoded message. Therefore, for any |from| that was obtained by
146
     * decrypting a ciphertext, we must have |flen| <= |num|. Similarly,
147
     * num < 2 * mdlen + 2 must hold for the modulus irrespective of
148
     * the ciphertext, see PKCS #1 v2.2, section 7.1.2.
149
     * This does not leak any side-channel information.
150
     */
151
0
    if (num < flen || num < 2 * mdlen + 2)
152
0
        goto decoding_err;
153
154
0
    dblen = num - mdlen - 1;
155
0
    db = OPENSSL_malloc(dblen);
156
0
    em = OPENSSL_malloc(num);
157
0
    if (db == NULL || em == NULL) {
158
0
        RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE);
159
0
        goto cleanup;
160
0
    }
161
162
    /*
163
     * Always do this zero-padding copy (even when num == flen) to avoid
164
     * leaking that information. The copy still leaks some side-channel
165
     * information, but it's impossible to have a fixed  memory access
166
     * pattern since we can't read out of the bounds of |from|.
167
     *
168
     * TODO(emilia): Consider porting BN_bn2bin_padded from BoringSSL.
169
     */
170
0
    memset(em, 0, num);
171
0
    memcpy(em + num - flen, from, flen);
172
173
    /*
174
     * The first byte must be zero, however we must not leak if this is
175
     * true. See James H. Manger, "A Chosen Ciphertext  Attack on RSA
176
     * Optimal Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001).
177
     */
178
0
    good = constant_time_is_zero(em[0]);
179
180
0
    maskedseed = em + 1;
181
0
    maskeddb = em + 1 + mdlen;
182
183
0
    if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md))
184
0
        goto cleanup;
185
0
    for (i = 0; i < mdlen; i++)
186
0
        seed[i] ^= maskedseed[i];
187
188
0
    if (PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md))
189
0
        goto cleanup;
190
0
    for (i = 0; i < dblen; i++)
191
0
        db[i] ^= maskeddb[i];
192
193
0
    if (!EVP_Digest((void *)param, plen, phash, NULL, md, NULL))
194
0
        goto cleanup;
195
196
0
    good &= constant_time_is_zero(CRYPTO_memcmp(db, phash, mdlen));
197
198
0
    found_one_byte = 0;
199
0
    for (i = mdlen; i < dblen; i++) {
200
        /*
201
         * Padding consists of a number of 0-bytes, followed by a 1.
202
         */
203
0
        unsigned int equals1 = constant_time_eq(db[i], 1);
204
0
        unsigned int equals0 = constant_time_is_zero(db[i]);
205
0
        one_index = constant_time_select_int(~found_one_byte & equals1,
206
0
                                             i, one_index);
207
0
        found_one_byte |= equals1;
208
0
        good &= (found_one_byte | equals0);
209
0
    }
210
211
0
    good &= found_one_byte;
212
213
    /*
214
     * At this point |good| is zero unless the plaintext was valid,
215
     * so plaintext-awareness ensures timing side-channels are no longer a
216
     * concern.
217
     */
218
0
    if (!good)
219
0
        goto decoding_err;
220
221
0
    msg_index = one_index + 1;
222
0
    mlen = dblen - msg_index;
223
224
0
    if (tlen < mlen) {
225
0
        RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, RSA_R_DATA_TOO_LARGE);
226
0
        mlen = -1;
227
0
    } else {
228
0
        memcpy(to, db + msg_index, mlen);
229
0
        goto cleanup;
230
0
    }
231
232
0
 decoding_err:
233
    /*
234
     * To avoid chosen ciphertext attacks, the error message should not
235
     * reveal which kind of decoding error happened.
236
     */
237
0
    RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1,
238
0
           RSA_R_OAEP_DECODING_ERROR);
239
0
 cleanup:
240
0
    if (db != NULL) {
241
0
        OPENSSL_cleanse(db, dblen);
242
0
        OPENSSL_free(db);
243
0
    }
244
0
    if (em != NULL) {
245
0
        OPENSSL_cleanse(em, num);
246
0
        OPENSSL_free(em);
247
0
    }
248
0
    return mlen;
249
0
}
250
251
int PKCS1_MGF1(unsigned char *mask, long len,
252
               const unsigned char *seed, long seedlen, const EVP_MD *dgst)
253
0
{
254
0
    long i, outlen = 0;
255
0
    unsigned char cnt[4];
256
0
    EVP_MD_CTX c;
257
0
    unsigned char md[EVP_MAX_MD_SIZE];
258
0
    int mdlen;
259
0
    int rv = -1;
260
261
0
    EVP_MD_CTX_init(&c);
262
0
    mdlen = EVP_MD_size(dgst);
263
0
    if (mdlen < 0)
264
0
        goto err;
265
0
    for (i = 0; outlen < len; i++) {
266
0
        cnt[0] = (unsigned char)((i >> 24) & 255);
267
0
        cnt[1] = (unsigned char)((i >> 16) & 255);
268
0
        cnt[2] = (unsigned char)((i >> 8)) & 255;
269
0
        cnt[3] = (unsigned char)(i & 255);
270
0
        if (!EVP_DigestInit_ex(&c, dgst, NULL)
271
0
            || !EVP_DigestUpdate(&c, seed, seedlen)
272
0
            || !EVP_DigestUpdate(&c, cnt, 4))
273
0
            goto err;
274
0
        if (outlen + mdlen <= len) {
275
0
            if (!EVP_DigestFinal_ex(&c, mask + outlen, NULL))
276
0
                goto err;
277
0
            outlen += mdlen;
278
0
        } else {
279
0
            if (!EVP_DigestFinal_ex(&c, md, NULL))
280
0
                goto err;
281
0
            memcpy(mask + outlen, md, len - outlen);
282
0
            outlen = len;
283
0
        }
284
0
    }
285
0
    rv = 0;
286
0
 err:
287
0
    EVP_MD_CTX_cleanup(&c);
288
0
    return rv;
289
0
}
290
291
#endif