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