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