self_test.c 14 KB

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