fips.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /* ====================================================================
  2. * Copyright (c) 2003 The OpenSSL Project. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in
  13. * the documentation and/or other materials provided with the
  14. * distribution.
  15. *
  16. * 3. All advertising materials mentioning features or use of this
  17. * software must display the following acknowledgment:
  18. * "This product includes software developed by the OpenSSL Project
  19. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  20. *
  21. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  22. * endorse or promote products derived from this software without
  23. * prior written permission. For written permission, please contact
  24. * openssl-core@openssl.org.
  25. *
  26. * 5. Products derived from this software may not be called "OpenSSL"
  27. * nor may "OpenSSL" appear in their names without prior written
  28. * permission of the OpenSSL Project.
  29. *
  30. * 6. Redistributions of any form whatsoever must retain the following
  31. * acknowledgment:
  32. * "This product includes software developed by the OpenSSL Project
  33. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  34. *
  35. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  36. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  37. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  38. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  41. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  42. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  43. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  44. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  45. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  46. * OF THE POSSIBILITY OF SUCH DAMAGE.
  47. *
  48. */
  49. #include <openssl/rand.h>
  50. #include <openssl/fips_rand.h>
  51. #include <openssl/err.h>
  52. #include <openssl/bio.h>
  53. #include <openssl/hmac.h>
  54. #include <openssl/rsa.h>
  55. #include <string.h>
  56. #include <limits.h>
  57. #include "fips_locl.h"
  58. #ifdef OPENSSL_FIPS
  59. # include <openssl/fips.h>
  60. # ifndef PATH_MAX
  61. # define PATH_MAX 1024
  62. # endif
  63. static int fips_selftest_fail;
  64. static int fips_mode;
  65. static const void *fips_rand_check;
  66. static void fips_set_mode(int onoff)
  67. {
  68. int owning_thread = fips_is_owning_thread();
  69. if (fips_is_started()) {
  70. if (!owning_thread)
  71. fips_w_lock();
  72. fips_mode = onoff;
  73. if (!owning_thread)
  74. fips_w_unlock();
  75. }
  76. }
  77. static void fips_set_rand_check(const void *rand_check)
  78. {
  79. int owning_thread = fips_is_owning_thread();
  80. if (fips_is_started()) {
  81. if (!owning_thread)
  82. fips_w_lock();
  83. fips_rand_check = rand_check;
  84. if (!owning_thread)
  85. fips_w_unlock();
  86. }
  87. }
  88. int FIPS_mode(void)
  89. {
  90. int ret = 0;
  91. int owning_thread = fips_is_owning_thread();
  92. if (fips_is_started()) {
  93. if (!owning_thread)
  94. fips_r_lock();
  95. ret = fips_mode;
  96. if (!owning_thread)
  97. fips_r_unlock();
  98. }
  99. return ret;
  100. }
  101. const void *FIPS_rand_check(void)
  102. {
  103. const void *ret = 0;
  104. int owning_thread = fips_is_owning_thread();
  105. if (fips_is_started()) {
  106. if (!owning_thread)
  107. fips_r_lock();
  108. ret = fips_rand_check;
  109. if (!owning_thread)
  110. fips_r_unlock();
  111. }
  112. return ret;
  113. }
  114. int FIPS_selftest_failed(void)
  115. {
  116. int ret = 0;
  117. if (fips_is_started()) {
  118. int owning_thread = fips_is_owning_thread();
  119. if (!owning_thread)
  120. fips_r_lock();
  121. ret = fips_selftest_fail;
  122. if (!owning_thread)
  123. fips_r_unlock();
  124. }
  125. return ret;
  126. }
  127. /*
  128. * Selftest failure fatal exit routine. This will be called during *any*
  129. * cryptographic operation. It has the minimum overhead possible to avoid too
  130. * big a performance hit.
  131. */
  132. void FIPS_selftest_check(void)
  133. {
  134. if (fips_selftest_fail) {
  135. OpenSSLDie(__FILE__, __LINE__, "FATAL FIPS SELFTEST FAILURE");
  136. }
  137. }
  138. void fips_set_selftest_fail(void)
  139. {
  140. fips_selftest_fail = 1;
  141. }
  142. int FIPS_selftest()
  143. {
  144. return FIPS_selftest_sha1()
  145. && FIPS_selftest_hmac()
  146. && FIPS_selftest_aes()
  147. && FIPS_selftest_des()
  148. && FIPS_selftest_rsa()
  149. && FIPS_selftest_dsa();
  150. }
  151. extern const void *FIPS_text_start(), *FIPS_text_end();
  152. extern const unsigned char FIPS_rodata_start[], FIPS_rodata_end[];
  153. unsigned char FIPS_signature[20] = { 0 };
  154. static const char FIPS_hmac_key[] = "etaonrishdlcupfm";
  155. unsigned int FIPS_incore_fingerprint(unsigned char *sig, unsigned int len)
  156. {
  157. const unsigned char *p1 = FIPS_text_start();
  158. const unsigned char *p2 = FIPS_text_end();
  159. const unsigned char *p3 = FIPS_rodata_start;
  160. const unsigned char *p4 = FIPS_rodata_end;
  161. HMAC_CTX c;
  162. HMAC_CTX_init(&c);
  163. HMAC_Init(&c, FIPS_hmac_key, strlen(FIPS_hmac_key), EVP_sha1());
  164. /* detect overlapping regions */
  165. if (p1 <= p3 && p2 >= p3)
  166. p3 = p1, p4 = p2 > p4 ? p2 : p4, p1 = NULL, p2 = NULL;
  167. else if (p3 <= p1 && p4 >= p1)
  168. p3 = p3, p4 = p2 > p4 ? p2 : p4, p1 = NULL, p2 = NULL;
  169. if (p1)
  170. HMAC_Update(&c, p1, (size_t)p2 - (size_t)p1);
  171. if (FIPS_signature >= p3 && FIPS_signature < p4) {
  172. /* "punch" hole */
  173. HMAC_Update(&c, p3, (size_t)FIPS_signature - (size_t)p3);
  174. p3 = FIPS_signature + sizeof(FIPS_signature);
  175. if (p3 < p4)
  176. HMAC_Update(&c, p3, (size_t)p4 - (size_t)p3);
  177. } else
  178. HMAC_Update(&c, p3, (size_t)p4 - (size_t)p3);
  179. HMAC_Final(&c, sig, &len);
  180. HMAC_CTX_cleanup(&c);
  181. return len;
  182. }
  183. int FIPS_check_incore_fingerprint(void)
  184. {
  185. unsigned char sig[EVP_MAX_MD_SIZE];
  186. unsigned int len;
  187. # if defined(__sgi) && (defined(__mips) || defined(mips))
  188. extern int __dso_displacement[];
  189. # else
  190. extern int OPENSSL_NONPIC_relocated;
  191. # endif
  192. if (FIPS_text_start() == NULL) {
  193. FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,
  194. FIPS_R_UNSUPPORTED_PLATFORM);
  195. return 0;
  196. }
  197. len = FIPS_incore_fingerprint(sig, sizeof(sig));
  198. if (len != sizeof(FIPS_signature) ||
  199. memcmp(FIPS_signature, sig, sizeof(FIPS_signature))) {
  200. if (FIPS_signature >= FIPS_rodata_start
  201. && FIPS_signature < FIPS_rodata_end)
  202. FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,
  203. FIPS_R_FINGERPRINT_DOES_NOT_MATCH_SEGMENT_ALIASING);
  204. # if defined(__sgi) && (defined(__mips) || defined(mips))
  205. else if (__dso_displacement != NULL)
  206. # else
  207. else if (OPENSSL_NONPIC_relocated)
  208. # endif
  209. FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,
  210. FIPS_R_FINGERPRINT_DOES_NOT_MATCH_NONPIC_RELOCATED);
  211. else
  212. FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,
  213. FIPS_R_FINGERPRINT_DOES_NOT_MATCH);
  214. return 0;
  215. }
  216. return 1;
  217. }
  218. int FIPS_mode_set(int onoff)
  219. {
  220. int fips_set_owning_thread();
  221. int fips_clear_owning_thread();
  222. int ret = 0;
  223. fips_w_lock();
  224. fips_set_started();
  225. fips_set_owning_thread();
  226. if (onoff) {
  227. unsigned char buf[48];
  228. fips_selftest_fail = 0;
  229. /*
  230. * Don't go into FIPS mode twice, just so we can do automagic seeding
  231. */
  232. if (FIPS_mode()) {
  233. FIPSerr(FIPS_F_FIPS_MODE_SET, FIPS_R_FIPS_MODE_ALREADY_SET);
  234. fips_selftest_fail = 1;
  235. ret = 0;
  236. goto end;
  237. }
  238. # ifdef OPENSSL_IA32_SSE2
  239. if ((OPENSSL_ia32cap & (1 << 25 | 1 << 26)) != (1 << 25 | 1 << 26)) {
  240. FIPSerr(FIPS_F_FIPS_MODE_SET, FIPS_R_UNSUPPORTED_PLATFORM);
  241. fips_selftest_fail = 1;
  242. ret = 0;
  243. goto end;
  244. }
  245. # endif
  246. if (fips_signature_witness() != FIPS_signature) {
  247. FIPSerr(FIPS_F_FIPS_MODE_SET, FIPS_R_CONTRADICTING_EVIDENCE);
  248. fips_selftest_fail = 1;
  249. ret = 0;
  250. goto end;
  251. }
  252. if (!FIPS_check_incore_fingerprint()) {
  253. fips_selftest_fail = 1;
  254. ret = 0;
  255. goto end;
  256. }
  257. /* Perform RNG KAT before seeding */
  258. if (!FIPS_selftest_rng()) {
  259. fips_selftest_fail = 1;
  260. ret = 0;
  261. goto end;
  262. }
  263. /* automagically seed PRNG if not already seeded */
  264. if (!FIPS_rand_status()) {
  265. if (RAND_bytes(buf, sizeof buf) <= 0) {
  266. fips_selftest_fail = 1;
  267. ret = 0;
  268. goto end;
  269. }
  270. FIPS_rand_set_key(buf, 32);
  271. FIPS_rand_seed(buf + 32, 16);
  272. }
  273. /* now switch into FIPS mode */
  274. fips_set_rand_check(FIPS_rand_method());
  275. RAND_set_rand_method(FIPS_rand_method());
  276. if (FIPS_selftest())
  277. fips_set_mode(1);
  278. else {
  279. fips_selftest_fail = 1;
  280. ret = 0;
  281. goto end;
  282. }
  283. ret = 1;
  284. goto end;
  285. }
  286. fips_set_mode(0);
  287. fips_selftest_fail = 0;
  288. ret = 1;
  289. end:
  290. fips_clear_owning_thread();
  291. fips_w_unlock();
  292. return ret;
  293. }
  294. void fips_w_lock(void)
  295. {
  296. CRYPTO_w_lock(CRYPTO_LOCK_FIPS);
  297. }
  298. void fips_w_unlock(void)
  299. {
  300. CRYPTO_w_unlock(CRYPTO_LOCK_FIPS);
  301. }
  302. void fips_r_lock(void)
  303. {
  304. CRYPTO_r_lock(CRYPTO_LOCK_FIPS);
  305. }
  306. void fips_r_unlock(void)
  307. {
  308. CRYPTO_r_unlock(CRYPTO_LOCK_FIPS);
  309. }
  310. static int fips_started = 0;
  311. static unsigned long fips_thread = 0;
  312. void fips_set_started(void)
  313. {
  314. fips_started = 1;
  315. }
  316. int fips_is_started(void)
  317. {
  318. return fips_started;
  319. }
  320. int fips_is_owning_thread(void)
  321. {
  322. int ret = 0;
  323. if (fips_is_started()) {
  324. CRYPTO_r_lock(CRYPTO_LOCK_FIPS2);
  325. if (fips_thread != 0 && fips_thread == CRYPTO_thread_id())
  326. ret = 1;
  327. CRYPTO_r_unlock(CRYPTO_LOCK_FIPS2);
  328. }
  329. return ret;
  330. }
  331. int fips_set_owning_thread(void)
  332. {
  333. int ret = 0;
  334. if (fips_is_started()) {
  335. CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
  336. if (fips_thread == 0) {
  337. fips_thread = CRYPTO_thread_id();
  338. ret = 1;
  339. }
  340. CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
  341. }
  342. return ret;
  343. }
  344. int fips_clear_owning_thread(void)
  345. {
  346. int ret = 0;
  347. if (fips_is_started()) {
  348. CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
  349. if (fips_thread == CRYPTO_thread_id()) {
  350. fips_thread = 0;
  351. ret = 1;
  352. }
  353. CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
  354. }
  355. return ret;
  356. }
  357. unsigned char *fips_signature_witness(void)
  358. {
  359. extern unsigned char FIPS_signature[];
  360. return FIPS_signature;
  361. }
  362. /*
  363. * Generalized public key test routine. Signs and verifies the data supplied
  364. * in tbs using mesage digest md and setting option digest flags md_flags. If
  365. * the 'kat' parameter is not NULL it will additionally check the signature
  366. * matches it: a known answer test The string "fail_str" is used for
  367. * identification purposes in case of failure.
  368. */
  369. int fips_pkey_signature_test(EVP_PKEY *pkey,
  370. const unsigned char *tbs, int tbslen,
  371. const unsigned char *kat, unsigned int katlen,
  372. const EVP_MD *digest, unsigned int md_flags,
  373. const char *fail_str)
  374. {
  375. int ret = 0;
  376. unsigned char sigtmp[256], *sig = sigtmp;
  377. unsigned int siglen;
  378. EVP_MD_CTX mctx;
  379. EVP_MD_CTX_init(&mctx);
  380. if ((pkey->type == EVP_PKEY_RSA)
  381. && (RSA_size(pkey->pkey.rsa) > sizeof(sigtmp))) {
  382. sig = OPENSSL_malloc(RSA_size(pkey->pkey.rsa));
  383. if (!sig) {
  384. FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST, ERR_R_MALLOC_FAILURE);
  385. return 0;
  386. }
  387. }
  388. if (tbslen == -1)
  389. tbslen = strlen((char *)tbs);
  390. if (md_flags)
  391. M_EVP_MD_CTX_set_flags(&mctx, md_flags);
  392. if (!EVP_SignInit_ex(&mctx, digest, NULL))
  393. goto error;
  394. if (!EVP_SignUpdate(&mctx, tbs, tbslen))
  395. goto error;
  396. if (!EVP_SignFinal(&mctx, sig, &siglen, pkey))
  397. goto error;
  398. if (kat && ((siglen != katlen) || memcmp(kat, sig, katlen)))
  399. goto error;
  400. if (!EVP_VerifyInit_ex(&mctx, digest, NULL))
  401. goto error;
  402. if (!EVP_VerifyUpdate(&mctx, tbs, tbslen))
  403. goto error;
  404. ret = EVP_VerifyFinal(&mctx, sig, siglen, pkey);
  405. error:
  406. if (sig != sigtmp)
  407. OPENSSL_free(sig);
  408. EVP_MD_CTX_cleanup(&mctx);
  409. if (ret != 1) {
  410. FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST, FIPS_R_TEST_FAILURE);
  411. if (fail_str)
  412. ERR_add_error_data(2, "Type=", fail_str);
  413. return 0;
  414. }
  415. return 1;
  416. }
  417. /*
  418. * Generalized symmetric cipher test routine. Encrypt data, verify result
  419. * against known answer, decrypt and compare with original plaintext.
  420. */
  421. int fips_cipher_test(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  422. const unsigned char *key,
  423. const unsigned char *iv,
  424. const unsigned char *plaintext,
  425. const unsigned char *ciphertext, int len)
  426. {
  427. unsigned char pltmp[FIPS_MAX_CIPHER_TEST_SIZE];
  428. unsigned char citmp[FIPS_MAX_CIPHER_TEST_SIZE];
  429. OPENSSL_assert(len <= FIPS_MAX_CIPHER_TEST_SIZE);
  430. if (EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, 1) <= 0)
  431. return 0;
  432. EVP_Cipher(ctx, citmp, plaintext, len);
  433. if (memcmp(citmp, ciphertext, len))
  434. return 0;
  435. if (EVP_CipherInit_ex(ctx, cipher, NULL, key, iv, 0) <= 0)
  436. return 0;
  437. EVP_Cipher(ctx, pltmp, citmp, len);
  438. if (memcmp(pltmp, plaintext, len))
  439. return 0;
  440. return 1;
  441. }
  442. # if 0
  443. /*
  444. * The purpose of this is to ensure the error code exists and the function
  445. * name is to keep the error checking script quiet
  446. */
  447. void hash_final(void)
  448. {
  449. FIPSerr(FIPS_F_HASH_FINAL, FIPS_R_NON_FIPS_METHOD);
  450. }
  451. # endif
  452. #endif