fips_enc.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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_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(!ctx->cipher) {
  235. EVPerr(EVP_F_FIPS_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET);
  236. return 0;
  237. }
  238. FIPS_selftest_check();
  239. if(!ctx->cipher->ctrl) {
  240. EVPerr(EVP_F_FIPS_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED);
  241. return 0;
  242. }
  243. ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
  244. if(ret == -1) {
  245. EVPerr(EVP_F_FIPS_CIPHER_CTX_CTRL, EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
  246. return 0;
  247. }
  248. return ret;
  249. }
  250. int FIPS_cipher_ctx_copy(EVP_CIPHER_CTX *out, const EVP_CIPHER_CTX *in)
  251. {
  252. if ((in == NULL) || (in->cipher == NULL))
  253. {
  254. EVPerr(EVP_F_FIPS_CIPHER_CTX_COPY,EVP_R_INPUT_NOT_INITIALIZED);
  255. return 0;
  256. }
  257. /* Only FIPS ciphers allowed */
  258. if (FIPS_mode() && !(in->cipher->flags & EVP_CIPH_FLAG_FIPS) &&
  259. !(out->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW))
  260. {
  261. EVPerr(EVP_F_FIPS_CIPHER_CTX_COPY, EVP_R_DISABLED_FOR_FIPS);
  262. out->cipher = &bad_cipher;
  263. return 0;
  264. }
  265. FIPS_cipher_ctx_cleanup(out);
  266. memcpy(out,in,sizeof *out);
  267. if (in->cipher_data && in->cipher->ctx_size)
  268. {
  269. out->cipher_data=OPENSSL_malloc(in->cipher->ctx_size);
  270. if (!out->cipher_data)
  271. {
  272. EVPerr(EVP_F_FIPS_CIPHER_CTX_COPY,ERR_R_MALLOC_FAILURE);
  273. return 0;
  274. }
  275. memcpy(out->cipher_data,in->cipher_data,in->cipher->ctx_size);
  276. }
  277. if (in->cipher->flags & EVP_CIPH_CUSTOM_COPY)
  278. return in->cipher->ctrl((EVP_CIPHER_CTX *)in, EVP_CTRL_COPY, 0, out);
  279. return 1;
  280. }
  281. /* You can't really set the key length with FIPS, so just check that the
  282. caller sets the length the context already has. */
  283. int FIPS_cipher_ctx_set_key_length(EVP_CIPHER_CTX *ctx, int keylen)
  284. {
  285. if (ctx->key_len == keylen)
  286. return 1;
  287. EVPerr(EVP_F_FIPS_CIPHER_CTX_SET_KEY_LENGTH,EVP_R_INVALID_KEY_LENGTH);
  288. return 0;
  289. }
  290. int FIPS_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  291. const unsigned char *in, unsigned int inl)
  292. {
  293. FIPS_selftest_check();
  294. return ctx->cipher->do_cipher(ctx,out,in,inl);
  295. }