camellia_glue.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /* Glue code for CAMELLIA encryption optimized for sparc64 crypto opcodes.
  2. *
  3. * Copyright (C) 2012 David S. Miller <davem@davemloft.net>
  4. */
  5. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6. #include <linux/crypto.h>
  7. #include <linux/init.h>
  8. #include <linux/module.h>
  9. #include <linux/mm.h>
  10. #include <linux/types.h>
  11. #include <crypto/algapi.h>
  12. #include <asm/fpumacro.h>
  13. #include <asm/pstate.h>
  14. #include <asm/elf.h>
  15. #include "opcodes.h"
  16. #define CAMELLIA_MIN_KEY_SIZE 16
  17. #define CAMELLIA_MAX_KEY_SIZE 32
  18. #define CAMELLIA_BLOCK_SIZE 16
  19. #define CAMELLIA_TABLE_BYTE_LEN 272
  20. struct camellia_sparc64_ctx {
  21. u64 encrypt_key[CAMELLIA_TABLE_BYTE_LEN / sizeof(u64)];
  22. u64 decrypt_key[CAMELLIA_TABLE_BYTE_LEN / sizeof(u64)];
  23. int key_len;
  24. };
  25. extern void camellia_sparc64_key_expand(const u32 *in_key, u64 *encrypt_key,
  26. unsigned int key_len, u64 *decrypt_key);
  27. static int camellia_set_key(struct crypto_tfm *tfm, const u8 *_in_key,
  28. unsigned int key_len)
  29. {
  30. struct camellia_sparc64_ctx *ctx = crypto_tfm_ctx(tfm);
  31. const u32 *in_key = (const u32 *) _in_key;
  32. u32 *flags = &tfm->crt_flags;
  33. if (key_len != 16 && key_len != 24 && key_len != 32) {
  34. *flags |= CRYPTO_TFM_RES_BAD_KEY_LEN;
  35. return -EINVAL;
  36. }
  37. ctx->key_len = key_len;
  38. camellia_sparc64_key_expand(in_key, &ctx->encrypt_key[0],
  39. key_len, &ctx->decrypt_key[0]);
  40. return 0;
  41. }
  42. extern void camellia_sparc64_crypt(const u64 *key, const u32 *input,
  43. u32 *output, unsigned int key_len);
  44. static void camellia_encrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  45. {
  46. struct camellia_sparc64_ctx *ctx = crypto_tfm_ctx(tfm);
  47. camellia_sparc64_crypt(&ctx->encrypt_key[0],
  48. (const u32 *) src,
  49. (u32 *) dst, ctx->key_len);
  50. }
  51. static void camellia_decrypt(struct crypto_tfm *tfm, u8 *dst, const u8 *src)
  52. {
  53. struct camellia_sparc64_ctx *ctx = crypto_tfm_ctx(tfm);
  54. camellia_sparc64_crypt(&ctx->decrypt_key[0],
  55. (const u32 *) src,
  56. (u32 *) dst, ctx->key_len);
  57. }
  58. extern void camellia_sparc64_load_keys(const u64 *key, unsigned int key_len);
  59. typedef void ecb_crypt_op(const u64 *input, u64 *output, unsigned int len,
  60. const u64 *key);
  61. extern ecb_crypt_op camellia_sparc64_ecb_crypt_3_grand_rounds;
  62. extern ecb_crypt_op camellia_sparc64_ecb_crypt_4_grand_rounds;
  63. #define CAMELLIA_BLOCK_MASK (~(CAMELLIA_BLOCK_SIZE - 1))
  64. static int __ecb_crypt(struct blkcipher_desc *desc,
  65. struct scatterlist *dst, struct scatterlist *src,
  66. unsigned int nbytes, bool encrypt)
  67. {
  68. struct camellia_sparc64_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  69. struct blkcipher_walk walk;
  70. ecb_crypt_op *op;
  71. const u64 *key;
  72. int err;
  73. op = camellia_sparc64_ecb_crypt_3_grand_rounds;
  74. if (ctx->key_len != 16)
  75. op = camellia_sparc64_ecb_crypt_4_grand_rounds;
  76. blkcipher_walk_init(&walk, dst, src, nbytes);
  77. err = blkcipher_walk_virt(desc, &walk);
  78. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  79. if (encrypt)
  80. key = &ctx->encrypt_key[0];
  81. else
  82. key = &ctx->decrypt_key[0];
  83. camellia_sparc64_load_keys(key, ctx->key_len);
  84. while ((nbytes = walk.nbytes)) {
  85. unsigned int block_len = nbytes & CAMELLIA_BLOCK_MASK;
  86. if (likely(block_len)) {
  87. const u64 *src64;
  88. u64 *dst64;
  89. src64 = (const u64 *)walk.src.virt.addr;
  90. dst64 = (u64 *) walk.dst.virt.addr;
  91. op(src64, dst64, block_len, key);
  92. }
  93. nbytes &= CAMELLIA_BLOCK_SIZE - 1;
  94. err = blkcipher_walk_done(desc, &walk, nbytes);
  95. }
  96. fprs_write(0);
  97. return err;
  98. }
  99. static int ecb_encrypt(struct blkcipher_desc *desc,
  100. struct scatterlist *dst, struct scatterlist *src,
  101. unsigned int nbytes)
  102. {
  103. return __ecb_crypt(desc, dst, src, nbytes, true);
  104. }
  105. static int ecb_decrypt(struct blkcipher_desc *desc,
  106. struct scatterlist *dst, struct scatterlist *src,
  107. unsigned int nbytes)
  108. {
  109. return __ecb_crypt(desc, dst, src, nbytes, false);
  110. }
  111. typedef void cbc_crypt_op(const u64 *input, u64 *output, unsigned int len,
  112. const u64 *key, u64 *iv);
  113. extern cbc_crypt_op camellia_sparc64_cbc_encrypt_3_grand_rounds;
  114. extern cbc_crypt_op camellia_sparc64_cbc_encrypt_4_grand_rounds;
  115. extern cbc_crypt_op camellia_sparc64_cbc_decrypt_3_grand_rounds;
  116. extern cbc_crypt_op camellia_sparc64_cbc_decrypt_4_grand_rounds;
  117. static int cbc_encrypt(struct blkcipher_desc *desc,
  118. struct scatterlist *dst, struct scatterlist *src,
  119. unsigned int nbytes)
  120. {
  121. struct camellia_sparc64_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  122. struct blkcipher_walk walk;
  123. cbc_crypt_op *op;
  124. const u64 *key;
  125. int err;
  126. op = camellia_sparc64_cbc_encrypt_3_grand_rounds;
  127. if (ctx->key_len != 16)
  128. op = camellia_sparc64_cbc_encrypt_4_grand_rounds;
  129. blkcipher_walk_init(&walk, dst, src, nbytes);
  130. err = blkcipher_walk_virt(desc, &walk);
  131. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  132. key = &ctx->encrypt_key[0];
  133. camellia_sparc64_load_keys(key, ctx->key_len);
  134. while ((nbytes = walk.nbytes)) {
  135. unsigned int block_len = nbytes & CAMELLIA_BLOCK_MASK;
  136. if (likely(block_len)) {
  137. const u64 *src64;
  138. u64 *dst64;
  139. src64 = (const u64 *)walk.src.virt.addr;
  140. dst64 = (u64 *) walk.dst.virt.addr;
  141. op(src64, dst64, block_len, key,
  142. (u64 *) walk.iv);
  143. }
  144. nbytes &= CAMELLIA_BLOCK_SIZE - 1;
  145. err = blkcipher_walk_done(desc, &walk, nbytes);
  146. }
  147. fprs_write(0);
  148. return err;
  149. }
  150. static int cbc_decrypt(struct blkcipher_desc *desc,
  151. struct scatterlist *dst, struct scatterlist *src,
  152. unsigned int nbytes)
  153. {
  154. struct camellia_sparc64_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  155. struct blkcipher_walk walk;
  156. cbc_crypt_op *op;
  157. const u64 *key;
  158. int err;
  159. op = camellia_sparc64_cbc_decrypt_3_grand_rounds;
  160. if (ctx->key_len != 16)
  161. op = camellia_sparc64_cbc_decrypt_4_grand_rounds;
  162. blkcipher_walk_init(&walk, dst, src, nbytes);
  163. err = blkcipher_walk_virt(desc, &walk);
  164. desc->flags &= ~CRYPTO_TFM_REQ_MAY_SLEEP;
  165. key = &ctx->decrypt_key[0];
  166. camellia_sparc64_load_keys(key, ctx->key_len);
  167. while ((nbytes = walk.nbytes)) {
  168. unsigned int block_len = nbytes & CAMELLIA_BLOCK_MASK;
  169. if (likely(block_len)) {
  170. const u64 *src64;
  171. u64 *dst64;
  172. src64 = (const u64 *)walk.src.virt.addr;
  173. dst64 = (u64 *) walk.dst.virt.addr;
  174. op(src64, dst64, block_len, key,
  175. (u64 *) walk.iv);
  176. }
  177. nbytes &= CAMELLIA_BLOCK_SIZE - 1;
  178. err = blkcipher_walk_done(desc, &walk, nbytes);
  179. }
  180. fprs_write(0);
  181. return err;
  182. }
  183. static struct crypto_alg algs[] = { {
  184. .cra_name = "camellia",
  185. .cra_driver_name = "camellia-sparc64",
  186. .cra_priority = SPARC_CR_OPCODE_PRIORITY,
  187. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  188. .cra_blocksize = CAMELLIA_BLOCK_SIZE,
  189. .cra_ctxsize = sizeof(struct camellia_sparc64_ctx),
  190. .cra_alignmask = 3,
  191. .cra_module = THIS_MODULE,
  192. .cra_u = {
  193. .cipher = {
  194. .cia_min_keysize = CAMELLIA_MIN_KEY_SIZE,
  195. .cia_max_keysize = CAMELLIA_MAX_KEY_SIZE,
  196. .cia_setkey = camellia_set_key,
  197. .cia_encrypt = camellia_encrypt,
  198. .cia_decrypt = camellia_decrypt
  199. }
  200. }
  201. }, {
  202. .cra_name = "ecb(camellia)",
  203. .cra_driver_name = "ecb-camellia-sparc64",
  204. .cra_priority = SPARC_CR_OPCODE_PRIORITY,
  205. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  206. .cra_blocksize = CAMELLIA_BLOCK_SIZE,
  207. .cra_ctxsize = sizeof(struct camellia_sparc64_ctx),
  208. .cra_alignmask = 7,
  209. .cra_type = &crypto_blkcipher_type,
  210. .cra_module = THIS_MODULE,
  211. .cra_u = {
  212. .blkcipher = {
  213. .min_keysize = CAMELLIA_MIN_KEY_SIZE,
  214. .max_keysize = CAMELLIA_MAX_KEY_SIZE,
  215. .setkey = camellia_set_key,
  216. .encrypt = ecb_encrypt,
  217. .decrypt = ecb_decrypt,
  218. },
  219. },
  220. }, {
  221. .cra_name = "cbc(camellia)",
  222. .cra_driver_name = "cbc-camellia-sparc64",
  223. .cra_priority = SPARC_CR_OPCODE_PRIORITY,
  224. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  225. .cra_blocksize = CAMELLIA_BLOCK_SIZE,
  226. .cra_ctxsize = sizeof(struct camellia_sparc64_ctx),
  227. .cra_alignmask = 7,
  228. .cra_type = &crypto_blkcipher_type,
  229. .cra_module = THIS_MODULE,
  230. .cra_u = {
  231. .blkcipher = {
  232. .min_keysize = CAMELLIA_MIN_KEY_SIZE,
  233. .max_keysize = CAMELLIA_MAX_KEY_SIZE,
  234. .ivsize = CAMELLIA_BLOCK_SIZE,
  235. .setkey = camellia_set_key,
  236. .encrypt = cbc_encrypt,
  237. .decrypt = cbc_decrypt,
  238. },
  239. },
  240. }
  241. };
  242. static bool __init sparc64_has_camellia_opcode(void)
  243. {
  244. unsigned long cfr;
  245. if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
  246. return false;
  247. __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
  248. if (!(cfr & CFR_CAMELLIA))
  249. return false;
  250. return true;
  251. }
  252. static int __init camellia_sparc64_mod_init(void)
  253. {
  254. int i;
  255. for (i = 0; i < ARRAY_SIZE(algs); i++)
  256. INIT_LIST_HEAD(&algs[i].cra_list);
  257. if (sparc64_has_camellia_opcode()) {
  258. pr_info("Using sparc64 camellia opcodes optimized CAMELLIA implementation\n");
  259. return crypto_register_algs(algs, ARRAY_SIZE(algs));
  260. }
  261. pr_info("sparc64 camellia opcodes not available.\n");
  262. return -ENODEV;
  263. }
  264. static void __exit camellia_sparc64_mod_fini(void)
  265. {
  266. crypto_unregister_algs(algs, ARRAY_SIZE(algs));
  267. }
  268. module_init(camellia_sparc64_mod_init);
  269. module_exit(camellia_sparc64_mod_fini);
  270. MODULE_LICENSE("GPL");
  271. MODULE_DESCRIPTION("Camellia Cipher Algorithm, sparc64 camellia opcode accelerated");
  272. MODULE_ALIAS_CRYPTO("camellia");
  273. #include "crop_devid.c"