enc_min.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. /* crypto/evp/enc_min.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. #include <stdio.h>
  59. #include "cryptlib.h"
  60. #include <openssl/evp.h>
  61. #include <openssl/err.h>
  62. #include <openssl/rand.h>
  63. #ifndef OPENSSL_NO_ENGINE
  64. # include <openssl/engine.h>
  65. #endif
  66. #include "evp_locl.h"
  67. void EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *ctx)
  68. {
  69. #ifdef OPENSSL_FIPS
  70. FIPS_selftest_check();
  71. #endif
  72. memset(ctx, 0, sizeof(EVP_CIPHER_CTX));
  73. /* ctx->cipher=NULL; */
  74. }
  75. #ifdef OPENSSL_FIPS
  76. /*
  77. * The purpose of these is to trap programs that attempt to use non FIPS
  78. * algorithms in FIPS mode and ignore the errors.
  79. */
  80. static int bad_init(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  81. const unsigned char *iv, int enc)
  82. {
  83. FIPS_ERROR_IGNORED("Cipher init");
  84. return 0;
  85. }
  86. static int bad_do_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  87. const unsigned char *in, unsigned int inl)
  88. {
  89. FIPS_ERROR_IGNORED("Cipher update");
  90. return 0;
  91. }
  92. /* NB: no cleanup because it is allowed after failed init */
  93. static int bad_set_asn1(EVP_CIPHER_CTX *ctx, ASN1_TYPE *typ)
  94. {
  95. FIPS_ERROR_IGNORED("Cipher set_asn1");
  96. return 0;
  97. }
  98. static int bad_get_asn1(EVP_CIPHER_CTX *ctx, ASN1_TYPE *typ)
  99. {
  100. FIPS_ERROR_IGNORED("Cipher get_asn1");
  101. return 0;
  102. }
  103. static int bad_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
  104. {
  105. FIPS_ERROR_IGNORED("Cipher ctrl");
  106. return 0;
  107. }
  108. static const EVP_CIPHER bad_cipher = {
  109. 0,
  110. 0,
  111. 0,
  112. 0,
  113. 0,
  114. bad_init,
  115. bad_do_cipher,
  116. NULL,
  117. 0,
  118. bad_set_asn1,
  119. bad_get_asn1,
  120. bad_ctrl,
  121. NULL
  122. };
  123. #endif
  124. #ifndef OPENSSL_NO_ENGINE
  125. # ifdef OPENSSL_FIPS
  126. static int do_engine_null(ENGINE *impl)
  127. {
  128. return 0;
  129. }
  130. static int do_evp_enc_engine_null(EVP_CIPHER_CTX *ctx,
  131. const EVP_CIPHER **pciph, ENGINE *impl)
  132. {
  133. return 1;
  134. }
  135. static int (*do_engine_finish) (ENGINE *impl)
  136. = do_engine_null;
  137. static int (*do_evp_enc_engine)
  138. (EVP_CIPHER_CTX *ctx, const EVP_CIPHER **pciph, ENGINE *impl)
  139. = do_evp_enc_engine_null;
  140. void int_EVP_CIPHER_set_engine_callbacks(int (*eng_ciph_fin) (ENGINE *impl),
  141. int (*eng_ciph_evp)
  142. (EVP_CIPHER_CTX *ctx,
  143. const EVP_CIPHER **pciph,
  144. ENGINE *impl))
  145. {
  146. do_engine_finish = eng_ciph_fin;
  147. do_evp_enc_engine = eng_ciph_evp;
  148. }
  149. # else
  150. # define do_engine_finish ENGINE_finish
  151. static int do_evp_enc_engine(EVP_CIPHER_CTX *ctx, const EVP_CIPHER **pcipher,
  152. ENGINE *impl)
  153. {
  154. if (impl) {
  155. if (!ENGINE_init(impl)) {
  156. EVPerr(EVP_F_DO_EVP_ENC_ENGINE, EVP_R_INITIALIZATION_ERROR);
  157. return 0;
  158. }
  159. } else
  160. /* Ask if an ENGINE is reserved for this job */
  161. impl = ENGINE_get_cipher_engine((*pcipher)->nid);
  162. if (impl) {
  163. /* There's an ENGINE for this job ... (apparently) */
  164. const EVP_CIPHER *c = ENGINE_get_cipher(impl, (*pcipher)->nid);
  165. if (!c) {
  166. /*
  167. * One positive side-effect of US's export control history, is
  168. * that we should at least be able to avoid using US mispellings
  169. * of "initialisation"?
  170. */
  171. EVPerr(EVP_F_DO_EVP_ENC_ENGINE, EVP_R_INITIALIZATION_ERROR);
  172. return 0;
  173. }
  174. /* We'll use the ENGINE's private cipher definition */
  175. *pcipher = c;
  176. /*
  177. * Store the ENGINE functional reference so we know 'cipher' came
  178. * from an ENGINE and we need to release it when done.
  179. */
  180. ctx->engine = impl;
  181. } else
  182. ctx->engine = NULL;
  183. return 1;
  184. }
  185. # endif
  186. #endif
  187. int EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *cipher,
  188. ENGINE *impl, const unsigned char *key,
  189. const unsigned char *iv, int enc)
  190. {
  191. if (enc == -1)
  192. enc = ctx->encrypt;
  193. else {
  194. if (enc)
  195. enc = 1;
  196. ctx->encrypt = enc;
  197. }
  198. #ifdef OPENSSL_FIPS
  199. if (FIPS_selftest_failed()) {
  200. FIPSerr(FIPS_F_EVP_CIPHERINIT_EX, FIPS_R_FIPS_SELFTEST_FAILED);
  201. ctx->cipher = &bad_cipher;
  202. return 0;
  203. }
  204. #endif
  205. #ifndef OPENSSL_NO_ENGINE
  206. /*
  207. * Whether it's nice or not, "Inits" can be used on "Final"'d contexts so
  208. * this context may already have an ENGINE! Try to avoid releasing the
  209. * previous handle, re-querying for an ENGINE, and having a
  210. * reinitialisation, when it may all be unecessary.
  211. */
  212. if (ctx->engine && ctx->cipher && (!cipher ||
  213. (cipher
  214. && (cipher->nid ==
  215. ctx->cipher->nid))))
  216. goto skip_to_init;
  217. #endif
  218. if (cipher) {
  219. /*
  220. * Ensure a context left lying around from last time is cleared (the
  221. * previous check attempted to avoid this if the same ENGINE and
  222. * EVP_CIPHER could be used).
  223. */
  224. EVP_CIPHER_CTX_cleanup(ctx);
  225. /* Restore encrypt field: it is zeroed by cleanup */
  226. ctx->encrypt = enc;
  227. #ifndef OPENSSL_NO_ENGINE
  228. if (!do_evp_enc_engine(ctx, &cipher, impl))
  229. return 0;
  230. #endif
  231. ctx->cipher = cipher;
  232. if (ctx->cipher->ctx_size) {
  233. ctx->cipher_data = OPENSSL_malloc(ctx->cipher->ctx_size);
  234. if (!ctx->cipher_data) {
  235. EVPerr(EVP_F_EVP_CIPHERINIT_EX, ERR_R_MALLOC_FAILURE);
  236. return 0;
  237. }
  238. } else {
  239. ctx->cipher_data = NULL;
  240. }
  241. ctx->key_len = cipher->key_len;
  242. ctx->flags = 0;
  243. if (ctx->cipher->flags & EVP_CIPH_CTRL_INIT) {
  244. if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL)) {
  245. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_INITIALIZATION_ERROR);
  246. return 0;
  247. }
  248. }
  249. } else if (!ctx->cipher) {
  250. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_NO_CIPHER_SET);
  251. return 0;
  252. }
  253. #ifndef OPENSSL_NO_ENGINE
  254. skip_to_init:
  255. #endif
  256. /* we assume block size is a power of 2 in *cryptUpdate */
  257. OPENSSL_assert(ctx->cipher->block_size == 1
  258. || ctx->cipher->block_size == 8
  259. || ctx->cipher->block_size == 16);
  260. if (!(EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_CUSTOM_IV)) {
  261. switch (EVP_CIPHER_CTX_mode(ctx)) {
  262. case EVP_CIPH_STREAM_CIPHER:
  263. case EVP_CIPH_ECB_MODE:
  264. break;
  265. case EVP_CIPH_CFB_MODE:
  266. case EVP_CIPH_OFB_MODE:
  267. ctx->num = 0;
  268. /* fall-through */
  269. case EVP_CIPH_CBC_MODE:
  270. OPENSSL_assert(EVP_CIPHER_CTX_iv_length(ctx) <=
  271. (int)sizeof(ctx->iv));
  272. if (iv)
  273. memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
  274. memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
  275. break;
  276. default:
  277. return 0;
  278. break;
  279. }
  280. }
  281. #ifdef OPENSSL_FIPS
  282. /*
  283. * After 'key' is set no further parameters changes are permissible. So
  284. * only check for non FIPS enabling at this point.
  285. */
  286. if (key && FIPS_mode()) {
  287. if (!(ctx->cipher->flags & EVP_CIPH_FLAG_FIPS)
  288. & !(ctx->flags & EVP_CIPH_FLAG_NON_FIPS_ALLOW)) {
  289. EVPerr(EVP_F_EVP_CIPHERINIT_EX, EVP_R_DISABLED_FOR_FIPS);
  290. # if 0
  291. ERR_add_error_data(2, "cipher=", EVP_CIPHER_name(ctx->cipher));
  292. # endif
  293. ctx->cipher = &bad_cipher;
  294. return 0;
  295. }
  296. }
  297. #endif
  298. if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT)) {
  299. if (!ctx->cipher->init(ctx, key, iv, enc))
  300. return 0;
  301. }
  302. ctx->buf_len = 0;
  303. ctx->final_used = 0;
  304. ctx->block_mask = ctx->cipher->block_size - 1;
  305. return 1;
  306. }
  307. int EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)
  308. {
  309. if (c->cipher != NULL) {
  310. if (c->cipher->cleanup && !c->cipher->cleanup(c))
  311. return 0;
  312. /* Cleanse cipher context data */
  313. if (c->cipher_data)
  314. OPENSSL_cleanse(c->cipher_data, c->cipher->ctx_size);
  315. }
  316. if (c->cipher_data)
  317. OPENSSL_free(c->cipher_data);
  318. #ifndef OPENSSL_NO_ENGINE
  319. if (c->engine)
  320. /*
  321. * The EVP_CIPHER we used belongs to an ENGINE, release the
  322. * functional reference we held for this reason.
  323. */
  324. do_engine_finish(c->engine);
  325. #endif
  326. memset(c, 0, sizeof(EVP_CIPHER_CTX));
  327. return 1;
  328. }
  329. int EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  330. const unsigned char *in, unsigned int inl)
  331. {
  332. #ifdef OPENSSL_FIPS
  333. FIPS_selftest_check();
  334. #endif
  335. return ctx->cipher->do_cipher(ctx, out, in, inl);
  336. }
  337. int EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *ptr)
  338. {
  339. int ret;
  340. if (!ctx->cipher) {
  341. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_NO_CIPHER_SET);
  342. return 0;
  343. }
  344. if (!ctx->cipher->ctrl) {
  345. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL, EVP_R_CTRL_NOT_IMPLEMENTED);
  346. return 0;
  347. }
  348. ret = ctx->cipher->ctrl(ctx, type, arg, ptr);
  349. if (ret == -1) {
  350. EVPerr(EVP_F_EVP_CIPHER_CTX_CTRL,
  351. EVP_R_CTRL_OPERATION_NOT_IMPLEMENTED);
  352. return 0;
  353. }
  354. return ret;
  355. }
  356. unsigned long EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx)
  357. {
  358. return ctx->cipher->flags;
  359. }
  360. int EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
  361. {
  362. return ctx->cipher->iv_len;
  363. }
  364. int EVP_CIPHER_nid(const EVP_CIPHER *cipher)
  365. {
  366. return cipher->nid;
  367. }