X-Git-Url: https://git.openssl.org/gitweb/?p=openssl.git;a=blobdiff_plain;f=test%2Fevp_test.c;h=667c945107cc7fefe722bb02f6470700c9ca0c0d;hp=8bfa5da59b03f0c9496b6a8a621a4e653bea86fb;hb=7d79d13a564d5c065318aa47f4cd511eece449e8;hpb=1f0fc03b8a21d139d4c5464106d5fd123c312469 diff --git a/test/evp_test.c b/test/evp_test.c index 8bfa5da59b..667c945107 100644 --- a/test/evp_test.c +++ b/test/evp_test.c @@ -1,5 +1,5 @@ /* - * Copyright 2015-2017 The OpenSSL Project Authors. All Rights Reserved. + * Copyright 2015-2018 The OpenSSL Project Authors. All Rights Reserved. * * Licensed under the OpenSSL license (the "License"). You may not use * this file except in compliance with the License. You can obtain a copy @@ -360,13 +360,18 @@ static int digest_test_run(EVP_TEST *t) { DIGEST_DATA *expected = t->data; EVP_MD_CTX *mctx; - unsigned char got[EVP_MAX_MD_SIZE]; + unsigned char *got = NULL; unsigned int got_len; t->err = "TEST_FAILURE"; if (!TEST_ptr(mctx = EVP_MD_CTX_new())) goto err; + got = OPENSSL_malloc(expected->output_len > EVP_MAX_MD_SIZE ? + expected->output_len : EVP_MAX_MD_SIZE); + if (!TEST_ptr(got)) + goto err; + if (!EVP_DigestInit_ex(mctx, expected->digest, NULL)) { t->err = "DIGESTINIT_ERROR"; goto err; @@ -376,9 +381,17 @@ static int digest_test_run(EVP_TEST *t) goto err; } - if (!EVP_DigestFinal(mctx, got, &got_len)) { - t->err = "DIGESTFINAL_ERROR"; - goto err; + if (EVP_MD_flags(expected->digest) & EVP_MD_FLAG_XOF) { + got_len = expected->output_len; + if (!EVP_DigestFinalXOF(mctx, got, got_len)) { + t->err = "DIGESTFINALXOF_ERROR"; + goto err; + } + } else { + if (!EVP_DigestFinal(mctx, got, &got_len)) { + t->err = "DIGESTFINAL_ERROR"; + goto err; + } } if (!TEST_int_eq(expected->output_len, got_len)) { t->err = "DIGEST_LENGTH_MISMATCH"; @@ -391,6 +404,7 @@ static int digest_test_run(EVP_TEST *t) t->err = NULL; err: + OPENSSL_free(got); EVP_MD_CTX_free(mctx); return 1; } @@ -897,31 +911,17 @@ static int mac_test_run(EVP_TEST *t) } #endif - if (!TEST_ptr(genctx = EVP_PKEY_CTX_new_id(expected->type, NULL))) { - t->err = "MAC_PKEY_CTX_ERROR"; - goto err; - } - - if (EVP_PKEY_keygen_init(genctx) <= 0) { - t->err = "MAC_KEYGEN_INIT_ERROR"; - goto err; - } - if (expected->type == EVP_PKEY_CMAC - && EVP_PKEY_CTX_ctrl_str(genctx, "cipher", expected->alg) <= 0) { - t->err = "MAC_ALGORITHM_SET_ERROR"; - goto err; - } - - if (EVP_PKEY_CTX_set_mac_key(genctx, expected->key, - expected->key_len) <= 0) { - t->err = "MAC_KEY_SET_ERROR"; + if (expected->type == EVP_PKEY_CMAC) + key = EVP_PKEY_new_CMAC_key(NULL, expected->key, expected->key_len, + EVP_get_cipherbyname(expected->alg)); + else + key = EVP_PKEY_new_raw_private_key(expected->type, NULL, expected->key, + expected->key_len); + if (key == NULL) { + t->err = "MAC_KEY_CREATE_ERROR"; goto err; } - if (EVP_PKEY_keygen(genctx, &key) <= 0) { - t->err = "MAC_KEY_GENERATE_ERROR"; - goto err; - } if (expected->type == EVP_PKEY_HMAC) { if (!TEST_ptr(md = EVP_get_digestbyname(expected->alg))) { t->err = "MAC_ALGORITHM_SET_ERROR"; @@ -1214,7 +1214,10 @@ static int pderive_test_run(EVP_TEST *t) unsigned char *got = NULL; size_t got_len; - got_len = expected->output_len; + if (EVP_PKEY_derive(expected->ctx, NULL, &got_len) <= 0) { + t->err = "DERIVE_ERROR"; + goto err; + } if (!TEST_ptr(got = OPENSSL_malloc(got_len))) { t->err = "DERIVE_ERROR"; goto err; @@ -1288,7 +1291,7 @@ static int parse_uint64(const char *value, uint64_t *pr) return -1; } *pr *= 10; - if (!TEST_true(isdigit(*p))) { + if (!TEST_true(isdigit((unsigned char)*p))) { TEST_error("Invalid character in string %s", value); return -1; } @@ -1623,10 +1626,21 @@ typedef struct kdf_data_st { static int kdf_test_init(EVP_TEST *t, const char *name) { KDF_DATA *kdata; + int kdf_nid = OBJ_sn2nid(name); + +#ifdef OPENSSL_NO_SCRYPT + if (strcmp(name, "scrypt") == 0) { + t->skip = 1; + return 1; + } +#endif + + if (kdf_nid == NID_undef) + kdf_nid = OBJ_ln2nid(name); if (!TEST_ptr(kdata = OPENSSL_zalloc(sizeof(*kdata)))) return 0; - kdata->ctx = EVP_PKEY_CTX_new_id(OBJ_sn2nid(name), NULL); + kdata->ctx = EVP_PKEY_CTX_new_id(kdf_nid, NULL); if (kdata->ctx == NULL) { OPENSSL_free(kdata); return 0; @@ -2364,7 +2378,7 @@ static void free_key_list(KEY_LIST *lst) /* * Is the key type an unsupported algorithm? */ -static int key_unsupported() +static int key_unsupported(void) { long err = ERR_peek_error(); @@ -2425,20 +2439,65 @@ top: if (strcmp(pp->key, "PrivateKey") == 0) { pkey = PEM_read_bio_PrivateKey(t->s.key, NULL, 0, NULL); if (pkey == NULL && !key_unsupported()) { + EVP_PKEY_free(pkey); TEST_info("Can't read private key %s", pp->value); TEST_openssl_errors(); return 0; } klist = &private_keys; - } - else if (strcmp(pp->key, "PublicKey") == 0) { + } else if (strcmp(pp->key, "PublicKey") == 0) { pkey = PEM_read_bio_PUBKEY(t->s.key, NULL, 0, NULL); if (pkey == NULL && !key_unsupported()) { + EVP_PKEY_free(pkey); TEST_info("Can't read public key %s", pp->value); TEST_openssl_errors(); return 0; } klist = &public_keys; + } else if (strcmp(pp->key, "PrivateKeyRaw") == 0 + || strcmp(pp->key, "PublicKeyRaw") == 0 ) { + char *strnid = NULL, *keydata = NULL; + unsigned char *keybin; + size_t keylen; + int nid; + + if (strcmp(pp->key, "PrivateKeyRaw") == 0) + klist = &private_keys; + else + klist = &public_keys; + + strnid = strchr(pp->value, ':'); + if (strnid != NULL) { + *strnid++ = '\0'; + keydata = strchr(strnid, ':'); + if (keydata != NULL) + *keydata++ = '\0'; + } + if (keydata == NULL) { + TEST_info("Failed to parse %s value", pp->key); + return 0; + } + + nid = OBJ_txt2nid(strnid); + if (nid == NID_undef) { + TEST_info("Uncrecognised algorithm NID"); + return 0; + } + if (!parse_bin(keydata, &keybin, &keylen)) { + TEST_info("Failed to create binary key"); + return 0; + } + if (klist == &private_keys) + pkey = EVP_PKEY_new_raw_private_key(nid, NULL, keybin, keylen); + else + pkey = EVP_PKEY_new_raw_public_key(nid, NULL, keybin, keylen); + if (pkey == NULL && !key_unsupported()) { + TEST_info("Can't read %s data", pp->key); + OPENSSL_free(keybin); + TEST_openssl_errors(); + return 0; + } + OPENSSL_free(keybin); } /* If we have a key add to list */ @@ -2450,6 +2509,17 @@ top: if (!TEST_ptr(key = OPENSSL_malloc(sizeof(*key)))) return 0; key->name = take_value(pp); + + /* Hack to detect SM2 keys */ + if(pkey != NULL && strstr(key->name, "SM2") != NULL) { +#ifdef OPENSSL_NO_SM2 + EVP_PKEY_free(pkey); + pkey = NULL; +#else + EVP_PKEY_set_alias_type(pkey, EVP_PKEY_SM2); +#endif + } + key->key = pkey; key->next = *klist; *klist = key; @@ -2510,16 +2580,15 @@ top: return 1; } -static char * const *testfiles; - static int run_file_tests(int i) { EVP_TEST *t; + const char *testfile = test_get_argument(i); int c; if (!TEST_ptr(t = OPENSSL_zalloc(sizeof(*t)))) return 0; - if (!test_start_file(&t->s, testfiles[i])) { + if (!test_start_file(&t->s, testfile)) { OPENSSL_free(t); return 0; } @@ -2544,15 +2613,15 @@ static int run_file_tests(int i) return c == 0; } -int test_main(int argc, char *argv[]) +int setup_tests(void) { - if (argc < 2) { - TEST_error("Usage: %s file...", argv[0]); + size_t n = test_get_argument_count(); + + if (n == 0) { + TEST_error("Usage: %s file...", test_get_program_name()); return 0; } - testfiles = &argv[1]; - - ADD_ALL_TESTS(run_file_tests, argc - 1); - return run_tests(argv[0]); + ADD_ALL_TESTS(run_file_tests, n); + return 1; }