camellia_glue.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. if (encrypt)
  79. key = &ctx->encrypt_key[0];
  80. else
  81. key = &ctx->decrypt_key[0];
  82. camellia_sparc64_load_keys(key, ctx->key_len);
  83. while ((nbytes = walk.nbytes)) {
  84. unsigned int block_len = nbytes & CAMELLIA_BLOCK_MASK;
  85. if (likely(block_len)) {
  86. const u64 *src64;
  87. u64 *dst64;
  88. src64 = (const u64 *)walk.src.virt.addr;
  89. dst64 = (u64 *) walk.dst.virt.addr;
  90. op(src64, dst64, block_len, key);
  91. }
  92. nbytes &= CAMELLIA_BLOCK_SIZE - 1;
  93. err = blkcipher_walk_done(desc, &walk, nbytes);
  94. }
  95. fprs_write(0);
  96. return err;
  97. }
  98. static int ecb_encrypt(struct blkcipher_desc *desc,
  99. struct scatterlist *dst, struct scatterlist *src,
  100. unsigned int nbytes)
  101. {
  102. return __ecb_crypt(desc, dst, src, nbytes, true);
  103. }
  104. static int ecb_decrypt(struct blkcipher_desc *desc,
  105. struct scatterlist *dst, struct scatterlist *src,
  106. unsigned int nbytes)
  107. {
  108. return __ecb_crypt(desc, dst, src, nbytes, false);
  109. }
  110. typedef void cbc_crypt_op(const u64 *input, u64 *output, unsigned int len,
  111. const u64 *key, u64 *iv);
  112. extern cbc_crypt_op camellia_sparc64_cbc_encrypt_3_grand_rounds;
  113. extern cbc_crypt_op camellia_sparc64_cbc_encrypt_4_grand_rounds;
  114. extern cbc_crypt_op camellia_sparc64_cbc_decrypt_3_grand_rounds;
  115. extern cbc_crypt_op camellia_sparc64_cbc_decrypt_4_grand_rounds;
  116. static int cbc_encrypt(struct blkcipher_desc *desc,
  117. struct scatterlist *dst, struct scatterlist *src,
  118. unsigned int nbytes)
  119. {
  120. struct camellia_sparc64_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  121. struct blkcipher_walk walk;
  122. cbc_crypt_op *op;
  123. const u64 *key;
  124. int err;
  125. op = camellia_sparc64_cbc_encrypt_3_grand_rounds;
  126. if (ctx->key_len != 16)
  127. op = camellia_sparc64_cbc_encrypt_4_grand_rounds;
  128. blkcipher_walk_init(&walk, dst, src, nbytes);
  129. err = blkcipher_walk_virt(desc, &walk);
  130. key = &ctx->encrypt_key[0];
  131. camellia_sparc64_load_keys(key, ctx->key_len);
  132. while ((nbytes = walk.nbytes)) {
  133. unsigned int block_len = nbytes & CAMELLIA_BLOCK_MASK;
  134. if (likely(block_len)) {
  135. const u64 *src64;
  136. u64 *dst64;
  137. src64 = (const u64 *)walk.src.virt.addr;
  138. dst64 = (u64 *) walk.dst.virt.addr;
  139. op(src64, dst64, block_len, key,
  140. (u64 *) walk.iv);
  141. }
  142. nbytes &= CAMELLIA_BLOCK_SIZE - 1;
  143. err = blkcipher_walk_done(desc, &walk, nbytes);
  144. }
  145. fprs_write(0);
  146. return err;
  147. }
  148. static int cbc_decrypt(struct blkcipher_desc *desc,
  149. struct scatterlist *dst, struct scatterlist *src,
  150. unsigned int nbytes)
  151. {
  152. struct camellia_sparc64_ctx *ctx = crypto_blkcipher_ctx(desc->tfm);
  153. struct blkcipher_walk walk;
  154. cbc_crypt_op *op;
  155. const u64 *key;
  156. int err;
  157. op = camellia_sparc64_cbc_decrypt_3_grand_rounds;
  158. if (ctx->key_len != 16)
  159. op = camellia_sparc64_cbc_decrypt_4_grand_rounds;
  160. blkcipher_walk_init(&walk, dst, src, nbytes);
  161. err = blkcipher_walk_virt(desc, &walk);
  162. key = &ctx->decrypt_key[0];
  163. camellia_sparc64_load_keys(key, ctx->key_len);
  164. while ((nbytes = walk.nbytes)) {
  165. unsigned int block_len = nbytes & CAMELLIA_BLOCK_MASK;
  166. if (likely(block_len)) {
  167. const u64 *src64;
  168. u64 *dst64;
  169. src64 = (const u64 *)walk.src.virt.addr;
  170. dst64 = (u64 *) walk.dst.virt.addr;
  171. op(src64, dst64, block_len, key,
  172. (u64 *) walk.iv);
  173. }
  174. nbytes &= CAMELLIA_BLOCK_SIZE - 1;
  175. err = blkcipher_walk_done(desc, &walk, nbytes);
  176. }
  177. fprs_write(0);
  178. return err;
  179. }
  180. static struct crypto_alg algs[] = { {
  181. .cra_name = "camellia",
  182. .cra_driver_name = "camellia-sparc64",
  183. .cra_priority = SPARC_CR_OPCODE_PRIORITY,
  184. .cra_flags = CRYPTO_ALG_TYPE_CIPHER,
  185. .cra_blocksize = CAMELLIA_BLOCK_SIZE,
  186. .cra_ctxsize = sizeof(struct camellia_sparc64_ctx),
  187. .cra_alignmask = 3,
  188. .cra_module = THIS_MODULE,
  189. .cra_u = {
  190. .cipher = {
  191. .cia_min_keysize = CAMELLIA_MIN_KEY_SIZE,
  192. .cia_max_keysize = CAMELLIA_MAX_KEY_SIZE,
  193. .cia_setkey = camellia_set_key,
  194. .cia_encrypt = camellia_encrypt,
  195. .cia_decrypt = camellia_decrypt
  196. }
  197. }
  198. }, {
  199. .cra_name = "ecb(camellia)",
  200. .cra_driver_name = "ecb-camellia-sparc64",
  201. .cra_priority = SPARC_CR_OPCODE_PRIORITY,
  202. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  203. .cra_blocksize = CAMELLIA_BLOCK_SIZE,
  204. .cra_ctxsize = sizeof(struct camellia_sparc64_ctx),
  205. .cra_alignmask = 7,
  206. .cra_type = &crypto_blkcipher_type,
  207. .cra_module = THIS_MODULE,
  208. .cra_u = {
  209. .blkcipher = {
  210. .min_keysize = CAMELLIA_MIN_KEY_SIZE,
  211. .max_keysize = CAMELLIA_MAX_KEY_SIZE,
  212. .setkey = camellia_set_key,
  213. .encrypt = ecb_encrypt,
  214. .decrypt = ecb_decrypt,
  215. },
  216. },
  217. }, {
  218. .cra_name = "cbc(camellia)",
  219. .cra_driver_name = "cbc-camellia-sparc64",
  220. .cra_priority = SPARC_CR_OPCODE_PRIORITY,
  221. .cra_flags = CRYPTO_ALG_TYPE_BLKCIPHER,
  222. .cra_blocksize = CAMELLIA_BLOCK_SIZE,
  223. .cra_ctxsize = sizeof(struct camellia_sparc64_ctx),
  224. .cra_alignmask = 7,
  225. .cra_type = &crypto_blkcipher_type,
  226. .cra_module = THIS_MODULE,
  227. .cra_u = {
  228. .blkcipher = {
  229. .min_keysize = CAMELLIA_MIN_KEY_SIZE,
  230. .max_keysize = CAMELLIA_MAX_KEY_SIZE,
  231. .setkey = camellia_set_key,
  232. .encrypt = cbc_encrypt,
  233. .decrypt = cbc_decrypt,
  234. },
  235. },
  236. }
  237. };
  238. static bool __init sparc64_has_camellia_opcode(void)
  239. {
  240. unsigned long cfr;
  241. if (!(sparc64_elf_hwcap & HWCAP_SPARC_CRYPTO))
  242. return false;
  243. __asm__ __volatile__("rd %%asr26, %0" : "=r" (cfr));
  244. if (!(cfr & CFR_CAMELLIA))
  245. return false;
  246. return true;
  247. }
  248. static int __init camellia_sparc64_mod_init(void)
  249. {
  250. int i;
  251. for (i = 0; i < ARRAY_SIZE(algs); i++)
  252. INIT_LIST_HEAD(&algs[i].cra_list);
  253. if (sparc64_has_camellia_opcode()) {
  254. pr_info("Using sparc64 camellia opcodes optimized CAMELLIA implementation\n");
  255. return crypto_register_algs(algs, ARRAY_SIZE(algs));
  256. }
  257. pr_info("sparc64 camellia opcodes not available.\n");
  258. return -ENODEV;
  259. }
  260. static void __exit camellia_sparc64_mod_fini(void)
  261. {
  262. crypto_unregister_algs(algs, ARRAY_SIZE(algs));
  263. }
  264. module_init(camellia_sparc64_mod_init);
  265. module_exit(camellia_sparc64_mod_fini);
  266. MODULE_LICENSE("GPL");
  267. MODULE_DESCRIPTION("Camellia Cipher Algorithm, sparc64 camellia opcode accelerated");
  268. MODULE_ALIAS("aes");