]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/rsa/rsa_pss.c
Convert all {NAME}err() in crypto/ to their corresponding ERR_raise() call
[thirdparty/openssl.git] / crypto / rsa / rsa_pss.c
CommitLineData
0f113f3e 1/*
33388b44 2 * Copyright 2005-2020 The OpenSSL Project Authors. All Rights Reserved.
429168e7 3 *
2a7b6f39 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
2039c421
RS
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
429168e7
DSH
8 */
9
c5f87134
P
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
429168e7 16#include <stdio.h>
b39fc560 17#include "internal/cryptlib.h"
429168e7
DSH
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>
706457b7 23#include "rsa_local.h"
429168e7 24
0f113f3e 25static const unsigned char zeroes[] = { 0, 0, 0, 0, 0, 0, 0, 0 };
429168e7 26
0491e058 27#if defined(_MSC_VER) && defined(_ARM_)
0f113f3e 28# pragma optimize("g", off)
0491e058
AP
29#endif
30
429168e7 31int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash,
0f113f3e
MC
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}
e8254d40
DSH
37
38int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const unsigned char *mHash,
0f113f3e
MC
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;
bfb0641f 47 EVP_MD_CTX *ctx = EVP_MD_CTX_new();
0f113f3e 48 unsigned char H_[EVP_MAX_MD_SIZE];
6e59a892 49
6e59a892
RL
50 if (ctx == NULL)
51 goto err;
d51204f1 52
0f113f3e
MC
53 if (mgf1Hash == NULL)
54 mgf1Hash = Hash;
e8254d40 55
6e59a892 56 hLen = EVP_MD_size(Hash);
0f113f3e
MC
57 if (hLen < 0)
58 goto err;
50e735f9
MC
59 /*-
60 * Negative sLen has special meanings:
61 * -1 sLen == hLen
62 * -2 salt length is autorecovered from signature
108909d3 63 * -3 salt length is maximized
50e735f9
MC
64 * -N reserved
65 */
90862ab4 66 if (sLen == RSA_PSS_SALTLEN_DIGEST) {
0f113f3e 67 sLen = hLen;
90862ab4 68 } else if (sLen < RSA_PSS_SALTLEN_MAX) {
9311d0c4 69 ERR_raise(ERR_LIB_RSA, RSA_R_SLEN_CHECK_FAILED);
0f113f3e
MC
70 goto err;
71 }
d51204f1 72
0f113f3e
MC
73 MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
74 emLen = RSA_size(rsa);
75 if (EM[0] & (0xFF << MSBits)) {
9311d0c4 76 ERR_raise(ERR_LIB_RSA, RSA_R_FIRST_OCTET_INVALID);
0f113f3e
MC
77 goto err;
78 }
79 if (MSBits == 0) {
80 EM++;
81 emLen--;
82 }
108909d3 83 if (emLen < hLen + 2) {
9311d0c4 84 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE);
108909d3
BE
85 goto err;
86 }
137096a7
DSH
87 if (sLen == RSA_PSS_SALTLEN_MAX) {
88 sLen = emLen - hLen - 2;
108909d3 89 } else if (sLen > emLen - hLen - 2) { /* sLen can be small negative */
9311d0c4 90 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE);
0f113f3e
MC
91 goto err;
92 }
93 if (EM[emLen - 1] != 0xbc) {
9311d0c4 94 ERR_raise(ERR_LIB_RSA, RSA_R_LAST_OCTET_INVALID);
0f113f3e
MC
95 goto err;
96 }
97 maskedDBLen = emLen - hLen - 1;
98 H = EM + maskedDBLen;
99 DB = OPENSSL_malloc(maskedDBLen);
90945fa3 100 if (DB == NULL) {
9311d0c4 101 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
102 goto err;
103 }
104 if (PKCS1_MGF1(DB, maskedDBLen, H, hLen, mgf1Hash) < 0)
105 goto err;
106 for (i = 0; i < maskedDBLen; i++)
107 DB[i] ^= EM[i];
108 if (MSBits)
109 DB[0] &= 0xFF >> (8 - MSBits);
110 for (i = 0; DB[i] == 0 && i < (maskedDBLen - 1); i++) ;
111 if (DB[i++] != 0x1) {
9311d0c4 112 ERR_raise(ERR_LIB_RSA, RSA_R_SLEN_RECOVERY_FAILED);
0f113f3e
MC
113 goto err;
114 }
137096a7 115 if (sLen != RSA_PSS_SALTLEN_AUTO && (maskedDBLen - i) != sLen) {
9311d0c4 116 ERR_raise(ERR_LIB_RSA, RSA_R_SLEN_CHECK_FAILED);
0f113f3e
MC
117 goto err;
118 }
6e59a892 119 if (!EVP_DigestInit_ex(ctx, Hash, NULL)
cbe29648 120 || !EVP_DigestUpdate(ctx, zeroes, sizeof(zeroes))
6e59a892 121 || !EVP_DigestUpdate(ctx, mHash, hLen))
0f113f3e
MC
122 goto err;
123 if (maskedDBLen - i) {
6e59a892 124 if (!EVP_DigestUpdate(ctx, DB + i, maskedDBLen - i))
0f113f3e
MC
125 goto err;
126 }
6e59a892 127 if (!EVP_DigestFinal_ex(ctx, H_, NULL))
0f113f3e
MC
128 goto err;
129 if (memcmp(H_, H, hLen)) {
9311d0c4 130 ERR_raise(ERR_LIB_RSA, RSA_R_BAD_SIGNATURE);
0f113f3e 131 ret = 0;
90862ab4 132 } else {
0f113f3e 133 ret = 1;
90862ab4 134 }
429168e7 135
0f113f3e 136 err:
b548a1f1 137 OPENSSL_free(DB);
bfb0641f 138 EVP_MD_CTX_free(ctx);
429168e7 139
0f113f3e 140 return ret;
429168e7 141
0f113f3e 142}
429168e7
DSH
143
144int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM,
0f113f3e
MC
145 const unsigned char *mHash,
146 const EVP_MD *Hash, int sLen)
147{
148 return RSA_padding_add_PKCS1_PSS_mgf1(rsa, EM, mHash, Hash, NULL, sLen);
149}
e8254d40
DSH
150
151int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM,
0f113f3e
MC
152 const unsigned char *mHash,
153 const EVP_MD *Hash, const EVP_MD *mgf1Hash,
154 int sLen)
155{
156 int i;
157 int ret = 0;
158 int hLen, maskedDBLen, MSBits, emLen;
159 unsigned char *H, *salt = NULL, *p;
6e59a892 160 EVP_MD_CTX *ctx = NULL;
d51204f1 161
0f113f3e
MC
162 if (mgf1Hash == NULL)
163 mgf1Hash = Hash;
e8254d40 164
6e59a892 165 hLen = EVP_MD_size(Hash);
0f113f3e
MC
166 if (hLen < 0)
167 goto err;
50e735f9
MC
168 /*-
169 * Negative sLen has special meanings:
170 * -1 sLen == hLen
171 * -2 salt length is maximized
108909d3 172 * -3 same as above (on signing)
50e735f9
MC
173 * -N reserved
174 */
90862ab4 175 if (sLen == RSA_PSS_SALTLEN_DIGEST) {
0f113f3e 176 sLen = hLen;
90862ab4 177 } else if (sLen == RSA_PSS_SALTLEN_MAX_SIGN) {
137096a7 178 sLen = RSA_PSS_SALTLEN_MAX;
90862ab4 179 } else if (sLen < RSA_PSS_SALTLEN_MAX) {
9311d0c4 180 ERR_raise(ERR_LIB_RSA, RSA_R_SLEN_CHECK_FAILED);
0f113f3e
MC
181 goto err;
182 }
d51204f1 183
0f113f3e
MC
184 MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
185 emLen = RSA_size(rsa);
186 if (MSBits == 0) {
187 *EM++ = 0;
188 emLen--;
189 }
108909d3 190 if (emLen < hLen + 2) {
9311d0c4 191 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
108909d3
BE
192 goto err;
193 }
137096a7 194 if (sLen == RSA_PSS_SALTLEN_MAX) {
0f113f3e 195 sLen = emLen - hLen - 2;
108909d3 196 } else if (sLen > emLen - hLen - 2) {
9311d0c4 197 ERR_raise(ERR_LIB_RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
0f113f3e
MC
198 goto err;
199 }
200 if (sLen > 0) {
201 salt = OPENSSL_malloc(sLen);
90945fa3 202 if (salt == NULL) {
9311d0c4 203 ERR_raise(ERR_LIB_RSA, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
204 goto err;
205 }
0f2deef5 206 if (RAND_bytes_ex(rsa->libctx, salt, sLen) <= 0)
0f113f3e
MC
207 goto err;
208 }
209 maskedDBLen = emLen - hLen - 1;
210 H = EM + maskedDBLen;
bfb0641f 211 ctx = EVP_MD_CTX_new();
6e59a892
RL
212 if (ctx == NULL)
213 goto err;
214 if (!EVP_DigestInit_ex(ctx, Hash, NULL)
cbe29648 215 || !EVP_DigestUpdate(ctx, zeroes, sizeof(zeroes))
6e59a892 216 || !EVP_DigestUpdate(ctx, mHash, hLen))
0f113f3e 217 goto err;
6e59a892 218 if (sLen && !EVP_DigestUpdate(ctx, salt, sLen))
0f113f3e 219 goto err;
6e59a892 220 if (!EVP_DigestFinal_ex(ctx, H, NULL))
0f113f3e 221 goto err;
429168e7 222
0f113f3e
MC
223 /* Generate dbMask in place then perform XOR on it */
224 if (PKCS1_MGF1(EM, maskedDBLen, H, hLen, mgf1Hash))
225 goto err;
429168e7 226
0f113f3e 227 p = EM;
429168e7 228
0f113f3e
MC
229 /*
230 * Initial PS XORs with all zeroes which is a NOP so just update pointer.
231 * Note from a test above this value is guaranteed to be non-negative.
232 */
233 p += emLen - sLen - hLen - 2;
234 *p++ ^= 0x1;
235 if (sLen > 0) {
236 for (i = 0; i < sLen; i++)
237 *p++ ^= salt[i];
238 }
239 if (MSBits)
240 EM[0] &= 0xFF >> (8 - MSBits);
429168e7 241
0f113f3e 242 /* H is already in place so just set final 0xbc */
429168e7 243
0f113f3e 244 EM[emLen - 1] = 0xbc;
429168e7 245
0f113f3e 246 ret = 1;
429168e7 247
0f113f3e 248 err:
bfb0641f 249 EVP_MD_CTX_free(ctx);
427e91d9 250 OPENSSL_clear_free(salt, (size_t)sLen); /* salt != NULL implies sLen > 0 */
429168e7 251
0f113f3e 252 return ret;
429168e7 253
0f113f3e 254}
0491e058 255
15671090
RL
256/*
257 * The defaults for PSS restrictions are defined in RFC 8017, A.2.3 RSASSA-PSS
258 * (https://tools.ietf.org/html/rfc8017#appendix-A.2.3):
259 *
260 * If the default values of the hashAlgorithm, maskGenAlgorithm, and
261 * trailerField fields of RSASSA-PSS-params are used, then the algorithm
262 * identifier will have the following value:
263 *
264 * rSASSA-PSS-Default-Identifier RSASSA-AlgorithmIdentifier ::= {
265 * algorithm id-RSASSA-PSS,
266 * parameters RSASSA-PSS-params : {
267 * hashAlgorithm sha1,
268 * maskGenAlgorithm mgf1SHA1,
269 * saltLength 20,
270 * trailerField trailerFieldBC
271 * }
272 * }
273 *
274 * RSASSA-AlgorithmIdentifier ::= AlgorithmIdentifier {
275 * {PKCS1Algorithms}
276 * }
277 */
278static const RSA_PSS_PARAMS_30 default_RSASSA_PSS_params = {
279 NID_sha1, /* default hashAlgorithm */
280 {
281 NID_mgf1, /* default maskGenAlgorithm */
282 NID_sha1 /* default MGF1 hash */
283 },
284 20, /* default saltLength */
285 1 /* default trailerField (0xBC) */
286};
287
23b2fc0b 288int ossl_rsa_pss_params_30_set_defaults(RSA_PSS_PARAMS_30 *rsa_pss_params)
15671090
RL
289{
290 if (rsa_pss_params == NULL)
291 return 0;
292 *rsa_pss_params = default_RSASSA_PSS_params;
293 return 1;
294}
295
23b2fc0b 296int ossl_rsa_pss_params_30_is_unrestricted(const RSA_PSS_PARAMS_30 *rsa_pss_params)
15671090
RL
297{
298 static RSA_PSS_PARAMS_30 pss_params_cmp = { 0, };
299
300 return rsa_pss_params == NULL
301 || memcmp(rsa_pss_params, &pss_params_cmp,
302 sizeof(*rsa_pss_params)) == 0;
303}
304
23b2fc0b
P
305int ossl_rsa_pss_params_30_copy(RSA_PSS_PARAMS_30 *to,
306 const RSA_PSS_PARAMS_30 *from)
15671090
RL
307{
308 memcpy(to, from, sizeof(*to));
309 return 1;
310}
311
23b2fc0b
P
312int ossl_rsa_pss_params_30_set_hashalg(RSA_PSS_PARAMS_30 *rsa_pss_params,
313 int hashalg_nid)
15671090
RL
314{
315 if (rsa_pss_params == NULL)
316 return 0;
317 rsa_pss_params->hash_algorithm_nid = hashalg_nid;
318 return 1;
319}
320
23b2fc0b
P
321int ossl_rsa_pss_params_30_set_maskgenalg(RSA_PSS_PARAMS_30 *rsa_pss_params,
322 int maskgenalg_nid)
15671090
RL
323{
324 if (rsa_pss_params == NULL)
325 return 0;
326 rsa_pss_params->mask_gen.algorithm_nid = maskgenalg_nid;
327 return 1;
328}
329
23b2fc0b
P
330int ossl_rsa_pss_params_30_set_maskgenhashalg(RSA_PSS_PARAMS_30 *rsa_pss_params,
331 int maskgenhashalg_nid)
15671090
RL
332{
333 if (rsa_pss_params == NULL)
334 return 0;
335 rsa_pss_params->mask_gen.hash_algorithm_nid = maskgenhashalg_nid;
336 return 1;
337}
338
23b2fc0b
P
339int ossl_rsa_pss_params_30_set_saltlen(RSA_PSS_PARAMS_30 *rsa_pss_params,
340 int saltlen)
15671090
RL
341{
342 if (rsa_pss_params == NULL)
343 return 0;
344 rsa_pss_params->salt_len = saltlen;
345 return 1;
346}
347
23b2fc0b
P
348int ossl_rsa_pss_params_30_set_trailerfield(RSA_PSS_PARAMS_30 *rsa_pss_params,
349 int trailerfield)
15671090
RL
350{
351 if (rsa_pss_params == NULL)
352 return 0;
353 rsa_pss_params->trailer_field = trailerfield;
354 return 1;
355}
356
23b2fc0b 357int ossl_rsa_pss_params_30_hashalg(const RSA_PSS_PARAMS_30 *rsa_pss_params)
15671090
RL
358{
359 if (rsa_pss_params == NULL)
360 return default_RSASSA_PSS_params.hash_algorithm_nid;
361 return rsa_pss_params->hash_algorithm_nid;
362}
363
23b2fc0b 364int ossl_rsa_pss_params_30_maskgenalg(const RSA_PSS_PARAMS_30 *rsa_pss_params)
15671090
RL
365{
366 if (rsa_pss_params == NULL)
367 return default_RSASSA_PSS_params.mask_gen.algorithm_nid;
368 return rsa_pss_params->mask_gen.algorithm_nid;
369}
370
23b2fc0b 371int ossl_rsa_pss_params_30_maskgenhashalg(const RSA_PSS_PARAMS_30 *rsa_pss_params)
15671090
RL
372{
373 if (rsa_pss_params == NULL)
374 return default_RSASSA_PSS_params.hash_algorithm_nid;
375 return rsa_pss_params->mask_gen.hash_algorithm_nid;
376}
377
23b2fc0b 378int ossl_rsa_pss_params_30_saltlen(const RSA_PSS_PARAMS_30 *rsa_pss_params)
15671090
RL
379{
380 if (rsa_pss_params == NULL)
381 return default_RSASSA_PSS_params.salt_len;
382 return rsa_pss_params->salt_len;
383}
384
23b2fc0b 385int ossl_rsa_pss_params_30_trailerfield(const RSA_PSS_PARAMS_30 *rsa_pss_params)
15671090
RL
386{
387 if (rsa_pss_params == NULL)
388 return default_RSASSA_PSS_params.trailer_field;
389 return rsa_pss_params->trailer_field;
390}
391
0491e058 392#if defined(_MSC_VER)
0f113f3e 393# pragma optimize("",on)
0491e058 394#endif