evp_fetch_prov_test.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /*
  2. * Copyright 2019-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  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
  8. */
  9. /*
  10. * SHA256 low level APIs are deprecated for public use, but still ok for
  11. * internal use. Note, that due to symbols not being exported, only the
  12. * #defines can be accessed. In this case SHA256_CBLOCK.
  13. */
  14. #include "internal/deprecated.h"
  15. #include <string.h>
  16. #include <openssl/sha.h>
  17. #include <openssl/evp.h>
  18. #include <openssl/provider.h>
  19. #include "testutil.h"
  20. static char *config_file = NULL;
  21. static char *alg = "digest";
  22. static int use_default_ctx = 0;
  23. static char *fetch_property = NULL;
  24. static int expected_fetch_result = 1;
  25. typedef enum OPTION_choice {
  26. OPT_ERR = -1,
  27. OPT_EOF = 0,
  28. OPT_ALG_FETCH_TYPE,
  29. OPT_FETCH_PROPERTY,
  30. OPT_FETCH_FAILURE,
  31. OPT_USE_DEFAULTCTX,
  32. OPT_CONFIG_FILE,
  33. OPT_TEST_ENUM
  34. } OPTION_CHOICE;
  35. const OPTIONS *test_get_options(void)
  36. {
  37. static const OPTIONS test_options[] = {
  38. OPT_TEST_OPTIONS_WITH_EXTRA_USAGE("[provname...]\n"),
  39. { "config", OPT_CONFIG_FILE, '<', "The configuration file to use for the libctx" },
  40. { "type", OPT_ALG_FETCH_TYPE, 's', "The fetch type to test" },
  41. { "property", OPT_FETCH_PROPERTY, 's', "The fetch property e.g. provider=fips" },
  42. { "fetchfail", OPT_FETCH_FAILURE, '-', "fetch is expected to fail" },
  43. { "defaultctx", OPT_USE_DEFAULTCTX, '-',
  44. "Use the default context if this is set" },
  45. { OPT_HELP_STR, 1, '-',
  46. "file\tProvider names to explicitly load\n" },
  47. { NULL }
  48. };
  49. return test_options;
  50. }
  51. static int calculate_digest(const EVP_MD *md, const char *msg, size_t len,
  52. const unsigned char *exptd)
  53. {
  54. unsigned char out[SHA256_DIGEST_LENGTH];
  55. EVP_MD_CTX *ctx;
  56. int ret = 0;
  57. if (!TEST_ptr(ctx = EVP_MD_CTX_new())
  58. || !TEST_true(EVP_DigestInit_ex(ctx, md, NULL))
  59. || !TEST_true(EVP_DigestUpdate(ctx, msg, len))
  60. || !TEST_true(EVP_DigestFinal_ex(ctx, out, NULL))
  61. || !TEST_mem_eq(out, SHA256_DIGEST_LENGTH, exptd,
  62. SHA256_DIGEST_LENGTH)
  63. || !TEST_true(md == EVP_MD_CTX_md(ctx)))
  64. goto err;
  65. ret = 1;
  66. err:
  67. EVP_MD_CTX_free(ctx);
  68. return ret;
  69. }
  70. static int load_providers(OPENSSL_CTX **libctx, OSSL_PROVIDER *prov[])
  71. {
  72. OPENSSL_CTX *ctx = NULL;
  73. int ret = 0;
  74. size_t i;
  75. ctx = OPENSSL_CTX_new();
  76. if (!TEST_ptr(ctx))
  77. goto err;
  78. if (!TEST_true(OPENSSL_CTX_load_config(ctx, config_file)))
  79. goto err;
  80. if (test_get_argument_count() > 2)
  81. goto err;
  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]))
  86. goto err;
  87. }
  88. ret = 1;
  89. *libctx = ctx;
  90. err:
  91. if (ret == 0)
  92. OPENSSL_CTX_free(ctx);
  93. return ret;
  94. }
  95. /*
  96. * Test EVP_MD_fetch()
  97. */
  98. static int test_EVP_MD_fetch(void)
  99. {
  100. OPENSSL_CTX *ctx = NULL;
  101. EVP_MD *md = NULL;
  102. OSSL_PROVIDER *prov[2] = {NULL, NULL};
  103. int ret = 0;
  104. const char testmsg[] = "Hello world";
  105. const unsigned char exptd[] = {
  106. 0x27, 0x51, 0x8b, 0xa9, 0x68, 0x30, 0x11, 0xf6, 0xb3, 0x96, 0x07, 0x2c,
  107. 0x05, 0xf6, 0x65, 0x6d, 0x04, 0xf5, 0xfb, 0xc3, 0x78, 0x7c, 0xf9, 0x24,
  108. 0x90, 0xec, 0x60, 0x6e, 0x50, 0x92, 0xe3, 0x26
  109. };
  110. if (use_default_ctx == 0 && !load_providers(&ctx, prov))
  111. goto err;
  112. /* Implicit fetching of the MD should produce the expected result */
  113. if (!TEST_true(calculate_digest(EVP_sha256(), testmsg, sizeof(testmsg),
  114. exptd))
  115. || !TEST_int_eq(EVP_MD_size(EVP_sha256()), SHA256_DIGEST_LENGTH)
  116. || !TEST_int_eq(EVP_MD_block_size(EVP_sha256()), SHA256_CBLOCK))
  117. goto err;
  118. /* Fetch the digest from a provider using properties. */
  119. md = EVP_MD_fetch(ctx, "SHA256", fetch_property);
  120. if (expected_fetch_result != 0) {
  121. if (!TEST_ptr(md)
  122. || !TEST_int_eq(EVP_MD_nid(md), NID_sha256)
  123. || !TEST_true(calculate_digest(md, testmsg, sizeof(testmsg), exptd))
  124. || !TEST_int_eq(EVP_MD_size(md), SHA256_DIGEST_LENGTH)
  125. || !TEST_int_eq(EVP_MD_block_size(md), SHA256_CBLOCK))
  126. goto err;
  127. /* Also test EVP_MD_up_ref() while we're doing this */
  128. if (!TEST_true(EVP_MD_up_ref(md)))
  129. goto err;
  130. /* Ref count should now be 2. Release first one here */
  131. EVP_MD_meth_free(md);
  132. } else {
  133. if (!TEST_ptr_null(md))
  134. goto err;
  135. }
  136. ret = 1;
  137. err:
  138. EVP_MD_meth_free(md);
  139. OSSL_PROVIDER_unload(prov[0]);
  140. OSSL_PROVIDER_unload(prov[1]);
  141. /* Not normally needed, but we would like to test that
  142. * OPENSSL_thread_stop_ex() behaves as expected.
  143. */
  144. if (ctx != NULL) {
  145. OPENSSL_thread_stop_ex(ctx);
  146. OPENSSL_CTX_free(ctx);
  147. }
  148. return ret;
  149. }
  150. static int encrypt_decrypt(const EVP_CIPHER *cipher, const unsigned char *msg,
  151. size_t len)
  152. {
  153. int ret = 0, ctlen, ptlen;
  154. EVP_CIPHER_CTX *ctx = NULL;
  155. unsigned char key[128 / 8];
  156. unsigned char ct[64], pt[64];
  157. memset(key, 0, sizeof(key));
  158. if (!TEST_ptr(ctx = EVP_CIPHER_CTX_new())
  159. || !TEST_true(EVP_CipherInit_ex(ctx, cipher, NULL, key, NULL, 1))
  160. || !TEST_true(EVP_CipherUpdate(ctx, ct, &ctlen, msg, len))
  161. || !TEST_true(EVP_CipherFinal_ex(ctx, ct, &ctlen))
  162. || !TEST_true(EVP_CipherInit_ex(ctx, cipher, NULL, key, NULL, 0))
  163. || !TEST_true(EVP_CipherUpdate(ctx, pt, &ptlen, ct, ctlen))
  164. || !TEST_true(EVP_CipherFinal_ex(ctx, pt, &ptlen))
  165. || !TEST_mem_eq(pt, ptlen, msg, len))
  166. goto err;
  167. ret = 1;
  168. err:
  169. EVP_CIPHER_CTX_free(ctx);
  170. return ret;
  171. }
  172. /*
  173. * Test EVP_CIPHER_fetch()
  174. */
  175. static int test_EVP_CIPHER_fetch(void)
  176. {
  177. OPENSSL_CTX *ctx = NULL;
  178. EVP_CIPHER *cipher = NULL;
  179. OSSL_PROVIDER *prov[2] = {NULL, NULL};
  180. int ret = 0;
  181. const unsigned char testmsg[] = "Hello world";
  182. if (use_default_ctx == 0 && !load_providers(&ctx, prov))
  183. goto err;
  184. /* Implicit fetching of the cipher should produce the expected result */
  185. if (!TEST_true(encrypt_decrypt(EVP_aes_128_cbc(), testmsg, sizeof(testmsg))))
  186. goto err;
  187. /* Fetch the cipher from a provider using properties. */
  188. cipher = EVP_CIPHER_fetch(ctx, "AES-128-CBC", fetch_property);
  189. if (expected_fetch_result != 0) {
  190. if (!TEST_ptr(cipher)
  191. || !TEST_true(encrypt_decrypt(cipher, testmsg, sizeof(testmsg)))) {
  192. if (!TEST_true(EVP_CIPHER_up_ref(cipher)))
  193. goto err;
  194. /* Ref count should now be 2. Release first one here */
  195. EVP_CIPHER_meth_free(cipher);
  196. }
  197. } else {
  198. if (!TEST_ptr_null(cipher))
  199. goto err;
  200. }
  201. ret = 1;
  202. err:
  203. EVP_CIPHER_meth_free(cipher);
  204. OSSL_PROVIDER_unload(prov[0]);
  205. OSSL_PROVIDER_unload(prov[1]);
  206. OPENSSL_CTX_free(ctx);
  207. return ret;
  208. }
  209. int setup_tests(void)
  210. {
  211. OPTION_CHOICE o;
  212. while ((o = opt_next()) != OPT_EOF) {
  213. switch (o) {
  214. case OPT_CONFIG_FILE:
  215. config_file = opt_arg();
  216. break;
  217. case OPT_ALG_FETCH_TYPE:
  218. alg = opt_arg();
  219. break;
  220. case OPT_FETCH_PROPERTY:
  221. fetch_property = opt_arg();
  222. break;
  223. case OPT_FETCH_FAILURE:
  224. expected_fetch_result = 0;
  225. break;
  226. case OPT_USE_DEFAULTCTX:
  227. use_default_ctx = 1;
  228. break;
  229. case OPT_TEST_CASES:
  230. break;
  231. default:
  232. case OPT_ERR:
  233. return 0;
  234. }
  235. }
  236. if (strcmp(alg, "digest") == 0)
  237. ADD_TEST(test_EVP_MD_fetch);
  238. else
  239. ADD_TEST(test_EVP_CIPHER_fetch);
  240. return 1;
  241. }