/src/openssl111/crypto/rsa/rsa_oaep.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* | 
| 2 |  |  * Copyright 1999-2019 The OpenSSL Project Authors. All Rights Reserved. | 
| 3 |  |  * | 
| 4 |  |  * Licensed under the OpenSSL license (the "License").  You may not use | 
| 5 |  |  * this file except in compliance with the License.  You can obtain a copy | 
| 6 |  |  * in the file LICENSE in the source distribution or at | 
| 7 |  |  * https://www.openssl.org/source/license.html | 
| 8 |  |  */ | 
| 9 |  |  | 
| 10 |  | /* EME-OAEP as defined in RFC 2437 (PKCS #1 v2.0) */ | 
| 11 |  |  | 
| 12 |  | /* | 
| 13 |  |  * See Victor Shoup, "OAEP reconsidered," Nov. 2000, <URL: | 
| 14 |  |  * http://www.shoup.net/papers/oaep.ps.Z> for problems with the security | 
| 15 |  |  * proof for the original OAEP scheme, which EME-OAEP is based on. A new | 
| 16 |  |  * proof can be found in E. Fujisaki, T. Okamoto, D. Pointcheval, J. Stern, | 
| 17 |  |  * "RSA-OEAP is Still Alive!", Dec. 2000, <URL: | 
| 18 |  |  * http://eprint.iacr.org/2000/061/>. The new proof has stronger requirements | 
| 19 |  |  * for the underlying permutation: "partial-one-wayness" instead of | 
| 20 |  |  * one-wayness.  For the RSA function, this is an equivalent notion. | 
| 21 |  |  */ | 
| 22 |  |  | 
| 23 |  | #include "internal/constant_time.h" | 
| 24 |  |  | 
| 25 |  | #include <stdio.h> | 
| 26 |  | #include "internal/cryptlib.h" | 
| 27 |  | #include <openssl/bn.h> | 
| 28 |  | #include <openssl/evp.h> | 
| 29 |  | #include <openssl/rand.h> | 
| 30 |  | #include <openssl/sha.h> | 
| 31 |  | #include "rsa_local.h" | 
| 32 |  |  | 
| 33 |  | int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen, | 
| 34 |  |                                const unsigned char *from, int flen, | 
| 35 |  |                                const unsigned char *param, int plen) | 
| 36 | 0 | { | 
| 37 | 0 |     return RSA_padding_add_PKCS1_OAEP_mgf1(to, tlen, from, flen, | 
| 38 | 0 |                                            param, plen, NULL, NULL); | 
| 39 | 0 | } | 
| 40 |  |  | 
| 41 |  | int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen, | 
| 42 |  |                                     const unsigned char *from, int flen, | 
| 43 |  |                                     const unsigned char *param, int plen, | 
| 44 |  |                                     const EVP_MD *md, const EVP_MD *mgf1md) | 
| 45 | 0 | { | 
| 46 | 0 |     int rv = 0; | 
| 47 | 0 |     int i, emlen = tlen - 1; | 
| 48 | 0 |     unsigned char *db, *seed; | 
| 49 | 0 |     unsigned char *dbmask = NULL; | 
| 50 | 0 |     unsigned char seedmask[EVP_MAX_MD_SIZE]; | 
| 51 | 0 |     int mdlen, dbmask_len = 0; | 
| 52 |  | 
 | 
| 53 | 0 |     if (md == NULL) | 
| 54 | 0 |         md = EVP_sha1(); | 
| 55 | 0 |     if (mgf1md == NULL) | 
| 56 | 0 |         mgf1md = md; | 
| 57 |  | 
 | 
| 58 | 0 |     mdlen = EVP_MD_size(md); | 
| 59 |  | 
 | 
| 60 | 0 |     if (flen > emlen - 2 * mdlen - 1) { | 
| 61 | 0 |         RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1, | 
| 62 | 0 |                RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE); | 
| 63 | 0 |         return 0; | 
| 64 | 0 |     } | 
| 65 |  |  | 
| 66 | 0 |     if (emlen < 2 * mdlen + 1) { | 
| 67 | 0 |         RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1, | 
| 68 | 0 |                RSA_R_KEY_SIZE_TOO_SMALL); | 
| 69 | 0 |         return 0; | 
| 70 | 0 |     } | 
| 71 |  |  | 
| 72 | 0 |     to[0] = 0; | 
| 73 | 0 |     seed = to + 1; | 
| 74 | 0 |     db = to + mdlen + 1; | 
| 75 |  | 
 | 
