e_camellia.c 12 KB

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