evp_test.c 56.5 KB
Newer Older
B
Ben Laurie 已提交
1
/*
2
 * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved.
B
Ben Laurie 已提交
3
 *
R
Rich Salz 已提交
4 5 6 7
 * Licensed under the OpenSSL license (the "License").  You may not use
 * this file except in compliance with the License.  You can obtain a copy
 * in the file LICENSE in the source distribution or at
 * https://www.openssl.org/source/license.html
B
Ben Laurie 已提交
8 9 10 11
 */

#include <stdio.h>
#include <string.h>
12 13
#include <stdlib.h>
#include <ctype.h>
B
Ben Laurie 已提交
14
#include <openssl/evp.h>
15
#include <openssl/pem.h>
16
#include <openssl/err.h>
17
#include <openssl/x509v3.h>
D
Dr. Stephen Henson 已提交
18
#include <openssl/pkcs12.h>
19
#include <openssl/kdf.h>
D
Dr. Stephen Henson 已提交
20
#include "internal/numbers.h"
R
Rich Salz 已提交
21
#include "testutil.h"
B
Ben Laurie 已提交
22

R
Rich Salz 已提交
23 24 25
/*
 * Remove spaces from beginning and end of a string
 */
26
static void remove_space(char **pval)
27
{
P
Pauli 已提交
28
    unsigned char *p = (unsigned char *)*pval, *beginning;
29

30 31 32
    while (isspace(*p))
        p++;

P
Pauli 已提交
33
    *pval = (char *)(beginning = p);
34 35 36 37

    p = p + strlen(*pval) - 1;

    /* Remove trailing space */
P
Pauli 已提交
38
    while (p >= beginning && isspace(*p))
39
        *p-- = 0;
40
}
B
Ben Laurie 已提交
41

42 43 44
/*
 * Given a line of the form:
 *      name = value # comment
R
Rich Salz 已提交
45
 * extract name and value. NB: modifies |linebuf|.
46 47
 */
static int parse_line(char **pkw, char **pval, char *linebuf)
48
{
R
Rich Salz 已提交
49
    char *p = linebuf + strlen(linebuf) - 1;
50

51
    if (*p != '\n') {
R
Rich Salz 已提交
52 53
        TEST_error("FATAL: missing EOL");
        return 0;
B
Ben Laurie 已提交
54 55
    }

56 57
    /* Look for # */
    p = strchr(linebuf, '#');
R
Rich Salz 已提交
58
    if (p != NULL)
59
        *p = '\0';
60

61
    /* Look for = sign */
R
Rich Salz 已提交
62
    if ((p = strchr(linebuf, '=')) == NULL)
63 64
        return 0;
    *p++ = '\0';
65

66 67 68 69 70
    *pkw = linebuf;
    *pval = p;
    remove_space(pkw);
    remove_space(pval);
    return 1;
71
}
B
Ben Laurie 已提交
72

E
Emilia Kasper 已提交
73 74 75 76 77 78 79 80 81 82 83 84
/*
 * Unescape some escape sequences in string literals.
 * Return the result in a newly allocated buffer.
 * Currently only supports '\n'.
 * If the input length is 0, returns a valid 1-byte buffer, but sets
 * the length to 0.
 */
static unsigned char* unescape(const char *input, size_t input_len,
                               size_t *out_len)
{
    unsigned char *ret, *p;
    size_t i;
R
Rich Salz 已提交
85

E
Emilia Kasper 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    if (input_len == 0) {
        *out_len = 0;
        return OPENSSL_zalloc(1);
    }

    /* Escaping is non-expanding; over-allocate original size for simplicity. */
    ret = p = OPENSSL_malloc(input_len);
    if (ret == NULL)
        return NULL;

    for (i = 0; i < input_len; i++) {
        if (input[i] == '\\') {
            if (i == input_len - 1 || input[i+1] != 'n')
                goto err;
            *p++ = '\n';
            i++;
        } else {
            *p++ = input[i];
        }
    }

    *out_len = p - ret;
    return ret;

 err:
    OPENSSL_free(ret);
    return NULL;
}

115 116
/* For a hex string "value" convert to a binary allocated buffer */
static int test_bin(const char *value, unsigned char **buf, size_t *buflen)
117
{
118
    long len;
D
Dr. Stephen Henson 已提交
119 120

    *buflen = 0;
121 122

    /* Check for empty value */
123
    if (!*value) {
E
Emilia Kasper 已提交
124 125 126 127 128 129
        /*
         * Don't return NULL for zero length buffer.
         * This is needed for some tests with empty keys: HMAC_Init_ex() expects
         * a non-NULL key buffer even if the key length is 0, in order to detect
         * key reset.
         */
130 131 132 133 134 135 136
        *buf = OPENSSL_malloc(1);
        if (!*buf)
            return 0;
        **buf = 0;
        *buflen = 0;
        return 1;
    }
137 138 139 140 141 142 143 144

    /* Check for NULL literal */
    if (strcmp(value, "NULL") == 0) {
        *buf = NULL;
        *buflen = 0;
        return 1;
    }

D
Dr. Stephen Henson 已提交
145 146 147 148 149 150 151 152
    /* Check for string literal */
    if (value[0] == '"') {
        size_t vlen;
        value++;
        vlen = strlen(value);
        if (value[vlen - 1] != '"')
            return 0;
        vlen--;
E
Emilia Kasper 已提交
153 154 155
        *buf = unescape(value, vlen, buflen);
        if (*buf == NULL)
            return 0;
D
Dr. Stephen Henson 已提交
156 157
        return 1;
    }
E
Emilia Kasper 已提交
158

159
    /* Otherwise assume as hex literal and convert it to binary buffer */
R
Rich Salz 已提交
160 161 162
    if (!TEST_ptr(*buf = OPENSSL_hexstr2buf(value, &len))) {
        TEST_info("Cannot convert %s", value);
        ERR_print_errors(bio_err);
163 164 165 166 167
        return -1;
    }
    /* Size of input buffer means we'll never overflow */
    *buflen = len;
    return 1;
168
}
M
Matt Caswell 已提交
169 170
#ifndef OPENSSL_NO_SCRYPT
/* Currently only used by scrypt tests */
D
Dr. Stephen Henson 已提交
171 172 173 174
/* Parse unsigned decimal 64 bit integer value */
static int test_uint64(const char *value, uint64_t *pr)
{
    const char *p = value;
R
Rich Salz 已提交
175 176 177

    if (!TEST_true(*p)) {
        TEST_info("Invalid empty integer value");
D
Dr. Stephen Henson 已提交
178 179 180 181
        return -1;
    }
    *pr = 0;
    while (*p) {
R
Rich Salz 已提交
182 183
        if (*pr > UINT64_MAX / 10) {
            TEST_error("Integer overflow in string %s", value);
D
Dr. Stephen Henson 已提交
184 185 186
            return -1;
        }
        *pr *= 10;
R
Rich Salz 已提交
187 188
        if (!TEST_true(isdigit(*p))) {
            TEST_error("Invalid character in string %s", value);
D
Dr. Stephen Henson 已提交
189 190 191 192 193 194 195
            return -1;
        }
        *pr += *p - '0';
        p++;
    }
    return 1;
}
M
Matt Caswell 已提交
196
#endif
197

R
Rich Salz 已提交
198 199
typedef struct evp_test_method_st EVP_TEST_METHOD;

200
/* Structure holding test information */
R
Rich Salz 已提交
201
typedef struct evp_test_st {
202
    /* file being read */
203
    BIO *in;
204 205
    /* temp memory BIO for reading in keys */
    BIO *key;
206
    /* method for this test */
R
Rich Salz 已提交
207
    const EVP_TEST_METHOD *meth;
208 209 210 211 212
    /* current line being processed */
    unsigned int line;
    /* start line of current test */
    unsigned int start_line;
    /* Error string for test */
213
    const char *err, *aux_err;
214 215
    /* Expected error value of test */
    char *expected_err;
216 217 218 219
    /* Expected error function string */
    char *func;
    /* Expected error reason string */
    char *reason;
220 221 222 223
    /* Number of tests */
    int ntests;
    /* Error count */
    int errors;
D
Dr. Stephen Henson 已提交
224 225
    /* Number of tests skipped */
    int nskip;
D
Dr. Stephen Henson 已提交
226
    /* If output mismatch expected and got value */
E
Emilia Kasper 已提交
227 228
    unsigned char *out_received;
    size_t out_received_len;
D
Dr. Stephen Henson 已提交
229
    unsigned char *out_expected;
E
Emilia Kasper 已提交
230
    size_t out_expected_len;
231 232
    /* test specific data */
    void *data;
D
Dr. Stephen Henson 已提交
233 234
    /* Current test should be skipped */
    int skip;
R
Rich Salz 已提交
235
} EVP_TEST;
236

R
Rich Salz 已提交
237 238 239 240
/*
 * Linked list of named keys.
 */
typedef struct key_list_st {
241 242
    char *name;
    EVP_PKEY *key;
R
Rich Salz 已提交
243 244 245 246 247 248
    struct key_list_st *next;
} KEY_LIST;

/* List of public and private keys */
static KEY_LIST *private_keys;
static KEY_LIST *public_keys;
249

R
Rich Salz 已提交
250 251 252 253
/*
 * Test method structure
 */
struct evp_test_method_st {
254 255 256
    /* Name of test as it appears in file */
    const char *name;
    /* Initialise test for "alg" */
R
Rich Salz 已提交
257
    int (*init) (EVP_TEST * t, const char *alg);
258
    /* Clean up method */
R
Rich Salz 已提交
259
    void (*cleanup) (EVP_TEST * t);
260
    /* Test specific name value pair processing */
R
Rich Salz 已提交
261
    int (*parse) (EVP_TEST * t, const char *name, const char *value);
262
    /* Run the test itself */
R
Rich Salz 已提交
263
    int (*run_test) (EVP_TEST * t);
264 265
};

R
Rich Salz 已提交
266 267 268 269 270 271 272 273 274 275 276 277
static const EVP_TEST_METHOD digest_test_method, cipher_test_method;
static const EVP_TEST_METHOD mac_test_method;
static const EVP_TEST_METHOD psign_test_method, pverify_test_method;
static const EVP_TEST_METHOD pdecrypt_test_method;
static const EVP_TEST_METHOD pverify_recover_test_method;
static const EVP_TEST_METHOD pderive_test_method;
static const EVP_TEST_METHOD pbe_test_method;
static const EVP_TEST_METHOD encode_test_method;
static const EVP_TEST_METHOD kdf_test_method;
static const EVP_TEST_METHOD keypair_test_method;

static const EVP_TEST_METHOD *evp_test_list[] = {
278 279
    &digest_test_method,
    &cipher_test_method,
D
Dr. Stephen Henson 已提交
280
    &mac_test_method,
281 282 283 284
    &psign_test_method,
    &pverify_test_method,
    &pdecrypt_test_method,
    &pverify_recover_test_method,
285
    &pderive_test_method,
D
Dr. Stephen Henson 已提交
286
    &pbe_test_method,
E
Emilia Kasper 已提交
287
    &encode_test_method,
288
    &kdf_test_method,
R
Rich Salz 已提交
289
    &keypair_test_method,
D
Dr. Stephen Henson 已提交
290
    NULL
291 292
};

R
Rich Salz 已提交
293
static const EVP_TEST_METHOD *evp_find_test(const char *name)
294
{
R
Rich Salz 已提交
295
    const EVP_TEST_METHOD **tt;
R
Rich Salz 已提交
296

297
    for (tt = evp_test_list; *tt; tt++) {
R
Rich Salz 已提交
298
        if (strcmp(name, (*tt)->name) == 0)
299 300 301
            return *tt;
    }
    return NULL;
302 303
}

D
Dr. Stephen Henson 已提交
304 305 306
static void hex_print(const char *name, const unsigned char *buf, size_t len)
{
    size_t i;
R
Rich Salz 已提交
307

D
Dr. Stephen Henson 已提交
308 309 310 311 312 313
    fprintf(stderr, "%s ", name);
    for (i = 0; i < len; i++)
        fprintf(stderr, "%02X", buf[i]);
    fputs("\n", stderr);
}