| 76 | 0 |     if (!EVP_Digest((void *)param, plen, db, NULL, md, NULL)) | 
| 77 | 0 |         goto err; | 
| 78 | 0 |     memset(db + mdlen, 0, emlen - flen - 2 * mdlen - 1); | 
| 79 | 0 |     db[emlen - flen - mdlen - 1] = 0x01; | 
| 80 | 0 |     memcpy(db + emlen - flen - mdlen, from, (unsigned int)flen); | 
| 81 | 0 |     if (RAND_bytes(seed, mdlen) <= 0) | 
| 82 | 0 |         goto err; | 
| 83 |  |  | 
| 84 | 0 |     dbmask_len = emlen - mdlen; | 
| 85 | 0 |     dbmask = OPENSSL_malloc(dbmask_len); | 
| 86 | 0 |     if (dbmask == NULL) { | 
| 87 | 0 |         RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE); | 
| 88 | 0 |         goto err; | 
| 89 | 0 |     } | 
| 90 |  |  | 
| 91 | 0 |     if (PKCS1_MGF1(dbmask, dbmask_len, seed, mdlen, mgf1md) < 0) | 
| 92 | 0 |         goto err; | 
| 93 | 0 |     for (i = 0; i < dbmask_len; i++) | 
| 94 | 0 |         db[i] ^= dbmask[i]; | 
| 95 |  | 
 | 
| 96 | 0 |     if (PKCS1_MGF1(seedmask, mdlen, db, dbmask_len, mgf1md) < 0) | 
| 97 | 0 |         goto err; | 
| 98 | 0 |     for (i = 0; i < mdlen; i++) | 
| 99 | 0 |         seed[i] ^= seedmask[i]; | 
| 100 | 0 |     rv = 1; | 
| 101 |  | 
 | 
