aegis128-aesni-glue.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * The AEGIS-128 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 AEGIS128_BLOCK_ALIGN 16
  21. #define AEGIS128_BLOCK_SIZE 16
  22. #define AEGIS128_NONCE_SIZE 16
  23. #define AEGIS128_STATE_BLOCKS 5
  24. #define AEGIS128_KEY_SIZE 16
  25. #define AEGIS128_MIN_AUTH_SIZE 8
  26. #define AEGIS128_MAX_AUTH_SIZE 16
  27. asmlinkage void crypto_aegis128_aesni_init(void *state, void *key, void *iv);
  28. asmlinkage void crypto_aegis128_aesni_ad(
  29. void *state, unsigned int length, const void *data);
  30. asmlinkage void crypto_aegis128_aesni_enc(
  31. void *state, unsigned int length, const void *src, void *dst);
  32. asmlinkage void crypto_aegis128_aesni_dec(
  33. void *state, unsigned int length, const void *src, void *dst);
  34. asmlinkage void crypto_aegis128_aesni_enc_tail(
  35. void *state, unsigned int length, const void *src, void *dst);
  36. asmlinkage void crypto_aegis128_aesni_dec_tail(
  37. void *state, unsigned int length, const void *src, void *dst);
  38. asmlinkage void crypto_aegis128_aesni_final(
  39. void *state, void *tag_xor, unsigned int cryptlen,
  40. unsigned int assoclen);
  41. struct aegis_block {
  42. u8 bytes[AEGIS128_BLOCK_SIZE] __aligned(AEGIS128_BLOCK_ALIGN);
  43. };
  44. struct aegis_state {
  45. struct aegis_block blocks[AEGIS128_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_aegis128_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 >= AEGIS128_BLOCK_SIZE) {
  72. if (pos > 0) {
  73. unsigned int fill = AEGIS128_BLOCK_SIZE - pos;
  74. memcpy(buf.bytes + pos, src, fill);
  75. crypto_aegis128_aesni_ad(state,
  76. AEGIS128_BLOCK_SIZE,
  77. buf.bytes);
  78. pos = 0;
  79. left -= fill;
  80. src += fill;
  81. }
  82. crypto_aegis128_aesni_ad(state, left, src);
  83. src += left & ~(AEGIS128_BLOCK_SIZE - 1);
  84. left &= AEGIS128_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, AEGIS128_BLOCK_SIZE - pos);
  95. crypto_aegis128_aesni_ad(state, AEGIS128_BLOCK_SIZE, buf.bytes);
  96. }
  97. }
  98. static void crypto_aegis128_aesni_process_crypt(
  99. struct aegis_state *state, struct aead_request *req,
  100. const struct aegis_crypt_ops *ops)
  101. {
  102. struct skcipher_walk walk;
  103. u8 *src, *dst;
  104. unsigned int chunksize, base;
  105. ops->skcipher_walk_init(&walk, req, false);
  106. while (walk.nbytes) {
  107. src = walk.src.virt.addr;
  108. dst = walk.dst.virt.addr;
  109. chunksize = walk.nbytes;
  110. ops->crypt_blocks(state, chunksize, src, dst);
  111. base = chunksize & ~(AEGIS128_BLOCK_SIZE - 1);
  112. src += base;
  113. dst += base;
  114. chunksize &= AEGIS128_BLOCK_SIZE - 1;
  115. if (chunksize > 0)
  116. ops->crypt_tail(state, chunksize, src, dst);
  117. skcipher_walk_done(&walk, 0);
  118. }
  119. }
  120. static struct aegis_ctx *crypto_aegis128_aesni_ctx(struct crypto_aead *aead)
  121. {
  122. u8 *ctx = crypto_aead_ctx(aead);
  123. ctx = PTR_ALIGN(ctx, __alignof__(struct aegis_ctx));
  124. return (void *)ctx;
  125. }
  126. static int crypto_aegis128_aesni_setkey(struct crypto_aead *aead, const u8 *key,
  127. unsigned int keylen)
  128. {
  129. struct aegis_ctx *ctx = crypto_aegis128_aesni_ctx(aead);
  130. if (keylen != AEGIS128_KEY_SIZE) {
  131. crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  132. return -EINVAL;
  133. }
  134. memcpy(ctx->key.bytes, key, AEGIS128_KEY_SIZE);
  135. return 0;
  136. }
  137. static int crypto_aegis128_aesni_setauthsize(struct crypto_aead *tfm,
  138. unsigned int authsize)
  139. {
  140. if (authsize > AEGIS128_MAX_AUTH_SIZE)
  141. return -EINVAL;
  142. if (authsize < AEGIS128_MIN_AUTH_SIZE)
  143. return -EINVAL;
  144. return 0;
  145. }
  146. static void crypto_aegis128_aesni_crypt(struct aead_request *req,
  147. struct aegis_block *tag_xor,
  148. unsigned int cryptlen,
  149. const struct aegis_crypt_ops *ops)
  150. {
  151. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  152. struct aegis_ctx *ctx = crypto_aegis128_aesni_ctx(tfm);
  153. struct aegis_state state;
  154. kernel_fpu_begin();
  155. crypto_aegis128_aesni_init(&state, ctx->key.bytes, req->iv);
  156. crypto_aegis128_aesni_process_ad(&state, req->src, req->assoclen);
  157. crypto_aegis128_aesni_process_crypt(&state, req, ops);
  158. crypto_aegis128_aesni_final(&state, tag_xor, req->assoclen, cryptlen);
  159. kernel_fpu_end();
  160. }
  161. static int crypto_aegis128_aesni_encrypt(struct aead_request *req)
  162. {
  163. static const struct aegis_crypt_ops OPS = {
  164. .skcipher_walk_init = skcipher_walk_aead_encrypt,
  165. .crypt_blocks = crypto_aegis128_aesni_enc,
  166. .crypt_tail = crypto_aegis128_aesni_enc_tail,
  167. };
  168. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  169. struct aegis_block tag = {};
  170. unsigned int authsize = crypto_aead_authsize(tfm);
  171. unsigned int cryptlen = req->cryptlen;
  172. crypto_aegis128_aesni_crypt(req, &tag, cryptlen, &OPS);
  173. scatterwalk_map_and_copy(tag.bytes, req->dst,
  174. req->assoclen + cryptlen, authsize, 1);
  175. return 0;
  176. }
  177. static int crypto_aegis128_aesni_decrypt(struct aead_request *req)
  178. {
  179. static const struct aegis_block zeros = {};
  180. static const struct aegis_crypt_ops OPS = {
  181. .skcipher_walk_init = skcipher_walk_aead_decrypt,
  182. .crypt_blocks = crypto_aegis128_aesni_dec,
  183. .crypt_tail = crypto_aegis128_aesni_dec_tail,
  184. };
  185. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  186. struct aegis_block tag;
  187. unsigned int authsize = crypto_aead_authsize(tfm);
  188. unsigned int cryptlen = req->cryptlen - authsize;
  189. scatterwalk_map_and_copy(tag.bytes, req->src,
  190. req->assoclen + cryptlen, authsize, 0);
  191. crypto_aegis128_aesni_crypt(req, &tag, cryptlen, &OPS);
  192. return crypto_memneq(tag.bytes, zeros.bytes, authsize) ? -EBADMSG : 0;
  193. }
  194. static int crypto_aegis128_aesni_init_tfm(struct crypto_aead *aead)
  195. {
  196. return 0;
  197. }
  198. static void crypto_aegis128_aesni_exit_tfm(struct crypto_aead *aead)
  199. {
  200. }
  201. static int cryptd_aegis128_aesni_setkey(struct crypto_aead *aead,
  202. const u8 *key, unsigned int keylen)
  203. {
  204. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  205. struct cryptd_aead *cryptd_tfm = *ctx;
  206. return crypto_aead_setkey(&cryptd_tfm->base, key, keylen);
  207. }
  208. static int cryptd_aegis128_aesni_setauthsize(struct crypto_aead *aead,
  209. unsigned int authsize)
  210. {
  211. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  212. struct cryptd_aead *cryptd_tfm = *ctx;
  213. return crypto_aead_setauthsize(&cryptd_tfm->base, authsize);
  214. }
  215. static int cryptd_aegis128_aesni_encrypt(struct aead_request *req)
  216. {
  217. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  218. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  219. struct cryptd_aead *cryptd_tfm = *ctx;
  220. aead = &cryptd_tfm->base;
  221. if (irq_fpu_usable() && (!in_atomic() ||
  222. !cryptd_aead_queued(cryptd_tfm)))
  223. aead = cryptd_aead_child(cryptd_tfm);
  224. aead_request_set_tfm(req, aead);
  225. return crypto_aead_encrypt(req);
  226. }
  227. static int cryptd_aegis128_aesni_decrypt(struct aead_request *req)
  228. {
  229. struct crypto_aead *aead = crypto_aead_reqtfm(req);
  230. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  231. struct cryptd_aead *cryptd_tfm = *ctx;
  232. aead = &cryptd_tfm->base;
  233. if (irq_fpu_usable() && (!in_atomic() ||
  234. !cryptd_aead_queued(cryptd_tfm)))
  235. aead = cryptd_aead_child(cryptd_tfm);
  236. aead_request_set_tfm(req, aead);
  237. return crypto_aead_decrypt(req);
  238. }
  239. static int cryptd_aegis128_aesni_init_tfm(struct crypto_aead *aead)
  240. {
  241. struct cryptd_aead *cryptd_tfm;
  242. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  243. cryptd_tfm = cryptd_alloc_aead("__aegis128-aesni", CRYPTO_ALG_INTERNAL,
  244. CRYPTO_ALG_INTERNAL);
  245. if (IS_ERR(cryptd_tfm))
  246. return PTR_ERR(cryptd_tfm);
  247. *ctx = cryptd_tfm;
  248. crypto_aead_set_reqsize(aead, crypto_aead_reqsize(&cryptd_tfm->base));
  249. return 0;
  250. }
  251. static void cryptd_aegis128_aesni_exit_tfm(struct crypto_aead *aead)
  252. {
  253. struct cryptd_aead **ctx = crypto_aead_ctx(aead);
  254. cryptd_free_aead(*ctx);
  255. }
  256. static struct aead_alg crypto_aegis128_aesni_alg[] = {
  257. {
  258. .setkey = crypto_aegis128_aesni_setkey,
  259. .setauthsize = crypto_aegis128_aesni_setauthsize,
  260. .encrypt = crypto_aegis128_aesni_encrypt,
  261. .decrypt = crypto_aegis128_aesni_decrypt,
  262. .init = crypto_aegis128_aesni_init_tfm,
  263. .exit = crypto_aegis128_aesni_exit_tfm,
  264. .ivsize = AEGIS128_NONCE_SIZE,
  265. .maxauthsize = AEGIS128_MAX_AUTH_SIZE,
  266. .chunksize = AEGIS128_BLOCK_SIZE,
  267. .base = {
  268. .cra_flags = CRYPTO_ALG_INTERNAL,
  269. .cra_blocksize = 1,
  270. .cra_ctxsize = sizeof(struct aegis_ctx) +
  271. __alignof__(struct aegis_ctx),
  272. .cra_alignmask = 0,
  273. .cra_name = "__aegis128",
  274. .cra_driver_name = "__aegis128-aesni",
  275. .cra_module = THIS_MODULE,
  276. }
  277. }, {
  278. .setkey = cryptd_aegis128_aesni_setkey,
  279. .setauthsize = cryptd_aegis128_aesni_setauthsize,
  280. .encrypt = cryptd_aegis128_aesni_encrypt,
  281. .decrypt = cryptd_aegis128_aesni_decrypt,
  282. .init = cryptd_aegis128_aesni_init_tfm,
  283. .exit = cryptd_aegis128_aesni_exit_tfm,
  284. .ivsize = AEGIS128_NONCE_SIZE,
  285. .maxauthsize = AEGIS128_MAX_AUTH_SIZE,
  286. .chunksize = AEGIS128_BLOCK_SIZE,
  287. .base = {
  288. .cra_flags = CRYPTO_ALG_ASYNC,
  289. .cra_blocksize = 1,
  290. .cra_ctxsize = sizeof(struct cryptd_aead *),
  291. .cra_alignmask = 0,
  292. .cra_priority = 400,
  293. .cra_name = "aegis128",
  294. .cra_driver_name = "aegis128-aesni",
  295. .cra_module = THIS_MODULE,
  296. }
  297. }
  298. };
  299. static int __init crypto_aegis128_aesni_module_init(void)
  300. {
  301. if (!boot_cpu_has(X86_FEATURE_XMM2) ||
  302. !boot_cpu_has(X86_FEATURE_AES) ||
  303. !cpu_has_xfeatures(XFEATURE_MASK_SSE, NULL))
  304. return -ENODEV;
  305. return crypto_register_aeads(crypto_aegis128_aesni_alg,
  306. ARRAY_SIZE(crypto_aegis128_aesni_alg));
  307. }
  308. static void __exit crypto_aegis128_aesni_module_exit(void)
  309. {
  310. crypto_unregister_aeads(crypto_aegis128_aesni_alg,
  311. ARRAY_SIZE(crypto_aegis128_aesni_alg));
  312. }
  313. module_init(crypto_aegis128_aesni_module_init);
  314. module_exit(crypto_aegis128_aesni_module_exit);
  315. MODULE_LICENSE("GPL");
  316. MODULE_AUTHOR("Ondrej Mosnacek <omosnacek@gmail.com>");
  317. MODULE_DESCRIPTION("AEGIS-128 AEAD algorithm -- AESNI+SSE2 implementation");
  318. MODULE_ALIAS_CRYPTO("aegis128");
  319. MODULE_ALIAS_CRYPTO("aegis128-aesni");