R
Rich Salz 已提交
314
static void clear_test(EVP_TEST *t)
D
Dr. Stephen Henson 已提交
315
{
R
Rich Salz 已提交
316 317
    OPENSSL_free(t->expected_err);
    t->expected_err = NULL;
318 319 320 321
    OPENSSL_free(t->func);
    t->func = NULL;
    OPENSSL_free(t->reason);
    t->reason = NULL;
R
Rich Salz 已提交
322 323
    OPENSSL_free(t->out_expected);
    t->out_expected = NULL;
E
Emilia Kasper 已提交
324
    t->out_expected_len = 0;
R
Rich Salz 已提交
325 326
    OPENSSL_free(t->out_received);
    t->out_received = NULL;
E
Emilia Kasper 已提交
327 328 329
    t->out_received_len = 0;
    /* Literals. */
    t->err = NULL;
D
Dr. Stephen Henson 已提交
330 331
}

R
Rich Salz 已提交
332
static void print_expected(EVP_TEST *t)
D
Dr. Stephen Henson 已提交
333
{
E
Emilia Kasper 已提交
334
    if (t->out_expected == NULL && t->out_received == NULL)
D
Dr. Stephen Henson 已提交
335
        return;
E
Emilia Kasper 已提交
336 337
    hex_print("Expected:", t->out_expected, t->out_expected_len);
    hex_print("Got:     ", t->out_received, t->out_received_len);
R
Rich Salz 已提交
338
    clear_test(t);
D
Dr. Stephen Henson 已提交
339 340
}

R
Rich Salz 已提交
341 342 343 344
/*
 * Check for errors in the test structure; return 1 if okay, else 0.
 */
static int check_test_error(EVP_TEST *t)
345
{
346 347 348
    unsigned long err;
    const char *func;
    const char *reason;
R
Rich Salz 已提交
349 350

    if (t->err == NULL && t->expected_err == NULL)
351
        return 1;
R
Rich Salz 已提交
352
    if (t->err != NULL && t->expected_err == NULL) {
353
        if (t->aux_err != NULL) {
R
Rich Salz 已提交
354 355
            TEST_info("Test line %d(%s): unexpected error %s",
                      t->start_line, t->aux_err, t->err);
356
        } else {
R
Rich Salz 已提交
357 358
            TEST_info("Test line %d: unexpected error %s",
                      t->start_line, t->err);
359
        }
D
Dr. Stephen Henson 已提交
360
        print_expected(t);
361
        return 0;
362
    }
R
Rich Salz 已提交
363 364 365
    if (t->err == NULL && t->expected_err != NULL) {
        TEST_info("Test line %d: succeeded expecting %s",
                  t->start_line, t->expected_err);
366 367
        return 0;
    }
368 369

    if (strcmp(t->err, t->expected_err) != 0) {
R
Rich Salz 已提交
370 371
        TEST_info("Test line %d: expecting %s got %s",
                  t->start_line, t->expected_err, t->err);
372 373 374 375 376 377 378
        return 0;
    }

    if (t->func == NULL && t->reason == NULL)
        return 1;

    if (t->func == NULL || t->reason == NULL) {
R
Rich Salz 已提交
379 380
        TEST_info("Test line %d: missing function or reason code",
                  t->start_line);
381 382 383 384 385
        return 0;
    }

    err = ERR_peek_error();
    if (err == 0) {
R
Rich Salz 已提交
386 387
        TEST_info("Test line %d, expected error \"%s:%s\" not set",
                  t->start_line, t->func, t->reason);
388 389 390 391 392
        return 0;
    }

    func = ERR_func_error_string(err);
    reason = ERR_reason_error_string(err);
393
    if (func == NULL && reason == NULL) {
R
Rich Salz 已提交
394 395 396
        TEST_info("Test line %d: expected error \"%s:%s\","
                  " no strings available.  Skipping...\n",
                  t->start_line, t->func, t->reason);
397 398 399
        return 1;
    }

400
    if (strcmp(func, t->func) == 0 && strcmp(reason, t->reason) == 0)
401
        return 1;
D
 
Dr. Stephen Henson 已提交
402

R
Rich Salz 已提交
403 404
    TEST_info("Test line %d: expected error \"%s:%s\", got \"%s:%s\"",
              t->start_line, t->func, t->reason, func, reason);
405

406 407
    return 0;
}
408

R
Rich Salz 已提交
409 410 411 412 413
/*
 * Setup a new test, run any existing test. Log a message and return 0
 * on error.
 */
static int run_and_get_next(EVP_TEST *t, const EVP_TEST_METHOD *tmeth)
414 415 416 417
{
    /* If we already have a test set up run it */
    if (t->meth) {
        t->ntests++;
D
Dr. Stephen Henson 已提交
418
        if (t->skip) {
R
Rich Salz 已提交
419 420
            /*TEST_info("Line %d skipped %s test", t->start_line, t->meth->name);
             */
D
Dr. Stephen Henson 已提交
421
            t->nskip++;
422 423
        } else {
            /* run the test */
424
            if (t->err == NULL && t->meth->run_test(t) != 1) {
R
Rich Salz 已提交
425
                TEST_info("Line %d error %s", t->start_line, t->meth->name);
426 427 428
                return 0;
            }
            if (!check_test_error(t)) {
R
Rich Salz 已提交
429
                test_openssl_errors();
430 431
                t->errors++;
            }
432
        }
433
        /* clean it up */
434
        ERR_clear_error();
435 436 437 438 439
        if (t->data != NULL) {
            t->meth->cleanup(t);
            OPENSSL_free(t->data);
            t->data = NULL;
        }
R
Rich Salz 已提交
440
        clear_test(t);
441 442 443 444
    }
    t->meth = tmeth;
    return 1;
}
445

R
Rich Salz 已提交
446
static int find_key(EVP_PKEY **ppk, const char *name, KEY_LIST *lst)
447 448
{
    for (; lst; lst = lst->next) {
R
Rich Salz 已提交
449
        if (strcmp(lst->name, name) == 0) {
D
Dr. Stephen Henson 已提交
450 451 452 453
            if (ppk)
                *ppk = lst->key;
            return 1;
        }
454
    }
D
Dr. Stephen Henson 已提交
455
    return 0;
456 457
}

R
Rich Salz 已提交
458
static void free_key_list(KEY_LIST *lst)
459
{
460
    while (lst != NULL) {
R
Rich Salz 已提交
461 462
        KEY_LIST *ltmp;

463 464
        EVP_PKEY_free(lst->key);
        OPENSSL_free(lst->name);
D
Dr. Stephen Henson 已提交
465 466 467
        ltmp = lst->next;
        OPENSSL_free(lst);
        lst = ltmp;
468 469 470
    }
}

D
Dr. Stephen Henson 已提交
471 472 473
static int check_unsupported()
{
    long err = ERR_peek_error();
R
Rich Salz 已提交
474

D
Dr. Stephen Henson 已提交
475
    if (ERR_GET_LIB(err) == ERR_LIB_EVP
R
Rich Salz 已提交
476
            && ERR_GET_REASON(err) == EVP_R_UNSUPPORTED_ALGORITHM) {
D
Dr. Stephen Henson 已提交
477 478 479
        ERR_clear_error();
        return 1;
    }
480 481 482 483 484 485 486 487 488 489 490 491
#ifndef OPENSSL_NO_EC
    /*
     * If EC support is enabled we should catch also EC_R_UNKNOWN_GROUP as an
     * hint to an unsupported algorithm/curve (e.g. if binary EC support is
     * disabled).
     */
    if (ERR_GET_LIB(err) == ERR_LIB_EC
        && ERR_GET_REASON(err) == EC_R_UNKNOWN_GROUP) {
        ERR_clear_error();
        return 1;
    }
#endif /* OPENSSL_NO_EC */
D
Dr. Stephen Henson 已提交
492 493 494
    return 0;
}

495

R
Rich Salz 已提交
496
static int read_key(EVP_TEST *t)
497 498
{
    char tmpbuf[80];
R
Rich Salz 已提交
499

500
    if (t->key == NULL) {
R
Rich Salz 已提交
501 502 503
        if (!TEST_ptr(t->key = BIO_new(BIO_s_mem())))
            return 0;
    } else if (!TEST_int_gt(BIO_reset(t->key), 0)) {
504 505
        return 0;
    }
R
Rich Salz 已提交
506

507 508 509
    /* Read to PEM end line and place content in memory BIO */
    while (BIO_gets(t->in, tmpbuf, sizeof(tmpbuf))) {
        t->line++;
R
Rich Salz 已提交
510
        if (!TEST_int_gt(BIO_puts(t->key, tmpbuf), 0))
511 512 513 514
            return 0;
        if (strncmp(tmpbuf, "-----END", 8) == 0)
            return 1;
    }
R
Rich Salz 已提交
515
    TEST_error("Can't find key end");
516 517 518
    return 0;
}

R
Rich Salz 已提交
519 520 521 522
/*
 * Parse a line into the current test |t|.  Return 0 on error.
 */
static int parse_test_line(EVP_TEST *t, char *buf)
523
{
524
    char *keyword = NULL, *value = NULL;
R
Rich Salz 已提交
525 526
    int add_key = 0;
    KEY_LIST **lst = NULL, *key = NULL;
527
    EVP_PKEY *pk = NULL;
R
Rich Salz 已提交
528 529
    const EVP_TEST_METHOD *tmeth = NULL;

530 531
    if (!parse_line(&keyword, &value, buf))
        return 1;
R
Rich Salz 已提交
532
    if (strcmp(keyword, "PrivateKey") == 0) {
533 534 535
        if (!read_key(t))
            return 0;
        pk = PEM_read_bio_PrivateKey(t->key, NULL, 0, NULL);
D
Dr. Stephen Henson 已提交
536
        if (pk == NULL && !check_unsupported()) {
R
Rich Salz 已提交
537
            TEST_info("Error reading private key %s", value);
538 539 540
            ERR_print_errors_fp(stderr);
            return 0;
        }
R
Rich Salz 已提交
541
        lst = &private_keys;
D
Dr. Stephen Henson 已提交
542
        add_key = 1;
543
    }
R
Rich Salz 已提交
544
    if (strcmp(keyword, "PublicKey") == 0) {
545 546 547
        if (!read_key(t))
            return 0;
        pk = PEM_read_bio_PUBKEY(t->key, NULL, 0, NULL);
D
Dr. Stephen Henson 已提交
548
        if (pk == NULL && !check_unsupported()) {
R
Rich Salz 已提交
549
            TEST_info("Error reading public key %s", value);
550 551 552
            ERR_print_errors_fp(stderr);
            return 0;
        }
R
Rich Salz 已提交
553
        lst = &public_keys;
D
Dr. Stephen Henson 已提交
554
        add_key = 1;
555 556
    }
    /* If we have a key add to list */
D
Dr. Stephen Henson 已提交
557 558
    if (add_key) {
        if (find_key(NULL, value, *lst)) {
R
Rich Salz 已提交
559
            TEST_info("Duplicate key %s", value);
560 561
            return 0;
        }
R
Rich Salz 已提交
562 563
        if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key)))
                || !TEST_ptr(key->name = OPENSSL_strdup(value)))
564 565 566 567
            return 0;
        key->key = pk;
        key->next = *lst;
        *lst = key;
568
        return 1;
569 570
    }

571
    /* See if keyword corresponds to a test start */
R
Rich Salz 已提交
572 573
    if ((tmeth = evp_find_test(keyword)) != NULL) {
        if (!run_and_get_next(t, tmeth))
574 575
            return 0;
        t->start_line = t->line;
D
Dr. Stephen Henson 已提交
576
        t->skip = 0;
577
        if (!tmeth->init(t, value)) {
R
Rich Salz 已提交
578
            TEST_info("Unknown %s: %s", keyword, value);
579
            return 0;
580
        }
581
        return 1;
R
Rich Salz 已提交
582 583
    }
    if (t->skip)
D
Dr. Stephen Henson 已提交
584
        return 1;