| 102 | 0 |  err: | 
| 103 | 0 |     OPENSSL_cleanse(seedmask, sizeof(seedmask)); | 
| 104 | 0 |     OPENSSL_clear_free(dbmask, dbmask_len); | 
| 105 | 0 |     return rv; | 
| 106 | 0 | } | 
| 107 |  |  | 
| 108 |  | int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen, | 
| 109 |  |                                  const unsigned char *from, int flen, int num, | 
| 110 |  |                                  const unsigned char *param, int plen) | 
| 111 | 0 | { | 
| 112 | 0 |     return RSA_padding_check_PKCS1_OAEP_mgf1(to, tlen, from, flen, num, | 
| 113 | 0 |                                              param, plen, NULL, NULL); | 
| 114 | 0 | } | 
| 115 |  |  | 
| 116 |  | int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen, | 
| 117 |  |                                       const unsigned char *from, int flen, | 
| 118 |  |                                       int num, const unsigned char *param, | 
| 119 |  |                                       int plen, const EVP_MD *md, | 
| 120 |  |                                       const EVP_MD *mgf1md) | 
| 121 | 0 | { | 
| 122 | 0 |     int i, dblen = 0, mlen = -1, one_index = 0, msg_index; | 
| 123 | 0 |     unsigned int good = 0, found_one_byte, mask; | 
| 124 | 0 |     const unsigned char *maskedseed, *maskeddb; | 
| 125 |  |     /* | 
| 126 |  |      * |em| is the encoded message, zero-padded to exactly |num| bytes: em = | 
| 127 |  |      * Y || maskedSeed || maskedDB | 
| 128 |  |      */ | 
| 129 | 0 |     unsigned char *db = NULL, *em = NULL, seed[EVP_MAX_MD_SIZE], | 
| 130 | 0 |         phash[EVP_MAX_MD_SIZE]; | 
| 131 | 0 |     int mdlen; | 
| 132 |  | 
 | 
| 133 | 0 |     if (md == NULL) | 
| 134 | 0 |         md = EVP_sha1(); | 
| 135 | 0 |     if (mgf1md == NULL) | 
| 136 | 0 |         mgf1md = md; | 
| 137 |  | 
 | 
| 138 | 0 |     mdlen = EVP_MD_size(md); | 
| 139 |  | 
 | 
| 140 | 0 |     if (tlen <= 0 || flen <= 0) | 
| 141 | 0 |         return -1; | 
| 142 |  |     /* | 
| 143 |  |      * |num| is the length of the modulus; |flen| is the length of the | 
| 144 |  |      * encoded message. Therefore, for any |from| that was obtained by | 
| 145 |  |      * decrypting a ciphertext, we must have |flen| <= |num|. Similarly, | 
| 146 |  |      * |num| >= 2 * |mdlen| + 2 must hold for the modulus irrespective of | 
| 147 |  |      * the ciphertext, see PKCS #1 v2.2, section 7.1.2. | 
| 148 |  |      * This does not leak any side-channel information. | 
| 149 |  |      */ | 
| 150 | 0 |     if (num < flen || num < 2 * mdlen + 2) { | 
| 151 | 0 |         RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, | 
| 152 | 0 |                RSA_R_OAEP_DECODING_ERROR); | 
| 153 | 0 |         return -1; | 
| 154 | 0 |     } | 
| 155 |  |  | 
| 156 | 0 |     dblen = num - mdlen - 1; | 
| 157 | 0 |     db = OPENSSL_malloc(dblen); | 
| 158 | 0 |     if (db == NULL) { | 
| 159 | 0 |         RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, ERR_R_MALLOC_FAILURE); | 
| 160 | 0 |         goto cleanup; | 
| 161 | 0 |     } | 
| 162 |  |  | 
| 163 | 0 |     em = OPENSSL_malloc(num); | 
| 164 | 0 |     if (em == NULL) { | 
| 165 | 0 |         RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, | 
| 166 | 0 |                ERR_R_MALLOC_FAILURE); | 
| 167 | 0 |         goto cleanup; | 
| 168 | 0 |     } | 
| 169 |  |  | 
| 170 |  |     /* | 
| 171 |  |      * Caller is encouraged to pass zero-padded message created with | 
| 172 |  |      * BN_bn2binpad. Trouble is that since we can't read out of |from|'s | 
| 173 |  |      * bounds, it's impossible to have an invariant memory access pattern | 
| 174 |  |      * in case |from| was not zero-padded in advance. | 
| 175 |  |      */ | 
| 176 | 0 |     for (from += flen, em += num, i = 0; i < num; i++) { | 
| 177 | 0 |         mask = ~constant_time_is_zero(flen); | 
| 178 | 0 |         flen -= 1 & mask; | 
| 179 | 0 |         from -= 1 & mask; | 
| 180 | 0 |         *--em = *from & mask; | 
| 181 | 0 |     } | 
| 182 |  |  | 
| 183 |  |     /* | 
| 184 |  |      * The first byte must be zero, however we must not leak if this is | 
| 185 |  |      * true. See James H. Manger, "A Chosen Ciphertext  Attack on RSA | 
| 186 |  |      * Optimal Asymmetric Encryption Padding (OAEP) [...]", CRYPTO 2001). | 
| 187 |  |      */ | 
| 188 | 0 |     good = constant_time_is_zero(em[0]); | 
| 189 |  | 
 | 
| 190 | 0 |     maskedseed = em + 1; | 
| 191 | 0 |     maskeddb = em + 1 + mdlen; | 
| 192 |  | 
 | 
