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