aegis128l-aesni-glue.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * The AEGIS-128L Authenticated-Encryption Algorithm
  3. * Glue for AES-NI + SSE2 implementation
  4. *
  5. * Copyright (c) 2017-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/scatterwalk.h>
  17. #include <linux/module.h>
  18. #include <asm/fpu/api.h>
  19. #include <asm/cpu_device_id.h>
  20. #define AEGIS128L_BLOCK_ALIGN 16
  21. #define AEGIS128L_BLOCK_SIZE 32
  22. #define AEGIS128L_NONCE_SIZE 16
  23. #define AEGIS128L_STATE_BLOCKS 8
  24. #define AEGIS128L_KEY_SIZE 16
  25. #define AEGIS128L_MIN_AUTH_SIZE 8
  26. #define AEGIS128L_MAX_AUTH_SIZE 16
  27. asmlinkage void crypto_aegis128l_aesni_init(void *state, void *key, void *iv);
  28. asmlinkage void crypto_aegis128l_aesni_ad(
  29. void *state, unsigned int length, const void *data);
  30. asmlinkage void crypto_aegis128l_aesni_enc(
  31. void *state, unsigned int length, const void *src, void *dst);
  32. asmlinkage void crypto_aegis128l_aesni_dec(
  33. void *state, unsigned int length, const void *src, void *dst);
  34. asmlinkage void crypto_aegis128l_aesni_enc_tail(
  35. void *state, unsigned int length, const void *src, void *dst);
  36. asmlinkage void crypto_aegis128l_aesni_dec_tail(
  37. void *state, unsigned int length, const void *src, void *dst);
  38. asmlinkage void crypto_aegis128l_aesni_final(
  39. void *state, void *tag_xor, unsigned int cryptlen,
  40. unsigned int assoclen);
  41. struct aegis_block {
  42. u8 bytes[AEGIS128L_BLOCK_SIZE] __aligned(AEGIS128L_BLOCK_ALIGN);
  43. };
  44. struct aegis_state {
  45. struct aegis_block blocks[AEGIS128L_STATE_BLOCKS];
  46. };
  47. struct aegis_ctx {
  48. struct aegis_block key;
  49. };
  50. struct aegis_crypt_ops {
  51. int (*skcipher_walk_init)(struct skcipher_walk *walk,
  52. struct aead_request *req, bool atomic);
  53. void (*crypt_blocks)(void *state, unsigned int length, const void *src,
  54. void *dst);
  55. void (*crypt_tail)(void *state, unsigned int length, const void *src,
  56. void *dst);
  57. };
  58. static void crypto_aegis128l_aesni_process_ad(
  59. struct aegis_state *state, struct scatterlist *sg_src,
  60. unsigned int assoclen)
  61. {
  62. struct scatter_walk walk;
  63. struct aegis_block buf;
  64. unsigned int pos = 0;
  65. scatterwalk_start(&walk, sg_src);
  66. while (assoclen != 0) {
  67. unsigned int size = scatterwalk_clamp(&walk, assoclen);
  68. unsigned int left = size;
  69. void *mapped = scatterwalk_map(&walk);
  70. const u8 *src = (const u8 *)mapped;
  71. if (pos + size >= AEGIS128L_BLOCK_SIZE) {
  72. if (pos > 0) {
  73. unsigned int fill = AEGIS128L_BLOCK_SIZE - pos;
  74. memcpy(buf.bytes + pos, src, fill);
  75. crypto_aegis128l_aesni_ad(state,
  76. AEGIS128L_BLOCK_SIZE,
  77. buf.bytes);
  78. pos = 0;
  79. left -= fill;
  80. src += fill;
  81. }
  82. crypto_aegis128l_aesni_ad(state, left, src);
  83. src += left & ~(AEGIS128L_BLOCK_SIZE - 1);
  84. left &= AEGIS128L_BLOCK_SIZE - 1;
  85. }
  86. memcpy(buf.bytes + pos, src, left);
  87. pos += left;
  88. assoclen -= size;
  89. scatterwalk_unmap(mapped);
  90. scatterwalk_advance(&walk, size);
  91. scatterwalk_done(&walk, 0, assoclen);
  92. }
  93. if (pos > 0) {
  94. memset(buf.bytes + pos, 0, AEGIS128L_BLOCK_SIZE - pos);
  95. crypto_aegis128l_aesni_ad(state, AEGIS128L_BLOCK_SIZE, buf.bytes);
  96. }
  97. }
  98. static void crypto_aegis128l_aesni_process_crypt(
  99. struct aegis_state *state, struct skcipher_walk *walk,
  100. const struct aegis_crypt_ops *ops)
  101. {
  102. while (walk->nbytes >= AEGIS128L_BLOCK_SIZE) {
  103. ops->crypt_blocks(state, round_down(walk->nbytes,
  104. AEGIS128L_BLOCK_SIZE),
  105. walk->src.virt.addr, walk->dst.virt.addr);
  106. skcipher_walk_done(walk, walk->nbytes % AEGIS128L_BLOCK_SIZE);
  107. }
  108. if (walk->nbytes) {
  109. ops->crypt_tail(state, walk->nbytes, walk->src.virt.addr,
  110. walk->dst.virt.addr);
  111. skcipher_walk_done(walk, 0);
  112. }
  113. }
  114. static struct aegis_ctx *crypto_aegis128l_aesni_ctx(struct crypto_aead *aead)
  115. {
  116. u8 *ctx = crypto_aead_ctx(aead);
  117. ctx = PTR_ALIGN(ctx, __alignof__(struct aegis_ctx));
  118. return (void *)ctx;
  119. }
  120. static int crypto_aegis128l_aesni_setkey(struct crypto_aead *aead,
  121. const u8 *key, unsigned int keylen)
  122. {
  123. struct aegis_ctx *ctx = crypto_aegis128l_aesni_ctx(aead);
  124. if (keylen != AEGIS128L_KEY_SIZE) {
  125. crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  126. return -EINVAL;
  127. }
  128. memcpy(ctx->key.bytes, key, AEGIS128L_KEY_SIZE);
  129. return 0;
  130. }
  131. static int crypto_aegis128l_aesni_setauthsize(struct crypto_aead *tfm,
  132. unsigned int authsize)
  133. {
  134. if (authsize > AEGIS128L_MAX_AUTH_SIZE)
  135. return -EINVAL;
  136. if (authsize < AEGIS128L_MIN_AUTH_SIZE)
  137. return -EINVAL;
  138. return 0;
  139. }
  140. static void crypto_aegis128l_aesni_crypt(struct aead_request *req,
  141. struct aegis_block *tag_xor,
  142. unsigned int cryptlen,
  143. const struct aegis_crypt_ops *ops)
  144. {
  145. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  146. struct aegis_ctx *ctx = crypto_aegis128l_aesni_ctx(tfm);
  147. struct skcipher_walk walk;
  148. struct aegis_state state;
  149. ops->skcipher_walk_init(&walk, req, true);
  150. kernel_fpu_begin();
  151. crypto_aegis128l_aesni_init(&state, ctx->key.bytes, req->iv);
  152. crypto_aegis128l_aesni_process_ad(&state, req->src, req->assoclen);
  153. crypto_aegis128l_aesni_process_crypt(&state, &walk, ops);
  154. crypto_aegis128l_aesni_final(&state, tag_xor, req->assoclen, cryptlen);
  155. kernel_fpu_end();
  156. }
  157. static int crypto_aegis128l_aesni_encrypt(struct aead_request *req)
  158. {
  159. static const struct aegis_crypt_ops OPS = {
  160. .skcipher_walk_init = skcipher_walk_aead_encrypt,
  161. .crypt_blocks = crypto_aegis128l_aesni_enc,
  162. .crypt_tail = crypto_aegis128l_aesni_enc_tail,
  163. };
  164. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  165. struct aegis_block tag = {};
  166. unsigned int authsize = crypto_aead_authsize(tfm);
  167. unsigned int cryptlen = req->cryptlen;
  168. crypto_aegis128l_aesni_crypt(req, &tag, cryptlen, &OPS);
  169. scatterwalk_map_and_copy(tag.bytes, req->dst,
  170. req->assoclen + cryptlen, authsize, 1);
  171. return 0;
  172. }
  173. static int crypto_aegis128l_aesni_decrypt(struct aead_request *req)
  174. {
  175. static const struct aegis_block zeros = {};
  176. static const struct aegis_crypt_ops OPS = {
  177. .skcipher_walk_init = skcipher_walk_aead_decrypt,
  178. .crypt_blocks = crypto_aegis128l_aesni_dec,
  179. .crypt_tail = crypto_aegis128l_aesni_dec_tail,
  180. };
  181. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  182. struct aegis_block tag;
  183. unsigned int authsize = crypto_aead_authsize(tfm);
  184. unsigned int cryptlen = req->cryptlen - authsize;
  185. scatterwalk_map_and_copy(tag.bytes, req->src,
  186. req->assoclen + cryptlen, authsize, 0);
  187. crypto_aegis128l_aesni_crypt(req, &tag, cryptlen, &OPS);
  188. return crypto_memneq(tag.bytes, zeros.bytes, authsize) ? -EBADMSG : 0;
  189. }
  190. static int crypto_aegis128l_aesni_init_tfm(struct crypto_aead *aead)
  191. {
  192. return 0;
  193. }
  194. static void crypto_aegis128l_aesni_exit_tfm(struct crypto_aead *aead)
  195. {
  196. }
  197. static int cryptd_aegis128l_aesni_setkey(struct crypto_aead *aead,
  198. const u8 *key, unsigned int keylen)
  199. {
  200. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  201. struct cryptd_aead *cryptd_tfm = *ctx;
  202. return crypto_aead_setkey(&cryptd_tfm->base, key, keylen);
  203. }
  204. static int cryptd_aegis128l_aesni_setauthsize(struct crypto_aead *aead,
  205. unsigned int authsize)
  206. {
  207. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  208. struct cryptd_aead *cryptd_tfm = *ctx;
  209. return crypto_aead_setauthsize(&cryptd_tfm->base, authsize);
  210. }
  211. static int cryptd_aegis128l_aesni_encrypt(struct aead_request *req)
  212. {
  213. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  214. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  215. struct cryptd_aead *cryptd_tfm = *ctx;
  216. aead = &cryptd_tfm->base;
  217. if (irq_fpu_usable() && (!in_atomic() ||
  218. !cryptd_aead_queued(cryptd_tfm)))
  219. aead = cryptd_aead_child(cryptd_tfm);
  220. aead_request_set_tfm(req, aead);
  221. return crypto_aead_encrypt(req);
  222. }
  223. static int cryptd_aegis128l_aesni_decrypt(struct aead_request *req)
  224. {
  225. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  226. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  227. struct cryptd_aead *cryptd_tfm = *ctx;
  228. aead = &cryptd_tfm->base;
  229. if (irq_fpu_usable() && (!in_atomic() ||
  230. !cryptd_aead_queued(cryptd_tfm)))
  231. aead = cryptd_aead_child(cryptd_tfm);
  232. aead_request_set_tfm(req, aead);
  233. return crypto_aead_decrypt(req);
  234. }
  235. static int cryptd_aegis128l_aesni_init_tfm(struct crypto_aead *aead)
  236. {
  237. struct cryptd_aead *cryptd_tfm;
  238. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  239. cryptd_tfm = cryptd_alloc_aead("__aegis128l-aesni", CRYPTO_ALG_INTERNAL,
  240. CRYPTO_ALG_INTERNAL);
  241. if (IS_ERR(cryptd_tfm))
  242. return PTR_ERR(cryptd_tfm);
  243. *ctx = cryptd_tfm;
  244. crypto_aead_set_reqsize(aead, crypto_aead_reqsize(&cryptd_tfm->base));
  245. return 0;
  246. }
  247. static void cryptd_aegis128l_aesni_exit_tfm(struct crypto_aead *aead)
  248. {
  249. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  250. cryptd_free_aead(*ctx);
  251. }
  252. static struct aead_alg crypto_aegis128l_aesni_alg[] = {
  253. {
  254. .setkey = crypto_aegis128l_aesni_setkey,
  255. .setauthsize = crypto_aegis128l_aesni_setauthsize,
  256. .encrypt = crypto_aegis128l_aesni_encrypt,
  257. .decrypt = crypto_aegis128l_aesni_decrypt,
  258. .init = crypto_aegis128l_aesni_init_tfm,
  259. .exit = crypto_aegis128l_aesni_exit_tfm,
  260. .ivsize = AEGIS128L_NONCE_SIZE,
  261. .maxauthsize = AEGIS128L_MAX_AUTH_SIZE,
  262. .chunksize = AEGIS128L_BLOCK_SIZE,
  263. .base = {
  264. .cra_flags = CRYPTO_ALG_INTERNAL,
  265. .cra_blocksize = 1,
  266. .cra_ctxsize = sizeof(struct aegis_ctx) +
  267. __alignof__(struct aegis_ctx),
  268. .cra_alignmask = 0,
  269. .cra_name = "__aegis128l",
  270. .cra_driver_name = "__aegis128l-aesni",
  271. .cra_module = THIS_MODULE,
  272. }
  273. }, {
  274. .setkey = cryptd_aegis128l_aesni_setkey,
  275. .setauthsize = cryptd_aegis128l_aesni_setauthsize,
  276. .encrypt = cryptd_aegis128l_aesni_encrypt,
  277. .decrypt = cryptd_aegis128l_aesni_decrypt,
  278. .init = cryptd_aegis128l_aesni_init_tfm,
  279. .exit = cryptd_aegis128l_aesni_exit_tfm,
  280. .ivsize = AEGIS128L_NONCE_SIZE,
  281. .maxauthsize = AEGIS128L_MAX_AUTH_SIZE,
  282. .chunksize = AEGIS128L_BLOCK_SIZE,
  283. .base = {
  284. .cra_flags = CRYPTO_ALG_ASYNC,
  285. .cra_blocksize = 1,
  286. .cra_ctxsize = sizeof(struct cryptd_aead *),
  287. .cra_alignmask = 0,
  288. .cra_priority = 400,
  289. .cra_name = "aegis128l",
  290. .cra_driver_name = "aegis128l-aesni",
  291. .cra_module = THIS_MODULE,
  292. }
  293. }
  294. };
  295. static int __init crypto_aegis128l_aesni_module_init(void)
  296. {
  297. if (!boot_cpu_has(X86_FEATURE_XMM2) ||
  298. !boot_cpu_has(X86_FEATURE_AES) ||
  299. !cpu_has_xfeatures(XFEATURE_MASK_SSE, NULL))
  300. return -ENODEV;
  301. return crypto_register_aeads(crypto_aegis128l_aesni_alg,
  302. ARRAY_SIZE(crypto_aegis128l_aesni_alg));
  303. }
  304. static void __exit crypto_aegis128l_aesni_module_exit(void)
  305. {
  306. crypto_unregister_aeads(crypto_aegis128l_aesni_alg,
  307. ARRAY_SIZE(crypto_aegis128l_aesni_alg));
  308. }
  309. module_init(crypto_aegis128l_aesni_module_init);
  310. module_exit(crypto_aegis128l_aesni_module_exit);
  311. MODULE_LICENSE("GPL");
  312. MODULE_AUTHOR("Ondrej Mosnacek <omosnacek@gmail.com>");
  313. MODULE_DESCRIPTION("AEGIS-128L AEAD algorithm -- AESNI+SSE2 implementation");
  314. MODULE_ALIAS_CRYPTO("aegis128l");
  315. MODULE_ALIAS_CRYPTO("aegis128l-aesni");