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