R
Rich Salz 已提交
585
    if (strcmp(keyword, "Result") == 0) {
586
        if (t->expected_err) {
R
Rich Salz 已提交
587
            TEST_info("Line %d: multiple result lines", t->line);
588
            return 0;
589
        }
R
Rich Salz 已提交
590
        if (!TEST_ptr(t->expected_err = OPENSSL_strdup(value)))
591 592 593
            return 0;
    } else if (strcmp(keyword, "Function") == 0) {
        if (t->func != NULL) {
R
Rich Salz 已提交
594
            TEST_info("Line %d: multiple function lines\n", t->line);
595 596
            return 0;
        }
R
Rich Salz 已提交
597
        if (!TEST_ptr(t->func = OPENSSL_strdup(value)))
598 599 600
            return 0;
    } else if (strcmp(keyword, "Reason") == 0) {
        if (t->reason != NULL) {
R
Rich Salz 已提交
601
            TEST_info("Line %d: multiple reason lines", t->line);
602 603
            return 0;
        }
R
Rich Salz 已提交
604
        if (!TEST_ptr(t->reason = OPENSSL_strdup(value)))
605 606 607
            return 0;
    } else {
        /* Must be test specific line: try to parse it */
R
Rich Salz 已提交
608
        int rv = t->meth == NULL ? 0 : t->meth->parse(t, keyword, value);
609

R
Rich Salz 已提交
610 611
        if (rv == 0) {
            TEST_info("Line %d: unknown keyword %s", t->line, keyword);
612
            return 0;
R
Rich Salz 已提交
613 614 615 616 617 618
        }
        if (rv < 0) {
            TEST_info("Line %d: error processing keyword %s\n",
                      t->line, keyword);
            return 0;
        }
619
    }
620 621
    return 1;
}
622

623
/* Message digest tests */
B
Ben Laurie 已提交
624

R
Rich Salz 已提交
625
typedef struct digest_data_st {
626 627 628 629 630
    /* Digest this test is for */
    const EVP_MD *digest;
    /* Input to digest */
    unsigned char *input;
    size_t input_len;
631 632
    /* Repeat count for input */
    size_t nrpt;
633 634 635
    /* Expected output */
    unsigned char *output;
    size_t output_len;
R
Rich Salz 已提交
636
} DIGEST_DATA;
B
Ben Laurie 已提交
637

R
Rich Salz 已提交
638
static int digest_test_init(EVP_TEST *t, const char *alg)
639 640
{
    const EVP_MD *digest;
R
Rich Salz 已提交
641 642
    DIGEST_DATA *mdat;

643
    digest = EVP_get_digestbyname(alg);
644 645 646 647 648 649
    if (!digest) {
        /* If alg has an OID assume disabled algorithm */
        if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
            t->skip = 1;
            return 1;
        }
650
        return 0;
651
    }
R
Rich Salz 已提交
652
    mdat = OPENSSL_zalloc(sizeof(*mdat));
653
    mdat->digest = digest;
654
    mdat->nrpt = 1;
655
    t->data = mdat;
B
Ben Laurie 已提交
656
    return 1;
657
}
B
Ben Laurie 已提交
658

R
Rich Salz 已提交
659
static void digest_test_cleanup(EVP_TEST *t)
660
{
R
Rich Salz 已提交
661 662 663 664
    DIGEST_DATA *mdat = t->data;

    OPENSSL_free(mdat->input);
    OPENSSL_free(mdat->output);
665 666
}

R
Rich Salz 已提交
667
static int digest_test_parse(EVP_TEST *t,
668 669
                             const char *keyword, const char *value)
{
R
Rich Salz 已提交
670 671
    DIGEST_DATA *mdata = t->data;

R
Rich Salz 已提交
672
    if (strcmp(keyword, "Input") == 0)
673
        return test_bin(value, &mdata->input, &mdata->input_len);
R
Rich Salz 已提交
674
    if (strcmp(keyword, "Output") == 0)
675
        return test_bin(value, &mdata->output, &mdata->output_len);
R
Rich Salz 已提交
676
    if (strcmp(keyword, "Count") == 0) {
677 678 679 680 681 682
        long nrpt = atoi(value);
        if (nrpt <= 0)
            return 0;
        mdata->nrpt = (size_t)nrpt;
        return 1;
    }
683 684 685
    return 0;
}

R
Rich Salz 已提交
686
static int digest_test_run(EVP_TEST *t)
687
{
R
Rich Salz 已提交
688
    DIGEST_DATA *mdata = t->data;
689
    size_t i;
690
    EVP_MD_CTX *mctx;
B
Ben Laurie 已提交
691
    unsigned char md[EVP_MAX_MD_SIZE];
692
    unsigned int md_len;
R
Rich Salz 已提交
693 694 695

    t->err = "TEST_FAILURE";
    if (!TEST_ptr(mctx = EVP_MD_CTX_new()))
696
        goto err;
R
Rich Salz 已提交
697 698 699

    if (!EVP_DigestInit_ex(mctx, mdata->digest, NULL)) {
        t->err = "DIGESTINIT_ERROR";
700
        goto err;
701
    }
R
Rich Salz 已提交
702 703 704 705 706 707 708
    for (i = 0; i < mdata->nrpt; i++)
        if (!EVP_DigestUpdate(mctx, mdata->input, mdata->input_len)) {
            t->err = "DIGESTUPDATE_ERROR";
            goto err;
        }
    if (!EVP_DigestFinal(mctx, md, &md_len)) {
        t->err = "DIGESTFINAL_ERROR";
709
        goto err;
R
Rich Salz 已提交
710 711 712
    }
    if (md_len != mdata->output_len) {
        t->err = "DIGEST_LENGTH_MISMATCH";
713
        goto err;
R
Rich Salz 已提交
714 715 716
    }
    if (!TEST_mem_eq(mdata->output, mdata->output_len, md, md_len)) {
        t->err = "DIGEST_MISMATCH";
717
        goto err;
R
Rich Salz 已提交
718 719 720
    }
    t->err = NULL;

721
 err:
722
    EVP_MD_CTX_free(mctx);
D
Dr. Stephen Henson 已提交
723
    return 1;
724
}
B
Ben Laurie 已提交
725

R
Rich Salz 已提交
726
static const EVP_TEST_METHOD digest_test_method = {
727 728 729 730 731 732 733 734
    "Digest",
    digest_test_init,
    digest_test_cleanup,
    digest_test_parse,
    digest_test_run
};

/* Cipher tests */
R
Rich Salz 已提交
735
typedef struct cipher_data_st {
736 737
    const EVP_CIPHER *cipher;
    int enc;
738
    /* EVP_CIPH_GCM_MODE, EVP_CIPH_CCM_MODE or EVP_CIPH_OCB_MODE if AEAD */
739 740 741 742 743 744 745 746 747 748 749 750 751 752
    int aead;
    unsigned char *key;
    size_t key_len;
    unsigned char *iv;
    size_t iv_len;
    unsigned char *plaintext;
    size_t plaintext_len;
    unsigned char *ciphertext;
    size_t ciphertext_len;
    /* GCM, CCM only */
    unsigned char *aad;
    size_t aad_len;
    unsigned char *tag;
    size_t tag_len;
R
Rich Salz 已提交
753
} CIPHER_DATA;
754

R
Rich Salz 已提交
755
static int cipher_test_init(EVP_TEST *t, const char *alg)
756 757
{
    const EVP_CIPHER *cipher;
R
Rich Salz 已提交
758 759
    CIPHER_DATA *cdat = t->data;

760
    cipher = EVP_get_cipherbyname(alg);
761 762 763 764 765 766
    if (!cipher) {
        /* If alg has an OID assume disabled algorithm */
        if (OBJ_sn2nid(alg) != NID_undef || OBJ_ln2nid(alg) != NID_undef) {
            t->skip = 1;
            return 1;
        }
767
        return 0;
768
    }
R
Rich Salz 已提交
769
    cdat = OPENSSL_malloc(sizeof(*cdat));
770 771 772 773 774 775 776 777 778 779
    cdat->cipher = cipher;
    cdat->enc = -1;
    cdat->key = NULL;
    cdat->iv = NULL;
    cdat->ciphertext = NULL;
    cdat->plaintext = NULL;
    cdat->aad = NULL;
    cdat->tag = NULL;
    t->data = cdat;
    if (EVP_CIPHER_mode(cipher) == EVP_CIPH_GCM_MODE
780
        || EVP_CIPHER_mode(cipher) == EVP_CIPH_OCB_MODE
781 782
        || EVP_CIPHER_mode(cipher) == EVP_CIPH_CCM_MODE)
        cdat->aead = EVP_CIPHER_mode(cipher);
783 784
    else if (EVP_CIPHER_flags(cipher) & EVP_CIPH_FLAG_AEAD_CIPHER)
        cdat->aead = -1;
785 786
    else
        cdat->aead = 0;
B
Ben Laurie 已提交
787

788 789
    return 1;
}
B
Ben Laurie 已提交
790

R
Rich Salz 已提交
791
static void cipher_test_cleanup(EVP_TEST *t)
792
{
R
Rich Salz 已提交
793 794 795 796 797 798 799 800
    CIPHER_DATA *cdat = t->data;

    OPENSSL_free(cdat->key);
    OPENSSL_free(cdat->iv);
    OPENSSL_free(cdat->ciphertext);
    OPENSSL_free(cdat->plaintext);
    OPENSSL_free(cdat->aad);
    OPENSSL_free(cdat->tag);
801
}
B
Ben Laurie 已提交
802

R
Rich Salz 已提交
803
static int cipher_test_parse(EVP_TEST *t, const char *keyword,
804 805
                             const char *value)
{
R
Rich Salz 已提交
806 807
    CIPHER_DATA *cdat = t->data;

R
Rich Salz 已提交
808
    if (strcmp(keyword, "Key") == 0)
809
        return test_bin(value, &cdat->key, &cdat->key_len);
R
Rich Salz 已提交
810
    if (strcmp(keyword, "IV") == 0)
811
        return test_bin(value, &cdat->iv, &cdat->iv_len);
R
Rich Salz 已提交
812
    if (strcmp(keyword, "Plaintext") == 0)
813
        return test_bin(value, &cdat->plaintext, &cdat->plaintext_len);
R
Rich Salz 已提交
814
    if (strcmp(keyword, "Ciphertext") == 0)
815 816
        return test_bin(value, &cdat->ciphertext, &cdat->ciphertext_len);
    if (cdat->aead) {
R
Rich Salz 已提交
817
        if (strcmp(keyword, "AAD") == 0)
818
            return test_bin(value, &cdat->aad, &cdat->aad_len);
R
Rich Salz 已提交
819
        if (strcmp(keyword, "Tag") == 0)
820
            return test_bin(value, &cdat->tag, &cdat->tag_len);
821
    }
B
Ben Laurie 已提交
822

R
Rich Salz 已提交
823 824
    if (strcmp(keyword, "Operation") == 0) {
        if (strcmp(value, "ENCRYPT") == 0)
825
            cdat->enc = 1;
R
Rich Salz 已提交
826
        else if (strcmp(value, "DECRYPT") == 0)
827 828 829 830
            cdat->enc = 0;
        else
            return 0;
        return 1;
831
    }
832
    return 0;
833
}
B
Ben Laurie 已提交
834

R
Rich Salz 已提交
835
static int cipher_test_enc(EVP_TEST *t, int enc,
836
                           size_t out_misalign, size_t inp_misalign, int frag)
837
{
R
Rich Salz 已提交
838
    CIPHER_DATA *cdat = t->data;
839
    unsigned char *in, *out, *tmp = NULL;
840
    size_t in_len, out_len, donelen = 0;
R
Rich Salz 已提交
841
    int ok = 0, tmplen, chunklen, tmpflen;
842
    EVP_CIPHER_CTX *ctx = NULL;
R
Rich Salz 已提交
843 844 845

    t->err = "TEST_FAILURE";
    if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new()))
846 847 848 849 850 851 852 853 854 855 856 857
        goto err;
    EVP_CIPHER_CTX_set_flags(ctx, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
    if (enc) {
        in = cdat->plaintext;
        in_len = cdat->plaintext_len;
        out = cdat->ciphertext;
        out_len = cdat->ciphertext_len;
    } else {
        in = cdat->ciphertext;
        in_len = cdat->ciphertext_len;
        out = cdat->plaintext;
        out_len = cdat->plaintext_len;
858
    }
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
    if (inp_misalign == (size_t)-1) {
        /*
         * Exercise in-place encryption
         */
        tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH);
        if (!tmp)
            goto err;
        in = memcpy(tmp + out_misalign, in, in_len);
    } else {
        inp_misalign += 16 - ((out_misalign + in_len) & 15);
        /*
         * 'tmp' will store both output and copy of input. We make the copy
         * of input to specifically aligned part of 'tmp'. So we just
         * figured out how much padding would ensure the required alignment,
         * now we allocate extended buffer and finally copy the input just
         * past inp_misalign in expression below. Output will be written
         * past out_misalign...
         */
        tmp = OPENSSL_malloc(out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
                             inp_misalign + in_len);
        if (!tmp)
            goto err;
        in = memcpy(tmp + out_misalign + in_len + 2 * EVP_MAX_BLOCK_LENGTH +
                    inp_misalign, in, in_len);
    }
