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