2 * Copyright 2005-2021 The OpenSSL Project Authors. All Rights Reserved.
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
11 * RSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.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"
25 static const unsigned char zeroes
[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
27 #if defined(_MSC_VER) && defined(_ARM_)
28 # pragma optimize("g", off)
31 int RSA_verify_PKCS1_PSS(RSA
*rsa
, const unsigned char *mHash
,
32 const EVP_MD
*Hash
, const unsigned char *EM
,
35 return RSA_verify_PKCS1_PSS_mgf1(rsa
, mHash
, Hash
, NULL
, EM
, sLen
);
38 int 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
)
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
];
56 hLen
= EVP_MD_size(Hash
);
60 * Negative sLen has special meanings:
62 * -2 salt length is autorecovered from signature
63 * -3 salt length is maximized
66 if (sLen
== RSA_PSS_SALTLEN_DIGEST
) {
68 } else if (sLen
< RSA_PSS_SALTLEN_MAX
) {
69 ERR_raise(ERR_LIB_RSA
, RSA_R_SLEN_CHECK_FAILED
);
73 MSBits
= (BN_num_bits(rsa
->n
) - 1) & 0x7;
74 emLen
= RSA_size(rsa
);
75 if (EM
[0] & (0xFF << MSBits
)) {
76 ERR_raise(ERR_LIB_RSA
, RSA_R_FIRST_OCTET_INVALID
);
83 if (emLen
< hLen
+ 2) {
84 ERR_raise(ERR_LIB_RSA
, RSA_R_DATA_TOO_LARGE
);
87 if (sLen
== RSA_PSS_SALTLEN_MAX
) {
88 sLen
= emLen
- hLen
- 2;
89 } else if (sLen
> emLen
- hLen
- 2) { /* sLen can be small negative */
90 ERR_raise(ERR_LIB_RSA
, RSA_R_DATA_TOO_LARGE
);
93 if (EM
[emLen
- 1] != 0xbc) {
94 ERR_raise(ERR_LIB_RSA
, RSA_R_LAST_OCTET_INVALID
);
97 maskedDBLen
= emLen
- hLen
- 1;
99 DB
= OPENSSL_malloc(maskedDBLen
);
101 ERR_raise(ERR_LIB_RSA
, ERR_R_MALLOC_FAILURE
);
104 if (PKCS1_MGF1(DB
, maskedDBLen
, H
, hLen
, mgf1Hash
) < 0)
106 for (i
= 0; i
< maskedDBLen
; i
++)
109 DB
[0] &= 0xFF >> (8 - MSBits
);
110 for (i
= 0; DB
[i
] == 0 && i
< (maskedDBLen
- 1); i
++) ;
111 if (DB
[i
++] != 0x1) {
112 ERR_raise(ERR_LIB_RSA
, RSA_R_SLEN_RECOVERY_FAILED
);
115 if (sLen
!= RSA_PSS_SALTLEN_AUTO
&& (maskedDBLen
- i
) != sLen
) {
116 ERR_raise_data(ERR_LIB_RSA
, RSA_R_SLEN_CHECK_FAILED
,
117 "expected: %d retrieved: %d", sLen
,
121 if (!EVP_DigestInit_ex(ctx
, Hash
, NULL
)
122 || !EVP_DigestUpdate(ctx
, zeroes
, sizeof(zeroes
))
123 || !EVP_DigestUpdate(ctx
, mHash
, hLen
))
125 if (maskedDBLen
- i
) {
126 if (!EVP_DigestUpdate(ctx
, DB
+ i
, maskedDBLen
- i
))
129 if (!EVP_DigestFinal_ex(ctx
, H_
, NULL
))
131 if (memcmp(H_
, H
, hLen
)) {
132 ERR_raise(ERR_LIB_RSA
, RSA_R_BAD_SIGNATURE
);
140 EVP_MD_CTX_free(ctx
);
146 int RSA_padding_add_PKCS1_PSS(RSA
*rsa
, unsigned char *EM
,
147 const unsigned char *mHash
,
148 const EVP_MD
*Hash
, int sLen
)
150 return RSA_padding_add_PKCS1_PSS_mgf1(rsa
, EM
, mHash
, Hash
, NULL
, sLen
);
153 int RSA_padding_add_PKCS1_PSS_mgf1(RSA
*rsa
, unsigned char *EM
,
154 const unsigned char *mHash
,
155 const EVP_MD
*Hash
, const EVP_MD
*mgf1Hash
,
160 int hLen
, maskedDBLen
, MSBits
, emLen
;
161 unsigned char *H
, *salt
= NULL
, *p
;
162 EVP_MD_CTX
*ctx
= NULL
;
164 if (mgf1Hash
== NULL
)
167 hLen
= EVP_MD_size(Hash
);
171 * Negative sLen has special meanings:
173 * -2 salt length is maximized
174 * -3 same as above (on signing)
177 if (sLen
== RSA_PSS_SALTLEN_DIGEST
) {
179 } else if (sLen
== RSA_PSS_SALTLEN_MAX_SIGN
) {
180 sLen
= RSA_PSS_SALTLEN_MAX
;
181 } else if (sLen
< RSA_PSS_SALTLEN_MAX
) {
182 ERR_raise(ERR_LIB_RSA
, RSA_R_SLEN_CHECK_FAILED
);
186 MSBits
= (BN_num_bits(rsa
->n
) - 1) & 0x7;
187 emLen
= RSA_size(rsa
);
192 if (emLen
< hLen
+ 2) {
193 ERR_raise(ERR_LIB_RSA
, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE
);
196 if (sLen
== RSA_PSS_SALTLEN_MAX
) {
197 sLen
= emLen
- hLen
- 2;
198 } else if (sLen
> emLen
- hLen
- 2) {
199 ERR_raise(ERR_LIB_RSA
, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE
);
203 salt
= OPENSSL_malloc(sLen
);
205 ERR_raise(ERR_LIB_RSA
, ERR_R_MALLOC_FAILURE
);
208 if (RAND_bytes_ex(rsa
->libctx
, salt
, sLen
) <= 0)
211 maskedDBLen
= emLen
- hLen
- 1;
212 H
= EM
+ maskedDBLen
;
213 ctx
= EVP_MD_CTX_new();
216 if (!EVP_DigestInit_ex(ctx
, Hash
, NULL
)
217 || !EVP_DigestUpdate(ctx
, zeroes
, sizeof(zeroes
))
218 || !EVP_DigestUpdate(ctx
, mHash
, hLen
))
220 if (sLen
&& !EVP_DigestUpdate(ctx
, salt
, sLen
))
222 if (!EVP_DigestFinal_ex(ctx
, H
, NULL
))
225 /* Generate dbMask in place then perform XOR on it */
226 if (PKCS1_MGF1(EM
, maskedDBLen
, H
, hLen
, mgf1Hash
))
232 * Initial PS XORs with all zeroes which is a NOP so just update pointer.
233 * Note from a test above this value is guaranteed to be non-negative.
235 p
+= emLen
- sLen
- hLen
- 2;
238 for (i
= 0; i
< sLen
; i
++)
242 EM
[0] &= 0xFF >> (8 - MSBits
);
244 /* H is already in place so just set final 0xbc */
246 EM
[emLen
- 1] = 0xbc;
251 EVP_MD_CTX_free(ctx
);
252 OPENSSL_clear_free(salt
, (size_t)sLen
); /* salt != NULL implies sLen > 0 */
259 * The defaults for PSS restrictions are defined in RFC 8017, A.2.3 RSASSA-PSS
260 * (https://tools.ietf.org/html/rfc8017#appendix-A.2.3):
262 * If the default values of the hashAlgorithm, maskGenAlgorithm, and
263 * trailerField fields of RSASSA-PSS-params are used, then the algorithm
264 * identifier will have the following value:
266 * rSASSA-PSS-Default-Identifier RSASSA-AlgorithmIdentifier ::= {
267 * algorithm id-RSASSA-PSS,
268 * parameters RSASSA-PSS-params : {
269 * hashAlgorithm sha1,
270 * maskGenAlgorithm mgf1SHA1,
272 * trailerField trailerFieldBC
276 * RSASSA-AlgorithmIdentifier ::= AlgorithmIdentifier {
280 static const RSA_PSS_PARAMS_30 default_RSASSA_PSS_params
= {
281 NID_sha1
, /* default hashAlgorithm */
283 NID_mgf1
, /* default maskGenAlgorithm */
284 NID_sha1
/* default MGF1 hash */
286 20, /* default saltLength */
287 1 /* default trailerField (0xBC) */
290 int ossl_rsa_pss_params_30_set_defaults(RSA_PSS_PARAMS_30
*rsa_pss_params
)
292 if (rsa_pss_params
== NULL
)
294 *rsa_pss_params
= default_RSASSA_PSS_params
;
298 int ossl_rsa_pss_params_30_is_unrestricted(const RSA_PSS_PARAMS_30
*rsa_pss_params
)
300 static RSA_PSS_PARAMS_30 pss_params_cmp
= { 0, };
302 return rsa_pss_params
== NULL
303 || memcmp(rsa_pss_params
, &pss_params_cmp
,
304 sizeof(*rsa_pss_params
)) == 0;
307 int ossl_rsa_pss_params_30_copy(RSA_PSS_PARAMS_30
*to
,
308 const RSA_PSS_PARAMS_30
*from
)
310 memcpy(to
, from
, sizeof(*to
));
314 int ossl_rsa_pss_params_30_set_hashalg(RSA_PSS_PARAMS_30
*rsa_pss_params
,
317 if (rsa_pss_params
== NULL
)
319 rsa_pss_params
->hash_algorithm_nid
= hashalg_nid
;
323 int ossl_rsa_pss_params_30_set_maskgenalg(RSA_PSS_PARAMS_30
*rsa_pss_params
,
326 if (rsa_pss_params
== NULL
)
328 rsa_pss_params
->mask_gen
.algorithm_nid
= maskgenalg_nid
;
332 int ossl_rsa_pss_params_30_set_maskgenhashalg(RSA_PSS_PARAMS_30
*rsa_pss_params
,
333 int maskgenhashalg_nid
)
335 if (rsa_pss_params
== NULL
)
337 rsa_pss_params
->mask_gen
.hash_algorithm_nid
= maskgenhashalg_nid
;
341 int ossl_rsa_pss_params_30_set_saltlen(RSA_PSS_PARAMS_30
*rsa_pss_params
,
344 if (rsa_pss_params
== NULL
)
346 rsa_pss_params
->salt_len
= saltlen
;
350 int ossl_rsa_pss_params_30_set_trailerfield(RSA_PSS_PARAMS_30
*rsa_pss_params
,
353 if (rsa_pss_params
== NULL
)
355 rsa_pss_params
->trailer_field
= trailerfield
;
359 int ossl_rsa_pss_params_30_hashalg(const RSA_PSS_PARAMS_30
*rsa_pss_params
)
361 if (rsa_pss_params
== NULL
)
362 return default_RSASSA_PSS_params
.hash_algorithm_nid
;
363 return rsa_pss_params
->hash_algorithm_nid
;
366 int ossl_rsa_pss_params_30_maskgenalg(const RSA_PSS_PARAMS_30
*rsa_pss_params
)
368 if (rsa_pss_params
== NULL
)
369 return default_RSASSA_PSS_params
.mask_gen
.algorithm_nid
;
370 return rsa_pss_params
->mask_gen
.algorithm_nid
;
373 int ossl_rsa_pss_params_30_maskgenhashalg(const RSA_PSS_PARAMS_30
*rsa_pss_params
)
375 if (rsa_pss_params
== NULL
)
376 return default_RSASSA_PSS_params
.hash_algorithm_nid
;
377 return rsa_pss_params
->mask_gen
.hash_algorithm_nid
;
380 int ossl_rsa_pss_params_30_saltlen(const RSA_PSS_PARAMS_30
*rsa_pss_params
)
382 if (rsa_pss_params
== NULL
)
383 return default_RSASSA_PSS_params
.salt_len
;
384 return rsa_pss_params
->salt_len
;
387 int ossl_rsa_pss_params_30_trailerfield(const RSA_PSS_PARAMS_30
*rsa_pss_params
)
389 if (rsa_pss_params
== NULL
)
390 return default_RSASSA_PSS_params
.trailer_field
;
391 return rsa_pss_params
->trailer_field
;
394 #if defined(_MSC_VER)
395 # pragma optimize("",on)