R
Rich Salz 已提交
884 885
    if (!EVP_CipherInit_ex(ctx, cdat->cipher, NULL, NULL, NULL, enc)) {
        t->err = "CIPHERINIT_ERROR";
886
        goto err;
R
Rich Salz 已提交
887
    }
888
    if (cdat->iv) {
889 890
        if (cdat->aead) {
            if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_IVLEN,
R
Rich Salz 已提交
891 892
                                     cdat->iv_len, 0)) {
                t->err = "INVALID_IV_LENGTH";
893
                goto err;
R
Rich Salz 已提交
894 895 896
            }
        } else if (cdat->iv_len != (size_t)EVP_CIPHER_CTX_iv_length(ctx)) {
            t->err = "INVALID_IV_LENGTH";
897
            goto err;
R
Rich Salz 已提交
898
        }
899
    }
900 901 902
    if (cdat->aead) {
        unsigned char *tag;
        /*
903 904
         * If encrypting or OCB just set tag length initially, otherwise
         * set tag length and value.
905
         */
906
        if (enc || cdat->aead == EVP_CIPH_OCB_MODE) {
R
Rich Salz 已提交
907
            t->err = "TAG_LENGTH_SET_ERROR";
908
            tag = NULL;
909
        } else {
R
Rich Salz 已提交
910
            t->err = "TAG_SET_ERROR";
911
            tag = cdat->tag;
912
        }
913 914
        if (tag || cdat->aead != EVP_CIPH_GCM_MODE) {
            if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
D
Dr. Stephen Henson 已提交
915
                                     cdat->tag_len, tag))
916
                goto err;
917
        }
918
    }
919

R
Rich Salz 已提交
920 921
    if (!EVP_CIPHER_CTX_set_key_length(ctx, cdat->key_len)) {
        t->err = "INVALID_KEY_LENGTH";
922
        goto err;
R
Rich Salz 已提交
923 924 925
    }
    if (!EVP_CipherInit_ex(ctx, NULL, NULL, cdat->key, cdat->iv, -1)) {
        t->err = "KEY_SET_ERROR";
926
        goto err;
R
Rich Salz 已提交
927
    }
928

929 930 931
    if (!enc && cdat->aead == EVP_CIPH_OCB_MODE) {
        if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_SET_TAG,
                                 cdat->tag_len, cdat->tag)) {
R
Rich Salz 已提交
932
            t->err = "TAG_SET_ERROR";
D
Dr. Stephen Henson 已提交
933
            goto err;
934 935 936
        }
    }

937 938
    if (cdat->aead == EVP_CIPH_CCM_MODE) {
        if (!EVP_CipherUpdate(ctx, NULL, &tmplen, NULL, out_len)) {
R
Rich Salz 已提交
939
            t->err = "CCM_PLAINTEXT_LENGTH_SET_ERROR";
940
            goto err;
941 942
        }
    }
943
    if (cdat->aad) {
R
Rich Salz 已提交
944
        t->err = "AAD_SET_ERROR";
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
        if (!frag) {
            if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad,
                                  cdat->aad_len))
                goto err;
        } else {
            /*
             * Supply the AAD in chunks less than the block size where possible
             */
            if (cdat->aad_len > 0) {
                if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad, 1))
                    goto err;
                donelen++;
            }
            if (cdat->aad_len > 2) {
                if (!EVP_CipherUpdate(ctx, NULL, &chunklen, cdat->aad + donelen,
                                      cdat->aad_len - 2))
                    goto err;
                donelen += cdat->aad_len - 2;
            }
            if (cdat->aad_len > 1
                    && !EVP_CipherUpdate(ctx, NULL, &chunklen,
                                         cdat->aad + donelen, 1))
                goto err;
968 969 970
        }
    }
    EVP_CIPHER_CTX_set_padding(ctx, 0);
R
Rich Salz 已提交
971
    t->err = "CIPHERUPDATE_ERROR";
972 973 974 975 976 977 978 979 980 981 982
    tmplen = 0;
    if (!frag) {
        /* We supply the data all in one go */
        if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &tmplen, in, in_len))
            goto err;
    } else {
        /* Supply the data in chunks less than the block size where possible */
        if (in_len > 0) {
            if (!EVP_CipherUpdate(ctx, tmp + out_misalign, &chunklen, in, 1))
                goto err;
            tmplen += chunklen;
983 984
            in++;
            in_len--;
985
        }
986
        if (in_len > 1) {
987
            if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
988
                                  in, in_len - 1))
989 990
                goto err;
            tmplen += chunklen;
991 992
            in += in_len - 1;
            in_len = 1;
993
        }
994
        if (in_len > 0 ) {
995
            if (!EVP_CipherUpdate(ctx, tmp + out_misalign + tmplen, &chunklen,
996
                                  in, 1))
997 998 999 1000
                goto err;
            tmplen += chunklen;
        }
    }
R
Rich Salz 已提交
1001 1002
    if (!EVP_CipherFinal_ex(ctx, tmp + out_misalign + tmplen, &tmpflen)) {
        t->err = "CIPHERFINAL_ERROR";
1003
        goto err;
R
Rich Salz 已提交
1004 1005 1006
    }
    if (!TEST_mem_eq(out, out_len, tmp + out_misalign, tmplen + tmpflen)) {
        t->err = "VALUE_MISMATCH";
1007
        goto err;
R
Rich Salz 已提交
1008
    }
1009 1010
    if (enc && cdat->aead) {
        unsigned char rtag[16];
R
Rich Salz 已提交
1011

1012
        if (cdat->tag_len > sizeof(rtag)) {
R
Rich Salz 已提交
1013
            t->err = "TAG_LENGTH_INTERNAL_ERROR";
1014 1015
            goto err;
        }
1016
        if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_AEAD_GET_TAG,
1017
                                 cdat->tag_len, rtag)) {
R
Rich Salz 已提交
1018
            t->err = "TAG_RETRIEVE_ERROR";
1019 1020
            goto err;
        }
R
Rich Salz 已提交
1021 1022
        if (!TEST_mem_eq(cdat->tag, cdat->tag_len, rtag, cdat->tag_len)) {
            t->err = "TAG_VALUE_MISMATCH";
1023 1024 1025
            goto err;
        }
    }
R
Rich Salz 已提交
1026 1027
    t->err = NULL;
    ok = 1;
1028
 err:
R
Rich Salz 已提交
1029
    OPENSSL_free(tmp);
1030
    EVP_CIPHER_CTX_free(ctx);
R
Rich Salz 已提交
1031
    return ok;
1032
}
B
Ben Laurie 已提交
1033

R
Rich Salz 已提交
1034
static int cipher_test_run(EVP_TEST *t)
1035
{
R
Rich Salz 已提交
1036
    CIPHER_DATA *cdat = t->data;
1037
    int rv, frag = 0;
1038 1039
    size_t out_misalign, inp_misalign;

1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
    if (!cdat->key) {
        t->err = "NO_KEY";
        return 0;
    }
    if (!cdat->iv && EVP_CIPHER_iv_length(cdat->cipher)) {
        /* IV is optional and usually omitted in wrap mode */
        if (EVP_CIPHER_mode(cdat->cipher) != EVP_CIPH_WRAP_MODE) {
            t->err = "NO_IV";
            return 0;
        }
    }
    if (cdat->aead && !cdat->tag) {
        t->err = "NO_TAG";
        return 0;
    }
1055
    for (out_misalign = 0; out_misalign <= 1;) {
1056 1057
        static char aux_err[64];
        t->aux_err = aux_err;
1058 1059 1060
        for (inp_misalign = (size_t)-1; inp_misalign != 2; inp_misalign++) {
            if (inp_misalign == (size_t)-1) {
                /* kludge: inp_misalign == -1 means "exercise in-place" */
1061 1062 1063 1064
                BIO_snprintf(aux_err, sizeof(aux_err),
                             "%s in-place, %sfragmented",
                             out_misalign ? "misaligned" : "aligned",
                             frag ? "" : "not ");
1065
            } else {
1066 1067
                BIO_snprintf(aux_err, sizeof(aux_err),
                             "%s output and %s input, %sfragmented",
1068
                             out_misalign ? "misaligned" : "aligned",
1069 1070
                             inp_misalign ? "misaligned" : "aligned",
                             frag ? "" : "not ");
1071
            }
1072
            if (cdat->enc) {
1073
                rv = cipher_test_enc(t, 1, out_misalign, inp_misalign, frag);
1074 1075 1076 1077 1078 1079 1080 1081
                /* Not fatal errors: return */
                if (rv != 1) {
                    if (rv < 0)
                        return 0;
                    return 1;
                }
            }
            if (cdat->enc != 1) {
1082
                rv = cipher_test_enc(t, 0, out_misalign, inp_misalign, frag);
1083 1084 1085 1086 1087 1088 1089
                /* Not fatal errors: return */
                if (rv != 1) {
                    if (rv < 0)
                        return 0;
                    return 1;
                }
            }
1090
        }
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105

        if (out_misalign == 1 && frag == 0) {
            /*
             * XTS, CCM and Wrap modes have special requirements about input
             * lengths so we don't fragment for those
             */
            if (cdat->aead == EVP_CIPH_CCM_MODE
                    || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_XTS_MODE
                     || EVP_CIPHER_mode(cdat->cipher) == EVP_CIPH_WRAP_MODE)
                break;
            out_misalign = 0;
            frag++;
        } else {
            out_misalign++;
        }
1106
    }
1107 1108
    t->aux_err = NULL;

1109
    return 1;
1110
}
1111

R
Rich Salz 已提交
1112
static const EVP_TEST_METHOD cipher_test_method = {
1113 1114 1115 1116 1117 1118
    "Cipher",
    cipher_test_init,
    cipher_test_cleanup,
    cipher_test_parse,
    cipher_test_run
};
D
Dr. Stephen Henson 已提交
1119

R
Rich Salz 已提交
1120
typedef struct mac_data_st {
D
Dr. Stephen Henson 已提交
1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133
    /* MAC type */
    int type;
    /* Algorithm string for this MAC */
    char *alg;
    /* MAC key */
    unsigned char *key;
    size_t key_len;
    /* Input to MAC */
    unsigned char *input;
    size_t input_len;
    /* Expected output */
    unsigned char *output;
    size_t output_len;
R
Rich Salz 已提交
1134
} MAC_DATA;
D
Dr. Stephen Henson 已提交
1135

R
Rich Salz 已提交
1136
static int mac_test_init(EVP_TEST *t, const char *alg)
D
Dr. Stephen Henson 已提交
1137 1138
{
    int type;
R
Rich Salz 已提交
1139 1140
    MAC_DATA *mdat;

M
Matt Caswell 已提交
1141
    if (strcmp(alg, "HMAC") == 0) {
D
Dr. Stephen Henson 已提交
1142
        type = EVP_PKEY_HMAC;
M
Matt Caswell 已提交
1143 1144
    } else if (strcmp(alg, "CMAC") == 0) {
#ifndef OPENSSL_NO_CMAC
D
Dr. Stephen Henson 已提交
1145
        type = EVP_PKEY_CMAC;
M
Matt Caswell 已提交
1146 1147 1148
#else
        t->skip = 1;
        return 1;
1149 1150 1151 1152 1153 1154 1155
#endif
    } else if (strcmp(alg, "Poly1305") == 0) {
#ifndef OPENSSL_NO_POLY1305
        type = EVP_PKEY_POLY1305;
#else
        t->skip = 1;
        return 1;
1156 1157 1158 1159 1160 1161 1162
#endif
    } else if (strcmp(alg, "SipHash") == 0) {
#ifndef OPENSSL_NO_SIPHASH
        type = EVP_PKEY_SIPHASH;
#else
        t->skip = 1;
        return 1;
M
Matt Caswell 已提交
1163 1164
#endif
    } else
D
Dr. Stephen Henson 已提交
1165 1166
        return 0;

R
Rich Salz 已提交
1167
    mdat = OPENSSL_zalloc(sizeof(*mdat));
D
Dr. Stephen Henson 已提交
1168 1169 1170 1171 1172
    mdat->type = type;
    t->data = mdat;
    return 1;
}

