]>
Commit | Line | Data |
---|---|---|
6ae5543c | 1 | /* |
f552d900 | 2 | * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved. |
6ae5543c RL |
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 | ||
96ebe52e | 10 | #include <string.h> /* memset */ |
6ae5543c RL |
11 | #include <openssl/evp.h> |
12 | #include <openssl/pem.h> | |
13 | #include <openssl/serializer.h> | |
14 | #include <openssl/provider.h> | |
7165593c | 15 | #include <openssl/param_build.h> |
6ae5543c | 16 | #include <openssl/core_names.h> |
8efc4a9c | 17 | #include "crypto/ecx.h" |
6ae5543c | 18 | #include "crypto/evp.h" /* For the internal API */ |
7165593c SL |
19 | #include "crypto/bn_dh.h" /* _bignum_ffdhe2048_p */ |
20 | #include "internal/nelem.h" | |
6ae5543c RL |
21 | #include "testutil.h" |
22 | ||
8efc4a9c MC |
23 | static char *datadir = NULL; |
24 | ||
25 | #define PRIV_TEXT 0 | |
26 | #define PRIV_PEM 1 | |
27 | #define PRIV_DER 2 | |
28 | #define PUB_TEXT 3 | |
29 | #define PUB_PEM 4 | |
30 | #define PUB_DER 5 | |
31 | ||
32 | static void stripcr(char *buf, size_t *len) | |
33 | { | |
34 | size_t i; | |
35 | char *curr, *writ; | |
36 | ||
37 | for (i = *len, curr = buf, writ = buf; i > 0; i--, curr++) { | |
38 | if (*curr == '\r') { | |
39 | (*len)--; | |
40 | continue; | |
41 | } | |
42 | if (curr != writ) | |
43 | *writ = *curr; | |
44 | writ++; | |
45 | } | |
46 | } | |
47 | ||
48 | static int compare_with_file(const char *alg, int type, BIO *membio) | |
49 | { | |
50 | char filename[80]; | |
51 | BIO *file = NULL; | |
b03ec3b5 | 52 | char buf[4096]; |
8efc4a9c MC |
53 | char *memdata, *fullfile = NULL; |
54 | const char *suffix; | |
55 | size_t readbytes; | |
56 | int ret = 0; | |
57 | int len; | |
58 | size_t slen; | |
59 | ||
60 | switch (type) { | |
61 | case PRIV_TEXT: | |
62 | suffix = "priv.txt"; | |
63 | break; | |
64 | ||
65 | case PRIV_PEM: | |
66 | suffix = "priv.pem"; | |
67 | break; | |
68 | ||
69 | case PRIV_DER: | |
70 | suffix = "priv.der"; | |
71 | break; | |
72 | ||
73 | case PUB_TEXT: | |
74 | suffix = "pub.txt"; | |
75 | break; | |
76 | ||
77 | case PUB_PEM: | |
78 | suffix = "pub.pem"; | |
79 | break; | |
80 | ||
81 | case PUB_DER: | |
82 | suffix = "pub.der"; | |
83 | break; | |
84 | ||
85 | default: | |
86 | TEST_error("Invalid file type"); | |
87 | goto err; | |
88 | } | |
89 | ||
90 | BIO_snprintf(filename, sizeof(filename), "%s.%s", alg, suffix); | |
91 | fullfile = test_mk_file_path(datadir, filename); | |
92 | if (!TEST_ptr(fullfile)) | |
93 | goto err; | |
94 | ||
95 | file = BIO_new_file(fullfile, "rb"); | |
96 | if (!TEST_ptr(file)) | |
97 | goto err; | |
98 | ||
99 | if (!TEST_true(BIO_read_ex(file, buf, sizeof(buf), &readbytes)) | |
100 | || !TEST_true(BIO_eof(file)) | |
101 | || !TEST_size_t_lt(readbytes, sizeof(buf))) | |
102 | goto err; | |
103 | ||
104 | len = BIO_get_mem_data(membio, &memdata); | |
105 | if (!TEST_int_gt(len, 0)) | |
106 | goto err; | |
107 | ||
108 | slen = len; | |
109 | if (type != PRIV_DER && type != PUB_DER) { | |
110 | stripcr(memdata, &slen); | |
111 | stripcr(buf, &readbytes); | |
112 | } | |
113 | ||
114 | if (!TEST_mem_eq(memdata, slen, buf, readbytes)) | |
115 | goto err; | |
116 | ||
117 | ret = 1; | |
118 | err: | |
119 | OPENSSL_free(fullfile); | |
120 | (void)BIO_reset(membio); | |
121 | BIO_free(file); | |
122 | return ret; | |
123 | } | |
124 | ||
125 | static int test_print_key_using_pem(const char *alg, const EVP_PKEY *pk) | |
6ae5543c | 126 | { |
8efc4a9c MC |
127 | BIO *membio = BIO_new(BIO_s_mem()); |
128 | int ret = 0; | |
129 | ||
130 | if (!TEST_ptr(membio)) | |
131 | goto err; | |
132 | ||
d59b7a54 RL |
133 | if (/* Output Encrypted private key in PEM form */ |
134 | !TEST_true(PEM_write_bio_PrivateKey(bio_out, pk, EVP_aes_256_cbc(), | |
135 | (unsigned char *)"pass", 4, | |
136 | NULL, NULL)) | |
137 | /* Private key in text form */ | |
138 | || !TEST_true(EVP_PKEY_print_private(membio, pk, 0, NULL)) | |
8efc4a9c | 139 | || !TEST_true(compare_with_file(alg, PRIV_TEXT, membio)) |
6ae5543c | 140 | /* Public key in PEM form */ |
8efc4a9c MC |
141 | || !TEST_true(PEM_write_bio_PUBKEY(membio, pk)) |
142 | || !TEST_true(compare_with_file(alg, PUB_PEM, membio)) | |
6ae5543c | 143 | /* Unencrypted private key in PEM form */ |
8efc4a9c | 144 | || !TEST_true(PEM_write_bio_PrivateKey(membio, pk, |
6ae5543c | 145 | NULL, NULL, 0, NULL, NULL)) |
d59b7a54 | 146 | || !TEST_true(compare_with_file(alg, PRIV_PEM, membio))) |
8efc4a9c | 147 | goto err; |
6ae5543c | 148 | |
8efc4a9c MC |
149 | ret = 1; |
150 | err: | |
151 | BIO_free(membio); | |
152 | return ret; | |
6ae5543c RL |
153 | } |
154 | ||
8efc4a9c MC |
155 | static int test_print_key_type_using_serializer(const char *alg, int type, |
156 | const EVP_PKEY *pk) | |
6ae5543c | 157 | { |
8efc4a9c | 158 | const char *pq; |
6ae5543c | 159 | OSSL_SERIALIZER_CTX *ctx = NULL; |
8efc4a9c | 160 | BIO *membio = BIO_new(BIO_s_mem()); |
f552d900 | 161 | int ret = 0; |
6ae5543c | 162 | |
8efc4a9c MC |
163 | switch (type) { |
164 | case PRIV_TEXT: | |
165 | pq = OSSL_SERIALIZER_PrivateKey_TO_TEXT_PQ; | |
166 | break; | |
167 | ||
168 | case PRIV_PEM: | |
169 | pq = OSSL_SERIALIZER_PrivateKey_TO_PEM_PQ; | |
170 | break; | |
171 | ||
172 | case PRIV_DER: | |
173 | pq = OSSL_SERIALIZER_PrivateKey_TO_DER_PQ; | |
174 | break; | |
175 | ||
176 | case PUB_TEXT: | |
177 | pq = OSSL_SERIALIZER_PUBKEY_TO_TEXT_PQ; | |
178 | break; | |
179 | ||
180 | case PUB_PEM: | |
181 | pq = OSSL_SERIALIZER_PUBKEY_TO_PEM_PQ; | |
182 | break; | |
183 | ||
184 | case PUB_DER: | |
185 | pq = OSSL_SERIALIZER_PUBKEY_TO_DER_PQ; | |
186 | break; | |
187 | ||
188 | default: | |
189 | TEST_error("Invalid serialization type"); | |
190 | goto err; | |
191 | } | |
192 | ||
f552d900 | 193 | if (!TEST_ptr(membio)) |
8efc4a9c | 194 | goto err; |
8efc4a9c | 195 | |
6ae5543c RL |
196 | /* Make a context, it's valid for several prints */ |
197 | TEST_note("Setting up a OSSL_SERIALIZER context with passphrase"); | |
198 | if (!TEST_ptr(ctx = OSSL_SERIALIZER_CTX_new_by_EVP_PKEY(pk, pq)) | |
199 | /* Check that this operation is supported */ | |
8efc4a9c | 200 | || !TEST_ptr(OSSL_SERIALIZER_CTX_get_serializer(ctx))) |
6ae5543c RL |
201 | goto err; |
202 | ||
203 | /* Use no cipher. This should give us an unencrypted PEM */ | |
8efc4a9c MC |
204 | TEST_note("Testing with no encryption"); |
205 | if (!TEST_true(OSSL_SERIALIZER_to_bio(ctx, membio)) | |
206 | || !TEST_true(compare_with_file(alg, type, membio))) | |
f552d900 | 207 | goto err; |
6ae5543c | 208 | |
8efc4a9c MC |
209 | if (type == PRIV_PEM) { |
210 | /* Set a passphrase to be used later */ | |
211 | if (!TEST_true(OSSL_SERIALIZER_CTX_set_passphrase(ctx, | |
212 | (unsigned char *)"pass", | |
213 | 4))) | |
214 | goto err; | |
6ae5543c | 215 | |
8efc4a9c MC |
216 | /* Use a valid cipher name */ |
217 | TEST_note("Displaying PEM encrypted with AES-256-CBC"); | |
218 | if (!TEST_true(OSSL_SERIALIZER_CTX_set_cipher(ctx, "AES-256-CBC", NULL)) | |
219 | || !TEST_true(OSSL_SERIALIZER_to_bio(ctx, bio_out))) | |
f552d900 | 220 | goto err; |
6ae5543c | 221 | |
8efc4a9c MC |
222 | /* Use an invalid cipher name, which should generate no output */ |
223 | TEST_note("NOT Displaying PEM encrypted with (invalid) FOO"); | |
224 | if (!TEST_false(OSSL_SERIALIZER_CTX_set_cipher(ctx, "FOO", NULL)) | |
225 | || !TEST_false(OSSL_SERIALIZER_to_bio(ctx, bio_out))) | |
f552d900 | 226 | goto err; |
8efc4a9c MC |
227 | |
228 | /* Clear the cipher. This should give us an unencrypted PEM again */ | |
229 | TEST_note("Testing with encryption cleared (no encryption)"); | |
230 | if (!TEST_true(OSSL_SERIALIZER_CTX_set_cipher(ctx, NULL, NULL)) | |
231 | || !TEST_true(OSSL_SERIALIZER_to_bio(ctx, membio)) | |
232 | || !TEST_true(compare_with_file(alg, type, membio))) | |
f552d900 | 233 | goto err; |
8efc4a9c | 234 | } |
f552d900 | 235 | ret = 1; |
6ae5543c | 236 | err: |
8efc4a9c | 237 | BIO_free(membio); |
6ae5543c RL |
238 | OSSL_SERIALIZER_CTX_free(ctx); |
239 | return ret; | |
240 | } | |
241 | ||
8efc4a9c MC |
242 | static int test_print_key_using_serializer(const char *alg, const EVP_PKEY *pk) |
243 | { | |
244 | int i; | |
245 | int ret = 1; | |
246 | ||
247 | for (i = 0; i < 6; i++) | |
248 | ret = ret && test_print_key_type_using_serializer(alg, i, pk); | |
249 | ||
250 | return ret; | |
251 | } | |
252 | ||
6ae5543c RL |
253 | /* Array indexes used in test_fromdata_rsa */ |
254 | #define N 0 | |
255 | #define E 1 | |
256 | #define D 2 | |
257 | #define P 3 | |
258 | #define Q 4 | |
259 | #define DP 5 | |
260 | #define DQ 6 | |
261 | #define QINV 7 | |
262 | ||
263 | static int test_fromdata_rsa(void) | |
264 | { | |
96ebe52e | 265 | int ret = 0, i; |
12603de6 | 266 | EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL; |
ff7262b4 | 267 | EVP_PKEY *pk = NULL, *copy_pk = NULL; |
6ae5543c RL |
268 | /* |
269 | * 32-bit RSA key, extracted from this command, | |
270 | * executed with OpenSSL 1.0.2: | |
271 | * | |
272 | * openssl genrsa 32 | openssl rsa -text | |
273 | */ | |
274 | static unsigned long key_numbers[] = { | |
275 | 0xbc747fc5, /* N */ | |
276 | 0x10001, /* E */ | |
277 | 0x7b133399, /* D */ | |
278 | 0xe963, /* P */ | |
279 | 0xceb7, /* Q */ | |
280 | 0x8599, /* DP */ | |
281 | 0xbd87, /* DQ */ | |
282 | 0xcc3b, /* QINV */ | |
283 | }; | |
284 | OSSL_PARAM fromdata_params[] = { | |
285 | OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_N, &key_numbers[N]), | |
286 | OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_E, &key_numbers[E]), | |
287 | OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_D, &key_numbers[D]), | |
96ebe52e SL |
288 | OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_FACTOR1, &key_numbers[P]), |
289 | OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_FACTOR2, &key_numbers[Q]), | |
290 | OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_EXPONENT1, &key_numbers[DP]), | |
291 | OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_EXPONENT2, &key_numbers[DQ]), | |
292 | OSSL_PARAM_ulong(OSSL_PKEY_PARAM_RSA_COEFFICIENT1, &key_numbers[QINV]), | |
6ae5543c RL |
293 | OSSL_PARAM_END |
294 | }; | |
96ebe52e SL |
295 | BIGNUM *bn = BN_new(); |
296 | BIGNUM *bn_from = BN_new(); | |
6ae5543c | 297 | |
e683582b | 298 | if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL))) |
6ae5543c RL |
299 | goto err; |
300 | ||
301 | if (!TEST_true(EVP_PKEY_key_fromdata_init(ctx)) | |
81a624f2 RL |
302 | || !TEST_true(EVP_PKEY_fromdata(ctx, &pk, fromdata_params)) |
303 | || !TEST_int_eq(EVP_PKEY_bits(pk), 32) | |
304 | || !TEST_int_eq(EVP_PKEY_security_bits(pk), 8) | |
305 | || !TEST_int_eq(EVP_PKEY_size(pk), 4)) | |
6ae5543c RL |
306 | goto err; |
307 | ||
12603de6 SL |
308 | if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, ""))) |
309 | goto err; | |
310 | ||
311 | if (!TEST_true(EVP_PKEY_check(key_ctx)) | |
312 | || !TEST_true(EVP_PKEY_public_check(key_ctx)) | |
313 | || !TEST_true(EVP_PKEY_private_check(key_ctx)) | |
314 | || !TEST_true(EVP_PKEY_pairwise_check(key_ctx))) | |
315 | goto err; | |
316 | ||
ff7262b4 RL |
317 | /* EVP_PKEY_copy_parameters() should fail for RSA */ |
318 | if (!TEST_ptr(copy_pk = EVP_PKEY_new()) | |
319 | || !TEST_false(EVP_PKEY_copy_parameters(copy_pk, pk))) | |
320 | goto err; | |
321 | ||
96ebe52e SL |
322 | for (i = 0; fromdata_params[i].key != NULL; ++i) { |
323 | if (!TEST_true(BN_set_word(bn_from, key_numbers[i])) | |
324 | || !TEST_true(EVP_PKEY_get_bn_param(pk, fromdata_params[i].key, &bn)) | |
325 | || !TEST_BN_eq(bn, bn_from)) | |
326 | goto err; | |
327 | } | |
8efc4a9c MC |
328 | ret = test_print_key_using_pem("RSA", pk) |
329 | && test_print_key_using_serializer("RSA", pk); | |
6ae5543c | 330 | err: |
96ebe52e SL |
331 | BN_free(bn_from); |
332 | BN_free(bn); | |
6ae5543c | 333 | EVP_PKEY_free(pk); |
ff7262b4 | 334 | EVP_PKEY_free(copy_pk); |
12603de6 | 335 | EVP_PKEY_CTX_free(key_ctx); |
6ae5543c RL |
336 | EVP_PKEY_CTX_free(ctx); |
337 | ||
338 | return ret; | |
339 | } | |
340 | ||
96ebe52e SL |
341 | static int test_evp_pkey_get_bn_param_large(void) |
342 | { | |
343 | int ret = 0; | |
344 | EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL; | |
345 | EVP_PKEY *pk = NULL; | |
346 | OSSL_PARAM_BLD *bld = NULL; | |
347 | OSSL_PARAM *fromdata_params = NULL; | |
348 | BIGNUM *n = NULL, *e = NULL, *d = NULL, *n_out = NULL; | |
349 | /* | |
350 | * The buffer size chosen here for n_data larger than the buffer used | |
351 | * internally in EVP_PKEY_get_bn_param. | |
352 | */ | |
353 | static unsigned char n_data[2050]; | |
354 | static const unsigned char e_data[] = { | |
355 | 0x1, 0x00, 0x01 | |
356 | }; | |
357 | static const unsigned char d_data[]= { | |
358 | 0x99, 0x33, 0x13, 0x7b | |
359 | }; | |
360 | ||
361 | /* N is a large buffer */ | |
362 | memset(n_data, 0xCE, sizeof(n_data)); | |
363 | ||
364 | if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) | |
365 | || !TEST_ptr(n = BN_bin2bn(n_data, sizeof(n_data), NULL)) | |
366 | || !TEST_ptr(e = BN_bin2bn(e_data, sizeof(e_data), NULL)) | |
367 | || !TEST_ptr(d = BN_bin2bn(d_data, sizeof(d_data), NULL)) | |
368 | || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_N, n)) | |
369 | || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_E, e)) | |
370 | || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_RSA_D, d)) | |
371 | || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld)) | |
372 | || !TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "RSA", NULL)) | |
373 | || !TEST_true(EVP_PKEY_key_fromdata_init(ctx)) | |
374 | || !TEST_true(EVP_PKEY_fromdata(ctx, &pk, fromdata_params)) | |
375 | || !TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, "")) | |
376 | || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_RSA_N, &n_out)) | |
377 | || !TEST_BN_eq(n, n_out)) | |
378 | goto err; | |
379 | ret = 1; | |
380 | err: | |
381 | BN_free(n_out); | |
382 | BN_free(n); | |
383 | BN_free(e); | |
384 | BN_free(d); | |
385 | EVP_PKEY_free(pk); | |
386 | EVP_PKEY_CTX_free(key_ctx); | |
387 | EVP_PKEY_CTX_free(ctx); | |
388 | OSSL_PARAM_BLD_free_params(fromdata_params); | |
389 | OSSL_PARAM_BLD_free(bld); | |
390 | return ret; | |
391 | } | |
392 | ||
393 | ||
285c6913 | 394 | #ifndef OPENSSL_NO_DH |
7165593c | 395 | static int test_fromdata_dh_named_group(void) |
6ae5543c RL |
396 | { |
397 | int ret = 0; | |
7165593c | 398 | int gindex = 0, pcounter = 0, hindex = 0; |
a54ff473 | 399 | EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL; |
ff7262b4 | 400 | EVP_PKEY *pk = NULL, *copy_pk = NULL; |
7165593c SL |
401 | size_t len; |
402 | BIGNUM *pub = NULL, *priv = NULL; | |
403 | BIGNUM *pub_out = NULL, *priv_out = NULL; | |
404 | BIGNUM *p = NULL, *q = NULL, *g = NULL, *j = NULL; | |
405 | OSSL_PARAM *fromdata_params = NULL; | |
406 | OSSL_PARAM_BLD *bld = NULL; | |
407 | char name_out[80]; | |
408 | unsigned char seed_out[32]; | |
409 | ||
6ae5543c | 410 | /* |
7165593c | 411 | * DH key data was generated using the following: |
738ee181 SL |
412 | * openssl genpkey -algorithm DH -pkeyopt group:ffdhe2048 |
413 | * -pkeyopt priv_len:224 -text | |
6ae5543c | 414 | */ |
7165593c SL |
415 | static const unsigned char priv_data[] = { |
416 | 0x88, 0x85, 0xe7, 0x9f, 0xee, 0x6d, 0xc5, 0x7c, 0x78, 0xaf, 0x63, 0x5d, | |
417 | 0x38, 0x2a, 0xd0, 0xed, 0x56, 0x4b, 0x47, 0x21, 0x2b, 0xfa, 0x55, 0xfa, | |
418 | 0x87, 0xe8, 0xa9, 0x7b, | |
6ae5543c | 419 | }; |
7165593c SL |
420 | static const unsigned char pub_data[] = { |
421 | 0x00, 0xd6, 0x2d, 0x77, 0xe0, 0xd3, 0x7d, 0xf8, 0xeb, 0x98, 0x50, 0xa1, | |
422 | 0x82, 0x22, 0x65, 0xd5, 0xd9, 0xfe, 0xc9, 0x3f, 0xbe, 0x16, 0x83, 0xbd, | |
423 | 0x33, 0xe9, 0xc6, 0x93, 0xcf, 0x08, 0xaf, 0x83, 0xfa, 0x80, 0x8a, 0x6c, | |
424 | 0x64, 0xdf, 0x70, 0x64, 0xd5, 0x0a, 0x7c, 0x5a, 0x72, 0xda, 0x66, 0xe6, | |
425 | 0xf9, 0xf5, 0x31, 0x21, 0x92, 0xb0, 0x60, 0x1a, 0xb5, 0xd3, 0xf0, 0xa5, | |
426 | 0xfa, 0x48, 0x95, 0x2e, 0x38, 0xd9, 0xc5, 0xe6, 0xda, 0xfb, 0x6c, 0x03, | |
427 | 0x9d, 0x4b, 0x69, 0xb7, 0x95, 0xe4, 0x5c, 0xc0, 0x93, 0x4f, 0x48, 0xd9, | |
428 | 0x7e, 0x06, 0x22, 0xb2, 0xde, 0xf3, 0x79, 0x24, 0xed, 0xe1, 0xd1, 0x4a, | |
429 | 0x57, 0xf1, 0x40, 0x86, 0x70, 0x42, 0x25, 0xc5, 0x27, 0x68, 0xc9, 0xfa, | |
430 | 0xe5, 0x8e, 0x62, 0x7e, 0xff, 0x49, 0x6c, 0x5b, 0xb5, 0xba, 0xf9, 0xef, | |
431 | 0x9a, 0x1a, 0x10, 0xd4, 0x81, 0x53, 0xcf, 0x83, 0x04, 0x18, 0x1c, 0xe1, | |
432 | 0xdb, 0xe1, 0x65, 0xa9, 0x7f, 0xe1, 0x33, 0xeb, 0xc3, 0x4f, 0xe3, 0xb7, | |
433 | 0x22, 0xf7, 0x1c, 0x09, 0x4f, 0xed, 0xc6, 0x07, 0x8e, 0x78, 0x05, 0x8f, | |
434 | 0x7c, 0x96, 0xd9, 0x12, 0xe0, 0x81, 0x74, 0x1a, 0xe9, 0x13, 0xc0, 0x20, | |
435 | 0x82, 0x65, 0xbb, 0x42, 0x3b, 0xed, 0x08, 0x6a, 0x84, 0x4f, 0xea, 0x77, | |
436 | 0x14, 0x32, 0xf9, 0xed, 0xc2, 0x12, 0xd6, 0xc5, 0xc6, 0xb3, 0xe5, 0xf2, | |
437 | 0x6e, 0xf6, 0x16, 0x7f, 0x37, 0xde, 0xbc, 0x09, 0xc7, 0x06, 0x6b, 0x12, | |
438 | 0xbc, 0xad, 0x2d, 0x49, 0x25, 0xd5, 0xdc, 0xf4, 0x18, 0x14, 0xd2, 0xf0, | |
439 | 0xf1, 0x1d, 0x1f, 0x3a, 0xaa, 0x15, 0x55, 0xbb, 0x0d, 0x7f, 0xbe, 0x67, | |
440 | 0xa1, 0xa7, 0xf0, 0xaa, 0xb3, 0xfb, 0x41, 0x82, 0x39, 0x49, 0x93, 0xbc, | |
441 | 0xa8, 0xee, 0x72, 0x13, 0x45, 0x65, 0x15, 0x42, 0x17, 0xaa, 0xd8, 0xab, | |
442 | 0xcf, 0x33, 0x42, 0x83, 0x42 | |
6ae5543c | 443 | }; |
7165593c SL |
444 | static const char group_name[] = "ffdhe2048"; |
445 | ||
446 | if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) | |
447 | || !TEST_ptr(pub = BN_bin2bn(pub_data, sizeof(pub_data), NULL)) | |
448 | || !TEST_ptr(priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL)) | |
449 | || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, | |
023b188c | 450 | OSSL_PKEY_PARAM_GROUP_NAME, |
7165593c SL |
451 | group_name, 0)) |
452 | || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, pub)) | |
453 | || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv)) | |
454 | || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld))) | |
455 | goto err; | |
6ae5543c | 456 | |
e683582b | 457 | if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL))) |
6ae5543c RL |
458 | goto err; |
459 | ||
460 | if (!TEST_true(EVP_PKEY_key_fromdata_init(ctx)) | |
81a624f2 | 461 | || !TEST_true(EVP_PKEY_fromdata(ctx, &pk, fromdata_params)) |
7165593c SL |
462 | || !TEST_int_eq(EVP_PKEY_bits(pk), 2048) |
463 | || !TEST_int_eq(EVP_PKEY_security_bits(pk), 112) | |
464 | || !TEST_int_eq(EVP_PKEY_size(pk), 256)) | |
465 | goto err; | |
466 | ||
023b188c | 467 | if (!TEST_true(EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME, |
7165593c SL |
468 | name_out, sizeof(name_out), |
469 | &len)) | |
470 | || !TEST_str_eq(name_out, group_name) | |
471 | || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PUB_KEY, | |
472 | &pub_out)) | |
473 | ||
474 | || !TEST_BN_eq(pub, pub_out) | |
475 | || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY, | |
476 | &priv_out)) | |
477 | || !TEST_BN_eq(priv, priv_out) | |
478 | || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_P, &p)) | |
479 | || !TEST_BN_eq(&_bignum_ffdhe2048_p, p) | |
480 | || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_Q, &q)) | |
481 | || !TEST_ptr(q) | |
482 | || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_G, &g)) | |
483 | || !TEST_BN_eq(&_bignum_const_2, g) | |
484 | || !TEST_false(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_COFACTOR, | |
485 | &j)) | |
486 | || !TEST_ptr_null(j) | |
487 | || !TEST_false(EVP_PKEY_get_octet_string_param(pk, | |
488 | OSSL_PKEY_PARAM_FFC_SEED, | |
489 | seed_out, | |
490 | sizeof(seed_out), &len)) | |
491 | || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_GINDEX, | |
492 | &gindex)) | |
493 | || !TEST_int_eq(gindex, -1) | |
494 | || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_H, &hindex)) | |
495 | || !TEST_int_eq(hindex, 0) | |
496 | || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_PCOUNTER, | |
497 | &pcounter)) | |
498 | || !TEST_int_eq(pcounter, -1)) | |
499 | goto err; | |
500 | ||
501 | if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, ""))) | |
502 | goto err; | |
503 | ||
504 | if (!TEST_true(EVP_PKEY_check(key_ctx)) | |
505 | || !TEST_true(EVP_PKEY_public_check(key_ctx)) | |
506 | || !TEST_true(EVP_PKEY_private_check(key_ctx)) | |
507 | || !TEST_true(EVP_PKEY_pairwise_check(key_ctx))) | |
6ae5543c RL |
508 | goto err; |
509 | ||
ff7262b4 RL |
510 | if (!TEST_ptr(copy_pk = EVP_PKEY_new()) |
511 | || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk))) | |
512 | goto err; | |
513 | ||
7165593c SL |
514 | ret = test_print_key_using_pem("DH", pk) |
515 | && test_print_key_using_serializer("DH", pk); | |
516 | err: | |
517 | BN_free(p); | |
518 | BN_free(q); | |
519 | BN_free(g); | |
520 | BN_free(j); | |
521 | BN_free(pub); | |
522 | BN_free(priv); | |
523 | BN_free(pub_out); | |
524 | BN_free(priv_out); | |
525 | EVP_PKEY_free(copy_pk); | |
526 | EVP_PKEY_free(pk); | |
527 | EVP_PKEY_CTX_free(ctx); | |
528 | EVP_PKEY_CTX_free(key_ctx); | |
529 | OSSL_PARAM_BLD_free_params(fromdata_params); | |
530 | OSSL_PARAM_BLD_free(bld); | |
531 | ||
532 | return ret; | |
533 | } | |
534 | ||
535 | static int test_fromdata_dh_fips186_4(void) | |
536 | { | |
537 | int ret = 0; | |
538 | int gindex = 0, pcounter = 0, hindex = 0; | |
539 | EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL; | |
540 | EVP_PKEY *pk = NULL; | |
541 | size_t len; | |
542 | BIGNUM *pub = NULL, *priv = NULL; | |
543 | BIGNUM *pub_out = NULL, *priv_out = NULL; | |
544 | BIGNUM *p = NULL, *q = NULL, *g = NULL, *j = NULL; | |
545 | OSSL_PARAM_BLD *bld = NULL; | |
546 | OSSL_PARAM *fromdata_params = NULL; | |
547 | char name_out[80]; | |
548 | unsigned char seed_out[32]; | |
549 | ||
550 | /* | |
551 | * DH key data was generated using the following: | |
738ee181 SL |
552 | * openssl genpkey -algorithm DH |
553 | * -pkeyopt group:ffdhe2048 -pkeyopt priv_len:224 -text | |
7165593c SL |
554 | */ |
555 | static const unsigned char priv_data[] = { | |
556 | 0x88, 0x85, 0xe7, 0x9f, 0xee, 0x6d, 0xc5, 0x7c, 0x78, 0xaf, 0x63, 0x5d, | |
557 | 0x38, 0x2a, 0xd0, 0xed, 0x56, 0x4b, 0x47, 0x21, 0x2b, 0xfa, 0x55, 0xfa, | |
558 | 0x87, 0xe8, 0xa9, 0x7b, | |
559 | }; | |
560 | static const unsigned char pub_data[] = { | |
561 | 0xd6, 0x2d, 0x77, 0xe0, 0xd3, 0x7d, 0xf8, 0xeb, 0x98, 0x50, 0xa1, 0x82, | |
562 | 0x22, 0x65, 0xd5, 0xd9, 0xfe, 0xc9, 0x3f, 0xbe, 0x16, 0x83, 0xbd, 0x33, | |
563 | 0xe9, 0xc6, 0x93, 0xcf, 0x08, 0xaf, 0x83, 0xfa, 0x80, 0x8a, 0x6c, 0x64, | |
564 | 0xdf, 0x70, 0x64, 0xd5, 0x0a, 0x7c, 0x5a, 0x72, 0xda, 0x66, 0xe6, 0xf9, | |
565 | 0xf5, 0x31, 0x21, 0x92, 0xb0, 0x60, 0x1a, 0xb5, 0xd3, 0xf0, 0xa5, 0xfa, | |
566 | 0x48, 0x95, 0x2e, 0x38, 0xd9, 0xc5, 0xe6, 0xda, 0xfb, 0x6c, 0x03, 0x9d, | |
567 | 0x4b, 0x69, 0xb7, 0x95, 0xe4, 0x5c, 0xc0, 0x93, 0x4f, 0x48, 0xd9, 0x7e, | |
568 | 0x06, 0x22, 0xb2, 0xde, 0xf3, 0x79, 0x24, 0xed, 0xe1, 0xd1, 0x4a, 0x57, | |
569 | 0xf1, 0x40, 0x86, 0x70, 0x42, 0x25, 0xc5, 0x27, 0x68, 0xc9, 0xfa, 0xe5, | |
570 | 0x8e, 0x62, 0x7e, 0xff, 0x49, 0x6c, 0x5b, 0xb5, 0xba, 0xf9, 0xef, 0x9a, | |
571 | 0x1a, 0x10, 0xd4, 0x81, 0x53, 0xcf, 0x83, 0x04, 0x18, 0x1c, 0xe1, 0xdb, | |
572 | 0xe1, 0x65, 0xa9, 0x7f, 0xe1, 0x33, 0xeb, 0xc3, 0x4f, 0xe3, 0xb7, 0x22, | |
573 | 0xf7, 0x1c, 0x09, 0x4f, 0xed, 0xc6, 0x07, 0x8e, 0x78, 0x05, 0x8f, 0x7c, | |
574 | 0x96, 0xd9, 0x12, 0xe0, 0x81, 0x74, 0x1a, 0xe9, 0x13, 0xc0, 0x20, 0x82, | |
575 | 0x65, 0xbb, 0x42, 0x3b, 0xed, 0x08, 0x6a, 0x84, 0x4f, 0xea, 0x77, 0x14, | |
576 | 0x32, 0xf9, 0xed, 0xc2, 0x12, 0xd6, 0xc5, 0xc6, 0xb3, 0xe5, 0xf2, 0x6e, | |
577 | 0xf6, 0x16, 0x7f, 0x37, 0xde, 0xbc, 0x09, 0xc7, 0x06, 0x6b, 0x12, 0xbc, | |
578 | 0xad, 0x2d, 0x49, 0x25, 0xd5, 0xdc, 0xf4, 0x18, 0x14, 0xd2, 0xf0, 0xf1, | |
579 | 0x1d, 0x1f, 0x3a, 0xaa, 0x15, 0x55, 0xbb, 0x0d, 0x7f, 0xbe, 0x67, 0xa1, | |
580 | 0xa7, 0xf0, 0xaa, 0xb3, 0xfb, 0x41, 0x82, 0x39, 0x49, 0x93, 0xbc, 0xa8, | |
581 | 0xee, 0x72, 0x13, 0x45, 0x65, 0x15, 0x42, 0x17, 0xaa, 0xd8, 0xab, 0xcf, | |
582 | 0x33, 0x42, 0x83, 0x42 | |
583 | }; | |
584 | static const char group_name[] = "ffdhe2048"; | |
585 | ||
586 | ||
587 | if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) | |
588 | || !TEST_ptr(pub = BN_bin2bn(pub_data, sizeof(pub_data), NULL)) | |
589 | || !TEST_ptr(priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL)) | |
590 | || !TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, | |
023b188c | 591 | OSSL_PKEY_PARAM_GROUP_NAME, |
7165593c SL |
592 | group_name, 0)) |
593 | || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, pub)) | |
594 | || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, priv)) | |
595 | || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld))) | |
596 | goto err; | |
597 | ||
598 | if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DH", NULL))) | |
599 | goto err; | |
600 | ||
601 | if (!TEST_true(EVP_PKEY_key_fromdata_init(ctx)) | |
602 | || !TEST_true(EVP_PKEY_fromdata(ctx, &pk, fromdata_params)) | |
603 | || !TEST_int_eq(EVP_PKEY_bits(pk), 2048) | |
604 | || !TEST_int_eq(EVP_PKEY_security_bits(pk), 112) | |
605 | || !TEST_int_eq(EVP_PKEY_size(pk), 256)) | |
606 | goto err; | |
607 | ||
023b188c | 608 | if (!TEST_true(EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME, |
7165593c SL |
609 | name_out, sizeof(name_out), |
610 | &len)) | |
611 | || !TEST_str_eq(name_out, group_name) | |
612 | || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PUB_KEY, | |
613 | &pub_out)) | |
614 | || !TEST_BN_eq(pub, pub_out) | |
615 | || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY, | |
616 | &priv_out)) | |
617 | || !TEST_BN_eq(priv, priv_out) | |
618 | || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_P, &p)) | |
619 | || !TEST_BN_eq(&_bignum_ffdhe2048_p, p) | |
620 | || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_Q, &q)) | |
621 | || !TEST_ptr(q) | |
622 | || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_G, &g)) | |
623 | || !TEST_BN_eq(&_bignum_const_2, g) | |
624 | || !TEST_false(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_COFACTOR, | |
625 | &j)) | |
626 | || !TEST_ptr_null(j) | |
627 | || !TEST_false(EVP_PKEY_get_octet_string_param(pk, | |
628 | OSSL_PKEY_PARAM_FFC_SEED, | |
629 | seed_out, | |
630 | sizeof(seed_out), &len)) | |
631 | || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_GINDEX, | |
632 | &gindex)) | |
633 | || !TEST_int_eq(gindex, -1) | |
634 | || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_H, &hindex)) | |
635 | || !TEST_int_eq(hindex, 0) | |
636 | || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_PCOUNTER, | |
637 | &pcounter)) | |
638 | || !TEST_int_eq(pcounter, -1)) | |
639 | goto err; | |
640 | ||
a54ff473 SL |
641 | if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, ""))) |
642 | goto err; | |
643 | ||
7165593c | 644 | if (!TEST_true(EVP_PKEY_check(key_ctx)) |
a54ff473 | 645 | || !TEST_true(EVP_PKEY_public_check(key_ctx)) |
7165593c SL |
646 | || !TEST_true(EVP_PKEY_private_check(key_ctx)) |
647 | || !TEST_true(EVP_PKEY_pairwise_check(key_ctx))) | |
a54ff473 SL |
648 | goto err; |
649 | ||
b03ec3b5 SL |
650 | ret = test_print_key_using_pem("DH", pk) |
651 | && test_print_key_using_serializer("DH", pk); | |
7165593c SL |
652 | err: |
653 | BN_free(p); | |
654 | BN_free(q); | |
655 | BN_free(g); | |
656 | BN_free(j); | |
657 | BN_free(pub); | |
658 | BN_free(priv); | |
659 | BN_free(pub_out); | |
660 | BN_free(priv_out); | |
8efc4a9c MC |
661 | EVP_PKEY_free(pk); |
662 | EVP_PKEY_CTX_free(ctx); | |
a54ff473 | 663 | EVP_PKEY_CTX_free(key_ctx); |
7165593c SL |
664 | OSSL_PARAM_BLD_free_params(fromdata_params); |
665 | OSSL_PARAM_BLD_free(bld); | |
8efc4a9c MC |
666 | |
667 | return ret; | |
668 | } | |
7165593c | 669 | |
8efc4a9c MC |
670 | #endif |
671 | ||
7165593c SL |
672 | |
673 | ||
8efc4a9c MC |
674 | #ifndef OPENSSL_NO_EC |
675 | /* Array indexes used in test_fromdata_ecx */ | |
676 | # define PRIV_KEY 0 | |
677 | # define PUB_KEY 1 | |
678 | ||
679 | # define X25519_IDX 0 | |
680 | # define X448_IDX 1 | |
244bc297 MC |
681 | # define ED25519_IDX 2 |
682 | # define ED448_IDX 3 | |
8efc4a9c MC |
683 | |
684 | static int test_fromdata_ecx(int tst) | |
685 | { | |
686 | int ret = 0; | |
687 | EVP_PKEY_CTX *ctx = NULL; | |
ff7262b4 | 688 | EVP_PKEY *pk = NULL, *copy_pk = NULL; |
244bc297 | 689 | const char *alg = NULL; |
96ebe52e SL |
690 | size_t len; |
691 | unsigned char out_pub[ED448_KEYLEN]; | |
692 | unsigned char out_priv[ED448_KEYLEN]; | |
8efc4a9c | 693 | |
244bc297 MC |
694 | /* ED448_KEYLEN > X448_KEYLEN > X25519_KEYLEN == ED25519_KEYLEN */ |
695 | static unsigned char key_numbers[4][2][ED448_KEYLEN] = { | |
8efc4a9c MC |
696 | /* X25519: Keys from RFC 7748 6.1 */ |
697 | { | |
698 | /* Private Key */ | |
699 | { | |
700 | 0x77, 0x07, 0x6d, 0x0a, 0x73, 0x18, 0xa5, 0x7d, 0x3c, 0x16, | |
701 | 0xc1, 0x72, 0x51, 0xb2, 0x66, 0x45, 0xdf, 0x4c, 0x2f, 0x87, | |
702 | 0xeb, 0xc0, 0x99, 0x2a, 0xb1, 0x77, 0xfb, 0xa5, 0x1d, 0xb9, | |
703 | 0x2c, 0x2a | |
704 | }, | |
705 | /* Public Key */ | |
706 | { | |
707 | 0x85, 0x20, 0xf0, 0x09, 0x89, 0x30, 0xa7, 0x54, 0x74, 0x8b, | |
708 | 0x7d, 0xdc, 0xb4, 0x3e, 0xf7, 0x5a, 0x0d, 0xbf, 0x3a, 0x0d, | |
709 | 0x26, 0x38, 0x1a, 0xf4, 0xeb, 0xa4, 0xa9, 0x8e, 0xaa, 0x9b, | |
710 | 0x4e, 0x6a | |
711 | } | |
712 | }, | |
713 | /* X448: Keys from RFC 7748 6.2 */ | |
714 | { | |
715 | /* Private Key */ | |
716 | { | |
717 | 0x9a, 0x8f, 0x49, 0x25, 0xd1, 0x51, 0x9f, 0x57, 0x75, 0xcf, | |
718 | 0x46, 0xb0, 0x4b, 0x58, 0x00, 0xd4, 0xee, 0x9e, 0xe8, 0xba, | |
719 | 0xe8, 0xbc, 0x55, 0x65, 0xd4, 0x98, 0xc2, 0x8d, 0xd9, 0xc9, | |
720 | 0xba, 0xf5, 0x74, 0xa9, 0x41, 0x97, 0x44, 0x89, 0x73, 0x91, | |
721 | 0x00, 0x63, 0x82, 0xa6, 0xf1, 0x27, 0xab, 0x1d, 0x9a, 0xc2, | |
722 | 0xd8, 0xc0, 0xa5, 0x98, 0x72, 0x6b | |
723 | }, | |
724 | /* Public Key */ | |
725 | { | |
726 | 0x9b, 0x08, 0xf7, 0xcc, 0x31, 0xb7, 0xe3, 0xe6, 0x7d, 0x22, | |
727 | 0xd5, 0xae, 0xa1, 0x21, 0x07, 0x4a, 0x27, 0x3b, 0xd2, 0xb8, | |
728 | 0x3d, 0xe0, 0x9c, 0x63, 0xfa, 0xa7, 0x3d, 0x2c, 0x22, 0xc5, | |
729 | 0xd9, 0xbb, 0xc8, 0x36, 0x64, 0x72, 0x41, 0xd9, 0x53, 0xd4, | |
730 | 0x0c, 0x5b, 0x12, 0xda, 0x88, 0x12, 0x0d, 0x53, 0x17, 0x7f, | |
731 | 0x80, 0xe5, 0x32, 0xc4, 0x1f, 0xa0 | |
732 | } | |
244bc297 MC |
733 | }, |
734 | /* ED25519: Keys from RFC 8032 */ | |
735 | { | |
736 | /* Private Key */ | |
737 | { | |
738 | 0x9d, 0x61, 0xb1, 0x9d, 0xef, 0xfd, 0x5a, 0x60, 0xba, 0x84, | |
739 | 0x4a, 0xf4, 0x92, 0xec, 0x2c, 0xc4, 0x44, 0x49, 0xc5, 0x69, | |
740 | 0x7b, 0x32, 0x69, 0x19, 0x70, 0x3b, 0xac, 0x03, 0x1c, 0xae, | |
741 | 0x7f, 0x60 | |
742 | }, | |
743 | /* Public Key */ | |
744 | { | |
745 | 0xd7, 0x5a, 0x98, 0x01, 0x82, 0xb1, 0x0a, 0xb7, 0xd5, 0x4b, | |
746 | 0xfe, 0xd3, 0xc9, 0x64, 0x07, 0x3a, 0x0e, 0xe1, 0x72, 0xf3, | |
747 | 0xda, 0xa6, 0x23, 0x25, 0xaf, 0x02, 0x1a, 0x68, 0xf7, 0x07, | |
748 | 0x51, 0x1a | |
749 | } | |
750 | }, | |
751 | /* ED448: Keys from RFC 8032 */ | |
752 | { | |
753 | /* Private Key */ | |
754 | { | |
755 | 0x6c, 0x82, 0xa5, 0x62, 0xcb, 0x80, 0x8d, 0x10, 0xd6, 0x32, | |
756 | 0xbe, 0x89, 0xc8, 0x51, 0x3e, 0xbf, 0x6c, 0x92, 0x9f, 0x34, | |
757 | 0xdd, 0xfa, 0x8c, 0x9f, 0x63, 0xc9, 0x96, 0x0e, 0xf6, 0xe3, | |
758 | 0x48, 0xa3, 0x52, 0x8c, 0x8a, 0x3f, 0xcc, 0x2f, 0x04, 0x4e, | |
759 | 0x39, 0xa3, 0xfc, 0x5b, 0x94, 0x49, 0x2f, 0x8f, 0x03, 0x2e, | |
760 | 0x75, 0x49, 0xa2, 0x00, 0x98, 0xf9, 0x5b | |
761 | }, | |
762 | /* Public Key */ | |
763 | { | |
764 | 0x5f, 0xd7, 0x44, 0x9b, 0x59, 0xb4, 0x61, 0xfd, 0x2c, 0xe7, | |
765 | 0x87, 0xec, 0x61, 0x6a, 0xd4, 0x6a, 0x1d, 0xa1, 0x34, 0x24, | |
766 | 0x85, 0xa7, 0x0e, 0x1f, 0x8a, 0x0e, 0xa7, 0x5d, 0x80, 0xe9, | |
767 | 0x67, 0x78, 0xed, 0xf1, 0x24, 0x76, 0x9b, 0x46, 0xc7, 0x06, | |
768 | 0x1b, 0xd6, 0x78, 0x3d, 0xf1, 0xe5, 0x0f, 0x6c, 0xd1, 0xfa, | |
769 | 0x1a, 0xbe, 0xaf, 0xe8, 0x25, 0x61, 0x80 | |
770 | } | |
8efc4a9c MC |
771 | } |
772 | }; | |
773 | OSSL_PARAM x25519_fromdata_params[] = { | |
774 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, | |
775 | key_numbers[X25519_IDX][PRIV_KEY], | |
776 | X25519_KEYLEN), | |
777 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, | |
778 | key_numbers[X25519_IDX][PUB_KEY], | |
779 | X25519_KEYLEN), | |
780 | OSSL_PARAM_END | |
781 | }; | |
782 | OSSL_PARAM x448_fromdata_params[] = { | |
783 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, | |
784 | key_numbers[X448_IDX][PRIV_KEY], | |
785 | X448_KEYLEN), | |
786 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, | |
787 | key_numbers[X448_IDX][PUB_KEY], | |
788 | X448_KEYLEN), | |
789 | OSSL_PARAM_END | |
790 | }; | |
244bc297 MC |
791 | OSSL_PARAM ed25519_fromdata_params[] = { |
792 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, | |
793 | key_numbers[ED25519_IDX][PRIV_KEY], | |
794 | ED25519_KEYLEN), | |
795 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, | |
796 | key_numbers[ED25519_IDX][PUB_KEY], | |
797 | ED25519_KEYLEN), | |
798 | OSSL_PARAM_END | |
799 | }; | |
800 | OSSL_PARAM ed448_fromdata_params[] = { | |
801 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PRIV_KEY, | |
802 | key_numbers[ED448_IDX][PRIV_KEY], | |
803 | ED448_KEYLEN), | |
804 | OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_PUB_KEY, | |
805 | key_numbers[ED448_IDX][PUB_KEY], | |
806 | ED448_KEYLEN), | |
807 | OSSL_PARAM_END | |
808 | }; | |
809 | OSSL_PARAM *fromdata_params = NULL; | |
810 | int bits = 0, security_bits = 0, size = 0; | |
8efc4a9c | 811 | |
244bc297 MC |
812 | switch (tst) { |
813 | case X25519_IDX: | |
8efc4a9c MC |
814 | fromdata_params = x25519_fromdata_params; |
815 | bits = X25519_BITS; | |
816 | security_bits = X25519_SECURITY_BITS; | |
817 | size = X25519_KEYLEN; | |
244bc297 MC |
818 | alg = "X25519"; |
819 | break; | |
820 | ||
821 | case X448_IDX: | |
8efc4a9c MC |
822 | fromdata_params = x448_fromdata_params; |
823 | bits = X448_BITS; | |
824 | security_bits = X448_SECURITY_BITS; | |
825 | size = X448_KEYLEN; | |
244bc297 MC |
826 | alg = "X448"; |
827 | break; | |
828 | ||
829 | case ED25519_IDX: | |
830 | fromdata_params = ed25519_fromdata_params; | |
831 | bits = ED25519_BITS; | |
832 | security_bits = ED25519_SECURITY_BITS; | |
833 | size = ED25519_KEYLEN; | |
834 | alg = "ED25519"; | |
835 | break; | |
836 | ||
837 | case ED448_IDX: | |
838 | fromdata_params = ed448_fromdata_params; | |
839 | bits = ED448_BITS; | |
840 | security_bits = ED448_SECURITY_BITS; | |
841 | size = ED448_KEYLEN; | |
842 | alg = "ED448"; | |
843 | break; | |
fd7d574d SL |
844 | default: |
845 | goto err; | |
8efc4a9c MC |
846 | } |
847 | ||
848 | ctx = EVP_PKEY_CTX_new_from_name(NULL, alg, NULL); | |
849 | if (!TEST_ptr(ctx)) | |
850 | goto err; | |
851 | ||
852 | if (!TEST_true(EVP_PKEY_key_fromdata_init(ctx)) | |
853 | || !TEST_true(EVP_PKEY_fromdata(ctx, &pk, fromdata_params)) | |
854 | || !TEST_int_eq(EVP_PKEY_bits(pk), bits) | |
855 | || !TEST_int_eq(EVP_PKEY_security_bits(pk), security_bits) | |
856 | || !TEST_int_eq(EVP_PKEY_size(pk), size)) | |
857 | goto err; | |
858 | ||
ff7262b4 | 859 | if (!TEST_ptr(copy_pk = EVP_PKEY_new()) |
7fa2b267 MC |
860 | /* This should succeed because there are no parameters to copy */ |
861 | || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk))) | |
ff7262b4 RL |
862 | goto err; |
863 | ||
96ebe52e SL |
864 | if (!TEST_true(EVP_PKEY_get_octet_string_param( |
865 | pk, fromdata_params[PRIV_KEY].key, | |
866 | out_priv, sizeof(out_priv), &len)) | |
867 | || !TEST_mem_eq(out_priv, len, | |
868 | fromdata_params[PRIV_KEY].data, | |
869 | fromdata_params[PRIV_KEY].data_size) | |
870 | || !TEST_true(EVP_PKEY_get_octet_string_param( | |
871 | pk, fromdata_params[PUB_KEY].key, | |
872 | out_pub, sizeof(out_pub), &len)) | |
873 | || !TEST_mem_eq(out_pub, len, | |
874 | fromdata_params[PUB_KEY].data, | |
875 | fromdata_params[PUB_KEY].data_size)) | |
876 | goto err; | |
877 | ||
8efc4a9c MC |
878 | ret = test_print_key_using_pem(alg, pk) |
879 | && test_print_key_using_serializer(alg, pk); | |
6ae5543c | 880 | |
f552d900 | 881 | err: |
6ae5543c | 882 | EVP_PKEY_free(pk); |
ff7262b4 | 883 | EVP_PKEY_free(copy_pk); |
6ae5543c RL |
884 | EVP_PKEY_CTX_free(ctx); |
885 | ||
886 | return ret; | |
887 | } | |
f552d900 | 888 | |
96ebe52e SL |
889 | #define CURVE_NAME 2 |
890 | ||
f552d900 SL |
891 | static int test_fromdata_ec(void) |
892 | { | |
893 | int ret = 0; | |
894 | EVP_PKEY_CTX *ctx = NULL; | |
ff7262b4 | 895 | EVP_PKEY *pk = NULL, *copy_pk = NULL; |
7165593c | 896 | OSSL_PARAM_BLD *bld = NULL; |
f552d900 | 897 | BIGNUM *ec_priv_bn = NULL; |
96ebe52e | 898 | BIGNUM *bn_priv = NULL; |
f552d900 SL |
899 | OSSL_PARAM *fromdata_params = NULL; |
900 | const char *alg = "EC"; | |
96ebe52e SL |
901 | const char *curve = "prime256v1"; |
902 | /* UNCOMPRESSED FORMAT */ | |
f552d900 | 903 | static const unsigned char ec_pub_keydata[] = { |
96ebe52e | 904 | POINT_CONVERSION_UNCOMPRESSED, |
f552d900 SL |
905 | 0x1b, 0x93, 0x67, 0x55, 0x1c, 0x55, 0x9f, 0x63, |
906 | 0xd1, 0x22, 0xa4, 0xd8, 0xd1, 0x0a, 0x60, 0x6d, | |
907 | 0x02, 0xa5, 0x77, 0x57, 0xc8, 0xa3, 0x47, 0x73, | |
908 | 0x3a, 0x6a, 0x08, 0x28, 0x39, 0xbd, 0xc9, 0xd2, | |
909 | 0x80, 0xec, 0xe9, 0xa7, 0x08, 0x29, 0x71, 0x2f, | |
910 | 0xc9, 0x56, 0x82, 0xee, 0x9a, 0x85, 0x0f, 0x6d, | |
911 | 0x7f, 0x59, 0x5f, 0x8c, 0xd1, 0x96, 0x0b, 0xdf, | |
912 | 0x29, 0x3e, 0x49, 0x07, 0x88, 0x3f, 0x9a, 0x29 | |
913 | }; | |
914 | static const unsigned char ec_priv_keydata[] = { | |
915 | 0x33, 0xd0, 0x43, 0x83, 0xa9, 0x89, 0x56, 0x03, | |
916 | 0xd2, 0xd7, 0xfe, 0x6b, 0x01, 0x6f, 0xe4, 0x59, | |
917 | 0xcc, 0x0d, 0x9a, 0x24, 0x6c, 0x86, 0x1b, 0x2e, | |
918 | 0xdc, 0x4b, 0x4d, 0x35, 0x43, 0xe1, 0x1b, 0xad | |
919 | }; | |
96ebe52e SL |
920 | const int compressed_sz = 1 + (sizeof(ec_pub_keydata) - 1) / 2; |
921 | unsigned char out_pub[sizeof(ec_pub_keydata)]; | |
922 | char out_curve_name[80]; | |
923 | const OSSL_PARAM *gettable = NULL; | |
924 | size_t len; | |
925 | ||
f552d900 | 926 | |
7165593c | 927 | if (!TEST_ptr(bld = OSSL_PARAM_BLD_new())) |
6d4e6009 | 928 | goto err; |
f552d900 SL |
929 | if (!TEST_ptr(ec_priv_bn = BN_bin2bn(ec_priv_keydata, |
930 | sizeof(ec_priv_keydata), NULL))) | |
931 | goto err; | |
932 | ||
11a1b341 | 933 | if (OSSL_PARAM_BLD_push_utf8_string(bld, OSSL_PKEY_PARAM_GROUP_NAME, |
96ebe52e | 934 | curve, 0) <= 0) |
f552d900 | 935 | goto err; |
6d4e6009 | 936 | if (OSSL_PARAM_BLD_push_octet_string(bld, OSSL_PKEY_PARAM_PUB_KEY, |
f552d900 SL |
937 | ec_pub_keydata, |
938 | sizeof(ec_pub_keydata)) <= 0) | |
939 | goto err; | |
6d4e6009 | 940 | if (OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, ec_priv_bn) <= 0) |
f552d900 | 941 | goto err; |
6d4e6009 | 942 | if (!TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld))) |
f552d900 SL |
943 | goto err; |
944 | ctx = EVP_PKEY_CTX_new_from_name(NULL, alg, NULL); | |
945 | if (!TEST_ptr(ctx)) | |
946 | goto err; | |
947 | ||
948 | if (!TEST_true(EVP_PKEY_key_fromdata_init(ctx)) | |
949 | || !TEST_true(EVP_PKEY_fromdata(ctx, &pk, fromdata_params)) | |
950 | || !TEST_int_eq(EVP_PKEY_bits(pk), 256) | |
951 | || !TEST_int_eq(EVP_PKEY_security_bits(pk), 128) | |
952 | || !TEST_int_eq(EVP_PKEY_size(pk), 2 + 35 * 2)) | |
953 | goto err; | |
954 | ||
ff7262b4 RL |
955 | if (!TEST_ptr(copy_pk = EVP_PKEY_new()) |
956 | || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk))) | |
957 | goto err; | |
958 | ||
96ebe52e | 959 | if (!TEST_ptr(gettable = EVP_PKEY_gettable_params(pk)) |
11a1b341 | 960 | || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_GROUP_NAME)) |
96ebe52e SL |
961 | || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_PUB_KEY)) |
962 | || !TEST_ptr(OSSL_PARAM_locate_const(gettable, OSSL_PKEY_PARAM_PRIV_KEY))) | |
963 | goto err; | |
964 | ||
11a1b341 | 965 | if (!EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME, |
96ebe52e SL |
966 | out_curve_name, sizeof(out_curve_name), |
967 | &len) | |
968 | || !TEST_str_eq(out_curve_name, curve) | |
969 | || !EVP_PKEY_get_octet_string_param(pk, OSSL_PKEY_PARAM_PUB_KEY, | |
970 | out_pub, sizeof(out_pub), &len) | |
971 | || !TEST_true(out_pub[0] == (POINT_CONVERSION_COMPRESSED + 1)) | |
972 | || !TEST_mem_eq(out_pub + 1, len - 1, | |
973 | ec_pub_keydata + 1, compressed_sz - 1) | |
974 | || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY, | |
975 | &bn_priv)) | |
976 | || !TEST_BN_eq(ec_priv_bn, bn_priv)) | |
977 | goto err; | |
978 | ||
f552d900 SL |
979 | ret = test_print_key_using_pem(alg, pk) |
980 | && test_print_key_using_serializer(alg, pk); | |
981 | err: | |
96ebe52e | 982 | BN_free(bn_priv); |
f552d900 | 983 | BN_free(ec_priv_bn); |
6d4e6009 P |
984 | OSSL_PARAM_BLD_free_params(fromdata_params); |
985 | OSSL_PARAM_BLD_free(bld); | |
f552d900 | 986 | EVP_PKEY_free(pk); |
ff7262b4 | 987 | EVP_PKEY_free(copy_pk); |
f552d900 SL |
988 | EVP_PKEY_CTX_free(ctx); |
989 | return ret; | |
990 | } | |
991 | ||
992 | #endif /* OPENSSL_NO_EC */ | |
6ae5543c | 993 | |
b03ec3b5 SL |
994 | #ifndef OPENSSL_NO_DSA |
995 | static int test_fromdata_dsa_fips186_4(void) | |
996 | { | |
997 | int ret = 0; | |
998 | EVP_PKEY_CTX *ctx = NULL, *key_ctx = NULL; | |
999 | EVP_PKEY *pk = NULL, *copy_pk = NULL; | |
1000 | BIGNUM *pub = NULL, *priv = NULL; | |
1001 | BIGNUM *p = NULL, *q = NULL, *g = NULL; | |
1002 | BIGNUM *pub_out = NULL, *priv_out = NULL; | |
1003 | BIGNUM *p_out = NULL, *q_out = NULL, *g_out = NULL, *j_out = NULL; | |
1004 | int gindex_out = 0, pcounter_out = 0, hindex_out = 0; | |
1005 | char name_out[80]; | |
1006 | unsigned char seed_out[32]; | |
1007 | size_t len; | |
1008 | OSSL_PARAM_BLD *bld = NULL; | |
1009 | OSSL_PARAM *fromdata_params = NULL; | |
1010 | ||
1011 | /* | |
1012 | * DSA parameter data was generated using the following: | |
1013 | * openssl genpkey -genparam -algorithm DSA -pkeyopt pbits:2048 \ | |
1014 | * -pkeyopt qbits:256 -pkeyopt type:0 \ | |
1015 | * -pkeyopt gindex:1 -out dsa_params.pem -text | |
1016 | */ | |
1017 | static const unsigned char p_data[] = { | |
1018 | 0x00, 0xa0, 0xb7, 0x02, 0xc4, 0xac, 0xa6, 0x42, 0xab, 0xf2, 0x34, 0x0b, | |
1019 | 0x22, 0x47, 0x1f, 0x33, 0xcf, 0xd5, 0x04, 0xe4, 0x3e, 0xec, 0xa1, 0x21, | |
1020 | 0xc8, 0x41, 0x2b, 0xef, 0xb8, 0x1f, 0x0b, 0x5b, 0x88, 0x8b, 0x67, 0xf8, | |
1021 | 0x68, 0x6d, 0x7c, 0x4d, 0x96, 0x5f, 0x3c, 0x66, 0xef, 0x58, 0x34, 0xd7, | |
1022 | 0xf6, 0xa2, 0x1b, 0xad, 0xc8, 0x12, 0x52, 0xb8, 0xe8, 0x2a, 0x63, 0xcc, | |
1023 | 0xea, 0xe7, 0x4e, 0xc8, 0x34, 0x4c, 0x58, 0x59, 0x0a, 0xc2, 0x4a, 0xe4, | |
1024 | 0xb4, 0x64, 0x20, 0xf4, 0xf6, 0x0a, 0xcf, 0x86, 0x01, 0x6c, 0x7f, 0x23, | |
1025 | 0x4a, 0x51, 0x07, 0x99, 0x42, 0x28, 0x7a, 0xff, 0x18, 0x67, 0x52, 0x64, | |
1026 | 0xf2, 0x9a, 0x62, 0x30, 0xc3, 0x00, 0xde, 0x23, 0xe9, 0x11, 0x95, 0x7e, | |
1027 | 0xd1, 0x3d, 0x8d, 0xb4, 0x0e, 0x9f, 0x9e, 0xb1, 0x30, 0x03, 0xf0, 0x73, | |
1028 | 0xa8, 0x40, 0x48, 0x42, 0x7b, 0x60, 0xa0, 0xc4, 0xf2, 0x3b, 0x2d, 0x0a, | |
1029 | 0x0c, 0xb8, 0x19, 0xfb, 0xb4, 0xf8, 0xe0, 0x2a, 0xc7, 0xf1, 0xc0, 0xc6, | |
1030 | 0x86, 0x14, 0x60, 0x12, 0x0f, 0xc0, 0xde, 0x4a, 0x67, 0xec, 0xc7, 0xde, | |
1031 | 0x76, 0x21, 0x1a, 0x55, 0x7f, 0x86, 0xc3, 0x97, 0x98, 0xce, 0xf5, 0xcd, | |
1032 | 0xf0, 0xe7, 0x12, 0xd6, 0x93, 0xee, 0x1b, 0x9b, 0x61, 0xef, 0x05, 0x8c, | |
1033 | 0x45, 0x46, 0xd9, 0x64, 0x6f, 0xbe, 0x27, 0xaa, 0x67, 0x01, 0xcc, 0x71, | |
1034 | 0xb1, 0x60, 0xce, 0x21, 0xd8, 0x51, 0x17, 0x27, 0x0d, 0x90, 0x3d, 0x18, | |
1035 | 0x7c, 0x87, 0x15, 0x8e, 0x48, 0x4c, 0x6c, 0xc5, 0x72, 0xeb, 0xb7, 0x56, | |
1036 | 0xf5, 0x6b, 0x60, 0x8f, 0xc2, 0xfd, 0x3f, 0x46, 0x5c, 0x00, 0x91, 0x85, | |
1037 | 0x79, 0x45, 0x5b, 0x1c, 0x82, 0xc4, 0x87, 0x50, 0x79, 0xba, 0xcc, 0x1c, | |
1038 | 0x32, 0x7e, 0x2e, 0xb8, 0x2e, 0xc5, 0x4e, 0xd1, 0x9b, 0xdb, 0x66, 0x79, | |
1039 | 0x7c, 0xfe, 0xaf, 0x6a, 0x05 | |
1040 | }; | |
1041 | static const unsigned char q_data[] = { | |
1042 | 0xa8, 0xcd, 0xf4, 0x33, 0x7b, 0x13, 0x0a, 0x24, 0xc1, 0xde, 0x4a, 0x04, | |
1043 | 0x7b, 0x4b, 0x71, 0x51, 0x32, 0xe9, 0x47, 0x74, 0xbd, 0x0c, 0x21, 0x40, | |
1044 | 0x84, 0x12, 0x0a, 0x17, 0x73, 0xdb, 0x29, 0xc7 | |
1045 | }; | |
1046 | static const unsigned char g_data[] = { | |
1047 | 0x6c, 0xc6, 0xa4, 0x3e, 0x61, 0x84, 0xc1, 0xff, 0x6f, 0x4a, 0x1a, 0x6b, | |
1048 | 0xb0, 0x24, 0x4b, 0xd2, 0x92, 0x5b, 0x29, 0x5c, 0x61, 0xb8, 0xc9, 0x2b, | |
1049 | 0xd6, 0xf7, 0x59, 0xfd, 0xd8, 0x70, 0x66, 0x77, 0xfc, 0xc1, 0xa4, 0xd4, | |
1050 | 0xb0, 0x1e, 0xd5, 0xbf, 0x59, 0x98, 0xb3, 0x66, 0x8b, 0xf4, 0x2e, 0xe6, | |
1051 | 0x12, 0x3e, 0xcc, 0xf8, 0x02, 0xb8, 0xc6, 0xc3, 0x47, 0xd2, 0xf5, 0xaa, | |
1052 | 0x0c, 0x5f, 0x51, 0xf5, 0xd0, 0x4c, 0x55, 0x3d, 0x07, 0x73, 0xa6, 0x57, | |
1053 | 0xce, 0x5a, 0xad, 0x42, 0x0c, 0x13, 0x0f, 0xe2, 0x31, 0x25, 0x8e, 0x72, | |
1054 | 0x12, 0x73, 0x10, 0xdb, 0x7f, 0x79, 0xeb, 0x59, 0xfc, 0xfe, 0xf7, 0x0c, | |
1055 | 0x1a, 0x81, 0x53, 0x96, 0x22, 0xb8, 0xe7, 0x58, 0xd8, 0x67, 0x80, 0x60, | |
1056 | 0xad, 0x8b, 0x55, 0x1c, 0x91, 0xf0, 0x72, 0x9a, 0x7e, 0xad, 0x37, 0xf1, | |
1057 | 0x77, 0x18, 0x96, 0x8a, 0x68, 0x70, 0xfc, 0x71, 0xa9, 0xa2, 0xe8, 0x35, | |
1058 | 0x27, 0x78, 0xf2, 0xef, 0x59, 0x36, 0x6d, 0x7c, 0xb6, 0x98, 0xd8, 0x1e, | |
1059 | 0xfa, 0x25, 0x73, 0x97, 0x45, 0x58, 0xe3, 0xae, 0xbd, 0x52, 0x54, 0x05, | |
1060 | 0xd8, 0x26, 0x26, 0xba, 0xba, 0x05, 0xb5, 0xe9, 0xe5, 0x76, 0xae, 0x25, | |
1061 | 0xdd, 0xfc, 0x10, 0x89, 0x5a, 0xa9, 0xee, 0x59, 0xc5, 0x79, 0x8b, 0xeb, | |
1062 | 0x1e, 0x2c, 0x61, 0xab, 0x0d, 0xd1, 0x10, 0x04, 0x91, 0x32, 0x77, 0x4a, | |
1063 | 0xa6, 0x64, 0x53, 0xda, 0x4c, 0xd7, 0x3a, 0x29, 0xd4, 0xf3, 0x82, 0x25, | |
1064 | 0x1d, 0x6f, 0x4a, 0x7f, 0xd3, 0x08, 0x3b, 0x42, 0x30, 0x10, 0xd8, 0xd0, | |
1065 | 0x97, 0x3a, 0xeb, 0x92, 0x63, 0xec, 0x93, 0x2b, 0x6f, 0x32, 0xd8, 0xcd, | |
1066 | 0x80, 0xd3, 0xc0, 0x4c, 0x03, 0xd5, 0xca, 0xbc, 0x8f, 0xc7, 0x43, 0x53, | |
1067 | 0x64, 0x66, 0x1c, 0x82, 0x2d, 0xfb, 0xff, 0x39, 0xba, 0xd6, 0x42, 0x62, | |
1068 | 0x02, 0x6f, 0x96, 0x36 | |
1069 | }; | |
1070 | static const unsigned char seed_data[] = { | |
1071 | 0x64, 0x46, 0x07, 0x32, 0x8d, 0x70, 0x9c, 0xb3, 0x8a, 0x35, 0xde, 0x62, | |
1072 | 0x00, 0xf2, 0x6d, 0x52, 0x37, 0x4d, 0xb3, 0x84, 0xe1, 0x9d, 0x41, 0x04, | |
1073 | 0xda, 0x7b, 0xdc, 0x0d, 0x8b, 0x5e, 0xe0, 0x84 | |
1074 | }; | |
1075 | const int gindex = 1; | |
1076 | const int pcounter = 53; | |
1077 | /* | |
1078 | * The keypair was generated using | |
1079 | * openssl genpkey -paramfile dsa_params.pem --pkeyopt pcounter:53 \ | |
1080 | * -pkeyopt gindex:1 \ | |
1081 | * -pkeyopt hexseed:644607328d709cb38a35de6200f26d -text | |
1082 | */ | |
1083 | static const unsigned char priv_data[] = { | |
1084 | 0x00, 0x8f, 0xc5, 0x9e, 0xd0, 0xf7, 0x2a, 0x0b, 0x66, 0xf1, 0x32, 0x73, | |
1085 | 0xae, 0xf6, 0xd9, 0xd4, 0xdb, 0x2d, 0x96, 0x55, 0x89, 0xff, 0xef, 0xa8, | |
1086 | 0x5f, 0x47, 0x8f, 0xca, 0x02, 0x8a, 0xe1, 0x35, 0x90 | |
1087 | }; | |
1088 | static const unsigned char pub_data[] = { | |
1089 | 0x44, 0x19, 0xc9, 0x46, 0x45, 0x57, 0xc1, 0xa9, 0xd8, 0x30, 0x99, 0x29, | |
1090 | 0x6a, 0x4b, 0x63, 0x71, 0x69, 0x96, 0x35, 0x17, 0xb2, 0x62, 0x9b, 0x80, | |
1091 | 0x0a, 0x95, 0x9d, 0x6a, 0xc0, 0x32, 0x0d, 0x07, 0x5f, 0x19, 0x44, 0x02, | |
1092 | 0xf1, 0xbd, 0xce, 0xdf, 0x10, 0xf8, 0x02, 0x5d, 0x7d, 0x98, 0x8a, 0x73, | |
1093 | 0x89, 0x00, 0xb6, 0x24, 0xd6, 0x33, 0xe7, 0xcf, 0x8b, 0x49, 0x2a, 0xaf, | |
1094 | 0x13, 0x1c, 0xb2, 0x52, 0x15, 0xfd, 0x9b, 0xd5, 0x40, 0x4a, 0x1a, 0xda, | |
1095 | 0x29, 0x4c, 0x92, 0x7e, 0x66, 0x06, 0xdb, 0x61, 0x86, 0xac, 0xb5, 0xda, | |
1096 | 0x3c, 0x7d, 0x73, 0x7e, 0x54, 0x32, 0x68, 0xa5, 0x02, 0xbc, 0x59, 0x47, | |
1097 | 0x84, 0xd3, 0x87, 0x71, 0x5f, 0xeb, 0x43, 0x45, 0x24, 0xd3, 0xec, 0x08, | |
1098 | 0x52, 0xc2, 0x89, 0x2d, 0x9c, 0x1a, 0xcc, 0x91, 0x65, 0x5d, 0xa3, 0xa1, | |
1099 | 0x35, 0x31, 0x10, 0x1c, 0x3a, 0xa8, 0x4d, 0x18, 0xd5, 0x06, 0xaf, 0xb2, | |
1100 | 0xec, 0x5c, 0x89, 0x9e, 0x90, 0x86, 0x10, 0x01, 0xeb, 0x51, 0xd5, 0x1b, | |
1101 | 0x9c, 0xcb, 0x66, 0x07, 0x3f, 0xc4, 0x6e, 0x0a, 0x1b, 0x73, 0xa0, 0x4b, | |
1102 | 0x5f, 0x4d, 0xab, 0x35, 0x28, 0xfa, 0xda, 0x3a, 0x0c, 0x08, 0xe8, 0xf3, | |
1103 | 0xef, 0x42, 0x67, 0xbc, 0x21, 0xf2, 0xc2, 0xb8, 0xff, 0x1a, 0x81, 0x05, | |
1104 | 0x68, 0x73, 0x62, 0xdf, 0xd7, 0xab, 0x0f, 0x22, 0x89, 0x57, 0x96, 0xd4, | |
1105 | 0x93, 0xaf, 0xa1, 0x21, 0xa3, 0x48, 0xe9, 0xf0, 0x97, 0x47, 0xa0, 0x27, | |
1106 | 0xba, 0x87, 0xb8, 0x15, 0x5f, 0xff, 0x2c, 0x50, 0x41, 0xf1, 0x7e, 0xc6, | |
1107 | 0x81, 0xc4, 0x51, 0xf1, 0xfd, 0xd6, 0x86, 0xf7, 0x69, 0x97, 0xf1, 0x49, | |
1108 | 0xc9, 0xf9, 0xf4, 0x9b, 0xf4, 0xe8, 0x85, 0xa7, 0xbd, 0x36, 0x55, 0x4a, | |
1109 | 0x3d, 0xe8, 0x65, 0x09, 0x7b, 0xb7, 0x12, 0x64, 0xd2, 0x0a, 0x53, 0x60, | |
1110 | 0x48, 0xd1, 0x8a, 0xbd | |
1111 | }; | |
1112 | ||
1113 | if (!TEST_ptr(bld = OSSL_PARAM_BLD_new()) | |
1114 | || !TEST_ptr(pub = BN_bin2bn(pub_data, sizeof(pub_data), NULL)) | |
1115 | || !TEST_ptr(priv = BN_bin2bn(priv_data, sizeof(priv_data), NULL)) | |
1116 | || !TEST_ptr(p = BN_bin2bn(p_data, sizeof(p_data), NULL)) | |
1117 | || !TEST_ptr(q = BN_bin2bn(q_data, sizeof(q_data), NULL)) | |
1118 | || !TEST_ptr(g = BN_bin2bn(g_data, sizeof(g_data), NULL)) | |
1119 | ||
1120 | || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_P, p)) | |
1121 | || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_Q, q)) | |
1122 | || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_FFC_G, g)) | |
1123 | || !TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, | |
1124 | OSSL_PKEY_PARAM_FFC_SEED, | |
1125 | seed_data, | |
1126 | sizeof(seed_data))) | |
1127 | || !TEST_true(OSSL_PARAM_BLD_push_int(bld, OSSL_PKEY_PARAM_FFC_GINDEX, | |
1128 | gindex)) | |
1129 | || !TEST_true(OSSL_PARAM_BLD_push_int(bld, | |
1130 | OSSL_PKEY_PARAM_FFC_PCOUNTER, | |
1131 | pcounter)) | |
1132 | || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PUB_KEY, | |
1133 | pub)) | |
1134 | || !TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_PRIV_KEY, | |
1135 | priv)) | |
1136 | || !TEST_ptr(fromdata_params = OSSL_PARAM_BLD_to_param(bld))) | |
1137 | goto err; | |
1138 | ||
1139 | if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL))) | |
1140 | goto err; | |
1141 | ||
1142 | if (!TEST_true(EVP_PKEY_key_fromdata_init(ctx)) | |
1143 | || !TEST_true(EVP_PKEY_fromdata(ctx, &pk, fromdata_params)) | |
1144 | || !TEST_int_eq(EVP_PKEY_bits(pk), 2048) | |
1145 | || !TEST_int_eq(EVP_PKEY_security_bits(pk), 112) | |
1146 | || !TEST_int_eq(EVP_PKEY_size(pk), 2 + 2 * (3 + sizeof(q_data)))) | |
1147 | goto err; | |
1148 | ||
023b188c | 1149 | if (!TEST_false(EVP_PKEY_get_utf8_string_param(pk, OSSL_PKEY_PARAM_GROUP_NAME, |
b03ec3b5 SL |
1150 | name_out, sizeof(name_out), |
1151 | &len)) | |
1152 | || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PUB_KEY, | |
1153 | &pub_out)) | |
1154 | || !TEST_BN_eq(pub, pub_out) | |
1155 | || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_PRIV_KEY, | |
1156 | &priv_out)) | |
1157 | || !TEST_BN_eq(priv, priv_out) | |
1158 | || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_P, &p_out)) | |
1159 | || !TEST_BN_eq(p, p_out) | |
1160 | || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_Q, &q_out)) | |
1161 | || !TEST_BN_eq(q, q_out) | |
1162 | || !TEST_true(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_G, &g_out)) | |
1163 | || !TEST_BN_eq(g, g_out) | |
1164 | || !TEST_false(EVP_PKEY_get_bn_param(pk, OSSL_PKEY_PARAM_FFC_COFACTOR, | |
1165 | &j_out)) | |
1166 | || !TEST_ptr_null(j_out) | |
1167 | || !TEST_true(EVP_PKEY_get_octet_string_param(pk, | |
1168 | OSSL_PKEY_PARAM_FFC_SEED, | |
1169 | seed_out, sizeof(seed_out), | |
1170 | &len)) | |
1171 | || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_GINDEX, | |
1172 | &gindex_out)) | |
1173 | || !TEST_int_eq(gindex, gindex_out) | |
1174 | || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_H, | |
1175 | &hindex_out)) | |
1176 | || !TEST_int_eq(hindex_out, 0) | |
1177 | || !TEST_true(EVP_PKEY_get_int_param(pk, OSSL_PKEY_PARAM_FFC_PCOUNTER, | |
1178 | &pcounter_out)) | |
1179 | || !TEST_int_eq(pcounter, pcounter_out)) | |
1180 | goto err; | |
1181 | ||
1182 | if (!TEST_ptr(key_ctx = EVP_PKEY_CTX_new_from_pkey(NULL, pk, ""))) | |
1183 | goto err; | |
1184 | ||
1185 | if (!TEST_true(EVP_PKEY_check(key_ctx)) | |
1186 | || !TEST_true(EVP_PKEY_public_check(key_ctx)) | |
1187 | || !TEST_true(EVP_PKEY_private_check(key_ctx)) | |
1188 | || !TEST_true(EVP_PKEY_pairwise_check(key_ctx))) | |
1189 | goto err; | |
1190 | ||
1191 | if (!TEST_ptr(copy_pk = EVP_PKEY_new()) | |
1192 | || !TEST_true(EVP_PKEY_copy_parameters(copy_pk, pk))) | |
1193 | goto err; | |
1194 | ||
1195 | ret = test_print_key_using_pem("DSA", pk) | |
1196 | && test_print_key_using_serializer("DSA", pk); | |
1197 | err: | |
1198 | OSSL_PARAM_BLD_free_params(fromdata_params); | |
1199 | OSSL_PARAM_BLD_free(bld); | |
1200 | BN_free(p); | |
1201 | BN_free(q); | |
1202 | BN_free(g); | |
1203 | BN_free(pub); | |
1204 | BN_free(priv); | |
1205 | BN_free(p_out); | |
1206 | BN_free(q_out); | |
1207 | BN_free(g_out); | |
1208 | BN_free(pub_out); | |
1209 | BN_free(priv_out); | |
1210 | BN_free(j_out); | |
1211 | EVP_PKEY_free(pk); | |
1212 | EVP_PKEY_free(copy_pk); | |
1213 | EVP_PKEY_CTX_free(ctx); | |
1214 | EVP_PKEY_CTX_free(key_ctx); | |
1215 | ||
1216 | return ret; | |
1217 | } | |
90113096 MB |
1218 | |
1219 | static int test_check_dsa(void) | |
1220 | { | |
1221 | int ret = 0; | |
1222 | EVP_PKEY_CTX *ctx = NULL; | |
1223 | ||
1224 | if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(NULL, "DSA", NULL)) | |
1225 | || !TEST_false(EVP_PKEY_check(ctx)) | |
1226 | || !TEST_false(EVP_PKEY_public_check(ctx)) | |
1227 | || !TEST_false(EVP_PKEY_private_check(ctx)) | |
1228 | || !TEST_false(EVP_PKEY_pairwise_check(ctx))) | |
1229 | goto err; | |
1230 | ||
1231 | ret = 1; | |
1232 | err: | |
1233 | EVP_PKEY_CTX_free(ctx); | |
1234 | ||
1235 | return ret; | |
1236 | } | |
b03ec3b5 SL |
1237 | #endif /* OPENSSL_NO_DSA */ |
1238 | ||
1239 | ||
6ae5543c RL |
1240 | int setup_tests(void) |
1241 | { | |
8efc4a9c MC |
1242 | if (!test_skip_common_options()) { |
1243 | TEST_error("Error parsing test options\n"); | |
1244 | return 0; | |
1245 | } | |
1246 | ||
1247 | if (!TEST_ptr(datadir = test_get_argument(0))) | |
1248 | return 0; | |
1249 | ||
96ebe52e | 1250 | ADD_TEST(test_evp_pkey_get_bn_param_large); |
6ae5543c | 1251 | ADD_TEST(test_fromdata_rsa); |
285c6913 | 1252 | #ifndef OPENSSL_NO_DH |
7165593c SL |
1253 | ADD_TEST(test_fromdata_dh_fips186_4); |
1254 | ADD_TEST(test_fromdata_dh_named_group); | |
8efc4a9c | 1255 | #endif |
b03ec3b5 | 1256 | #ifndef OPENSSL_NO_DSA |
90113096 | 1257 | ADD_TEST(test_check_dsa); |
b03ec3b5 SL |
1258 | ADD_TEST(test_fromdata_dsa_fips186_4); |
1259 | #endif | |
8efc4a9c | 1260 | #ifndef OPENSSL_NO_EC |
244bc297 | 1261 | ADD_ALL_TESTS(test_fromdata_ecx, 4); |
f552d900 | 1262 | ADD_TEST(test_fromdata_ec); |
285c6913 | 1263 | #endif |
6ae5543c RL |
1264 | return 1; |
1265 | } |