| 193 | 0 |     if (PKCS1_MGF1(seed, mdlen, maskeddb, dblen, mgf1md)) | 
| 194 | 0 |         goto cleanup; | 
| 195 | 0 |     for (i = 0; i < mdlen; i++) | 
| 196 | 0 |         seed[i] ^= maskedseed[i]; | 
| 197 |  | 
 | 
| 198 | 0 |     if (PKCS1_MGF1(db, dblen, seed, mdlen, mgf1md)) | 
| 199 | 0 |         goto cleanup; | 
| 200 | 0 |     for (i = 0; i < dblen; i++) | 
| 201 | 0 |         db[i] ^= maskeddb[i]; | 
| 202 |  | 
 | 
| 203 | 0 |     if (!EVP_Digest((void *)param, plen, phash, NULL, md, NULL)) | 
| 204 | 0 |         goto cleanup; | 
| 205 |  |  | 
| 206 | 0 |     good &= constant_time_is_zero(CRYPTO_memcmp(db, phash, mdlen)); | 
| 207 |  | 
 | 
| 208 | 0 |     found_one_byte = 0; | 
| 209 | 0 |     for (i = mdlen; i < dblen; i++) { | 
| 210 |  |         /* | 
| 211 |  |          * Padding consists of a number of 0-bytes, followed by a 1. | 
| 212 |  |          */ | 
| 213 | 0 |         unsigned int equals1 = constant_time_eq(db[i], 1); | 
| 214 | 0 |         unsigned int equals0 = constant_time_is_zero(db[i]); | 
| 215 | 0 |         one_index = constant_time_select_int(~found_one_byte & equals1, | 
| 216 | 0 |                                              i, one_index); | 
| 217 | 0 |         found_one_byte |= equals1; | 
| 218 | 0 |         good &= (found_one_byte | equals0); | 
| 219 | 0 |     } | 
| 220 |  | 
 | 
| 221 | 0 |     good &= found_one_byte; | 
| 222 |  |  | 
| 223 |  |     /* | 
| 224 |  |      * At this point |good| is zero unless the plaintext was valid, | 
| 225 |  |      * so plaintext-awareness ensures timing side-channels are no longer a | 
| 226 |  |      * concern. | 
| 227 |  |      */ | 
| 228 | 0 |     msg_index = one_index + 1; | 
| 229 | 0 |     mlen = dblen - msg_index; | 
| 230 |  |  | 
| 231 |  |     /* | 
| 232 |  |      * For good measure, do this check in constant time as well. | 
| 233 |  |      */ | 
| 234 | 0 |     good &= constant_time_ge(tlen, mlen); | 
| 235 |  |  | 
| 236 |  |     /* | 
| 237 |  |      * Move the result in-place by |dblen|-|mdlen|-1-|mlen| bytes to the left. | 
| 238 |  |      * Then if |good| move |mlen| bytes from |db|+|mdlen|+1 to |to|. | 
| 239 |  |      * Otherwise leave |to| unchanged. | 
| 240 |  |      * Copy the memory back in a way that does not reveal the size of | 
| 241 |  |      * the data being copied via a timing side channel. This requires copying | 
| 242 |  |      * parts of the buffer multiple times based on the bits set in the real | 
| 243 |  |      * length. Clear bits do a non-copy with identical access pattern. | 
| 244 |  |      * The loop below has overall complexity of O(N*log(N)). | 
| 245 |  |      */ | 
| 246 | 0 |     tlen = constant_time_select_int(constant_time_lt(dblen - mdlen - 1, tlen), | 
| 247 | 0 |                                     dblen - mdlen - 1, tlen); | 
| 248 | 0 |     for (msg_index = 1; msg_index < dblen - mdlen - 1; msg_index <<= 1) { | 
| 249 | 0 |         mask = ~constant_time_eq(msg_index & (dblen - mdlen - 1 - mlen), 0); | 
| 250 | 0 |         for (i = mdlen + 1; i < dblen - msg_index; i++) | 
| 251 | 0 |             db[i] = constant_time_select_8(mask, db[i + msg_index], db[i]); | 
| 252 | 0 |     } | 
| 253 | 0 |     for (i = 0; i < tlen; i++) { | 
| 254 | 0 |         mask = good & constant_time_lt(i, mlen); | 
| 255 | 0 |         to[i] = constant_time_select_8(mask, db[i + mdlen + 1], to[i]); | 
| 256 | 0 |     } | 
| 257 |  |  | 
| 258 |  |     /* | 
| 259 |  |      * To avoid chosen ciphertext attacks, the error message should not | 
| 260 |  |      * reveal which kind of decoding error happened. | 
| 261 |  |      */ | 
| 262 | 0 |     RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1, | 
| 263 | 0 |            RSA_R_OAEP_DECODING_ERROR); | 
| 264 | 0 |     err_clear_last_constant_time(1 & good); | 
| 265 | 0 |  cleanup: | 
| 266 | 0 |     OPENSSL_cleanse(seed, sizeof(seed)); | 
| 267 | 0 |     OPENSSL_clear_free(db, dblen); | 
| 268 | 0 |     OPENSSL_clear_free(em, num); | 
| 269 |  | 
 | 