R
Rich Salz 已提交
1173
static void mac_test_cleanup(EVP_TEST *t)
D
Dr. Stephen Henson 已提交
1174
{
R
Rich Salz 已提交
1175 1176 1177 1178 1179 1180
    MAC_DATA *mdat = t->data;

    OPENSSL_free(mdat->alg);
    OPENSSL_free(mdat->key);
    OPENSSL_free(mdat->input);
    OPENSSL_free(mdat->output);
D
Dr. Stephen Henson 已提交
1181 1182
}

R
Rich Salz 已提交
1183
static int mac_test_parse(EVP_TEST *t,
D
Dr. Stephen Henson 已提交
1184 1185
                          const char *keyword, const char *value)
{
R
Rich Salz 已提交
1186 1187
    MAC_DATA *mdata = t->data;

R
Rich Salz 已提交
1188
    if (strcmp(keyword, "Key") == 0)
D
Dr. Stephen Henson 已提交
1189
        return test_bin(value, &mdata->key, &mdata->key_len);
R
Rich Salz 已提交
1190
    if (strcmp(keyword, "Algorithm") == 0) {
R
Rich Salz 已提交
1191
        mdata->alg = OPENSSL_strdup(value);
D
Dr. Stephen Henson 已提交
1192 1193 1194 1195
        if (!mdata->alg)
            return 0;
        return 1;
    }
R
Rich Salz 已提交
1196
    if (strcmp(keyword, "Input") == 0)
D
Dr. Stephen Henson 已提交
1197
        return test_bin(value, &mdata->input, &mdata->input_len);
R
Rich Salz 已提交
1198
    if (strcmp(keyword, "Output") == 0)
D
Dr. Stephen Henson 已提交
1199 1200 1201 1202
        return test_bin(value, &mdata->output, &mdata->output_len);
    return 0;
}

R
Rich Salz 已提交
1203
static int mac_test_run(EVP_TEST *t)
D
Dr. Stephen Henson 已提交
1204
{
R
Rich Salz 已提交
1205
    MAC_DATA *mdata = t->data;
D
Dr. Stephen Henson 已提交
1206 1207 1208 1209 1210 1211 1212
    EVP_MD_CTX *mctx = NULL;
    EVP_PKEY_CTX *pctx = NULL, *genctx = NULL;
    EVP_PKEY *key = NULL;
    const EVP_MD *md = NULL;
    unsigned char *mac = NULL;
    size_t mac_len;

M
Matt Caswell 已提交
1213
#ifdef OPENSSL_NO_DES
1214
    if (mdata->alg != NULL && strstr(mdata->alg, "DES") != NULL) {
M
Matt Caswell 已提交
1215
        /* Skip DES */
R
Rich Salz 已提交
1216
        t->err = NULL;
M
Matt Caswell 已提交
1217 1218 1219 1220
        goto err;
    }
#endif

R
Rich Salz 已提交
1221 1222
    if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_id(mdata->type, NULL))) {
        t->err = "MAC_PKEY_CTX_ERROR";
D
Dr. Stephen Henson 已提交
1223
        goto err;
R
Rich Salz 已提交
1224
    }
D
Dr. Stephen Henson 已提交
1225

R
Rich Salz 已提交
1226 1227 1228 1229 1230 1231 1232
    if (EVP_PKEY_keygen_init(genctx) <= 0) {
        t->err = "MAC_KEYGEN_INIT_ERROR";
        goto err;
    }
    if (mdata->type == EVP_PKEY_CMAC
             && EVP_PKEY_CTX_ctrl_str(genctx, "cipher", mdata->alg) <= 0) {
        t->err = "MAC_ALGORITHM_SET_ERROR";
D
Dr. Stephen Henson 已提交
1233 1234 1235
        goto err;
    }

R
Rich Salz 已提交
1236 1237
    if (EVP_PKEY_CTX_set_mac_key(genctx, mdata->key, mdata->key_len) <= 0) {
        t->err = "MAC_KEY_SET_ERROR";
D
Dr. Stephen Henson 已提交
1238
        goto err;
R
Rich Salz 已提交
1239
    }
D
Dr. Stephen Henson 已提交
1240

R
Rich Salz 已提交
1241 1242
    if (EVP_PKEY_keygen(genctx, &key) <= 0) {
        t->err = "MAC_KEY_GENERATE_ERROR";
D
Dr. Stephen Henson 已提交
1243
        goto err;
R
Rich Salz 已提交
1244
    }
D
Dr. Stephen Henson 已提交
1245
    if (mdata->type == EVP_PKEY_HMAC) {
R
Rich Salz 已提交
1246 1247
        if (!TEST_ptr(md = EVP_get_digestbyname(mdata->alg))) {
            t->err = "MAC_ALGORITHM_SET_ERROR";
D
Dr. Stephen Henson 已提交
1248
            goto err;
R
Rich Salz 已提交
1249
        }
D
Dr. Stephen Henson 已提交
1250
    }
R
Rich Salz 已提交
1251 1252
    if (!TEST_ptr(mctx = EVP_MD_CTX_new())) {
        t->err = "INTERNAL_ERROR";
D
Dr. Stephen Henson 已提交
1253
        goto err;
R
Rich Salz 已提交
1254 1255 1256
    }
    if (!EVP_DigestSignInit(mctx, &pctx, md, NULL, key)) {
        t->err = "DIGESTSIGNINIT_ERROR";
D
Dr. Stephen Henson 已提交
1257
        goto err;
R
Rich Salz 已提交
1258
    }
D
Dr. Stephen Henson 已提交
1259

R
Rich Salz 已提交
1260 1261
    if (!EVP_DigestSignUpdate(mctx, mdata->input, mdata->input_len)) {
        t->err = "DIGESTSIGNUPDATE_ERROR";
D
Dr. Stephen Henson 已提交
1262 1263
        goto err;
    }
R
Rich Salz 已提交
1264 1265
    if (!EVP_DigestSignFinal(mctx, NULL, &mac_len)) {
        t->err = "DIGESTSIGNFINAL_LENGTH_ERROR";
D
Dr. Stephen Henson 已提交
1266
        goto err;
R
Rich Salz 已提交
1267
    }
R
Rich Salz 已提交
1268
    if (!TEST_ptr(mac = OPENSSL_malloc(mac_len))) {
R
Rich Salz 已提交
1269
        t->err = "TEST_FAILURE";
D
Dr. Stephen Henson 已提交
1270
        goto err;
R
Rich Salz 已提交
1271
    }
R
Rich Salz 已提交
1272 1273 1274 1275 1276
    if (!EVP_DigestSignFinal(mctx, mac, &mac_len)
            || !TEST_mem_eq(mdata->output, mdata->output_len, mac, mac_len)) {
        t->err = "TEST_MAC_ERR";
        goto err;
    }
R
Rich Salz 已提交
1277 1278

    t->err = NULL;
D
Dr. Stephen Henson 已提交
1279
 err:
1280
    EVP_MD_CTX_free(mctx);
R
Rich Salz 已提交
1281
    OPENSSL_free(mac);
R
Rich Salz 已提交
1282 1283
    EVP_PKEY_CTX_free(genctx);
    EVP_PKEY_free(key);
D
Dr. Stephen Henson 已提交
1284 1285 1286
    return 1;
}

R
Rich Salz 已提交
1287
static const EVP_TEST_METHOD mac_test_method = {
D
Dr. Stephen Henson 已提交
1288 1289 1290 1291 1292 1293
    "MAC",
    mac_test_init,
    mac_test_cleanup,
    mac_test_parse,
    mac_test_run
};
1294 1295 1296 1297 1298 1299

/*
 * Public key operations. These are all very similar and can share
 * a lot of common code.
 */

R
Rich Salz 已提交
1300
typedef struct pkey_data_st {
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
    /* Context for this operation */
    EVP_PKEY_CTX *ctx;
    /* Key operation to perform */
    int (*keyop) (EVP_PKEY_CTX *ctx,
                  unsigned char *sig, size_t *siglen,
                  const unsigned char *tbs, size_t tbslen);
    /* Input to MAC */
    unsigned char *input;
    size_t input_len;
    /* Expected output */
    unsigned char *output;
    size_t output_len;
R
Rich Salz 已提交
1313
} PKEY_DATA;
1314 1315 1316 1317 1318

/*
 * Perform public key operation setup: lookup key, allocated ctx and call
 * the appropriate initialisation function
 */
R
Rich Salz 已提交
1319
static int pkey_test_init(EVP_TEST *t, const char *name,
1320 1321 1322 1323 1324 1325 1326 1327
                          int use_public,
                          int (*keyopinit) (EVP_PKEY_CTX *ctx),
                          int (*keyop) (EVP_PKEY_CTX *ctx,
                                        unsigned char *sig, size_t *siglen,
                                        const unsigned char *tbs,
                                        size_t tbslen)
    )
{
R
Rich Salz 已提交
1328
    PKEY_DATA *kdata;
1329
    EVP_PKEY *pkey = NULL;
D
Dr. Stephen Henson 已提交
1330
    int rv = 0;
R
Rich Salz 已提交
1331

D
Dr. Stephen Henson 已提交
1332
    if (use_public)
R
Rich Salz 已提交
1333 1334 1335 1336
        rv = find_key(&pkey, name, public_keys);
    if (rv == 0)
        rv = find_key(&pkey, name, private_keys);
    if (rv == 0 || pkey == NULL) {
D
Dr. Stephen Henson 已提交
1337 1338 1339 1340
        t->skip = 1;
        return 1;
    }

R
Rich Salz 已提交
1341
    if (!TEST_ptr(kdata = OPENSSL_malloc(sizeof(*kdata)))) {
D
Dr. Stephen Henson 已提交
1342
        EVP_PKEY_free(pkey);
1343
        return 0;
D
Dr. Stephen Henson 已提交
1344
    }
1345 1346 1347 1348 1349
    kdata->ctx = NULL;
    kdata->input = NULL;
    kdata->output = NULL;
    kdata->keyop = keyop;
    t->data = kdata;
R
Rich Salz 已提交
1350
    if (!TEST_ptr(kdata->ctx = EVP_PKEY_CTX_new(pkey, NULL)))
1351 1352
        return 0;
    if (keyopinit(kdata->ctx) <= 0)
1353
        t->err = "KEYOP_INIT_ERROR";
1354 1355 1356
    return 1;
}

R
Rich Salz 已提交
1357
static void pkey_test_cleanup(EVP_TEST *t)
1358
{
R
Rich Salz 已提交
1359
    PKEY_DATA *kdata = t->data;
R
Rich Salz 已提交
1360 1361 1362

    OPENSSL_free(kdata->input);
    OPENSSL_free(kdata->output);
R
Rich Salz 已提交
1363
    EVP_PKEY_CTX_free(kdata->ctx);
1364 1365
}

R
Rich Salz 已提交
1366
static int pkey_test_ctrl(EVP_TEST *t, EVP_PKEY_CTX *pctx,
1367
                          const char *value)
1368 1369 1370 1371
{
    int rv;
    char *p, *tmpval;

R
Rich Salz 已提交
1372
    if (!TEST_ptr(tmpval = OPENSSL_strdup(value)))
1373 1374 1375 1376 1377
        return 0;
    p = strchr(tmpval, ':');
    if (p != NULL)
        *p++ = 0;
    rv = EVP_PKEY_CTX_ctrl_str(pctx, tmpval, p);
1378 1379 1380 1381 1382 1383
    if (rv == -2) {
        t->err = "PKEY_CTRL_INVALID";
        rv = 1;
    } else if (p != NULL && rv <= 0) {
        /* If p has an OID and lookup fails assume disabled algorithm */
        int nid = OBJ_sn2nid(p);
R
Rich Salz 已提交
1384

1385 1386 1387 1388
        if (nid == NID_undef)
             nid = OBJ_ln2nid(p);
        if ((nid != NID_undef) && EVP_get_digestbynid(nid) == NULL &&
            EVP_get_cipherbynid(nid) == NULL) {
1389 1390
            t->skip = 1;
            rv = 1;
1391 1392 1393
        } else {
            t->err = "PKEY_CTRL_ERROR";
            rv = 1;
1394 1395
        }
    }
1396 1397 1398 1399
    OPENSSL_free(tmpval);
    return rv > 0;
}

