]>
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; | |
841 | /* Input to MAC */ | |
842 | unsigned char *input; | |
843 | size_t input_len; | |
844 | /* Expected output */ | |
845 | unsigned char *output; | |
846 | size_t output_len; | |
7e6a3025 RL |
847 | /* Collection of controls */ |
848 | STACK_OF(OPENSSL_STRING) *controls; | |
6c5943c9 | 849 | } MAC_DATA; |
83251f39 | 850 | |
6c5943c9 | 851 | static int mac_test_init(EVP_TEST *t, const char *alg) |
83251f39 | 852 | { |
2bdb4af5 RL |
853 | const EVP_MAC *mac = NULL; |
854 | int type = NID_undef; | |
6c5943c9 RS |
855 | MAC_DATA *mdat; |
856 | ||
2bdb4af5 RL |
857 | if ((mac = EVP_get_macbyname(alg)) == NULL) { |
858 | /* | |
859 | * Since we didn't find an EVP_MAC, we check for known EVP_PKEY methods | |
860 | * For debugging purposes, we allow 'NNNN by EVP_PKEY' to force running | |
861 | * the EVP_PKEY method. | |
862 | */ | |
863 | size_t sz = strlen(alg); | |
864 | static const char epilogue[] = " by EVP_PKEY"; | |
865 | ||
866 | if (strcmp(alg + sz - (sizeof(epilogue) - 1), epilogue) == 0) | |
867 | sz -= sizeof(epilogue) - 1; | |
868 | ||
869 | if (strncmp(alg, "HMAC", sz) == 0) { | |
870 | type = EVP_PKEY_HMAC; | |
871 | } else if (strncmp(alg, "CMAC", sz) == 0) { | |
b4a3aeeb | 872 | #ifndef OPENSSL_NO_CMAC |
2bdb4af5 | 873 | type = EVP_PKEY_CMAC; |
b4a3aeeb | 874 | #else |
2bdb4af5 RL |
875 | t->skip = 1; |
876 | return 1; | |
52ad5b60 | 877 | #endif |
2bdb4af5 | 878 | } else if (strncmp(alg, "Poly1305", sz) == 0) { |
52ad5b60 | 879 | #ifndef OPENSSL_NO_POLY1305 |
2bdb4af5 | 880 | type = EVP_PKEY_POLY1305; |
52ad5b60 | 881 | #else |
2bdb4af5 RL |
882 | t->skip = 1; |
883 | return 1; | |
3f5616d7 | 884 | #endif |
2bdb4af5 | 885 | } else if (strncmp(alg, "SipHash", sz) == 0) { |
3f5616d7 | 886 | #ifndef OPENSSL_NO_SIPHASH |
2bdb4af5 | 887 | type = EVP_PKEY_SIPHASH; |
3f5616d7 | 888 | #else |
2bdb4af5 RL |
889 | t->skip = 1; |
890 | return 1; | |
b4a3aeeb | 891 | #endif |
2bdb4af5 RL |
892 | } else { |
893 | /* | |
894 | * Not a known EVP_PKEY method either. If it's a known OID, then | |
895 | * assume it's been disabled. | |
896 | */ | |
897 | if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) { | |
898 | t->skip = 1; | |
899 | return 1; | |
900 | } | |
901 | ||
902 | return 0; | |
903 | } | |
904 | } | |
83251f39 | 905 | |
6c5943c9 | 906 | mdat = OPENSSL_zalloc(sizeof(*mdat)); |
83251f39 | 907 | mdat->type = type; |
2bdb4af5 | 908 | mdat->mac = mac; |
7e6a3025 | 909 | mdat->controls = sk_OPENSSL_STRING_new_null(); |
83251f39 DSH |
910 | t->data = mdat; |
911 | return 1; | |
912 | } | |
913 | ||
7e6a3025 RL |
914 | /* Because OPENSSL_free is a macro, it can't be passed as a function pointer */ |
915 | static void openssl_free(char *m) | |
916 | { | |
917 | OPENSSL_free(m); | |
918 | } | |
919 | ||
6c5943c9 | 920 | static void mac_test_cleanup(EVP_TEST *t) |
83251f39 | 921 | { |
6c5943c9 RS |
922 | MAC_DATA *mdat = t->data; |
923 | ||
7e6a3025 | 924 | sk_OPENSSL_STRING_pop_free(mdat->controls, openssl_free); |
6c5943c9 RS |
925 | OPENSSL_free(mdat->alg); |
926 | OPENSSL_free(mdat->key); | |
927 | OPENSSL_free(mdat->input); | |
928 | OPENSSL_free(mdat->output); | |
83251f39 DSH |
929 | } |
930 | ||
6c5943c9 | 931 | static int mac_test_parse(EVP_TEST *t, |
83251f39 DSH |
932 | const char *keyword, const char *value) |
933 | { | |
6c5943c9 RS |
934 | MAC_DATA *mdata = t->data; |
935 | ||
86885c28 | 936 | if (strcmp(keyword, "Key") == 0) |
c49e0b04 | 937 | return parse_bin(value, &mdata->key, &mdata->key_len); |
86885c28 | 938 | if (strcmp(keyword, "Algorithm") == 0) { |
7644a9ae | 939 | mdata->alg = OPENSSL_strdup(value); |
83251f39 DSH |
940 | if (!mdata->alg) |
941 | return 0; | |
942 | return 1; | |
943 | } | |
86885c28 | 944 | if (strcmp(keyword, "Input") == 0) |
c49e0b04 | 945 | return parse_bin(value, &mdata->input, &mdata->input_len); |
86885c28 | 946 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 947 | return parse_bin(value, &mdata->output, &mdata->output_len); |
7e6a3025 RL |
948 | if (strcmp(keyword, "Ctrl") == 0) |
949 | return sk_OPENSSL_STRING_push(mdata->controls, | |
950 | OPENSSL_strdup(value)) != 0; | |
83251f39 DSH |
951 | return 0; |
952 | } | |
953 | ||
ce5d64c7 RL |
954 | static int mac_test_ctrl_pkey(EVP_TEST *t, EVP_PKEY_CTX *pctx, |
955 | const char *value) | |
956 | { | |
957 | int rv; | |
958 | char *p, *tmpval; | |
959 | ||
960 | if (!TEST_ptr(tmpval = OPENSSL_strdup(value))) | |
961 | return 0; | |
962 | p = strchr(tmpval, ':'); | |
963 | if (p != NULL) | |
964 | *p++ = '\0'; | |
965 | rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p); | |
966 | if (rv == -2) | |
967 | t->err = "PKEY_CTRL_INVALID"; | |
968 | else if (rv <= 0) | |
969 | t->err = "PKEY_CTRL_ERROR"; | |
970 | else | |
971 | rv = 1; | |
972 | OPENSSL_free(tmpval); | |
973 | return rv > 0; | |
974 | } | |
975 | ||
2bdb4af5 | 976 | static int mac_test_run_pkey(EVP_TEST *t) |
83251f39 | 977 | { |
e3d378bc | 978 | MAC_DATA *expected = t->data; |
83251f39 DSH |
979 | EVP_MD_CTX *mctx = NULL; |
980 | EVP_PKEY_CTX *pctx = NULL, *genctx = NULL; | |
981 | EVP_PKEY *key = NULL; | |
982 | const EVP_MD *md = NULL; | |
e3d378bc AP |
983 | unsigned char *got = NULL; |
984 | size_t got_len; | |
7e6a3025 | 985 | int i; |
83251f39 | 986 | |
2bdb4af5 RL |
987 | if (expected->alg == NULL) |
988 | TEST_info("Trying the EVP_PKEY %s test", OBJ_nid2sn(expected->type)); | |
989 | else | |
990 | TEST_info("Trying the EVP_PKEY %s test with %s", | |
991 | OBJ_nid2sn(expected->type), expected->alg); | |
992 | ||
96bea000 | 993 | #ifdef OPENSSL_NO_DES |
e3d378bc | 994 | if (expected->alg != NULL && strstr(expected->alg, "DES") != NULL) { |
96bea000 | 995 | /* Skip DES */ |
6c5943c9 | 996 | t->err = NULL; |
96bea000 MC |
997 | goto err; |
998 | } | |
999 | #endif | |
1000 | ||
9442c8d7 MC |
1001 | if (expected->type == EVP_PKEY_CMAC) |
1002 | key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len, | |
1003 | EVP_get_cipherbyname(expected->alg)); | |
1004 | else | |
f929439f MC |
1005 | key = EVP_PKEY_new_raw_private_key(expected->type, NULL, expected->key, |
1006 | expected->key_len); | |
9442c8d7 MC |
1007 | if (key == NULL) { |
1008 | t->err = "MAC_KEY_CREATE_ERROR"; | |
83251f39 | 1009 | goto err; |
6c5943c9 | 1010 | } |
83251f39 | 1011 | |
e3d378bc AP |
1012 | if (expected->type == EVP_PKEY_HMAC) { |
1013 | if (!TEST_ptr(md = EVP_get_digestbyname(expected->alg))) { | |
6c5943c9 | 1014 | t->err = "MAC_ALGORITHM_SET_ERROR"; |
83251f39 | 1015 | goto err; |
6c5943c9 | 1016 | } |
83251f39 | 1017 | } |
6c5943c9 RS |
1018 | if (!TEST_ptr(mctx = EVP_MD_CTX_new())) { |
1019 | t->err = "INTERNAL_ERROR"; | |
83251f39 | 1020 | goto err; |
6c5943c9 RS |
1021 | } |
1022 | if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key)) { | |
1023 | t->err = "DIGESTSIGNINIT_ERROR"; | |
83251f39 | 1024 | goto err; |
6c5943c9 | 1025 | } |
7e6a3025 | 1026 | for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) |
ce5d64c7 RL |
1027 | if (!mac_test_ctrl_pkey(t, pctx, |
1028 | sk_OPENSSL_STRING_value(expected->controls, | |
1029 | i))) { | |
7e6a3025 RL |
1030 | t->err = "EVPPKEYCTXCTRL_ERROR"; |
1031 | goto err; | |
1032 | } | |
e3d378bc | 1033 | if (!EVP_DigestSignUpdate(mctx, expected->input, expected->input_len)) { |
6c5943c9 | 1034 | t->err = "DIGESTSIGNUPDATE_ERROR"; |
83251f39 | 1035 | goto err; |
83251f39 | 1036 | } |
e3d378bc | 1037 | if (!EVP_DigestSignFinal(mctx, NULL, &got_len)) { |
6c5943c9 | 1038 | t->err = "DIGESTSIGNFINAL_LENGTH_ERROR"; |
83251f39 | 1039 | goto err; |
6c5943c9 | 1040 | } |
e3d378bc | 1041 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { |
6c5943c9 | 1042 | t->err = "TEST_FAILURE"; |
83251f39 | 1043 | goto err; |
6c5943c9 | 1044 | } |
e3d378bc | 1045 | if (!EVP_DigestSignFinal(mctx, got, &got_len) |
4cceb185 P |
1046 | || !memory_err_compare(t, "TEST_MAC_ERR", |
1047 | expected->output, expected->output_len, | |
1048 | got, got_len)) { | |
41248607 RS |
1049 | t->err = "TEST_MAC_ERR"; |
1050 | goto err; | |
1051 | } | |
6c5943c9 | 1052 | t->err = NULL; |
83251f39 | 1053 | err: |
bfb0641f | 1054 | EVP_MD_CTX_free(mctx); |
e3d378bc | 1055 | OPENSSL_free(got); |
c5ba2d99 RS |
1056 | EVP_PKEY_CTX_free(genctx); |
1057 | EVP_PKEY_free(key); | |
83251f39 DSH |
1058 | return 1; |
1059 | } | |
1060 | ||
2bdb4af5 RL |
1061 | static int mac_test_run_mac(EVP_TEST *t) |
1062 | { | |
1063 | MAC_DATA *expected = t->data; | |
1064 | EVP_MAC_CTX *ctx = NULL; | |
1065 | const void *algo = NULL; | |
1066 | int algo_ctrl = 0; | |
1067 | unsigned char *got = NULL; | |
1068 | size_t got_len; | |
1069 | int rv, i; | |
1070 | ||
1071 | if (expected->alg == NULL) | |
1072 | TEST_info("Trying the EVP_MAC %s test", EVP_MAC_name(expected->mac)); | |
1073 | else | |
1074 | TEST_info("Trying the EVP_MAC %s test with %s", | |
1075 | EVP_MAC_name(expected->mac), expected->alg); | |
1076 | ||
1077 | #ifdef OPENSSL_NO_DES | |
1078 | if (expected->alg != NULL && strstr(expected->alg, "DES") != NULL) { | |
1079 | /* Skip DES */ | |
1080 | t->err = NULL; | |
1081 | goto err; | |
1082 | } | |
1083 | #endif | |
1084 | ||
1085 | if ((ctx = EVP_MAC_CTX_new(expected->mac)) == NULL) { | |
1086 | t->err = "MAC_CREATE_ERROR"; | |
1087 | goto err; | |
1088 | } | |
1089 | ||
1090 | if (expected->alg != NULL | |
1091 | && ((algo_ctrl = EVP_MAC_CTRL_SET_CIPHER, | |
1092 | algo = EVP_get_cipherbyname(expected->alg)) == NULL | |
1093 | && (algo_ctrl = EVP_MAC_CTRL_SET_MD, | |
1094 | algo = EVP_get_digestbyname(expected->alg)) == NULL)) { | |
1095 | t->err = "MAC_BAD_ALGORITHM"; | |
1096 | goto err; | |
1097 | } | |
1098 | ||
1099 | ||
1100 | if (algo_ctrl != 0) { | |
1101 | rv = EVP_MAC_ctrl(ctx, algo_ctrl, algo); | |
1102 | if (rv == -2) { | |
1103 | t->err = "MAC_CTRL_INVALID"; | |
1104 | goto err; | |
1105 | } else if (rv <= 0) { | |
1106 | t->err = "MAC_CTRL_ERROR"; | |
1107 | goto err; | |
1108 | } | |
1109 | } | |
1110 | ||
1111 | rv = EVP_MAC_ctrl(ctx, EVP_MAC_CTRL_SET_KEY, | |
1112 | expected->key, expected->key_len); | |
1113 | if (rv == -2) { | |
1114 | t->err = "MAC_CTRL_INVALID"; | |
1115 | goto err; | |
1116 | } else if (rv <= 0) { | |
1117 | t->err = "MAC_CTRL_ERROR"; | |
1118 | goto err; | |
1119 | } | |
1120 | ||
1121 | if (!EVP_MAC_init(ctx)) { | |
1122 | t->err = "MAC_INIT_ERROR"; | |
1123 | goto err; | |
1124 | } | |
1125 | for (i = 0; i < sk_OPENSSL_STRING_num(expected->controls); i++) { | |
1126 | char *p, *tmpval; | |
1127 | char *value = sk_OPENSSL_STRING_value(expected->controls, i); | |
1128 | ||
1129 | if (!TEST_ptr(tmpval = OPENSSL_strdup(value))) { | |
1130 | t->err = "MAC_CTRL_ERROR"; | |
1131 | goto err; | |
1132 | } | |
1133 | p = strchr(tmpval, ':'); | |
1134 | if (p != NULL) | |
1135 | *p++ = '\0'; | |
1136 | rv = EVP_MAC_ctrl_str(ctx, tmpval, p); | |
1137 | OPENSSL_free(tmpval); | |
1138 | if (rv == -2) { | |
1139 | t->err = "MAC_CTRL_INVALID"; | |
1140 | goto err; | |
1141 | } else if (rv <= 0) { | |
1142 | t->err = "MAC_CTRL_ERROR"; | |
1143 | goto err; | |
1144 | } | |
1145 | } | |
1146 | if (!EVP_MAC_update(ctx, expected->input, expected->input_len)) { | |
1147 | t->err = "MAC_UPDATE_ERROR"; | |
1148 | goto err; | |
1149 | } | |
1150 | if (!EVP_MAC_final(ctx, NULL, &got_len)) { | |
1151 | t->err = "MAC_FINAL_LENGTH_ERROR"; | |
1152 | goto err; | |
1153 | } | |
1154 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { | |
1155 | t->err = "TEST_FAILURE"; | |
1156 | goto err; | |
1157 | } | |
1158 | if (!EVP_MAC_final(ctx, got, &got_len) | |
1159 | || !memory_err_compare(t, "TEST_MAC_ERR", | |
1160 | expected->output, expected->output_len, | |
1161 | got, got_len)) { | |
1162 | t->err = "TEST_MAC_ERR"; | |
1163 | goto err; | |
1164 | } | |
1165 | t->err = NULL; | |
1166 | err: | |
1167 | EVP_MAC_CTX_free(ctx); | |
1168 | OPENSSL_free(got); | |
1169 | return 1; | |
1170 | } | |
1171 | ||
1172 | static int mac_test_run(EVP_TEST *t) | |
1173 | { | |
1174 | MAC_DATA *expected = t->data; | |
1175 | ||
1176 | if (expected->mac != NULL) | |
1177 | return mac_test_run_mac(t); | |
1178 | return mac_test_run_pkey(t); | |
1179 | } | |
1180 | ||
6c5943c9 | 1181 | static const EVP_TEST_METHOD mac_test_method = { |
83251f39 DSH |
1182 | "MAC", |
1183 | mac_test_init, | |
1184 | mac_test_cleanup, | |
1185 | mac_test_parse, | |
1186 | mac_test_run | |
1187 | }; | |
5824cc29 | 1188 | |
c49e0b04 RS |
1189 | |
1190 | /** | |
1191 | *** PUBLIC KEY TESTS | |
1192 | *** These are all very similar and share much common code. | |
1193 | **/ | |
5824cc29 | 1194 | |
6c5943c9 | 1195 | typedef struct pkey_data_st { |
5824cc29 DSH |
1196 | /* Context for this operation */ |
1197 | EVP_PKEY_CTX *ctx; | |
1198 | /* Key operation to perform */ | |
1199 | int (*keyop) (EVP_PKEY_CTX *ctx, | |
1200 | unsigned char *sig, size_t *siglen, | |
1201 | const unsigned char *tbs, size_t tbslen); | |
1202 | /* Input to MAC */ | |
1203 | unsigned char *input; | |
1204 | size_t input_len; | |
1205 | /* Expected output */ | |
1206 | unsigned char *output; | |
1207 | size_t output_len; | |
6c5943c9 | 1208 | } PKEY_DATA; |
5824cc29 DSH |
1209 | |
1210 | /* | |
1211 | * Perform public key operation setup: lookup key, allocated ctx and call | |
1212 | * the appropriate initialisation function | |
1213 | */ | |
6c5943c9 | 1214 | static int pkey_test_init(EVP_TEST *t, const char *name, |
5824cc29 DSH |
1215 | int use_public, |
1216 | int (*keyopinit) (EVP_PKEY_CTX *ctx), | |
c49e0b04 RS |
1217 | int (*keyop)(EVP_PKEY_CTX *ctx, |
1218 | unsigned char *sig, size_t *siglen, | |
1219 | const unsigned char *tbs, | |
1220 | size_t tbslen)) | |
5824cc29 | 1221 | { |
6c5943c9 | 1222 | PKEY_DATA *kdata; |
5824cc29 | 1223 | EVP_PKEY *pkey = NULL; |
7a6c9792 | 1224 | int rv = 0; |
6c5943c9 | 1225 | |
7a6c9792 | 1226 | if (use_public) |
6c5943c9 RS |
1227 | rv = find_key(&pkey, name, public_keys); |
1228 | if (rv == 0) | |
1229 | rv = find_key(&pkey, name, private_keys); | |
1230 | if (rv == 0 || pkey == NULL) { | |
7a6c9792 DSH |
1231 | t->skip = 1; |
1232 | return 1; | |
1233 | } | |
1234 | ||
c49e0b04 | 1235 | if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) { |
7a6c9792 | 1236 | EVP_PKEY_free(pkey); |
5824cc29 | 1237 | return 0; |
7a6c9792 | 1238 | } |
5824cc29 | 1239 | kdata->keyop = keyop; |
9e206ce5 P |
1240 | if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL))) { |
1241 | EVP_PKEY_free(pkey); | |
1242 | OPENSSL_free(kdata); | |
5824cc29 | 1243 | return 0; |
9e206ce5 | 1244 | } |
5824cc29 | 1245 | if (keyopinit(kdata->ctx) <= 0) |
cce65266 | 1246 | t->err = "KEYOP_INIT_ERROR"; |
c49e0b04 | 1247 | t->data = kdata; |
5824cc29 DSH |
1248 | return 1; |
1249 | } | |
1250 | ||
6c5943c9 | 1251 | static void pkey_test_cleanup(EVP_TEST *t) |
5824cc29 | 1252 | { |
6c5943c9 | 1253 | PKEY_DATA *kdata = t->data; |
b548a1f1 RS |
1254 | |
1255 | OPENSSL_free(kdata->input); | |
1256 | OPENSSL_free(kdata->output); | |
c5ba2d99 | 1257 | EVP_PKEY_CTX_free(kdata->ctx); |
5824cc29 DSH |
1258 | } |
1259 | ||
6c5943c9 | 1260 | static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx, |
dfbdf4ab | 1261 | const char *value) |
4ddd5ace DSH |
1262 | { |
1263 | int rv; | |
1264 | char *p, *tmpval; | |
1265 | ||
6c5943c9 | 1266 | if (!TEST_ptr(tmpval = OPENSSL_strdup(value))) |
4ddd5ace DSH |
1267 | return 0; |
1268 | p = strchr(tmpval, ':'); | |
1269 | if (p != NULL) | |
c49e0b04 | 1270 | *p++ = '\0'; |
4ddd5ace | 1271 | rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p); |
cce65266 DSH |
1272 | if (rv == -2) { |
1273 | t->err = "PKEY_CTRL_INVALID"; | |
1274 | rv = 1; | |
1275 | } else if (p != NULL && rv <= 0) { | |
1276 | /* If p has an OID and lookup fails assume disabled algorithm */ | |
1277 | int nid = OBJ_sn2nid(p); | |
6c5943c9 | 1278 | |
cce65266 DSH |
1279 | if (nid == NID_undef) |
1280 | nid = OBJ_ln2nid(p); | |
c49e0b04 RS |
1281 | if (nid != NID_undef |
1282 | && EVP_get_digestbynid(nid) == NULL | |
1283 | && EVP_get_cipherbynid(nid) == NULL) { | |
dfbdf4ab RL |
1284 | t->skip = 1; |
1285 | rv = 1; | |
cce65266 DSH |
1286 | } else { |
1287 | t->err = "PKEY_CTRL_ERROR"; | |
1288 | rv = 1; | |
dfbdf4ab RL |
1289 | } |
1290 | } | |
4ddd5ace DSH |
1291 | OPENSSL_free(tmpval); |
1292 | return rv > 0; | |
1293 | } | |
1294 | ||
6c5943c9 | 1295 | static int pkey_test_parse(EVP_TEST *t, |
5824cc29 DSH |
1296 | const char *keyword, const char *value) |
1297 | { | |
6c5943c9 | 1298 | PKEY_DATA *kdata = t->data; |
86885c28 | 1299 | if (strcmp(keyword, "Input") == 0) |
c49e0b04 | 1300 | return parse_bin(value, &kdata->input, &kdata->input_len); |
86885c28 | 1301 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 1302 | return parse_bin(value, &kdata->output, &kdata->output_len); |
4ddd5ace | 1303 | if (strcmp(keyword, "Ctrl") == 0) |
dfbdf4ab | 1304 | return pkey_test_ctrl(t, kdata->ctx, value); |
5824cc29 DSH |
1305 | return 0; |
1306 | } | |
1307 | ||
6c5943c9 | 1308 | static int pkey_test_run(EVP_TEST *t) |
5824cc29 | 1309 | { |
e3d378bc AP |
1310 | PKEY_DATA *expected = t->data; |
1311 | unsigned char *got = NULL; | |
1312 | size_t got_len; | |
6c5943c9 | 1313 | |
e3d378bc AP |
1314 | if (expected->keyop(expected->ctx, NULL, &got_len, |
1315 | expected->input, expected->input_len) <= 0 | |
1316 | || !TEST_ptr(got = OPENSSL_malloc(got_len))) { | |
6c5943c9 | 1317 | t->err = "KEYOP_LENGTH_ERROR"; |
5824cc29 | 1318 | goto err; |
6c5943c9 | 1319 | } |
e3d378bc AP |
1320 | if (expected->keyop(expected->ctx, got, &got_len, |
1321 | expected->input, expected->input_len) <= 0) { | |
6c5943c9 | 1322 | t->err = "KEYOP_ERROR"; |
5824cc29 | 1323 | goto err; |
6c5943c9 | 1324 | } |
4cceb185 P |
1325 | if (!memory_err_compare(t, "KEYOP_MISMATCH", |
1326 | expected->output, expected->output_len, | |
1327 | got, got_len)) | |
5824cc29 | 1328 | goto err; |
4cceb185 | 1329 | |
6c5943c9 | 1330 | t->err = NULL; |
5824cc29 | 1331 | err: |
e3d378bc | 1332 | OPENSSL_free(got); |
5824cc29 DSH |
1333 | return 1; |
1334 | } | |
1335 | ||
6c5943c9 | 1336 | static int sign_test_init(EVP_TEST *t, const char *name) |
5824cc29 DSH |
1337 | { |
1338 | return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign); | |
1339 | } | |
1340 | ||
6c5943c9 | 1341 | static const EVP_TEST_METHOD psign_test_method = { |
5824cc29 DSH |
1342 | "Sign", |
1343 | sign_test_init, | |
1344 | pkey_test_cleanup, | |
1345 | pkey_test_parse, | |
1346 | pkey_test_run | |
1347 | }; | |
1348 | ||
6c5943c9 | 1349 | static int verify_recover_test_init(EVP_TEST *t, const char *name) |
5824cc29 DSH |
1350 | { |
1351 | return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init, | |
1352 | EVP_PKEY_verify_recover); | |
1353 | } | |
1354 | ||
6c5943c9 | 1355 | static const EVP_TEST_METHOD pverify_recover_test_method = { |
5824cc29 DSH |
1356 | "VerifyRecover", |
1357 | verify_recover_test_init, | |
1358 | pkey_test_cleanup, | |
1359 | pkey_test_parse, | |
1360 | pkey_test_run | |
1361 | }; | |
1362 | ||
6c5943c9 | 1363 | static int decrypt_test_init(EVP_TEST *t, const char *name) |
5824cc29 DSH |
1364 | { |
1365 | return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init, | |
1366 | EVP_PKEY_decrypt); | |
1367 | } | |
1368 | ||
6c5943c9 | 1369 | static const EVP_TEST_METHOD pdecrypt_test_method = { |
5824cc29 DSH |
1370 | "Decrypt", |
1371 | decrypt_test_init, | |
1372 | pkey_test_cleanup, | |
1373 | pkey_test_parse, | |
1374 | pkey_test_run | |
1375 | }; | |
1376 | ||
6c5943c9 | 1377 | static int verify_test_init(EVP_TEST *t, const char *name) |
5824cc29 DSH |
1378 | { |
1379 | return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0); | |
1380 | } | |
1381 | ||
6c5943c9 | 1382 | static int verify_test_run(EVP_TEST *t) |
5824cc29 | 1383 | { |
6c5943c9 RS |
1384 | PKEY_DATA *kdata = t->data; |
1385 | ||
5824cc29 DSH |
1386 | if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len, |
1387 | kdata->input, kdata->input_len) <= 0) | |
1388 | t->err = "VERIFY_ERROR"; | |
1389 | return 1; | |
1390 | } | |
1391 | ||
6c5943c9 | 1392 | static const EVP_TEST_METHOD pverify_test_method = { |
5824cc29 DSH |
1393 | "Verify", |
1394 | verify_test_init, | |
1395 | pkey_test_cleanup, | |
1396 | pkey_test_parse, | |
1397 | verify_test_run | |
1398 | }; | |
3b53e18a | 1399 | |
d4ad48d7 | 1400 | |
6c5943c9 | 1401 | static int pderive_test_init(EVP_TEST *t, const char *name) |
d4ad48d7 DSH |
1402 | { |
1403 | return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0); | |
1404 | } | |
1405 | ||
6c5943c9 | 1406 | static int pderive_test_parse(EVP_TEST *t, |
d4ad48d7 DSH |
1407 | const char *keyword, const char *value) |
1408 | { | |
6c5943c9 | 1409 | PKEY_DATA *kdata = t->data; |
d4ad48d7 DSH |
1410 | |
1411 | if (strcmp(keyword, "PeerKey") == 0) { | |
1412 | EVP_PKEY *peer; | |
6c5943c9 | 1413 | if (find_key(&peer, value, public_keys) == 0) |
d4ad48d7 DSH |
1414 | return 0; |
1415 | if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0) | |
1416 | return 0; | |
1417 | return 1; | |
1418 | } | |
1419 | if (strcmp(keyword, "SharedSecret") == 0) | |
c49e0b04 | 1420 | return parse_bin(value, &kdata->output, &kdata->output_len); |
4ddd5ace | 1421 | if (strcmp(keyword, "Ctrl") == 0) |
dfbdf4ab | 1422 | return pkey_test_ctrl(t, kdata->ctx, value); |
d4ad48d7 DSH |
1423 | return 0; |
1424 | } | |
1425 | ||
6c5943c9 | 1426 | static int pderive_test_run(EVP_TEST *t) |
d4ad48d7 | 1427 | { |
e3d378bc AP |
1428 | PKEY_DATA *expected = t->data; |
1429 | unsigned char *got = NULL; | |
1430 | size_t got_len; | |
d4ad48d7 | 1431 | |
9b82c8b1 DSH |
1432 | if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) { |
1433 | t->err = "DERIVE_ERROR"; | |
1434 | goto err; | |
1435 | } | |
e3d378bc | 1436 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { |
6c5943c9 | 1437 | t->err = "DERIVE_ERROR"; |
d4ad48d7 | 1438 | goto err; |
6c5943c9 | 1439 | } |
e3d378bc | 1440 | if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) { |
6c5943c9 | 1441 | t->err = "DERIVE_ERROR"; |
d4ad48d7 | 1442 | goto err; |
6c5943c9 | 1443 | } |
4cceb185 P |
1444 | if (!memory_err_compare(t, "SHARED_SECRET_MISMATCH", |
1445 | expected->output, expected->output_len, | |
1446 | got, got_len)) | |
d4ad48d7 | 1447 | goto err; |
6c5943c9 RS |
1448 | |
1449 | t->err = NULL; | |
d4ad48d7 | 1450 | err: |
e3d378bc | 1451 | OPENSSL_free(got); |
d4ad48d7 DSH |
1452 | return 1; |
1453 | } | |
1454 | ||
6c5943c9 | 1455 | static const EVP_TEST_METHOD pderive_test_method = { |
d4ad48d7 DSH |
1456 | "Derive", |
1457 | pderive_test_init, | |
1458 | pkey_test_cleanup, | |
1459 | pderive_test_parse, | |
1460 | pderive_test_run | |
1461 | }; | |
1462 | ||
3b53e18a | 1463 | |
c49e0b04 RS |
1464 | /** |
1465 | *** PBE TESTS | |
1466 | **/ | |
1467 | ||
1468 | typedef enum pbe_type_enum { | |
1469 | PBE_TYPE_INVALID = 0, | |
1470 | PBE_TYPE_SCRYPT, PBE_TYPE_PBKDF2, PBE_TYPE_PKCS12 | |
1471 | } PBE_TYPE; | |
3b53e18a | 1472 | |
6c5943c9 | 1473 | typedef struct pbe_data_st { |
c49e0b04 | 1474 | PBE_TYPE pbe_type; |
6c5943c9 | 1475 | /* scrypt parameters */ |
3b53e18a | 1476 | uint64_t N, r, p, maxmem; |
6c5943c9 | 1477 | /* PKCS#12 parameters */ |
351fe214 DSH |
1478 | int id, iter; |
1479 | const EVP_MD *md; | |
6c5943c9 | 1480 | /* password */ |
3b53e18a DSH |
1481 | unsigned char *pass; |
1482 | size_t pass_len; | |
6c5943c9 | 1483 | /* salt */ |
3b53e18a DSH |
1484 | unsigned char *salt; |
1485 | size_t salt_len; | |
6c5943c9 | 1486 | /* Expected output */ |
3b53e18a DSH |
1487 | unsigned char *key; |
1488 | size_t key_len; | |
6c5943c9 | 1489 | } PBE_DATA; |
3b53e18a | 1490 | |
b0809bc8 | 1491 | #ifndef OPENSSL_NO_SCRYPT |
c49e0b04 RS |
1492 | /* |
1493 | * Parse unsigned decimal 64 bit integer value | |
1494 | */ | |
1495 | static int parse_uint64(const char *value, uint64_t *pr) | |
1496 | { | |
1497 | const char *p = value; | |
1498 | ||
1499 | if (!TEST_true(*p)) { | |
1500 | TEST_info("Invalid empty integer value"); | |
1501 | return -1; | |
1502 | } | |
1503 | for (*pr = 0; *p; ) { | |
1504 | if (*pr > UINT64_MAX / 10) { | |
1505 | TEST_error("Integer overflow in string %s", value); | |
1506 | return -1; | |
1507 | } | |
1508 | *pr *= 10; | |
00dfbaad | 1509 | if (!TEST_true(isdigit((unsigned char)*p))) { |
c49e0b04 RS |
1510 | TEST_error("Invalid character in string %s", value); |
1511 | return -1; | |
1512 | } | |
1513 | *pr += *p - '0'; | |
1514 | p++; | |
1515 | } | |
1516 | return 1; | |
1517 | } | |
1518 | ||
6c5943c9 | 1519 | static int scrypt_test_parse(EVP_TEST *t, |
3b53e18a DSH |
1520 | const char *keyword, const char *value) |
1521 | { | |
6c5943c9 | 1522 | PBE_DATA *pdata = t->data; |
351fe214 | 1523 | |
3b53e18a | 1524 | if (strcmp(keyword, "N") == 0) |
c49e0b04 | 1525 | return parse_uint64(value, &pdata->N); |
3b53e18a | 1526 | if (strcmp(keyword, "p") == 0) |
c49e0b04 | 1527 | return parse_uint64(value, &pdata->p); |
3b53e18a | 1528 | if (strcmp(keyword, "r") == 0) |
c49e0b04 | 1529 | return parse_uint64(value, &pdata->r); |
3b53e18a | 1530 | if (strcmp(keyword, "maxmem") == 0) |
c49e0b04 | 1531 | return parse_uint64(value, &pdata->maxmem); |
3b53e18a DSH |
1532 | return 0; |
1533 | } | |
b0809bc8 | 1534 | #endif |
3b53e18a | 1535 | |
6c5943c9 | 1536 | static int pbkdf2_test_parse(EVP_TEST *t, |
351fe214 | 1537 | const char *keyword, const char *value) |
3b53e18a | 1538 | { |
6c5943c9 | 1539 | PBE_DATA *pdata = t->data; |
351fe214 DSH |
1540 | |
1541 | if (strcmp(keyword, "iter") == 0) { | |
1542 | pdata->iter = atoi(value); | |
1543 | if (pdata->iter <= 0) | |
c49e0b04 | 1544 | return -1; |
351fe214 DSH |
1545 | return 1; |
1546 | } | |
1547 | if (strcmp(keyword, "MD") == 0) { | |
1548 | pdata->md = EVP_get_digestbyname(value); | |
1549 | if (pdata->md == NULL) | |
c49e0b04 | 1550 | return -1; |
351fe214 DSH |
1551 | return 1; |
1552 | } | |
1553 | return 0; | |
1554 | } | |
1555 | ||
6c5943c9 | 1556 | static int pkcs12_test_parse(EVP_TEST *t, |
351fe214 DSH |
1557 | const char *keyword, const char *value) |
1558 | { | |
6c5943c9 | 1559 | PBE_DATA *pdata = t->data; |
351fe214 DSH |
1560 | |
1561 | if (strcmp(keyword, "id") == 0) { | |
1562 | pdata->id = atoi(value); | |
1563 | if (pdata->id <= 0) | |
c49e0b04 | 1564 | return -1; |
351fe214 DSH |
1565 | return 1; |
1566 | } | |
1567 | return pbkdf2_test_parse(t, keyword, value); | |
3b53e18a DSH |
1568 | } |
1569 | ||
6c5943c9 | 1570 | static int pbe_test_init(EVP_TEST *t, const char *alg) |
3b53e18a | 1571 | { |
6c5943c9 | 1572 | PBE_DATA *pdat; |
c49e0b04 | 1573 | PBE_TYPE pbe_type = PBE_TYPE_INVALID; |
351fe214 | 1574 | |
83bd31da | 1575 | if (strcmp(alg, "scrypt") == 0) { |
b0809bc8 | 1576 | #ifndef OPENSSL_NO_SCRYPT |
3b53e18a | 1577 | pbe_type = PBE_TYPE_SCRYPT; |
83bd31da MC |
1578 | #else |
1579 | t->skip = 1; | |
1580 | return 1; | |
b0809bc8 | 1581 | #endif |
83bd31da | 1582 | } else if (strcmp(alg, "pbkdf2") == 0) { |
351fe214 | 1583 | pbe_type = PBE_TYPE_PBKDF2; |
83bd31da | 1584 | } else if (strcmp(alg, "pkcs12") == 0) { |
351fe214 | 1585 | pbe_type = PBE_TYPE_PKCS12; |
83bd31da | 1586 | } else { |
6c5943c9 | 1587 | TEST_error("Unknown pbe algorithm %s", alg); |
83bd31da | 1588 | } |
c49e0b04 | 1589 | pdat = OPENSSL_zalloc(sizeof(*pdat)); |
3b53e18a | 1590 | pdat->pbe_type = pbe_type; |
3b53e18a DSH |
1591 | t->data = pdat; |
1592 | return 1; | |
1593 | } | |
1594 | ||
6c5943c9 | 1595 | static void pbe_test_cleanup(EVP_TEST *t) |
3b53e18a | 1596 | { |
6c5943c9 RS |
1597 | PBE_DATA *pdat = t->data; |
1598 | ||
1599 | OPENSSL_free(pdat->pass); | |
1600 | OPENSSL_free(pdat->salt); | |
1601 | OPENSSL_free(pdat->key); | |
3b53e18a DSH |
1602 | } |
1603 | ||
6c5943c9 RS |
1604 | static int pbe_test_parse(EVP_TEST *t, |
1605 | const char *keyword, const char *value) | |
3b53e18a | 1606 | { |
6c5943c9 | 1607 | PBE_DATA *pdata = t->data; |
351fe214 | 1608 | |
3b53e18a | 1609 | if (strcmp(keyword, "Password") == 0) |
c49e0b04 | 1610 | return parse_bin(value, &pdata->pass, &pdata->pass_len); |
3b53e18a | 1611 | if (strcmp(keyword, "Salt") == 0) |
c49e0b04 | 1612 | return parse_bin(value, &pdata->salt, &pdata->salt_len); |
3b53e18a | 1613 | if (strcmp(keyword, "Key") == 0) |
c49e0b04 | 1614 | return parse_bin(value, &pdata->key, &pdata->key_len); |
b0809bc8 | 1615 | if (pdata->pbe_type == PBE_TYPE_PBKDF2) |
351fe214 DSH |
1616 | return pbkdf2_test_parse(t, keyword, value); |
1617 | else if (pdata->pbe_type == PBE_TYPE_PKCS12) | |
1618 | return pkcs12_test_parse(t, keyword, value); | |
b0809bc8 RS |
1619 | #ifndef OPENSSL_NO_SCRYPT |
1620 | else if (pdata->pbe_type == PBE_TYPE_SCRYPT) | |
1621 | return scrypt_test_parse(t, keyword, value); | |
1622 | #endif | |
3b53e18a DSH |
1623 | return 0; |
1624 | } | |
1625 | ||
6c5943c9 | 1626 | static int pbe_test_run(EVP_TEST *t) |
3b53e18a | 1627 | { |
e3d378bc | 1628 | PBE_DATA *expected = t->data; |
351fe214 DSH |
1629 | unsigned char *key; |
1630 | ||
e3d378bc | 1631 | if (!TEST_ptr(key = OPENSSL_malloc(expected->key_len))) { |
6c5943c9 | 1632 | t->err = "INTERNAL_ERROR"; |
351fe214 | 1633 | goto err; |
6c5943c9 | 1634 | } |
e3d378bc AP |
1635 | if (expected->pbe_type == PBE_TYPE_PBKDF2) { |
1636 | if (PKCS5_PBKDF2_HMAC((char *)expected->pass, expected->pass_len, | |
1637 | expected->salt, expected->salt_len, | |
1638 | expected->iter, expected->md, | |
1639 | expected->key_len, key) == 0) { | |
6c5943c9 | 1640 | t->err = "PBKDF2_ERROR"; |
351fe214 | 1641 | goto err; |
6c5943c9 | 1642 | } |
b0809bc8 | 1643 | #ifndef OPENSSL_NO_SCRYPT |
e3d378bc AP |
1644 | } else if (expected->pbe_type == PBE_TYPE_SCRYPT) { |
1645 | if (EVP_PBE_scrypt((const char *)expected->pass, expected->pass_len, | |
1646 | expected->salt, expected->salt_len, expected->N, | |
1647 | expected->r, expected->p, expected->maxmem, | |
1648 | key, expected->key_len) == 0) { | |
6c5943c9 | 1649 | t->err = "SCRYPT_ERROR"; |
351fe214 | 1650 | goto err; |
6c5943c9 | 1651 | } |
b0809bc8 | 1652 | #endif |
e3d378bc AP |
1653 | } else if (expected->pbe_type == PBE_TYPE_PKCS12) { |
1654 | if (PKCS12_key_gen_uni(expected->pass, expected->pass_len, | |
1655 | expected->salt, expected->salt_len, | |
1656 | expected->id, expected->iter, expected->key_len, | |
1657 | key, expected->md) == 0) { | |
6c5943c9 | 1658 | t->err = "PKCS12_ERROR"; |
351fe214 | 1659 | goto err; |
6c5943c9 | 1660 | } |
351fe214 | 1661 | } |
4cceb185 P |
1662 | if (!memory_err_compare(t, "KEY_MISMATCH", expected->key, expected->key_len, |
1663 | key, expected->key_len)) | |
351fe214 | 1664 | goto err; |
4cceb185 | 1665 | |
6c5943c9 RS |
1666 | t->err = NULL; |
1667 | err: | |
351fe214 | 1668 | OPENSSL_free(key); |
351fe214 | 1669 | return 1; |
3b53e18a DSH |
1670 | } |
1671 | ||
6c5943c9 | 1672 | static const EVP_TEST_METHOD pbe_test_method = { |
3b53e18a DSH |
1673 | "PBE", |
1674 | pbe_test_init, | |
1675 | pbe_test_cleanup, | |
1676 | pbe_test_parse, | |
1677 | pbe_test_run | |
1678 | }; | |
3cdd1e94 | 1679 | |
c49e0b04 RS |
1680 | |
1681 | /** | |
1682 | *** BASE64 TESTS | |
1683 | **/ | |
3cdd1e94 EK |
1684 | |
1685 | typedef enum { | |
1686 | BASE64_CANONICAL_ENCODING = 0, | |
1687 | BASE64_VALID_ENCODING = 1, | |
1688 | BASE64_INVALID_ENCODING = 2 | |
1689 | } base64_encoding_type; | |
1690 | ||
6c5943c9 | 1691 | typedef struct encode_data_st { |
3cdd1e94 EK |
1692 | /* Input to encoding */ |
1693 | unsigned char *input; | |
1694 | size_t input_len; | |
1695 | /* Expected output */ | |
1696 | unsigned char *output; | |
1697 | size_t output_len; | |
1698 | base64_encoding_type encoding; | |
6c5943c9 | 1699 | } ENCODE_DATA; |
3cdd1e94 | 1700 | |
6c5943c9 | 1701 | static int encode_test_init(EVP_TEST *t, const char *encoding) |
3cdd1e94 | 1702 | { |
c49e0b04 | 1703 | ENCODE_DATA *edata; |
3cdd1e94 | 1704 | |
c49e0b04 RS |
1705 | if (!TEST_ptr(edata = OPENSSL_zalloc(sizeof(*edata)))) |
1706 | return 0; | |
3cdd1e94 EK |
1707 | if (strcmp(encoding, "canonical") == 0) { |
1708 | edata->encoding = BASE64_CANONICAL_ENCODING; | |
1709 | } else if (strcmp(encoding, "valid") == 0) { | |
1710 | edata->encoding = BASE64_VALID_ENCODING; | |
1711 | } else if (strcmp(encoding, "invalid") == 0) { | |
1712 | edata->encoding = BASE64_INVALID_ENCODING; | |
c49e0b04 | 1713 | if (!TEST_ptr(t->expected_err = OPENSSL_strdup("DECODE_ERROR"))) |
3cdd1e94 EK |
1714 | return 0; |
1715 | } else { | |
c49e0b04 RS |
1716 | TEST_error("Bad encoding: %s." |
1717 | " Should be one of {canonical, valid, invalid}", | |
1718 | encoding); | |
3cdd1e94 EK |
1719 | return 0; |
1720 | } | |
1721 | t->data = edata; | |
1722 | return 1; | |
1723 | } | |
1724 | ||
6c5943c9 | 1725 | static void encode_test_cleanup(EVP_TEST *t) |
3cdd1e94 | 1726 | { |
6c5943c9 RS |
1727 | ENCODE_DATA *edata = t->data; |
1728 | ||
1729 | OPENSSL_free(edata->input); | |
1730 | OPENSSL_free(edata->output); | |
3cdd1e94 EK |
1731 | memset(edata, 0, sizeof(*edata)); |
1732 | } | |
1733 | ||
6c5943c9 | 1734 | static int encode_test_parse(EVP_TEST *t, |
3cdd1e94 EK |
1735 | const char *keyword, const char *value) |
1736 | { | |
6c5943c9 | 1737 | ENCODE_DATA *edata = t->data; |
c49e0b04 | 1738 | |
3cdd1e94 | 1739 | if (strcmp(keyword, "Input") == 0) |
c49e0b04 | 1740 | return parse_bin(value, &edata->input, &edata->input_len); |
3cdd1e94 | 1741 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 1742 | return parse_bin(value, &edata->output, &edata->output_len); |
3cdd1e94 EK |
1743 | return 0; |
1744 | } | |
1745 | ||
6c5943c9 | 1746 | static int encode_test_run(EVP_TEST *t) |
3cdd1e94 | 1747 | { |
e3d378bc | 1748 | ENCODE_DATA *expected = t->data; |
3cdd1e94 EK |
1749 | unsigned char *encode_out = NULL, *decode_out = NULL; |
1750 | int output_len, chunk_len; | |
6c5943c9 | 1751 | EVP_ENCODE_CTX *decode_ctx; |
254b26af | 1752 | |
6c5943c9 RS |
1753 | if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) { |
1754 | t->err = "INTERNAL_ERROR"; | |
254b26af | 1755 | goto err; |
6c5943c9 | 1756 | } |
3cdd1e94 | 1757 | |
e3d378bc | 1758 | if (expected->encoding == BASE64_CANONICAL_ENCODING) { |
6c5943c9 RS |
1759 | EVP_ENCODE_CTX *encode_ctx; |
1760 | ||
1761 | if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new()) | |
1762 | || !TEST_ptr(encode_out = | |
e3d378bc | 1763 | OPENSSL_malloc(EVP_ENCODE_LENGTH(expected->input_len)))) |
3cdd1e94 EK |
1764 | goto err; |
1765 | ||
254b26af RL |
1766 | EVP_EncodeInit(encode_ctx); |
1767 | EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len, | |
e3d378bc | 1768 | expected->input, expected->input_len); |
3cdd1e94 EK |
1769 | output_len = chunk_len; |
1770 | ||
254b26af | 1771 | EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len); |
3cdd1e94 EK |
1772 | output_len += chunk_len; |
1773 | ||
254b26af RL |
1774 | EVP_ENCODE_CTX_free(encode_ctx); |
1775 | ||
4cceb185 P |
1776 | if (!memory_err_compare(t, "BAD_ENCODING", |
1777 | expected->output, expected->output_len, | |
1778 | encode_out, output_len)) | |
3cdd1e94 | 1779 | goto err; |
3cdd1e94 EK |
1780 | } |
1781 | ||
6c5943c9 | 1782 | if (!TEST_ptr(decode_out = |
e3d378bc | 1783 | OPENSSL_malloc(EVP_DECODE_LENGTH(expected->output_len)))) |
3cdd1e94 EK |
1784 | goto err; |
1785 | ||
254b26af | 1786 | EVP_DecodeInit(decode_ctx); |
e3d378bc AP |
1787 | if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, expected->output, |
1788 | expected->output_len) < 0) { | |
6c5943c9 | 1789 | t->err = "DECODE_ERROR"; |
3cdd1e94 EK |
1790 | goto err; |
1791 | } | |
1792 | output_len = chunk_len; | |
1793 | ||
254b26af | 1794 | if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) { |
6c5943c9 | 1795 | t->err = "DECODE_ERROR"; |
3cdd1e94 EK |
1796 | goto err; |
1797 | } | |
1798 | output_len += chunk_len; | |
1799 | ||
e3d378bc | 1800 | if (expected->encoding != BASE64_INVALID_ENCODING |
4cceb185 P |
1801 | && !memory_err_compare(t, "BAD_DECODING", |
1802 | expected->input, expected->input_len, | |
1803 | decode_out, output_len)) { | |
6c5943c9 | 1804 | t->err = "BAD_DECODING"; |
3cdd1e94 EK |
1805 | goto err; |
1806 | } | |
1807 | ||
6c5943c9 | 1808 | t->err = NULL; |
3cdd1e94 | 1809 | err: |
3cdd1e94 EK |
1810 | OPENSSL_free(encode_out); |
1811 | OPENSSL_free(decode_out); | |
254b26af | 1812 | EVP_ENCODE_CTX_free(decode_ctx); |
3cdd1e94 EK |
1813 | return 1; |
1814 | } | |
1815 | ||
6c5943c9 | 1816 | static const EVP_TEST_METHOD encode_test_method = { |
3cdd1e94 EK |
1817 | "Encoding", |
1818 | encode_test_init, | |
1819 | encode_test_cleanup, | |
1820 | encode_test_parse, | |
1821 | encode_test_run, | |
1822 | }; | |
44a284d2 | 1823 | |
c49e0b04 RS |
1824 | /** |
1825 | *** KDF TESTS | |
1826 | **/ | |
44a284d2 | 1827 | |
6c5943c9 | 1828 | typedef struct kdf_data_st { |
44a284d2 DSH |
1829 | /* Context for this operation */ |
1830 | EVP_PKEY_CTX *ctx; | |
1831 | /* Expected output */ | |
1832 | unsigned char *output; | |
1833 | size_t output_len; | |
6c5943c9 | 1834 | } KDF_DATA; |
44a284d2 DSH |
1835 | |
1836 | /* | |
1837 | * Perform public key operation setup: lookup key, allocated ctx and call | |
1838 | * the appropriate initialisation function | |
1839 | */ | |
6c5943c9 | 1840 | static int kdf_test_init(EVP_TEST *t, const char *name) |
44a284d2 | 1841 | { |
6c5943c9 | 1842 | KDF_DATA *kdata; |
b15d5ab6 DSH |
1843 | int kdf_nid = OBJ_sn2nid(name); |
1844 | ||
ab78f89b MC |
1845 | #ifdef OPENSSL_NO_SCRYPT |
1846 | if (strcmp(name, "scrypt") == 0) { | |
1847 | t->skip = 1; | |
1848 | return 1; | |
1849 | } | |
1850 | #endif | |
1851 | ||
b15d5ab6 DSH |
1852 | if (kdf_nid == NID_undef) |
1853 | kdf_nid = OBJ_ln2nid(name); | |
44a284d2 | 1854 | |
c49e0b04 | 1855 | if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) |
44a284d2 | 1856 | return 0; |
b15d5ab6 | 1857 | kdata->ctx = EVP_PKEY_CTX_new_id(kdf_nid, NULL); |
9e206ce5 P |
1858 | if (kdata->ctx == NULL) { |
1859 | OPENSSL_free(kdata); | |
44a284d2 | 1860 | return 0; |
9e206ce5 P |
1861 | } |
1862 | if (EVP_PKEY_derive_init(kdata->ctx) <= 0) { | |
1863 | EVP_PKEY_CTX_free(kdata->ctx); | |
1864 | OPENSSL_free(kdata); | |
44a284d2 | 1865 | return 0; |
9e206ce5 | 1866 | } |
c49e0b04 | 1867 | t->data = kdata; |
44a284d2 DSH |
1868 | return 1; |
1869 | } | |
1870 | ||
6c5943c9 | 1871 | static void kdf_test_cleanup(EVP_TEST *t) |
44a284d2 | 1872 | { |
6c5943c9 | 1873 | KDF_DATA *kdata = t->data; |
44a284d2 DSH |
1874 | OPENSSL_free(kdata->output); |
1875 | EVP_PKEY_CTX_free(kdata->ctx); | |
1876 | } | |
1877 | ||
6c5943c9 | 1878 | static int kdf_test_parse(EVP_TEST *t, |
44a284d2 DSH |
1879 | const char *keyword, const char *value) |
1880 | { | |
6c5943c9 RS |
1881 | KDF_DATA *kdata = t->data; |
1882 | ||
44a284d2 | 1883 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 1884 | return parse_bin(value, &kdata->output, &kdata->output_len); |
7d04be79 | 1885 | if (strncmp(keyword, "Ctrl", 4) == 0) |
dfbdf4ab | 1886 | return pkey_test_ctrl(t, kdata->ctx, value); |
44a284d2 DSH |
1887 | return 0; |
1888 | } | |
1889 | ||
6c5943c9 | 1890 | static int kdf_test_run(EVP_TEST *t) |
44a284d2 | 1891 | { |
e3d378bc AP |
1892 | KDF_DATA *expected = t->data; |
1893 | unsigned char *got = NULL; | |
1894 | size_t got_len = expected->output_len; | |
6c5943c9 | 1895 | |
e3d378bc | 1896 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { |
6c5943c9 | 1897 | t->err = "INTERNAL_ERROR"; |
44a284d2 | 1898 | goto err; |
6c5943c9 | 1899 | } |
e3d378bc | 1900 | if (EVP_PKEY_derive(expected->ctx, got, &got_len) <= 0) { |
6c5943c9 | 1901 | t->err = "KDF_DERIVE_ERROR"; |
44a284d2 | 1902 | goto err; |
6c5943c9 | 1903 | } |
4cceb185 P |
1904 | if (!memory_err_compare(t, "KDF_MISMATCH", |
1905 | expected->output, expected->output_len, | |
1906 | got, got_len)) | |
44a284d2 | 1907 | goto err; |
4cceb185 | 1908 | |
6c5943c9 RS |
1909 | t->err = NULL; |
1910 | ||
44a284d2 | 1911 | err: |
e3d378bc | 1912 | OPENSSL_free(got); |
44a284d2 DSH |
1913 | return 1; |
1914 | } | |
1915 | ||
6c5943c9 | 1916 | static const EVP_TEST_METHOD kdf_test_method = { |
44a284d2 DSH |
1917 | "KDF", |
1918 | kdf_test_init, | |
1919 | kdf_test_cleanup, | |
1920 | kdf_test_parse, | |
1921 | kdf_test_run | |
1922 | }; | |
d91b7423 | 1923 | |
c49e0b04 RS |
1924 | |
1925 | /** | |
1926 | *** KEYPAIR TESTS | |
1927 | **/ | |
1928 | ||
1929 | typedef struct keypair_test_data_st { | |
d91b7423 RS |
1930 | EVP_PKEY *privk; |
1931 | EVP_PKEY *pubk; | |
6c5943c9 | 1932 | } KEYPAIR_TEST_DATA; |
d91b7423 | 1933 | |
6c5943c9 | 1934 | static int keypair_test_init(EVP_TEST *t, const char *pair) |
d91b7423 | 1935 | { |
c49e0b04 | 1936 | KEYPAIR_TEST_DATA *data; |
d91b7423 RS |
1937 | int rv = 0; |
1938 | EVP_PKEY *pk = NULL, *pubk = NULL; | |
1939 | char *pub, *priv = NULL; | |
d91b7423 | 1940 | |
c49e0b04 | 1941 | /* Split private and public names. */ |
6c5943c9 RS |
1942 | if (!TEST_ptr(priv = OPENSSL_strdup(pair)) |
1943 | || !TEST_ptr(pub = strchr(priv, ':'))) { | |
1944 | t->err = "PARSING_ERROR"; | |
d91b7423 RS |
1945 | goto end; |
1946 | } | |
c49e0b04 | 1947 | *pub++ = '\0'; |
d91b7423 | 1948 | |
6c5943c9 | 1949 | if (!TEST_true(find_key(&pk, priv, private_keys))) { |
c49e0b04 | 1950 | TEST_info("Can't find private key: %s", priv); |
6c5943c9 | 1951 | t->err = "MISSING_PRIVATE_KEY"; |
d91b7423 RS |
1952 | goto end; |
1953 | } | |
6c5943c9 | 1954 | if (!TEST_true(find_key(&pubk, pub, public_keys))) { |
c49e0b04 | 1955 | TEST_info("Can't find public key: %s", pub); |
6c5943c9 | 1956 | t->err = "MISSING_PUBLIC_KEY"; |
d91b7423 RS |
1957 | goto end; |
1958 | } | |
1959 | ||
1960 | if (pk == NULL && pubk == NULL) { | |
1961 | /* Both keys are listed but unsupported: skip this test */ | |
1962 | t->skip = 1; | |
1963 | rv = 1; | |
1964 | goto end; | |
1965 | } | |
1966 | ||
6c5943c9 | 1967 | if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data)))) |
d91b7423 | 1968 | goto end; |
d91b7423 RS |
1969 | data->privk = pk; |
1970 | data->pubk = pubk; | |
1971 | t->data = data; | |
d91b7423 | 1972 | rv = 1; |
6c5943c9 | 1973 | t->err = NULL; |
d91b7423 RS |
1974 | |
1975 | end: | |
6c5943c9 | 1976 | OPENSSL_free(priv); |
d91b7423 RS |
1977 | return rv; |
1978 | } | |
1979 | ||
6c5943c9 | 1980 | static void keypair_test_cleanup(EVP_TEST *t) |
d91b7423 | 1981 | { |
6c5943c9 | 1982 | OPENSSL_free(t->data); |
d91b7423 | 1983 | t->data = NULL; |
d91b7423 RS |
1984 | } |
1985 | ||
c49e0b04 RS |
1986 | /* |
1987 | * For tests that do not accept any custom keywords. | |
d91b7423 | 1988 | */ |
6c5943c9 | 1989 | static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value) |
d91b7423 RS |
1990 | { |
1991 | return 0; | |
1992 | } | |
1993 | ||
6c5943c9 | 1994 | static int keypair_test_run(EVP_TEST *t) |
d91b7423 RS |
1995 | { |
1996 | int rv = 0; | |
6c5943c9 | 1997 | const KEYPAIR_TEST_DATA *pair = t->data; |
d91b7423 RS |
1998 | |
1999 | if (pair->privk == NULL || pair->pubk == NULL) { | |
6c5943c9 RS |
2000 | /* |
2001 | * this can only happen if only one of the keys is not set | |
d91b7423 RS |
2002 | * which means that one of them was unsupported while the |
2003 | * other isn't: hence a key type mismatch. | |
2004 | */ | |
6c5943c9 | 2005 | t->err = "KEYPAIR_TYPE_MISMATCH"; |
d91b7423 RS |
2006 | rv = 1; |
2007 | goto end; | |
2008 | } | |
2009 | ||
2010 | if ((rv = EVP_PKEY_cmp(pair->privk, pair->pubk)) != 1 ) { | |
2011 | if ( 0 == rv ) { | |
6c5943c9 | 2012 | t->err = "KEYPAIR_MISMATCH"; |
d91b7423 | 2013 | } else if ( -1 == rv ) { |
6c5943c9 | 2014 | t->err = "KEYPAIR_TYPE_MISMATCH"; |
d91b7423 | 2015 | } else if ( -2 == rv ) { |
6c5943c9 | 2016 | t->err = "UNSUPPORTED_KEY_COMPARISON"; |
d91b7423 | 2017 | } else { |
6c5943c9 | 2018 | TEST_error("Unexpected error in key comparison"); |
d91b7423 RS |
2019 | rv = 0; |
2020 | goto end; | |
2021 | } | |
2022 | rv = 1; | |
2023 | goto end; | |
2024 | } | |
2025 | ||
2026 | rv = 1; | |
6c5943c9 | 2027 | t->err = NULL; |
d91b7423 RS |
2028 | |
2029 | end: | |
d91b7423 RS |
2030 | return rv; |
2031 | } | |
2032 | ||
6c5943c9 | 2033 | static const EVP_TEST_METHOD keypair_test_method = { |
d91b7423 RS |
2034 | "PrivPubKeyPair", |
2035 | keypair_test_init, | |
2036 | keypair_test_cleanup, | |
2037 | void_test_parse, | |
2038 | keypair_test_run | |
2039 | }; | |
2040 | ||
1f0fc03b DSH |
2041 | /** |
2042 | *** KEYGEN TEST | |
2043 | **/ | |
2044 | ||
2045 | typedef struct keygen_test_data_st { | |
2046 | EVP_PKEY_CTX *genctx; /* Keygen context to use */ | |
2047 | char *keyname; /* Key name to store key or NULL */ | |
2048 | } KEYGEN_TEST_DATA; | |
2049 | ||
2050 | static int keygen_test_init(EVP_TEST *t, const char *alg) | |
2051 | { | |
2052 | KEYGEN_TEST_DATA *data; | |
2053 | EVP_PKEY_CTX *genctx; | |
2054 | int nid = OBJ_sn2nid(alg); | |
2055 | ||
2056 | if (nid == NID_undef) { | |
2057 | nid = OBJ_ln2nid(alg); | |
2058 | if (nid == NID_undef) | |
2059 | return 0; | |
2060 | } | |
2061 | ||
2062 | if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_id(nid, NULL))) { | |
2063 | /* assume algorithm disabled */ | |
2064 | t->skip = 1; | |
2065 | return 1; | |
2066 | } | |
2067 | ||
2068 | if (EVP_PKEY_keygen_init(genctx) <= 0) { | |
2069 | t->err = "KEYGEN_INIT_ERROR"; | |
2070 | goto err; | |
2071 | } | |
2072 | ||
2073 | if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data)))) | |
2074 | goto err; | |
2075 | data->genctx = genctx; | |
2076 | data->keyname = NULL; | |
2077 | t->data = data; | |
2078 | t->err = NULL; | |
2079 | return 1; | |
2080 | ||
2081 | err: | |
2082 | EVP_PKEY_CTX_free(genctx); | |
2083 | return 0; | |
2084 | } | |
2085 | ||
2086 | static void keygen_test_cleanup(EVP_TEST *t) | |
2087 | { | |
2088 | KEYGEN_TEST_DATA *keygen = t->data; | |
2089 | ||
2090 | EVP_PKEY_CTX_free(keygen->genctx); | |
2091 | OPENSSL_free(keygen->keyname); | |
2092 | OPENSSL_free(t->data); | |
2093 | t->data = NULL; | |
2094 | } | |
2095 | ||
2096 | static int keygen_test_parse(EVP_TEST *t, | |
2097 | const char *keyword, const char *value) | |
2098 | { | |
2099 | KEYGEN_TEST_DATA *keygen = t->data; | |
2100 | ||
2101 | if (strcmp(keyword, "KeyName") == 0) | |
2102 | return TEST_ptr(keygen->keyname = OPENSSL_strdup(value)); | |
2103 | if (strcmp(keyword, "Ctrl") == 0) | |
2104 | return pkey_test_ctrl(t, keygen->genctx, value); | |
2105 | return 0; | |
2106 | } | |
2107 | ||
2108 | static int keygen_test_run(EVP_TEST *t) | |
2109 | { | |
2110 | KEYGEN_TEST_DATA *keygen = t->data; | |
2111 | EVP_PKEY *pkey = NULL; | |
2112 | ||
2113 | t->err = NULL; | |
2114 | if (EVP_PKEY_keygen(keygen->genctx, &pkey) <= 0) { | |
2115 | t->err = "KEYGEN_GENERATE_ERROR"; | |
2116 | goto err; | |
2117 | } | |
2118 | ||
2119 | if (keygen->keyname != NULL) { | |
2120 | KEY_LIST *key; | |
2121 | ||
2122 | if (find_key(NULL, keygen->keyname, private_keys)) { | |
2123 | TEST_info("Duplicate key %s", keygen->keyname); | |
2124 | goto err; | |
2125 | } | |
2126 | ||
2127 | if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key)))) | |
2128 | goto err; | |
2129 | key->name = keygen->keyname; | |
2130 | keygen->keyname = NULL; | |
2131 | key->key = pkey; | |
2132 | key->next = private_keys; | |
2133 | private_keys = key; | |
2134 | } else { | |
2135 | EVP_PKEY_free(pkey); | |
2136 | } | |
2137 | ||
2138 | return 1; | |
2139 | ||
2140 | err: | |
2141 | EVP_PKEY_free(pkey); | |
2142 | return 0; | |
2143 | } | |
2144 | ||
2145 | static const EVP_TEST_METHOD keygen_test_method = { | |
2146 | "KeyGen", | |
2147 | keygen_test_init, | |
2148 | keygen_test_cleanup, | |
2149 | keygen_test_parse, | |
2150 | keygen_test_run, | |
2151 | }; | |
c49e0b04 RS |
2152 | |
2153 | /** | |
2154 | *** DIGEST SIGN+VERIFY TESTS | |
2155 | **/ | |
2156 | ||
75726fe8 | 2157 | typedef struct { |
2117a737 DSH |
2158 | int is_verify; /* Set to 1 if verifying */ |
2159 | int is_oneshot; /* Set to 1 for one shot operation */ | |
2160 | const EVP_MD *md; /* Digest to use */ | |
2161 | EVP_MD_CTX *ctx; /* Digest context */ | |
75726fe8 | 2162 | EVP_PKEY_CTX *pctx; |
2117a737 DSH |
2163 | STACK_OF(EVP_TEST_BUFFER) *input; /* Input data: streaming */ |
2164 | unsigned char *osin; /* Input data if one shot */ | |
2165 | size_t osin_len; /* Input length data if one shot */ | |
2166 | unsigned char *output; /* Expected output */ | |
2167 | size_t output_len; /* Expected output length */ | |
75726fe8 DSH |
2168 | } DIGESTSIGN_DATA; |
2169 | ||
7b22334f DSH |
2170 | static int digestsigver_test_init(EVP_TEST *t, const char *alg, int is_verify, |
2171 | int is_oneshot) | |
75726fe8 DSH |
2172 | { |
2173 | const EVP_MD *md = NULL; | |
2174 | DIGESTSIGN_DATA *mdat; | |
2175 | ||
2176 | if (strcmp(alg, "NULL") != 0) { | |
2177 | if ((md = EVP_get_digestbyname(alg)) == NULL) { | |
2178 | /* If alg has an OID assume disabled algorithm */ | |
2179 | if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) { | |
2180 | t->skip = 1; | |
2181 | return 1; | |
2182 | } | |
2183 | return 0; | |
2184 | } | |
2185 | } | |
2186 | if (!TEST_ptr(mdat = OPENSSL_zalloc(sizeof(*mdat)))) | |
2187 | return 0; | |
2188 | mdat->md = md; | |
2189 | if (!TEST_ptr(mdat->ctx = EVP_MD_CTX_new())) { | |
2190 | OPENSSL_free(mdat); | |
2191 | return 0; | |
2192 | } | |
2193 | mdat->is_verify = is_verify; | |
7b22334f | 2194 | mdat->is_oneshot = is_oneshot; |
75726fe8 DSH |
2195 | t->data = mdat; |
2196 | return 1; | |
2197 | } | |
2198 | ||
2199 | static int digestsign_test_init(EVP_TEST *t, const char *alg) | |
2200 | { | |
7b22334f | 2201 | return digestsigver_test_init(t, alg, 0, 0); |
75726fe8 DSH |
2202 | } |
2203 | ||
2204 | static void digestsigver_test_cleanup(EVP_TEST *t) | |
2205 | { | |
2206 | DIGESTSIGN_DATA *mdata = t->data; | |
2207 | ||
2208 | EVP_MD_CTX_free(mdata->ctx); | |
2209 | sk_EVP_TEST_BUFFER_pop_free(mdata->input, evp_test_buffer_free); | |
7b22334f | 2210 | OPENSSL_free(mdata->osin); |
75726fe8 DSH |
2211 | OPENSSL_free(mdata->output); |
2212 | OPENSSL_free(mdata); | |
2213 | t->data = NULL; | |
2214 | } | |
2215 | ||
2216 | static int digestsigver_test_parse(EVP_TEST *t, | |
2217 | const char *keyword, const char *value) | |
2218 | { | |
2219 | DIGESTSIGN_DATA *mdata = t->data; | |
2220 | ||
2221 | if (strcmp(keyword, "Key") == 0) { | |
2222 | EVP_PKEY *pkey = NULL; | |
2223 | int rv = 0; | |
2224 | ||
2225 | if (mdata->is_verify) | |
2226 | rv = find_key(&pkey, value, public_keys); | |
2227 | if (rv == 0) | |
2228 | rv = find_key(&pkey, value, private_keys); | |
2229 | if (rv == 0 || pkey == NULL) { | |
2230 | t->skip = 1; | |
2231 | return 1; | |
2232 | } | |
2233 | if (mdata->is_verify) { | |
2234 | if (!EVP_DigestVerifyInit(mdata->ctx, &mdata->pctx, mdata->md, | |
2235 | NULL, pkey)) | |
2236 | t->err = "DIGESTVERIFYINIT_ERROR"; | |
2237 | return 1; | |
2238 | } | |
2239 | if (!EVP_DigestSignInit(mdata->ctx, &mdata->pctx, mdata->md, NULL, | |
2240 | pkey)) | |
2241 | t->err = "DIGESTSIGNINIT_ERROR"; | |
2242 | return 1; | |
2243 | } | |
2244 | ||
7b22334f DSH |
2245 | if (strcmp(keyword, "Input") == 0) { |
2246 | if (mdata->is_oneshot) | |
c49e0b04 | 2247 | return parse_bin(value, &mdata->osin, &mdata->osin_len); |
75726fe8 | 2248 | return evp_test_buffer_append(value, &mdata->input); |
7b22334f | 2249 | } |
75726fe8 | 2250 | if (strcmp(keyword, "Output") == 0) |
c49e0b04 | 2251 | return parse_bin(value, &mdata->output, &mdata->output_len); |
7b22334f DSH |
2252 | |
2253 | if (!mdata->is_oneshot) { | |
2254 | if (strcmp(keyword, "Count") == 0) | |
2255 | return evp_test_buffer_set_count(value, mdata->input); | |
2256 | if (strcmp(keyword, "Ncopy") == 0) | |
2257 | return evp_test_buffer_ncopy(value, mdata->input); | |
2258 | } | |
75726fe8 DSH |
2259 | if (strcmp(keyword, "Ctrl") == 0) { |
2260 | if (mdata->pctx == NULL) | |
2261 | return 0; | |
2262 | return pkey_test_ctrl(t, mdata->pctx, value); | |
2263 | } | |
2264 | return 0; | |
2265 | } | |
2266 | ||
2267 | static int digestsign_update_fn(void *ctx, const unsigned char *buf, | |
2268 | size_t buflen) | |
2269 | { | |
2270 | return EVP_DigestSignUpdate(ctx, buf, buflen); | |
2271 | } | |
2272 | ||
2273 | static int digestsign_test_run(EVP_TEST *t) | |
2274 | { | |
e3d378bc AP |
2275 | DIGESTSIGN_DATA *expected = t->data; |
2276 | unsigned char *got = NULL; | |
2277 | size_t got_len; | |
75726fe8 | 2278 | |
e3d378bc AP |
2279 | if (!evp_test_buffer_do(expected->input, digestsign_update_fn, |
2280 | expected->ctx)) { | |
75726fe8 DSH |
2281 | t->err = "DIGESTUPDATE_ERROR"; |
2282 | goto err; | |
2283 | } | |
2284 | ||
e3d378bc | 2285 | if (!EVP_DigestSignFinal(expected->ctx, NULL, &got_len)) { |
75726fe8 DSH |
2286 | t->err = "DIGESTSIGNFINAL_LENGTH_ERROR"; |
2287 | goto err; | |
2288 | } | |
e3d378bc | 2289 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { |
75726fe8 DSH |
2290 | t->err = "MALLOC_FAILURE"; |
2291 | goto err; | |
2292 | } | |
e3d378bc | 2293 | if (!EVP_DigestSignFinal(expected->ctx, got, &got_len)) { |
75726fe8 DSH |
2294 | t->err = "DIGESTSIGNFINAL_ERROR"; |
2295 | goto err; | |
2296 | } | |
4cceb185 P |
2297 | if (!memory_err_compare(t, "SIGNATURE_MISMATCH", |
2298 | expected->output, expected->output_len, | |
2299 | got, got_len)) | |
75726fe8 | 2300 | goto err; |
75726fe8 | 2301 | |
4cceb185 | 2302 | t->err = NULL; |
75726fe8 | 2303 | err: |
e3d378bc | 2304 | OPENSSL_free(got); |
75726fe8 DSH |
2305 | return 1; |
2306 | } | |
2307 | ||
2308 | static const EVP_TEST_METHOD digestsign_test_method = { | |
2309 | "DigestSign", | |
2310 | digestsign_test_init, | |
2311 | digestsigver_test_cleanup, | |
2312 | digestsigver_test_parse, | |
2313 | digestsign_test_run | |
2314 | }; | |
2315 | ||
2316 | static int digestverify_test_init(EVP_TEST *t, const char *alg) | |
2317 | { | |
7b22334f | 2318 | return digestsigver_test_init(t, alg, 1, 0); |
75726fe8 DSH |
2319 | } |
2320 | ||
2321 | static int digestverify_update_fn(void *ctx, const unsigned char *buf, | |
2322 | size_t buflen) | |
2323 | { | |
2324 | return EVP_DigestVerifyUpdate(ctx, buf, buflen); | |
2325 | } | |
2326 | ||
2327 | static int digestverify_test_run(EVP_TEST *t) | |
2328 | { | |
2329 | DIGESTSIGN_DATA *mdata = t->data; | |
2330 | ||
2331 | if (!evp_test_buffer_do(mdata->input, digestverify_update_fn, mdata->ctx)) { | |
2332 | t->err = "DIGESTUPDATE_ERROR"; | |
2333 | return 1; | |
2334 | } | |
2335 | ||
2336 | if (EVP_DigestVerifyFinal(mdata->ctx, mdata->output, | |
2337 | mdata->output_len) <= 0) | |
2338 | t->err = "VERIFY_ERROR"; | |
2339 | return 1; | |
2340 | } | |
2341 | ||
2342 | static const EVP_TEST_METHOD digestverify_test_method = { | |
2343 | "DigestVerify", | |
2344 | digestverify_test_init, | |
2345 | digestsigver_test_cleanup, | |
2346 | digestsigver_test_parse, | |
2347 | digestverify_test_run | |
2348 | }; | |
2349 | ||
7b22334f DSH |
2350 | static int oneshot_digestsign_test_init(EVP_TEST *t, const char *alg) |
2351 | { | |
2352 | return digestsigver_test_init(t, alg, 0, 1); | |
2353 | } | |
2354 | ||
2355 | static int oneshot_digestsign_test_run(EVP_TEST *t) | |
2356 | { | |
e3d378bc AP |
2357 | DIGESTSIGN_DATA *expected = t->data; |
2358 | unsigned char *got = NULL; | |
2359 | size_t got_len; | |
7b22334f | 2360 | |
e3d378bc AP |
2361 | if (!EVP_DigestSign(expected->ctx, NULL, &got_len, |
2362 | expected->osin, expected->osin_len)) { | |
7b22334f DSH |
2363 | t->err = "DIGESTSIGN_LENGTH_ERROR"; |
2364 | goto err; | |
2365 | } | |
e3d378bc | 2366 | if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { |
7b22334f DSH |
2367 | t->err = "MALLOC_FAILURE"; |
2368 | goto err; | |
2369 | } | |
e3d378bc AP |
2370 | if (!EVP_DigestSign(expected->ctx, got, &got_len, |
2371 | expected->osin, expected->osin_len)) { | |
7b22334f DSH |
2372 | t->err = "DIGESTSIGN_ERROR"; |
2373 | goto err; | |
2374 | } | |
4cceb185 P |
2375 | if (!memory_err_compare(t, "SIGNATURE_MISMATCH", |
2376 | expected->output, expected->output_len, | |
2377 | got, got_len)) | |
7b22334f | 2378 | goto err; |
7b22334f | 2379 | |
4cceb185 | 2380 | t->err = NULL; |
7b22334f | 2381 | err: |
e3d378bc | 2382 | OPENSSL_free(got); |
7b22334f DSH |
2383 | return 1; |
2384 | } | |
2385 | ||
2386 | static const EVP_TEST_METHOD oneshot_digestsign_test_method = { | |
2387 | "OneShotDigestSign", | |
2388 | oneshot_digestsign_test_init, | |
2389 | digestsigver_test_cleanup, | |
2390 | digestsigver_test_parse, | |
2391 | oneshot_digestsign_test_run | |
2392 | }; | |
2393 | ||
2394 | static int oneshot_digestverify_test_init(EVP_TEST *t, const char *alg) | |
2395 | { | |
2396 | return digestsigver_test_init(t, alg, 1, 1); | |
2397 | } | |
2398 | ||
2399 | static int oneshot_digestverify_test_run(EVP_TEST *t) | |
2400 | { | |
2401 | DIGESTSIGN_DATA *mdata = t->data; | |
2402 | ||
2403 | if (EVP_DigestVerify(mdata->ctx, mdata->output, mdata->output_len, | |
2404 | mdata->osin, mdata->osin_len) <= 0) | |
2405 | t->err = "VERIFY_ERROR"; | |
2406 | return 1; | |
2407 | } | |
2408 | ||
2409 | static const EVP_TEST_METHOD oneshot_digestverify_test_method = { | |
2410 | "OneShotDigestVerify", | |
2411 | oneshot_digestverify_test_init, | |
2412 | digestsigver_test_cleanup, | |
2413 | digestsigver_test_parse, | |
2414 | oneshot_digestverify_test_run | |
2415 | }; | |
2416 | ||
c49e0b04 RS |
2417 | |
2418 | /** | |
2419 | *** PARSING AND DISPATCH | |
2420 | **/ | |
2421 | ||
2422 | static const EVP_TEST_METHOD *evp_test_list[] = { | |
2423 | &cipher_test_method, | |
2424 | &digest_test_method, | |
2425 | &digestsign_test_method, | |
2426 | &digestverify_test_method, | |
2427 | &encode_test_method, | |
2428 | &kdf_test_method, | |
2429 | &keypair_test_method, | |
1f0fc03b | 2430 | &keygen_test_method, |
c49e0b04 RS |
2431 | &mac_test_method, |
2432 | &oneshot_digestsign_test_method, | |
2433 | &oneshot_digestverify_test_method, | |
2434 | &pbe_test_method, | |
2435 | &pdecrypt_test_method, | |
2436 | &pderive_test_method, | |
2437 | &psign_test_method, | |
2438 | &pverify_recover_test_method, | |
2439 | &pverify_test_method, | |
2440 | NULL | |
2441 | }; | |
2442 | ||
2443 | static const EVP_TEST_METHOD *find_test(const char *name) | |
2444 | { | |
2445 | const EVP_TEST_METHOD **tt; | |
2446 | ||
2447 | for (tt = evp_test_list; *tt; tt++) { | |
2448 | if (strcmp(name, (*tt)->name) == 0) | |
2449 | return *tt; | |
2450 | } | |
2451 | return NULL; | |
2452 | } | |
2453 | ||
2454 | static void clear_test(EVP_TEST *t) | |
2455 | { | |
ae269dd8 | 2456 | test_clearstanza(&t->s); |
c49e0b04 RS |
2457 | ERR_clear_error(); |
2458 | if (t->data != NULL) { | |
2459 | if (t->meth != NULL) | |
2460 | t->meth->cleanup(t); | |
2461 | OPENSSL_free(t->data); | |
2462 | t->data = NULL; | |
2463 | } | |
2464 | OPENSSL_free(t->expected_err); | |
2465 | t->expected_err = NULL; | |
2466 | OPENSSL_free(t->func); | |
2467 | t->func = NULL; | |
2468 | OPENSSL_free(t->reason); | |
2469 | t->reason = NULL; | |
ae269dd8 | 2470 | |
c49e0b04 RS |
2471 | /* Text literal. */ |
2472 | t->err = NULL; | |
2473 | t->skip = 0; | |
2474 | t->meth = NULL; | |
2475 | } | |
2476 | ||
2477 | /* | |
2478 | * Check for errors in the test structure; return 1 if okay, else 0. | |
2479 | */ | |
2480 | static int check_test_error(EVP_TEST *t) | |
2481 | { | |
2482 | unsigned long err; | |
2483 | const char *func; | |
2484 | const char *reason; | |
2485 | ||
2486 | if (t->err == NULL && t->expected_err == NULL) | |
2487 | return 1; | |
2488 | if (t->err != NULL && t->expected_err == NULL) { | |
2489 | if (t->aux_err != NULL) { | |
ae269dd8 RS |
2490 | TEST_info("%s:%d: Source of above error (%s); unexpected error %s", |
2491 | t->s.test_file, t->s.start, t->aux_err, t->err); | |
c49e0b04 | 2492 | } else { |
ae269dd8 RS |
2493 | TEST_info("%s:%d: Source of above error; unexpected error %s", |
2494 | t->s.test_file, t->s.start, t->err); | |
c49e0b04 RS |
2495 | } |
2496 | return 0; | |
2497 | } | |
2498 | if (t->err == NULL && t->expected_err != NULL) { | |
ae269dd8 RS |
2499 | TEST_info("%s:%d: Succeeded but was expecting %s", |
2500 | t->s.test_file, t->s.start, t->expected_err); | |
c49e0b04 RS |
2501 | return 0; |
2502 | } | |
2503 | ||
2504 | if (strcmp(t->err, t->expected_err) != 0) { | |
ae269dd8 RS |
2505 | TEST_info("%s:%d: Expected %s got %s", |
2506 | t->s.test_file, t->s.start, t->expected_err, t->err); | |
c49e0b04 RS |
2507 | return 0; |
2508 | } | |
2509 | ||
2510 | if (t->func == NULL && t->reason == NULL) | |
2511 | return 1; | |
2512 | ||
2513 | if (t->func == NULL || t->reason == NULL) { | |
ae269dd8 RS |
2514 | TEST_info("%s:%d: Test is missing function or reason code", |
2515 | t->s.test_file, t->s.start); | |
c49e0b04 RS |
2516 | return 0; |
2517 | } | |
2518 | ||
2519 | err = ERR_peek_error(); | |
2520 | if (err == 0) { | |
ae269dd8 RS |
2521 | TEST_info("%s:%d: Expected error \"%s:%s\" not set", |
2522 | t->s.test_file, t->s.start, t->func, t->reason); | |
c49e0b04 RS |
2523 | return 0; |
2524 | } | |
2525 | ||
2526 | func = ERR_func_error_string(err); | |
2527 | reason = ERR_reason_error_string(err); | |
2528 | if (func == NULL && reason == NULL) { | |
ae269dd8 RS |
2529 | TEST_info("%s:%d: Expected error \"%s:%s\", no strings available." |
2530 | " Assuming ok.", | |
2531 | t->s.test_file, t->s.start, t->func, t->reason); | |
c49e0b04 RS |
2532 | return 1; |
2533 | } | |
2534 | ||
2535 | if (strcmp(func, t->func) == 0 && strcmp(reason, t->reason) == 0) | |
2536 | return 1; | |
2537 | ||
ae269dd8 RS |
2538 | TEST_info("%s:%d: Expected error \"%s:%s\", got \"%s:%s\"", |
2539 | t->s.test_file, t->s.start, t->func, t->reason, func, reason); | |
c49e0b04 RS |
2540 | |
2541 | return 0; | |
2542 | } | |
2543 | ||
2544 | /* | |
2545 | * Run a parsed test. Log a message and return 0 on error. | |
2546 | */ | |
2547 | static int run_test(EVP_TEST *t) | |
2548 | { | |
2549 | if (t->meth == NULL) | |
2550 | return 1; | |
ae269dd8 | 2551 | t->s.numtests++; |
c49e0b04 | 2552 | if (t->skip) { |
ae269dd8 | 2553 | t->s.numskip++; |
c49e0b04 RS |
2554 | } else { |
2555 | /* run the test */ | |
2556 | if (t->err == NULL && t->meth->run_test(t) != 1) { | |
ae269dd8 RS |
2557 | TEST_info("%s:%d %s error", |
2558 | t->s.test_file, t->s.start, t->meth->name); | |
c49e0b04 RS |
2559 | return 0; |
2560 | } | |
2561 | if (!check_test_error(t)) { | |
8fe3127c | 2562 | TEST_openssl_errors(); |
ae269dd8 | 2563 | t->s.errors++; |
c49e0b04 RS |
2564 | } |
2565 | } | |
2566 | ||
2567 | /* clean it up */ | |
2568 | return 1; | |
2569 | } | |
2570 | ||
2571 | static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst) | |
2572 | { | |
2573 | for (; lst != NULL; lst = lst->next) { | |
2574 | if (strcmp(lst->name, name) == 0) { | |
2575 | if (ppk != NULL) | |
2576 | *ppk = lst->key; | |
2577 | return 1; | |
2578 | } | |
2579 | } | |
2580 | return 0; | |
2581 | } | |
2582 | ||
2583 | static void free_key_list(KEY_LIST *lst) | |
2584 | { | |
2585 | while (lst != NULL) { | |
2586 | KEY_LIST *next = lst->next; | |
2587 | ||
2588 | EVP_PKEY_free(lst->key); | |
2589 | OPENSSL_free(lst->name); | |
2590 | OPENSSL_free(lst); | |
2591 | lst = next; | |
2592 | } | |
2593 | } | |
2594 | ||
c49e0b04 RS |
2595 | /* |
2596 | * Is the key type an unsupported algorithm? | |
2597 | */ | |
3cb7c5cf | 2598 | static int key_unsupported(void) |
c49e0b04 RS |
2599 | { |
2600 | long err = ERR_peek_error(); | |
2601 | ||
2602 | if (ERR_GET_LIB(err) == ERR_LIB_EVP | |
2603 | && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) { | |
2604 | ERR_clear_error(); | |
2605 | return 1; | |
2606 | } | |
2607 | #ifndef OPENSSL_NO_EC | |
2608 | /* | |
2609 | * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an | |
2610 | * hint to an unsupported algorithm/curve (e.g. if binary EC support is | |
2611 | * disabled). | |
2612 | */ | |
2613 | if (ERR_GET_LIB(err) == ERR_LIB_EC | |
2614 | && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) { | |
2615 | ERR_clear_error(); | |
2616 | return 1; | |
2617 | } | |
2618 | #endif /* OPENSSL_NO_EC */ | |
2619 | return 0; | |
2620 | } | |
2621 | ||
2622 | /* | |
ae269dd8 | 2623 | * NULL out the value from |pp| but return it. This "steals" a pointer. |
c49e0b04 | 2624 | */ |
ae269dd8 | 2625 | static char *take_value(PAIR *pp) |
c49e0b04 | 2626 | { |
ae269dd8 RS |
2627 | char *p = pp->value; |
2628 | ||
2629 | pp->value = NULL; | |
2630 | return p; | |
2631 | } | |
2632 | ||
2633 | /* | |
2634 | * Read and parse one test. Return 0 if failure, 1 if okay. | |
2635 | */ | |
2636 | static int parse(EVP_TEST *t) | |
2637 | { | |
2638 | KEY_LIST *key, **klist; | |
c49e0b04 | 2639 | EVP_PKEY *pkey; |
ae269dd8 RS |
2640 | PAIR *pp; |
2641 | int i; | |
c49e0b04 | 2642 | |
c49e0b04 | 2643 | top: |
ae269dd8 RS |
2644 | do { |
2645 | if (BIO_eof(t->s.fp)) | |
c49e0b04 | 2646 | return EOF; |
ae269dd8 RS |
2647 | clear_test(t); |
2648 | if (!test_readstanza(&t->s)) | |
2649 | return 0; | |
2650 | } while (t->s.numpairs == 0); | |
2651 | pp = &t->s.pairs[0]; | |
c49e0b04 | 2652 | |
ae269dd8 | 2653 | /* Are we adding a key? */ |
c49e0b04 RS |
2654 | klist = NULL; |
2655 | pkey = NULL; | |
ae269dd8 RS |
2656 | if (strcmp(pp->key, "PrivateKey") == 0) { |
2657 | pkey = PEM_read_bio_PrivateKey(t->s.key, NULL, 0, NULL); | |
c49e0b04 | 2658 | if (pkey == NULL && !key_unsupported()) { |
1bf2cc23 | 2659 | EVP_PKEY_free(pkey); |
ae269dd8 | 2660 | TEST_info("Can't read private key %s", pp->value); |
8fe3127c | 2661 | TEST_openssl_errors(); |
c49e0b04 RS |
2662 | return 0; |
2663 | } | |
2664 | klist = &private_keys; | |
4665244c | 2665 | } else if (strcmp(pp->key, "PublicKey") == 0) { |
ae269dd8 | 2666 | pkey = PEM_read_bio_PUBKEY(t->s.key, NULL, 0, NULL); |
c49e0b04 | 2667 | if (pkey == NULL && !key_unsupported()) { |
1bf2cc23 | 2668 | EVP_PKEY_free(pkey); |
ae269dd8 | 2669 | TEST_info("Can't read public key %s", pp->value); |
8fe3127c | 2670 | TEST_openssl_errors(); |
c49e0b04 RS |
2671 | return 0; |
2672 | } | |
2673 | klist = &public_keys; | |
4665244c MC |
2674 | } else if (strcmp(pp->key, "PrivateKeyRaw") == 0 |
2675 | || strcmp(pp->key, "PublicKeyRaw") == 0 ) { | |
2676 | char *strnid = NULL, *keydata = NULL; | |
2677 | unsigned char *keybin; | |
2678 | size_t keylen; | |
2679 | int nid; | |
2680 | ||
2681 | if (strcmp(pp->key, "PrivateKeyRaw") == 0) | |
2682 | klist = &private_keys; | |
2683 | else | |
2684 | klist = &public_keys; | |
2685 | ||
2686 | strnid = strchr(pp->value, ':'); | |
2687 | if (strnid != NULL) { | |
2688 | *strnid++ = '\0'; | |
2689 | keydata = strchr(strnid, ':'); | |
2690 | if (keydata != NULL) | |
2691 | *keydata++ = '\0'; | |
2692 | } | |
2693 | if (keydata == NULL) { | |
2694 | TEST_info("Failed to parse %s value", pp->key); | |
2695 | return 0; | |
2696 | } | |
2697 | ||
2698 | nid = OBJ_txt2nid(strnid); | |
2699 | if (nid == NID_undef) { | |
2700 | TEST_info("Uncrecognised algorithm NID"); | |
2701 | return 0; | |
2702 | } | |
2703 | if (!parse_bin(keydata, &keybin, &keylen)) { | |
2704 | TEST_info("Failed to create binary key"); | |
2705 | return 0; | |
2706 | } | |
2707 | if (klist == &private_keys) | |
f929439f | 2708 | pkey = EVP_PKEY_new_raw_private_key(nid, NULL, keybin, keylen); |
4665244c | 2709 | else |
f929439f | 2710 | pkey = EVP_PKEY_new_raw_public_key(nid, NULL, keybin, keylen); |
66a925ea | 2711 | if (pkey == NULL && !key_unsupported()) { |
4665244c MC |
2712 | TEST_info("Can't read %s data", pp->key); |
2713 | OPENSSL_free(keybin); | |
2714 | TEST_openssl_errors(); | |
2715 | return 0; | |
2716 | } | |
2717 | OPENSSL_free(keybin); | |
c49e0b04 RS |
2718 | } |
2719 | ||
2720 | /* If we have a key add to list */ | |
2721 | if (klist != NULL) { | |
ae269dd8 RS |
2722 | if (find_key(NULL, pp->value, *klist)) { |
2723 | TEST_info("Duplicate key %s", pp->value); | |
c49e0b04 RS |
2724 | return 0; |
2725 | } | |
ae269dd8 | 2726 | if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key)))) |
c49e0b04 | 2727 | return 0; |
ae269dd8 | 2728 | key->name = take_value(pp); |
ddb634fe JL |
2729 | |
2730 | /* Hack to detect SM2 keys */ | |
c86acc9f MC |
2731 | if(pkey != NULL && strstr(key->name, "SM2") != NULL) { |
2732 | #ifdef OPENSSL_NO_SM2 | |
2733 | EVP_PKEY_free(pkey); | |
2734 | pkey = NULL; | |
2735 | #else | |
ddb634fe | 2736 | EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2); |
c86acc9f MC |
2737 | #endif |
2738 | } | |
ddb634fe | 2739 | |
c49e0b04 RS |
2740 | key->key = pkey; |
2741 | key->next = *klist; | |
2742 | *klist = key; | |
2743 | ||
2744 | /* Go back and start a new stanza. */ | |
ae269dd8 RS |
2745 | if (t->s.numpairs != 1) |
2746 | TEST_info("Line %d: missing blank line\n", t->s.curr); | |
c49e0b04 RS |
2747 | goto top; |
2748 | } | |
2749 | ||
ae269dd8 RS |
2750 | /* Find the test, based on first keyword. */ |
2751 | if (!TEST_ptr(t->meth = find_test(pp->key))) | |
2752 | return 0; | |
2753 | if (!t->meth->init(t, pp->value)) { | |
2754 | TEST_error("unknown %s: %s\n", pp->key, pp->value); | |
2755 | return 0; | |
c49e0b04 RS |
2756 | } |
2757 | if (t->skip == 1) { | |
ae269dd8 RS |
2758 | /* TEST_info("skipping %s %s", pp->key, pp->value); */ |
2759 | return 0; | |
c49e0b04 RS |
2760 | } |
2761 | ||
ae269dd8 RS |
2762 | for (pp++, i = 1; i < t->s.numpairs; pp++, i++) { |
2763 | if (strcmp(pp->key, "Result") == 0) { | |
c49e0b04 | 2764 | if (t->expected_err != NULL) { |
ae269dd8 RS |
2765 | TEST_info("Line %d: multiple result lines", t->s.curr); |
2766 | return 0; | |
c49e0b04 | 2767 | } |
ae269dd8 RS |
2768 | t->expected_err = take_value(pp); |
2769 | } else if (strcmp(pp->key, "Function") == 0) { | |
c49e0b04 | 2770 | if (t->func != NULL) { |
ae269dd8 RS |
2771 | TEST_info("Line %d: multiple function lines\n", t->s.curr); |
2772 | return 0; | |
c49e0b04 | 2773 | } |
ae269dd8 RS |
2774 | t->func = take_value(pp); |
2775 | } else if (strcmp(pp->key, "Reason") == 0) { | |
c49e0b04 | 2776 | if (t->reason != NULL) { |
ae269dd8 RS |
2777 | TEST_info("Line %d: multiple reason lines", t->s.curr); |
2778 | return 0; | |
c49e0b04 | 2779 | } |
ae269dd8 | 2780 | t->reason = take_value(pp); |
c49e0b04 RS |
2781 | } else { |
2782 | /* Must be test specific line: try to parse it */ | |
ae269dd8 | 2783 | int rv = t->meth->parse(t, pp->key, pp->value); |
c49e0b04 RS |
2784 | |
2785 | if (rv == 0) { | |
ae269dd8 RS |
2786 | TEST_info("Line %d: unknown keyword %s", t->s.curr, pp->key); |
2787 | return 0; | |
c49e0b04 RS |
2788 | } |
2789 | if (rv < 0) { | |
ce5d64c7 RL |
2790 | TEST_info("Line %d: error processing keyword %s = %s\n", |
2791 | t->s.curr, pp->key, pp->value); | |
ae269dd8 | 2792 | return 0; |
c49e0b04 RS |
2793 | } |
2794 | } | |
2795 | } | |
2796 | ||
2797 | return 1; | |
c49e0b04 RS |
2798 | } |
2799 | ||
ae269dd8 | 2800 | static int run_file_tests(int i) |
6c5943c9 | 2801 | { |
ae269dd8 | 2802 | EVP_TEST *t; |
ad887416 | 2803 | const char *testfile = test_get_argument(i); |
c49e0b04 | 2804 | int c; |
6c5943c9 | 2805 | |
ae269dd8 | 2806 | if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t)))) |
6c5943c9 | 2807 | return 0; |
ad887416 | 2808 | if (!test_start_file(&t->s, testfile)) { |
ae269dd8 RS |
2809 | OPENSSL_free(t); |
2810 | return 0; | |
2811 | } | |
c49e0b04 | 2812 | |
ae269dd8 RS |
2813 | while (!BIO_eof(t->s.fp)) { |
2814 | c = parse(t); | |
2815 | if (t->skip) | |
c49e0b04 | 2816 | continue; |
ae269dd8 RS |
2817 | if (c == 0 || !run_test(t)) { |
2818 | t->s.errors++; | |
c49e0b04 RS |
2819 | break; |
2820 | } | |
6c5943c9 | 2821 | } |
ae269dd8 RS |
2822 | test_end_file(&t->s); |
2823 | clear_test(t); | |
6c5943c9 | 2824 | |
6c5943c9 RS |
2825 | free_key_list(public_keys); |
2826 | free_key_list(private_keys); | |
ae269dd8 RS |
2827 | BIO_free(t->s.key); |
2828 | c = t->s.errors; | |
2829 | OPENSSL_free(t); | |
2830 | return c == 0; | |
6c5943c9 RS |
2831 | } |
2832 | ||
ad887416 | 2833 | int setup_tests(void) |
6c5943c9 | 2834 | { |
ad887416 P |
2835 | size_t n = test_get_argument_count(); |
2836 | ||
2837 | if (n == 0) { | |
2838 | TEST_error("Usage: %s file...", test_get_program_name()); | |
6c5943c9 RS |
2839 | return 0; |
2840 | } | |
6c5943c9 | 2841 | |
ad887416 P |
2842 | ADD_ALL_TESTS(run_file_tests, n); |
2843 | return 1; | |
6c5943c9 | 2844 | } |