| 270 | 0 |     return constant_time_select_int(good, mlen, -1); | 
| 271 | 0 | } | 
| 272 |  |  | 
| 273 |  | int PKCS1_MGF1(unsigned char *mask, long len, | 
| 274 |  |                const unsigned char *seed, long seedlen, const EVP_MD *dgst) | 
| 275 | 0 | { | 
| 276 | 0 |     long i, outlen = 0; | 
| 277 | 0 |     unsigned char cnt[4]; | 
| 278 | 0 |     EVP_MD_CTX *c = EVP_MD_CTX_new(); | 
| 279 | 0 |     unsigned char md[EVP_MAX_MD_SIZE]; | 
| 280 | 0 |     int mdlen; | 
| 281 | 0 |     int rv = -1; | 
| 282 |  | 
 | 
| 283 | 0 |     if (c == NULL) | 
| 284 | 0 |         goto err; | 
| 285 | 0 |     mdlen = EVP_MD_size(dgst); | 
| 286 | 0 |     if (mdlen < 0) | 
| 287 | 0 |         goto err; | 
| 288 | 0 |     for (i = 0; outlen < len; i++) { | 
| 289 | 0 |         cnt[0] = (unsigned char)((i >> 24) & 255); | 
| 290 | 0 |         cnt[1] = (unsigned char)((i >> 16) & 255); | 
| 291 | 0 |         cnt[2] = (unsigned char)((i >> 8)) & 255; | 
| 292 | 0 |         cnt[3] = (unsigned char)(i & 255); | 
| 293 | 0 |         if (!EVP_DigestInit_ex(c, dgst, NULL) | 
| 294 | 0 |             || !EVP_DigestUpdate(c, seed, seedlen) | 
| 295 | 0 |             || !EVP_DigestUpdate(c, cnt, 4)) | 
| 296 | 0 |             goto err; | 
| 297 | 0 |         if (outlen + mdlen <= len) { | 
| 298 | 0 |             if (!EVP_DigestFinal_ex(c, mask + outlen, NULL)) | 
| 299 | 0 |                 goto err; | 
| 300 | 0 |             outlen += mdlen; | 
| 301 | 0 |         } else { | 
| 302 | 0 |             if (!EVP_DigestFinal_ex(c, md, NULL)) | 
| 303 | 0 |                 goto err; | 
| 304 | 0 |             memcpy(mask + outlen, md, len - outlen); | 
| 305 | 0 |             outlen = len; | 
| 306 | 0 |         } | 
| 307 | 0 |     } | 
| 308 | 0 |     rv = 0; | 
| 309 | 0 |  err: | 
| 310 | 0 |     OPENSSL_cleanse(md, sizeof(md)); | 
| 311 | 0 |     EVP_MD_CTX_free(c); | 
| 312 | 0 |     return rv; | 
| 313 | 0 | } |