morus640_glue.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * The MORUS-640 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/morus640_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 morus640_state {
  25. struct morus640_block s[MORUS_STATE_BLOCKS];
  26. };
  27. struct morus640_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_morus640_glue_process_ad(
  36. struct morus640_state *state,
  37. const struct morus640_glue_ops *ops,
  38. struct scatterlist *sg_src, unsigned int assoclen)
  39. {
  40. struct scatter_walk walk;
  41. struct morus640_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 >= MORUS640_BLOCK_SIZE) {
  50. if (pos > 0) {
  51. unsigned int fill = MORUS640_BLOCK_SIZE - pos;
  52. memcpy(buf.bytes + pos, src, fill);
  53. ops->ad(state, buf.bytes, MORUS640_BLOCK_SIZE);
  54. pos = 0;
  55. left -= fill;
  56. src += fill;
  57. }
  58. ops->ad(state, src, left);
  59. src += left & ~(MORUS640_BLOCK_SIZE - 1);
  60. left &= MORUS640_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, MORUS640_BLOCK_SIZE - pos);
  71. ops->ad(state, buf.bytes, MORUS640_BLOCK_SIZE);
  72. }
  73. }
  74. static void crypto_morus640_glue_process_crypt(struct morus640_state *state,
  75. struct morus640_ops ops,
  76. struct skcipher_walk *walk)
  77. {
  78. while (walk->nbytes >= MORUS640_BLOCK_SIZE) {
  79. ops.crypt_blocks(state, walk->src.virt.addr,
  80. walk->dst.virt.addr,
  81. round_down(walk->nbytes, MORUS640_BLOCK_SIZE));
  82. skcipher_walk_done(walk, walk->nbytes % MORUS640_BLOCK_SIZE);
  83. }
  84. if (walk->nbytes) {
  85. ops.crypt_tail(state, walk->src.virt.addr, walk->dst.virt.addr,
  86. walk->nbytes);
  87. skcipher_walk_done(walk, 0);
  88. }
  89. }
  90. int crypto_morus640_glue_setkey(struct crypto_aead *aead, const u8 *key,
  91. unsigned int keylen)
  92. {
  93. struct morus640_ctx *ctx = crypto_aead_ctx(aead);
  94. if (keylen != MORUS640_BLOCK_SIZE) {
  95. crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  96. return -EINVAL;
  97. }
  98. memcpy(ctx->key.bytes, key, MORUS640_BLOCK_SIZE);
  99. return 0;
  100. }
  101. EXPORT_SYMBOL_GPL(crypto_morus640_glue_setkey);
  102. int crypto_morus640_glue_setauthsize(struct crypto_aead *tfm,
  103. unsigned int authsize)
  104. {
  105. return (authsize <= MORUS_MAX_AUTH_SIZE) ? 0 : -EINVAL;
  106. }
  107. EXPORT_SYMBOL_GPL(crypto_morus640_glue_setauthsize);
  108. static void crypto_morus640_glue_crypt(struct aead_request *req,
  109. struct morus640_ops ops,
  110. unsigned int cryptlen,
  111. struct morus640_block *tag_xor)
  112. {
  113. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  114. struct morus640_ctx *ctx = crypto_aead_ctx(tfm);
  115. struct morus640_state state;
  116. struct skcipher_walk walk;
  117. ops.skcipher_walk_init(&walk, req, true);
  118. kernel_fpu_begin();
  119. ctx->ops->init(&state, &ctx->key, req->iv);
  120. crypto_morus640_glue_process_ad(&state, ctx->ops, req->src, req->assoclen);
  121. crypto_morus640_glue_process_crypt(&state, ops, &walk);
  122. ctx->ops->final(&state, tag_xor, req->assoclen, cryptlen);
  123. kernel_fpu_end();
  124. }
  125. int crypto_morus640_glue_encrypt(struct aead_request *req)
  126. {
  127. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  128. struct morus640_ctx *ctx = crypto_aead_ctx(tfm);
  129. struct morus640_ops OPS = {
  130. .skcipher_walk_init = skcipher_walk_aead_encrypt,
  131. .crypt_blocks = ctx->ops->enc,
  132. .crypt_tail = ctx->ops->enc_tail,
  133. };
  134. struct morus640_block tag = {};
  135. unsigned int authsize = crypto_aead_authsize(tfm);
  136. unsigned int cryptlen = req->cryptlen;
  137. crypto_morus640_glue_crypt(req, OPS, cryptlen, &tag);
  138. scatterwalk_map_and_copy(tag.bytes, req->dst,
  139. req->assoclen + cryptlen, authsize, 1);
  140. return 0;
  141. }
  142. EXPORT_SYMBOL_GPL(crypto_morus640_glue_encrypt);
  143. int crypto_morus640_glue_decrypt(struct aead_request *req)
  144. {
  145. static const u8 zeros[MORUS640_BLOCK_SIZE] = {};
  146. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  147. struct morus640_ctx *ctx = crypto_aead_ctx(tfm);
  148. struct morus640_ops OPS = {
  149. .skcipher_walk_init = skcipher_walk_aead_decrypt,
  150. .crypt_blocks = ctx->ops->dec,
  151. .crypt_tail = ctx->ops->dec_tail,
  152. };
  153. struct morus640_block tag;
  154. unsigned int authsize = crypto_aead_authsize(tfm);
  155. unsigned int cryptlen = req->cryptlen - authsize;
  156. scatterwalk_map_and_copy(tag.bytes, req->src,
  157. req->assoclen + cryptlen, authsize, 0);
  158. crypto_morus640_glue_crypt(req, OPS, cryptlen, &tag);
  159. return crypto_memneq(tag.bytes, zeros, authsize) ? -EBADMSG : 0;
  160. }
  161. EXPORT_SYMBOL_GPL(crypto_morus640_glue_decrypt);
  162. void crypto_morus640_glue_init_ops(struct crypto_aead *aead,
  163. const struct morus640_glue_ops *ops)
  164. {
  165. struct morus640_ctx *ctx = crypto_aead_ctx(aead);
  166. ctx->ops = ops;
  167. }
  168. EXPORT_SYMBOL_GPL(crypto_morus640_glue_init_ops);
  169. int cryptd_morus640_glue_setkey(struct crypto_aead *aead, const u8 *key,
  170. unsigned int keylen)
  171. {
  172. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  173. struct cryptd_aead *cryptd_tfm = *ctx;
  174. return crypto_aead_setkey(&cryptd_tfm->base, key, keylen);
  175. }
  176. EXPORT_SYMBOL_GPL(cryptd_morus640_glue_setkey);
  177. int cryptd_morus640_glue_setauthsize(struct crypto_aead *aead,
  178. unsigned int authsize)
  179. {
  180. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  181. struct cryptd_aead *cryptd_tfm = *ctx;
  182. return crypto_aead_setauthsize(&cryptd_tfm->base, authsize);
  183. }
  184. EXPORT_SYMBOL_GPL(cryptd_morus640_glue_setauthsize);
  185. int cryptd_morus640_glue_encrypt(struct aead_request *req)
  186. {
  187. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  188. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  189. struct cryptd_aead *cryptd_tfm = *ctx;
  190. aead = &cryptd_tfm->base;
  191. if (irq_fpu_usable() && (!in_atomic() ||
  192. !cryptd_aead_queued(cryptd_tfm)))
  193. aead = cryptd_aead_child(cryptd_tfm);
  194. aead_request_set_tfm(req, aead);
  195. return crypto_aead_encrypt(req);
  196. }
  197. EXPORT_SYMBOL_GPL(cryptd_morus640_glue_encrypt);
  198. int cryptd_morus640_glue_decrypt(struct aead_request *req)
  199. {
  200. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  201. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  202. struct cryptd_aead *cryptd_tfm = *ctx;
  203. aead = &cryptd_tfm->base;
  204. if (irq_fpu_usable() && (!in_atomic() ||
  205. !cryptd_aead_queued(cryptd_tfm)))
  206. aead = cryptd_aead_child(cryptd_tfm);
  207. aead_request_set_tfm(req, aead);
  208. return crypto_aead_decrypt(req);
  209. }
  210. EXPORT_SYMBOL_GPL(cryptd_morus640_glue_decrypt);
  211. int cryptd_morus640_glue_init_tfm(struct crypto_aead *aead)
  212. {
  213. struct cryptd_aead *cryptd_tfm;
  214. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  215. const char *name = crypto_aead_alg(aead)->base.cra_driver_name;
  216. char internal_name[CRYPTO_MAX_ALG_NAME];
  217. if (snprintf(internal_name, CRYPTO_MAX_ALG_NAME, "__%s", name)
  218. >= CRYPTO_MAX_ALG_NAME)
  219. return -ENAMETOOLONG;
  220. cryptd_tfm = cryptd_alloc_aead(internal_name, CRYPTO_ALG_INTERNAL,
  221. CRYPTO_ALG_INTERNAL);
  222. if (IS_ERR(cryptd_tfm))
  223. return PTR_ERR(cryptd_tfm);
  224. *ctx = cryptd_tfm;
  225. crypto_aead_set_reqsize(aead, crypto_aead_reqsize(&cryptd_tfm->base));
  226. return 0;
  227. }
  228. EXPORT_SYMBOL_GPL(cryptd_morus640_glue_init_tfm);
  229. void cryptd_morus640_glue_exit_tfm(struct crypto_aead *aead)
  230. {
  231. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  232. cryptd_free_aead(*ctx);
  233. }
  234. EXPORT_SYMBOL_GPL(cryptd_morus640_glue_exit_tfm);
  235. MODULE_LICENSE("GPL");
  236. MODULE_AUTHOR("Ondrej Mosnacek <omosnacek@gmail.com>");
  237. MODULE_DESCRIPTION("MORUS-640 AEAD mode -- glue for x86 optimizations");