VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.3/crypto/rsa/rsa_pss.c@ 102334

最後變更 在這個檔案從102334是 101211,由 vboxsync 提交於 17 月 前

openssl-3.1.3: Applied and adjusted our OpenSSL changes to 3.1.2. bugref:10527

檔案大小: 12.2 KB
 
1/*
2 * Copyright 2005-2022 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (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/*
11 * RSA low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14#include "internal/deprecated.h"
15
16#include <stdio.h>
17#include "internal/cryptlib.h"
18#include <openssl/bn.h>
19#include <openssl/rsa.h>
20#include <openssl/evp.h>
21#include <openssl/rand.h>
22#include <openssl/sha.h>
23#include "rsa_local.h"
24
25static const unsigned char zeroes[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
26
27#if defined(_MSC_VER) && defined(_ARM_)
28# pragma optimize("g", off)
29#endif
30
31int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash,
32 const EVP_MD *Hash, const unsigned char *EM,
33 int sLen)
34{
35 return RSA_verify_PKCS1_PSS_mgf1(rsa, mHash, Hash, NULL, EM, sLen);
36}
37
38int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const unsigned char *mHash,
39 const EVP_MD *Hash, const EVP_MD *mgf1Hash,
40 const unsigned char *EM, int sLen)
41{
42 int i;
43 int ret = 0;
44 int hLen, maskedDBLen, MSBits, emLen;
45 const unsigned char *H;
46 unsigned char *DB = NULL;
47 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
48 unsigned char H_[EVP_MAX_MD_SIZE];
49
50 if (ctx == NULL)
51 goto err;
52
53 if (mgf1Hash == NULL)
54 mgf1Hash = Hash;
55
56 hLen = EVP_MD_get_size(Hash);
57 if (hLen < 0)
58 goto err;
59 /*-
60 * Negative sLen has special meanings:
61 * -1 sLen == hLen
62 * -2 salt length is autorecovered from signature
63 * -3 salt length is maximized
64 * -4 salt length is autorecovered from signature
65 * -N reserved
66 */
67 if (sLen == RSA_PSS_SALTLEN_DIGEST) {
68 sLen = hLen;
69 } else if (sLen < RSA_PSS_SALTLEN_AUTO_DIGEST_MAX) {
70 ERR_raise(ERR_LIB_RSA, RSA_R_SLEN_CHECK_FAILED);
71 goto err;
72 }
73
74 MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
75 emLen = RSA_size(rsa);
76 if (EM[0] & (0xFF << MSBits)) {
77 ERR_raise(ERR_LIB_RSA, RSA_R_FIRST_OCTET_INVALID);
78 goto err;
79 }
80 if (MSBits == 0) {
81 EM++;
82 emLen--;
83 }
84 if (emLen < hLen + 2) {
85 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE);
86 goto err;
87 }
88 if (sLen == RSA_PSS_SALTLEN_MAX) {
89 sLen = emLen - hLen - 2;
90 } else if (sLen > emLen - hLen - 2) { /* sLen can be small negative */
91 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE);
92 goto err;
93 }
94 if (EM[emLen - 1] != 0xbc) {
95 ERR_raise(ERR_LIB_RSA, RSA_R_LAST_OCTET_INVALID);
96 goto err;
97 }
98 maskedDBLen = emLen - hLen - 1;
99 H = EM + maskedDBLen;
100 DB = OPENSSL_malloc(maskedDBLen);
101 if (DB == NULL) {
102 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
103 goto err;
104 }
105 if (PKCS1_MGF1(DB, maskedDBLen, H, hLen, mgf1Hash) < 0)
106 goto err;
107 for (i = 0; i < maskedDBLen; i++)
108 DB[i] ^= EM[i];
109 if (MSBits)
110 DB[0] &= 0xFF >> (8 - MSBits);
111 for (i = 0; DB[i] == 0 && i < (maskedDBLen - 1); i++) ;
112 if (DB[i++] != 0x1) {
113 ERR_raise(ERR_LIB_RSA, RSA_R_SLEN_RECOVERY_FAILED);
114 goto err;
115 }
116 if (sLen != RSA_PSS_SALTLEN_AUTO
117 && sLen != RSA_PSS_SALTLEN_AUTO_DIGEST_MAX
118 && (maskedDBLen - i) != sLen) {
119 ERR_raise_data(ERR_LIB_RSA, RSA_R_SLEN_CHECK_FAILED,
120 "expected: %d retrieved: %d", sLen,
121 maskedDBLen - i);
122 goto err;
123 }
124 if (!EVP_DigestInit_ex(ctx, Hash, NULL)
125 || !EVP_DigestUpdate(ctx, zeroes, sizeof(zeroes))
126 || !EVP_DigestUpdate(ctx, mHash, hLen))
127 goto err;
128 if (maskedDBLen - i) {
129 if (!EVP_DigestUpdate(ctx, DB + i, maskedDBLen - i))
130 goto err;
131 }
132 if (!EVP_DigestFinal_ex(ctx, H_, NULL))
133 goto err;
134 if (memcmp(H_, H, hLen)) {
135 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
136 ret = 0;
137 } else {
138 ret = 1;
139 }
140
141 err:
142 OPENSSL_free(DB);
143 EVP_MD_CTX_free(ctx);
144
145 return ret;
146
147}
148
149int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM,
150 const unsigned char *mHash,
151 const EVP_MD *Hash, int sLen)
152{
153 return RSA_padding_add_PKCS1_PSS_mgf1(rsa, EM, mHash, Hash, NULL, sLen);
154}
155
156int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM,
157 const unsigned char *mHash,
158 const EVP_MD *Hash, const EVP_MD *mgf1Hash,
159 int sLen)
160{
161 int i;
162 int ret = 0;
163 int hLen, maskedDBLen, MSBits, emLen;
164 unsigned char *H, *salt = NULL, *p;
165 EVP_MD_CTX *ctx = NULL;
166 int sLenMax = -1;
167
168 if (mgf1Hash == NULL)
169 mgf1Hash = Hash;
170
171 hLen = EVP_MD_get_size(Hash);
172 if (hLen < 0)
173 goto err;
174 /*-
175 * Negative sLen has special meanings:
176 * -1 sLen == hLen
177 * -2 salt length is maximized
178 * -3 same as above (on signing)
179 * -4 salt length is min(hLen, maximum salt length)
180 * -N reserved
181 */
182 /* FIPS 186-4 section 5 "The RSA Digital Signature Algorithm", subsection
183 * 5.5 "PKCS #1" says: "For RSASSA-PSS […] the length (in bytes) of the
184 * salt (sLen) shall satisfy 0 <= sLen <= hLen, where hLen is the length of
185 * the hash function output block (in bytes)."
186 *
187 * Provide a way to use at most the digest length, so that the default does
188 * not violate FIPS 186-4. */
189 if (sLen == RSA_PSS_SALTLEN_DIGEST) {
190 sLen = hLen;
191 } else if (sLen == RSA_PSS_SALTLEN_MAX_SIGN
192 || sLen == RSA_PSS_SALTLEN_AUTO) {
193 sLen = RSA_PSS_SALTLEN_MAX;
194 } else if (sLen == RSA_PSS_SALTLEN_AUTO_DIGEST_MAX) {
195 sLen = RSA_PSS_SALTLEN_MAX;
196 sLenMax = hLen;
197 } else if (sLen < RSA_PSS_SALTLEN_AUTO_DIGEST_MAX) {
198 ERR_raise(ERR_LIB_RSA, RSA_R_SLEN_CHECK_FAILED);
199 goto err;
200 }
201
202 MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
203 emLen = RSA_size(rsa);
204 if (MSBits == 0) {
205 *EM++ = 0;
206 emLen--;
207 }
208 if (emLen < hLen + 2) {
209 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
210 goto err;
211 }
212 if (sLen == RSA_PSS_SALTLEN_MAX) {
213 sLen = emLen - hLen - 2;
214 if (sLenMax >= 0 && sLen > sLenMax)
215 sLen = sLenMax;
216 } else if (sLen > emLen - hLen - 2) {
217 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
218 goto err;
219 }
220 if (sLen > 0) {
221 salt = OPENSSL_malloc(sLen);
222 if (salt == NULL) {
223 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
224 goto err;
225 }
226 if (RAND_bytes_ex(rsa->libctx, salt, sLen, 0) <= 0)
227 goto err;
228 }
229 maskedDBLen = emLen - hLen - 1;
230 H = EM + maskedDBLen;
231 ctx = EVP_MD_CTX_new();
232 if (ctx == NULL)
233 goto err;
234 if (!EVP_DigestInit_ex(ctx, Hash, NULL)
235 || !EVP_DigestUpdate(ctx, zeroes, sizeof(zeroes))
236 || !EVP_DigestUpdate(ctx, mHash, hLen))
237 goto err;
238 if (sLen && !EVP_DigestUpdate(ctx, salt, sLen))
239 goto err;
240 if (!EVP_DigestFinal_ex(ctx, H, NULL))
241 goto err;
242
243 /* Generate dbMask in place then perform XOR on it */
244 if (PKCS1_MGF1(EM, maskedDBLen, H, hLen, mgf1Hash))
245 goto err;
246
247 p = EM;
248
249 /*
250 * Initial PS XORs with all zeroes which is a NOP so just update pointer.
251 * Note from a test above this value is guaranteed to be non-negative.
252 */
253 p += emLen - sLen - hLen - 2;
254 *p++ ^= 0x1;
255 if (sLen > 0) {
256 for (i = 0; i < sLen; i++)
257 *p++ ^= salt[i];
258 }
259 if (MSBits)
260 EM[0] &= 0xFF >> (8 - MSBits);
261
262 /* H is already in place so just set final 0xbc */
263
264 EM[emLen - 1] = 0xbc;
265
266 ret = 1;
267
268 err:
269 EVP_MD_CTX_free(ctx);
270 OPENSSL_clear_free(salt, (size_t)sLen); /* salt != NULL implies sLen > 0 */
271
272 return ret;
273
274}
275
276/*
277 * The defaults for PSS restrictions are defined in RFC 8017, A.2.3 RSASSA-PSS
278 * (https://tools.ietf.org/html/rfc8017#appendix-A.2.3):
279 *
280 * If the default values of the hashAlgorithm, maskGenAlgorithm, and
281 * trailerField fields of RSASSA-PSS-params are used, then the algorithm
282 * identifier will have the following value:
283 *
284 * rSASSA-PSS-Default-Identifier RSASSA-AlgorithmIdentifier ::= {
285 * algorithm id-RSASSA-PSS,
286 * parameters RSASSA-PSS-params : {
287 * hashAlgorithm sha1,
288 * maskGenAlgorithm mgf1SHA1,
289 * saltLength 20,
290 * trailerField trailerFieldBC
291 * }
292 * }
293 *
294 * RSASSA-AlgorithmIdentifier ::= AlgorithmIdentifier {
295 * {PKCS1Algorithms}
296 * }
297 */
298static const RSA_PSS_PARAMS_30 default_RSASSA_PSS_params = {
299 NID_sha1, /* default hashAlgorithm */
300 {
301 NID_mgf1, /* default maskGenAlgorithm */
302 NID_sha1 /* default MGF1 hash */
303 },
304 20, /* default saltLength */
305 1 /* default trailerField (0xBC) */
306};
307
308int ossl_rsa_pss_params_30_set_defaults(RSA_PSS_PARAMS_30 *rsa_pss_params)
309{
310 if (rsa_pss_params == NULL)
311 return 0;
312 *rsa_pss_params = default_RSASSA_PSS_params;
313 return 1;
314}
315
316int ossl_rsa_pss_params_30_is_unrestricted(const RSA_PSS_PARAMS_30 *rsa_pss_params)
317{
318 static RSA_PSS_PARAMS_30 pss_params_cmp = { 0, };
319
320 return rsa_pss_params == NULL
321 || memcmp(rsa_pss_params, &pss_params_cmp,
322 sizeof(*rsa_pss_params)) == 0;
323}
324
325int ossl_rsa_pss_params_30_copy(RSA_PSS_PARAMS_30 *to,
326 const RSA_PSS_PARAMS_30 *from)
327{
328 memcpy(to, from, sizeof(*to));
329 return 1;
330}
331
332int ossl_rsa_pss_params_30_set_hashalg(RSA_PSS_PARAMS_30 *rsa_pss_params,
333 int hashalg_nid)
334{
335 if (rsa_pss_params == NULL)
336 return 0;
337 rsa_pss_params->hash_algorithm_nid = hashalg_nid;
338 return 1;
339}
340
341int ossl_rsa_pss_params_30_set_maskgenalg(RSA_PSS_PARAMS_30 *rsa_pss_params,
342 int maskgenalg_nid)
343{
344 if (rsa_pss_params == NULL)
345 return 0;
346 rsa_pss_params->mask_gen.algorithm_nid = maskgenalg_nid;
347 return 1;
348}
349
350int ossl_rsa_pss_params_30_set_maskgenhashalg(RSA_PSS_PARAMS_30 *rsa_pss_params,
351 int maskgenhashalg_nid)
352{
353 if (rsa_pss_params == NULL)
354 return 0;
355 rsa_pss_params->mask_gen.hash_algorithm_nid = maskgenhashalg_nid;
356 return 1;
357}
358
359int ossl_rsa_pss_params_30_set_saltlen(RSA_PSS_PARAMS_30 *rsa_pss_params,
360 int saltlen)
361{
362 if (rsa_pss_params == NULL)
363 return 0;
364 rsa_pss_params->salt_len = saltlen;
365 return 1;
366}
367
368int ossl_rsa_pss_params_30_set_trailerfield(RSA_PSS_PARAMS_30 *rsa_pss_params,
369 int trailerfield)
370{
371 if (rsa_pss_params == NULL)
372 return 0;
373 rsa_pss_params->trailer_field = trailerfield;
374 return 1;
375}
376
377int ossl_rsa_pss_params_30_hashalg(const RSA_PSS_PARAMS_30 *rsa_pss_params)
378{
379 if (rsa_pss_params == NULL)
380 return default_RSASSA_PSS_params.hash_algorithm_nid;
381 return rsa_pss_params->hash_algorithm_nid;
382}
383
384int ossl_rsa_pss_params_30_maskgenalg(const RSA_PSS_PARAMS_30 *rsa_pss_params)
385{
386 if (rsa_pss_params == NULL)
387 return default_RSASSA_PSS_params.mask_gen.algorithm_nid;
388 return rsa_pss_params->mask_gen.algorithm_nid;
389}
390
391int ossl_rsa_pss_params_30_maskgenhashalg(const RSA_PSS_PARAMS_30 *rsa_pss_params)
392{
393 if (rsa_pss_params == NULL)
394 return default_RSASSA_PSS_params.hash_algorithm_nid;
395 return rsa_pss_params->mask_gen.hash_algorithm_nid;
396}
397
398int ossl_rsa_pss_params_30_saltlen(const RSA_PSS_PARAMS_30 *rsa_pss_params)
399{
400 if (rsa_pss_params == NULL)
401 return default_RSASSA_PSS_params.salt_len;
402 return rsa_pss_params->salt_len;
403}
404
405int ossl_rsa_pss_params_30_trailerfield(const RSA_PSS_PARAMS_30 *rsa_pss_params)
406{
407 if (rsa_pss_params == NULL)
408 return default_RSASSA_PSS_params.trailer_field;
409 return rsa_pss_params->trailer_field;
410}
411
412#if defined(_MSC_VER)
413# pragma optimize("",on)
414#endif
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette