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