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