2
0

self_test.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. /*
  2. * Copyright 2019-2023 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. #include <string.h>
  10. #include <openssl/evp.h>
  11. #include <openssl/params.h>
  12. #include <openssl/crypto.h>
  13. #include "internal/cryptlib.h"
  14. #include <openssl/fipskey.h>
  15. #include <openssl/err.h>
  16. #include <openssl/proverr.h>
  17. #include <openssl/rand.h>
  18. #include "internal/e_os.h"
  19. #include "internal/tsan_assist.h"
  20. #include "prov/providercommon.h"
  21. #include "crypto/rand.h"
  22. /*
  23. * We're cheating here. Normally we don't allow RUN_ONCE usage inside the FIPS
  24. * module because all such initialisation should be associated with an
  25. * individual OSSL_LIB_CTX. That doesn't work with the self test though because
  26. * it should be run once regardless of the number of OSSL_LIB_CTXs we have.
  27. */
  28. #define ALLOW_RUN_ONCE_IN_FIPS
  29. #include "internal/thread_once.h"
  30. #include "self_test.h"
  31. #define FIPS_STATE_INIT 0
  32. #define FIPS_STATE_SELFTEST 1
  33. #define FIPS_STATE_RUNNING 2
  34. #define FIPS_STATE_ERROR 3
  35. /*
  36. * The number of times the module will report it is in the error state
  37. * before going quiet.
  38. */
  39. #define FIPS_ERROR_REPORTING_RATE_LIMIT 10
  40. /* The size of a temp buffer used to read in data */
  41. #define INTEGRITY_BUF_SIZE (4096)
  42. #define MAX_MD_SIZE 64
  43. #define MAC_NAME "HMAC"
  44. #define DIGEST_NAME "SHA256"
  45. static int FIPS_conditional_error_check = 1;
  46. static CRYPTO_RWLOCK *self_test_lock = NULL;
  47. static unsigned char fixed_key[32] = { FIPS_KEY_ELEMENTS };
  48. static CRYPTO_ONCE fips_self_test_init = CRYPTO_ONCE_STATIC_INIT;
  49. DEFINE_RUN_ONCE_STATIC(do_fips_self_test_init)
  50. {
  51. /*
  52. * These locks get freed in platform specific ways that may occur after we
  53. * do mem leak checking. If we don't know how to free it for a particular
  54. * platform then we just leak it deliberately.
  55. */
  56. self_test_lock = CRYPTO_THREAD_lock_new();
  57. return self_test_lock != NULL;
  58. }
  59. /*
  60. * Declarations for the DEP entry/exit points.
  61. * Ones not required or incorrect need to be undefined or redefined respectively.
  62. */
  63. #define DEP_INITIAL_STATE FIPS_STATE_INIT
  64. #define DEP_INIT_ATTRIBUTE static
  65. #define DEP_FINI_ATTRIBUTE static
  66. static void init(void);
  67. static void cleanup(void);
  68. /*
  69. * This is the Default Entry Point (DEP) code.
  70. * See FIPS 140-2 IG 9.10
  71. */
  72. #if defined(_WIN32) || defined(__CYGWIN__)
  73. # ifdef __CYGWIN__
  74. /* pick DLL_[PROCESS|THREAD]_[ATTACH|DETACH] definitions */
  75. # include <windows.h>
  76. /*
  77. * this has side-effect of _WIN32 getting defined, which otherwise is
  78. * mutually exclusive with __CYGWIN__...
  79. */
  80. # endif
  81. BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved);
  82. BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
  83. {
  84. switch (fdwReason) {
  85. case DLL_PROCESS_ATTACH:
  86. init();
  87. break;
  88. case DLL_PROCESS_DETACH:
  89. cleanup();
  90. break;
  91. default:
  92. break;
  93. }
  94. return TRUE;
  95. }
  96. #elif defined(__GNUC__) && !defined(_AIX)
  97. # undef DEP_INIT_ATTRIBUTE
  98. # undef DEP_FINI_ATTRIBUTE
  99. # define DEP_INIT_ATTRIBUTE static __attribute__((constructor))
  100. # define DEP_FINI_ATTRIBUTE static __attribute__((destructor))
  101. #elif defined(__sun)
  102. # pragma init(init)
  103. # pragma fini(cleanup)
  104. #elif defined(_AIX) && !defined(__GNUC__)
  105. void _init(void);
  106. void _cleanup(void);
  107. # pragma init(_init)
  108. # pragma fini(_cleanup)
  109. void _init(void)
  110. {
  111. init();
  112. }
  113. void _cleanup(void)
  114. {
  115. cleanup();
  116. }
  117. #elif defined(__hpux)
  118. # pragma init "init"
  119. # pragma fini "cleanup"
  120. #elif defined(__TANDEM)
  121. /* Method automatically called by the NonStop OS when the DLL loads */
  122. void __INIT__init(void) {
  123. init();
  124. }
  125. /* Method automatically called by the NonStop OS prior to unloading the DLL */
  126. void __TERM__cleanup(void) {
  127. cleanup();
  128. }
  129. #else
  130. /*
  131. * This build does not support any kind of DEP.
  132. * We force the self-tests to run as part of the FIPS provider initialisation
  133. * rather than being triggered by the DEP.
  134. */
  135. # undef DEP_INIT_ATTRIBUTE
  136. # undef DEP_FINI_ATTRIBUTE
  137. # undef DEP_INITIAL_STATE
  138. # define DEP_INITIAL_STATE FIPS_STATE_SELFTEST
  139. #endif
  140. static TSAN_QUALIFIER int FIPS_state = DEP_INITIAL_STATE;
  141. #if defined(DEP_INIT_ATTRIBUTE)
  142. DEP_INIT_ATTRIBUTE void init(void)
  143. {
  144. tsan_store(&FIPS_state, FIPS_STATE_SELFTEST);
  145. }
  146. #endif
  147. #if defined(DEP_FINI_ATTRIBUTE)
  148. DEP_FINI_ATTRIBUTE void cleanup(void)
  149. {
  150. CRYPTO_THREAD_lock_free(self_test_lock);
  151. }
  152. #endif
  153. /*
  154. * We need an explicit HMAC-SHA-256 KAT even though it is also
  155. * checked as part of the KDF KATs. Refer IG 10.3.
  156. */
  157. static const unsigned char hmac_kat_pt[] = {
  158. 0xdd, 0x0c, 0x30, 0x33, 0x35, 0xf9, 0xe4, 0x2e,
  159. 0xc2, 0xef, 0xcc, 0xbf, 0x07, 0x95, 0xee, 0xa2
  160. };
  161. static const unsigned char hmac_kat_key[] = {
  162. 0xf4, 0x55, 0x66, 0x50, 0xac, 0x31, 0xd3, 0x54,
  163. 0x61, 0x61, 0x0b, 0xac, 0x4e, 0xd8, 0x1b, 0x1a,
  164. 0x18, 0x1b, 0x2d, 0x8a, 0x43, 0xea, 0x28, 0x54,
  165. 0xcb, 0xae, 0x22, 0xca, 0x74, 0x56, 0x08, 0x13
  166. };
  167. static const unsigned char hmac_kat_digest[] = {
  168. 0xf5, 0xf5, 0xe5, 0xf2, 0x66, 0x49, 0xe2, 0x40,
  169. 0xfc, 0x9e, 0x85, 0x7f, 0x2b, 0x9a, 0xbe, 0x28,
  170. 0x20, 0x12, 0x00, 0x92, 0x82, 0x21, 0x3e, 0x51,
  171. 0x44, 0x5d, 0xe3, 0x31, 0x04, 0x01, 0x72, 0x6b
  172. };
  173. static int integrity_self_test(OSSL_SELF_TEST *ev, OSSL_LIB_CTX *libctx)
  174. {
  175. int ok = 0;
  176. unsigned char out[EVP_MAX_MD_SIZE];
  177. size_t out_len = 0;
  178. OSSL_PARAM params[2];
  179. EVP_MAC *mac = EVP_MAC_fetch(libctx, MAC_NAME, NULL);
  180. EVP_MAC_CTX *ctx = EVP_MAC_CTX_new(mac);
  181. OSSL_SELF_TEST_onbegin(ev, OSSL_SELF_TEST_TYPE_KAT_INTEGRITY,
  182. OSSL_SELF_TEST_DESC_INTEGRITY_HMAC);
  183. params[0] = OSSL_PARAM_construct_utf8_string("digest", DIGEST_NAME, 0);
  184. params[1] = OSSL_PARAM_construct_end();
  185. if (ctx == NULL
  186. || mac == NULL
  187. || !EVP_MAC_init(ctx, hmac_kat_key, sizeof(hmac_kat_key), params)
  188. || !EVP_MAC_update(ctx, hmac_kat_pt, sizeof(hmac_kat_pt))
  189. || !EVP_MAC_final(ctx, out, &out_len, MAX_MD_SIZE))
  190. goto err;
  191. /* Optional corruption */
  192. OSSL_SELF_TEST_oncorrupt_byte(ev, out);
  193. if (out_len != sizeof(hmac_kat_digest)
  194. || memcmp(out, hmac_kat_digest, out_len) != 0)
  195. goto err;
  196. ok = 1;
  197. err:
  198. OSSL_SELF_TEST_onend(ev, ok);
  199. EVP_MAC_free(mac);
  200. EVP_MAC_CTX_free(ctx);
  201. return ok;
  202. }
  203. /*
  204. * Calculate the HMAC SHA256 of data read using a BIO and read_cb, and verify
  205. * the result matches the expected value.
  206. * Return 1 if verified, or 0 if it fails.
  207. */
  208. static int verify_integrity(OSSL_CORE_BIO *bio, OSSL_FUNC_BIO_read_ex_fn read_ex_cb,
  209. unsigned char *expected, size_t expected_len,
  210. OSSL_LIB_CTX *libctx, OSSL_SELF_TEST *ev,
  211. const char *event_type)
  212. {
  213. int ret = 0, status;
  214. unsigned char out[MAX_MD_SIZE];
  215. unsigned char buf[INTEGRITY_BUF_SIZE];
  216. size_t bytes_read = 0, out_len = 0;
  217. EVP_MAC *mac = NULL;
  218. EVP_MAC_CTX *ctx = NULL;
  219. OSSL_PARAM params[2], *p = params;
  220. if (!integrity_self_test(ev, libctx))
  221. goto err;
  222. OSSL_SELF_TEST_onbegin(ev, event_type, OSSL_SELF_TEST_DESC_INTEGRITY_HMAC);
  223. mac = EVP_MAC_fetch(libctx, MAC_NAME, NULL);
  224. if (mac == NULL)
  225. goto err;
  226. ctx = EVP_MAC_CTX_new(mac);
  227. if (ctx == NULL)
  228. goto err;
  229. *p++ = OSSL_PARAM_construct_utf8_string("digest", DIGEST_NAME, 0);
  230. *p = OSSL_PARAM_construct_end();
  231. if (!EVP_MAC_init(ctx, fixed_key, sizeof(fixed_key), params))
  232. goto err;
  233. while (1) {
  234. status = read_ex_cb(bio, buf, sizeof(buf), &bytes_read);
  235. if (status != 1)
  236. break;
  237. if (!EVP_MAC_update(ctx, buf, bytes_read))
  238. goto err;
  239. }
  240. if (!EVP_MAC_final(ctx, out, &out_len, sizeof(out)))
  241. goto err;
  242. OSSL_SELF_TEST_oncorrupt_byte(ev, out);
  243. if (expected_len != out_len
  244. || memcmp(expected, out, out_len) != 0)
  245. goto err;
  246. ret = 1;
  247. err:
  248. OSSL_SELF_TEST_onend(ev, ret);
  249. EVP_MAC_CTX_free(ctx);
  250. EVP_MAC_free(mac);
  251. return ret;
  252. }
  253. static void set_fips_state(int state)
  254. {
  255. tsan_store(&FIPS_state, state);
  256. }
  257. /* This API is triggered either on loading of the FIPS module or on demand */
  258. int SELF_TEST_post(SELF_TEST_POST_PARAMS *st, int on_demand_test)
  259. {
  260. int ok = 0;
  261. int kats_already_passed = 0;
  262. long checksum_len;
  263. OSSL_CORE_BIO *bio_module = NULL, *bio_indicator = NULL;
  264. unsigned char *module_checksum = NULL;
  265. unsigned char *indicator_checksum = NULL;
  266. int loclstate;
  267. OSSL_SELF_TEST *ev = NULL;
  268. EVP_RAND *testrand = NULL;
  269. EVP_RAND_CTX *rng;
  270. if (!RUN_ONCE(&fips_self_test_init, do_fips_self_test_init))
  271. return 0;
  272. loclstate = tsan_load(&FIPS_state);
  273. if (loclstate == FIPS_STATE_RUNNING) {
  274. if (!on_demand_test)
  275. return 1;
  276. } else if (loclstate != FIPS_STATE_SELFTEST) {
  277. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_STATE);
  278. return 0;
  279. }
  280. if (!CRYPTO_THREAD_write_lock(self_test_lock))
  281. return 0;
  282. loclstate = tsan_load(&FIPS_state);
  283. if (loclstate == FIPS_STATE_RUNNING) {
  284. if (!on_demand_test) {
  285. CRYPTO_THREAD_unlock(self_test_lock);
  286. return 1;
  287. }
  288. set_fips_state(FIPS_STATE_SELFTEST);
  289. } else if (loclstate != FIPS_STATE_SELFTEST) {
  290. CRYPTO_THREAD_unlock(self_test_lock);
  291. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_STATE);
  292. return 0;
  293. }
  294. if (st == NULL
  295. || st->module_checksum_data == NULL) {
  296. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONFIG_DATA);
  297. goto end;
  298. }
  299. ev = OSSL_SELF_TEST_new(st->cb, st->cb_arg);
  300. if (ev == NULL)
  301. goto end;
  302. module_checksum = OPENSSL_hexstr2buf(st->module_checksum_data,
  303. &checksum_len);
  304. if (module_checksum == NULL) {
  305. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONFIG_DATA);
  306. goto end;
  307. }
  308. bio_module = (*st->bio_new_file_cb)(st->module_filename, "rb");
  309. /* Always check the integrity of the fips module */
  310. if (bio_module == NULL
  311. || !verify_integrity(bio_module, st->bio_read_ex_cb,
  312. module_checksum, checksum_len, st->libctx,
  313. ev, OSSL_SELF_TEST_TYPE_MODULE_INTEGRITY)) {
  314. ERR_raise(ERR_LIB_PROV, PROV_R_MODULE_INTEGRITY_FAILURE);
  315. goto end;
  316. }
  317. /* This will be NULL during installation - so the self test KATS will run */
  318. if (st->indicator_data != NULL) {
  319. /*
  320. * If the kats have already passed indicator is set - then check the
  321. * integrity of the indicator.
  322. */
  323. if (st->indicator_checksum_data == NULL) {
  324. ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_CONFIG_DATA);
  325. goto end;
  326. }
  327. indicator_checksum = OPENSSL_hexstr2buf(st->indicator_checksum_data,
  328. &checksum_len);
  329. if (indicator_checksum == NULL) {
  330. ERR_raise(ERR_LIB_PROV, PROV_R_INVALID_CONFIG_DATA);
  331. goto end;
  332. }
  333. bio_indicator =
  334. (*st->bio_new_buffer_cb)(st->indicator_data,
  335. strlen(st->indicator_data));
  336. if (bio_indicator == NULL
  337. || !verify_integrity(bio_indicator, st->bio_read_ex_cb,
  338. indicator_checksum, checksum_len,
  339. st->libctx, ev,
  340. OSSL_SELF_TEST_TYPE_INSTALL_INTEGRITY)) {
  341. ERR_raise(ERR_LIB_PROV, PROV_R_INDICATOR_INTEGRITY_FAILURE);
  342. goto end;
  343. } else {
  344. kats_already_passed = 1;
  345. }
  346. }
  347. /*
  348. * Only runs the KAT's during installation OR on_demand().
  349. * NOTE: If the installation option 'self_test_onload' is chosen then this
  350. * path will always be run, since kats_already_passed will always be 0.
  351. */
  352. if (on_demand_test || kats_already_passed == 0) {
  353. if (!SELF_TEST_kats(ev, st->libctx)) {
  354. ERR_raise(ERR_LIB_PROV, PROV_R_SELF_TEST_KAT_FAILURE);
  355. goto end;
  356. }
  357. }
  358. /* Verify that the RNG has been restored properly */
  359. rng = ossl_rand_get0_private_noncreating(st->libctx);
  360. if (rng != NULL)
  361. if ((testrand = EVP_RAND_fetch(st->libctx, "TEST-RAND", NULL)) == NULL
  362. || strcmp(EVP_RAND_get0_name(EVP_RAND_CTX_get0_rand(rng)),
  363. EVP_RAND_get0_name(testrand)) == 0) {
  364. ERR_raise(ERR_LIB_PROV, PROV_R_SELF_TEST_KAT_FAILURE);
  365. goto end;
  366. }
  367. ok = 1;
  368. end:
  369. EVP_RAND_free(testrand);
  370. OSSL_SELF_TEST_free(ev);
  371. OPENSSL_free(module_checksum);
  372. OPENSSL_free(indicator_checksum);
  373. if (st != NULL) {
  374. (*st->bio_free_cb)(bio_indicator);
  375. (*st->bio_free_cb)(bio_module);
  376. }
  377. if (ok)
  378. set_fips_state(FIPS_STATE_RUNNING);
  379. else
  380. ossl_set_error_state(OSSL_SELF_TEST_TYPE_NONE);
  381. CRYPTO_THREAD_unlock(self_test_lock);
  382. return ok;
  383. }
  384. void SELF_TEST_disable_conditional_error_state(void)
  385. {
  386. FIPS_conditional_error_check = 0;
  387. }
  388. void ossl_set_error_state(const char *type)
  389. {
  390. int cond_test = (type != NULL && strcmp(type, OSSL_SELF_TEST_TYPE_PCT) == 0);
  391. if (!cond_test || (FIPS_conditional_error_check == 1)) {
  392. set_fips_state(FIPS_STATE_ERROR);
  393. ERR_raise(ERR_LIB_PROV, PROV_R_FIPS_MODULE_ENTERING_ERROR_STATE);
  394. } else {
  395. ERR_raise(ERR_LIB_PROV, PROV_R_FIPS_MODULE_CONDITIONAL_ERROR);
  396. }
  397. }
  398. int ossl_prov_is_running(void)
  399. {
  400. int res, loclstate;
  401. static TSAN_QUALIFIER unsigned int rate_limit = 0;
  402. loclstate = tsan_load(&FIPS_state);
  403. res = loclstate == FIPS_STATE_RUNNING || loclstate == FIPS_STATE_SELFTEST;
  404. if (loclstate == FIPS_STATE_ERROR)
  405. if (tsan_counter(&rate_limit) < FIPS_ERROR_REPORTING_RATE_LIMIT)
  406. ERR_raise(ERR_LIB_PROV, PROV_R_FIPS_MODULE_IN_ERROR_STATE);
  407. return res;
  408. }