Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/rsa/padding.c

raw
1 // Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project 2005.
2 // Copyright (c) 2005 The OpenSSL Project. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
4
5 #include <openssl/rsa.h>
6
7 #include <assert.h>
8 #include <limits.h>
9 #include <string.h>
10
11 #include <openssl/bn.h>
12 #include <openssl/digest.h>
13 #include <openssl/err.h>
14 #include <openssl/mem.h>
15 #include <openssl/rand.h>
16 #include <openssl/sha.h>
17
18 #include "internal.h"
19 #include "../../internal.h"
20
21
22 int RSA_padding_add_PKCS1_type_1(uint8_t *to, size_t to_len,
23 const uint8_t *from, size_t from_len) {
24 // See RFC 8017, section 9.2.
25 if (to_len < RSA_PKCS1_PADDING_SIZE) {
26 OPENSSL_PUT_ERROR(RSA, RSA_R_KEY_SIZE_TOO_SMALL);
27 return 0;
28 }
29
30 if (from_len > to_len - RSA_PKCS1_PADDING_SIZE) {
31 OPENSSL_PUT_ERROR(RSA, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
32 return 0;
33 }
34
35 to[0] = 0;
36 to[1] = 1;
37 OPENSSL_memset(to + 2, 0xff, to_len - 3 - from_len);
38 to[to_len - from_len - 1] = 0;
39 OPENSSL_memcpy(to + to_len - from_len, from, from_len);
40 return 1;
41 }
42
43 int RSA_padding_check_PKCS1_type_1(uint8_t *out, size_t *out_len,
44 size_t max_out, const uint8_t *from,
45 size_t from_len) {
46 // See RFC 8017, section 9.2. This is part of signature verification and thus
47 // does not need to run in constant-time.
48 if (from_len < 2) {
49 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_SMALL);
50 return 0;
51 }
52
53 // Check the header.
54 if (from[0] != 0 || from[1] != 1) {
55 OPENSSL_PUT_ERROR(RSA, RSA_R_BLOCK_TYPE_IS_NOT_01);
56 return 0;
57 }
58
59 // Scan over padded data, looking for the 00.
60 size_t pad;
61 for (pad = 2 /* header */; pad < from_len; pad++) {
62 if (from[pad] == 0x00) {
63 break;
64 }
65
66 if (from[pad] != 0xff) {
67 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_FIXED_HEADER_DECRYPT);
68 return 0;
69 }
70 }
71
72 if (pad == from_len) {
73 OPENSSL_PUT_ERROR(RSA, RSA_R_NULL_BEFORE_BLOCK_MISSING);
74 return 0;
75 }
76
77 if (pad < 2 /* header */ + 8) {
78 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_PAD_BYTE_COUNT);
79 return 0;
80 }
81
82 // Skip over the 00.
83 pad++;
84
85 if (from_len - pad > max_out) {
86 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE);
87 return 0;
88 }
89
90 OPENSSL_memcpy(out, from + pad, from_len - pad);
91 *out_len = from_len - pad;
92 return 1;
93 }
94
95 int RSA_padding_add_none(uint8_t *to, size_t to_len, const uint8_t *from,
96 size_t from_len) {
97 if (from_len > to_len) {
98 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
99 return 0;
100 }
101
102 if (from_len < to_len) {
103 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_SMALL);
104 return 0;
105 }
106
107 OPENSSL_memcpy(to, from, from_len);
108 return 1;
109 }
110
111 int PKCS1_MGF1(uint8_t *out, size_t len, const uint8_t *seed, size_t seed_len,
112 const EVP_MD *md) {
113 // We have to avoid the underlying SHA services updating the indicator
114 // state, so we lock the state here.
115 FIPS_service_indicator_lock_state();
116
117 int ret = 0;
118 EVP_MD_CTX ctx;
119 EVP_MD_CTX_init(&ctx);
120
121 size_t md_len = EVP_MD_size(md);
122
123 for (uint32_t i = 0; len > 0; i++) {
124 uint8_t counter[4];
125 counter[0] = (uint8_t)(i >> 24);
126 counter[1] = (uint8_t)(i >> 16);
127 counter[2] = (uint8_t)(i >> 8);
128 counter[3] = (uint8_t)i;
129 if (!EVP_DigestInit_ex(&ctx, md, NULL) ||
130 !EVP_DigestUpdate(&ctx, seed, seed_len) ||
131 !EVP_DigestUpdate(&ctx, counter, sizeof(counter))) {
132 goto err;
133 }
134
135 if (md_len <= len) {
136 if (!EVP_DigestFinal_ex(&ctx, out, NULL)) {
137 goto err;
138 }
139 out += md_len;
140 len -= md_len;
141 } else {
142 uint8_t digest[EVP_MAX_MD_SIZE];
143 if (!EVP_DigestFinal_ex(&ctx, digest, NULL)) {
144 goto err;
145 }
146 OPENSSL_memcpy(out, digest, len);
147 len = 0;
148 }
149 }
150
151 ret = 1;
152
153 err:
154 EVP_MD_CTX_cleanup(&ctx);
155 FIPS_service_indicator_unlock_state();
156 return ret;
157 }
158
159 static const uint8_t kPSSZeroes[] = {0, 0, 0, 0, 0, 0, 0, 0};
160
161 int RSA_verify_PKCS1_PSS_mgf1(const RSA *rsa, const uint8_t *mHash,
162 const EVP_MD *Hash, const EVP_MD *mgf1Hash,
163 const uint8_t *EM, int sLen) {
164 // We have to avoid the underlying SHA services updating the indicator
165 // state, so we lock the state here.
166 FIPS_service_indicator_lock_state();
167 if (mgf1Hash == NULL) {
168 mgf1Hash = Hash;
169 }
170
171 int ret = 0;
172 uint8_t *DB = NULL;
173 EVP_MD_CTX ctx;
174 EVP_MD_CTX_init(&ctx);
175
176 // Negative sLen has special meanings:
177 // RSA_PSS_SALTLEN_DIGEST sLen == hLen
178 // -2 salt length is autorecovered from signature
179 // -N reserved
180 size_t hLen = EVP_MD_size(Hash);
181 if (sLen == RSA_PSS_SALTLEN_DIGEST) {
182 sLen = (int)hLen;
183 } else if (sLen == -2) {
184 sLen = -2;
185 } else if (sLen < -2) {
186 OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_CHECK_FAILED);
187 goto err;
188 }
189
190 unsigned MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
191 size_t emLen = RSA_size(rsa);
192 if (EM[0] & (0xFF << MSBits)) {
193 OPENSSL_PUT_ERROR(RSA, RSA_R_FIRST_OCTET_INVALID);
194 goto err;
195 }
196 if (MSBits == 0) {
197 EM++;
198 emLen--;
199 }
200 // |sLen| may be -2 for the non-standard salt length recovery mode.
201 if (emLen < hLen + 2 ||
202 (sLen >= 0 && emLen < hLen + (size_t)sLen + 2)) {
203 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE);
204 goto err;
205 }
206 if (EM[emLen - 1] != 0xbc) {
207 OPENSSL_PUT_ERROR(RSA, RSA_R_LAST_OCTET_INVALID);
208 goto err;
209 }
210 size_t maskedDBLen = emLen - hLen - 1;
211 const uint8_t *H = EM + maskedDBLen;
212 DB = OPENSSL_malloc(maskedDBLen);
213 if (!DB) {
214 goto err;
215 }
216 OPENSSL_BEGIN_ALLOW_DEPRECATED
217 if (!PKCS1_MGF1(DB, maskedDBLen, H, hLen, mgf1Hash)) {
218 goto err;
219 }
220 OPENSSL_END_ALLOW_DEPRECATED
221 for (size_t i = 0; i < maskedDBLen; i++) {
222 DB[i] ^= EM[i];
223 }
224 if (MSBits) {
225 DB[0] &= 0xFF >> (8 - MSBits);
226 }
227 // This step differs slightly from EMSA-PSS-VERIFY (RFC 8017) step 10 because
228 // it accepts a non-standard salt recovery flow. DB should be some number of
229 // zeros, a one, then the salt.
230 size_t salt_start;
231 for (salt_start = 0; DB[salt_start] == 0 && salt_start < maskedDBLen - 1;
232 salt_start++) {
233 ;
234 }
235 if (DB[salt_start] != 0x1) {
236 OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_RECOVERY_FAILED);
237 goto err;
238 }
239 salt_start++;
240 // If a salt length was specified, check it matches.
241 if (sLen >= 0 && maskedDBLen - salt_start != (size_t)sLen) {
242 OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_CHECK_FAILED);
243 goto err;
244 }
245 uint8_t H_[EVP_MAX_MD_SIZE];
246 if (!EVP_DigestInit_ex(&ctx, Hash, NULL) ||
247 !EVP_DigestUpdate(&ctx, kPSSZeroes, sizeof(kPSSZeroes)) ||
248 !EVP_DigestUpdate(&ctx, mHash, hLen) ||
249 !EVP_DigestUpdate(&ctx, DB + salt_start, maskedDBLen - salt_start) ||
250 !EVP_DigestFinal_ex(&ctx, H_, NULL)) {
251 goto err;
252 }
253 if (OPENSSL_memcmp(H_, H, hLen) != 0) {
254 OPENSSL_PUT_ERROR(RSA, RSA_R_BAD_SIGNATURE);
255 goto err;
256 }
257
258 ret = 1;
259
260 err:
261 OPENSSL_free(DB);
262 EVP_MD_CTX_cleanup(&ctx);
263 FIPS_service_indicator_unlock_state();
264 return ret;
265 }
266
267 int RSA_padding_add_PKCS1_PSS_mgf1(const RSA *rsa, unsigned char *EM,
268 const unsigned char *mHash,
269 const EVP_MD *Hash, const EVP_MD *mgf1Hash,
270 int sLenRequested) {
271 // We have to avoid the underlying SHA services updating the indicator
272 // state, so we lock the state here.
273 FIPS_service_indicator_lock_state();
274 int ret = 0;
275 size_t maskedDBLen, MSBits, emLen;
276 size_t hLen;
277 unsigned char *H, *salt = NULL, *p;
278
279 if (mgf1Hash == NULL) {
280 mgf1Hash = Hash;
281 }
282
283 hLen = EVP_MD_size(Hash);
284
285 if (BN_is_zero(rsa->n)) {
286 OPENSSL_PUT_ERROR(RSA, RSA_R_EMPTY_PUBLIC_KEY);
287 goto err;
288 }
289
290 MSBits = (BN_num_bits(rsa->n) - 1) & 0x7;
291 emLen = RSA_size(rsa);
292 if (MSBits == 0) {
293 assert(emLen >= 1);
294 *EM++ = 0;
295 emLen--;
296 }
297
298 if (emLen < hLen + 2) {
299 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
300 goto err;
301 }
302
303 // Negative sLenRequested has special meanings:
304 // RSA_PSS_SALTLEN_DIGEST sLen == hLen
305 // -2 salt length is maximized
306 // -N reserved
307 size_t sLen;
308 if (sLenRequested == RSA_PSS_SALTLEN_DIGEST) {
309 sLen = hLen;
310 } else if (sLenRequested == -2) {
311 sLen = emLen - hLen - 2;
312 } else if (sLenRequested < 0) {
313 OPENSSL_PUT_ERROR(RSA, RSA_R_SLEN_CHECK_FAILED);
314 goto err;
315 } else {
316 sLen = (size_t)sLenRequested;
317 }
318
319 if (emLen - hLen - 2 < sLen) {
320 OPENSSL_PUT_ERROR(RSA, RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
321 goto err;
322 }
323
324 if (sLen > 0) {
325 salt = OPENSSL_malloc(sLen);
326 if (!salt) {
327 goto err;
328 }
329 AWSLC_ABORT_IF_NOT_ONE(RAND_bytes(salt, sLen));
330 }
331 maskedDBLen = emLen - hLen - 1;
332 H = EM + maskedDBLen;
333
334 EVP_MD_CTX ctx;
335 EVP_MD_CTX_init(&ctx);
336 int digest_ok = EVP_DigestInit_ex(&ctx, Hash, NULL) &&
337 EVP_DigestUpdate(&ctx, kPSSZeroes, sizeof(kPSSZeroes)) &&
338 EVP_DigestUpdate(&ctx, mHash, hLen) &&
339 EVP_DigestUpdate(&ctx, salt, sLen) &&
340 EVP_DigestFinal_ex(&ctx, H, NULL);
341 EVP_MD_CTX_cleanup(&ctx);
342 if (!digest_ok) {
343 goto err;
344 }
345 OPENSSL_BEGIN_ALLOW_DEPRECATED
346 // Generate dbMask in place then perform XOR on it
347 if (!PKCS1_MGF1(EM, maskedDBLen, H, hLen, mgf1Hash)) {
348 goto err;
349 }
350 OPENSSL_END_ALLOW_DEPRECATED
351 p = EM;
352
353 // Initial PS XORs with all zeroes which is a NOP so just update
354 // pointer. Note from a test above this value is guaranteed to
355 // be non-negative.
356 p += emLen - sLen - hLen - 2;
357 *p++ ^= 0x1;
358 if (sLen > 0) {
359 for (size_t i = 0; i < sLen; i++) {
360 *p++ ^= salt[i];
361 }
362 }
363 if (MSBits) {
364 EM[0] &= 0xFF >> (8 - MSBits);
365 }
366
367 // H is already in place so just set final 0xbc
368
369 EM[emLen - 1] = 0xbc;
370
371 ret = 1;
372
373 err:
374 OPENSSL_free(salt);
375 FIPS_service_indicator_unlock_state();
376
377 return ret;
378 }