Debian Codegraph @

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

raw
1 // Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2 // SPDX-License-Identifier: Apache-2.0 OR ISC
3 #include <openssl/evp.h>
4 #include <openssl/mem.h>
5 #include <openssl/base.h>
6
7 #include "../delocate.h"
8 #include "../../evp_extra/internal.h"
9 #include "../ml_dsa/ml_dsa.h"
10 #include "internal.h"
11
12 // ML-DSA OIDs as defined within:
13 // https://csrc.nist.gov/projects/computer-security-objects-register/algorithm-registration
14 //2.16.840.1.101.3.4.3.17
15 static const uint8_t kOIDMLDSA44[] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x11};
16 //2.16.840.1.101.3.4.3.18
17 static const uint8_t kOIDMLDSA65[] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x12};
18 //2.16.840.1.101.3.4.3.19
19 static const uint8_t kOIDMLDSA87[] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x03, 0x13};
20
21 // PQDSA functions: these are init/new/clear/free/get_sig functions for PQDSA_KEY
22 // These are analagous to the ec_key functions in crypto/fipsmodule/ec/ec_key.c
23
24 PQDSA_KEY *PQDSA_KEY_new(void) {
25 PQDSA_KEY *ret = OPENSSL_zalloc(sizeof(PQDSA_KEY));
26 if (ret == NULL) {
27 return NULL;
28 }
29
30 return ret;
31 }
32
33 static void PQDSA_KEY_clear(PQDSA_KEY *key) {
34 key->pqdsa = NULL;
35 OPENSSL_free(key->public_key);
36 OPENSSL_free(key->private_key);
37 OPENSSL_free(key->seed);
38 key->public_key = NULL;
39 key->private_key = NULL;
40 key->seed = NULL;
41 }
42
43 int PQDSA_KEY_init(PQDSA_KEY *key, const PQDSA *pqdsa) {
44 if (key == NULL || pqdsa == NULL) {
45 return 0;
46 }
47 // If the key is already initialized clear it.
48 PQDSA_KEY_clear(key);
49
50 key->pqdsa = pqdsa;
51 key->public_key = OPENSSL_zalloc(pqdsa->public_key_len);
52 key->private_key = OPENSSL_zalloc(pqdsa->private_key_len);
53 key->seed = OPENSSL_zalloc(pqdsa->keygen_seed_len);
54 if (key->public_key == NULL || key->private_key == NULL || key->seed == NULL) {
55 PQDSA_KEY_clear(key);
56 return 0;
57 }
58 return 1;
59 }
60
61 void PQDSA_KEY_free(PQDSA_KEY *key) {
62 if (key == NULL) {
63 return;
64 }
65 PQDSA_KEY_clear(key);
66 OPENSSL_free(key);
67 }
68
69 const PQDSA *PQDSA_KEY_get0_dsa(PQDSA_KEY* key) {
70 if (key == NULL) {
71 return NULL;
72 }
73 return key->pqdsa;
74 }
75
76 int PQDSA_KEY_set_raw_public_key(PQDSA_KEY *key, CBS *in) {
77 // Check if the parsed length corresponds with the expected length.
78 if (CBS_len(in) != key->pqdsa->public_key_len) {
79 OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE);
80 return 0;
81 }
82
83 OPENSSL_free(key->public_key);
84 key->public_key = OPENSSL_memdup(CBS_data(in), key->pqdsa->public_key_len);
85 if (key->public_key == NULL) {
86 return 0;
87 }
88
89 return 1;
90 }
91
92 int PQDSA_KEY_set_raw_keypair_from_seed(PQDSA_KEY *key, CBS *in) {
93 // Check if the parsed length corresponds with the expected length.
94 if (CBS_len(in) != key->pqdsa->keygen_seed_len) {
95 OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE);
96 return 0;
97 }
98
99 int ret = 0;
100 uint8_t *public_key = OPENSSL_malloc(key->pqdsa->public_key_len);
101 uint8_t *private_key = OPENSSL_malloc(key->pqdsa->private_key_len);
102 uint8_t *seed = OPENSSL_malloc(key->pqdsa->keygen_seed_len);
103 if (public_key == NULL || private_key == NULL || seed == NULL) {
104 goto err;
105 }
106
107 if (!key->pqdsa->method->pqdsa_keygen_internal(public_key, private_key,
108 CBS_data(in))) {
109 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
110 goto err;
111 }
112
113 if (!CBS_copy_bytes(in, seed, key->pqdsa->keygen_seed_len)) {
114 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
115 goto err;
116 }
117
118 // Success: transfer ownership to key.
119 OPENSSL_free(key->public_key);
120 OPENSSL_free(key->private_key);
121 OPENSSL_free(key->seed);
122 key->public_key = public_key;
123 key->private_key = private_key;
124 key->seed = seed;
125 public_key = NULL;
126 private_key = NULL;
127 seed = NULL;
128 ret = 1;
129
130 err:
131 OPENSSL_free(public_key);
132 OPENSSL_free(private_key);
133 OPENSSL_free(seed);
134 return ret;
135 }
136
137 int PQDSA_KEY_set_raw_private_key(PQDSA_KEY *key, CBS *in) {
138 // Check if the parsed length corresponds with the expected length.
139 if (CBS_len(in) != key->pqdsa->private_key_len) {
140 OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE);
141 return 0;
142 }
143
144 int ret = 0;
145 uint8_t *private_key = OPENSSL_memdup(CBS_data(in), key->pqdsa->private_key_len);
146 uint8_t *public_key = OPENSSL_malloc(key->pqdsa->public_key_len);
147 if (private_key == NULL || public_key == NULL) {
148 goto err;
149 }
150
151 if (!key->pqdsa->method->pqdsa_pack_pk_from_sk(public_key, private_key)) {
152 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
153 goto err;
154 }
155
156 // Success: transfer ownership to key.
157 OPENSSL_free(key->public_key);
158 OPENSSL_free(key->private_key);
159 OPENSSL_free(key->seed);
160 key->public_key = public_key;
161 key->private_key = private_key;
162 key->seed = NULL;
163 public_key = NULL;
164 private_key = NULL;
165 ret = 1;
166
167 err:
168 OPENSSL_free(public_key);
169 OPENSSL_free(private_key);
170 return ret;
171 }
172
173 /*
174 * Sets up a PQDSA_KEY structure using both a seed and an expanded private key.
175 * This function is used when both the seed and expanded key are provided in the
176 * ASN.1 encoding.
177 *
178 * The function performs the following steps:
179 * 1. Generates a keypair from the provided seed.
180 * 2. Derives a public key from the provided expanded private key.
181 * 3. Compares the public keys from steps 1 and 2 to ensure consistency.
182 * 4. If consistent, stores the seed, expanded private key, and derived public key
183 * in the PQDSA_KEY structure.
184 */
185 int PQDSA_KEY_set_raw_keypair_from_both(PQDSA_KEY *key, CBS *seed,
186 CBS *expanded_key) {
187 // Check if the parsed length corresponds with the expected length.
188 if (CBS_len(seed) != key->pqdsa->keygen_seed_len ||
189 CBS_len(expanded_key) != key->pqdsa->private_key_len) {
190 OPENSSL_PUT_ERROR(EVP, EVP_R_INVALID_BUFFER_SIZE);
191 return 0;
192 }
193
194 int ret = 0;
195 uint8_t *seed_public_key = NULL;
196 uint8_t *seed_private_key = NULL;
197 uint8_t *expanded_public_key = NULL;
198 uint8_t *new_private_key = NULL;
199 uint8_t *new_seed = NULL;
200
201 // Allocate temp buffers for seed-derived keypair.
202 seed_public_key = OPENSSL_malloc(key->pqdsa->public_key_len);
203 seed_private_key = OPENSSL_malloc(key->pqdsa->private_key_len);
204 if (seed_public_key == NULL || seed_private_key == NULL) {
205 goto err;
206 }
207
208 // Generate keypair from seed.
209 if (!key->pqdsa->method->pqdsa_keygen_internal(seed_public_key,
210 seed_private_key,
211 CBS_data(seed))) {
212 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
213 goto err;
214 }
215
216 // Derive public key from the expanded private key.
217 expanded_public_key = OPENSSL_malloc(key->pqdsa->public_key_len);
218 if (expanded_public_key == NULL) {
219 goto err;
220 }
221
222 if (!key->pqdsa->method->pqdsa_pack_pk_from_sk(expanded_public_key,
223 CBS_data(expanded_key))) {
224 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
225 goto err;
226 }
227
228 // Compare public keys for consistency.
229 if (CRYPTO_memcmp(seed_public_key, expanded_public_key,
230 key->pqdsa->public_key_len) != 0) {
231 OPENSSL_PUT_ERROR(EVP, EVP_R_DECODE_ERROR);
232 goto err;
233 }
234
235 // Allocate final copies of private key and seed.
236 new_private_key = OPENSSL_memdup(CBS_data(expanded_key),
237 key->pqdsa->private_key_len);
238 new_seed = OPENSSL_memdup(CBS_data(seed), key->pqdsa->keygen_seed_len);
239 if (new_private_key == NULL || new_seed == NULL) {
240 goto err;
241 }
242
243 // Success: transfer ownership to key.
244 OPENSSL_free(key->public_key);
245 OPENSSL_free(key->private_key);
246 OPENSSL_free(key->seed);
247 key->public_key = expanded_public_key;
248 key->private_key = new_private_key;
249 key->seed = new_seed;
250 expanded_public_key = NULL;
251 new_private_key = NULL;
252 new_seed = NULL;
253 ret = 1;
254
255 err:
256 OPENSSL_free(seed_public_key);
257 OPENSSL_free(seed_private_key);
258 OPENSSL_free(expanded_public_key);
259 OPENSSL_free(new_private_key);
260 OPENSSL_free(new_seed);
261 return ret;
262 }
263
264 DEFINE_LOCAL_DATA(PQDSA_METHOD, sig_ml_dsa_44_method) {
265 out->pqdsa_keygen = ml_dsa_44_keypair;
266 out->pqdsa_keygen_internal = ml_dsa_44_keypair_internal;
267 out->pqdsa_sign_message = ml_dsa_44_sign;
268 out->pqdsa_sign = ml_dsa_extmu_44_sign;
269 out->pqdsa_verify_message = ml_dsa_44_verify;
270 out->pqdsa_verify = ml_dsa_extmu_44_verify;
271 out->pqdsa_pack_pk_from_sk = ml_dsa_44_pack_pk_from_sk;
272 }
273
274 DEFINE_LOCAL_DATA(PQDSA_METHOD, sig_ml_dsa_65_method) {
275 out->pqdsa_keygen = ml_dsa_65_keypair;
276 out->pqdsa_keygen_internal = ml_dsa_65_keypair_internal;
277 out->pqdsa_sign_message = ml_dsa_65_sign;
278 out->pqdsa_sign = ml_dsa_extmu_65_sign;
279 out->pqdsa_verify_message = ml_dsa_65_verify;
280 out->pqdsa_verify = ml_dsa_extmu_65_verify;
281 out->pqdsa_pack_pk_from_sk = ml_dsa_65_pack_pk_from_sk;
282 }
283
284 DEFINE_LOCAL_DATA(PQDSA_METHOD, sig_ml_dsa_87_method) {
285 out->pqdsa_keygen = ml_dsa_87_keypair;
286 out->pqdsa_keygen_internal = ml_dsa_87_keypair_internal;
287 out->pqdsa_sign_message = ml_dsa_87_sign;
288 out->pqdsa_sign = ml_dsa_extmu_87_sign;
289 out->pqdsa_verify_message = ml_dsa_87_verify;
290 out->pqdsa_verify = ml_dsa_extmu_87_verify;
291 out->pqdsa_pack_pk_from_sk = ml_dsa_87_pack_pk_from_sk;
292 }
293
294 DEFINE_LOCAL_DATA(PQDSA, sig_ml_dsa_44) {
295 out->nid = NID_MLDSA44;
296 out->oid = kOIDMLDSA44;
297 out->oid_len = sizeof(kOIDMLDSA44);
298 out->comment = "MLDSA44";
299 out->public_key_len = MLDSA44_PUBLIC_KEY_BYTES;
300 out->private_key_len = MLDSA44_PRIVATE_KEY_BYTES;
301 out->signature_len = MLDSA44_SIGNATURE_BYTES;
302 out->keygen_seed_len = MLDSA44_KEYGEN_SEED_BYTES;
303 out->sign_seed_len = MLDSA44_SIGNATURE_SEED_BYTES;
304 out->digest_len = 64; // MLDSA_CRHBYTES, unavailable here due to bcm.c undef
305 out->method = sig_ml_dsa_44_method();
306 }
307
308 DEFINE_LOCAL_DATA(PQDSA, sig_ml_dsa_65) {
309 out->nid = NID_MLDSA65;
310 out->oid = kOIDMLDSA65;
311 out->oid_len = sizeof(kOIDMLDSA65);
312 out->comment = "MLDSA65";
313 out->public_key_len = MLDSA65_PUBLIC_KEY_BYTES;
314 out->private_key_len = MLDSA65_PRIVATE_KEY_BYTES;
315 out->signature_len = MLDSA65_SIGNATURE_BYTES;
316 out->keygen_seed_len = MLDSA65_KEYGEN_SEED_BYTES;
317 out->sign_seed_len = MLDSA65_SIGNATURE_SEED_BYTES;
318 out->digest_len = 64; // MLDSA_CRHBYTES, unavailable here due to bcm.c undef
319 out->method = sig_ml_dsa_65_method();
320 }
321
322 DEFINE_LOCAL_DATA(PQDSA, sig_ml_dsa_87) {
323 out->nid = NID_MLDSA87;
324 out->oid = kOIDMLDSA87;
325 out->oid_len = sizeof(kOIDMLDSA87);
326 out->comment = "MLDSA87";
327 out->public_key_len = MLDSA87_PUBLIC_KEY_BYTES;
328 out->private_key_len = MLDSA87_PRIVATE_KEY_BYTES;
329 out->signature_len = MLDSA87_SIGNATURE_BYTES;
330 out->keygen_seed_len = MLDSA87_KEYGEN_SEED_BYTES;
331 out->sign_seed_len = MLDSA87_SIGNATURE_SEED_BYTES;
332 out->digest_len = 64; // MLDSA_CRHBYTES, unavailable here due to bcm.c undef
333 out->method = sig_ml_dsa_87_method();
334 }
335
336 const PQDSA *PQDSA_find_dsa_by_nid(int nid) {
337 switch (nid) {
338 case NID_MLDSA44:
339 return sig_ml_dsa_44();
340 case NID_MLDSA65:
341 return sig_ml_dsa_65();
342 case NID_MLDSA87:
343 return sig_ml_dsa_87();
344 default:
345 return NULL;
346 }
347 }
348
349 const EVP_PKEY_ASN1_METHOD *PQDSA_find_asn1_by_nid(int nid) {
350 switch (nid) {
351 case NID_MLDSA44:
352 case NID_MLDSA65:
353 case NID_MLDSA87:
354 return &pqdsa_asn1_meth;
355 default:
356 return NULL;
357 }
358 }