fips_enc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468
  1. /* fipe/evp/fips_enc.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. #define OPENSSL_FIPSAPI
  59. #include <stdio.h>
  60. #include <string.h>
  61. #include <openssl/evp.h>
  62. #include <openssl/err.h>
  63. #include <openssl/fips.h>
  64. void FIPS_cipher_ctx_init(EVP_CIPHER_CTX *ctx)
  65. {
  66. memset(ctx,0,sizeof(EVP_CIPHER_CTX));
  67. /* ctx->cipher=NULL; */
  68. }
  69. EVP_CIPHER_CTX *FIPS_cipher_ctx_new(void)
  70. {
  71. EVP_CIPHER_CTX *ctx=OPENSSL_malloc(sizeof *ctx);
  72. if (ctx)
  73. FIPS_cipher_ctx_init(ctx);
  74. return ctx;
  75. }
  76. /* The purpose of these is to trap programs that attempt to use non FIPS
  77. * algorithms in FIPS mode and ignore the errors.
  78. */
  79. static int bad_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  80. const unsigned char *iv, int enc)
  81. { FIPS_ERROR_IGNORED("Cipher init"); return 0;}
  82. static int bad_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  83. const unsigned char *in, size_t inl)
  84. { FIPS_ERROR_IGNORED("Cipher update"); return 0;}
  85. /* NB: no cleanup because it is allowed after failed init */
  86. static int bad_set_asn1(EVP_CIPHER_CTX *ctx, ASN1_TYPE *typ)
  87. { FIPS_ERROR_IGNORED("Cipher set_asn1"); return 0;}
  88. static int bad_get_asn1(EVP_CIPHER_CTX *ctx, ASN1_TYPE *typ)
  89. { FIPS_ERROR_IGNORED("Cipher get_asn1"); return 0;}
  90. static int bad_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
  91. { FIPS_ERROR_IGNORED("Cipher ctrl"); return 0;}
  92. static const EVP_CIPHER bad_cipher =
  93. {
  94. 0,
  95. 0,
  96. 0,
  97. 0,
  98. 0,
  99. bad_init,
  100. bad_do_cipher,
  101. NULL,
  102. 0,
  103. bad_set_asn1,
  104. bad_get_asn1,
  105. bad_ctrl,
  106. NULL
  107. };
  108. int FIPS_cipherinit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  109. const unsigned char *key, const unsigned char *iv, int enc)
  110. {
  111. if(FIPS_selftest_failed())
  112. {
  113. FIPSerr(FIPS_F_FIPS_CIPHERINIT,FIPS_R_FIPS_SELFTEST_FAILED);
  114. ctx->cipher = &bad_cipher;
  115. return 0;
  116. }
  117. if (enc == -1)
  118. enc = ctx->encrypt;
  119. else
  120. {
  121. if (enc)
  122. enc = 1;
  123. ctx->encrypt = enc;
  124. }
  125. if (cipher)
  126. {
  127. /* Only FIPS ciphers allowed */
  128. if (FIPS_module_mode() && !(cipher->flags & EVP_CIPH_FLAG_FIPS) &&
  129. !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW))
  130. {
  131. EVPerr(EVP_F_FIPS_CIPHERINIT, EVP_R_DISABLED_FOR_FIPS);
  132. ctx->cipher = &bad_cipher;
  133. return 0;
  134. }
  135. /* Ensure a context left lying around from last time is cleared
  136. * (the previous check attempted to avoid this if the same
  137. * ENGINE and EVP_CIPHER could be used). */
  138. FIPS_cipher_ctx_cleanup(ctx);
  139. /* Restore encrypt field: it is zeroed by cleanup */
  140. ctx->encrypt = enc;
  141. ctx->cipher=cipher;
  142. if (ctx->cipher->ctx_size)
  143. {
  144. ctx->cipher_data=OPENSSL_malloc(ctx->cipher->ctx_size);
  145. if (!ctx->cipher_data)
  146. {
  147. EVPerr(EVP_F_FIPS_CIPHERINIT, ERR_R_MALLOC_FAILURE);
  148. return 0;
  149. }
  150. }
  151. else
  152. {
  153. ctx->cipher_data = NULL;
  154. }
  155. ctx->key_len = cipher->key_len;
  156. ctx->flags = 0;
  157. if(ctx->cipher->flags & EVP_CIPH_CTRL_INIT)
  158. {
  159. if(!FIPS_cipher_ctx_ctrl(ctx, EVP_CTRL_INIT, 0, NULL))
  160. {
  161. EVPerr(EVP_F_FIPS_CIPHERINIT, EVP_R_INITIALIZATION_ERROR);
  162. return 0;
  163. }
  164. }
  165. }
  166. else if(!ctx->cipher)
  167. {
  168. EVPerr(EVP_F_FIPS_CIPHERINIT, EVP_R_NO_CIPHER_SET);
  169. return 0;
  170. }
  171. /* we assume block size is a power of 2 in *cryptUpdate */
  172. OPENSSL_assert(ctx->cipher->block_size == 1
  173. || ctx->cipher->block_size == 8
  174. || ctx->cipher->block_size == 16);
  175. if(!(M_EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_CUSTOM_IV)) {
  176. switch(M_EVP_CIPHER_CTX_mode(ctx)) {
  177. case EVP_CIPH_STREAM_CIPHER:
  178. case EVP_CIPH_ECB_MODE:
  179. break;
  180. case EVP_CIPH_CFB_MODE:
  181. case EVP_CIPH_OFB_MODE:
  182. ctx->num = 0;
  183. /* fall-through */
  184. case EVP_CIPH_CBC_MODE:
  185. OPENSSL_assert(M_EVP_CIPHER_CTX_iv_length(ctx) <=
  186. (int)sizeof(ctx->iv));
  187. if(iv) memcpy(ctx->oiv, iv, M_EVP_CIPHER_CTX_iv_length(ctx));
  188. memcpy(ctx->iv, ctx->oiv, M_EVP_CIPHER_CTX_iv_length(ctx));
  189. break;
  190. case EVP_CIPH_CTR_MODE:
  191. /* Don't reuse IV for CTR mode */
  192. if(iv)
  193. memcpy(ctx->iv, iv, M_EVP_CIPHER_CTX_iv_length(ctx));
  194. break;
  195. default:
  196. return 0;
  197. break;
  198. }
  199. }
  200. if(key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
  201. if(!ctx->cipher->init(ctx,key,iv,enc)) return 0;
  202. }
  203. ctx->buf_len=0;
  204. ctx->final_used=0;
  205. ctx->block_mask=ctx->cipher->block_size-1;
  206. return 1;
  207. }
  208. void FIPS_cipher_ctx_free(EVP_CIPHER_CTX *ctx)
  209. {
  210. if (ctx)
  211. {
  212. FIPS_cipher_ctx_cleanup(ctx);
  213. OPENSSL_free(ctx);
  214. }
  215. }
  216. int FIPS_cipher_ctx_cleanup(EVP_CIPHER_CTX *c)
  217. {
  218. if (c->cipher != NULL)
  219. {
  220. if(c->cipher->cleanup && !c->cipher->cleanup(c))
  221. return 0;
  222. /* Cleanse cipher context data */
  223. if (c->cipher_data)
  224. OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size);
  225. }
  226. if (c->cipher_data)
  227. OPENSSL_free(c->cipher_data);
  228. memset(c,0,sizeof(EVP_CIPHER_CTX));
  229. return 1;
  230. }
  231. int FIPS_cipher_ctx_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
  232. {
  233. int ret;
  234. if (FIPS_selftest_failed())
  235. {
  236. FIPSerr(FIPS_F_FIPS_CIPHER_CTX_CTRL, FIPS_R_SELFTEST_FAILED);
  237. return 0;
  238. }
  239. if(!ctx->cipher) {
  240. EVPerr(EVP_F_FIPS_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET);
  241. return 0;
  242. }
  243. if(!ctx->cipher->ctrl) {
  244. EVPerr(EVP_F_FIPS_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED);
  245. return 0;
  246. }
  247. ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
  248. if(ret == -1) {
  249. EVPerr(EVP_F_FIPS_CIPHER_CTX_CTRL, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
  250. return 0;
  251. }
  252. return ret;
  253. }
  254. int FIPS_cipher_ctx_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
  255. {
  256. if ((in == NULL) || (in->cipher == NULL))
  257. {
  258. EVPerr(EVP_F_FIPS_CIPHER_CTX_COPY,EVP_R_INPUT_NOT_INITIALIZED);
  259. return 0;
  260. }
  261. /* Only FIPS ciphers allowed */
  262. if (FIPS_module_mode() && !(in->cipher->flags & EVP_CIPH_FLAG_FIPS) &&
  263. !(out->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW))
  264. {
  265. EVPerr(EVP_F_FIPS_CIPHER_CTX_COPY, EVP_R_DISABLED_FOR_FIPS);
  266. out->cipher = &bad_cipher;
  267. return 0;
  268. }
  269. FIPS_cipher_ctx_cleanup(out);
  270. memcpy(out,in,sizeof *out);
  271. if (in->cipher_data && in->cipher->ctx_size)
  272. {
  273. out->cipher_data=OPENSSL_malloc(in->cipher->ctx_size);
  274. if (!out->cipher_data)
  275. {
  276. EVPerr(EVP_F_FIPS_CIPHER_CTX_COPY,ERR_R_MALLOC_FAILURE);
  277. return 0;
  278. }
  279. memcpy(out->cipher_data,in->cipher_data,in->cipher->ctx_size);
  280. }
  281. if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
  282. return in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out);
  283. return 1;
  284. }
  285. /* You can't really set the key length with FIPS, so just check that the
  286. caller sets the length the context already has. */
  287. int FIPS_cipher_ctx_set_key_length(EVP_CIPHER_CTX *ctx, int keylen)
  288. {
  289. if (ctx->key_len == keylen)
  290. return 1;
  291. EVPerr(EVP_F_FIPS_CIPHER_CTX_SET_KEY_LENGTH,EVP_R_INVALID_KEY_LENGTH);
  292. return 0;
  293. }
  294. int FIPS_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  295. const unsigned char *in, unsigned int inl)
  296. {
  297. if (FIPS_selftest_failed())
  298. {
  299. FIPSerr(FIPS_F_FIPS_CIPHER, FIPS_R_SELFTEST_FAILED);
  300. return -1;
  301. }
  302. return ctx->cipher->do_cipher(ctx,out,in,inl);
  303. }
  304. const EVP_CIPHER *FIPS_get_cipherbynid(int nid)
  305. {
  306. switch (nid)
  307. {
  308. case NID_aes_128_cbc:
  309. return FIPS_evp_aes_128_cbc();
  310. case NID_aes_128_ccm:
  311. return FIPS_evp_aes_128_ccm();
  312. case NID_aes_128_cfb1:
  313. return FIPS_evp_aes_128_cfb1();
  314. case NID_aes_128_cfb128:
  315. return FIPS_evp_aes_128_cfb128();
  316. case NID_aes_128_cfb8:
  317. return FIPS_evp_aes_128_cfb8();
  318. case NID_aes_128_ctr:
  319. return FIPS_evp_aes_128_ctr();
  320. case NID_aes_128_ecb:
  321. return FIPS_evp_aes_128_ecb();
  322. case NID_aes_128_gcm:
  323. return FIPS_evp_aes_128_gcm();
  324. case NID_aes_128_ofb128:
  325. return FIPS_evp_aes_128_ofb();
  326. case NID_aes_128_xts:
  327. return FIPS_evp_aes_128_xts();
  328. case NID_aes_192_cbc:
  329. return FIPS_evp_aes_192_cbc();
  330. case NID_aes_192_ccm:
  331. return FIPS_evp_aes_192_ccm();
  332. case NID_aes_192_cfb1:
  333. return FIPS_evp_aes_192_cfb1();
  334. case NID_aes_192_cfb128:
  335. return FIPS_evp_aes_192_cfb128();
  336. case NID_aes_192_cfb8:
  337. return FIPS_evp_aes_192_cfb8();
  338. case NID_aes_192_ctr:
  339. return FIPS_evp_aes_192_ctr();
  340. case NID_aes_192_ecb:
  341. return FIPS_evp_aes_192_ecb();
  342. case NID_aes_192_gcm:
  343. return FIPS_evp_aes_192_gcm();
  344. case NID_aes_192_ofb128:
  345. return FIPS_evp_aes_192_ofb();
  346. case NID_aes_256_cbc:
  347. return FIPS_evp_aes_256_cbc();
  348. case NID_aes_256_ccm:
  349. return FIPS_evp_aes_256_ccm();
  350. case NID_aes_256_cfb1:
  351. return FIPS_evp_aes_256_cfb1();
  352. case NID_aes_256_cfb128:
  353. return FIPS_evp_aes_256_cfb128();
  354. case NID_aes_256_cfb8:
  355. return FIPS_evp_aes_256_cfb8();
  356. case NID_aes_256_ctr:
  357. return FIPS_evp_aes_256_ctr();
  358. case NID_aes_256_ecb:
  359. return FIPS_evp_aes_256_ecb();
  360. case NID_aes_256_gcm:
  361. return FIPS_evp_aes_256_gcm();
  362. case NID_aes_256_ofb128:
  363. return FIPS_evp_aes_256_ofb();
  364. case NID_aes_256_xts:
  365. return FIPS_evp_aes_256_xts();
  366. case NID_des_ede_ecb:
  367. return FIPS_evp_des_ede();
  368. case NID_des_ede3_ecb:
  369. return FIPS_evp_des_ede3();
  370. case NID_des_ede3_cbc:
  371. return FIPS_evp_des_ede3_cbc();
  372. case NID_des_ede3_cfb1:
  373. return FIPS_evp_des_ede3_cfb1();
  374. case NID_des_ede3_cfb64:
  375. return FIPS_evp_des_ede3_cfb64();
  376. case NID_des_ede3_cfb8:
  377. return FIPS_evp_des_ede3_cfb8();
  378. case NID_des_ede3_ofb64:
  379. return FIPS_evp_des_ede3_ofb();
  380. case NID_des_ede_cbc:
  381. return FIPS_evp_des_ede_cbc();
  382. case NID_des_ede_cfb64:
  383. return FIPS_evp_des_ede_cfb64();
  384. case NID_des_ede_ofb64:
  385. return FIPS_evp_des_ede_ofb();
  386. default:
  387. return NULL;
  388. }
  389. }