R
Rich Salz 已提交
1400
static int pkey_test_parse(EVP_TEST *t,
1401 1402
                           const char *keyword, const char *value)
{
R
Rich Salz 已提交
1403
    PKEY_DATA *kdata = t->data;
R
Rich Salz 已提交
1404
    if (strcmp(keyword, "Input") == 0)
1405
        return test_bin(value, &kdata->input, &kdata->input_len);
R
Rich Salz 已提交
1406
    if (strcmp(keyword, "Output") == 0)
1407
        return test_bin(value, &kdata->output, &kdata->output_len);
1408
    if (strcmp(keyword, "Ctrl") == 0)
1409
        return pkey_test_ctrl(t, kdata->ctx, value);
1410 1411 1412
    return 0;
}

R
Rich Salz 已提交
1413
static int pkey_test_run(EVP_TEST *t)
1414
{
R
Rich Salz 已提交
1415
    PKEY_DATA *kdata = t->data;
1416 1417
    unsigned char *out = NULL;
    size_t out_len;
R
Rich Salz 已提交
1418

1419
    if (kdata->keyop(kdata->ctx, NULL, &out_len, kdata->input,
R
Rich Salz 已提交
1420 1421 1422
                     kdata->input_len) <= 0
            || !TEST_ptr(out = OPENSSL_malloc(out_len))) {
        t->err = "KEYOP_LENGTH_ERROR";
1423
        goto err;
R
Rich Salz 已提交
1424 1425 1426 1427
    }
    if (kdata->keyop(kdata->ctx, out,
                     &out_len, kdata->input, kdata->input_len) <= 0) {
        t->err = "KEYOP_ERROR";
1428
        goto err;
R
Rich Salz 已提交
1429 1430 1431
    }
    if (!TEST_mem_eq(kdata->output, kdata->output_len, out, out_len)) {
        t->err = "KEYOP_MISMATCH";
1432
        goto err;
R
Rich Salz 已提交
1433 1434
    }
    t->err = NULL;
1435
 err:
R
Rich Salz 已提交
1436
    OPENSSL_free(out);
1437 1438 1439
    return 1;
}

R
Rich Salz 已提交
1440
static int sign_test_init(EVP_TEST *t, const char *name)
1441 1442 1443 1444
{
    return pkey_test_init(t, name, 0, EVP_PKEY_sign_init, EVP_PKEY_sign);
}

R
Rich Salz 已提交
1445
static const EVP_TEST_METHOD psign_test_method = {
1446 1447 1448 1449 1450 1451 1452
    "Sign",
    sign_test_init,
    pkey_test_cleanup,
    pkey_test_parse,
    pkey_test_run
};

R
Rich Salz 已提交
1453
static int verify_recover_test_init(EVP_TEST *t, const char *name)
1454 1455 1456 1457 1458
{
    return pkey_test_init(t, name, 1, EVP_PKEY_verify_recover_init,
                          EVP_PKEY_verify_recover);
}

R
Rich Salz 已提交
1459
static const EVP_TEST_METHOD pverify_recover_test_method = {
1460 1461 1462 1463 1464 1465 1466
    "VerifyRecover",
    verify_recover_test_init,
    pkey_test_cleanup,
    pkey_test_parse,
    pkey_test_run
};

R
Rich Salz 已提交
1467
static int decrypt_test_init(EVP_TEST *t, const char *name)
1468 1469 1470 1471 1472
{
    return pkey_test_init(t, name, 0, EVP_PKEY_decrypt_init,
                          EVP_PKEY_decrypt);
}

R
Rich Salz 已提交
1473
static const EVP_TEST_METHOD pdecrypt_test_method = {
1474 1475 1476 1477 1478 1479 1480
    "Decrypt",
    decrypt_test_init,
    pkey_test_cleanup,
    pkey_test_parse,
    pkey_test_run
};

R
Rich Salz 已提交
1481
static int verify_test_init(EVP_TEST *t, const char *name)
1482 1483 1484 1485
{
    return pkey_test_init(t, name, 1, EVP_PKEY_verify_init, 0);
}

R
Rich Salz 已提交
1486
static int verify_test_run(EVP_TEST *t)
1487
{
R
Rich Salz 已提交
1488 1489
    PKEY_DATA *kdata = t->data;

1490 1491 1492 1493 1494 1495
    if (EVP_PKEY_verify(kdata->ctx, kdata->output, kdata->output_len,
                        kdata->input, kdata->input_len) <= 0)
        t->err = "VERIFY_ERROR";
    return 1;
}

R
Rich Salz 已提交
1496
static const EVP_TEST_METHOD pverify_test_method = {
1497 1498 1499 1500 1501 1502
    "Verify",
    verify_test_init,
    pkey_test_cleanup,
    pkey_test_parse,
    verify_test_run
};
D
Dr. Stephen Henson 已提交
1503

1504

R
Rich Salz 已提交
1505
static int pderive_test_init(EVP_TEST *t, const char *name)
1506 1507 1508 1509
{
    return pkey_test_init(t, name, 0, EVP_PKEY_derive_init, 0);
}

R
Rich Salz 已提交
1510
static int pderive_test_parse(EVP_TEST *t,
1511 1512
                              const char *keyword, const char *value)
{
R
Rich Salz 已提交
1513
    PKEY_DATA *kdata = t->data;
1514 1515 1516

    if (strcmp(keyword, "PeerKey") == 0) {
        EVP_PKEY *peer;
R
Rich Salz 已提交
1517
        if (find_key(&peer, value, public_keys) == 0)
1518 1519 1520 1521 1522 1523 1524
            return 0;
        if (EVP_PKEY_derive_set_peer(kdata->ctx, peer) <= 0)
            return 0;
        return 1;
    }
    if (strcmp(keyword, "SharedSecret") == 0)
        return test_bin(value, &kdata->output, &kdata->output_len);
1525
    if (strcmp(keyword, "Ctrl") == 0)
1526
        return pkey_test_ctrl(t, kdata->ctx, value);
1527 1528 1529
    return 0;
}

R
Rich Salz 已提交
1530
static int pderive_test_run(EVP_TEST *t)
1531
{
R
Rich Salz 已提交
1532
    PKEY_DATA *kdata = t->data;
1533 1534 1535 1536
    unsigned char *out = NULL;
    size_t out_len;

    out_len = kdata->output_len;
R
Rich Salz 已提交
1537 1538
    if (!TEST_ptr(out = OPENSSL_malloc(out_len))) {
        t->err = "DERIVE_ERROR";
1539
        goto err;
R
Rich Salz 已提交
1540 1541 1542
    }
    if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0) {
        t->err = "DERIVE_ERROR";
1543
        goto err;
R
Rich Salz 已提交
1544 1545 1546
    }
    if (!TEST_mem_eq(kdata->output, kdata->output_len, out, out_len)) {
        t->err = "SHARED_SECRET_MISMATCH";
1547
        goto err;
R
Rich Salz 已提交
1548 1549 1550
    }

    t->err = NULL;
1551 1552 1553 1554 1555
 err:
    OPENSSL_free(out);
    return 1;
}

R
Rich Salz 已提交
1556
static const EVP_TEST_METHOD pderive_test_method = {
1557 1558 1559 1560 1561 1562 1563
    "Derive",
    pderive_test_init,
    pkey_test_cleanup,
    pderive_test_parse,
    pderive_test_run
};

D
Dr. Stephen Henson 已提交
1564 1565 1566
/* PBE tests */

#define PBE_TYPE_SCRYPT 1
D
Dr. Stephen Henson 已提交
1567 1568
#define PBE_TYPE_PBKDF2 2
#define PBE_TYPE_PKCS12 3
D
Dr. Stephen Henson 已提交
1569

R
Rich Salz 已提交
1570
typedef struct pbe_data_st {
D
Dr. Stephen Henson 已提交
1571
    int pbe_type;
R
Rich Salz 已提交
1572
        /* scrypt parameters */
D
Dr. Stephen Henson 已提交
1573
    uint64_t N, r, p, maxmem;
R
Rich Salz 已提交
1574
        /* PKCS#12 parameters */
D
Dr. Stephen Henson 已提交
1575 1576
    int id, iter;
    const EVP_MD *md;
R
Rich Salz 已提交
1577
        /* password */
D
Dr. Stephen Henson 已提交
1578 1579
    unsigned char *pass;
    size_t pass_len;
R
Rich Salz 已提交
1580
        /* salt */
D
Dr. Stephen Henson 已提交
1581 1582
    unsigned char *salt;
    size_t salt_len;
R
Rich Salz 已提交
1583
        /* Expected output */
D
Dr. Stephen Henson 已提交
1584 1585
    unsigned char *key;
    size_t key_len;
R
Rich Salz 已提交
1586
} PBE_DATA;
D
Dr. Stephen Henson 已提交
1587

R
Rich Salz 已提交
1588
#ifndef OPENSSL_NO_SCRYPT
R
Rich Salz 已提交
1589
static int scrypt_test_parse(EVP_TEST *t,
D
Dr. Stephen Henson 已提交
1590 1591
                             const char *keyword, const char *value)
{
R
Rich Salz 已提交
1592
    PBE_DATA *pdata = t->data;
D
Dr. Stephen Henson 已提交
1593

D
Dr. Stephen Henson 已提交
1594 1595 1596 1597 1598 1599 1600 1601 1602 1603
    if (strcmp(keyword, "N") == 0)
        return test_uint64(value, &pdata->N);
    if (strcmp(keyword, "p") == 0)
        return test_uint64(value, &pdata->p);
    if (strcmp(keyword, "r") == 0)
        return test_uint64(value, &pdata->r);
    if (strcmp(keyword, "maxmem") == 0)
        return test_uint64(value, &pdata->maxmem);
    return 0;
}
R
Rich Salz 已提交
1604
#endif
D
Dr. Stephen Henson 已提交
1605

R
Rich Salz 已提交
1606
static int pbkdf2_test_parse(EVP_TEST *t,
D
Dr. Stephen Henson 已提交
1607
                             const char *keyword, const char *value)
D
Dr. Stephen Henson 已提交
1608
{
R
Rich Salz 已提交
1609
    PBE_DATA *pdata = t->data;
D
Dr. Stephen Henson 已提交
1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625

    if (strcmp(keyword, "iter") == 0) {
        pdata->iter = atoi(value);
        if (pdata->iter <= 0)
            return 0;
        return 1;
    }
    if (strcmp(keyword, "MD") == 0) {
        pdata->md = EVP_get_digestbyname(value);
        if (pdata->md == NULL)
            return 0;
        return 1;
    }
    return 0;
}

R
Rich Salz 已提交
1626
static int pkcs12_test_parse(EVP_TEST *t,
D
Dr. Stephen Henson 已提交
1627 1628
                             const char *keyword, const char *value)
{
R
Rich Salz 已提交
1629
    PBE_DATA *pdata = t->data;
D
Dr. Stephen Henson 已提交
1630 1631 1632 1633 1634 1635 1636 1637

    if (strcmp(keyword, "id") == 0) {
        pdata->id = atoi(value);
        if (pdata->id <= 0)
            return 0;
        return 1;
    }
    return pbkdf2_test_parse(t, keyword, value);
D
Dr. Stephen Henson 已提交
1638 1639
}

