fips_post.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. fips_post_end();
  177. return rv;
  178. }
  179. /* Generalized public key test routine. Signs and verifies the data
  180. * supplied in tbs using mesage digest md and setting RSA padding mode
  181. * pad_mode. If the 'kat' parameter is not NULL it will
  182. * additionally check the signature matches it: a known answer test
  183. * The string "fail_str" is used for identification purposes in case
  184. * of failure. If "pkey" is NULL just perform a message digest check.
  185. */
  186. int fips_pkey_signature_test(int id, EVP_PKEY *pkey,
  187. const unsigned char *tbs, size_t tbslen,
  188. const unsigned char *kat, size_t katlen,
  189. const EVP_MD *digest, int pad_mode,
  190. const char *fail_str)
  191. {
  192. int subid;
  193. void *ex = NULL;
  194. int ret = 0;
  195. unsigned char *sig = NULL;
  196. unsigned int siglen;
  197. static const unsigned char str1[]="12345678901234567890";
  198. DSA_SIG *dsig = NULL;
  199. ECDSA_SIG *esig = NULL;
  200. EVP_MD_CTX mctx;
  201. FIPS_md_ctx_init(&mctx);
  202. if (tbs == NULL)
  203. tbs = str1;
  204. if (tbslen == 0)
  205. tbslen = strlen((char *)tbs);
  206. if (digest == NULL)
  207. digest = EVP_sha256();
  208. subid = M_EVP_MD_type(digest);
  209. if (!fips_post_started(id, subid, pkey))
  210. return 1;
  211. if (!pkey || pkey->type == EVP_PKEY_RSA)
  212. {
  213. size_t sigsize;
  214. if (!pkey)
  215. sigsize = EVP_MAX_MD_SIZE;
  216. else
  217. sigsize = RSA_size(pkey->pkey.rsa);
  218. sig = OPENSSL_malloc(sigsize);
  219. if (!sig)
  220. {
  221. FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST,ERR_R_MALLOC_FAILURE);
  222. goto error;
  223. }
  224. }
  225. if (!FIPS_digestinit(&mctx, digest))
  226. goto error;
  227. if (!FIPS_digestupdate(&mctx, tbs, tbslen))
  228. goto error;
  229. if (!fips_post_corrupt(id, subid, pkey))
  230. {
  231. if (!FIPS_digestupdate(&mctx, tbs, 1))
  232. goto error;
  233. }
  234. if (pkey == NULL)
  235. {
  236. if (!FIPS_digestfinal(&mctx, sig, &siglen))
  237. goto error;
  238. }
  239. else if (pkey->type == EVP_PKEY_RSA)
  240. {
  241. if (!FIPS_rsa_sign_ctx(pkey->pkey.rsa, &mctx,
  242. pad_mode, 0, NULL, sig, &siglen))
  243. goto error;
  244. }
  245. else if (pkey->type == EVP_PKEY_DSA)
  246. {
  247. dsig = FIPS_dsa_sign_ctx(pkey->pkey.dsa, &mctx);
  248. if (!dsig)
  249. goto error;
  250. }
  251. else if (pkey->type == EVP_PKEY_EC)
  252. {
  253. esig = FIPS_ecdsa_sign_ctx(pkey->pkey.ec, &mctx);
  254. if (!esig)
  255. goto error;
  256. }
  257. if (kat && ((siglen != katlen) || memcmp(kat, sig, katlen)))
  258. goto error;
  259. #if 0
  260. {
  261. /* Debug code to print out self test KAT discrepancies */
  262. unsigned int i;
  263. fprintf(stderr, "%s=", fail_str);
  264. for (i = 0; i < siglen; i++)
  265. fprintf(stderr, "%02X", sig[i]);
  266. fprintf(stderr, "\n");
  267. goto error;
  268. }
  269. #endif
  270. /* If just digest test we've finished */
  271. if (pkey == NULL)
  272. {
  273. ret = 1;
  274. /* Well actually sucess as we've set ret to 1 */
  275. goto error;
  276. }
  277. if (!FIPS_digestinit(&mctx, digest))
  278. goto error;
  279. if (!FIPS_digestupdate(&mctx, tbs, tbslen))
  280. goto error;
  281. if (pkey->type == EVP_PKEY_RSA)
  282. {
  283. ret = FIPS_rsa_verify_ctx(pkey->pkey.rsa, &mctx,
  284. pad_mode, 0, NULL, sig, siglen);
  285. }
  286. else if (pkey->type == EVP_PKEY_DSA)
  287. {
  288. ret = FIPS_dsa_verify_ctx(pkey->pkey.dsa, &mctx, dsig);
  289. }
  290. else if (pkey->type == EVP_PKEY_EC)
  291. {
  292. ret = FIPS_ecdsa_verify_ctx(pkey->pkey.ec, &mctx, esig);
  293. }
  294. error:
  295. if (dsig != NULL)
  296. FIPS_dsa_sig_free(dsig);
  297. if (esig != NULL)
  298. FIPS_ecdsa_sig_free(esig);
  299. if (sig)
  300. OPENSSL_free(sig);
  301. FIPS_md_ctx_cleanup(&mctx);
  302. if (ret != 1)
  303. {
  304. FIPSerr(FIPS_F_FIPS_PKEY_SIGNATURE_TEST,FIPS_R_TEST_FAILURE);
  305. if (fail_str)
  306. FIPS_add_error_data(2, "Type=", fail_str);
  307. fips_post_failed(id, subid, ex);
  308. return 0;
  309. }
  310. return fips_post_success(id, subid, pkey);
  311. }
  312. /* Generalized symmetric cipher test routine. Encrypt data, verify result
  313. * against known answer, decrypt and compare with original plaintext.
  314. */
  315. int fips_cipher_test(int id, EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  316. const unsigned char *key,
  317. const unsigned char *iv,
  318. const unsigned char *plaintext,
  319. const unsigned char *ciphertext,
  320. int len)
  321. {
  322. unsigned char pltmp[FIPS_MAX_CIPHER_TEST_SIZE];
  323. unsigned char citmp[FIPS_MAX_CIPHER_TEST_SIZE];
  324. int subid = M_EVP_CIPHER_nid(cipher);
  325. int rv = 0;
  326. OPENSSL_assert(len <= FIPS_MAX_CIPHER_TEST_SIZE);
  327. memset(pltmp, 0, FIPS_MAX_CIPHER_TEST_SIZE);
  328. memset(citmp, 0, FIPS_MAX_CIPHER_TEST_SIZE);
  329. if (!fips_post_started(id, subid, NULL))
  330. return 1;
  331. if (FIPS_cipherinit(ctx, cipher, key, iv, 1) <= 0)
  332. goto error;
  333. if (!FIPS_cipher(ctx, citmp, plaintext, len))
  334. goto error;
  335. if (memcmp(citmp, ciphertext, len))
  336. goto error;
  337. if (!fips_post_corrupt(id, subid, NULL))
  338. citmp[0] ^= 0x1;
  339. if (FIPS_cipherinit(ctx, cipher, key, iv, 0) <= 0)
  340. goto error;
  341. FIPS_cipher(ctx, pltmp, citmp, len);
  342. if (memcmp(pltmp, plaintext, len))
  343. goto error;
  344. rv = 1;
  345. error:
  346. if (rv == 0)
  347. {
  348. fips_post_failed(id, subid, NULL);
  349. return 0;
  350. }
  351. return fips_post_success(id, subid, NULL);
  352. }
  353. #endif