e_camellia.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /* crypto/evp/e_camellia.c */
  2. /* ====================================================================
  3. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. *
  17. * 3. All advertising materials mentioning features or use of this
  18. * software must display the following acknowledgment:
  19. * "This product includes software developed by the OpenSSL Project
  20. * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
  21. *
  22. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  23. * endorse or promote products derived from this software without
  24. * prior written permission. For written permission, please contact
  25. * openssl-core@openssl.org.
  26. *
  27. * 5. Products derived from this software may not be called "OpenSSL"
  28. * nor may "OpenSSL" appear in their names without prior written
  29. * permission of the OpenSSL Project.
  30. *
  31. * 6. Redistributions of any form whatsoever must retain the following
  32. * acknowledgment:
  33. * "This product includes software developed by the OpenSSL Project
  34. * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  37. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  38. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  39. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  42. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  43. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  44. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  45. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  46. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  47. * OF THE POSSIBILITY OF SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This product includes cryptographic software written by Eric Young
  51. * (eay@cryptsoft.com). This product includes software written by Tim
  52. * Hudson (tjh@cryptsoft.com).
  53. *
  54. */
  55. #include <openssl/opensslconf.h>
  56. #ifndef OPENSSL_NO_CAMELLIA
  57. # include <openssl/evp.h>
  58. # include <openssl/err.h>
  59. # include <string.h>
  60. # include <assert.h>
  61. # include <openssl/camellia.h>
  62. # include "evp_locl.h"
  63. # include "modes_lcl.h"
  64. static int camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  65. const unsigned char *iv, int enc);
  66. /* Camellia subkey Structure */
  67. typedef struct {
  68. CAMELLIA_KEY ks;
  69. block128_f block;
  70. union {
  71. cbc128_f cbc;
  72. ctr128_f ctr;
  73. } stream;
  74. } EVP_CAMELLIA_KEY;
  75. # define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4))
  76. /* Attribute operation for Camellia */
  77. # define data(ctx) EVP_C_DATA(EVP_CAMELLIA_KEY,ctx)
  78. # if defined(AES_ASM) && (defined(__sparc) || defined(__sparc__))
  79. /* ---------^^^ this is not a typo, just a way to detect that
  80. * assembler support was in general requested... */
  81. # include "sparc_arch.h"
  82. extern unsigned int OPENSSL_sparcv9cap_P[];
  83. # define SPARC_CMLL_CAPABLE (OPENSSL_sparcv9cap_P[1] & CFR_CAMELLIA)
  84. void cmll_t4_set_key(const unsigned char *key, int bits, CAMELLIA_KEY *ks);
  85. void cmll_t4_encrypt(const unsigned char *in, unsigned char *out,
  86. const CAMELLIA_KEY *key);
  87. void cmll_t4_decrypt(const unsigned char *in, unsigned char *out,
  88. const CAMELLIA_KEY *key);
  89. void cmll128_t4_cbc_encrypt(const unsigned char *in, unsigned char *out,
  90. size_t len, const CAMELLIA_KEY *key,
  91. unsigned char *ivec);
  92. void cmll128_t4_cbc_decrypt(const unsigned char *in, unsigned char *out,
  93. size_t len, const CAMELLIA_KEY *key,
  94. unsigned char *ivec);
  95. void cmll256_t4_cbc_encrypt(const unsigned char *in, unsigned char *out,
  96. size_t len, const CAMELLIA_KEY *key,
  97. unsigned char *ivec);
  98. void cmll256_t4_cbc_decrypt(const unsigned char *in, unsigned char *out,
  99. size_t len, const CAMELLIA_KEY *key,
  100. unsigned char *ivec);
  101. void cmll128_t4_ctr32_encrypt(const unsigned char *in, unsigned char *out,
  102. size_t blocks, const CAMELLIA_KEY *key,
  103. unsigned char *ivec);
  104. void cmll256_t4_ctr32_encrypt(const unsigned char *in, unsigned char *out,
  105. size_t blocks, const CAMELLIA_KEY *key,
  106. unsigned char *ivec);
  107. static int cmll_t4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  108. const unsigned char *iv, int enc)
  109. {
  110. int ret, mode, bits;
  111. EVP_CAMELLIA_KEY *dat = (EVP_CAMELLIA_KEY *) ctx->cipher_data;
  112. mode = ctx->cipher->flags & EVP_CIPH_MODE;
  113. bits = ctx->key_len * 8;
  114. cmll_t4_set_key(key, bits, &dat->ks);
  115. if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
  116. && !enc) {
  117. ret = 0;
  118. dat->block = (block128_f) cmll_t4_decrypt;
  119. switch (bits) {
  120. case 128:
  121. dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
  122. (cbc128_f) cmll128_t4_cbc_decrypt : NULL;
  123. break;
  124. case 192:
  125. case 256:
  126. dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
  127. (cbc128_f) cmll256_t4_cbc_decrypt : NULL;
  128. break;
  129. default:
  130. ret = -1;
  131. }
  132. } else {
  133. ret = 0;
  134. dat->block = (block128_f) cmll_t4_encrypt;
  135. switch (bits) {
  136. case 128:
  137. if (mode == EVP_CIPH_CBC_MODE)
  138. dat->stream.cbc = (cbc128_f) cmll128_t4_cbc_encrypt;
  139. else if (mode == EVP_CIPH_CTR_MODE)
  140. dat->stream.ctr = (ctr128_f) cmll128_t4_ctr32_encrypt;
  141. else
  142. dat->stream.cbc = NULL;
  143. break;
  144. case 192:
  145. case 256:
  146. if (mode == EVP_CIPH_CBC_MODE)
  147. dat->stream.cbc = (cbc128_f) cmll256_t4_cbc_encrypt;
  148. else if (mode == EVP_CIPH_CTR_MODE)
  149. dat->stream.ctr = (ctr128_f) cmll256_t4_ctr32_encrypt;
  150. else
  151. dat->stream.cbc = NULL;
  152. break;
  153. default:
  154. ret = -1;
  155. }
  156. }
  157. if (ret < 0) {
  158. EVPerr(EVP_F_CMLL_T4_INIT_KEY, EVP_R_CAMELLIA_KEY_SETUP_FAILED);
  159. return 0;
  160. }
  161. return 1;
  162. }
  163. # define cmll_t4_cbc_cipher camellia_cbc_cipher
  164. static int cmll_t4_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  165. const unsigned char *in, size_t len);
  166. # define cmll_t4_ecb_cipher camellia_ecb_cipher
  167. static int cmll_t4_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  168. const unsigned char *in, size_t len);
  169. # define cmll_t4_ofb_cipher camellia_ofb_cipher
  170. static int cmll_t4_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  171. const unsigned char *in, size_t len);
  172. # define cmll_t4_cfb_cipher camellia_cfb_cipher
  173. static int cmll_t4_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  174. const unsigned char *in, size_t len);
  175. # define cmll_t4_cfb8_cipher camellia_cfb8_cipher
  176. static int cmll_t4_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  177. const unsigned char *in, size_t len);
  178. # define cmll_t4_cfb1_cipher camellia_cfb1_cipher
  179. static int cmll_t4_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  180. const unsigned char *in, size_t len);
  181. # define cmll_t4_ctr_cipher camellia_ctr_cipher
  182. static int cmll_t4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  183. const unsigned char *in, size_t len);
  184. # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
  185. static const EVP_CIPHER cmll_t4_##keylen##_##mode = { \
  186. nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
  187. flags|EVP_CIPH_##MODE##_MODE, \
  188. cmll_t4_init_key, \
  189. cmll_t4_##mode##_cipher, \
  190. NULL, \
  191. sizeof(EVP_CAMELLIA_KEY), \
  192. NULL,NULL,NULL,NULL }; \
  193. static const EVP_CIPHER camellia_##keylen##_##mode = { \
  194. nid##_##keylen##_##nmode,blocksize, \
  195. keylen/8,ivlen, \
  196. flags|EVP_CIPH_##MODE##_MODE, \
  197. camellia_init_key, \
  198. camellia_##mode##_cipher, \
  199. NULL, \
  200. sizeof(EVP_CAMELLIA_KEY), \
  201. NULL,NULL,NULL,NULL }; \
  202. const EVP_CIPHER *EVP_camellia_##keylen##_##mode(void) \
  203. { return SPARC_CMLL_CAPABLE?&cmll_t4_##keylen##_##mode:&camellia_##keylen##_##mode; }
  204. # else
  205. # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
  206. static const EVP_CIPHER camellia_##keylen##_##mode = { \
  207. nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
  208. flags|EVP_CIPH_##MODE##_MODE, \
  209. camellia_init_key, \
  210. camellia_##mode##_cipher, \
  211. NULL, \
  212. sizeof(EVP_CAMELLIA_KEY), \
  213. NULL,NULL,NULL,NULL }; \
  214. const EVP_CIPHER *EVP_camellia_##keylen##_##mode(void) \
  215. { return &camellia_##keylen##_##mode; }
  216. # endif
  217. # define BLOCK_CIPHER_generic_pack(nid,keylen,flags) \
  218. BLOCK_CIPHER_generic(nid,keylen,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
  219. BLOCK_CIPHER_generic(nid,keylen,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
  220. BLOCK_CIPHER_generic(nid,keylen,1,16,ofb128,ofb,OFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
  221. BLOCK_CIPHER_generic(nid,keylen,1,16,cfb128,cfb,CFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
  222. BLOCK_CIPHER_generic(nid,keylen,1,16,cfb1,cfb1,CFB,flags) \
  223. BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags)
  224. # if 0 /* not yet, missing NID */
  225. BLOCK_CIPHER_generic(nid, keylen, 1, 16, ctr, ctr, CTR, flags)
  226. # endif
  227. /* The subkey for Camellia is generated. */
  228. static int camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  229. const unsigned char *iv, int enc)
  230. {
  231. int ret, mode;
  232. EVP_CAMELLIA_KEY *dat = (EVP_CAMELLIA_KEY *) ctx->cipher_data;
  233. ret = Camellia_set_key(key, ctx->key_len * 8, &dat->ks);
  234. if (ret < 0) {
  235. EVPerr(EVP_F_CAMELLIA_INIT_KEY, EVP_R_CAMELLIA_KEY_SETUP_FAILED);
  236. return 0;
  237. }
  238. mode = ctx->cipher->flags & EVP_CIPH_MODE;
  239. if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
  240. && !enc) {
  241. dat->block = (block128_f) Camellia_decrypt;
  242. dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
  243. (cbc128_f) Camellia_cbc_encrypt : NULL;
  244. } else {
  245. dat->block = (block128_f) Camellia_encrypt;
  246. dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
  247. (cbc128_f) Camellia_cbc_encrypt : NULL;
  248. }
  249. return 1;
  250. }
  251. static int camellia_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  252. const unsigned char *in, size_t len)
  253. {
  254. EVP_CAMELLIA_KEY *dat = (EVP_CAMELLIA_KEY *) ctx->cipher_data;
  255. if (dat->stream.cbc)
  256. (*dat->stream.cbc) (in, out, len, &dat->ks, ctx->iv, ctx->encrypt);
  257. else if (ctx->encrypt)
  258. CRYPTO_cbc128_encrypt(in, out, len, &dat->ks, ctx->iv, dat->block);
  259. else
  260. CRYPTO_cbc128_decrypt(in, out, len, &dat->ks, ctx->iv, dat->block);
  261. return 1;
  262. }
  263. static int camellia_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  264. const unsigned char *in, size_t len)
  265. {
  266. size_t bl = ctx->cipher->block_size;
  267. size_t i;
  268. EVP_CAMELLIA_KEY *dat = (EVP_CAMELLIA_KEY *) ctx->cipher_data;
  269. if (len < bl)
  270. return 1;
  271. for (i = 0, len -= bl; i <= len; i += bl)
  272. (*dat->block) (in + i, out + i, &dat->ks);
  273. return 1;
  274. }
  275. static int camellia_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  276. const unsigned char *in, size_t len)
  277. {
  278. EVP_CAMELLIA_KEY *dat = (EVP_CAMELLIA_KEY *) ctx->cipher_data;
  279. CRYPTO_ofb128_encrypt(in, out, len, &dat->ks,
  280. ctx->iv, &ctx->num, dat->block);
  281. return 1;
  282. }
  283. static int camellia_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  284. const unsigned char *in, size_t len)
  285. {
  286. EVP_CAMELLIA_KEY *dat = (EVP_CAMELLIA_KEY *) ctx->cipher_data;
  287. CRYPTO_cfb128_encrypt(in, out, len, &dat->ks,
  288. ctx->iv, &ctx->num, ctx->encrypt, dat->block);
  289. return 1;
  290. }
  291. static int camellia_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  292. const unsigned char *in, size_t len)
  293. {
  294. EVP_CAMELLIA_KEY *dat = (EVP_CAMELLIA_KEY *) ctx->cipher_data;
  295. CRYPTO_cfb128_8_encrypt(in, out, len, &dat->ks,
  296. ctx->iv, &ctx->num, ctx->encrypt, dat->block);
  297. return 1;
  298. }
  299. static int camellia_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  300. const unsigned char *in, size_t len)
  301. {
  302. EVP_CAMELLIA_KEY *dat = (EVP_CAMELLIA_KEY *) ctx->cipher_data;
  303. if (ctx->flags & EVP_CIPH_FLAG_LENGTH_BITS) {
  304. CRYPTO_cfb128_1_encrypt(in, out, len, &dat->ks,
  305. ctx->iv, &ctx->num, ctx->encrypt, dat->block);
  306. return 1;
  307. }
  308. while (len >= MAXBITCHUNK) {
  309. CRYPTO_cfb128_1_encrypt(in, out, MAXBITCHUNK * 8, &dat->ks,
  310. ctx->iv, &ctx->num, ctx->encrypt, dat->block);
  311. len -= MAXBITCHUNK;
  312. }
  313. if (len)
  314. CRYPTO_cfb128_1_encrypt(in, out, len * 8, &dat->ks,
  315. ctx->iv, &ctx->num, ctx->encrypt, dat->block);
  316. return 1;
  317. }
  318. # if 0 /* not yet, missing NID */
  319. static int camellia_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  320. const unsigned char *in, size_t len)
  321. {
  322. unsigned int num = ctx->num;
  323. EVP_CAMELLIA_KEY *dat = (EVP_CAMELLIA_KEY *) ctx->cipher_data;
  324. if (dat->stream.ctr)
  325. CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks,
  326. ctx->iv, ctx->buf, &num, dat->stream.ctr);
  327. else
  328. CRYPTO_ctr128_encrypt(in, out, len, &dat->ks,
  329. ctx->iv, ctx->buf, &num, dat->block);
  330. ctx->num = (size_t)num;
  331. return 1;
  332. }
  333. # endif
  334. BLOCK_CIPHER_generic_pack(NID_camellia, 128, 0)
  335. BLOCK_CIPHER_generic_pack(NID_camellia, 192, 0)
  336. BLOCK_CIPHER_generic_pack(NID_camellia, 256, 0)
  337. #else
  338. # ifdef PEDANTIC
  339. static void *dummy = &dummy;
  340. # endif
  341. #endif