R
Rich Salz 已提交
1640
static int pbe_test_init(EVP_TEST *t, const char *alg)
D
Dr. Stephen Henson 已提交
1641
{
R
Rich Salz 已提交
1642
    PBE_DATA *pdat;
D
Dr. Stephen Henson 已提交
1643
    int pbe_type = 0;
D
Dr. Stephen Henson 已提交
1644

M
Matt Caswell 已提交
1645
    if (strcmp(alg, "scrypt") == 0) {
R
Rich Salz 已提交
1646
#ifndef OPENSSL_NO_SCRYPT
D
Dr. Stephen Henson 已提交
1647
        pbe_type = PBE_TYPE_SCRYPT;
M
Matt Caswell 已提交
1648 1649 1650
#else
        t->skip = 1;
        return 1;
R
Rich Salz 已提交
1651
#endif
M
Matt Caswell 已提交
1652
    } else if (strcmp(alg, "pbkdf2") == 0) {
D
Dr. Stephen Henson 已提交
1653
        pbe_type = PBE_TYPE_PBKDF2;
M
Matt Caswell 已提交
1654
    } else if (strcmp(alg, "pkcs12") == 0) {
D
Dr. Stephen Henson 已提交
1655
        pbe_type = PBE_TYPE_PKCS12;
M
Matt Caswell 已提交
1656
    } else {
R
Rich Salz 已提交
1657
        TEST_error("Unknown pbe algorithm %s", alg);
M
Matt Caswell 已提交
1658
    }
D
Dr. Stephen Henson 已提交
1659 1660 1661 1662 1663 1664 1665 1666
    pdat = OPENSSL_malloc(sizeof(*pdat));
    pdat->pbe_type = pbe_type;
    pdat->pass = NULL;
    pdat->salt = NULL;
    pdat->N = 0;
    pdat->r = 0;
    pdat->p = 0;
    pdat->maxmem = 0;
D
Dr. Stephen Henson 已提交
1667 1668 1669
    pdat->id = 0;
    pdat->iter = 0;
    pdat->md = NULL;
D
Dr. Stephen Henson 已提交
1670 1671 1672 1673
    t->data = pdat;
    return 1;
}

R
Rich Salz 已提交
1674
static void pbe_test_cleanup(EVP_TEST *t)
D
Dr. Stephen Henson 已提交
1675
{
R
Rich Salz 已提交
1676 1677 1678 1679 1680
    PBE_DATA *pdat = t->data;

    OPENSSL_free(pdat->pass);
    OPENSSL_free(pdat->salt);
    OPENSSL_free(pdat->key);
D
Dr. Stephen Henson 已提交
1681 1682
}

R
Rich Salz 已提交
1683 1684
static int pbe_test_parse(EVP_TEST *t,
                          const char *keyword, const char *value)
D
Dr. Stephen Henson 已提交
1685
{
R
Rich Salz 已提交
1686
    PBE_DATA *pdata = t->data;
D
Dr. Stephen Henson 已提交
1687

D
Dr. Stephen Henson 已提交
1688 1689 1690 1691 1692 1693
    if (strcmp(keyword, "Password") == 0)
        return test_bin(value, &pdata->pass, &pdata->pass_len);
    if (strcmp(keyword, "Salt") == 0)
        return test_bin(value, &pdata->salt, &pdata->salt_len);
    if (strcmp(keyword, "Key") == 0)
        return test_bin(value, &pdata->key, &pdata->key_len);
R
Rich Salz 已提交
1694
    if (pdata->pbe_type == PBE_TYPE_PBKDF2)
D
Dr. Stephen Henson 已提交
1695 1696 1697
        return pbkdf2_test_parse(t, keyword, value);
    else if (pdata->pbe_type == PBE_TYPE_PKCS12)
        return pkcs12_test_parse(t, keyword, value);
R
Rich Salz 已提交
1698 1699 1700 1701
#ifndef OPENSSL_NO_SCRYPT
    else if (pdata->pbe_type == PBE_TYPE_SCRYPT)
        return scrypt_test_parse(t, keyword, value);
#endif
D
Dr. Stephen Henson 已提交
1702 1703 1704
    return 0;
}

R
Rich Salz 已提交
1705
static int pbe_test_run(EVP_TEST *t)
D
Dr. Stephen Henson 已提交
1706
{
R
Rich Salz 已提交
1707
    PBE_DATA *pdata = t->data;
D
Dr. Stephen Henson 已提交
1708 1709
    unsigned char *key;

R
Rich Salz 已提交
1710 1711
    if (!TEST_ptr(key = OPENSSL_malloc(pdata->key_len))) {
        t->err = "INTERNAL_ERROR";
D
Dr. Stephen Henson 已提交
1712
        goto err;
R
Rich Salz 已提交
1713
    }
D
Dr. Stephen Henson 已提交
1714 1715 1716 1717
    if (pdata->pbe_type == PBE_TYPE_PBKDF2) {
        if (PKCS5_PBKDF2_HMAC((char *)pdata->pass, pdata->pass_len,
                              pdata->salt, pdata->salt_len,
                              pdata->iter, pdata->md,
R
Rich Salz 已提交
1718 1719
                              pdata->key_len, key) == 0) {
            t->err = "PBKDF2_ERROR";
D
Dr. Stephen Henson 已提交
1720
            goto err;
R
Rich Salz 已提交
1721
        }
R
Rich Salz 已提交
1722
#ifndef OPENSSL_NO_SCRYPT
D
Dr. Stephen Henson 已提交
1723 1724 1725 1726
    } else if (pdata->pbe_type == PBE_TYPE_SCRYPT) {
        if (EVP_PBE_scrypt((const char *)pdata->pass, pdata->pass_len,
                           pdata->salt, pdata->salt_len,
                           pdata->N, pdata->r, pdata->p, pdata->maxmem,
R
Rich Salz 已提交
1727 1728
                           key, pdata->key_len) == 0) {
            t->err = "SCRYPT_ERROR";
D
Dr. Stephen Henson 已提交
1729
            goto err;
R
Rich Salz 已提交
1730
        }
R
Rich Salz 已提交
1731
#endif
D
Dr. Stephen Henson 已提交
1732 1733 1734 1735
    } else if (pdata->pbe_type == PBE_TYPE_PKCS12) {
        if (PKCS12_key_gen_uni(pdata->pass, pdata->pass_len,
                               pdata->salt, pdata->salt_len,
                               pdata->id, pdata->iter, pdata->key_len,
R
Rich Salz 已提交
1736 1737
                               key, pdata->md) == 0) {
            t->err = "PKCS12_ERROR";
D
Dr. Stephen Henson 已提交
1738
            goto err;
R
Rich Salz 已提交
1739
        }
D
Dr. Stephen Henson 已提交
1740
    }
R
Rich Salz 已提交
1741 1742
    if (!TEST_mem_eq(pdata->key, pdata->key_len, key, pdata->key_len)) {
        t->err = "KEY_MISMATCH";
D
Dr. Stephen Henson 已提交
1743
        goto err;
R
Rich Salz 已提交
1744 1745 1746
    }
    t->err = NULL;
err:
D
Dr. Stephen Henson 已提交
1747 1748
    OPENSSL_free(key);
    return 1;
D
Dr. Stephen Henson 已提交
1749 1750
}

R
Rich Salz 已提交
1751
static const EVP_TEST_METHOD pbe_test_method = {
D
Dr. Stephen Henson 已提交
1752 1753 1754 1755 1756 1757
    "PBE",
    pbe_test_init,
    pbe_test_cleanup,
    pbe_test_parse,
    pbe_test_run
};
E
Emilia Kasper 已提交
1758 1759 1760 1761 1762 1763 1764 1765 1766

/* Base64 tests */

typedef enum {
    BASE64_CANONICAL_ENCODING = 0,
    BASE64_VALID_ENCODING = 1,
    BASE64_INVALID_ENCODING = 2
} base64_encoding_type;

R
Rich Salz 已提交
1767
typedef struct encode_data_st {
E
Emilia Kasper 已提交
1768 1769 1770 1771 1772 1773 1774
    /* Input to encoding */
    unsigned char *input;
    size_t input_len;
    /* Expected output */
    unsigned char *output;
    size_t output_len;
    base64_encoding_type encoding;
R
Rich Salz 已提交
1775
} ENCODE_DATA;
E
Emilia Kasper 已提交
1776

R
Rich Salz 已提交
1777
static int encode_test_init(EVP_TEST *t, const char *encoding)
E
Emilia Kasper 已提交
1778
{
R
Rich Salz 已提交
1779
    ENCODE_DATA *edata = OPENSSL_zalloc(sizeof(*edata));
E
Emilia Kasper 已提交
1780 1781 1782 1783 1784 1785 1786

    if (strcmp(encoding, "canonical") == 0) {
        edata->encoding = BASE64_CANONICAL_ENCODING;
    } else if (strcmp(encoding, "valid") == 0) {
        edata->encoding = BASE64_VALID_ENCODING;
    } else if (strcmp(encoding, "invalid") == 0) {
        edata->encoding = BASE64_INVALID_ENCODING;
R
Rich Salz 已提交
1787
        t->expected_err = OPENSSL_strdup("DECODE_ERROR");
E
Emilia Kasper 已提交
1788 1789 1790
        if (t->expected_err == NULL)
            return 0;
    } else {
R
Rich Salz 已提交
1791 1792
        TEST_info("Bad encoding: %s. Should be one of "
                  "{canonical, valid, invalid}", encoding);
E
Emilia Kasper 已提交
1793 1794 1795 1796 1797 1798
        return 0;
    }
    t->data = edata;
    return 1;
}

R
Rich Salz 已提交
1799
static void encode_test_cleanup(EVP_TEST *t)
E
Emilia Kasper 已提交
1800
{
R
Rich Salz 已提交
1801 1802 1803 1804
    ENCODE_DATA *edata = t->data;

    OPENSSL_free(edata->input);
    OPENSSL_free(edata->output);
E
Emilia Kasper 已提交
1805 1806 1807
    memset(edata, 0, sizeof(*edata));
}

R
Rich Salz 已提交
1808
static int encode_test_parse(EVP_TEST *t,
E
Emilia Kasper 已提交
1809 1810
                             const char *keyword, const char *value)
{
R
Rich Salz 已提交
1811
    ENCODE_DATA *edata = t->data;
E
Emilia Kasper 已提交
1812 1813 1814 1815 1816 1817 1818
    if (strcmp(keyword, "Input") == 0)
        return test_bin(value, &edata->input, &edata->input_len);
    if (strcmp(keyword, "Output") == 0)
        return test_bin(value, &edata->output, &edata->output_len);
    return 0;
}

R
Rich Salz 已提交
1819
static int encode_test_run(EVP_TEST *t)
E
Emilia Kasper 已提交
1820
{
R
Rich Salz 已提交
1821
    ENCODE_DATA *edata = t->data;
E
Emilia Kasper 已提交
1822 1823
    unsigned char *encode_out = NULL, *decode_out = NULL;
    int output_len, chunk_len;
R
Rich Salz 已提交
1824
    EVP_ENCODE_CTX *decode_ctx;
1825

R
Rich Salz 已提交
1826 1827
    if (!TEST_ptr(decode_ctx = EVP_ENCODE_CTX_new())) {
        t->err = "INTERNAL_ERROR";
1828
        goto err;
R
Rich Salz 已提交
1829
    }
E
Emilia Kasper 已提交
1830 1831

    if (edata->encoding == BASE64_CANONICAL_ENCODING) {
R
Rich Salz 已提交
1832 1833 1834 1835 1836
        EVP_ENCODE_CTX *encode_ctx;

        if (!TEST_ptr(encode_ctx = EVP_ENCODE_CTX_new())
                || !TEST_ptr(encode_out =
                        OPENSSL_malloc(EVP_ENCODE_LENGTH(edata->input_len))))
E
Emilia Kasper 已提交
1837 1838
            goto err;

1839 1840
        EVP_EncodeInit(encode_ctx);
        EVP_EncodeUpdate(encode_ctx, encode_out, &chunk_len,
E
Emilia Kasper 已提交
1841 1842 1843
                         edata->input, edata->input_len);
        output_len = chunk_len;

1844
        EVP_EncodeFinal(encode_ctx, encode_out + chunk_len, &chunk_len);
E
Emilia Kasper 已提交
1845 1846
        output_len += chunk_len;

1847 1848
        EVP_ENCODE_CTX_free(encode_ctx);

R
Rich Salz 已提交
1849 1850 1851
        if (!TEST_mem_eq(edata->output, edata->output_len,
                         encode_out, output_len)) {
            t->err = "BAD_ENCODING";
E
Emilia Kasper 已提交
1852 1853 1854 1855
            goto err;
        }
    }

R
Rich Salz 已提交
1856 1857
    if (!TEST_ptr(decode_out =
                OPENSSL_malloc(EVP_DECODE_LENGTH(edata->output_len))))
E
Emilia Kasper 已提交
1858 1859
        goto err;

1860 1861
    EVP_DecodeInit(decode_ctx);
    if (EVP_DecodeUpdate(decode_ctx, decode_out, &chunk_len, edata->output,
E
Emilia Kasper 已提交
1862
                         edata->output_len) < 0) {
R
Rich Salz 已提交
1863
        t->err = "DECODE_ERROR";
E
Emilia Kasper 已提交
1864 1865 1866 1867
        goto err;
    }
    output_len = chunk_len;

1868
    if (EVP_DecodeFinal(decode_ctx, decode_out + chunk_len, &chunk_len) != 1) {
R
Rich Salz 已提交
1869
        t->err = "DECODE_ERROR";
E
Emilia Kasper 已提交
1870 1871 1872 1873
        goto err;
    }
    output_len += chunk_len;

R
Rich Salz 已提交
1874 1875 1876 1877
    if (edata->encoding != BASE64_INVALID_ENCODING
            && !TEST_mem_eq(edata->input, edata->input_len,
                            decode_out, output_len)) {
        t->err = "BAD_DECODING";
E
Emilia Kasper 已提交
1878 1879 1880
        goto err;
    }

R
Rich Salz 已提交
1881
    t->err = NULL;
E
Emilia Kasper 已提交
1882 1883 1884
 err:
    OPENSSL_free(encode_out);
    OPENSSL_free(decode_out);
1885
    EVP_ENCODE_CTX_free(decode_ctx);
E
Emilia Kasper 已提交
1886 1887 1888
    return 1;
}

