2 * Copyright 2019 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include <openssl/sha.h>
12 #include <openssl/evp.h>
13 #include <openssl/provider.h>
16 static char *alg = "digest";
17 static int use_default_ctx = 0;
18 static char *fetch_property = NULL;
19 static int expected_fetch_result = 1;
21 typedef enum OPTION_choice {
31 const OPTIONS *test_get_options(void)
33 static const OPTIONS test_options[] = {
34 OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[provname...]\n"),
35 { "type", OPT_ALG_FETCH_TYPE, 's', "The fetch type to test" },
36 { "property", OPT_FETCH_PROPERTY, 's', "The fetch property e.g. fips=yes" },
37 { "fetchfail", OPT_FETCH_FAILURE, '-', "fetch is expected to fail" },
38 { "defaultctx", OPT_USE_DEFAULTCTX, '-',
39 "Use the default context if this is set" },
40 { OPT_HELP_STR, 1, '-',
41 "file\tProvider names to explicitly load\n" },
47 static int calculate_digest(const EVP_MD *md, const char *msg, size_t len,
48 const unsigned char *exptd)
50 unsigned char out[SHA256_DIGEST_LENGTH];
54 if (!TEST_ptr(ctx = EVP_MD_CTX_new())
55 || !TEST_true(EVP_DigestInit_ex(ctx, md, NULL))
56 || !TEST_true(EVP_DigestUpdate(ctx, msg, len))
57 || !TEST_true(EVP_DigestFinal_ex(ctx, out, NULL))
58 || !TEST_mem_eq(out, SHA256_DIGEST_LENGTH, exptd,
60 || !TEST_true(md == EVP_MD_CTX_md(ctx)))
69 static int load_providers(OPENSSL_CTX **libctx, OSSL_PROVIDER *prov[])
75 ctx = OPENSSL_CTX_new();
79 if (test_get_argument_count() > 2)
82 for (i = 0; i < test_get_argument_count(); ++i) {
83 char *provname = test_get_argument(i);
84 prov[i] = OSSL_PROVIDER_load(ctx, provname);
85 if (!TEST_ptr(prov[i]))
97 static int test_EVP_MD_fetch(void)
99 OPENSSL_CTX *ctx = NULL;
101 OSSL_PROVIDER *prov[2] = {NULL, NULL};
103 const char testmsg[] = "Hello world";
104 const unsigned char exptd[] = {
105 0x27, 0x51, 0x8b, 0xa9, 0x68, 0x30, 0x11, 0xf6, 0xb3, 0x96, 0x07, 0x2c,
106 0x05, 0xf6, 0x65, 0x6d, 0x04, 0xf5, 0xfb, 0xc3, 0x78, 0x7c, 0xf9, 0x24,
107 0x90, 0xec, 0x60, 0x6e, 0x50, 0x92, 0xe3, 0x26
110 if (use_default_ctx == 0 && !load_providers(&ctx, prov))
113 /* Implicit fetching of the MD should produce the expected result */
114 if (!TEST_true(calculate_digest(EVP_sha256(), testmsg, sizeof(testmsg),
116 || !TEST_int_eq(EVP_MD_size(EVP_sha256()), SHA256_DIGEST_LENGTH)
117 || !TEST_int_eq(EVP_MD_block_size(EVP_sha256()), SHA256_CBLOCK))
120 /* Fetch the digest from a provider using properties. */
121 md = EVP_MD_fetch(ctx, "SHA256", fetch_property);
122 if (expected_fetch_result != 0) {
124 || !TEST_int_eq(EVP_MD_nid(md), NID_sha256)
125 || !TEST_true(calculate_digest(md, testmsg, sizeof(testmsg), exptd))
126 || !TEST_int_eq(EVP_MD_size(md), SHA256_DIGEST_LENGTH)
127 || !TEST_int_eq(EVP_MD_block_size(md), SHA256_CBLOCK))
130 /* Also test EVP_MD_up_ref() while we're doing this */
131 if (!TEST_true(EVP_MD_up_ref(md)))
133 /* Ref count should now be 2. Release first one here */
134 EVP_MD_meth_free(md);
136 if (!TEST_ptr_null(md))
142 EVP_MD_meth_free(md);
143 OSSL_PROVIDER_unload(prov[0]);
144 OSSL_PROVIDER_unload(prov[1]);
145 /* Not normally needed, but we would like to test that
146 * OPENSSL_thread_stop_ex() behaves as expected.
149 OPENSSL_thread_stop_ex(ctx);
150 OPENSSL_CTX_free(ctx);
155 static int encrypt_decrypt(const EVP_CIPHER *cipher, const unsigned char *msg,
158 int ret = 0, ctlen, ptlen;
159 EVP_CIPHER_CTX *ctx = NULL;
160 unsigned char key[128 / 8];
161 unsigned char ct[64], pt[64];
163 memset(key, 0, sizeof(key));
164 if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
165 || !TEST_true(EVP_CipherInit_ex(ctx, cipher, NULL, key, NULL, 1))
166 || !TEST_true(EVP_CipherUpdate(ctx, ct, &ctlen, msg, len))
167 || !TEST_true(EVP_CipherFinal_ex(ctx, ct, &ctlen))
168 || !TEST_true(EVP_CipherInit_ex(ctx, cipher, NULL, key, NULL, 0))
169 || !TEST_true(EVP_CipherUpdate(ctx, pt, &ptlen, ct, ctlen))
170 || !TEST_true(EVP_CipherFinal_ex(ctx, pt, &ptlen))
171 || !TEST_mem_eq(pt, ptlen, msg, len))
176 EVP_CIPHER_CTX_free(ctx);
181 * Test EVP_CIPHER_fetch()
183 static int test_EVP_CIPHER_fetch(void)
185 OPENSSL_CTX *ctx = NULL;
186 EVP_CIPHER *cipher = NULL;
187 OSSL_PROVIDER *prov[2] = {NULL, NULL};
189 const unsigned char testmsg[] = "Hello world";
191 if (use_default_ctx == 0 && !load_providers(&ctx, prov))
194 /* Implicit fetching of the cipher should produce the expected result */
195 if (!TEST_true(encrypt_decrypt(EVP_aes_128_cbc(), testmsg, sizeof(testmsg))))
198 /* Fetch the cipher from a provider using properties. */
199 cipher = EVP_CIPHER_fetch(ctx, "AES-128-CBC", fetch_property);
200 if (expected_fetch_result != 0) {
201 if (!TEST_ptr(cipher)
202 || !TEST_true(encrypt_decrypt(cipher, testmsg, sizeof(testmsg)))) {
203 if (!TEST_true(EVP_CIPHER_up_ref(cipher)))
205 /* Ref count should now be 2. Release first one here */
206 EVP_CIPHER_meth_free(cipher);
209 if (!TEST_ptr_null(cipher))
214 EVP_CIPHER_meth_free(cipher);
215 OSSL_PROVIDER_unload(prov[0]);
216 OSSL_PROVIDER_unload(prov[1]);
217 OPENSSL_CTX_free(ctx);
221 int setup_tests(void)
225 while ((o = opt_next()) != OPT_EOF) {
227 case OPT_ALG_FETCH_TYPE:
230 case OPT_FETCH_PROPERTY:
231 fetch_property = opt_arg();
233 case OPT_FETCH_FAILURE:
234 expected_fetch_result = 0;
236 case OPT_USE_DEFAULTCTX:
246 if (strcmp(alg, "digest") == 0)
247 ADD_TEST(test_EVP_MD_fetch);
249 ADD_TEST(test_EVP_CIPHER_fetch);