e_camellia.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright 2006-2020 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * Camellia low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <openssl/opensslconf.h>
  15. #include <openssl/evp.h>
  16. #include <openssl/err.h>
  17. #include <string.h>
  18. #include <assert.h>
  19. #include <openssl/camellia.h>
  20. #include "crypto/evp.h"
  21. #include "crypto/modes.h"
  22. #include "crypto/cmll_platform.h"
  23. static int camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  24. const unsigned char *iv, int enc);
  25. /* Camellia subkey Structure */
  26. typedef struct {
  27. CAMELLIA_KEY ks;
  28. block128_f block;
  29. union {
  30. cbc128_f cbc;
  31. ctr128_f ctr;
  32. } stream;
  33. } EVP_CAMELLIA_KEY;
  34. #define MAXBITCHUNK ((size_t)1<<(sizeof(size_t)*8-4))
  35. /* Attribute operation for Camellia */
  36. #define data(ctx) EVP_C_DATA(EVP_CAMELLIA_KEY,ctx)
  37. #if defined(AES_ASM) && (defined(__sparc) || defined(__sparc__))
  38. /* ---------^^^ this is not a typo, just a way to detect that
  39. * assembler support was in general requested... */
  40. # include "sparc_arch.h"
  41. static int cmll_t4_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  42. const unsigned char *iv, int enc)
  43. {
  44. int ret, mode, bits;
  45. EVP_CAMELLIA_KEY *dat =
  46. (EVP_CAMELLIA_KEY *)EVP_CIPHER_CTX_get_cipher_data(ctx);
  47. mode = EVP_CIPHER_CTX_mode(ctx);
  48. bits = EVP_CIPHER_CTX_key_length(ctx) * 8;
  49. cmll_t4_set_key(key, bits, &dat->ks);
  50. if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
  51. && !enc) {
  52. ret = 0;
  53. dat->block = (block128_f) cmll_t4_decrypt;
  54. switch (bits) {
  55. case 128:
  56. dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
  57. (cbc128_f) cmll128_t4_cbc_decrypt : NULL;
  58. break;
  59. case 192:
  60. case 256:
  61. dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
  62. (cbc128_f) cmll256_t4_cbc_decrypt : NULL;
  63. break;
  64. default:
  65. ret = -1;
  66. }
  67. } else {
  68. ret = 0;
  69. dat->block = (block128_f) cmll_t4_encrypt;
  70. switch (bits) {
  71. case 128:
  72. if (mode == EVP_CIPH_CBC_MODE)
  73. dat->stream.cbc = (cbc128_f) cmll128_t4_cbc_encrypt;
  74. else if (mode == EVP_CIPH_CTR_MODE)
  75. dat->stream.ctr = (ctr128_f) cmll128_t4_ctr32_encrypt;
  76. else
  77. dat->stream.cbc = NULL;
  78. break;
  79. case 192:
  80. case 256:
  81. if (mode == EVP_CIPH_CBC_MODE)
  82. dat->stream.cbc = (cbc128_f) cmll256_t4_cbc_encrypt;
  83. else if (mode == EVP_CIPH_CTR_MODE)
  84. dat->stream.ctr = (ctr128_f) cmll256_t4_ctr32_encrypt;
  85. else
  86. dat->stream.cbc = NULL;
  87. break;
  88. default:
  89. ret = -1;
  90. }
  91. }
  92. if (ret < 0) {
  93. EVPerr(EVP_F_CMLL_T4_INIT_KEY, EVP_R_CAMELLIA_KEY_SETUP_FAILED);
  94. return 0;
  95. }
  96. return 1;
  97. }
  98. # define cmll_t4_cbc_cipher camellia_cbc_cipher
  99. static int cmll_t4_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  100. const unsigned char *in, size_t len);
  101. # define cmll_t4_ecb_cipher camellia_ecb_cipher
  102. static int cmll_t4_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  103. const unsigned char *in, size_t len);
  104. # define cmll_t4_ofb_cipher camellia_ofb_cipher
  105. static int cmll_t4_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  106. const unsigned char *in, size_t len);
  107. # define cmll_t4_cfb_cipher camellia_cfb_cipher
  108. static int cmll_t4_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  109. const unsigned char *in, size_t len);
  110. # define cmll_t4_cfb8_cipher camellia_cfb8_cipher
  111. static int cmll_t4_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  112. const unsigned char *in, size_t len);
  113. # define cmll_t4_cfb1_cipher camellia_cfb1_cipher
  114. static int cmll_t4_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  115. const unsigned char *in, size_t len);
  116. # define cmll_t4_ctr_cipher camellia_ctr_cipher
  117. static int cmll_t4_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  118. const unsigned char *in, size_t len);
  119. # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
  120. static const EVP_CIPHER cmll_t4_##keylen##_##mode = { \
  121. nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
  122. flags|EVP_CIPH_##MODE##_MODE, \
  123. cmll_t4_init_key, \
  124. cmll_t4_##mode##_cipher, \
  125. NULL, \
  126. sizeof(EVP_CAMELLIA_KEY), \
  127. NULL,NULL,NULL,NULL }; \
  128. static const EVP_CIPHER camellia_##keylen##_##mode = { \
  129. nid##_##keylen##_##nmode,blocksize, \
  130. keylen/8,ivlen, \
  131. flags|EVP_CIPH_##MODE##_MODE, \
  132. camellia_init_key, \
  133. camellia_##mode##_cipher, \
  134. NULL, \
  135. sizeof(EVP_CAMELLIA_KEY), \
  136. NULL,NULL,NULL,NULL }; \
  137. const EVP_CIPHER *EVP_camellia_##keylen##_##mode(void) \
  138. { return SPARC_CMLL_CAPABLE?&cmll_t4_##keylen##_##mode:&camellia_##keylen##_##mode; }
  139. #else
  140. # define BLOCK_CIPHER_generic(nid,keylen,blocksize,ivlen,nmode,mode,MODE,flags) \
  141. static const EVP_CIPHER camellia_##keylen##_##mode = { \
  142. nid##_##keylen##_##nmode,blocksize,keylen/8,ivlen, \
  143. flags|EVP_CIPH_##MODE##_MODE, \
  144. camellia_init_key, \
  145. camellia_##mode##_cipher, \
  146. NULL, \
  147. sizeof(EVP_CAMELLIA_KEY), \
  148. NULL,NULL,NULL,NULL }; \
  149. const EVP_CIPHER *EVP_camellia_##keylen##_##mode(void) \
  150. { return &camellia_##keylen##_##mode; }
  151. #endif
  152. #define BLOCK_CIPHER_generic_pack(nid,keylen,flags) \
  153. BLOCK_CIPHER_generic(nid,keylen,16,16,cbc,cbc,CBC,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
  154. BLOCK_CIPHER_generic(nid,keylen,16,0,ecb,ecb,ECB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
  155. BLOCK_CIPHER_generic(nid,keylen,1,16,ofb128,ofb,OFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
  156. BLOCK_CIPHER_generic(nid,keylen,1,16,cfb128,cfb,CFB,flags|EVP_CIPH_FLAG_DEFAULT_ASN1) \
  157. BLOCK_CIPHER_generic(nid,keylen,1,16,cfb1,cfb1,CFB,flags) \
  158. BLOCK_CIPHER_generic(nid,keylen,1,16,cfb8,cfb8,CFB,flags) \
  159. BLOCK_CIPHER_generic(nid, keylen, 1, 16, ctr, ctr, CTR, flags)
  160. /* The subkey for Camellia is generated. */
  161. static int camellia_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
  162. const unsigned char *iv, int enc)
  163. {
  164. int ret, mode;
  165. EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx);
  166. ret = Camellia_set_key(key, EVP_CIPHER_CTX_key_length(ctx) * 8, &dat->ks);
  167. if (ret < 0) {
  168. EVPerr(EVP_F_CAMELLIA_INIT_KEY, EVP_R_CAMELLIA_KEY_SETUP_FAILED);
  169. return 0;
  170. }
  171. mode = EVP_CIPHER_CTX_mode(ctx);
  172. if ((mode == EVP_CIPH_ECB_MODE || mode == EVP_CIPH_CBC_MODE)
  173. && !enc) {
  174. dat->block = (block128_f) Camellia_decrypt;
  175. dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
  176. (cbc128_f) Camellia_cbc_encrypt : NULL;
  177. } else {
  178. dat->block = (block128_f) Camellia_encrypt;
  179. dat->stream.cbc = mode == EVP_CIPH_CBC_MODE ?
  180. (cbc128_f) Camellia_cbc_encrypt : NULL;
  181. }
  182. return 1;
  183. }
  184. static int camellia_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  185. const unsigned char *in, size_t len)
  186. {
  187. EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx);
  188. if (dat->stream.cbc)
  189. (*dat->stream.cbc) (in, out, len, &dat->ks,
  190. EVP_CIPHER_CTX_iv_noconst(ctx),
  191. EVP_CIPHER_CTX_encrypting(ctx));
  192. else if (EVP_CIPHER_CTX_encrypting(ctx))
  193. CRYPTO_cbc128_encrypt(in, out, len, &dat->ks,
  194. EVP_CIPHER_CTX_iv_noconst(ctx), dat->block);
  195. else
  196. CRYPTO_cbc128_decrypt(in, out, len, &dat->ks,
  197. EVP_CIPHER_CTX_iv_noconst(ctx), dat->block);
  198. return 1;
  199. }
  200. static int camellia_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  201. const unsigned char *in, size_t len)
  202. {
  203. size_t bl = EVP_CIPHER_CTX_block_size(ctx);
  204. size_t i;
  205. EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx);
  206. if (len < bl)
  207. return 1;
  208. for (i = 0, len -= bl; i <= len; i += bl)
  209. (*dat->block) (in + i, out + i, &dat->ks);
  210. return 1;
  211. }
  212. static int camellia_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  213. const unsigned char *in, size_t len)
  214. {
  215. EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx);
  216. int num = EVP_CIPHER_CTX_num(ctx);
  217. CRYPTO_ofb128_encrypt(in, out, len, &dat->ks,
  218. EVP_CIPHER_CTX_iv_noconst(ctx), &num, dat->block);
  219. EVP_CIPHER_CTX_set_num(ctx, num);
  220. return 1;
  221. }
  222. static int camellia_cfb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  223. const unsigned char *in, size_t len)
  224. {
  225. EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx);
  226. int num = EVP_CIPHER_CTX_num(ctx);
  227. CRYPTO_cfb128_encrypt(in, out, len, &dat->ks,
  228. EVP_CIPHER_CTX_iv_noconst(ctx), &num, EVP_CIPHER_CTX_encrypting(ctx), dat->block);
  229. EVP_CIPHER_CTX_set_num(ctx, num);
  230. return 1;
  231. }
  232. static int camellia_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  233. const unsigned char *in, size_t len)
  234. {
  235. EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx);
  236. int num = EVP_CIPHER_CTX_num(ctx);
  237. CRYPTO_cfb128_8_encrypt(in, out, len, &dat->ks,
  238. EVP_CIPHER_CTX_iv_noconst(ctx), &num, EVP_CIPHER_CTX_encrypting(ctx), dat->block);
  239. EVP_CIPHER_CTX_set_num(ctx, num);
  240. return 1;
  241. }
  242. static int camellia_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  243. const unsigned char *in, size_t len)
  244. {
  245. EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx);
  246. if (EVP_CIPHER_CTX_test_flags(ctx, EVP_CIPH_FLAG_LENGTH_BITS)) {
  247. int num = EVP_CIPHER_CTX_num(ctx);
  248. CRYPTO_cfb128_1_encrypt(in, out, len, &dat->ks,
  249. EVP_CIPHER_CTX_iv_noconst(ctx), &num, EVP_CIPHER_CTX_encrypting(ctx), dat->block);
  250. EVP_CIPHER_CTX_set_num(ctx, num);
  251. return 1;
  252. }
  253. while (len >= MAXBITCHUNK) {
  254. int num = EVP_CIPHER_CTX_num(ctx);
  255. CRYPTO_cfb128_1_encrypt(in, out, MAXBITCHUNK * 8, &dat->ks,
  256. EVP_CIPHER_CTX_iv_noconst(ctx), &num, EVP_CIPHER_CTX_encrypting(ctx), dat->block);
  257. EVP_CIPHER_CTX_set_num(ctx, num);
  258. len -= MAXBITCHUNK;
  259. out += MAXBITCHUNK;
  260. in += MAXBITCHUNK;
  261. }
  262. if (len) {
  263. int num = EVP_CIPHER_CTX_num(ctx);
  264. CRYPTO_cfb128_1_encrypt(in, out, len * 8, &dat->ks,
  265. EVP_CIPHER_CTX_iv_noconst(ctx), &num, EVP_CIPHER_CTX_encrypting(ctx), dat->block);
  266. EVP_CIPHER_CTX_set_num(ctx, num);
  267. }
  268. return 1;
  269. }
  270. static int camellia_ctr_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
  271. const unsigned char *in, size_t len)
  272. {
  273. unsigned int num = EVP_CIPHER_CTX_num(ctx);
  274. EVP_CAMELLIA_KEY *dat = EVP_C_DATA(EVP_CAMELLIA_KEY,ctx);
  275. if (dat->stream.ctr)
  276. CRYPTO_ctr128_encrypt_ctr32(in, out, len, &dat->ks,
  277. EVP_CIPHER_CTX_iv_noconst(ctx),
  278. EVP_CIPHER_CTX_buf_noconst(ctx), &num,
  279. dat->stream.ctr);
  280. else
  281. CRYPTO_ctr128_encrypt(in, out, len, &dat->ks,
  282. EVP_CIPHER_CTX_iv_noconst(ctx),
  283. EVP_CIPHER_CTX_buf_noconst(ctx), &num,
  284. dat->block);
  285. EVP_CIPHER_CTX_set_num(ctx, num);
  286. return 1;
  287. }
  288. BLOCK_CIPHER_generic_pack(NID_camellia, 128, 0)
  289. BLOCK_CIPHER_generic_pack(NID_camellia, 192, 0)
  290. BLOCK_CIPHER_generic_pack(NID_camellia, 256, 0)