R
Rich Salz 已提交
1889
static const EVP_TEST_METHOD encode_test_method = {
E
Emilia Kasper 已提交
1890 1891 1892 1893 1894 1895
    "Encoding",
    encode_test_init,
    encode_test_cleanup,
    encode_test_parse,
    encode_test_run,
};
1896

1897
/* KDF operations */
1898

R
Rich Salz 已提交
1899
typedef struct kdf_data_st {
1900 1901 1902 1903 1904
    /* Context for this operation */
    EVP_PKEY_CTX *ctx;
    /* Expected output */
    unsigned char *output;
    size_t output_len;
R
Rich Salz 已提交
1905
} KDF_DATA;
1906 1907 1908 1909 1910

/*
 * Perform public key operation setup: lookup key, allocated ctx and call
 * the appropriate initialisation function
 */
R
Rich Salz 已提交
1911
static int kdf_test_init(EVP_TEST *t, const char *name)
1912
{
R
Rich Salz 已提交
1913
    KDF_DATA *kdata;
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928

    kdata = OPENSSL_malloc(sizeof(*kdata));
    if (kdata == NULL)
        return 0;
    kdata->ctx = NULL;
    kdata->output = NULL;
    t->data = kdata;
    kdata->ctx = EVP_PKEY_CTX_new_id(OBJ_sn2nid(name), NULL);
    if (kdata->ctx == NULL)
        return 0;
    if (EVP_PKEY_derive_init(kdata->ctx) <= 0)
        return 0;
    return 1;
}

R
Rich Salz 已提交
1929
static void kdf_test_cleanup(EVP_TEST *t)
1930
{
R
Rich Salz 已提交
1931
    KDF_DATA *kdata = t->data;
1932 1933 1934 1935
    OPENSSL_free(kdata->output);
    EVP_PKEY_CTX_free(kdata->ctx);
}

R
Rich Salz 已提交
1936
static int kdf_test_parse(EVP_TEST *t,
1937 1938
                          const char *keyword, const char *value)
{
R
Rich Salz 已提交
1939 1940
    KDF_DATA *kdata = t->data;

1941 1942
    if (strcmp(keyword, "Output") == 0)
        return test_bin(value, &kdata->output, &kdata->output_len);
1943
    if (strncmp(keyword, "Ctrl", 4) == 0)
1944
        return pkey_test_ctrl(t, kdata->ctx, value);
1945 1946 1947
    return 0;
}

R
Rich Salz 已提交
1948
static int kdf_test_run(EVP_TEST *t)
1949
{
R
Rich Salz 已提交
1950
    KDF_DATA *kdata = t->data;
1951 1952
    unsigned char *out = NULL;
    size_t out_len = kdata->output_len;
R
Rich Salz 已提交
1953 1954 1955

    if (!TEST_ptr(out = OPENSSL_malloc(out_len))) {
        t->err = "INTERNAL_ERROR";
1956
        goto err;
R
Rich Salz 已提交
1957 1958 1959
    }
    if (EVP_PKEY_derive(kdata->ctx, out, &out_len) <= 0) {
        t->err = "KDF_DERIVE_ERROR";
1960
        goto err;
R
Rich Salz 已提交
1961 1962 1963
    }
    if (!TEST_mem_eq(kdata->output, kdata->output_len, out, out_len)) {
        t->err = "KDF_MISMATCH";
1964
        goto err;
R
Rich Salz 已提交
1965 1966 1967
    }
    t->err = NULL;

1968 1969 1970 1971 1972
 err:
    OPENSSL_free(out);
    return 1;
}

R
Rich Salz 已提交
1973
static const EVP_TEST_METHOD kdf_test_method = {
1974 1975 1976 1977 1978 1979
    "KDF",
    kdf_test_init,
    kdf_test_cleanup,
    kdf_test_parse,
    kdf_test_run
};
R
Rich Salz 已提交
1980

R
Rich Salz 已提交
1981
typedef struct keypair_test_data_st {
R
Rich Salz 已提交
1982 1983
    EVP_PKEY *privk;
    EVP_PKEY *pubk;
R
Rich Salz 已提交
1984
} KEYPAIR_TEST_DATA;
R
Rich Salz 已提交
1985

R
Rich Salz 已提交
1986
static int keypair_test_init(EVP_TEST *t, const char *pair)
R
Rich Salz 已提交
1987 1988 1989 1990
{
    int rv = 0;
    EVP_PKEY *pk = NULL, *pubk = NULL;
    char *pub, *priv = NULL;
R
Rich Salz 已提交
1991
    KEYPAIR_TEST_DATA *data;
R
Rich Salz 已提交
1992

R
Rich Salz 已提交
1993 1994 1995
    if (!TEST_ptr(priv = OPENSSL_strdup(pair))
            || !TEST_ptr(pub = strchr(priv, ':'))) {
        t->err = "PARSING_ERROR";
R
Rich Salz 已提交
1996 1997 1998 1999
        goto end;
    }
    *pub++ = 0; /* split priv and pub strings */

R
Rich Salz 已提交
2000 2001 2002
    if (!TEST_true(find_key(&pk, priv, private_keys))) {
        TEST_info("Cannot find private key: %s", priv);
        t->err = "MISSING_PRIVATE_KEY";
R
Rich Salz 已提交
2003 2004
        goto end;
    }
R
Rich Salz 已提交
2005 2006 2007
    if (!TEST_true(find_key(&pubk, pub, public_keys))) {
        TEST_info("Cannot find public key: %s", pub);
        t->err = "MISSING_PUBLIC_KEY";
R
Rich Salz 已提交
2008 2009 2010 2011 2012 2013 2014 2015 2016 2017
        goto end;
    }

    if (pk == NULL && pubk == NULL) {
        /* Both keys are listed but unsupported: skip this test */
        t->skip = 1;
        rv = 1;
        goto end;
    }

R
Rich Salz 已提交
2018
    if (!TEST_ptr(data = OPENSSL_malloc(sizeof(*data))))
R
Rich Salz 已提交
2019 2020 2021 2022 2023 2024
        goto end;

    data->privk = pk;
    data->pubk = pubk;
    t->data = data;
    rv = 1;
R
Rich Salz 已提交
2025
    t->err = NULL;
R
Rich Salz 已提交
2026 2027

end:
R
Rich Salz 已提交
2028
    OPENSSL_free(priv);
R
Rich Salz 已提交
2029 2030 2031
    return rv;
}

R
Rich Salz 已提交
2032
static void keypair_test_cleanup(EVP_TEST *t)
R
Rich Salz 已提交
2033
{
R
Rich Salz 已提交
2034
    OPENSSL_free(t->data);
R
Rich Salz 已提交
2035 2036 2037 2038 2039 2040
    t->data = NULL;
}

/* For test that do not accept any custom keyword:
 *      return 0 if called
 */
R
Rich Salz 已提交
2041
static int void_test_parse(EVP_TEST *t, const char *keyword, const char *value)
R
Rich Salz 已提交
2042 2043 2044 2045
{
    return 0;
}

R
Rich Salz 已提交
2046
static int keypair_test_run(EVP_TEST *t)
R
Rich Salz 已提交
2047 2048
{
    int rv = 0;
R
Rich Salz 已提交
2049
    const KEYPAIR_TEST_DATA *pair = t->data;
R
Rich Salz 已提交
2050 2051

    if (pair->privk == NULL || pair->pubk == NULL) {
R
Rich Salz 已提交
2052 2053
        /*
         * this can only happen if only one of the keys is not set
R
Rich Salz 已提交
2054 2055 2056
         * which means that one of them was unsupported while the
         * other isn't: hence a key type mismatch.
         */
R
Rich Salz 已提交
2057
        t->err = "KEYPAIR_TYPE_MISMATCH";
R
Rich Salz 已提交
2058 2059 2060 2061 2062 2063
        rv = 1;
        goto end;
    }

    if ((rv = EVP_PKEY_cmp(pair->privk, pair->pubk)) != 1 ) {
        if ( 0 == rv ) {
R
Rich Salz 已提交
2064
            t->err = "KEYPAIR_MISMATCH";
R
Rich Salz 已提交
2065
        } else if ( -1 == rv ) {
R
Rich Salz 已提交
2066
            t->err = "KEYPAIR_TYPE_MISMATCH";
R
Rich Salz 已提交
2067
        } else if ( -2 == rv ) {
R
Rich Salz 已提交
2068
            t->err = "UNSUPPORTED_KEY_COMPARISON";
R
Rich Salz 已提交
2069
        } else {
R
Rich Salz 已提交
2070
            TEST_error("Unexpected error in key comparison");
R
Rich Salz 已提交
2071 2072 2073 2074 2075 2076 2077 2078
            rv = 0;
            goto end;
        }
        rv = 1;
        goto end;
    }

    rv = 1;
R
Rich Salz 已提交
2079
    t->err = NULL;
R
Rich Salz 已提交
2080 2081 2082 2083 2084

end:
    return rv;
}

R
Rich Salz 已提交
2085
static const EVP_TEST_METHOD keypair_test_method = {
R
Rich Salz 已提交
2086 2087 2088 2089 2090 2091 2092
    "PrivPubKeyPair",
    keypair_test_init,
    keypair_test_cleanup,
    void_test_parse,
    keypair_test_run
};

R
Rich Salz 已提交
2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141
static int do_test_file(const char *testfile)
{
    BIO *in;
    char buf[10240];
    EVP_TEST t;

    if (!TEST_ptr(in = BIO_new_file(testfile, "rb")))
        return 0;
    memset(&t, 0, sizeof(t));
    t.start_line = -1;
    t.in = in;
    t.err = NULL;
    while (BIO_gets(in, buf, sizeof(buf))) {
        t.line++;
        if (!TEST_true(parse_test_line(&t, buf)))
            return 0;
    }
    /* Run any final test we have */
    if (!run_and_get_next(&t, NULL))
        return 0;

    TEST_info("Completed %d tests with %d errors and %d skipped",
              t.ntests, t.errors, t.nskip);
    free_key_list(public_keys);
    free_key_list(private_keys);
    BIO_free(t.key);
    BIO_free(in);
    return t.errors == 0;
}

static char * const *testfiles;

static int run_file_tests(int i)
{
    return do_test_file(testfiles[i]);
}

int test_main(int argc, char *argv[])
{
    if (argc < 2) {
        TEST_error("Usage: %s file...", argv[0]);
        return 0;
    }
    testfiles = &argv[1];

    ADD_ALL_TESTS(run_file_tests, argc - 1);

    return run_tests(argv[0]);
}