]>
Commit | Line | Data |
---|---|---|
0e360199 | 1 | /* |
760e2d60 | 2 | * Copyright 2015-2019 The OpenSSL Project Authors. All Rights Reserved. |
0e360199 | 3 | * |
909f1a2e | 4 | * Licensed under the Apache License 2.0 (the "License"). You may not use |
440e5d80 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 | |
0e360199 BL |
8 | */ |
9 | ||
10 | #include <stdio.h> | |
11 | #include <string.h> | |
307e3978 DSH |
12 | #include <stdlib.h> |
13 | #include <ctype.h> | |
0e360199 | 14 | #include <openssl/evp.h> |
5824cc29 | 15 | #include <openssl/pem.h> |
0b13e9f0 | 16 | #include <openssl/err.h> |
d5e5e2ff | 17 | #include <openssl/provider.h> |
307e3978 | 18 | #include <openssl/x509v3.h> |
351fe214 | 19 | #include <openssl/pkcs12.h> |
44a284d2 | 20 | #include <openssl/kdf.h> |
25446a66 RL |
21 | #include <openssl/params.h> |
22 | #include <openssl/core_names.h> | |
3b53e18a | 23 | #include "internal/numbers.h" |
25446a66 | 24 | #include "internal/nelem.h" |
6c5943c9 | 25 | #include "testutil.h" |
c3fc7d9a | 26 | #include "evp_test.h" |
0e360199 | 27 | |
b1ceb439 | 28 | #define AAD_NUM 4 |
c49e0b04 RS |
29 | |
30 | typedef struct evp_test_method_st EVP_TEST_METHOD; | |
7193f872 | 31 | |
6c5943c9 | 32 | /* |
c49e0b04 | 33 | * Structure holding test information |
6c5943c9 | 34 | */ |
c49e0b04 | 35 | typedef struct evp_test_st { |
ae269dd8 RS |
36 | STANZA s; /* Common test stanza */ |
37 | char *name; | |
c49e0b04 | 38 | int skip; /* Current test should be skipped */ |
c49e0b04 RS |
39 | const EVP_TEST_METHOD *meth; /* method for this test */ |
40 | const char *err, *aux_err; /* Error string for test */ | |
41 | char *expected_err; /* Expected error value of test */ | |
c49e0b04 RS |
42 | char *reason; /* Expected error reason string */ |
43 | void *data; /* test specific data */ | |
44 | } EVP_TEST; | |
0e360199 | 45 | |
307e3978 | 46 | /* |
c49e0b04 | 47 | * Test method structure |
307e3978 | 48 | */ |
c49e0b04 RS |
49 | struct evp_test_method_st { |
50 | /* Name of test as it appears in file */ | |
51 | const char *name; | |
52 | /* Initialise test for "alg" */ | |
53 | int (*init) (EVP_TEST * t, const char *alg); | |
54 | /* Clean up method */ | |
55 | void (*cleanup) (EVP_TEST * t); | |
56 | /* Test specific name value pair processing */ | |
57 | int (*parse) (EVP_TEST * t, const char *name, const char *value); | |
58 | /* Run the test itself */ | |
59 | int (*run_test) (EVP_TEST * t); | |
60 | }; | |
5b46eee0 | 61 | |
0e360199 | 62 | |
3cdd1e94 | 63 | /* |
c49e0b04 | 64 | * Linked list of named keys. |
3cdd1e94 | 65 | */ |
c49e0b04 RS |
66 | typedef struct key_list_st { |
67 | char *name; | |
68 | EVP_PKEY *key; | |
69 | struct key_list_st *next; | |
70 | } KEY_LIST; | |
fa013b65 | 71 | |
c49e0b04 RS |
72 | /* |
73 | * List of public and private keys | |
74 | */ | |
75 | static KEY_LIST *private_keys; | |
76 | static KEY_LIST *public_keys; | |
77 | static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst); | |
fa013b65 | 78 | |
c49e0b04 | 79 | static int parse_bin(const char *value, unsigned char **buf, size_t *buflen); |
3cdd1e94 | 80 | |
4cceb185 P |
81 | /* |
82 | * Compare two memory regions for equality, returning zero if they differ. | |
83 | * However, if there is expected to be an error and the actual error | |
84 | * matches then the memory is expected to be different so handle this | |
85 | * case without producing unnecessary test framework output. | |
86 | */ | |
87 | static int memory_err_compare(EVP_TEST *t, const char *err, | |
88 | const void *expected, size_t expected_len, | |
89 | const void *got, size_t got_len) | |
90 | { | |
91 | int r; | |
92 | ||
93 | if (t->expected_err != NULL && strcmp(t->expected_err, err) == 0) | |
94 | r = !TEST_mem_ne(expected, expected_len, got, got_len); | |
95 | else | |
96 | r = TEST_mem_eq(expected, expected_len, got, got_len); | |
97 | if (!r) | |
98 | t->err = err; | |
99 | return r; | |
100 | } | |
101 | ||
c3fc7d9a DSH |
102 | /* |
103 | * Structure used to hold a list of blocks of memory to test | |
104 | * calls to "update" like functions. | |
105 | */ | |
c3fc7d9a DSH |
106 | struct evp_test_buffer_st { |
107 | unsigned char *buf; | |
108 | size_t buflen; | |
109 | size_t count; | |
110 | int count_set; | |
111 | }; | |
112 | ||
113 | static void evp_test_buffer_free(EVP_TEST_BUFFER *db) | |
114 | { | |
115 | if (db != NULL) { | |
116 | OPENSSL_free(db->buf); | |
117 | OPENSSL_free(db); | |
118 | } | |
119 | } | |
120 | ||
c49e0b04 RS |
121 | /* |
122 | * append buffer to a list | |
123 | */ | |
c3fc7d9a DSH |
124 | static int evp_test_buffer_append(const char *value, |
125 | STACK_OF(EVP_TEST_BUFFER) **sk) | |
126 | { | |
127 | EVP_TEST_BUFFER *db = NULL; | |
128 | ||
129 | if (!TEST_ptr(db = OPENSSL_malloc(sizeof(*db)))) | |
130 | goto err; | |
131 | ||
c49e0b04 | 132 | if (!parse_bin(value, &db->buf, &db->buflen)) |
c3fc7d9a DSH |
133 | goto err; |
134 | db->count = 1; | |
135 | db->count_set = 0; | |
136 | ||
137 | if (*sk == NULL && !TEST_ptr(*sk = sk_EVP_TEST_BUFFER_new_null())) | |
c49e0b04 | 138 | goto err; |
c3fc7d9a DSH |
139 | if (!sk_EVP_TEST_BUFFER_push(*sk, db)) |
140 | goto err; | |
141 | ||
142 | return 1; | |
143 | ||
c49e0b04 | 144 | err: |
c3fc7d9a | 145 | evp_test_buffer_free(db); |
c3fc7d9a DSH |
146 | return 0; |
147 | } | |
148 | ||
149 | /* | |
150 | * replace last buffer in list with copies of itself | |
151 | */ | |
152 | static int evp_test_buffer_ncopy(const char *value, | |
153 | STACK_OF(EVP_TEST_BUFFER) *sk) | |
154 | { | |
155 | EVP_TEST_BUFFER *db; | |
156 | unsigned char *tbuf, *p; | |
157 | size_t tbuflen; | |
158 | int ncopy = atoi(value); | |
159 | int i; | |
160 | ||
161 | if (ncopy <= 0) | |
162 | return 0; | |
163 | if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0) | |
164 | return 0; | |
165 | db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1); | |
166 | ||
167 | tbuflen = db->buflen * ncopy; | |
168 | if (!TEST_ptr(tbuf = OPENSSL_malloc(tbuflen))) | |
169 | return 0; | |
170 | for (i = 0, p = tbuf; i < ncopy; i++, p += db->buflen) | |
171 | memcpy(p, db->buf, db->buflen); | |
172 | ||
173 | OPENSSL_free(db->buf); | |
174 | db->buf = tbuf; | |
175 | db->buflen = tbuflen; | |
176 | return 1; | |
177 | } | |
178 | ||
c49e0b04 RS |
179 | /* |
180 | * set repeat count for last buffer in list | |
181 | */ | |
c3fc7d9a DSH |
182 | static int evp_test_buffer_set_count(const char *value, |
183 | STACK_OF(EVP_TEST_BUFFER) *sk) | |
184 | { | |
185 | EVP_TEST_BUFFER *db; | |
186 | int count = atoi(value); | |
187 | ||
188 | if (count <= 0) | |
189 | return 0; | |
190 | ||
191 | if (sk == NULL || sk_EVP_TEST_BUFFER_num(sk) == 0) | |
192 | return 0; | |
193 | ||
194 | db = sk_EVP_TEST_BUFFER_value(sk, sk_EVP_TEST_BUFFER_num(sk) - 1); | |
c49e0b04 | 195 | if (db->count_set != 0) |
c3fc7d9a DSH |
196 | return 0; |
197 | ||
198 | db->count = (size_t)count; | |
199 | db->count_set = 1; | |
200 | return 1; | |
201 | } | |
202 | ||
203 | /* | |
204 | * call "fn" with each element of the list in turn | |
205 | */ | |
206 | static int evp_test_buffer_do(STACK_OF(EVP_TEST_BUFFER) *sk, | |
207 | int (*fn)(void *ctx, | |
208 | const unsigned char *buf, | |
209 | size_t buflen), | |
210 | void *ctx) | |
211 | { | |
212 | int i; | |
213 | ||
214 | for (i = 0; i < sk_EVP_TEST_BUFFER_num(sk); i++) { | |
215 | EVP_TEST_BUFFER *tb = sk_EVP_TEST_BUFFER_value(sk, i); | |
216 | size_t j; | |
217 | ||
218 | for (j = 0; j < tb->count; j++) { | |
219 | if (fn(ctx, tb->buf, tb->buflen) <= 0) | |
220 | return 0; | |
221 | } | |
222 | } | |
223 | return 1; | |
224 | } | |
225 | ||
6c5943c9 | 226 | /* |
c49e0b04 RS |
227 | * Unescape some sequences in string literals (only \n for now). |
228 | * Return an allocated buffer, set |out_len|. If |input_len| | |
229 | * is zero, get an empty buffer but set length to zero. | |
6c5943c9 | 230 | */ |
c49e0b04 RS |
231 | static unsigned char* unescape(const char *input, size_t input_len, |
232 | size_t *out_len) | |
233 | { | |
234 | unsigned char *ret, *p; | |
235 | size_t i; | |
5824cc29 | 236 | |
c49e0b04 RS |
237 | if (input_len == 0) { |
238 | *out_len = 0; | |
239 | return OPENSSL_zalloc(1); | |
240 | } | |
307e3978 | 241 | |
c49e0b04 RS |
242 | /* Escaping is non-expanding; over-allocate original size for simplicity. */ |
243 | if (!TEST_ptr(ret = p = OPENSSL_malloc(input_len))) | |
244 | return NULL; | |
6c5943c9 | 245 | |
c49e0b04 RS |
246 | for (i = 0; i < input_len; i++) { |
247 | if (*input == '\\') { | |
248 | if (i == input_len - 1 || *++input != 'n') { | |
249 | TEST_error("Bad escape sequence in file"); | |
250 | goto err; | |
251 | } | |
252 | *p++ = '\n'; | |
253 | i++; | |
254 | input++; | |
255 | } else { | |
256 | *p++ = *input++; | |
257 | } | |
258 | } | |
307e3978 | 259 | |
c49e0b04 RS |
260 | *out_len = p - ret; |
261 | return ret; | |
86885c28 | 262 | |
c49e0b04 RS |
263 | err: |
264 | OPENSSL_free(ret); | |
307e3978 | 265 | return NULL; |
0f113f3e MC |
266 | } |
267 | ||
6c5943c9 | 268 | /* |
c49e0b04 RS |
269 | * For a hex string "value" convert to a binary allocated buffer. |
270 | * Return 1 on success or 0 on failure. | |
6c5943c9 | 271 | */ |
c49e0b04 | 272 | static int parse_bin(const char *value, unsigned char **buf, size_t *buflen) |
0f113f3e | 273 | { |
c49e0b04 | 274 | long len; |
6c5943c9 | 275 | |
c49e0b04 RS |
276 | /* Check for NULL literal */ |
277 | if (strcmp(value, "NULL") == 0) { | |
278 | *buf = NULL; | |
279 | *buflen = 0; | |
307e3978 | 280 | return 1; |
71f60ef3 | 281 | } |
6c5943c9 | 282 | |
c49e0b04 RS |
283 | /* Check for empty value */ |
284 | if (*value == '\0') { | |
285 | /* | |
286 | * Don't return NULL for zero length buffer. This is needed for | |
287 | * some tests with empty keys: HMAC_Init_ex() expects a non-NULL key | |
288 | * buffer even if the key length is 0, in order to detect key reset. | |
289 | */ | |
290 | *buf = OPENSSL_malloc(1); | |
291 | if (*buf == NULL) | |
5824cc29 | 292 | return 0; |
c49e0b04 RS |
293 | **buf = 0; |
294 | *buflen = 0; | |
71f60ef3 | 295 | return 1; |
5824cc29 DSH |
296 | } |
297 | ||
c49e0b04 RS |
298 | /* Check for string literal */ |
299 | if (value[0] == '"') { | |
300 | size_t vlen = strlen(++value); | |
301 | ||
302 | if (vlen == 0 || value[vlen - 1] != '"') | |
307e3978 | 303 | return 0; |
c49e0b04 RS |
304 | vlen--; |
305 | *buf = unescape(value, vlen, buflen); | |
306 | return *buf == NULL ? 0 : 1; | |
6c5943c9 | 307 | } |
307e3978 | 308 | |
c49e0b04 RS |
309 | /* Otherwise assume as hex literal and convert it to binary buffer */ |
310 | if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) { | |
311 | TEST_info("Can't convert %s", value); | |
8fe3127c | 312 | TEST_openssl_errors(); |
c49e0b04 | 313 | return -1; |
0f113f3e | 314 | } |
c49e0b04 RS |
315 | /* Size of input buffer means we'll never overflow */ |
316 | *buflen = len; | |
307e3978 DSH |
317 | return 1; |
318 | } | |
0f113f3e | 319 | |
c49e0b04 RS |
320 | |
321 | /** | |
322 | *** MESSAGE DIGEST TESTS | |
323 | **/ | |
4897dc40 | 324 | |
6c5943c9 | 325 | typedef struct digest_data_st { |
307e3978 DSH |
326 | /* Digest this test is for */ |
327 | const EVP_MD *digest; | |
328 | /* Input to digest */ | |
c3fc7d9a | 329 | STACK_OF(EVP_TEST_BUFFER) *input; |
307e3978 DSH |
330 | /* Expected output */ |
331 | unsigned char *output; | |
332 | size_t output_len; | |
6c5943c9 | 333 | } DIGEST_DATA; |
4897dc40 | 334 | |
6c5943c9 | 335 | static int digest_test_init(EVP_TEST *t, const char *alg) |
307e3978 | 336 | { |
6c5943c9 | 337 | DIGEST_DATA *mdat; |
c49e0b04 | 338 | const EVP_MD *digest; |
6c5943c9 | 339 | |
c49e0b04 | 340 | if ((digest = EVP_get_digestbyname(alg)) == NULL) { |
578ce42d DSH |
341 | /* If alg has an OID assume disabled algorithm */ |
342 | if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) { | |
343 | t->skip = 1; | |
344 | return 1; | |
345 | } | |
307e3978 | 346 | return 0; |
578ce42d | 347 | } |
c49e0b04 RS |
348 | if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat)))) |
349 | return 0; | |
307e3978 | 350 | t->data = mdat; |
c49e0b04 | 351 | mdat->digest = digest; |
4897dc40 | 352 | return 1; |
0f113f3e | 353 | } |
4897dc40 | 354 | |
6c5943c9 | 355 | static void digest_test_cleanup(EVP_TEST *t) |
307e3978 | 356 | { |
6c5943c9 RS |
357 | DIGEST_DATA *mdat = t->data; |
358 | ||
c3fc7d9a | 359 | sk_EVP_TEST_BUFFER_pop_free(mdat->input, evp_test_buffer_free); |
6c5943c9 | 360 | OPENSSL_free(mdat->output); |
307e3978 DSH |
361 | } |
362 | ||
6c5943c9 | 363 | static int digest_test_parse(EVP_TEST *t, |
307e3978 DSH |
364 | const char *keyword, const char *value) |
365 | { | |
6c5943c9 RS |
366 | DIGEST_DATA *mdata = t->data; |
367 | ||
86885c28 | 368 | if (strcmp(keyword, "Input") == 0) |
c3fc7d9a | 369 | return evp_test_buffer_append(value, &mdata->input); |
86885c28 | 370 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 371 | return parse_bin(value, &mdata->output, &mdata->output_len); |
c3fc7d9a DSH |
372 | if (strcmp(keyword, "Count") == 0) |
373 | return evp_test_buffer_set_count(value, mdata->input); | |
374 | if (strcmp(keyword, "Ncopy") == 0) | |
375 | return evp_test_buffer_ncopy(value, mdata->input); | |
307e3978 DSH |
376 | return 0; |
377 | } | |
378 | ||
c3fc7d9a DSH |
379 | static int digest_update_fn(void *ctx, const unsigned char *buf, size_t buflen) |
380 | { | |
381 | return EVP_DigestUpdate(ctx, buf, buflen); | |
382 | } | |
383 | ||
6c5943c9 | 384 | static int digest_test_run(EVP_TEST *t) |
0f113f3e | 385 | { |
e3d378bc | 386 | DIGEST_DATA *expected = t->data; |
307e3978 | 387 | EVP_MD_CTX *mctx; |
cd8d1456 | 388 | unsigned char *got = NULL; |
e3d378bc | 389 | unsigned int got_len; |
6c5943c9 RS |
390 | |
391 | t->err = "TEST_FAILURE"; | |
392 | if (!TEST_ptr(mctx = EVP_MD_CTX_new())) | |
307e3978 | 393 | goto err; |
6c5943c9 | 394 | |
cd8d1456 AP |
395 | got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ? |
396 | expected->output_len : EVP_MAX_MD_SIZE); | |
397 | if (!TEST_ptr(got)) | |
398 | goto err; | |
399 | ||
e3d378bc | 400 | if (!EVP_DigestInit_ex(mctx, expected->digest, NULL)) { |
6c5943c9 | 401 | t->err = "DIGESTINIT_ERROR"; |
307e3978 | 402 | goto err; |
618be04e | 403 | } |
e3d378bc | 404 | if (!evp_test_buffer_do(expected->input, digest_update_fn, mctx)) { |
c3fc7d9a DSH |
405 | t->err = "DIGESTUPDATE_ERROR"; |
406 | goto err; | |
407 | } | |
408 | ||
cd8d1456 AP |
409 | if (EVP_MD_flags(expected->digest) & EVP_MD_FLAG_XOF) { |
410 | got_len = expected->output_len; | |
411 | if (!EVP_DigestFinalXOF(mctx, got, got_len)) { | |
412 | t->err = "DIGESTFINALXOF_ERROR"; | |
413 | goto err; | |
414 | } | |
415 | } else { | |
416 | if (!EVP_DigestFinal(mctx, got, &got_len)) { | |
417 | t->err = "DIGESTFINAL_ERROR"; | |
418 | goto err; | |
419 | } | |
6c5943c9 | 420 | } |
e3d378bc | 421 | if (!TEST_int_eq(expected->output_len, got_len)) { |
6c5943c9 | 422 | t->err = "DIGEST_LENGTH_MISMATCH"; |
307e3978 | 423 | goto err; |
6c5943c9 | 424 | } |
4cceb185 P |
425 | if (!memory_err_compare(t, "DIGEST_MISMATCH", |
426 | expected->output, expected->output_len, | |
427 | got, got_len)) | |
307e3978 | 428 | goto err; |
4cceb185 | 429 | |
6c5943c9 RS |
430 | t->err = NULL; |
431 | ||
307e3978 | 432 | err: |
cd8d1456 | 433 | OPENSSL_free(got); |
bfb0641f | 434 | EVP_MD_CTX_free(mctx); |
b033e5d5 | 435 | return 1; |
307e3978 | 436 | } |
4897dc40 | 437 | |
6c5943c9 | 438 | static const EVP_TEST_METHOD digest_test_method = { |
307e3978 DSH |
439 | "Digest", |
440 | digest_test_init, | |
441 | digest_test_cleanup, | |
442 | digest_test_parse, | |
443 | digest_test_run | |
444 | }; | |
445 | ||
c49e0b04 RS |
446 | |
447 | /** | |
448 | *** CIPHER TESTS | |
449 | **/ | |
450 | ||
6c5943c9 | 451 | typedef struct cipher_data_st { |
307e3978 DSH |
452 | const EVP_CIPHER *cipher; |
453 | int enc; | |
2207ba7b | 454 | /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */ |
307e3978 DSH |
455 | int aead; |
456 | unsigned char *key; | |
457 | size_t key_len; | |
458 | unsigned char *iv; | |
459 | size_t iv_len; | |
460 | unsigned char *plaintext; | |
461 | size_t plaintext_len; | |
462 | unsigned char *ciphertext; | |
463 | size_t ciphertext_len; | |
b1ceb439 TS |
464 | /* GCM, CCM, OCB and SIV only */ |
465 | unsigned char *aad[AAD_NUM]; | |
466 | size_t aad_len[AAD_NUM]; | |
307e3978 DSH |
467 | unsigned char *tag; |
468 | size_t tag_len; | |
67c81ec3 | 469 | int tag_late; |
6c5943c9 | 470 | } CIPHER_DATA; |
307e3978 | 471 | |
6c5943c9 | 472 | static int cipher_test_init(EVP_TEST *t, const char *alg) |
307e3978 DSH |
473 | { |
474 | const EVP_CIPHER *cipher; | |
c49e0b04 RS |
475 | CIPHER_DATA *cdat; |
476 | int m; | |
6c5943c9 | 477 | |
c49e0b04 | 478 | if ((cipher = EVP_get_cipherbyname(alg)) == NULL) { |
33a89fa6 DSH |
479 | /* If alg has an OID assume disabled algorithm */ |
480 | if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) { | |
481 | t->skip = 1; | |
482 | return 1; | |
483 | } | |
0f113f3e | 484 | return 0; |
33a89fa6 | 485 | } |
c49e0b04 | 486 | cdat = OPENSSL_zalloc(sizeof(*cdat)); |
307e3978 DSH |
487 | cdat->cipher = cipher; |
488 | cdat->enc = -1; | |
c49e0b04 RS |
489 | m = EVP_CIPHER_mode(cipher); |
490 | if (m == EVP_CIPH_GCM_MODE | |
491 | || m == EVP_CIPH_OCB_MODE | |
b1ceb439 | 492 | || m == EVP_CIPH_SIV_MODE |
c49e0b04 | 493 | || m == EVP_CIPH_CCM_MODE) |
523fcfb4 | 494 | cdat->aead = m; |
eb85cb86 AP |
495 | else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER) |
496 | cdat->aead = -1; | |
307e3978 DSH |
497 | else |
498 | cdat->aead = 0; | |
4897dc40 | 499 | |
c49e0b04 | 500 | t->data = cdat; |
307e3978 DSH |
501 | return 1; |
502 | } | |
4897dc40 | 503 | |
6c5943c9 | 504 | static void cipher_test_cleanup(EVP_TEST *t) |
307e3978 | 505 | { |
b1ceb439 | 506 | int i; |
6c5943c9 RS |
507 | CIPHER_DATA *cdat = t->data; |
508 | ||
509 | OPENSSL_free(cdat->key); | |
510 | OPENSSL_free(cdat->iv); | |
511 | OPENSSL_free(cdat->ciphertext); | |
512 | OPENSSL_free(cdat->plaintext); | |
b1ceb439 TS |
513 | for (i = 0; i < AAD_NUM; i++) |
514 | OPENSSL_free(cdat->aad[i]); | |
6c5943c9 | 515 | OPENSSL_free(cdat->tag); |
307e3978 | 516 | } |
4897dc40 | 517 | |
6c5943c9 | 518 | static int cipher_test_parse(EVP_TEST *t, const char *keyword, |
307e3978 DSH |
519 | const char *value) |
520 | { | |
6c5943c9 | 521 | CIPHER_DATA *cdat = t->data; |
b1ceb439 | 522 | int i; |
6c5943c9 | 523 | |
86885c28 | 524 | if (strcmp(keyword, "Key") == 0) |
c49e0b04 | 525 | return parse_bin(value, &cdat->key, &cdat->key_len); |
86885c28 | 526 | if (strcmp(keyword, "IV") == 0) |
c49e0b04 | 527 | return parse_bin(value, &cdat->iv, &cdat->iv_len); |
86885c28 | 528 | if (strcmp(keyword, "Plaintext") == 0) |
c49e0b04 | 529 | return parse_bin(value, &cdat->plaintext, &cdat->plaintext_len); |
86885c28 | 530 | if (strcmp(keyword, "Ciphertext") == 0) |
c49e0b04 | 531 | return parse_bin(value, &cdat->ciphertext, &cdat->ciphertext_len); |
307e3978 | 532 | if (cdat->aead) { |
b1ceb439 TS |
533 | if (strcmp(keyword, "AAD") == 0) { |
534 | for (i = 0; i < AAD_NUM; i++) { | |
535 | if (cdat->aad[i] == NULL) | |
536 | return parse_bin(value, &cdat->aad[i], &cdat->aad_len[i]); | |
537 | } | |
538 | return 0; | |
539 | } | |
86885c28 | 540 | if (strcmp(keyword, "Tag") == 0) |
c49e0b04 | 541 | return parse_bin(value, &cdat->tag, &cdat->tag_len); |
67c81ec3 TN |
542 | if (strcmp(keyword, "SetTagLate") == 0) { |
543 | if (strcmp(value, "TRUE") == 0) | |
544 | cdat->tag_late = 1; | |
545 | else if (strcmp(value, "FALSE") == 0) | |
546 | cdat->tag_late = 0; | |
547 | else | |
548 | return 0; | |
549 | return 1; | |
550 | } | |
0f113f3e | 551 | } |
4897dc40 | 552 | |
86885c28 RS |
553 | if (strcmp(keyword, "Operation") == 0) { |
554 | if (strcmp(value, "ENCRYPT") == 0) | |
307e3978 | 555 | cdat->enc = 1; |
86885c28 | 556 | else if (strcmp(value, "DECRYPT") == 0) |
307e3978 DSH |
557 | cdat->enc = 0; |
558 | else | |
559 | return 0; | |
560 | return 1; | |
0f113f3e | 561 | } |
307e3978 | 562 | return 0; |
0f113f3e | 563 | } |
4897dc40 | 564 | |
6c5943c9 | 565 | static int cipher_test_enc(EVP_TEST *t, int enc, |
0b96d77a | 566 | size_t out_misalign, size_t inp_misalign, int frag) |
0f113f3e | 567 | { |
e3d378bc AP |
568 | CIPHER_DATA *expected = t->data; |
569 | unsigned char *in, *expected_out, *tmp = NULL; | |
0b96d77a | 570 | size_t in_len, out_len, donelen = 0; |
b1ceb439 | 571 | int ok = 0, tmplen, chunklen, tmpflen, i; |
307e3978 | 572 | EVP_CIPHER_CTX *ctx = NULL; |
6c5943c9 RS |
573 | |
574 | t->err = "TEST_FAILURE"; | |
575 | if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())) | |
307e3978 DSH |
576 | goto err; |
577 | EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW); | |
578 | if (enc) { | |
e3d378bc AP |
579 | in = expected->plaintext; |
580 | in_len = expected->plaintext_len; | |
581 | expected_out = expected->ciphertext; | |
582 | out_len = expected->ciphertext_len; | |
307e3978 | 583 | } else { |
e3d378bc AP |
584 | in = expected->ciphertext; |
585 | in_len = expected->ciphertext_len; | |
586 | expected_out = expected->plaintext; | |
587 | out_len = expected->plaintext_len; | |
0f113f3e | 588 | } |
ff715da4 AP |
589 | if (inp_misalign == (size_t)-1) { |
590 | /* | |
591 | * Exercise in-place encryption | |
592 | */ | |
593 | tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH); | |
594 | if (!tmp) | |
595 | goto err; | |
596 | in = memcpy(tmp + out_misalign, in, in_len); | |
597 | } else { | |
598 | inp_misalign += 16 - ((out_misalign + in_len) & 15); | |
599 | /* | |
600 | * 'tmp' will store both output and copy of input. We make the copy | |
601 | * of input to specifically aligned part of 'tmp'. So we just | |
602 | * figured out how much padding would ensure the required alignment, | |
603 | * now we allocate extended buffer and finally copy the input just | |
604 | * past inp_misalign in expression below. Output will be written | |
605 | * past out_misalign... | |
606 | */ | |
607 | tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH + | |
608 | inp_misalign + in_len); | |
609 | if (!tmp) | |
610 | goto err; | |
611 | in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH + | |
612 | inp_misalign, in, in_len); | |
613 | } | |
e3d378bc | 614 | if (!EVP_CipherInit_ex(ctx, expected->cipher, NULL, NULL, NULL, enc)) { |
6c5943c9 | 615 | t->err = "CIPHERINIT_ERROR"; |
307e3978 | 616 | goto err; |
6c5943c9 | 617 | } |
e3d378bc AP |
618 | if (expected->iv) { |
619 | if (expected->aead) { | |
2207ba7b | 620 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN, |
e3d378bc | 621 | expected->iv_len, 0)) { |
6c5943c9 | 622 | t->err = "INVALID_IV_LENGTH"; |
307e3978 | 623 | goto err; |
6c5943c9 | 624 | } |
e3d378bc | 625 | } else if (expected->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx)) { |
6c5943c9 | 626 | t->err = "INVALID_IV_LENGTH"; |
307e3978 | 627 | goto err; |
6c5943c9 | 628 | } |
0f113f3e | 629 | } |
e3d378bc | 630 | if (expected->aead) { |
307e3978 DSH |
631 | unsigned char *tag; |
632 | /* | |
2207ba7b DSH |
633 | * If encrypting or OCB just set tag length initially, otherwise |
634 | * set tag length and value. | |
307e3978 | 635 | */ |
67c81ec3 | 636 | if (enc || expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late) { |
6c5943c9 | 637 | t->err = "TAG_LENGTH_SET_ERROR"; |
307e3978 | 638 | tag = NULL; |
0f113f3e | 639 | } else { |
6c5943c9 | 640 | t->err = "TAG_SET_ERROR"; |
e3d378bc | 641 | tag = expected->tag; |
0f113f3e | 642 | } |
e3d378bc | 643 | if (tag || expected->aead != EVP_CIPH_GCM_MODE) { |
2207ba7b | 644 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, |
e3d378bc | 645 | expected->tag_len, tag)) |
307e3978 | 646 | goto err; |
0f113f3e | 647 | } |
307e3978 | 648 | } |
0f113f3e | 649 | |
e3d378bc | 650 | if (!EVP_CIPHER_CTX_set_key_length(ctx, expected->key_len)) { |
6c5943c9 | 651 | t->err = "INVALID_KEY_LENGTH"; |
307e3978 | 652 | goto err; |
6c5943c9 | 653 | } |
e3d378bc | 654 | if (!EVP_CipherInit_ex(ctx, NULL, NULL, expected->key, expected->iv, -1)) { |
6c5943c9 | 655 | t->err = "KEY_SET_ERROR"; |
307e3978 | 656 | goto err; |
6c5943c9 | 657 | } |
48ebde22 RL |
658 | /* Check that we get the same IV back */ |
659 | if (expected->iv != NULL | |
660 | && (EVP_CIPHER_flags(expected->cipher) & EVP_CIPH_CUSTOM_IV) == 0 | |
661 | && !TEST_mem_eq(expected->iv, expected->iv_len, | |
662 | EVP_CIPHER_CTX_iv(ctx), expected->iv_len)) { | |
663 | t->err = "INVALID_IV"; | |
664 | goto err; | |
665 | } | |
307e3978 | 666 | |
e3d378bc | 667 | if (expected->aead == EVP_CIPH_CCM_MODE) { |
307e3978 | 668 | if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) { |
6c5943c9 | 669 | t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR"; |
307e3978 | 670 | goto err; |
0f113f3e MC |
671 | } |
672 | } | |
b1ceb439 | 673 | if (expected->aad[0] != NULL) { |
6c5943c9 | 674 | t->err = "AAD_SET_ERROR"; |
0b96d77a | 675 | if (!frag) { |
b1ceb439 TS |
676 | for (i = 0; expected->aad[i] != NULL; i++) { |
677 | if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], | |
678 | expected->aad_len[i])) | |
679 | goto err; | |
680 | } | |
0b96d77a MC |
681 | } else { |
682 | /* | |
683 | * Supply the AAD in chunks less than the block size where possible | |
684 | */ | |
b1ceb439 TS |
685 | for (i = 0; expected->aad[i] != NULL; i++) { |
686 | if (expected->aad_len[i] > 0) { | |
687 | if (!EVP_CipherUpdate(ctx, NULL, &chunklen, expected->aad[i], 1)) | |
688 | goto err; | |
689 | donelen++; | |
690 | } | |
691 | if (expected->aad_len[i] > 2) { | |
692 | if (!EVP_CipherUpdate(ctx, NULL, &chunklen, | |
693 | expected->aad[i] + donelen, | |
694 | expected->aad_len[i] - 2)) | |
695 | goto err; | |
696 | donelen += expected->aad_len[i] - 2; | |
697 | } | |
698 | if (expected->aad_len[i] > 1 | |
699 | && !EVP_CipherUpdate(ctx, NULL, &chunklen, | |
700 | expected->aad[i] + donelen, 1)) | |
0b96d77a | 701 | goto err; |
0b96d77a | 702 | } |
307e3978 DSH |
703 | } |
704 | } | |
67c81ec3 TN |
705 | |
706 | if (!enc && (expected->aead == EVP_CIPH_OCB_MODE || expected->tag_late)) { | |
707 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG, | |
708 | expected->tag_len, expected->tag)) { | |
709 | t->err = "TAG_SET_ERROR"; | |
710 | goto err; | |
711 | } | |
712 | } | |
713 | ||
307e3978 | 714 | EVP_CIPHER_CTX_set_padding(ctx, 0); |
6c5943c9 | 715 | t->err = "CIPHERUPDATE_ERROR"; |
0b96d77a | 716 | tmplen = 0; |
0b96d77a MC |
717 | if (!frag) { |
718 | /* We supply the data all in one go */ | |
719 | if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len)) | |
720 | goto err; | |
721 | } else { | |
722 | /* Supply the data in chunks less than the block size where possible */ | |
723 | if (in_len > 0) { | |
724 | if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1)) | |
725 | goto err; | |
726 | tmplen += chunklen; | |
ef055ec5 MC |
727 | in++; |
728 | in_len--; | |
0b96d77a | 729 | } |
ef055ec5 | 730 | if (in_len > 1) { |
0b96d77a | 731 | if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen, |
ef055ec5 | 732 | in, in_len - 1)) |
0b96d77a MC |
733 | goto err; |
734 | tmplen += chunklen; | |
ef055ec5 MC |
735 | in += in_len - 1; |
736 | in_len = 1; | |
0b96d77a | 737 | } |
ef055ec5 | 738 | if (in_len > 0 ) { |
0b96d77a | 739 | if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen, |
ef055ec5 | 740 | in, 1)) |
0b96d77a MC |
741 | goto err; |
742 | tmplen += chunklen; | |
743 | } | |
744 | } | |
6c5943c9 RS |
745 | if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) { |
746 | t->err = "CIPHERFINAL_ERROR"; | |
00212c66 | 747 | goto err; |
6c5943c9 | 748 | } |
4cceb185 P |
749 | if (!memory_err_compare(t, "VALUE_MISMATCH", expected_out, out_len, |
750 | tmp + out_misalign, tmplen + tmpflen)) | |
307e3978 | 751 | goto err; |
e3d378bc | 752 | if (enc && expected->aead) { |
307e3978 | 753 | unsigned char rtag[16]; |
6c5943c9 | 754 | |
e3d378bc | 755 | if (!TEST_size_t_le(expected->tag_len, sizeof(rtag))) { |
6c5943c9 | 756 | t->err = "TAG_LENGTH_INTERNAL_ERROR"; |
307e3978 DSH |
757 | goto err; |
758 | } | |
2207ba7b | 759 | if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG, |
e3d378bc | 760 | expected->tag_len, rtag)) { |
6c5943c9 | 761 | t->err = "TAG_RETRIEVE_ERROR"; |
307e3978 DSH |
762 | goto err; |
763 | } | |
4cceb185 P |
764 | if (!memory_err_compare(t, "TAG_VALUE_MISMATCH", |
765 | expected->tag, expected->tag_len, | |
766 | rtag, expected->tag_len)) | |
307e3978 | 767 | goto err; |
307e3978 | 768 | } |
6c5943c9 RS |
769 | t->err = NULL; |
770 | ok = 1; | |
307e3978 | 771 | err: |
b548a1f1 | 772 | OPENSSL_free(tmp); |
307e3978 | 773 | EVP_CIPHER_CTX_free(ctx); |
6c5943c9 | 774 | return ok; |
307e3978 | 775 | } |
0e360199 | 776 | |
6c5943c9 | 777 | static int cipher_test_run(EVP_TEST *t) |
307e3978 | 778 | { |
6c5943c9 | 779 | CIPHER_DATA *cdat = t->data; |
0b96d77a | 780 | int rv, frag = 0; |
9a2d2fb3 AP |
781 | size_t out_misalign, inp_misalign; |
782 | ||
307e3978 DSH |
783 | if (!cdat->key) { |
784 | t->err = "NO_KEY"; | |
785 | return 0; | |
786 | } | |
787 | if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) { | |
788 | /* IV is optional and usually omitted in wrap mode */ | |
789 | if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) { | |
790 | t->err = "NO_IV"; | |
791 | return 0; | |
792 | } | |
793 | } | |
794 | if (cdat->aead && !cdat->tag) { | |
795 | t->err = "NO_TAG"; | |
796 | return 0; | |
797 | } | |
0b96d77a | 798 | for (out_misalign = 0; out_misalign <= 1;) { |
9a2d2fb3 AP |
799 | static char aux_err[64]; |
800 | t->aux_err = aux_err; | |
ff715da4 AP |
801 | for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) { |
802 | if (inp_misalign == (size_t)-1) { | |
803 | /* kludge: inp_misalign == -1 means "exercise in-place" */ | |
0b96d77a MC |
804 | BIO_snprintf(aux_err, sizeof(aux_err), |
805 | "%s in-place, %sfragmented", | |
806 | out_misalign ? "misaligned" : "aligned", | |
807 | frag ? "" : "not "); | |
ff715da4 | 808 | } else { |
0b96d77a MC |
809 | BIO_snprintf(aux_err, sizeof(aux_err), |
810 | "%s output and %s input, %sfragmented", | |
ff715da4 | 811 | out_misalign ? "misaligned" : "aligned", |
0b96d77a MC |
812 | inp_misalign ? "misaligned" : "aligned", |
813 | frag ? "" : "not "); | |
ff715da4 | 814 | } |
9a2d2fb3 | 815 | if (cdat->enc) { |
0b96d77a | 816 | rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag); |
9a2d2fb3 AP |
817 | /* Not fatal errors: return */ |
818 | if (rv != 1) { | |
819 | if (rv < 0) | |
820 | return 0; | |
821 | return 1; | |
822 | } | |
823 | } | |
824 | if (cdat->enc != 1) { | |
0b96d77a | 825 | rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag); |
9a2d2fb3 AP |
826 | /* Not fatal errors: return */ |
827 | if (rv != 1) { | |
828 | if (rv < 0) | |
829 | return 0; | |
830 | return 1; | |
831 | } | |
832 | } | |
307e3978 | 833 | } |
0b96d77a MC |
834 | |
835 | if (out_misalign == 1 && frag == 0) { | |
836 | /* | |
b1ceb439 | 837 | * XTS, SIV, CCM and Wrap modes have special requirements about input |
0b96d77a MC |
838 | * lengths so we don't fragment for those |
839 | */ | |
840 | if (cdat->aead == EVP_CIPH_CCM_MODE | |
b1ceb439 | 841 | || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_SIV_MODE |
0b96d77a | 842 | || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE |
e3d378bc | 843 | || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE) |
0b96d77a MC |
844 | break; |
845 | out_misalign = 0; | |
846 | frag++; | |
847 | } else { | |
848 | out_misalign++; | |
849 | } | |
307e3978 | 850 | } |
9a2d2fb3 AP |
851 | t->aux_err = NULL; |
852 | ||
307e3978 | 853 | return 1; |
0f113f3e | 854 | } |
307e3978 | 855 | |
6c5943c9 | 856 | static const EVP_TEST_METHOD cipher_test_method = { |
307e3978 DSH |
857 | "Cipher", |
858 | cipher_test_init, | |
859 | cipher_test_cleanup, | |
860 | cipher_test_parse, | |
861 | cipher_test_run | |
862 | }; | |
83251f39 | 863 | |
c49e0b04 RS |
864 | |
865 | /** | |
866 | *** MAC TESTS | |
867 | **/ | |
868 | ||
6c5943c9 | 869 | typedef struct mac_data_st { |
2bdb4af5 | 870 | /* MAC type in one form or another */ |
25446a66 | 871 | EVP_MAC *mac; /* for mac_test_run_mac */ |
2bdb4af5 | 872 | int type; /* for mac_test_run_pkey */ |
83251f39 DSH |
873 | /* Algorithm string for this MAC */ |
874 | char *alg; | |
875 | /* MAC key */ | |
876 | unsigned char *key; | |
877 | size_t key_len; | |
afc580b9 P |
878 | /* MAC IV (GMAC) */ |
879 | unsigned char *iv; | |
880 | size_t iv_len; | |
83251f39 DSH |
881 | /* Input to MAC */ |
882 | unsigned char *input; | |
883 | size_t input_len; | |
884 | /* Expected output */ | |
885 | unsigned char *output; | |
886 | size_t output_len; | |
6e624a64 SL |
887 | unsigned char *custom; |
888 | size_t custom_len; | |
b215db23 AS |
889 | /* MAC salt (blake2) */ |
890 | unsigned char *salt; | |
891 | size_t salt_len; | |
7e6a3025 RL |
892 | /* Collection of controls */ |
893 | STACK_OF(OPENSSL_STRING) *controls; | |
6c5943c9 | 894 | } MAC_DATA; |
83251f39 | 895 | |
6c5943c9 | 896 | static int mac_test_init(EVP_TEST *t, const char *alg) |
83251f39 | 897 | { |
25446a66 | 898 | EVP_MAC *mac = NULL; |
2bdb4af5 | 899 | int type = NID_undef; |
6c5943c9 RS |
900 | MAC_DATA *mdat; |
901 | ||
25446a66 | 902 | if ((mac = EVP_MAC_fetch(NULL, alg, NULL)) == NULL) { |
2bdb4af5 RL |
903 | /* |
904 | * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods | |
905 | * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running | |
906 | * the EVP_PKEY method. | |
907 | */ | |
908 | size_t sz = strlen(alg); | |
909 | static const char epilogue[] = " by EVP_PKEY"; | |
910 | ||
88e3cf0a RL |
911 | if (sz >= sizeof(epilogue) |
912 | && strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0) | |
2bdb4af5 RL |
913 | sz -= sizeof(epilogue) - 1; |
914 | ||
915 | if (strncmp(alg, "HMAC", sz) == 0) { | |
916 | type = EVP_PKEY_HMAC; | |
917 | } else if (strncmp(alg, "CMAC", sz) == 0) { | |
b4a3aeeb | 918 | #ifndef OPENSSL_NO_CMAC |
2bdb4af5 | 919 | type = EVP_PKEY_CMAC; |
b4a3aeeb | 920 | #else |
2bdb4af5 RL |
921 | t->skip = 1; |
922 | return 1; | |
52ad5b60 | 923 | #endif |
2bdb4af5 | 924 | } else if (strncmp(alg, "Poly1305", sz) == 0) { |
52ad5b60 | 925 | #ifndef OPENSSL_NO_POLY1305 |
2bdb4af5 | 926 | type = EVP_PKEY_POLY1305; |
52ad5b60 | 927 | #else |
2bdb4af5 RL |
928 | t->skip = 1; |
929 | return 1; | |
3f5616d7 | 930 | #endif |
2bdb4af5 | 931 | } else if (strncmp(alg, "SipHash", sz) == 0) { |
3f5616d7 | 932 | #ifndef OPENSSL_NO_SIPHASH |
2bdb4af5 | 933 | type = EVP_PKEY_SIPHASH; |
3f5616d7 | 934 | #else |
2bdb4af5 RL |
935 | t->skip = 1; |
936 | return 1; | |
b4a3aeeb | 937 | #endif |
2bdb4af5 RL |
938 | } else { |
939 | /* | |
940 | * Not a known EVP_PKEY method either. If it's a known OID, then | |
941 | * assume it's been disabled. | |
942 | */ | |
943 | if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) { | |
944 | t->skip = 1; | |
945 | return 1; | |
946 | } | |
947 | ||
948 | return 0; | |
949 | } | |
950 | } | |
83251f39 | 951 | |
6c5943c9 | 952 | mdat = OPENSSL_zalloc(sizeof(*mdat)); |
83251f39 | 953 | mdat->type = type; |
2bdb4af5 | 954 | mdat->mac = mac; |
7e6a3025 | 955 | mdat->controls = sk_OPENSSL_STRING_new_null(); |
83251f39 DSH |
956 | t->data = mdat; |
957 | return 1; | |
958 | } | |
959 | ||
7e6a3025 RL |
960 | /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */ |
961 | static void openssl_free(char *m) | |
962 | { | |
963 | OPENSSL_free(m); | |
964 | } | |
965 | ||
6c5943c9 | 966 | static void mac_test_cleanup(EVP_TEST *t) |
83251f39 | 967 | { |
6c5943c9 RS |
968 | MAC_DATA *mdat = t->data; |
969 | ||
25446a66 | 970 | EVP_MAC_free(mdat->mac); |
7e6a3025 | 971 | sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free); |
6c5943c9 RS |
972 | OPENSSL_free(mdat->alg); |
973 | OPENSSL_free(mdat->key); | |
afc580b9 | 974 | OPENSSL_free(mdat->iv); |
6e624a64 | 975 | OPENSSL_free(mdat->custom); |
b215db23 | 976 | OPENSSL_free(mdat->salt); |
6c5943c9 RS |
977 | OPENSSL_free(mdat->input); |
978 | OPENSSL_free(mdat->output); | |
83251f39 DSH |
979 | } |
980 | ||
6c5943c9 | 981 | static int mac_test_parse(EVP_TEST *t, |
83251f39 DSH |
982 | const char *keyword, const char *value) |
983 | { | |
6c5943c9 RS |
984 | MAC_DATA *mdata = t->data; |
985 | ||
86885c28 | 986 | if (strcmp(keyword, "Key") == 0) |
c49e0b04 | 987 | return parse_bin(value, &mdata->key, &mdata->key_len); |
afc580b9 P |
988 | if (strcmp(keyword, "IV") == 0) |
989 | return parse_bin(value, &mdata->iv, &mdata->iv_len); | |
6e624a64 SL |
990 | if (strcmp(keyword, "Custom") == 0) |
991 | return parse_bin(value, &mdata->custom, &mdata->custom_len); | |
b215db23 AS |
992 | if (strcmp(keyword, "Salt") == 0) |
993 | return parse_bin(value, &mdata->salt, &mdata->salt_len); | |
86885c28 | 994 | if (strcmp(keyword, "Algorithm") == 0) { |
7644a9ae | 995 | mdata->alg = OPENSSL_strdup(value); |
83251f39 DSH |
996 | if (!mdata->alg) |
997 | return 0; | |
998 | return 1; | |
999 | } | |
86885c28 | 1000 | if (strcmp(keyword, "Input") == 0) |
c49e0b04 | 1001 | return parse_bin(value, &mdata->input, &mdata->input_len); |
86885c28 | 1002 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 1003 | return parse_bin(value, &mdata->output, &mdata->output_len); |
7e6a3025 RL |
1004 | if (strcmp(keyword, "Ctrl") == 0) |
1005 | return sk_OPENSSL_STRING_push(mdata->controls, | |
1006 | OPENSSL_strdup(value)) != 0; | |
83251f39 DSH |
1007 | return 0; |
1008 | } | |
1009 | ||
ce5d64c7 RL |
1010 | static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx, |
1011 | const char *value) | |
1012 | { | |
1013 | int rv; | |
1014 | char *p, *tmpval; | |
1015 | ||
1016 | if (!TEST_ptr(tmpval = OPENSSL_strdup(value))) | |
1017 | return 0; | |
1018 | p = strchr(tmpval, ':'); | |
1019 | if (p != NULL) | |
1020 | *p++ = '\0'; | |
1021 | rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p); | |
1022 | if (rv == -2) | |
1023 | t->err = "PKEY_CTRL_INVALID"; | |
1024 | else if (rv <= 0) | |
1025 | t->err = "PKEY_CTRL_ERROR"; | |
1026 | else | |
1027 | rv = 1; | |
1028 | OPENSSL_free(tmpval); | |
1029 | return rv > 0; | |
1030 | } | |
1031 | ||
2bdb4af5 | 1032 | static int mac_test_run_pkey(EVP_TEST *t) |
83251f39 | 1033 | { |
e3d378bc | 1034 | MAC_DATA *expected = t->data; |
83251f39 DSH |
1035 | EVP_MD_CTX *mctx = NULL; |
1036 | EVP_PKEY_CTX *pctx = NULL, *genctx = NULL; | |
1037 | EVP_PKEY *key = NULL; | |
1038 | const EVP_MD *md = NULL; | |
e3d378bc AP |
1039 | unsigned char *got = NULL; |
1040 | size_t got_len; | |
7e6a3025 | 1041 | int i; |
83251f39 | 1042 | |
2bdb4af5 RL |
1043 | if (expected->alg == NULL) |
1044 | TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type)); | |
1045 | else | |
1046 | TEST_info("Trying the EVP_PKEY %s test with %s", | |
1047 | OBJ_nid2sn(expected->type), expected->alg); | |
1048 | ||
96bea000 | 1049 | #ifdef OPENSSL_NO_DES |
e3d378bc | 1050 | if (expected->alg != NULL && strstr(expected->alg, "DES") != NULL) { |
96bea000 | 1051 | /* Skip DES */ |
6c5943c9 | 1052 | t->err = NULL; |
96bea000 MC |
1053 | goto err; |
1054 | } | |
1055 | #endif | |
1056 | ||
9442c8d7 MC |
1057 | if (expected->type == EVP_PKEY_CMAC) |
1058 | key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len, | |
1059 | EVP_get_cipherbyname(expected->alg)); | |
1060 | else | |
f929439f MC |
1061 | key = EVP_PKEY_new_raw_private_key(expected->type, NULL, expected->key, |
1062 | expected->key_len); | |
9442c8d7 MC |
1063 | if (key == NULL) { |
1064 | t->err = "MAC_KEY_CREATE_ERROR"; | |
83251f39 | 1065 | goto err; |
6c5943c9 | 1066 | } |
83251f39 | 1067 | |
e3d378bc AP |
1068 | if (expected->type == EVP_PKEY_HMAC) { |
1069 | if (!TEST_ptr(md = EVP_get_digestbyname(expected->alg))) { | |
6c5943c9 | 1070 | t->err = "MAC_ALGORITHM_SET_ERROR"; |
83251f39 | 1071 | goto err; |
6c5943c9 | 1072 | } |
83251f39 | 1073 | } |
6c5943c9 RS |
1074 | if (!TEST_ptr(mctx = EVP_MD_CTX_new())) { |
1075 | t->err = "INTERNAL_ERROR"; | |
83251f39 | 1076 | goto err; |
6c5943c9 RS |
1077 | } |
1078 | if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key)) { | |
1079 | t->err = "DIGESTSIGNINIT_ERROR"; | |
83251f39 | 1080 | goto err; |
6c5943c9 | 1081 | } |
7e6a3025 | 1082 | for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) |
ce5d64c7 RL |
1083 | if (!mac_test_ctrl_pkey(t, pctx, |
1084 | sk_OPENSSL_STRING_value(expected->controls, | |
1085 | i))) { | |
7e6a3025 RL |
1086 | t->err = "EVPPKEYCTXCTRL_ERROR"; |
1087 | goto err; | |
1088 | } | |
e3d378bc | 1089 | if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) { |
6c5943c9 | 1090 | t->err = "DIGESTSIGNUPDATE_ERROR"; |
83251f39 | 1091 | goto err; |
83251f39 | 1092 | } |
e3d378bc | 1093 | if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) { |
6c5943c9 | 1094 | t->err = "DIGESTSIGNFINAL_LENGTH_ERROR"; |
83251f39 | 1095 | goto err; |
6c5943c9 | 1096 | } |
e3d378bc | 1097 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { |
6c5943c9 | 1098 | t->err = "TEST_FAILURE"; |
83251f39 | 1099 | goto err; |
6c5943c9 | 1100 | } |
e3d378bc | 1101 | if (!EVP_DigestSignFinal(mctx, got, &got_len) |
4cceb185 P |
1102 | || !memory_err_compare(t, "TEST_MAC_ERR", |
1103 | expected->output, expected->output_len, | |
1104 | got, got_len)) { | |
41248607 RS |
1105 | t->err = "TEST_MAC_ERR"; |
1106 | goto err; | |
1107 | } | |
6c5943c9 | 1108 | t->err = NULL; |
83251f39 | 1109 | err: |
bfb0641f | 1110 | EVP_MD_CTX_free(mctx); |
e3d378bc | 1111 | OPENSSL_free(got); |
c5ba2d99 RS |
1112 | EVP_PKEY_CTX_free(genctx); |
1113 | EVP_PKEY_free(key); | |
83251f39 DSH |
1114 | return 1; |
1115 | } | |
1116 | ||
2bdb4af5 RL |
1117 | static int mac_test_run_mac(EVP_TEST *t) |
1118 | { | |
1119 | MAC_DATA *expected = t->data; | |
1120 | EVP_MAC_CTX *ctx = NULL; | |
2bdb4af5 RL |
1121 | unsigned char *got = NULL; |
1122 | size_t got_len; | |
25446a66 RL |
1123 | int i; |
1124 | OSSL_PARAM params[21]; | |
1125 | size_t params_n = 0; | |
1126 | size_t params_n_allocstart = 0; | |
1127 | const OSSL_PARAM *defined_params = | |
1128 | EVP_MAC_CTX_settable_params(expected->mac); | |
2bdb4af5 RL |
1129 | |
1130 | if (expected->alg == NULL) | |
1131 | TEST_info("Trying the EVP_MAC %s test", EVP_MAC_name(expected->mac)); | |
1132 | else | |
1133 | TEST_info("Trying the EVP_MAC %s test with %s", | |
1134 | EVP_MAC_name(expected->mac), expected->alg); | |
1135 | ||
1136 | #ifdef OPENSSL_NO_DES | |
1137 | if (expected->alg != NULL && strstr(expected->alg, "DES") != NULL) { | |
1138 | /* Skip DES */ | |
1139 | t->err = NULL; | |
1140 | goto err; | |
1141 | } | |
1142 | #endif | |
1143 | ||
25446a66 RL |
1144 | if (expected->alg != NULL) |
1145 | params[params_n++] = | |
1146 | OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_ALGORITHM, | |
1147 | expected->alg, | |
1148 | strlen(expected->alg) + 1); | |
1149 | if (expected->key != NULL) | |
1150 | params[params_n++] = | |
1151 | OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_KEY, | |
1152 | expected->key, | |
1153 | expected->key_len); | |
1154 | if (expected->custom != NULL) | |
1155 | params[params_n++] = | |
1156 | OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_CUSTOM, | |
1157 | expected->custom, | |
1158 | expected->custom_len); | |
1159 | if (expected->salt != NULL) | |
1160 | params[params_n++] = | |
1161 | OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_SALT, | |
1162 | expected->salt, | |
1163 | expected->salt_len); | |
1164 | if (expected->iv != NULL) | |
1165 | params[params_n++] = | |
1166 | OSSL_PARAM_construct_octet_string(OSSL_MAC_PARAM_IV, | |
1167 | expected->iv, | |
1168 | expected->iv_len); | |
1169 | ||
1170 | /* | |
1171 | * Unknown controls. They must match parameters that the MAC recognises | |
1172 | */ | |
1173 | if (params_n + sk_OPENSSL_STRING_num(expected->controls) | |
1174 | >= OSSL_NELEM(params)) { | |
1175 | t->err = "MAC_TOO_MANY_PARAMETERS"; | |
2bdb4af5 RL |
1176 | goto err; |
1177 | } | |
25446a66 RL |
1178 | params_n_allocstart = params_n; |
1179 | for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) { | |
1180 | char *tmpkey, *tmpval; | |
1181 | char *value = sk_OPENSSL_STRING_value(expected->controls, i); | |
2bdb4af5 | 1182 | |
25446a66 RL |
1183 | if (!TEST_ptr(tmpkey = OPENSSL_strdup(value))) { |
1184 | t->err = "MAC_PARAM_ERROR"; | |
2bdb4af5 RL |
1185 | goto err; |
1186 | } | |
25446a66 RL |
1187 | tmpval = strchr(tmpkey, ':'); |
1188 | if (tmpval != NULL) | |
1189 | *tmpval++ = '\0'; | |
1190 | ||
1191 | if (!OSSL_PARAM_allocate_from_text(¶ms[params_n], defined_params, | |
1192 | tmpkey, tmpval, | |
1193 | strlen(tmpval))) { | |
1194 | OPENSSL_free(tmpkey); | |
1195 | t->err = "MAC_PARAM_ERROR"; | |
6e624a64 SL |
1196 | goto err; |
1197 | } | |
25446a66 | 1198 | params_n++; |
2bdb4af5 | 1199 | |
25446a66 | 1200 | OPENSSL_free(tmpkey); |
b215db23 | 1201 | } |
25446a66 | 1202 | params[params_n] = OSSL_PARAM_construct_end(); |
b215db23 | 1203 | |
25446a66 RL |
1204 | if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) { |
1205 | t->err = "MAC_CREATE_ERROR"; | |
1206 | goto err; | |
afc580b9 P |
1207 | } |
1208 | ||
25446a66 RL |
1209 | if (!EVP_MAC_CTX_set_params(ctx, params)) { |
1210 | t->err = "MAC_BAD_PARAMS"; | |
1211 | goto err; | |
2bdb4af5 | 1212 | } |
b215db23 AS |
1213 | if (!EVP_MAC_init(ctx)) { |
1214 | t->err = "MAC_INIT_ERROR"; | |
1215 | goto err; | |
1216 | } | |
2bdb4af5 RL |
1217 | if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) { |
1218 | t->err = "MAC_UPDATE_ERROR"; | |
1219 | goto err; | |
1220 | } | |
25446a66 | 1221 | if (!EVP_MAC_final(ctx, NULL, &got_len, 0)) { |
2bdb4af5 RL |
1222 | t->err = "MAC_FINAL_LENGTH_ERROR"; |
1223 | goto err; | |
1224 | } | |
1225 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { | |
1226 | t->err = "TEST_FAILURE"; | |
1227 | goto err; | |
1228 | } | |
25446a66 | 1229 | if (!EVP_MAC_final(ctx, got, &got_len, got_len) |
2bdb4af5 RL |
1230 | || !memory_err_compare(t, "TEST_MAC_ERR", |
1231 | expected->output, expected->output_len, | |
1232 | got, got_len)) { | |
1233 | t->err = "TEST_MAC_ERR"; | |
1234 | goto err; | |
1235 | } | |
1236 | t->err = NULL; | |
1237 | err: | |
25446a66 RL |
1238 | while (params_n-- > params_n_allocstart) { |
1239 | OPENSSL_free(params[params_n].data); | |
1240 | } | |
2bdb4af5 RL |
1241 | EVP_MAC_CTX_free(ctx); |
1242 | OPENSSL_free(got); | |
1243 | return 1; | |
1244 | } | |
1245 | ||
1246 | static int mac_test_run(EVP_TEST *t) | |
1247 | { | |
1248 | MAC_DATA *expected = t->data; | |
1249 | ||
1250 | if (expected->mac != NULL) | |
1251 | return mac_test_run_mac(t); | |
1252 | return mac_test_run_pkey(t); | |
1253 | } | |
1254 | ||
6c5943c9 | 1255 | static const EVP_TEST_METHOD mac_test_method = { |
83251f39 DSH |
1256 | "MAC", |
1257 | mac_test_init, | |
1258 | mac_test_cleanup, | |
1259 | mac_test_parse, | |
1260 | mac_test_run | |
1261 | }; | |
5824cc29 | 1262 | |
c49e0b04 RS |
1263 | |
1264 | /** | |
1265 | *** PUBLIC KEY TESTS | |
1266 | *** These are all very similar and share much common code. | |
1267 | **/ | |
5824cc29 | 1268 | |
6c5943c9 | 1269 | typedef struct pkey_data_st { |
5824cc29 DSH |
1270 | /* Context for this operation */ |
1271 | EVP_PKEY_CTX *ctx; | |
1272 | /* Key operation to perform */ | |
1273 | int (*keyop) (EVP_PKEY_CTX *ctx, | |
1274 | unsigned char *sig, size_t *siglen, | |
1275 | const unsigned char *tbs, size_t tbslen); | |
1276 | /* Input to MAC */ | |
1277 | unsigned char *input; | |
1278 | size_t input_len; | |
1279 | /* Expected output */ | |
1280 | unsigned char *output; | |
1281 | size_t output_len; | |
6c5943c9 | 1282 | } PKEY_DATA; |
5824cc29 DSH |
1283 | |
1284 | /* | |
1285 | * Perform public key operation setup: lookup key, allocated ctx and call | |
1286 | * the appropriate initialisation function | |
1287 | */ | |
6c5943c9 | 1288 | static int pkey_test_init(EVP_TEST *t, const char *name, |
5824cc29 DSH |
1289 | int use_public, |
1290 | int (*keyopinit) (EVP_PKEY_CTX *ctx), | |
c49e0b04 RS |
1291 | int (*keyop)(EVP_PKEY_CTX *ctx, |
1292 | unsigned char *sig, size_t *siglen, | |
1293 | const unsigned char *tbs, | |
1294 | size_t tbslen)) | |
5824cc29 | 1295 | { |
6c5943c9 | 1296 | PKEY_DATA *kdata; |
5824cc29 | 1297 | EVP_PKEY *pkey = NULL; |
7a6c9792 | 1298 | int rv = 0; |
6c5943c9 | 1299 | |
7a6c9792 | 1300 | if (use_public) |
6c5943c9 RS |
1301 | rv = find_key(&pkey, name, public_keys); |
1302 | if (rv == 0) | |
1303 | rv = find_key(&pkey, name, private_keys); | |
1304 | if (rv == 0 || pkey == NULL) { | |
7a6c9792 DSH |
1305 | t->skip = 1; |
1306 | return 1; | |
1307 | } | |
1308 | ||
c49e0b04 | 1309 | if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) { |
7a6c9792 | 1310 | EVP_PKEY_free(pkey); |
5824cc29 | 1311 | return 0; |
7a6c9792 | 1312 | } |
5824cc29 | 1313 | kdata->keyop = keyop; |
9e206ce5 P |
1314 | if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL))) { |
1315 | EVP_PKEY_free(pkey); | |
1316 | OPENSSL_free(kdata); | |
5824cc29 | 1317 | return 0; |
9e206ce5 | 1318 | } |
5824cc29 | 1319 | if (keyopinit(kdata->ctx) <= 0) |
cce65266 | 1320 | t->err = "KEYOP_INIT_ERROR"; |
c49e0b04 | 1321 | t->data = kdata; |
5824cc29 DSH |
1322 | return 1; |
1323 | } | |
1324 | ||
6c5943c9 | 1325 | static void pkey_test_cleanup(EVP_TEST *t) |
5824cc29 | 1326 | { |
6c5943c9 | 1327 | PKEY_DATA *kdata = t->data; |
b548a1f1 RS |
1328 | |
1329 | OPENSSL_free(kdata->input); | |
1330 | OPENSSL_free(kdata->output); | |
c5ba2d99 | 1331 | EVP_PKEY_CTX_free(kdata->ctx); |
5824cc29 DSH |
1332 | } |
1333 | ||
6c5943c9 | 1334 | static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx, |
dfbdf4ab | 1335 | const char *value) |
4ddd5ace DSH |
1336 | { |
1337 | int rv; | |
1338 | char *p, *tmpval; | |
1339 | ||
6c5943c9 | 1340 | if (!TEST_ptr(tmpval = OPENSSL_strdup(value))) |
4ddd5ace DSH |
1341 | return 0; |
1342 | p = strchr(tmpval, ':'); | |
1343 | if (p != NULL) | |
c49e0b04 | 1344 | *p++ = '\0'; |
4ddd5ace | 1345 | rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p); |
cce65266 DSH |
1346 | if (rv == -2) { |
1347 | t->err = "PKEY_CTRL_INVALID"; | |
1348 | rv = 1; | |
1349 | } else if (p != NULL && rv <= 0) { | |
1350 | /* If p has an OID and lookup fails assume disabled algorithm */ | |
1351 | int nid = OBJ_sn2nid(p); | |
6c5943c9 | 1352 | |
cce65266 DSH |
1353 | if (nid == NID_undef) |
1354 | nid = OBJ_ln2nid(p); | |
c49e0b04 RS |
1355 | if (nid != NID_undef |
1356 | && EVP_get_digestbynid(nid) == NULL | |
1357 | && EVP_get_cipherbynid(nid) == NULL) { | |
dfbdf4ab RL |
1358 | t->skip = 1; |
1359 | rv = 1; | |
cce65266 DSH |
1360 | } else { |
1361 | t->err = "PKEY_CTRL_ERROR"; | |
1362 | rv = 1; | |
dfbdf4ab RL |
1363 | } |
1364 | } | |
4ddd5ace DSH |
1365 | OPENSSL_free(tmpval); |
1366 | return rv > 0; | |
1367 | } | |
1368 | ||
6c5943c9 | 1369 | static int pkey_test_parse(EVP_TEST *t, |
5824cc29 DSH |
1370 | const char *keyword, const char *value) |
1371 | { | |
6c5943c9 | 1372 | PKEY_DATA *kdata = t->data; |
86885c28 | 1373 | if (strcmp(keyword, "Input") == 0) |
c49e0b04 | 1374 | return parse_bin(value, &kdata->input, &kdata->input_len); |
86885c28 | 1375 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 1376 | return parse_bin(value, &kdata->output, &kdata->output_len); |
4ddd5ace | 1377 | if (strcmp(keyword, "Ctrl") == 0) |
dfbdf4ab | 1378 | return pkey_test_ctrl(t, kdata->ctx, value); |
5824cc29 DSH |
1379 | return 0; |
1380 | } | |
1381 | ||
6c5943c9 | 1382 | static int pkey_test_run(EVP_TEST *t) |
5824cc29 | 1383 | { |
e3d378bc AP |
1384 | PKEY_DATA *expected = t->data; |
1385 | unsigned char *got = NULL; | |
1386 | size_t got_len; | |
d7fcf1fe | 1387 | EVP_PKEY_CTX *copy = NULL; |
6c5943c9 | 1388 | |
e3d378bc AP |
1389 | if (expected->keyop(expected->ctx, NULL, &got_len, |
1390 | expected->input, expected->input_len) <= 0 | |
1391 | || !TEST_ptr(got = OPENSSL_malloc(got_len))) { | |
6c5943c9 | 1392 | t->err = "KEYOP_LENGTH_ERROR"; |
5824cc29 | 1393 | goto err; |
6c5943c9 | 1394 | } |
e3d378bc AP |
1395 | if (expected->keyop(expected->ctx, got, &got_len, |
1396 | expected->input, expected->input_len) <= 0) { | |
6c5943c9 | 1397 | t->err = "KEYOP_ERROR"; |
5824cc29 | 1398 | goto err; |
6c5943c9 | 1399 | } |
4cceb185 P |
1400 | if (!memory_err_compare(t, "KEYOP_MISMATCH", |
1401 | expected->output, expected->output_len, | |
1402 | got, got_len)) | |
5824cc29 | 1403 | goto err; |
4cceb185 | 1404 | |
6c5943c9 | 1405 | t->err = NULL; |
d7fcf1fe DB |
1406 | OPENSSL_free(got); |
1407 | got = NULL; | |
1408 | ||
1409 | /* Repeat the test on a copy. */ | |
1410 | if (!TEST_ptr(copy = EVP_PKEY_CTX_dup(expected->ctx))) { | |
1411 | t->err = "INTERNAL_ERROR"; | |
1412 | goto err; | |
1413 | } | |
1414 | if (expected->keyop(copy, NULL, &got_len, expected->input, | |
1415 | expected->input_len) <= 0 | |
1416 | || !TEST_ptr(got = OPENSSL_malloc(got_len))) { | |
1417 | t->err = "KEYOP_LENGTH_ERROR"; | |
1418 | goto err; | |
1419 | } | |
1420 | if (expected->keyop(copy, got, &got_len, expected->input, | |
1421 | expected->input_len) <= 0) { | |
1422 | t->err = "KEYOP_ERROR"; | |
1423 | goto err; | |
1424 | } | |
1425 | if (!memory_err_compare(t, "KEYOP_MISMATCH", | |
1426 | expected->output, expected->output_len, | |
1427 | got, got_len)) | |
1428 | goto err; | |
1429 | ||
5824cc29 | 1430 | err: |
e3d378bc | 1431 | OPENSSL_free(got); |
d7fcf1fe | 1432 | EVP_PKEY_CTX_free(copy); |
5824cc29 DSH |
1433 | return 1; |
1434 | } | |
1435 | ||
6c5943c9 | 1436 | static int sign_test_init(EVP_TEST *t, const char *name) |
5824cc29 DSH |
1437 | { |
1438 | return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign); | |
1439 | } | |
1440 | ||
6c5943c9 | 1441 | static const EVP_TEST_METHOD psign_test_method = { |
5824cc29 DSH |
1442 | "Sign", |
1443 | sign_test_init, | |
1444 | pkey_test_cleanup, | |
1445 | pkey_test_parse, | |
1446 | pkey_test_run | |
1447 | }; | |
1448 | ||
6c5943c9 | 1449 | static int verify_recover_test_init(EVP_TEST *t, const char *name) |
5824cc29 DSH |
1450 | { |
1451 | return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init, | |
1452 | EVP_PKEY_verify_recover); | |
1453 | } | |
1454 | ||
6c5943c9 | 1455 | static const EVP_TEST_METHOD pverify_recover_test_method = { |
5824cc29 DSH |
1456 | "VerifyRecover", |
1457 | verify_recover_test_init, | |
1458 | pkey_test_cleanup, | |
1459 | pkey_test_parse, | |
1460 | pkey_test_run | |
1461 | }; | |
1462 | ||
6c5943c9 | 1463 | static int decrypt_test_init(EVP_TEST *t, const char *name) |
5824cc29 DSH |
1464 | { |
1465 | return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init, | |
1466 | EVP_PKEY_decrypt); | |
1467 | } | |
1468 | ||
6c5943c9 | 1469 | static const EVP_TEST_METHOD pdecrypt_test_method = { |
5824cc29 DSH |
1470 | "Decrypt", |
1471 | decrypt_test_init, | |
1472 | pkey_test_cleanup, | |
1473 | pkey_test_parse, | |
1474 | pkey_test_run | |
1475 | }; | |
1476 | ||
6c5943c9 | 1477 | static int verify_test_init(EVP_TEST *t, const char *name) |
5824cc29 DSH |
1478 | { |
1479 | return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0); | |
1480 | } | |
1481 | ||
6c5943c9 | 1482 | static int verify_test_run(EVP_TEST *t) |
5824cc29 | 1483 | { |
6c5943c9 RS |
1484 | PKEY_DATA *kdata = t->data; |
1485 | ||
5824cc29 DSH |
1486 | if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len, |
1487 | kdata->input, kdata->input_len) <= 0) | |
1488 | t->err = "VERIFY_ERROR"; | |
1489 | return 1; | |
1490 | } | |
1491 | ||
6c5943c9 | 1492 | static const EVP_TEST_METHOD pverify_test_method = { |
5824cc29 DSH |
1493 | "Verify", |
1494 | verify_test_init, | |
1495 | pkey_test_cleanup, | |
1496 | pkey_test_parse, | |
1497 | verify_test_run | |
1498 | }; | |
3b53e18a | 1499 | |
d4ad48d7 | 1500 | |
6c5943c9 | 1501 | static int pderive_test_init(EVP_TEST *t, const char *name) |
d4ad48d7 DSH |
1502 | { |
1503 | return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0); | |
1504 | } | |
1505 | ||
6c5943c9 | 1506 | static int pderive_test_parse(EVP_TEST *t, |
d4ad48d7 DSH |
1507 | const char *keyword, const char *value) |
1508 | { | |
6c5943c9 | 1509 | PKEY_DATA *kdata = t->data; |
d4ad48d7 DSH |
1510 | |
1511 | if (strcmp(keyword, "PeerKey") == 0) { | |
1512 | EVP_PKEY *peer; | |
6c5943c9 | 1513 | if (find_key(&peer, value, public_keys) == 0) |
d4ad48d7 DSH |
1514 | return 0; |
1515 | if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0) | |
1516 | return 0; | |
1517 | return 1; | |
1518 | } | |
1519 | if (strcmp(keyword, "SharedSecret") == 0) | |
c49e0b04 | 1520 | return parse_bin(value, &kdata->output, &kdata->output_len); |
4ddd5ace | 1521 | if (strcmp(keyword, "Ctrl") == 0) |
dfbdf4ab | 1522 | return pkey_test_ctrl(t, kdata->ctx, value); |
d4ad48d7 DSH |
1523 | return 0; |
1524 | } | |
1525 | ||
6c5943c9 | 1526 | static int pderive_test_run(EVP_TEST *t) |
d4ad48d7 | 1527 | { |
e3d378bc AP |
1528 | PKEY_DATA *expected = t->data; |
1529 | unsigned char *got = NULL; | |
1530 | size_t got_len; | |
d4ad48d7 | 1531 | |
9b82c8b1 DSH |
1532 | if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) { |
1533 | t->err = "DERIVE_ERROR"; | |
1534 | goto err; | |
1535 | } | |
e3d378bc | 1536 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { |
6c5943c9 | 1537 | t->err = "DERIVE_ERROR"; |
d4ad48d7 | 1538 | goto err; |
6c5943c9 | 1539 | } |
e3d378bc | 1540 | if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) { |
6c5943c9 | 1541 | t->err = "DERIVE_ERROR"; |
d4ad48d7 | 1542 | goto err; |
6c5943c9 | 1543 | } |
4cceb185 P |
1544 | if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH", |
1545 | expected->output, expected->output_len, | |
1546 | got, got_len)) | |
d4ad48d7 | 1547 | goto err; |
6c5943c9 RS |
1548 | |
1549 | t->err = NULL; | |
d4ad48d7 | 1550 | err: |
e3d378bc | 1551 | OPENSSL_free(got); |
d4ad48d7 DSH |
1552 | return 1; |
1553 | } | |
1554 | ||
6c5943c9 | 1555 | static const EVP_TEST_METHOD pderive_test_method = { |
d4ad48d7 DSH |
1556 | "Derive", |
1557 | pderive_test_init, | |
1558 | pkey_test_cleanup, | |
1559 | pderive_test_parse, | |
1560 | pderive_test_run | |
1561 | }; | |
1562 | ||
3b53e18a | 1563 | |
c49e0b04 RS |
1564 | /** |
1565 | *** PBE TESTS | |
1566 | **/ | |
1567 | ||
1568 | typedef enum pbe_type_enum { | |
1569 | PBE_TYPE_INVALID = 0, | |
1570 | PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12 | |
1571 | } PBE_TYPE; | |
3b53e18a | 1572 | |
6c5943c9 | 1573 | typedef struct pbe_data_st { |
c49e0b04 | 1574 | PBE_TYPE pbe_type; |
6c5943c9 | 1575 | /* scrypt parameters */ |
3b53e18a | 1576 | uint64_t N, r, p, maxmem; |
6c5943c9 | 1577 | /* PKCS#12 parameters */ |
351fe214 DSH |
1578 | int id, iter; |
1579 | const EVP_MD *md; | |
6c5943c9 | 1580 | /* password */ |
3b53e18a DSH |
1581 | unsigned char *pass; |
1582 | size_t pass_len; | |
6c5943c9 | 1583 | /* salt */ |
3b53e18a DSH |
1584 | unsigned char *salt; |
1585 | size_t salt_len; | |
6c5943c9 | 1586 | /* Expected output */ |
3b53e18a DSH |
1587 | unsigned char *key; |
1588 | size_t key_len; | |
6c5943c9 | 1589 | } PBE_DATA; |
3b53e18a | 1590 | |
b0809bc8 | 1591 | #ifndef OPENSSL_NO_SCRYPT |
c49e0b04 RS |
1592 | /* |
1593 | * Parse unsigned decimal 64 bit integer value | |
1594 | */ | |
1595 | static int parse_uint64(const char *value, uint64_t *pr) | |
1596 | { | |
1597 | const char *p = value; | |
1598 | ||
1599 | if (!TEST_true(*p)) { | |
1600 | TEST_info("Invalid empty integer value"); | |
1601 | return -1; | |
1602 | } | |
1603 | for (*pr = 0; *p; ) { | |
1604 | if (*pr > UINT64_MAX / 10) { | |
1605 | TEST_error("Integer overflow in string %s", value); | |
1606 | return -1; | |
1607 | } | |
1608 | *pr *= 10; | |
00dfbaad | 1609 | if (!TEST_true(isdigit((unsigned char)*p))) { |
c49e0b04 RS |
1610 | TEST_error("Invalid character in string %s", value); |
1611 | return -1; | |
1612 | } | |
1613 | *pr += *p - '0'; | |
1614 | p++; | |
1615 | } | |
1616 | return 1; | |
1617 | } | |
1618 | ||
6c5943c9 | 1619 | static int scrypt_test_parse(EVP_TEST *t, |
3b53e18a DSH |
1620 | const char *keyword, const char *value) |
1621 | { | |
6c5943c9 | 1622 | PBE_DATA *pdata = t->data; |
351fe214 | 1623 | |
3b53e18a | 1624 | if (strcmp(keyword, "N") == 0) |
c49e0b04 | 1625 | return parse_uint64(value, &pdata->N); |
3b53e18a | 1626 | if (strcmp(keyword, "p") == 0) |
c49e0b04 | 1627 | return parse_uint64(value, &pdata->p); |
3b53e18a | 1628 | if (strcmp(keyword, "r") == 0) |
c49e0b04 | 1629 | return parse_uint64(value, &pdata->r); |
3b53e18a | 1630 | if (strcmp(keyword, "maxmem") == 0) |
c49e0b04 | 1631 | return parse_uint64(value, &pdata->maxmem); |
3b53e18a DSH |
1632 | return 0; |
1633 | } | |
b0809bc8 | 1634 | #endif |
3b53e18a | 1635 | |
6c5943c9 | 1636 | static int pbkdf2_test_parse(EVP_TEST *t, |
351fe214 | 1637 | const char *keyword, const char *value) |
3b53e18a | 1638 | { |
6c5943c9 | 1639 | PBE_DATA *pdata = t->data; |
351fe214 DSH |
1640 | |
1641 | if (strcmp(keyword, "iter") == 0) { | |
1642 | pdata->iter = atoi(value); | |
1643 | if (pdata->iter <= 0) | |
c49e0b04 | 1644 | return -1; |
351fe214 DSH |
1645 | return 1; |
1646 | } | |
1647 | if (strcmp(keyword, "MD") == 0) { | |
1648 | pdata->md = EVP_get_digestbyname(value); | |
1649 | if (pdata->md == NULL) | |
c49e0b04 | 1650 | return -1; |
351fe214 DSH |
1651 | return 1; |
1652 | } | |
1653 | return 0; | |
1654 | } | |
1655 | ||
6c5943c9 | 1656 | static int pkcs12_test_parse(EVP_TEST *t, |
351fe214 DSH |
1657 | const char *keyword, const char *value) |
1658 | { | |
6c5943c9 | 1659 | PBE_DATA *pdata = t->data; |
351fe214 DSH |
1660 | |
1661 | if (strcmp(keyword, "id") == 0) { | |
1662 | pdata->id = atoi(value); | |
1663 | if (pdata->id <= 0) | |
c49e0b04 | 1664 | return -1; |
351fe214 DSH |
1665 | return 1; |
1666 | } | |
1667 | return pbkdf2_test_parse(t, keyword, value); | |
3b53e18a DSH |
1668 | } |
1669 | ||
6c5943c9 | 1670 | static int pbe_test_init(EVP_TEST *t, const char *alg) |
3b53e18a | 1671 | { |
6c5943c9 | 1672 | PBE_DATA *pdat; |
c49e0b04 | 1673 | PBE_TYPE pbe_type = PBE_TYPE_INVALID; |
351fe214 | 1674 | |
83bd31da | 1675 | if (strcmp(alg, "scrypt") == 0) { |
b0809bc8 | 1676 | #ifndef OPENSSL_NO_SCRYPT |
3b53e18a | 1677 | pbe_type = PBE_TYPE_SCRYPT; |
83bd31da MC |
1678 | #else |
1679 | t->skip = 1; | |
1680 | return 1; | |
b0809bc8 | 1681 | #endif |
83bd31da | 1682 | } else if (strcmp(alg, "pbkdf2") == 0) { |
351fe214 | 1683 | pbe_type = PBE_TYPE_PBKDF2; |
83bd31da | 1684 | } else if (strcmp(alg, "pkcs12") == 0) { |
351fe214 | 1685 | pbe_type = PBE_TYPE_PKCS12; |
83bd31da | 1686 | } else { |
6c5943c9 | 1687 | TEST_error("Unknown pbe algorithm %s", alg); |
83bd31da | 1688 | } |
c49e0b04 | 1689 | pdat = OPENSSL_zalloc(sizeof(*pdat)); |
3b53e18a | 1690 | pdat->pbe_type = pbe_type; |
3b53e18a DSH |
1691 | t->data = pdat; |
1692 | return 1; | |
1693 | } | |
1694 | ||
6c5943c9 | 1695 | static void pbe_test_cleanup(EVP_TEST *t) |
3b53e18a | 1696 | { |
6c5943c9 RS |
1697 | PBE_DATA *pdat = t->data; |
1698 | ||
1699 | OPENSSL_free(pdat->pass); | |
1700 | OPENSSL_free(pdat->salt); | |
1701 | OPENSSL_free(pdat->key); | |
3b53e18a DSH |
1702 | } |
1703 | ||
6c5943c9 RS |
1704 | static int pbe_test_parse(EVP_TEST *t, |
1705 | const char *keyword, const char *value) | |
3b53e18a | 1706 | { |
6c5943c9 | 1707 | PBE_DATA *pdata = t->data; |
351fe214 | 1708 | |
3b53e18a | 1709 | if (strcmp(keyword, "Password") == 0) |
c49e0b04 | 1710 | return parse_bin(value, &pdata->pass, &pdata->pass_len); |
3b53e18a | 1711 | if (strcmp(keyword, "Salt") == 0) |
c49e0b04 | 1712 | return parse_bin(value, &pdata->salt, &pdata->salt_len); |
3b53e18a | 1713 | if (strcmp(keyword, "Key") == 0) |
c49e0b04 | 1714 | return parse_bin(value, &pdata->key, &pdata->key_len); |
b0809bc8 | 1715 | if (pdata->pbe_type == PBE_TYPE_PBKDF2) |
351fe214 DSH |
1716 | return pbkdf2_test_parse(t, keyword, value); |
1717 | else if (pdata->pbe_type == PBE_TYPE_PKCS12) | |
1718 | return pkcs12_test_parse(t, keyword, value); | |
b0809bc8 RS |
1719 | #ifndef OPENSSL_NO_SCRYPT |
1720 | else if (pdata->pbe_type == PBE_TYPE_SCRYPT) | |
1721 | return scrypt_test_parse(t, keyword, value); | |
1722 | #endif | |
3b53e18a DSH |
1723 | return 0; |
1724 | } | |
1725 | ||
6c5943c9 | 1726 | static int pbe_test_run(EVP_TEST *t) |
3b53e18a | 1727 | { |
e3d378bc | 1728 | PBE_DATA *expected = t->data; |
351fe214 DSH |
1729 | unsigned char *key; |
1730 | ||
e3d378bc | 1731 | if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) { |
6c5943c9 | 1732 | t->err = "INTERNAL_ERROR"; |
351fe214 | 1733 | goto err; |
6c5943c9 | 1734 | } |
e3d378bc AP |
1735 | if (expected->pbe_type == PBE_TYPE_PBKDF2) { |
1736 | if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len, | |
1737 | expected->salt, expected->salt_len, | |
1738 | expected->iter, expected->md, | |
1739 | expected->key_len, key) == 0) { | |
6c5943c9 | 1740 | t->err = "PBKDF2_ERROR"; |
351fe214 | 1741 | goto err; |
6c5943c9 | 1742 | } |
b0809bc8 | 1743 | #ifndef OPENSSL_NO_SCRYPT |
e3d378bc AP |
1744 | } else if (expected->pbe_type == PBE_TYPE_SCRYPT) { |
1745 | if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len, | |
1746 | expected->salt, expected->salt_len, expected->N, | |
1747 | expected->r, expected->p, expected->maxmem, | |
1748 | key, expected->key_len) == 0) { | |
6c5943c9 | 1749 | t->err = "SCRYPT_ERROR"; |
351fe214 | 1750 | goto err; |
6c5943c9 | 1751 | } |
b0809bc8 | 1752 | #endif |
e3d378bc AP |
1753 | } else if (expected->pbe_type == PBE_TYPE_PKCS12) { |
1754 | if (PKCS12_key_gen_uni(expected->pass, expected->pass_len, | |
1755 | expected->salt, expected->salt_len, | |
1756 | expected->id, expected->iter, expected->key_len, | |
1757 | key, expected->md) == 0) { | |
6c5943c9 | 1758 | t->err = "PKCS12_ERROR"; |
351fe214 | 1759 | goto err; |
6c5943c9 | 1760 | } |
351fe214 | 1761 | } |
4cceb185 P |
1762 | if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len, |
1763 | key, expected->key_len)) | |
351fe214 | 1764 | goto err; |
4cceb185 | 1765 | |
6c5943c9 RS |
1766 | t->err = NULL; |
1767 | err: | |
351fe214 | 1768 | OPENSSL_free(key); |
351fe214 | 1769 | return 1; |
3b53e18a DSH |
1770 | } |
1771 | ||
6c5943c9 | 1772 | static const EVP_TEST_METHOD pbe_test_method = { |
3b53e18a DSH |
1773 | "PBE", |
1774 | pbe_test_init, | |
1775 | pbe_test_cleanup, | |
1776 | pbe_test_parse, | |
1777 | pbe_test_run | |
1778 | }; | |
3cdd1e94 | 1779 | |
c49e0b04 RS |
1780 | |
1781 | /** | |
1782 | *** BASE64 TESTS | |
1783 | **/ | |
3cdd1e94 EK |
1784 | |
1785 | typedef enum { | |
1786 | BASE64_CANONICAL_ENCODING = 0, | |
1787 | BASE64_VALID_ENCODING = 1, | |
1788 | BASE64_INVALID_ENCODING = 2 | |
1789 | } base64_encoding_type; | |
1790 | ||
6c5943c9 | 1791 | typedef struct encode_data_st { |
3cdd1e94 EK |
1792 | /* Input to encoding */ |
1793 | unsigned char *input; | |
1794 | size_t input_len; | |
1795 | /* Expected output */ | |
1796 | unsigned char *output; | |
1797 | size_t output_len; | |
1798 | base64_encoding_type encoding; | |
6c5943c9 | 1799 | } ENCODE_DATA; |
3cdd1e94 | 1800 | |
6c5943c9 | 1801 | static int encode_test_init(EVP_TEST *t, const char *encoding) |
3cdd1e94 | 1802 | { |
c49e0b04 | 1803 | ENCODE_DATA *edata; |
3cdd1e94 | 1804 | |
c49e0b04 RS |
1805 | if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata)))) |
1806 | return 0; | |
3cdd1e94 EK |
1807 | if (strcmp(encoding, "canonical") == 0) { |
1808 | edata->encoding = BASE64_CANONICAL_ENCODING; | |
1809 | } else if (strcmp(encoding, "valid") == 0) { | |
1810 | edata->encoding = BASE64_VALID_ENCODING; | |
1811 | } else if (strcmp(encoding, "invalid") == 0) { | |
1812 | edata->encoding = BASE64_INVALID_ENCODING; | |
c49e0b04 | 1813 | if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR"))) |
760e2d60 | 1814 | goto err; |
3cdd1e94 | 1815 | } else { |
c49e0b04 RS |
1816 | TEST_error("Bad encoding: %s." |
1817 | " Should be one of {canonical, valid, invalid}", | |
1818 | encoding); | |
760e2d60 | 1819 | goto err; |
3cdd1e94 EK |
1820 | } |
1821 | t->data = edata; | |
1822 | return 1; | |
760e2d60 F |
1823 | err: |
1824 | OPENSSL_free(edata); | |
1825 | return 0; | |
3cdd1e94 EK |
1826 | } |
1827 | ||
6c5943c9 | 1828 | static void encode_test_cleanup(EVP_TEST *t) |
3cdd1e94 | 1829 | { |
6c5943c9 RS |
1830 | ENCODE_DATA *edata = t->data; |
1831 | ||
1832 | OPENSSL_free(edata->input); | |
1833 | OPENSSL_free(edata->output); | |
3cdd1e94 EK |
1834 | memset(edata, 0, sizeof(*edata)); |
1835 | } | |
1836 | ||
6c5943c9 | 1837 | static int encode_test_parse(EVP_TEST *t, |
3cdd1e94 EK |
1838 | const char *keyword, const char *value) |
1839 | { | |
6c5943c9 | 1840 | ENCODE_DATA *edata = t->data; |
c49e0b04 | 1841 | |
3cdd1e94 | 1842 | if (strcmp(keyword, "Input") == 0) |
c49e0b04 | 1843 | return parse_bin(value, &edata->input, &edata->input_len); |
3cdd1e94 | 1844 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 1845 | return parse_bin(value, &edata->output, &edata->output_len); |
3cdd1e94 EK |
1846 | return 0; |
1847 | } | |
1848 | ||
6c5943c9 | 1849 | static int encode_test_run(EVP_TEST *t) |
3cdd1e94 | 1850 | { |
e3d378bc | 1851 | ENCODE_DATA *expected = t->data; |
3cdd1e94 EK |
1852 | unsigned char *encode_out = NULL, *decode_out = NULL; |
1853 | int output_len, chunk_len; | |
760e2d60 | 1854 | EVP_ENCODE_CTX *decode_ctx = NULL, *encode_ctx = NULL; |
254b26af | 1855 | |
6c5943c9 RS |
1856 | if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) { |
1857 | t->err = "INTERNAL_ERROR"; | |
254b26af | 1858 | goto err; |
6c5943c9 | 1859 | } |
3cdd1e94 | 1860 | |
e3d378bc | 1861 | if (expected->encoding == BASE64_CANONICAL_ENCODING) { |
6c5943c9 RS |
1862 | |
1863 | if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new()) | |
1864 | || !TEST_ptr(encode_out = | |
e3d378bc | 1865 | OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len)))) |
3cdd1e94 EK |
1866 | goto err; |
1867 | ||
254b26af | 1868 | EVP_EncodeInit(encode_ctx); |
760e2d60 F |
1869 | if (!TEST_true(EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len, |
1870 | expected->input, expected->input_len))) | |
1871 | goto err; | |
1872 | ||
3cdd1e94 EK |
1873 | output_len = chunk_len; |
1874 | ||
254b26af | 1875 | EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len); |
3cdd1e94 EK |
1876 | output_len += chunk_len; |
1877 | ||
4cceb185 P |
1878 | if (!memory_err_compare(t, "BAD_ENCODING", |
1879 | expected->output, expected->output_len, | |
1880 | encode_out, output_len)) | |
3cdd1e94 | 1881 | goto err; |
3cdd1e94 EK |
1882 | } |
1883 | ||
6c5943c9 | 1884 | if (!TEST_ptr(decode_out = |
e3d378bc | 1885 | OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len)))) |
3cdd1e94 EK |
1886 | goto err; |
1887 | ||
254b26af | 1888 | EVP_DecodeInit(decode_ctx); |
e3d378bc AP |
1889 | if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output, |
1890 | expected->output_len) < 0) { | |
6c5943c9 | 1891 | t->err = "DECODE_ERROR"; |
3cdd1e94 EK |
1892 | goto err; |
1893 | } | |
1894 | output_len = chunk_len; | |
1895 | ||
254b26af | 1896 | if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) { |
6c5943c9 | 1897 | t->err = "DECODE_ERROR"; |
3cdd1e94 EK |
1898 | goto err; |
1899 | } | |
1900 | output_len += chunk_len; | |
1901 | ||
e3d378bc | 1902 | if (expected->encoding != BASE64_INVALID_ENCODING |
4cceb185 P |
1903 | && !memory_err_compare(t, "BAD_DECODING", |
1904 | expected->input, expected->input_len, | |
1905 | decode_out, output_len)) { | |
6c5943c9 | 1906 | t->err = "BAD_DECODING"; |
3cdd1e94 EK |
1907 | goto err; |
1908 | } | |
1909 | ||
6c5943c9 | 1910 | t->err = NULL; |
3cdd1e94 | 1911 | err: |
3cdd1e94 EK |
1912 | OPENSSL_free(encode_out); |
1913 | OPENSSL_free(decode_out); | |
254b26af | 1914 | EVP_ENCODE_CTX_free(decode_ctx); |
760e2d60 | 1915 | EVP_ENCODE_CTX_free(encode_ctx); |
3cdd1e94 EK |
1916 | return 1; |
1917 | } | |
1918 | ||
6c5943c9 | 1919 | static const EVP_TEST_METHOD encode_test_method = { |
3cdd1e94 EK |
1920 | "Encoding", |
1921 | encode_test_init, | |
1922 | encode_test_cleanup, | |
1923 | encode_test_parse, | |
1924 | encode_test_run, | |
1925 | }; | |
44a284d2 | 1926 | |
5a285add | 1927 | |
c49e0b04 RS |
1928 | /** |
1929 | *** KDF TESTS | |
1930 | **/ | |
44a284d2 | 1931 | |
6c5943c9 | 1932 | typedef struct kdf_data_st { |
44a284d2 | 1933 | /* Context for this operation */ |
5a285add | 1934 | EVP_KDF_CTX *ctx; |
44a284d2 DSH |
1935 | /* Expected output */ |
1936 | unsigned char *output; | |
1937 | size_t output_len; | |
6c5943c9 | 1938 | } KDF_DATA; |
44a284d2 DSH |
1939 | |
1940 | /* | |
1941 | * Perform public key operation setup: lookup key, allocated ctx and call | |
1942 | * the appropriate initialisation function | |
1943 | */ | |
6c5943c9 | 1944 | static int kdf_test_init(EVP_TEST *t, const char *name) |
44a284d2 | 1945 | { |
6c5943c9 | 1946 | KDF_DATA *kdata; |
d2ba8123 | 1947 | const EVP_KDF *kdf; |
b15d5ab6 | 1948 | |
ab78f89b MC |
1949 | #ifdef OPENSSL_NO_SCRYPT |
1950 | if (strcmp(name, "scrypt") == 0) { | |
1951 | t->skip = 1; | |
1952 | return 1; | |
1953 | } | |
1aec7716 SL |
1954 | #endif /* OPENSSL_NO_SCRYPT */ |
1955 | ||
1956 | #ifdef OPENSSL_NO_CMS | |
1957 | if (strcmp(name, "X942KDF") == 0) { | |
1958 | t->skip = 1; | |
1959 | return 1; | |
1960 | } | |
1961 | #endif /* OPENSSL_NO_CMS */ | |
ab78f89b | 1962 | |
d2ba8123 SL |
1963 | kdf = EVP_get_kdfbyname(name); |
1964 | if (kdf == NULL) | |
1965 | return 0; | |
44a284d2 | 1966 | |
c49e0b04 | 1967 | if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) |
44a284d2 | 1968 | return 0; |
d2ba8123 | 1969 | kdata->ctx = EVP_KDF_CTX_new(kdf); |
9e206ce5 P |
1970 | if (kdata->ctx == NULL) { |
1971 | OPENSSL_free(kdata); | |
44a284d2 | 1972 | return 0; |
9e206ce5 | 1973 | } |
c49e0b04 | 1974 | t->data = kdata; |
44a284d2 DSH |
1975 | return 1; |
1976 | } | |
1977 | ||
6c5943c9 | 1978 | static void kdf_test_cleanup(EVP_TEST *t) |
44a284d2 | 1979 | { |
6c5943c9 | 1980 | KDF_DATA *kdata = t->data; |
44a284d2 | 1981 | OPENSSL_free(kdata->output); |
5a285add DM |
1982 | EVP_KDF_CTX_free(kdata->ctx); |
1983 | } | |
1984 | ||
1985 | static int kdf_test_ctrl(EVP_TEST *t, EVP_KDF_CTX *kctx, | |
1986 | const char *value) | |
1987 | { | |
1988 | int rv; | |
1989 | char *p, *tmpval; | |
1990 | ||
1991 | if (!TEST_ptr(tmpval = OPENSSL_strdup(value))) | |
1992 | return 0; | |
1993 | p = strchr(tmpval, ':'); | |
1994 | if (p != NULL) | |
1995 | *p++ = '\0'; | |
1996 | rv = EVP_KDF_ctrl_str(kctx, tmpval, p); | |
1997 | if (rv == -2) { | |
1998 | t->err = "KDF_CTRL_INVALID"; | |
1999 | rv = 1; | |
2000 | } else if (p != NULL && rv <= 0) { | |
2001 | /* If p has an OID and lookup fails assume disabled algorithm */ | |
2002 | int nid = OBJ_sn2nid(p); | |
2003 | ||
2004 | if (nid == NID_undef) | |
2005 | nid = OBJ_ln2nid(p); | |
2006 | if (nid != NID_undef | |
2007 | && EVP_get_digestbynid(nid) == NULL | |
2008 | && EVP_get_cipherbynid(nid) == NULL) { | |
2009 | t->skip = 1; | |
2010 | rv = 1; | |
2011 | } else { | |
2012 | t->err = "KDF_CTRL_ERROR"; | |
2013 | rv = 1; | |
2014 | } | |
2015 | } | |
2016 | OPENSSL_free(tmpval); | |
2017 | return rv > 0; | |
44a284d2 DSH |
2018 | } |
2019 | ||
6c5943c9 | 2020 | static int kdf_test_parse(EVP_TEST *t, |
44a284d2 DSH |
2021 | const char *keyword, const char *value) |
2022 | { | |
6c5943c9 RS |
2023 | KDF_DATA *kdata = t->data; |
2024 | ||
44a284d2 | 2025 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 2026 | return parse_bin(value, &kdata->output, &kdata->output_len); |
7d04be79 | 2027 | if (strncmp(keyword, "Ctrl", 4) == 0) |
5a285add | 2028 | return kdf_test_ctrl(t, kdata->ctx, value); |
44a284d2 DSH |
2029 | return 0; |
2030 | } | |
2031 | ||
6c5943c9 | 2032 | static int kdf_test_run(EVP_TEST *t) |
44a284d2 | 2033 | { |
e3d378bc AP |
2034 | KDF_DATA *expected = t->data; |
2035 | unsigned char *got = NULL; | |
2036 | size_t got_len = expected->output_len; | |
6c5943c9 | 2037 | |
e3d378bc | 2038 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { |
6c5943c9 | 2039 | t->err = "INTERNAL_ERROR"; |
44a284d2 | 2040 | goto err; |
6c5943c9 | 2041 | } |
5a285add | 2042 | if (EVP_KDF_derive(expected->ctx, got, got_len) <= 0) { |
6c5943c9 | 2043 | t->err = "KDF_DERIVE_ERROR"; |
44a284d2 | 2044 | goto err; |
6c5943c9 | 2045 | } |
4cceb185 P |
2046 | if (!memory_err_compare(t, "KDF_MISMATCH", |
2047 | expected->output, expected->output_len, | |
2048 | got, got_len)) | |
44a284d2 | 2049 | goto err; |
4cceb185 | 2050 | |
6c5943c9 RS |
2051 | t->err = NULL; |
2052 | ||
44a284d2 | 2053 | err: |
e3d378bc | 2054 | OPENSSL_free(got); |
44a284d2 DSH |
2055 | return 1; |
2056 | } | |
2057 | ||
6c5943c9 | 2058 | static const EVP_TEST_METHOD kdf_test_method = { |
44a284d2 DSH |
2059 | "KDF", |
2060 | kdf_test_init, | |
2061 | kdf_test_cleanup, | |
2062 | kdf_test_parse, | |
2063 | kdf_test_run | |
2064 | }; | |
d91b7423 | 2065 | |
c49e0b04 | 2066 | |
5a285add DM |
2067 | /** |
2068 | *** PKEY KDF TESTS | |
2069 | **/ | |
2070 | ||
2071 | typedef struct pkey_kdf_data_st { | |
2072 | /* Context for this operation */ | |
2073 | EVP_PKEY_CTX *ctx; | |
2074 | /* Expected output */ | |
2075 | unsigned char *output; | |
2076 | size_t output_len; | |
2077 | } PKEY_KDF_DATA; | |
2078 | ||
2079 | /* | |
2080 | * Perform public key operation setup: lookup key, allocated ctx and call | |
2081 | * the appropriate initialisation function | |
2082 | */ | |
2083 | static int pkey_kdf_test_init(EVP_TEST *t, const char *name) | |
2084 | { | |
2085 | PKEY_KDF_DATA *kdata; | |
2086 | int kdf_nid = OBJ_sn2nid(name); | |
2087 | ||
2088 | #ifdef OPENSSL_NO_SCRYPT | |
2089 | if (strcmp(name, "scrypt") == 0) { | |
2090 | t->skip = 1; | |
2091 | return 1; | |
2092 | } | |
1aec7716 SL |
2093 | #endif /* OPENSSL_NO_SCRYPT */ |
2094 | ||
2095 | #ifdef OPENSSL_NO_CMS | |
2096 | if (strcmp(name, "X942KDF") == 0) { | |
2097 | t->skip = 1; | |
2098 | return 1; | |
2099 | } | |
2100 | #endif /* OPENSSL_NO_CMS */ | |
5a285add DM |
2101 | |
2102 | if (kdf_nid == NID_undef) | |
2103 | kdf_nid = OBJ_ln2nid(name); | |
2104 | ||
2105 | if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) | |
2106 | return 0; | |
2107 | kdata->ctx = EVP_PKEY_CTX_new_id(kdf_nid, NULL); | |
2108 | if (kdata->ctx == NULL) { | |
2109 | OPENSSL_free(kdata); | |
2110 | return 0; | |
2111 | } | |
2112 | if (EVP_PKEY_derive_init(kdata->ctx) <= 0) { | |
2113 | EVP_PKEY_CTX_free(kdata->ctx); | |
2114 | OPENSSL_free(kdata); | |
2115 | return 0; | |
2116 | } | |
2117 | t->data = kdata; | |
2118 | return 1; | |
2119 | } | |
2120 | ||
2121 | static void pkey_kdf_test_cleanup(EVP_TEST *t) | |
2122 | { | |
2123 | PKEY_KDF_DATA *kdata = t->data; | |
2124 | OPENSSL_free(kdata->output); | |
2125 | EVP_PKEY_CTX_free(kdata->ctx); | |
2126 | } | |
2127 | ||
2128 | static int pkey_kdf_test_parse(EVP_TEST *t, | |
2129 | const char *keyword, const char *value) | |
2130 | { | |
2131 | PKEY_KDF_DATA *kdata = t->data; | |
2132 | ||
2133 | if (strcmp(keyword, "Output") == 0) | |
2134 | return parse_bin(value, &kdata->output, &kdata->output_len); | |
2135 | if (strncmp(keyword, "Ctrl", 4) == 0) | |
2136 | return pkey_test_ctrl(t, kdata->ctx, value); | |
2137 | return 0; | |
2138 | } | |
2139 | ||
2140 | static int pkey_kdf_test_run(EVP_TEST *t) | |
2141 | { | |
2142 | PKEY_KDF_DATA *expected = t->data; | |
2143 | unsigned char *got = NULL; | |
2144 | size_t got_len = expected->output_len; | |
2145 | ||
2146 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { | |
2147 | t->err = "INTERNAL_ERROR"; | |
2148 | goto err; | |
2149 | } | |
2150 | if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) { | |
2151 | t->err = "KDF_DERIVE_ERROR"; | |
2152 | goto err; | |
2153 | } | |
2154 | if (!TEST_mem_eq(expected->output, expected->output_len, got, got_len)) { | |
2155 | t->err = "KDF_MISMATCH"; | |
2156 | goto err; | |
2157 | } | |
2158 | t->err = NULL; | |
2159 | ||
2160 | err: | |
2161 | OPENSSL_free(got); | |
2162 | return 1; | |
2163 | } | |
2164 | ||
2165 | static const EVP_TEST_METHOD pkey_kdf_test_method = { | |
2166 | "PKEYKDF", | |
2167 | pkey_kdf_test_init, | |
2168 | pkey_kdf_test_cleanup, | |
2169 | pkey_kdf_test_parse, | |
2170 | pkey_kdf_test_run | |
2171 | }; | |
2172 | ||
2173 | ||
c49e0b04 RS |
2174 | /** |
2175 | *** KEYPAIR TESTS | |
2176 | **/ | |
2177 | ||
2178 | typedef struct keypair_test_data_st { | |
d91b7423 RS |
2179 | EVP_PKEY *privk; |
2180 | EVP_PKEY *pubk; | |
6c5943c9 | 2181 | } KEYPAIR_TEST_DATA; |
d91b7423 | 2182 | |
6c5943c9 | 2183 | static int keypair_test_init(EVP_TEST *t, const char *pair) |
d91b7423 | 2184 | { |
c49e0b04 | 2185 | KEYPAIR_TEST_DATA *data; |
d91b7423 RS |
2186 | int rv = 0; |
2187 | EVP_PKEY *pk = NULL, *pubk = NULL; | |
2188 | char *pub, *priv = NULL; | |
d91b7423 | 2189 | |
c49e0b04 | 2190 | /* Split private and public names. */ |
6c5943c9 RS |
2191 | if (!TEST_ptr(priv = OPENSSL_strdup(pair)) |
2192 | || !TEST_ptr(pub = strchr(priv, ':'))) { | |
2193 | t->err = "PARSING_ERROR"; | |
d91b7423 RS |
2194 | goto end; |
2195 | } | |
c49e0b04 | 2196 | *pub++ = '\0'; |
d91b7423 | 2197 | |
6c5943c9 | 2198 | if (!TEST_true(find_key(&pk, priv, private_keys))) { |
c49e0b04 | 2199 | TEST_info("Can't find private key: %s", priv); |
6c5943c9 | 2200 | t->err = "MISSING_PRIVATE_KEY"; |
d91b7423 RS |
2201 | goto end; |
2202 | } | |
6c5943c9 | 2203 | if (!TEST_true(find_key(&pubk, pub, public_keys))) { |
c49e0b04 | 2204 | TEST_info("Can't find public key: %s", pub); |
6c5943c9 | 2205 | t->err = "MISSING_PUBLIC_KEY"; |
d91b7423 RS |
2206 | goto end; |
2207 | } | |
2208 | ||
2209 | if (pk == NULL && pubk == NULL) { | |
2210 | /* Both keys are listed but unsupported: skip this test */ | |
2211 | t->skip = 1; | |
2212 | rv = 1; | |
2213 | goto end; | |
2214 | } | |
2215 | ||
6c5943c9 | 2216 | if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data)))) |
d91b7423 | 2217 | goto end; |
d91b7423 RS |
2218 | data->privk = pk; |
2219 | data->pubk = pubk; | |
2220 | t->data = data; | |
d91b7423 | 2221 | rv = 1; |
6c5943c9 | 2222 | t->err = NULL; |
d91b7423 RS |
2223 | |
2224 | end: | |
6c5943c9 | 2225 | OPENSSL_free(priv); |
d91b7423 RS |
2226 | return rv; |
2227 | } | |
2228 | ||
6c5943c9 | 2229 | static void keypair_test_cleanup(EVP_TEST *t) |
d91b7423 | 2230 | { |
6c5943c9 | 2231 | OPENSSL_free(t->data); |
d91b7423 | 2232 | t->data = NULL; |
d91b7423 RS |
2233 | } |
2234 | ||
c49e0b04 RS |
2235 | /* |
2236 | * For tests that do not accept any custom keywords. | |
d91b7423 | 2237 | */ |
6c5943c9 | 2238 | static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value) |
d91b7423 RS |
2239 | { |
2240 | return 0; | |
2241 | } | |
2242 | ||
6c5943c9 | 2243 | static int keypair_test_run(EVP_TEST *t) |
d91b7423 RS |
2244 | { |
2245 | int rv = 0; | |
6c5943c9 | 2246 | const KEYPAIR_TEST_DATA *pair = t->data; |
d91b7423 RS |
2247 | |
2248 | if (pair->privk == NULL || pair->pubk == NULL) { | |
6c5943c9 RS |
2249 | /* |
2250 | * this can only happen if only one of the keys is not set | |
d91b7423 RS |
2251 | * which means that one of them was unsupported while the |
2252 | * other isn't: hence a key type mismatch. | |
2253 | */ | |
6c5943c9 | 2254 | t->err = "KEYPAIR_TYPE_MISMATCH"; |
d91b7423 RS |
2255 | rv = 1; |
2256 | goto end; | |
2257 | } | |
2258 | ||
2259 | if ((rv = EVP_PKEY_cmp(pair->privk, pair->pubk)) != 1 ) { | |
2260 | if ( 0 == rv ) { | |
6c5943c9 | 2261 | t->err = "KEYPAIR_MISMATCH"; |
d91b7423 | 2262 | } else if ( -1 == rv ) { |
6c5943c9 | 2263 | t->err = "KEYPAIR_TYPE_MISMATCH"; |
d91b7423 | 2264 | } else if ( -2 == rv ) { |
6c5943c9 | 2265 | t->err = "UNSUPPORTED_KEY_COMPARISON"; |
d91b7423 | 2266 | } else { |
6c5943c9 | 2267 | TEST_error("Unexpected error in key comparison"); |
d91b7423 RS |
2268 | rv = 0; |
2269 | goto end; | |
2270 | } | |
2271 | rv = 1; | |
2272 | goto end; | |
2273 | } | |
2274 | ||
2275 | rv = 1; | |
6c5943c9 | 2276 | t->err = NULL; |
d91b7423 RS |
2277 | |
2278 | end: | |
d91b7423 RS |
2279 | return rv; |
2280 | } | |
2281 | ||
6c5943c9 | 2282 | static const EVP_TEST_METHOD keypair_test_method = { |
d91b7423 RS |
2283 | "PrivPubKeyPair", |
2284 | keypair_test_init, | |
2285 | keypair_test_cleanup, | |
2286 | void_test_parse, | |
2287 | keypair_test_run | |
2288 | }; | |
2289 | ||
1f0fc03b DSH |
2290 | /** |
2291 | *** KEYGEN TEST | |
2292 | **/ | |
2293 | ||
2294 | typedef struct keygen_test_data_st { | |
2295 | EVP_PKEY_CTX *genctx; /* Keygen context to use */ | |
2296 | char *keyname; /* Key name to store key or NULL */ | |
2297 | } KEYGEN_TEST_DATA; | |
2298 | ||
2299 | static int keygen_test_init(EVP_TEST *t, const char *alg) | |
2300 | { | |
2301 | KEYGEN_TEST_DATA *data; | |
2302 | EVP_PKEY_CTX *genctx; | |
2303 | int nid = OBJ_sn2nid(alg); | |
2304 | ||
2305 | if (nid == NID_undef) { | |
2306 | nid = OBJ_ln2nid(alg); | |
2307 | if (nid == NID_undef) | |
2308 | return 0; | |
2309 | } | |
2310 | ||
2311 | if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_id(nid, NULL))) { | |
2312 | /* assume algorithm disabled */ | |
2313 | t->skip = 1; | |
2314 | return 1; | |
2315 | } | |
2316 | ||
2317 | if (EVP_PKEY_keygen_init(genctx) <= 0) { | |
2318 | t->err = "KEYGEN_INIT_ERROR"; | |
2319 | goto err; | |
2320 | } | |
2321 | ||
2322 | if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data)))) | |
2323 | goto err; | |
2324 | data->genctx = genctx; | |
2325 | data->keyname = NULL; | |
2326 | t->data = data; | |
2327 | t->err = NULL; | |
2328 | return 1; | |
2329 | ||
2330 | err: | |
2331 | EVP_PKEY_CTX_free(genctx); | |
2332 | return 0; | |
2333 | } | |
2334 | ||
2335 | static void keygen_test_cleanup(EVP_TEST *t) | |
2336 | { | |
2337 | KEYGEN_TEST_DATA *keygen = t->data; | |
2338 | ||
2339 | EVP_PKEY_CTX_free(keygen->genctx); | |
2340 | OPENSSL_free(keygen->keyname); | |
2341 | OPENSSL_free(t->data); | |
2342 | t->data = NULL; | |
2343 | } | |
2344 | ||
2345 | static int keygen_test_parse(EVP_TEST *t, | |
2346 | const char *keyword, const char *value) | |
2347 | { | |
2348 | KEYGEN_TEST_DATA *keygen = t->data; | |
2349 | ||
2350 | if (strcmp(keyword, "KeyName") == 0) | |
2351 | return TEST_ptr(keygen->keyname = OPENSSL_strdup(value)); | |
2352 | if (strcmp(keyword, "Ctrl") == 0) | |
2353 | return pkey_test_ctrl(t, keygen->genctx, value); | |
2354 | return 0; | |
2355 | } | |
2356 | ||
2357 | static int keygen_test_run(EVP_TEST *t) | |
2358 | { | |
2359 | KEYGEN_TEST_DATA *keygen = t->data; | |
2360 | EVP_PKEY *pkey = NULL; | |
2361 | ||
2362 | t->err = NULL; | |
2363 | if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) { | |
2364 | t->err = "KEYGEN_GENERATE_ERROR"; | |
2365 | goto err; | |
2366 | } | |
2367 | ||
2368 | if (keygen->keyname != NULL) { | |
2369 | KEY_LIST *key; | |
2370 | ||
2371 | if (find_key(NULL, keygen->keyname, private_keys)) { | |
2372 | TEST_info("Duplicate key %s", keygen->keyname); | |
2373 | goto err; | |
2374 | } | |
2375 | ||
2376 | if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key)))) | |
2377 | goto err; | |
2378 | key->name = keygen->keyname; | |
2379 | keygen->keyname = NULL; | |
2380 | key->key = pkey; | |
2381 | key->next = private_keys; | |
2382 | private_keys = key; | |
2383 | } else { | |
2384 | EVP_PKEY_free(pkey); | |
2385 | } | |
2386 | ||
2387 | return 1; | |
2388 | ||
2389 | err: | |
2390 | EVP_PKEY_free(pkey); | |
2391 | return 0; | |
2392 | } | |
2393 | ||
2394 | static const EVP_TEST_METHOD keygen_test_method = { | |
2395 | "KeyGen", | |
2396 | keygen_test_init, | |
2397 | keygen_test_cleanup, | |
2398 | keygen_test_parse, | |
2399 | keygen_test_run, | |
2400 | }; | |
c49e0b04 RS |
2401 | |
2402 | /** | |
2403 | *** DIGEST SIGN+VERIFY TESTS | |
2404 | **/ | |
2405 | ||
75726fe8 | 2406 | typedef struct { |
2117a737 DSH |
2407 | int is_verify; /* Set to 1 if verifying */ |
2408 | int is_oneshot; /* Set to 1 for one shot operation */ | |
2409 | const EVP_MD *md; /* Digest to use */ | |
2410 | EVP_MD_CTX *ctx; /* Digest context */ | |
75726fe8 | 2411 | EVP_PKEY_CTX *pctx; |
2117a737 DSH |
2412 | STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */ |
2413 | unsigned char *osin; /* Input data if one shot */ | |
2414 | size_t osin_len; /* Input length data if one shot */ | |
2415 | unsigned char *output; /* Expected output */ | |
2416 | size_t output_len; /* Expected output length */ | |
75726fe8 DSH |
2417 | } DIGESTSIGN_DATA; |
2418 | ||
7b22334f DSH |
2419 | static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify, |
2420 | int is_oneshot) | |
75726fe8 DSH |
2421 | { |
2422 | const EVP_MD *md = NULL; | |
2423 | DIGESTSIGN_DATA *mdat; | |
2424 | ||
2425 | if (strcmp(alg, "NULL") != 0) { | |
2426 | if ((md = EVP_get_digestbyname(alg)) == NULL) { | |
2427 | /* If alg has an OID assume disabled algorithm */ | |
2428 | if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) { | |
2429 | t->skip = 1; | |
2430 | return 1; | |
2431 | } | |
2432 | return 0; | |
2433 | } | |
2434 | } | |
2435 | if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat)))) | |
2436 | return 0; | |
2437 | mdat->md = md; | |
2438 | if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) { | |
2439 | OPENSSL_free(mdat); | |
2440 | return 0; | |
2441 | } | |
2442 | mdat->is_verify = is_verify; | |
7b22334f | 2443 | mdat->is_oneshot = is_oneshot; |
75726fe8 DSH |
2444 | t->data = mdat; |
2445 | return 1; | |
2446 | } | |
2447 | ||
2448 | static int digestsign_test_init(EVP_TEST *t, const char *alg) | |
2449 | { | |
7b22334f | 2450 | return digestsigver_test_init(t, alg, 0, 0); |
75726fe8 DSH |
2451 | } |
2452 | ||
2453 | static void digestsigver_test_cleanup(EVP_TEST *t) | |
2454 | { | |
2455 | DIGESTSIGN_DATA *mdata = t->data; | |
2456 | ||
2457 | EVP_MD_CTX_free(mdata->ctx); | |
2458 | sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free); | |
7b22334f | 2459 | OPENSSL_free(mdata->osin); |
75726fe8 DSH |
2460 | OPENSSL_free(mdata->output); |
2461 | OPENSSL_free(mdata); | |
2462 | t->data = NULL; | |
2463 | } | |
2464 | ||
2465 | static int digestsigver_test_parse(EVP_TEST *t, | |
2466 | const char *keyword, const char *value) | |
2467 | { | |
2468 | DIGESTSIGN_DATA *mdata = t->data; | |
2469 | ||
2470 | if (strcmp(keyword, "Key") == 0) { | |
2471 | EVP_PKEY *pkey = NULL; | |
2472 | int rv = 0; | |
2473 | ||
2474 | if (mdata->is_verify) | |
2475 | rv = find_key(&pkey, value, public_keys); | |
2476 | if (rv == 0) | |
2477 | rv = find_key(&pkey, value, private_keys); | |
2478 | if (rv == 0 || pkey == NULL) { | |
2479 | t->skip = 1; | |
2480 | return 1; | |
2481 | } | |
2482 | if (mdata->is_verify) { | |
2483 | if (!EVP_DigestVerifyInit(mdata->ctx, &mdata->pctx, mdata->md, | |
2484 | NULL, pkey)) | |
2485 | t->err = "DIGESTVERIFYINIT_ERROR"; | |
2486 | return 1; | |
2487 | } | |
2488 | if (!EVP_DigestSignInit(mdata->ctx, &mdata->pctx, mdata->md, NULL, | |
2489 | pkey)) | |
2490 | t->err = "DIGESTSIGNINIT_ERROR"; | |
2491 | return 1; | |
2492 | } | |
2493 | ||
7b22334f DSH |
2494 | if (strcmp(keyword, "Input") == 0) { |
2495 | if (mdata->is_oneshot) | |
c49e0b04 | 2496 | return parse_bin(value, &mdata->osin, &mdata->osin_len); |
75726fe8 | 2497 | return evp_test_buffer_append(value, &mdata->input); |
7b22334f | 2498 | } |
75726fe8 | 2499 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 2500 | return parse_bin(value, &mdata->output, &mdata->output_len); |
7b22334f DSH |
2501 | |
2502 | if (!mdata->is_oneshot) { | |
2503 | if (strcmp(keyword, "Count") == 0) | |
2504 | return evp_test_buffer_set_count(value, mdata->input); | |
2505 | if (strcmp(keyword, "Ncopy") == 0) | |
2506 | return evp_test_buffer_ncopy(value, mdata->input); | |
2507 | } | |
75726fe8 DSH |
2508 | if (strcmp(keyword, "Ctrl") == 0) { |
2509 | if (mdata->pctx == NULL) | |
2510 | return 0; | |
2511 | return pkey_test_ctrl(t, mdata->pctx, value); | |
2512 | } | |
2513 | return 0; | |
2514 | } | |
2515 | ||
2516 | static int digestsign_update_fn(void *ctx, const unsigned char *buf, | |
2517 | size_t buflen) | |
2518 | { | |
2519 | return EVP_DigestSignUpdate(ctx, buf, buflen); | |
2520 | } | |
2521 | ||
2522 | static int digestsign_test_run(EVP_TEST *t) | |
2523 | { | |
e3d378bc AP |
2524 | DIGESTSIGN_DATA *expected = t->data; |
2525 | unsigned char *got = NULL; | |
2526 | size_t got_len; | |
75726fe8 | 2527 | |
e3d378bc AP |
2528 | if (!evp_test_buffer_do(expected->input, digestsign_update_fn, |
2529 | expected->ctx)) { | |
75726fe8 DSH |
2530 | t->err = "DIGESTUPDATE_ERROR"; |
2531 | goto err; | |
2532 | } | |
2533 | ||
e3d378bc | 2534 | if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) { |
75726fe8 DSH |
2535 | t->err = "DIGESTSIGNFINAL_LENGTH_ERROR"; |
2536 | goto err; | |
2537 | } | |
e3d378bc | 2538 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { |
75726fe8 DSH |
2539 | t->err = "MALLOC_FAILURE"; |
2540 | goto err; | |
2541 | } | |
e3d378bc | 2542 | if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) { |
75726fe8 DSH |
2543 | t->err = "DIGESTSIGNFINAL_ERROR"; |
2544 | goto err; | |
2545 | } | |
4cceb185 P |
2546 | if (!memory_err_compare(t, "SIGNATURE_MISMATCH", |
2547 | expected->output, expected->output_len, | |
2548 | got, got_len)) | |
75726fe8 | 2549 | goto err; |
75726fe8 | 2550 | |
4cceb185 | 2551 | t->err = NULL; |
75726fe8 | 2552 | err: |
e3d378bc | 2553 | OPENSSL_free(got); |
75726fe8 DSH |
2554 | return 1; |
2555 | } | |
2556 | ||
2557 | static const EVP_TEST_METHOD digestsign_test_method = { | |
2558 | "DigestSign", | |
2559 | digestsign_test_init, | |
2560 | digestsigver_test_cleanup, | |
2561 | digestsigver_test_parse, | |
2562 | digestsign_test_run | |
2563 | }; | |
2564 | ||
2565 | static int digestverify_test_init(EVP_TEST *t, const char *alg) | |
2566 | { | |
7b22334f | 2567 | return digestsigver_test_init(t, alg, 1, 0); |
75726fe8 DSH |
2568 | } |
2569 | ||
2570 | static int digestverify_update_fn(void *ctx, const unsigned char *buf, | |
2571 | size_t buflen) | |
2572 | { | |
2573 | return EVP_DigestVerifyUpdate(ctx, buf, buflen); | |
2574 | } | |
2575 | ||
2576 | static int digestverify_test_run(EVP_TEST *t) | |
2577 | { | |
2578 | DIGESTSIGN_DATA *mdata = t->data; | |
2579 | ||
2580 | if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) { | |
2581 | t->err = "DIGESTUPDATE_ERROR"; | |
2582 | return 1; | |
2583 | } | |
2584 | ||
2585 | if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output, | |
2586 | mdata->output_len) <= 0) | |
2587 | t->err = "VERIFY_ERROR"; | |
2588 | return 1; | |
2589 | } | |
2590 | ||
2591 | static const EVP_TEST_METHOD digestverify_test_method = { | |
2592 | "DigestVerify", | |
2593 | digestverify_test_init, | |
2594 | digestsigver_test_cleanup, | |
2595 | digestsigver_test_parse, | |
2596 | digestverify_test_run | |
2597 | }; | |
2598 | ||
7b22334f DSH |
2599 | static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg) |
2600 | { | |
2601 | return digestsigver_test_init(t, alg, 0, 1); | |
2602 | } | |
2603 | ||
2604 | static int oneshot_digestsign_test_run(EVP_TEST *t) | |
2605 | { | |
e3d378bc AP |
2606 | DIGESTSIGN_DATA *expected = t->data; |
2607 | unsigned char *got = NULL; | |
2608 | size_t got_len; | |
7b22334f | 2609 | |
e3d378bc AP |
2610 | if (!EVP_DigestSign(expected->ctx, NULL, &got_len, |
2611 | expected->osin, expected->osin_len)) { | |
7b22334f DSH |
2612 | t->err = "DIGESTSIGN_LENGTH_ERROR"; |
2613 | goto err; | |
2614 | } | |
e3d378bc | 2615 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { |
7b22334f DSH |
2616 | t->err = "MALLOC_FAILURE"; |
2617 | goto err; | |
2618 | } | |
e3d378bc AP |
2619 | if (!EVP_DigestSign(expected->ctx, got, &got_len, |
2620 | expected->osin, expected->osin_len)) { | |
7b22334f DSH |
2621 | t->err = "DIGESTSIGN_ERROR"; |
2622 | goto err; | |
2623 | } | |
4cceb185 P |
2624 | if (!memory_err_compare(t, "SIGNATURE_MISMATCH", |
2625 | expected->output, expected->output_len, | |
2626 | got, got_len)) | |
7b22334f | 2627 | goto err; |
7b22334f | 2628 | |
4cceb185 | 2629 | t->err = NULL; |
7b22334f | 2630 | err: |
e3d378bc | 2631 | OPENSSL_free(got); |
7b22334f DSH |
2632 | return 1; |
2633 | } | |
2634 | ||
2635 | static const EVP_TEST_METHOD oneshot_digestsign_test_method = { | |
2636 | "OneShotDigestSign", | |
2637 | oneshot_digestsign_test_init, | |
2638 | digestsigver_test_cleanup, | |
2639 | digestsigver_test_parse, | |
2640 | oneshot_digestsign_test_run | |
2641 | }; | |
2642 | ||
2643 | static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg) | |
2644 | { | |
2645 | return digestsigver_test_init(t, alg, 1, 1); | |
2646 | } | |
2647 | ||
2648 | static int oneshot_digestverify_test_run(EVP_TEST *t) | |
2649 | { | |
2650 | DIGESTSIGN_DATA *mdata = t->data; | |
2651 | ||
2652 | if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len, | |
2653 | mdata->osin, mdata->osin_len) <= 0) | |
2654 | t->err = "VERIFY_ERROR"; | |
2655 | return 1; | |
2656 | } | |
2657 | ||
2658 | static const EVP_TEST_METHOD oneshot_digestverify_test_method = { | |
2659 | "OneShotDigestVerify", | |
2660 | oneshot_digestverify_test_init, | |
2661 | digestsigver_test_cleanup, | |
2662 | digestsigver_test_parse, | |
2663 | oneshot_digestverify_test_run | |
2664 | }; | |
2665 | ||
c49e0b04 RS |
2666 | |
2667 | /** | |
2668 | *** PARSING AND DISPATCH | |
2669 | **/ | |
2670 | ||
2671 | static const EVP_TEST_METHOD *evp_test_list[] = { | |
2672 | &cipher_test_method, | |
2673 | &digest_test_method, | |
2674 | &digestsign_test_method, | |
2675 | &digestverify_test_method, | |
2676 | &encode_test_method, | |
2677 | &kdf_test_method, | |
5a285add | 2678 | &pkey_kdf_test_method, |
c49e0b04 | 2679 | &keypair_test_method, |
1f0fc03b | 2680 | &keygen_test_method, |
c49e0b04 RS |
2681 | &mac_test_method, |
2682 | &oneshot_digestsign_test_method, | |
2683 | &oneshot_digestverify_test_method, | |
2684 | &pbe_test_method, | |
2685 | &pdecrypt_test_method, | |
2686 | &pderive_test_method, | |
2687 | &psign_test_method, | |
2688 | &pverify_recover_test_method, | |
2689 | &pverify_test_method, | |
2690 | NULL | |
2691 | }; | |
2692 | ||
2693 | static const EVP_TEST_METHOD *find_test(const char *name) | |
2694 | { | |
2695 | const EVP_TEST_METHOD **tt; | |
2696 | ||
2697 | for (tt = evp_test_list; *tt; tt++) { | |
2698 | if (strcmp(name, (*tt)->name) == 0) | |
2699 | return *tt; | |
2700 | } | |
2701 | return NULL; | |
2702 | } | |
2703 | ||
2704 | static void clear_test(EVP_TEST *t) | |
2705 | { | |
ae269dd8 | 2706 | test_clearstanza(&t->s); |
c49e0b04 RS |
2707 | ERR_clear_error(); |
2708 | if (t->data != NULL) { | |
2709 | if (t->meth != NULL) | |
2710 | t->meth->cleanup(t); | |
2711 | OPENSSL_free(t->data); | |
2712 | t->data = NULL; | |
2713 | } | |
2714 | OPENSSL_free(t->expected_err); | |
2715 | t->expected_err = NULL; | |
c49e0b04 RS |
2716 | OPENSSL_free(t->reason); |
2717 | t->reason = NULL; | |
ae269dd8 | 2718 | |
c49e0b04 RS |
2719 | /* Text literal. */ |
2720 | t->err = NULL; | |
2721 | t->skip = 0; | |
2722 | t->meth = NULL; | |
2723 | } | |
2724 | ||
2725 | /* | |
2726 | * Check for errors in the test structure; return 1 if okay, else 0. | |
2727 | */ | |
2728 | static int check_test_error(EVP_TEST *t) | |
2729 | { | |
2730 | unsigned long err; | |
2731 | const char *func; | |
2732 | const char *reason; | |
2733 | ||
2734 | if (t->err == NULL && t->expected_err == NULL) | |
2735 | return 1; | |
2736 | if (t->err != NULL && t->expected_err == NULL) { | |
2737 | if (t->aux_err != NULL) { | |
ae269dd8 RS |
2738 | TEST_info("%s:%d: Source of above error (%s); unexpected error %s", |
2739 | t->s.test_file, t->s.start, t->aux_err, t->err); | |
c49e0b04 | 2740 | } else { |
ae269dd8 RS |
2741 | TEST_info("%s:%d: Source of above error; unexpected error %s", |
2742 | t->s.test_file, t->s.start, t->err); | |
c49e0b04 RS |
2743 | } |
2744 | return 0; | |
2745 | } | |
2746 | if (t->err == NULL && t->expected_err != NULL) { | |
ae269dd8 RS |
2747 | TEST_info("%s:%d: Succeeded but was expecting %s", |
2748 | t->s.test_file, t->s.start, t->expected_err); | |
c49e0b04 RS |
2749 | return 0; |
2750 | } | |
2751 | ||
2752 | if (strcmp(t->err, t->expected_err) != 0) { | |
ae269dd8 RS |
2753 | TEST_info("%s:%d: Expected %s got %s", |
2754 | t->s.test_file, t->s.start, t->expected_err, t->err); | |
c49e0b04 RS |
2755 | return 0; |
2756 | } | |
2757 | ||
aac96e27 | 2758 | if (t->reason == NULL) |
c49e0b04 RS |
2759 | return 1; |
2760 | ||
aac96e27 | 2761 | if (t->reason == NULL) { |
ae269dd8 RS |
2762 | TEST_info("%s:%d: Test is missing function or reason code", |
2763 | t->s.test_file, t->s.start); | |
c49e0b04 RS |
2764 | return 0; |
2765 | } | |
2766 | ||
2767 | err = ERR_peek_error(); | |
2768 | if (err == 0) { | |
aac96e27 RS |
2769 | TEST_info("%s:%d: Expected error \"%s\" not set", |
2770 | t->s.test_file, t->s.start, t->reason); | |
c49e0b04 RS |
2771 | return 0; |
2772 | } | |
2773 | ||
2774 | func = ERR_func_error_string(err); | |
2775 | reason = ERR_reason_error_string(err); | |
2776 | if (func == NULL && reason == NULL) { | |
aac96e27 | 2777 | TEST_info("%s:%d: Expected error \"%s\", no strings available." |
ae269dd8 | 2778 | " Assuming ok.", |
aac96e27 | 2779 | t->s.test_file, t->s.start, t->reason); |
c49e0b04 RS |
2780 | return 1; |
2781 | } | |
2782 | ||
aac96e27 | 2783 | if (strcmp(reason, t->reason) == 0) |
c49e0b04 RS |
2784 | return 1; |
2785 | ||
aac96e27 RS |
2786 | TEST_info("%s:%d: Expected error \"%s\", got \"%s\"", |
2787 | t->s.test_file, t->s.start, t->reason, reason); | |
c49e0b04 RS |
2788 | |
2789 | return 0; | |
2790 | } | |
2791 | ||
2792 | /* | |
2793 | * Run a parsed test. Log a message and return 0 on error. | |
2794 | */ | |
2795 | static int run_test(EVP_TEST *t) | |
2796 | { | |
2797 | if (t->meth == NULL) | |
2798 | return 1; | |
ae269dd8 | 2799 | t->s.numtests++; |
c49e0b04 | 2800 | if (t->skip) { |
ae269dd8 | 2801 | t->s.numskip++; |
c49e0b04 RS |
2802 | } else { |
2803 | /* run the test */ | |
2804 | if (t->err == NULL && t->meth->run_test(t) != 1) { | |
ae269dd8 RS |
2805 | TEST_info("%s:%d %s error", |
2806 | t->s.test_file, t->s.start, t->meth->name); | |
c49e0b04 RS |
2807 | return 0; |
2808 | } | |
2809 | if (!check_test_error(t)) { | |
8fe3127c | 2810 | TEST_openssl_errors(); |
ae269dd8 | 2811 | t->s.errors++; |
c49e0b04 RS |
2812 | } |
2813 | } | |
2814 | ||
2815 | /* clean it up */ | |
2816 | return 1; | |
2817 | } | |
2818 | ||
2819 | static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst) | |
2820 | { | |
2821 | for (; lst != NULL; lst = lst->next) { | |
2822 | if (strcmp(lst->name, name) == 0) { | |
2823 | if (ppk != NULL) | |
2824 | *ppk = lst->key; | |
2825 | return 1; | |
2826 | } | |
2827 | } | |
2828 | return 0; | |
2829 | } | |
2830 | ||
2831 | static void free_key_list(KEY_LIST *lst) | |
2832 | { | |
2833 | while (lst != NULL) { | |
2834 | KEY_LIST *next = lst->next; | |
2835 | ||
2836 | EVP_PKEY_free(lst->key); | |
2837 | OPENSSL_free(lst->name); | |
2838 | OPENSSL_free(lst); | |
2839 | lst = next; | |
2840 | } | |
2841 | } | |
2842 | ||
c49e0b04 RS |
2843 | /* |
2844 | * Is the key type an unsupported algorithm? | |
2845 | */ | |
3cb7c5cf | 2846 | static int key_unsupported(void) |
c49e0b04 RS |
2847 | { |
2848 | long err = ERR_peek_error(); | |
2849 | ||
2850 | if (ERR_GET_LIB(err) == ERR_LIB_EVP | |
2851 | && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) { | |
2852 | ERR_clear_error(); | |
2853 | return 1; | |
2854 | } | |
2855 | #ifndef OPENSSL_NO_EC | |
2856 | /* | |
2857 | * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an | |
2858 | * hint to an unsupported algorithm/curve (e.g. if binary EC support is | |
2859 | * disabled). | |
2860 | */ | |
2861 | if (ERR_GET_LIB(err) == ERR_LIB_EC | |
2862 | && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) { | |
2863 | ERR_clear_error(); | |
2864 | return 1; | |
2865 | } | |
2866 | #endif /* OPENSSL_NO_EC */ | |
2867 | return 0; | |
2868 | } | |
2869 | ||
2870 | /* | |
ae269dd8 | 2871 | * NULL out the value from |pp| but return it. This "steals" a pointer. |
c49e0b04 | 2872 | */ |
ae269dd8 | 2873 | static char *take_value(PAIR *pp) |
c49e0b04 | 2874 | { |
ae269dd8 RS |
2875 | char *p = pp->value; |
2876 | ||
2877 | pp->value = NULL; | |
2878 | return p; | |
2879 | } | |
2880 | ||
3b5d61f4 RL |
2881 | /* |
2882 | * Return 1 if one of the providers named in the string is available. | |
2883 | * The provider names are separated with whitespace. | |
2884 | * NOTE: destructive function, it inserts '\0' after each provider name. | |
2885 | */ | |
2886 | static int prov_available(char *providers) | |
2887 | { | |
2888 | char *p; | |
2889 | int more = 1; | |
2890 | ||
2891 | while (more) { | |
2892 | for (; isspace(*providers); providers++) | |
2893 | continue; | |
2894 | if (*providers == '\0') | |
2895 | break; /* End of the road */ | |
2896 | for (p = providers; *p != '\0' && !isspace(*p); p++) | |
2897 | continue; | |
2898 | if (*p == '\0') | |
2899 | more = 0; | |
2900 | else | |
2901 | *p = '\0'; | |
2902 | if (OSSL_PROVIDER_available(NULL, providers)) | |
2903 | return 1; /* Found one */ | |
2904 | } | |
2905 | return 0; | |
2906 | } | |
2907 | ||
ae269dd8 RS |
2908 | /* |
2909 | * Read and parse one test. Return 0 if failure, 1 if okay. | |
2910 | */ | |
2911 | static int parse(EVP_TEST *t) | |
2912 | { | |
2913 | KEY_LIST *key, **klist; | |
c49e0b04 | 2914 | EVP_PKEY *pkey; |
ae269dd8 RS |
2915 | PAIR *pp; |
2916 | int i; | |
c49e0b04 | 2917 | |
c49e0b04 | 2918 | top: |
ae269dd8 RS |
2919 | do { |
2920 | if (BIO_eof(t->s.fp)) | |
c49e0b04 | 2921 | return EOF; |
ae269dd8 RS |
2922 | clear_test(t); |
2923 | if (!test_readstanza(&t->s)) | |
2924 | return 0; | |
2925 | } while (t->s.numpairs == 0); | |
2926 | pp = &t->s.pairs[0]; | |
c49e0b04 | 2927 | |
ae269dd8 | 2928 | /* Are we adding a key? */ |
c49e0b04 RS |
2929 | klist = NULL; |
2930 | pkey = NULL; | |
ae269dd8 RS |
2931 | if (strcmp(pp->key, "PrivateKey") == 0) { |
2932 | pkey = PEM_read_bio_PrivateKey(t->s.key, NULL, 0, NULL); | |
c49e0b04 | 2933 | if (pkey == NULL && !key_unsupported()) { |
1bf2cc23 | 2934 | EVP_PKEY_free(pkey); |
ae269dd8 | 2935 | TEST_info("Can't read private key %s", pp->value); |
8fe3127c | 2936 | TEST_openssl_errors(); |
c49e0b04 RS |
2937 | return 0; |
2938 | } | |
2939 | klist = &private_keys; | |
4665244c | 2940 | } else if (strcmp(pp->key, "PublicKey") == 0) { |
ae269dd8 | 2941 | pkey = PEM_read_bio_PUBKEY(t->s.key, NULL, 0, NULL); |
c49e0b04 | 2942 | if (pkey == NULL && !key_unsupported()) { |
1bf2cc23 | 2943 | EVP_PKEY_free(pkey); |
ae269dd8 | 2944 | TEST_info("Can't read public key %s", pp->value); |
8fe3127c | 2945 | TEST_openssl_errors(); |
c49e0b04 RS |
2946 | return 0; |
2947 | } | |
2948 | klist = &public_keys; | |
4665244c MC |
2949 | } else if (strcmp(pp->key, "PrivateKeyRaw") == 0 |
2950 | || strcmp(pp->key, "PublicKeyRaw") == 0 ) { | |
2951 | char *strnid = NULL, *keydata = NULL; | |
2952 | unsigned char *keybin; | |
2953 | size_t keylen; | |
2954 | int nid; | |
2955 | ||
2956 | if (strcmp(pp->key, "PrivateKeyRaw") == 0) | |
2957 | klist = &private_keys; | |
2958 | else | |
2959 | klist = &public_keys; | |
2960 | ||
2961 | strnid = strchr(pp->value, ':'); | |
2962 | if (strnid != NULL) { | |
2963 | *strnid++ = '\0'; | |
2964 | keydata = strchr(strnid, ':'); | |
2965 | if (keydata != NULL) | |
2966 | *keydata++ = '\0'; | |
2967 | } | |
2968 | if (keydata == NULL) { | |
2969 | TEST_info("Failed to parse %s value", pp->key); | |
2970 | return 0; | |
2971 | } | |
2972 | ||
2973 | nid = OBJ_txt2nid(strnid); | |
2974 | if (nid == NID_undef) { | |
2975 | TEST_info("Uncrecognised algorithm NID"); | |
2976 | return 0; | |
2977 | } | |
2978 | if (!parse_bin(keydata, &keybin, &keylen)) { | |
2979 | TEST_info("Failed to create binary key"); | |
2980 | return 0; | |
2981 | } | |
2982 | if (klist == &private_keys) | |
f929439f | 2983 | pkey = EVP_PKEY_new_raw_private_key(nid, NULL, keybin, keylen); |
4665244c | 2984 | else |
f929439f | 2985 | pkey = EVP_PKEY_new_raw_public_key(nid, NULL, keybin, keylen); |
66a925ea | 2986 | if (pkey == NULL && !key_unsupported()) { |
4665244c MC |
2987 | TEST_info("Can't read %s data", pp->key); |
2988 | OPENSSL_free(keybin); | |
2989 | TEST_openssl_errors(); | |
2990 | return 0; | |
2991 | } | |
2992 | OPENSSL_free(keybin); | |
c49e0b04 RS |
2993 | } |
2994 | ||
2995 | /* If we have a key add to list */ | |
2996 | if (klist != NULL) { | |
ae269dd8 RS |
2997 | if (find_key(NULL, pp->value, *klist)) { |
2998 | TEST_info("Duplicate key %s", pp->value); | |
c49e0b04 RS |
2999 | return 0; |
3000 | } | |
ae269dd8 | 3001 | if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key)))) |
c49e0b04 | 3002 | return 0; |
ae269dd8 | 3003 | key->name = take_value(pp); |
ddb634fe JL |
3004 | |
3005 | /* Hack to detect SM2 keys */ | |
c86acc9f MC |
3006 | if(pkey != NULL && strstr(key->name, "SM2") != NULL) { |
3007 | #ifdef OPENSSL_NO_SM2 | |
3008 | EVP_PKEY_free(pkey); | |
3009 | pkey = NULL; | |
3010 | #else | |
ddb634fe | 3011 | EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2); |
c86acc9f MC |
3012 | #endif |
3013 | } | |
ddb634fe | 3014 | |
c49e0b04 RS |
3015 | key->key = pkey; |
3016 | key->next = *klist; | |
3017 | *klist = key; | |
3018 | ||
3019 | /* Go back and start a new stanza. */ | |
ae269dd8 RS |
3020 | if (t->s.numpairs != 1) |
3021 | TEST_info("Line %d: missing blank line\n", t->s.curr); | |
c49e0b04 RS |
3022 | goto top; |
3023 | } | |
3024 | ||
ae269dd8 RS |
3025 | /* Find the test, based on first keyword. */ |
3026 | if (!TEST_ptr(t->meth = find_test(pp->key))) | |
3027 | return 0; | |
3028 | if (!t->meth->init(t, pp->value)) { | |
3029 | TEST_error("unknown %s: %s\n", pp->key, pp->value); | |
3030 | return 0; | |
c49e0b04 RS |
3031 | } |
3032 | if (t->skip == 1) { | |
ae269dd8 RS |
3033 | /* TEST_info("skipping %s %s", pp->key, pp->value); */ |
3034 | return 0; | |
c49e0b04 RS |
3035 | } |
3036 | ||
ae269dd8 | 3037 | for (pp++, i = 1; i < t->s.numpairs; pp++, i++) { |
3b5d61f4 RL |
3038 | if (strcmp(pp->key, "Availablein") == 0) { |
3039 | if (!prov_available(pp->value)) { | |
3040 | TEST_info("skipping, providers not available: %s:%d", | |
3041 | t->s.test_file, t->s.start); | |
3042 | t->skip = 1; | |
3043 | return 0; | |
3044 | } | |
8453096e | 3045 | } else if (strcmp(pp->key, "Result") == 0) { |
c49e0b04 | 3046 | if (t->expected_err != NULL) { |
ae269dd8 RS |
3047 | TEST_info("Line %d: multiple result lines", t->s.curr); |
3048 | return 0; | |
c49e0b04 | 3049 | } |
ae269dd8 RS |
3050 | t->expected_err = take_value(pp); |
3051 | } else if (strcmp(pp->key, "Function") == 0) { | |
aac96e27 | 3052 | /* Ignore old line. */ |
ae269dd8 | 3053 | } else if (strcmp(pp->key, "Reason") == 0) { |
c49e0b04 | 3054 | if (t->reason != NULL) { |
ae269dd8 RS |
3055 | TEST_info("Line %d: multiple reason lines", t->s.curr); |
3056 | return 0; | |
c49e0b04 | 3057 | } |
ae269dd8 | 3058 | t->reason = take_value(pp); |
c49e0b04 RS |
3059 | } else { |
3060 | /* Must be test specific line: try to parse it */ | |
ae269dd8 | 3061 | int rv = t->meth->parse(t, pp->key, pp->value); |
c49e0b04 RS |
3062 | |
3063 | if (rv == 0) { | |
ae269dd8 RS |
3064 | TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key); |
3065 | return 0; | |
c49e0b04 RS |
3066 | } |
3067 | if (rv < 0) { | |
ce5d64c7 RL |
3068 | TEST_info("Line %d: error processing keyword %s = %s\n", |
3069 | t->s.curr, pp->key, pp->value); | |
ae269dd8 | 3070 | return 0; |
c49e0b04 RS |
3071 | } |
3072 | } | |
3073 | } | |
3074 | ||
3075 | return 1; | |
c49e0b04 RS |
3076 | } |
3077 | ||
ae269dd8 | 3078 | static int run_file_tests(int i) |
6c5943c9 | 3079 | { |
ae269dd8 | 3080 | EVP_TEST *t; |
ad887416 | 3081 | const char *testfile = test_get_argument(i); |
c49e0b04 | 3082 | int c; |
6c5943c9 | 3083 | |
ae269dd8 | 3084 | if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t)))) |
6c5943c9 | 3085 | return 0; |
ad887416 | 3086 | if (!test_start_file(&t->s, testfile)) { |
ae269dd8 RS |
3087 | OPENSSL_free(t); |
3088 | return 0; | |
3089 | } | |
c49e0b04 | 3090 | |
ae269dd8 RS |
3091 | while (!BIO_eof(t->s.fp)) { |
3092 | c = parse(t); | |
d5e5e2ff SL |
3093 | if (t->skip) { |
3094 | t->s.numskip++; | |
c49e0b04 | 3095 | continue; |
d5e5e2ff | 3096 | } |
ae269dd8 RS |
3097 | if (c == 0 || !run_test(t)) { |
3098 | t->s.errors++; | |
c49e0b04 RS |
3099 | break; |
3100 | } | |
6c5943c9 | 3101 | } |
ae269dd8 RS |
3102 | test_end_file(&t->s); |
3103 | clear_test(t); | |
6c5943c9 | 3104 | |
6c5943c9 RS |
3105 | free_key_list(public_keys); |
3106 | free_key_list(private_keys); | |
ae269dd8 RS |
3107 | BIO_free(t->s.key); |
3108 | c = t->s.errors; | |
3109 | OPENSSL_free(t); | |
3110 | return c == 0; | |
6c5943c9 RS |
3111 | } |
3112 | ||
a43ce58f SL |
3113 | OPT_TEST_DECLARE_USAGE("file...\n") |
3114 | ||
ad887416 | 3115 | int setup_tests(void) |
6c5943c9 | 3116 | { |
ad887416 P |
3117 | size_t n = test_get_argument_count(); |
3118 | ||
a43ce58f | 3119 | if (n == 0) |
6c5943c9 | 3120 | return 0; |
6c5943c9 | 3121 | |
ad887416 P |
3122 | ADD_ALL_TESTS(run_file_tests, n); |
3123 | return 1; | |
6c5943c9 | 3124 | } |