fips.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /* ====================================================================
  2. * Copyright (c) 2011 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. #define OPENSSL_FIPSAPI
  50. #include <openssl/crypto.h>
  51. #include <openssl/rand.h>
  52. #include <openssl/fips_rand.h>
  53. #include <openssl/err.h>
  54. #include <openssl/bio.h>
  55. #include <openssl/hmac.h>
  56. #include <openssl/rsa.h>
  57. #include <openssl/dsa.h>
  58. #include <openssl/ecdsa.h>
  59. #include <string.h>
  60. #include <limits.h>
  61. #include "fips_locl.h"
  62. #ifdef OPENSSL_FIPS
  63. #include <openssl/fips.h>
  64. #ifndef PATH_MAX
  65. #define PATH_MAX 1024
  66. #endif
  67. static int fips_selftest_fail = 0;
  68. static int fips_mode = 0;
  69. static int fips_started = 0;
  70. static int fips_is_owning_thread(void);
  71. static int fips_set_owning_thread(void);
  72. static int fips_clear_owning_thread(void);
  73. static unsigned char *fips_signature_witness(void);
  74. #define fips_w_lock() CRYPTO_w_lock(CRYPTO_LOCK_FIPS)
  75. #define fips_w_unlock() CRYPTO_w_unlock(CRYPTO_LOCK_FIPS)
  76. #define fips_r_lock() CRYPTO_r_lock(CRYPTO_LOCK_FIPS)
  77. #define fips_r_unlock() CRYPTO_r_unlock(CRYPTO_LOCK_FIPS)
  78. static void fips_set_mode(int onoff)
  79. {
  80. int owning_thread = fips_is_owning_thread();
  81. if (fips_started)
  82. {
  83. if (!owning_thread) fips_w_lock();
  84. fips_mode = onoff;
  85. if (!owning_thread) 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_started)
  93. {
  94. if (!owning_thread) fips_r_lock();
  95. ret = fips_mode;
  96. if (!owning_thread) fips_r_unlock();
  97. }
  98. return ret;
  99. }
  100. int FIPS_selftest_failed(void)
  101. {
  102. int ret = 0;
  103. if (fips_started)
  104. {
  105. int owning_thread = fips_is_owning_thread();
  106. if (!owning_thread) fips_r_lock();
  107. ret = fips_selftest_fail;
  108. if (!owning_thread) fips_r_unlock();
  109. }
  110. return ret;
  111. }
  112. /* Selftest failure fatal exit routine. This will be called
  113. * during *any* cryptographic operation. It has the minimum
  114. * overhead possible to avoid too big a performance hit.
  115. */
  116. void FIPS_selftest_check(void)
  117. {
  118. if (fips_selftest_fail)
  119. {
  120. OpenSSLDie(__FILE__,__LINE__, "FATAL FIPS SELFTEST FAILURE");
  121. }
  122. }
  123. void fips_set_selftest_fail(void)
  124. {
  125. fips_selftest_fail = 1;
  126. }
  127. extern const void *FIPS_text_start(), *FIPS_text_end();
  128. extern const unsigned char FIPS_rodata_start[], FIPS_rodata_end[];
  129. unsigned char FIPS_signature [20] = { 0 };
  130. static const char FIPS_hmac_key[]="etaonrishdlcupfm";
  131. unsigned int FIPS_incore_fingerprint(unsigned char *sig,unsigned int len)
  132. {
  133. const unsigned char *p1 = FIPS_text_start();
  134. const unsigned char *p2 = FIPS_text_end();
  135. const unsigned char *p3 = FIPS_rodata_start;
  136. const unsigned char *p4 = FIPS_rodata_end;
  137. HMAC_CTX c;
  138. HMAC_CTX_init(&c);
  139. HMAC_Init(&c,FIPS_hmac_key,strlen(FIPS_hmac_key),EVP_sha1());
  140. /* detect overlapping regions */
  141. if (p1<=p3 && p2>=p3)
  142. p3=p1, p4=p2>p4?p2:p4, p1=NULL, p2=NULL;
  143. else if (p3<=p1 && p4>=p1)
  144. p3=p3, p4=p2>p4?p2:p4, p1=NULL, p2=NULL;
  145. if (p1)
  146. HMAC_Update(&c,p1,(size_t)p2-(size_t)p1);
  147. if (FIPS_signature>=p3 && FIPS_signature<p4)
  148. {
  149. /* "punch" hole */
  150. HMAC_Update(&c,p3,(size_t)FIPS_signature-(size_t)p3);
  151. p3 = FIPS_signature+sizeof(FIPS_signature);
  152. if (p3<p4)
  153. HMAC_Update(&c,p3,(size_t)p4-(size_t)p3);
  154. }
  155. else
  156. HMAC_Update(&c,p3,(size_t)p4-(size_t)p3);
  157. if (!fips_post_corrupt(FIPS_TEST_INTEGRITY, 0, NULL))
  158. HMAC_Update(&c, (unsigned char *)FIPS_hmac_key, 1);
  159. HMAC_Final(&c,sig,&len);
  160. HMAC_CTX_cleanup(&c);
  161. return len;
  162. }
  163. int FIPS_check_incore_fingerprint(void)
  164. {
  165. unsigned char sig[EVP_MAX_MD_SIZE];
  166. unsigned int len;
  167. int rv = 0;
  168. #if defined(__sgi) && (defined(__mips) || defined(mips))
  169. extern int __dso_displacement[];
  170. #else
  171. extern int OPENSSL_NONPIC_relocated;
  172. #endif
  173. if (!fips_post_started(FIPS_TEST_INTEGRITY, 0, NULL))
  174. return 1;
  175. if (FIPS_text_start()==NULL)
  176. {
  177. FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_UNSUPPORTED_PLATFORM);
  178. goto err;
  179. }
  180. len=FIPS_incore_fingerprint(sig,sizeof(sig));
  181. if (len!=sizeof(FIPS_signature) ||
  182. memcmp(FIPS_signature,sig,sizeof(FIPS_signature)))
  183. {
  184. if (FIPS_signature>=FIPS_rodata_start && FIPS_signature<FIPS_rodata_end)
  185. FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH_SEGMENT_ALIASING);
  186. #if defined(__sgi) && (defined(__mips) || defined(mips))
  187. else if (__dso_displacement!=NULL)
  188. #else
  189. else if (OPENSSL_NONPIC_relocated)
  190. #endif
  191. FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH_NONPIC_RELOCATED);
  192. else
  193. FIPSerr(FIPS_F_FIPS_CHECK_INCORE_FINGERPRINT,FIPS_R_FINGERPRINT_DOES_NOT_MATCH);
  194. #ifdef OPENSSL_FIPS_DEBUGGER
  195. rv = 1;
  196. #endif
  197. goto err;
  198. }
  199. rv = 1;
  200. err:
  201. if (rv == 0)
  202. fips_post_failed(FIPS_TEST_INTEGRITY, 0, NULL);
  203. else
  204. if (!fips_post_success(FIPS_TEST_INTEGRITY, 0, NULL))
  205. return 0;
  206. return rv;
  207. }
  208. int FIPS_mode_set(int onoff)
  209. {
  210. int fips_set_owning_thread();
  211. int fips_clear_owning_thread();
  212. int ret = 0;
  213. fips_w_lock();
  214. fips_started = 1;
  215. fips_set_owning_thread();
  216. if(onoff)
  217. {
  218. fips_selftest_fail = 0;
  219. /* Don't go into FIPS mode twice, just so we can do automagic
  220. seeding */
  221. if(FIPS_mode())
  222. {
  223. FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_FIPS_MODE_ALREADY_SET);
  224. fips_selftest_fail = 1;
  225. ret = 0;
  226. goto end;
  227. }
  228. #ifdef OPENSSL_IA32_SSE2
  229. if ((OPENSSL_ia32cap & (1<<25|1<<26)) != (1<<25|1<<26))
  230. {
  231. FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_UNSUPPORTED_PLATFORM);
  232. fips_selftest_fail = 1;
  233. ret = 0;
  234. goto end;
  235. }
  236. #endif
  237. if(fips_signature_witness() != FIPS_signature)
  238. {
  239. FIPSerr(FIPS_F_FIPS_MODE_SET,FIPS_R_CONTRADICTING_EVIDENCE);
  240. fips_selftest_fail = 1;
  241. ret = 0;
  242. goto end;
  243. }
  244. if(FIPS_selftest())
  245. fips_set_mode(1);
  246. else
  247. {
  248. fips_selftest_fail = 1;
  249. ret = 0;
  250. goto end;
  251. }
  252. ret = 1;
  253. goto end;
  254. }
  255. fips_set_mode(0);
  256. fips_selftest_fail = 0;
  257. ret = 1;
  258. end:
  259. fips_clear_owning_thread();
  260. fips_w_unlock();
  261. return ret;
  262. }
  263. static CRYPTO_THREADID fips_thread;
  264. static int fips_thread_set = 0;
  265. static int fips_is_owning_thread(void)
  266. {
  267. int ret = 0;
  268. if (fips_started)
  269. {
  270. CRYPTO_r_lock(CRYPTO_LOCK_FIPS2);
  271. if (fips_thread_set)
  272. {
  273. CRYPTO_THREADID cur;
  274. CRYPTO_THREADID_current(&cur);
  275. if (!CRYPTO_THREADID_cmp(&cur, &fips_thread))
  276. ret = 1;
  277. }
  278. CRYPTO_r_unlock(CRYPTO_LOCK_FIPS2);
  279. }
  280. return ret;
  281. }
  282. int fips_set_owning_thread(void)
  283. {
  284. int ret = 0;
  285. if (fips_started)
  286. {
  287. CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
  288. if (!fips_thread_set)
  289. {
  290. CRYPTO_THREADID_current(&fips_thread);
  291. ret = 1;
  292. fips_thread_set = 1;
  293. }
  294. CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
  295. }
  296. return ret;
  297. }
  298. int fips_clear_owning_thread(void)
  299. {
  300. int ret = 0;
  301. if (fips_started)
  302. {
  303. CRYPTO_w_lock(CRYPTO_LOCK_FIPS2);
  304. if (fips_thread_set)
  305. {
  306. CRYPTO_THREADID cur;
  307. CRYPTO_THREADID_current(&cur);
  308. if (!CRYPTO_THREADID_cmp(&cur, &fips_thread))
  309. fips_thread_set = 0;
  310. }
  311. CRYPTO_w_unlock(CRYPTO_LOCK_FIPS2);
  312. }
  313. return ret;
  314. }
  315. unsigned char *fips_signature_witness(void)
  316. {
  317. extern unsigned char FIPS_signature[];
  318. return FIPS_signature;
  319. }
  320. #if 0
  321. /* The purpose of this is to ensure the error code exists and the function
  322. * name is to keep the error checking script quiet
  323. */
  324. void hash_final(void)
  325. {
  326. FIPSerr(FIPS_F_HASH_FINAL,FIPS_R_NON_FIPS_METHOD);
  327. }
  328. #endif
  329. #endif