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