fips_post.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. #ifdef OPENSSL_FIPS
  62. /* Power on self test (POST) support functions */
  63. #include <openssl/fips.h>
  64. #include "fips_locl.h"
  65. /* POST notification callback */
  66. int (*fips_post_cb)(int op, int id, int subid, void *ex);
  67. void FIPS_post_set_callback(
  68. int (*post_cb)(int op, int id, int subid, void *ex))
  69. {
  70. fips_post_cb = post_cb;
  71. }
  72. /* POST status: i.e. status of all tests */
  73. #define FIPS_POST_STATUS_NOT_STARTED 0
  74. #define FIPS_POST_STATUS_OK 1
  75. #define FIPS_POST_STATUS_RUNNING 2
  76. #define FIPS_POST_STATUS_FAILED -1
  77. static int post_status = 0;
  78. /* Set to 1 if any test failed */
  79. static int post_failure = 0;
  80. /* All tests started */
  81. int fips_post_begin(void)
  82. {
  83. post_failure = 0;
  84. post_status = FIPS_POST_STATUS_NOT_STARTED;
  85. if (fips_post_cb)
  86. if (!fips_post_cb(FIPS_POST_BEGIN, 0, 0, NULL))
  87. return 0;
  88. post_status = FIPS_POST_STATUS_RUNNING;
  89. return 1;
  90. }
  91. void fips_post_end(void)
  92. {
  93. if (post_failure)
  94. {
  95. post_status = FIPS_POST_STATUS_FAILED;
  96. if(fips_post_cb)
  97. fips_post_cb(FIPS_POST_END, 0, 0, NULL);
  98. }
  99. else
  100. {
  101. post_status = FIPS_POST_STATUS_OK;
  102. if (fips_post_cb)
  103. fips_post_cb(FIPS_POST_END, 1, 0, NULL);
  104. }
  105. }
  106. /* A self test started */
  107. int fips_post_started(int id, int subid, void *ex)
  108. {
  109. if (fips_post_cb)
  110. return fips_post_cb(FIPS_POST_STARTED, id, subid, ex);
  111. return 1;
  112. }
  113. /* A self test passed successfully */
  114. int fips_post_success(int id, int subid, void *ex)
  115. {
  116. if (fips_post_cb)
  117. return fips_post_cb(FIPS_POST_SUCCESS, id, subid, ex);
  118. return 1;
  119. }
  120. /* A self test failed */
  121. int fips_post_failed(int id, int subid, void *ex)
  122. {
  123. post_failure = 1;
  124. if (fips_post_cb)
  125. return fips_post_cb(FIPS_POST_FAIL, id, subid, ex);
  126. return 1;
  127. }
  128. /* Indicate if a self test failure should be induced */
  129. int fips_post_corrupt(int id, int subid, void *ex)
  130. {
  131. if (fips_post_cb)
  132. return fips_post_cb(FIPS_POST_CORRUPT, id, subid, ex);
  133. return 1;
  134. }
  135. /* Note: if selftests running return status OK so their operation is
  136. * not interrupted. This will only happen while selftests are actually
  137. * running so will not interfere with normal operation.
  138. */
  139. int fips_post_status(void)
  140. {
  141. return post_status > 0 ? 1 : 0;
  142. }
  143. /* Run all selftests */
  144. int FIPS_selftest(void)
  145. {
  146. int rv = 1;
  147. fips_post_begin();
  148. if(!FIPS_check_incore_fingerprint())
  149. rv = 0;
  150. if (!FIPS_selftest_drbg())
  151. rv = 0;
  152. if (!FIPS_selftest_x931())
  153. rv = 0;
  154. if (!FIPS_selftest_sha1())
  155. rv = 0;
  156. if (!FIPS_selftest_hmac())
  157. rv = 0;
  158. if (!FIPS_selftest_cmac())
  159. rv = 0;
  160. if (!FIPS_selftest_aes())
  161. rv = 0;
  162. if (!FIPS_selftest_aes_ccm())
  163. rv = 0;
  164. if (!FIPS_selftest_aes_gcm())
  165. rv = 0;
  166. if (!FIPS_selftest_aes_xts())
  167. rv = 0;
  168. if (!FIPS_selftest_des())
  169. rv = 0;
  170. if (!FIPS_selftest_rsa())
  171. rv = 0;
  172. if (!FIPS_selftest_ecdsa())
  173. rv = 0;
  174. if (!FIPS_selftest_dsa())
  175. rv = 0;
  176. if (!FIPS_selftest_ecdh())
  177. rv = 0;
  178. fips_post_end();
  179. return rv;
  180. }
  181. /* Generalized public key test routine. Signs and verifies the data
  182. * supplied in tbs using mesage digest md and setting RSA padding mode
  183. * pad_mode. If the 'kat' parameter is not NULL it will
  184. * additionally check the signature matches it: a known answer test
  185. * The string "fail_str" is used for identification purposes in case
  186. * of failure. If "pkey" is NULL just perform a message digest check.
  187. */
  188. int fips_pkey_signature_test(int id, EVP_PKEY *pkey,
  189. const unsigned char *tbs, size_t tbslen,
  190. const unsigned char *kat, size_t katlen,
  191. const EVP_MD *digest, int pad_mode,
  192. const char *fail_str)
  193. {
  194. int subid;
  195. void *ex = NULL;
  196. int ret = 0;
  197. unsigned char *sig = NULL;
  198. unsigned int siglen;
  199. __fips_constseg
  200. static const unsigned char str1[]="12345678901234567890";
  201. DSA_SIG *dsig = NULL;
  202. ECDSA_SIG *esig = NULL;
  203. EVP_MD_CTX mctx;
  204. FIPS_md_ctx_init(&mctx);
  205. if (tbs == NULL)
  206. tbs = str1;
  207. if (tbslen == 0)
  208. tbslen = strlen((char *)tbs);
  209. if (digest == NULL)
  210. digest = EVP_sha256();
  211. subid = M_EVP_MD_type(digest);
  212. if (!fips_post_started(id, subid, pkey))
  213. return 1;
  214. if (!pkey || pkey->type == EVP_PKEY_RSA)
  215. {
  216. size_t sigsize;
  217. if (!pkey)
  218. sigsize = EVP_MAX_MD_SIZE;
  219. else
  220. sigsize = RSA_size(pkey->pkey.rsa);
  221. sig = OPENSSL_malloc(sigsize);
  222. if (!sig)
  223. {
  224. FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST,ERR_R_MALLOC_FAILURE);
  225. goto error;
  226. }
  227. }
  228. if (!FIPS_digestinit(&mctx, digest))
  229. goto error;
  230. if (!FIPS_digestupdate(&mctx, tbs, tbslen))
  231. goto error;
  232. if (!fips_post_corrupt(id, subid, pkey))
  233. {
  234. if (!FIPS_digestupdate(&mctx, tbs, 1))
  235. goto error;
  236. }
  237. if (pkey == NULL)
  238. {
  239. if (!FIPS_digestfinal(&mctx, sig, &siglen))
  240. goto error;
  241. }
  242. else if (pkey->type == EVP_PKEY_RSA)
  243. {
  244. if (!FIPS_rsa_sign_ctx(pkey->pkey.rsa, &mctx,
  245. pad_mode, 0, NULL, sig, &siglen))
  246. goto error;
  247. }
  248. else if (pkey->type == EVP_PKEY_DSA)
  249. {
  250. dsig = FIPS_dsa_sign_ctx(pkey->pkey.dsa, &mctx);
  251. if (!dsig)
  252. goto error;
  253. }
  254. else if (pkey->type == EVP_PKEY_EC)
  255. {
  256. esig = FIPS_ecdsa_sign_ctx(pkey->pkey.ec, &mctx);
  257. if (!esig)
  258. goto error;
  259. }
  260. if (kat && ((siglen != katlen) || memcmp(kat, sig, katlen)))
  261. goto error;
  262. #if 0
  263. {
  264. /* Debug code to print out self test KAT discrepancies */
  265. unsigned int i;
  266. fprintf(stderr, "%s=", fail_str);
  267. for (i = 0; i < siglen; i++)
  268. fprintf(stderr, "%02X", sig[i]);
  269. fprintf(stderr, "\n");
  270. goto error;
  271. }
  272. #endif
  273. /* If just digest test we've finished */
  274. if (pkey == NULL)
  275. {
  276. ret = 1;
  277. /* Well actually sucess as we've set ret to 1 */
  278. goto error;
  279. }
  280. if (!FIPS_digestinit(&mctx, digest))
  281. goto error;
  282. if (!FIPS_digestupdate(&mctx, tbs, tbslen))
  283. goto error;
  284. if (pkey->type == EVP_PKEY_RSA)
  285. {
  286. ret = FIPS_rsa_verify_ctx(pkey->pkey.rsa, &mctx,
  287. pad_mode, 0, NULL, sig, siglen);
  288. }
  289. else if (pkey->type == EVP_PKEY_DSA)
  290. {
  291. ret = FIPS_dsa_verify_ctx(pkey->pkey.dsa, &mctx, dsig);
  292. }
  293. else if (pkey->type == EVP_PKEY_EC)
  294. {
  295. ret = FIPS_ecdsa_verify_ctx(pkey->pkey.ec, &mctx, esig);
  296. }
  297. error:
  298. if (dsig != NULL)
  299. FIPS_dsa_sig_free(dsig);
  300. if (esig != NULL)
  301. FIPS_ecdsa_sig_free(esig);
  302. if (sig)
  303. OPENSSL_free(sig);
  304. FIPS_md_ctx_cleanup(&mctx);
  305. if (ret != 1)
  306. {
  307. FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST,FIPS_R_TEST_FAILURE);
  308. if (fail_str)
  309. FIPS_add_error_data(2, "Type=", fail_str);
  310. fips_post_failed(id, subid, ex);
  311. return 0;
  312. }
  313. return fips_post_success(id, subid, pkey);
  314. }
  315. /* Generalized symmetric cipher test routine. Encrypt data, verify result
  316. * against known answer, decrypt and compare with original plaintext.
  317. */
  318. int fips_cipher_test(int id, EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  319. const unsigned char *key,
  320. const unsigned char *iv,
  321. const unsigned char *plaintext,
  322. const unsigned char *ciphertext,
  323. int len)
  324. {
  325. unsigned char pltmp[FIPS_MAX_CIPHER_TEST_SIZE];
  326. unsigned char citmp[FIPS_MAX_CIPHER_TEST_SIZE];
  327. int subid = M_EVP_CIPHER_nid(cipher);
  328. int rv = 0;
  329. OPENSSL_assert(len <= FIPS_MAX_CIPHER_TEST_SIZE);
  330. memset(pltmp, 0, FIPS_MAX_CIPHER_TEST_SIZE);
  331. memset(citmp, 0, FIPS_MAX_CIPHER_TEST_SIZE);
  332. if (!fips_post_started(id, subid, NULL))
  333. return 1;
  334. if (FIPS_cipherinit(ctx, cipher, key, iv, 1) <= 0)
  335. goto error;
  336. if (!FIPS_cipher(ctx, citmp, plaintext, len))
  337. goto error;
  338. if (memcmp(citmp, ciphertext, len))
  339. goto error;
  340. if (!fips_post_corrupt(id, subid, NULL))
  341. citmp[0] ^= 0x1;
  342. if (FIPS_cipherinit(ctx, cipher, key, iv, 0) <= 0)
  343. goto error;
  344. FIPS_cipher(ctx, pltmp, citmp, len);
  345. if (memcmp(pltmp, plaintext, len))
  346. goto error;
  347. rv = 1;
  348. error:
  349. if (rv == 0)
  350. {
  351. fips_post_failed(id, subid, NULL);
  352. return 0;
  353. }
  354. return fips_post_success(id, subid, NULL);
  355. }
  356. #endif