aegis128-core.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * The AEGIS-128 Authenticated-Encryption Algorithm
  4. *
  5. * Copyright (c) 2017-2018 Ondrej Mosnacek <omosnacek@gmail.com>
  6. * Copyright (C) 2017-2018 Red Hat, Inc. All rights reserved.
  7. */
  8. #include <crypto/algapi.h>
  9. #include <crypto/internal/aead.h>
  10. #include <crypto/internal/simd.h>
  11. #include <crypto/internal/skcipher.h>
  12. #include <crypto/scatterwalk.h>
  13. #include <linux/err.h>
  14. #include <linux/init.h>
  15. #include <linux/jump_label.h>
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/scatterlist.h>
  19. #include <asm/simd.h>
  20. #include "aegis.h"
  21. #define AEGIS128_NONCE_SIZE 16
  22. #define AEGIS128_STATE_BLOCKS 5
  23. #define AEGIS128_KEY_SIZE 16
  24. #define AEGIS128_MIN_AUTH_SIZE 8
  25. #define AEGIS128_MAX_AUTH_SIZE 16
  26. struct aegis_state {
  27. union aegis_block blocks[AEGIS128_STATE_BLOCKS];
  28. };
  29. struct aegis_ctx {
  30. union aegis_block key;
  31. };
  32. static __ro_after_init DEFINE_STATIC_KEY_FALSE(have_simd);
  33. static const union aegis_block crypto_aegis_const[2] = {
  34. { .words64 = {
  35. cpu_to_le64(U64_C(0x0d08050302010100)),
  36. cpu_to_le64(U64_C(0x6279e99059372215)),
  37. } },
  38. { .words64 = {
  39. cpu_to_le64(U64_C(0xf12fc26d55183ddb)),
  40. cpu_to_le64(U64_C(0xdd28b57342311120)),
  41. } },
  42. };
  43. static bool aegis128_do_simd(void)
  44. {
  45. #ifdef CONFIG_CRYPTO_AEGIS128_SIMD
  46. if (static_branch_likely(&have_simd))
  47. return crypto_simd_usable();
  48. #endif
  49. return false;
  50. }
  51. bool crypto_aegis128_have_simd(void);
  52. void crypto_aegis128_update_simd(struct aegis_state *state, const void *msg);
  53. void crypto_aegis128_init_simd(struct aegis_state *state,
  54. const union aegis_block *key,
  55. const u8 *iv);
  56. void crypto_aegis128_encrypt_chunk_simd(struct aegis_state *state, u8 *dst,
  57. const u8 *src, unsigned int size);
  58. void crypto_aegis128_decrypt_chunk_simd(struct aegis_state *state, u8 *dst,
  59. const u8 *src, unsigned int size);
  60. void crypto_aegis128_final_simd(struct aegis_state *state,
  61. union aegis_block *tag_xor,
  62. u64 assoclen, u64 cryptlen);
  63. static void crypto_aegis128_update(struct aegis_state *state)
  64. {
  65. union aegis_block tmp;
  66. unsigned int i;
  67. tmp = state->blocks[AEGIS128_STATE_BLOCKS - 1];
  68. for (i = AEGIS128_STATE_BLOCKS - 1; i > 0; i--)
  69. crypto_aegis_aesenc(&state->blocks[i], &state->blocks[i - 1],
  70. &state->blocks[i]);
  71. crypto_aegis_aesenc(&state->blocks[0], &tmp, &state->blocks[0]);
  72. }
  73. static void crypto_aegis128_update_a(struct aegis_state *state,
  74. const union aegis_block *msg)
  75. {
  76. if (aegis128_do_simd()) {
  77. crypto_aegis128_update_simd(state, msg);
  78. return;
  79. }
  80. crypto_aegis128_update(state);
  81. crypto_aegis_block_xor(&state->blocks[0], msg);
  82. }
  83. static void crypto_aegis128_update_u(struct aegis_state *state, const void *msg)
  84. {
  85. if (aegis128_do_simd()) {
  86. crypto_aegis128_update_simd(state, msg);
  87. return;
  88. }
  89. crypto_aegis128_update(state);
  90. crypto_xor(state->blocks[0].bytes, msg, AEGIS_BLOCK_SIZE);
  91. }
  92. static void crypto_aegis128_init(struct aegis_state *state,
  93. const union aegis_block *key,
  94. const u8 *iv)
  95. {
  96. union aegis_block key_iv;
  97. unsigned int i;
  98. key_iv = *key;
  99. crypto_xor(key_iv.bytes, iv, AEGIS_BLOCK_SIZE);
  100. state->blocks[0] = key_iv;
  101. state->blocks[1] = crypto_aegis_const[1];
  102. state->blocks[2] = crypto_aegis_const[0];
  103. state->blocks[3] = *key;
  104. state->blocks[4] = *key;
  105. crypto_aegis_block_xor(&state->blocks[3], &crypto_aegis_const[0]);
  106. crypto_aegis_block_xor(&state->blocks[4], &crypto_aegis_const[1]);
  107. for (i = 0; i < 5; i++) {
  108. crypto_aegis128_update_a(state, key);
  109. crypto_aegis128_update_a(state, &key_iv);
  110. }
  111. }
  112. static void crypto_aegis128_ad(struct aegis_state *state,
  113. const u8 *src, unsigned int size)
  114. {
  115. if (AEGIS_ALIGNED(src)) {
  116. const union aegis_block *src_blk =
  117. (const union aegis_block *)src;
  118. while (size >= AEGIS_BLOCK_SIZE) {
  119. crypto_aegis128_update_a(state, src_blk);
  120. size -= AEGIS_BLOCK_SIZE;
  121. src_blk++;
  122. }
  123. } else {
  124. while (size >= AEGIS_BLOCK_SIZE) {
  125. crypto_aegis128_update_u(state, src);
  126. size -= AEGIS_BLOCK_SIZE;
  127. src += AEGIS_BLOCK_SIZE;
  128. }
  129. }
  130. }
  131. static void crypto_aegis128_encrypt_chunk(struct aegis_state *state, u8 *dst,
  132. const u8 *src, unsigned int size)
  133. {
  134. union aegis_block tmp;
  135. if (AEGIS_ALIGNED(src) && AEGIS_ALIGNED(dst)) {
  136. while (size >= AEGIS_BLOCK_SIZE) {
  137. union aegis_block *dst_blk =
  138. (union aegis_block *)dst;
  139. const union aegis_block *src_blk =
  140. (const union aegis_block *)src;
  141. tmp = state->blocks[2];
  142. crypto_aegis_block_and(&tmp, &state->blocks[3]);
  143. crypto_aegis_block_xor(&tmp, &state->blocks[4]);
  144. crypto_aegis_block_xor(&tmp, &state->blocks[1]);
  145. crypto_aegis_block_xor(&tmp, src_blk);
  146. crypto_aegis128_update_a(state, src_blk);
  147. *dst_blk = tmp;
  148. size -= AEGIS_BLOCK_SIZE;
  149. src += AEGIS_BLOCK_SIZE;
  150. dst += AEGIS_BLOCK_SIZE;
  151. }
  152. } else {
  153. while (size >= AEGIS_BLOCK_SIZE) {
  154. tmp = state->blocks[2];
  155. crypto_aegis_block_and(&tmp, &state->blocks[3]);
  156. crypto_aegis_block_xor(&tmp, &state->blocks[4]);
  157. crypto_aegis_block_xor(&tmp, &state->blocks[1]);
  158. crypto_xor(tmp.bytes, src, AEGIS_BLOCK_SIZE);
  159. crypto_aegis128_update_u(state, src);
  160. memcpy(dst, tmp.bytes, AEGIS_BLOCK_SIZE);
  161. size -= AEGIS_BLOCK_SIZE;
  162. src += AEGIS_BLOCK_SIZE;
  163. dst += AEGIS_BLOCK_SIZE;
  164. }
  165. }
  166. if (size > 0) {
  167. union aegis_block msg = {};
  168. memcpy(msg.bytes, src, size);
  169. tmp = state->blocks[2];
  170. crypto_aegis_block_and(&tmp, &state->blocks[3]);
  171. crypto_aegis_block_xor(&tmp, &state->blocks[4]);
  172. crypto_aegis_block_xor(&tmp, &state->blocks[1]);
  173. crypto_aegis128_update_a(state, &msg);
  174. crypto_aegis_block_xor(&msg, &tmp);
  175. memcpy(dst, msg.bytes, size);
  176. }
  177. }
  178. static void crypto_aegis128_decrypt_chunk(struct aegis_state *state, u8 *dst,
  179. const u8 *src, unsigned int size)
  180. {
  181. union aegis_block tmp;
  182. if (AEGIS_ALIGNED(src) && AEGIS_ALIGNED(dst)) {
  183. while (size >= AEGIS_BLOCK_SIZE) {
  184. union aegis_block *dst_blk =
  185. (union aegis_block *)dst;
  186. const union aegis_block *src_blk =
  187. (const union aegis_block *)src;
  188. tmp = state->blocks[2];
  189. crypto_aegis_block_and(&tmp, &state->blocks[3]);
  190. crypto_aegis_block_xor(&tmp, &state->blocks[4]);
  191. crypto_aegis_block_xor(&tmp, &state->blocks[1]);
  192. crypto_aegis_block_xor(&tmp, src_blk);
  193. crypto_aegis128_update_a(state, &tmp);
  194. *dst_blk = tmp;
  195. size -= AEGIS_BLOCK_SIZE;
  196. src += AEGIS_BLOCK_SIZE;
  197. dst += AEGIS_BLOCK_SIZE;
  198. }
  199. } else {
  200. while (size >= AEGIS_BLOCK_SIZE) {
  201. tmp = state->blocks[2];
  202. crypto_aegis_block_and(&tmp, &state->blocks[3]);
  203. crypto_aegis_block_xor(&tmp, &state->blocks[4]);
  204. crypto_aegis_block_xor(&tmp, &state->blocks[1]);
  205. crypto_xor(tmp.bytes, src, AEGIS_BLOCK_SIZE);
  206. crypto_aegis128_update_a(state, &tmp);
  207. memcpy(dst, tmp.bytes, AEGIS_BLOCK_SIZE);
  208. size -= AEGIS_BLOCK_SIZE;
  209. src += AEGIS_BLOCK_SIZE;
  210. dst += AEGIS_BLOCK_SIZE;
  211. }
  212. }
  213. if (size > 0) {
  214. union aegis_block msg = {};
  215. memcpy(msg.bytes, src, size);
  216. tmp = state->blocks[2];
  217. crypto_aegis_block_and(&tmp, &state->blocks[3]);
  218. crypto_aegis_block_xor(&tmp, &state->blocks[4]);
  219. crypto_aegis_block_xor(&tmp, &state->blocks[1]);
  220. crypto_aegis_block_xor(&msg, &tmp);
  221. memset(msg.bytes + size, 0, AEGIS_BLOCK_SIZE - size);
  222. crypto_aegis128_update_a(state, &msg);
  223. memcpy(dst, msg.bytes, size);
  224. }
  225. }
  226. static void crypto_aegis128_process_ad(struct aegis_state *state,
  227. struct scatterlist *sg_src,
  228. unsigned int assoclen)
  229. {
  230. struct scatter_walk walk;
  231. union aegis_block buf;
  232. unsigned int pos = 0;
  233. scatterwalk_start(&walk, sg_src);
  234. while (assoclen != 0) {
  235. unsigned int size = scatterwalk_clamp(&walk, assoclen);
  236. unsigned int left = size;
  237. void *mapped = scatterwalk_map(&walk);
  238. const u8 *src = (const u8 *)mapped;
  239. if (pos + size >= AEGIS_BLOCK_SIZE) {
  240. if (pos > 0) {
  241. unsigned int fill = AEGIS_BLOCK_SIZE - pos;
  242. memcpy(buf.bytes + pos, src, fill);
  243. crypto_aegis128_update_a(state, &buf);
  244. pos = 0;
  245. left -= fill;
  246. src += fill;
  247. }
  248. crypto_aegis128_ad(state, src, left);
  249. src += left & ~(AEGIS_BLOCK_SIZE - 1);
  250. left &= AEGIS_BLOCK_SIZE - 1;
  251. }
  252. memcpy(buf.bytes + pos, src, left);
  253. pos += left;
  254. assoclen -= size;
  255. scatterwalk_unmap(mapped);
  256. scatterwalk_advance(&walk, size);
  257. scatterwalk_done(&walk, 0, assoclen);
  258. }
  259. if (pos > 0) {
  260. memset(buf.bytes + pos, 0, AEGIS_BLOCK_SIZE - pos);
  261. crypto_aegis128_update_a(state, &buf);
  262. }
  263. }
  264. static __always_inline
  265. int crypto_aegis128_process_crypt(struct aegis_state *state,
  266. struct aead_request *req,
  267. struct skcipher_walk *walk,
  268. void (*crypt)(struct aegis_state *state,
  269. u8 *dst, const u8 *src,
  270. unsigned int size))
  271. {
  272. int err = 0;
  273. while (walk->nbytes) {
  274. unsigned int nbytes = walk->nbytes;
  275. if (nbytes < walk->total)
  276. nbytes = round_down(nbytes, walk->stride);
  277. crypt(state, walk->dst.virt.addr, walk->src.virt.addr, nbytes);
  278. err = skcipher_walk_done(walk, walk->nbytes - nbytes);
  279. }
  280. return err;
  281. }
  282. static void crypto_aegis128_final(struct aegis_state *state,
  283. union aegis_block *tag_xor,
  284. u64 assoclen, u64 cryptlen)
  285. {
  286. u64 assocbits = assoclen * 8;
  287. u64 cryptbits = cryptlen * 8;
  288. union aegis_block tmp;
  289. unsigned int i;
  290. tmp.words64[0] = cpu_to_le64(assocbits);
  291. tmp.words64[1] = cpu_to_le64(cryptbits);
  292. crypto_aegis_block_xor(&tmp, &state->blocks[3]);
  293. for (i = 0; i < 7; i++)
  294. crypto_aegis128_update_a(state, &tmp);
  295. for (i = 0; i < AEGIS128_STATE_BLOCKS; i++)
  296. crypto_aegis_block_xor(tag_xor, &state->blocks[i]);
  297. }
  298. static int crypto_aegis128_setkey(struct crypto_aead *aead, const u8 *key,
  299. unsigned int keylen)
  300. {
  301. struct aegis_ctx *ctx = crypto_aead_ctx(aead);
  302. if (keylen != AEGIS128_KEY_SIZE)
  303. return -EINVAL;
  304. memcpy(ctx->key.bytes, key, AEGIS128_KEY_SIZE);
  305. return 0;
  306. }
  307. static int crypto_aegis128_setauthsize(struct crypto_aead *tfm,
  308. unsigned int authsize)
  309. {
  310. if (authsize > AEGIS128_MAX_AUTH_SIZE)
  311. return -EINVAL;
  312. if (authsize < AEGIS128_MIN_AUTH_SIZE)
  313. return -EINVAL;
  314. return 0;
  315. }
  316. static int crypto_aegis128_encrypt(struct aead_request *req)
  317. {
  318. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  319. union aegis_block tag = {};
  320. unsigned int authsize = crypto_aead_authsize(tfm);
  321. struct aegis_ctx *ctx = crypto_aead_ctx(tfm);
  322. unsigned int cryptlen = req->cryptlen;
  323. struct skcipher_walk walk;
  324. struct aegis_state state;
  325. skcipher_walk_aead_encrypt(&walk, req, false);
  326. if (aegis128_do_simd()) {
  327. crypto_aegis128_init_simd(&state, &ctx->key, req->iv);
  328. crypto_aegis128_process_ad(&state, req->src, req->assoclen);
  329. crypto_aegis128_process_crypt(&state, req, &walk,
  330. crypto_aegis128_encrypt_chunk_simd);
  331. crypto_aegis128_final_simd(&state, &tag, req->assoclen,
  332. cryptlen);
  333. } else {
  334. crypto_aegis128_init(&state, &ctx->key, req->iv);
  335. crypto_aegis128_process_ad(&state, req->src, req->assoclen);
  336. crypto_aegis128_process_crypt(&state, req, &walk,
  337. crypto_aegis128_encrypt_chunk);
  338. crypto_aegis128_final(&state, &tag, req->assoclen, cryptlen);
  339. }
  340. scatterwalk_map_and_copy(tag.bytes, req->dst, req->assoclen + cryptlen,
  341. authsize, 1);
  342. return 0;
  343. }
  344. static int crypto_aegis128_decrypt(struct aead_request *req)
  345. {
  346. static const u8 zeros[AEGIS128_MAX_AUTH_SIZE] = {};
  347. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  348. union aegis_block tag;
  349. unsigned int authsize = crypto_aead_authsize(tfm);
  350. unsigned int cryptlen = req->cryptlen - authsize;
  351. struct aegis_ctx *ctx = crypto_aead_ctx(tfm);
  352. struct skcipher_walk walk;
  353. struct aegis_state state;
  354. scatterwalk_map_and_copy(tag.bytes, req->src, req->assoclen + cryptlen,
  355. authsize, 0);
  356. skcipher_walk_aead_decrypt(&walk, req, false);
  357. if (aegis128_do_simd()) {
  358. crypto_aegis128_init_simd(&state, &ctx->key, req->iv);
  359. crypto_aegis128_process_ad(&state, req->src, req->assoclen);
  360. crypto_aegis128_process_crypt(&state, req, &walk,
  361. crypto_aegis128_decrypt_chunk_simd);
  362. crypto_aegis128_final_simd(&state, &tag, req->assoclen,
  363. cryptlen);
  364. } else {
  365. crypto_aegis128_init(&state, &ctx->key, req->iv);
  366. crypto_aegis128_process_ad(&state, req->src, req->assoclen);
  367. crypto_aegis128_process_crypt(&state, req, &walk,
  368. crypto_aegis128_decrypt_chunk);
  369. crypto_aegis128_final(&state, &tag, req->assoclen, cryptlen);
  370. }
  371. return crypto_memneq(tag.bytes, zeros, authsize) ? -EBADMSG : 0;
  372. }
  373. static struct aead_alg crypto_aegis128_alg = {
  374. .setkey = crypto_aegis128_setkey,
  375. .setauthsize = crypto_aegis128_setauthsize,
  376. .encrypt = crypto_aegis128_encrypt,
  377. .decrypt = crypto_aegis128_decrypt,
  378. .ivsize = AEGIS128_NONCE_SIZE,
  379. .maxauthsize = AEGIS128_MAX_AUTH_SIZE,
  380. .chunksize = AEGIS_BLOCK_SIZE,
  381. .base = {
  382. .cra_blocksize = 1,
  383. .cra_ctxsize = sizeof(struct aegis_ctx),
  384. .cra_alignmask = 0,
  385. .cra_priority = 100,
  386. .cra_name = "aegis128",
  387. .cra_driver_name = "aegis128-generic",
  388. .cra_module = THIS_MODULE,
  389. }
  390. };
  391. static int __init crypto_aegis128_module_init(void)
  392. {
  393. if (IS_ENABLED(CONFIG_CRYPTO_AEGIS128_SIMD) &&
  394. crypto_aegis128_have_simd())
  395. static_branch_enable(&have_simd);
  396. return crypto_register_aead(&crypto_aegis128_alg);
  397. }
  398. static void __exit crypto_aegis128_module_exit(void)
  399. {
  400. crypto_unregister_aead(&crypto_aegis128_alg);
  401. }
  402. subsys_initcall(crypto_aegis128_module_init);
  403. module_exit(crypto_aegis128_module_exit);
  404. MODULE_LICENSE("GPL");
  405. MODULE_AUTHOR("Ondrej Mosnacek <omosnacek@gmail.com>");
  406. MODULE_DESCRIPTION("AEGIS-128 AEAD algorithm");
  407. MODULE_ALIAS_CRYPTO("aegis128");
  408. MODULE_ALIAS_CRYPTO("aegis128-generic");