morus1280_glue.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * The MORUS-1280 Authenticated-Encryption Algorithm
  3. * Common x86 SIMD glue skeleton
  4. *
  5. * Copyright (c) 2016-2018 Ondrej Mosnacek <omosnacek@gmail.com>
  6. * Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the Free
  10. * Software Foundation; either version 2 of the License, or (at your option)
  11. * any later version.
  12. */
  13. #include <crypto/cryptd.h>
  14. #include <crypto/internal/aead.h>
  15. #include <crypto/internal/skcipher.h>
  16. #include <crypto/morus1280_glue.h>
  17. #include <crypto/scatterwalk.h>
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/scatterlist.h>
  23. #include <asm/fpu/api.h>
  24. struct morus1280_state {
  25. struct morus1280_block s[MORUS_STATE_BLOCKS];
  26. };
  27. struct morus1280_ops {
  28. int (*skcipher_walk_init)(struct skcipher_walk *walk,
  29. struct aead_request *req, bool atomic);
  30. void (*crypt_blocks)(void *state, const void *src, void *dst,
  31. unsigned int length);
  32. void (*crypt_tail)(void *state, const void *src, void *dst,
  33. unsigned int length);
  34. };
  35. static void crypto_morus1280_glue_process_ad(
  36. struct morus1280_state *state,
  37. const struct morus1280_glue_ops *ops,
  38. struct scatterlist *sg_src, unsigned int assoclen)
  39. {
  40. struct scatter_walk walk;
  41. struct morus1280_block buf;
  42. unsigned int pos = 0;
  43. scatterwalk_start(&walk, sg_src);
  44. while (assoclen != 0) {
  45. unsigned int size = scatterwalk_clamp(&walk, assoclen);
  46. unsigned int left = size;
  47. void *mapped = scatterwalk_map(&walk);
  48. const u8 *src = (const u8 *)mapped;
  49. if (pos + size >= MORUS1280_BLOCK_SIZE) {
  50. if (pos > 0) {
  51. unsigned int fill = MORUS1280_BLOCK_SIZE - pos;
  52. memcpy(buf.bytes + pos, src, fill);
  53. ops->ad(state, buf.bytes, MORUS1280_BLOCK_SIZE);
  54. pos = 0;
  55. left -= fill;
  56. src += fill;
  57. }
  58. ops->ad(state, src, left);
  59. src += left & ~(MORUS1280_BLOCK_SIZE - 1);
  60. left &= MORUS1280_BLOCK_SIZE - 1;
  61. }
  62. memcpy(buf.bytes + pos, src, left);
  63. pos += left;
  64. assoclen -= size;
  65. scatterwalk_unmap(mapped);
  66. scatterwalk_advance(&walk, size);
  67. scatterwalk_done(&walk, 0, assoclen);
  68. }
  69. if (pos > 0) {
  70. memset(buf.bytes + pos, 0, MORUS1280_BLOCK_SIZE - pos);
  71. ops->ad(state, buf.bytes, MORUS1280_BLOCK_SIZE);
  72. }
  73. }
  74. static void crypto_morus1280_glue_process_crypt(struct morus1280_state *state,
  75. struct morus1280_ops ops,
  76. struct skcipher_walk *walk)
  77. {
  78. while (walk->nbytes >= MORUS1280_BLOCK_SIZE) {
  79. ops.crypt_blocks(state, walk->src.virt.addr,
  80. walk->dst.virt.addr,
  81. round_down(walk->nbytes,
  82. MORUS1280_BLOCK_SIZE));
  83. skcipher_walk_done(walk, walk->nbytes % MORUS1280_BLOCK_SIZE);
  84. }
  85. if (walk->nbytes) {
  86. ops.crypt_tail(state, walk->src.virt.addr, walk->dst.virt.addr,
  87. walk->nbytes);
  88. skcipher_walk_done(walk, 0);
  89. }
  90. }
  91. int crypto_morus1280_glue_setkey(struct crypto_aead *aead, const u8 *key,
  92. unsigned int keylen)
  93. {
  94. struct morus1280_ctx *ctx = crypto_aead_ctx(aead);
  95. if (keylen == MORUS1280_BLOCK_SIZE) {
  96. memcpy(ctx->key.bytes, key, MORUS1280_BLOCK_SIZE);
  97. } else if (keylen == MORUS1280_BLOCK_SIZE / 2) {
  98. memcpy(ctx->key.bytes, key, keylen);
  99. memcpy(ctx->key.bytes + keylen, key, keylen);
  100. } else {
  101. crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  102. return -EINVAL;
  103. }
  104. return 0;
  105. }
  106. EXPORT_SYMBOL_GPL(crypto_morus1280_glue_setkey);
  107. int crypto_morus1280_glue_setauthsize(struct crypto_aead *tfm,
  108. unsigned int authsize)
  109. {
  110. return (authsize <= MORUS_MAX_AUTH_SIZE) ? 0 : -EINVAL;
  111. }
  112. EXPORT_SYMBOL_GPL(crypto_morus1280_glue_setauthsize);
  113. static void crypto_morus1280_glue_crypt(struct aead_request *req,
  114. struct morus1280_ops ops,
  115. unsigned int cryptlen,
  116. struct morus1280_block *tag_xor)
  117. {
  118. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  119. struct morus1280_ctx *ctx = crypto_aead_ctx(tfm);
  120. struct morus1280_state state;
  121. struct skcipher_walk walk;
  122. ops.skcipher_walk_init(&walk, req, true);
  123. kernel_fpu_begin();
  124. ctx->ops->init(&state, &ctx->key, req->iv);
  125. crypto_morus1280_glue_process_ad(&state, ctx->ops, req->src, req->assoclen);
  126. crypto_morus1280_glue_process_crypt(&state, ops, &walk);
  127. ctx->ops->final(&state, tag_xor, req->assoclen, cryptlen);
  128. kernel_fpu_end();
  129. }
  130. int crypto_morus1280_glue_encrypt(struct aead_request *req)
  131. {
  132. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  133. struct morus1280_ctx *ctx = crypto_aead_ctx(tfm);
  134. struct morus1280_ops OPS = {
  135. .skcipher_walk_init = skcipher_walk_aead_encrypt,
  136. .crypt_blocks = ctx->ops->enc,
  137. .crypt_tail = ctx->ops->enc_tail,
  138. };
  139. struct morus1280_block tag = {};
  140. unsigned int authsize = crypto_aead_authsize(tfm);
  141. unsigned int cryptlen = req->cryptlen;
  142. crypto_morus1280_glue_crypt(req, OPS, cryptlen, &tag);
  143. scatterwalk_map_and_copy(tag.bytes, req->dst,
  144. req->assoclen + cryptlen, authsize, 1);
  145. return 0;
  146. }
  147. EXPORT_SYMBOL_GPL(crypto_morus1280_glue_encrypt);
  148. int crypto_morus1280_glue_decrypt(struct aead_request *req)
  149. {
  150. static const u8 zeros[MORUS1280_BLOCK_SIZE] = {};
  151. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  152. struct morus1280_ctx *ctx = crypto_aead_ctx(tfm);
  153. struct morus1280_ops OPS = {
  154. .skcipher_walk_init = skcipher_walk_aead_decrypt,
  155. .crypt_blocks = ctx->ops->dec,
  156. .crypt_tail = ctx->ops->dec_tail,
  157. };
  158. struct morus1280_block tag;
  159. unsigned int authsize = crypto_aead_authsize(tfm);
  160. unsigned int cryptlen = req->cryptlen - authsize;
  161. scatterwalk_map_and_copy(tag.bytes, req->src,
  162. req->assoclen + cryptlen, authsize, 0);
  163. crypto_morus1280_glue_crypt(req, OPS, cryptlen, &tag);
  164. return crypto_memneq(tag.bytes, zeros, authsize) ? -EBADMSG : 0;
  165. }
  166. EXPORT_SYMBOL_GPL(crypto_morus1280_glue_decrypt);
  167. void crypto_morus1280_glue_init_ops(struct crypto_aead *aead,
  168. const struct morus1280_glue_ops *ops)
  169. {
  170. struct morus1280_ctx *ctx = crypto_aead_ctx(aead);
  171. ctx->ops = ops;
  172. }
  173. EXPORT_SYMBOL_GPL(crypto_morus1280_glue_init_ops);
  174. int cryptd_morus1280_glue_setkey(struct crypto_aead *aead, const u8 *key,
  175. unsigned int keylen)
  176. {
  177. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  178. struct cryptd_aead *cryptd_tfm = *ctx;
  179. return crypto_aead_setkey(&cryptd_tfm->base, key, keylen);
  180. }
  181. EXPORT_SYMBOL_GPL(cryptd_morus1280_glue_setkey);
  182. int cryptd_morus1280_glue_setauthsize(struct crypto_aead *aead,
  183. unsigned int authsize)
  184. {
  185. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  186. struct cryptd_aead *cryptd_tfm = *ctx;
  187. return crypto_aead_setauthsize(&cryptd_tfm->base, authsize);
  188. }
  189. EXPORT_SYMBOL_GPL(cryptd_morus1280_glue_setauthsize);
  190. int cryptd_morus1280_glue_encrypt(struct aead_request *req)
  191. {
  192. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  193. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  194. struct cryptd_aead *cryptd_tfm = *ctx;
  195. aead = &cryptd_tfm->base;
  196. if (irq_fpu_usable() && (!in_atomic() ||
  197. !cryptd_aead_queued(cryptd_tfm)))
  198. aead = cryptd_aead_child(cryptd_tfm);
  199. aead_request_set_tfm(req, aead);
  200. return crypto_aead_encrypt(req);
  201. }
  202. EXPORT_SYMBOL_GPL(cryptd_morus1280_glue_encrypt);
  203. int cryptd_morus1280_glue_decrypt(struct aead_request *req)
  204. {
  205. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  206. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  207. struct cryptd_aead *cryptd_tfm = *ctx;
  208. aead = &cryptd_tfm->base;
  209. if (irq_fpu_usable() && (!in_atomic() ||
  210. !cryptd_aead_queued(cryptd_tfm)))
  211. aead = cryptd_aead_child(cryptd_tfm);
  212. aead_request_set_tfm(req, aead);
  213. return crypto_aead_decrypt(req);
  214. }
  215. EXPORT_SYMBOL_GPL(cryptd_morus1280_glue_decrypt);
  216. int cryptd_morus1280_glue_init_tfm(struct crypto_aead *aead)
  217. {
  218. struct cryptd_aead *cryptd_tfm;
  219. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  220. const char *name = crypto_aead_alg(aead)->base.cra_driver_name;
  221. char internal_name[CRYPTO_MAX_ALG_NAME];
  222. if (snprintf(internal_name, CRYPTO_MAX_ALG_NAME, "__%s", name)
  223. >= CRYPTO_MAX_ALG_NAME)
  224. return -ENAMETOOLONG;
  225. cryptd_tfm = cryptd_alloc_aead(internal_name, CRYPTO_ALG_INTERNAL,
  226. CRYPTO_ALG_INTERNAL);
  227. if (IS_ERR(cryptd_tfm))
  228. return PTR_ERR(cryptd_tfm);
  229. *ctx = cryptd_tfm;
  230. crypto_aead_set_reqsize(aead, crypto_aead_reqsize(&cryptd_tfm->base));
  231. return 0;
  232. }
  233. EXPORT_SYMBOL_GPL(cryptd_morus1280_glue_init_tfm);
  234. void cryptd_morus1280_glue_exit_tfm(struct crypto_aead *aead)
  235. {
  236. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  237. cryptd_free_aead(*ctx);
  238. }
  239. EXPORT_SYMBOL_GPL(cryptd_morus1280_glue_exit_tfm);
  240. MODULE_LICENSE("GPL");
  241. MODULE_AUTHOR("Ondrej Mosnacek <omosnacek@gmail.com>");
  242. MODULE_DESCRIPTION("MORUS-1280 AEAD mode -- glue for x86 optimizations");