aegis128-core.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. static void crypto_aegis128_update(struct aegis_state *state)
  52. {
  53. union aegis_block tmp;
  54. unsigned int i;
  55. tmp = state->blocks[AEGIS128_STATE_BLOCKS - 1];
  56. for (i = AEGIS128_STATE_BLOCKS - 1; i > 0; i--)
  57. crypto_aegis_aesenc(&state->blocks[i], &state->blocks[i - 1],
  58. &state->blocks[i]);
  59. crypto_aegis_aesenc(&state->blocks[0], &tmp, &state->blocks[0]);
  60. }
  61. static void crypto_aegis128_update_a(struct aegis_state *state,
  62. const union aegis_block *msg,
  63. bool do_simd)
  64. {
  65. if (IS_ENABLED(CONFIG_CRYPTO_AEGIS128_SIMD) && do_simd) {
  66. crypto_aegis128_update_simd(state, msg);
  67. return;
  68. }
  69. crypto_aegis128_update(state);
  70. crypto_aegis_block_xor(&state->blocks[0], msg);
  71. }
  72. static void crypto_aegis128_update_u(struct aegis_state *state, const void *msg,
  73. bool do_simd)
  74. {
  75. if (IS_ENABLED(CONFIG_CRYPTO_AEGIS128_SIMD) && do_simd) {
  76. crypto_aegis128_update_simd(state, msg);
  77. return;
  78. }
  79. crypto_aegis128_update(state);
  80. crypto_xor(state->blocks[0].bytes, msg, AEGIS_BLOCK_SIZE);
  81. }
  82. static void crypto_aegis128_init(struct aegis_state *state,
  83. const union aegis_block *key,
  84. const u8 *iv)
  85. {
  86. union aegis_block key_iv;
  87. unsigned int i;
  88. key_iv = *key;
  89. crypto_xor(key_iv.bytes, iv, AEGIS_BLOCK_SIZE);
  90. state->blocks[0] = key_iv;
  91. state->blocks[1] = crypto_aegis_const[1];
  92. state->blocks[2] = crypto_aegis_const[0];
  93. state->blocks[3] = *key;
  94. state->blocks[4] = *key;
  95. crypto_aegis_block_xor(&state->blocks[3], &crypto_aegis_const[0]);
  96. crypto_aegis_block_xor(&state->blocks[4], &crypto_aegis_const[1]);
  97. for (i = 0; i < 5; i++) {
  98. crypto_aegis128_update_a(state, key, false);
  99. crypto_aegis128_update_a(state, &key_iv, false);
  100. }
  101. }
  102. static void crypto_aegis128_ad(struct aegis_state *state,
  103. const u8 *src, unsigned int size,
  104. bool do_simd)
  105. {
  106. if (AEGIS_ALIGNED(src)) {
  107. const union aegis_block *src_blk =
  108. (const union aegis_block *)src;
  109. while (size >= AEGIS_BLOCK_SIZE) {
  110. crypto_aegis128_update_a(state, src_blk, do_simd);
  111. size -= AEGIS_BLOCK_SIZE;
  112. src_blk++;
  113. }
  114. } else {
  115. while (size >= AEGIS_BLOCK_SIZE) {
  116. crypto_aegis128_update_u(state, src, do_simd);
  117. size -= AEGIS_BLOCK_SIZE;
  118. src += AEGIS_BLOCK_SIZE;
  119. }
  120. }
  121. }
  122. static void crypto_aegis128_wipe_chunk(struct aegis_state *state, u8 *dst,
  123. const u8 *src, unsigned int size)
  124. {
  125. memzero_explicit(dst, size);
  126. }
  127. static void crypto_aegis128_encrypt_chunk(struct aegis_state *state, u8 *dst,
  128. const u8 *src, unsigned int size)
  129. {
  130. union aegis_block tmp;
  131. if (AEGIS_ALIGNED(src) && AEGIS_ALIGNED(dst)) {
  132. while (size >= AEGIS_BLOCK_SIZE) {
  133. union aegis_block *dst_blk =
  134. (union aegis_block *)dst;
  135. const union aegis_block *src_blk =
  136. (const union aegis_block *)src;
  137. tmp = state->blocks[2];
  138. crypto_aegis_block_and(&tmp, &state->blocks[3]);
  139. crypto_aegis_block_xor(&tmp, &state->blocks[4]);
  140. crypto_aegis_block_xor(&tmp, &state->blocks[1]);
  141. crypto_aegis_block_xor(&tmp, src_blk);
  142. crypto_aegis128_update_a(state, src_blk, false);
  143. *dst_blk = tmp;
  144. size -= AEGIS_BLOCK_SIZE;
  145. src += AEGIS_BLOCK_SIZE;
  146. dst += AEGIS_BLOCK_SIZE;
  147. }
  148. } else {
  149. while (size >= AEGIS_BLOCK_SIZE) {
  150. tmp = state->blocks[2];
  151. crypto_aegis_block_and(&tmp, &state->blocks[3]);
  152. crypto_aegis_block_xor(&tmp, &state->blocks[4]);
  153. crypto_aegis_block_xor(&tmp, &state->blocks[1]);
  154. crypto_xor(tmp.bytes, src, AEGIS_BLOCK_SIZE);
  155. crypto_aegis128_update_u(state, src, false);
  156. memcpy(dst, tmp.bytes, AEGIS_BLOCK_SIZE);
  157. size -= AEGIS_BLOCK_SIZE;
  158. src += AEGIS_BLOCK_SIZE;
  159. dst += AEGIS_BLOCK_SIZE;
  160. }
  161. }
  162. if (size > 0) {
  163. union aegis_block msg = {};
  164. memcpy(msg.bytes, src, size);
  165. tmp = state->blocks[2];
  166. crypto_aegis_block_and(&tmp, &state->blocks[3]);
  167. crypto_aegis_block_xor(&tmp, &state->blocks[4]);
  168. crypto_aegis_block_xor(&tmp, &state->blocks[1]);
  169. crypto_aegis128_update_a(state, &msg, false);
  170. crypto_aegis_block_xor(&msg, &tmp);
  171. memcpy(dst, msg.bytes, size);
  172. }
  173. }
  174. static void crypto_aegis128_decrypt_chunk(struct aegis_state *state, u8 *dst,
  175. const u8 *src, unsigned int size)
  176. {
  177. union aegis_block tmp;
  178. if (AEGIS_ALIGNED(src) && AEGIS_ALIGNED(dst)) {
  179. while (size >= AEGIS_BLOCK_SIZE) {
  180. union aegis_block *dst_blk =
  181. (union aegis_block *)dst;
  182. const union aegis_block *src_blk =
  183. (const union aegis_block *)src;
  184. tmp = state->blocks[2];
  185. crypto_aegis_block_and(&tmp, &state->blocks[3]);
  186. crypto_aegis_block_xor(&tmp, &state->blocks[4]);
  187. crypto_aegis_block_xor(&tmp, &state->blocks[1]);
  188. crypto_aegis_block_xor(&tmp, src_blk);
  189. crypto_aegis128_update_a(state, &tmp, false);
  190. *dst_blk = tmp;
  191. size -= AEGIS_BLOCK_SIZE;
  192. src += AEGIS_BLOCK_SIZE;
  193. dst += AEGIS_BLOCK_SIZE;
  194. }
  195. } else {
  196. while (size >= AEGIS_BLOCK_SIZE) {
  197. tmp = state->blocks[2];
  198. crypto_aegis_block_and(&tmp, &state->blocks[3]);
  199. crypto_aegis_block_xor(&tmp, &state->blocks[4]);
  200. crypto_aegis_block_xor(&tmp, &state->blocks[1]);
  201. crypto_xor(tmp.bytes, src, AEGIS_BLOCK_SIZE);
  202. crypto_aegis128_update_a(state, &tmp, false);
  203. memcpy(dst, tmp.bytes, AEGIS_BLOCK_SIZE);
  204. size -= AEGIS_BLOCK_SIZE;
  205. src += AEGIS_BLOCK_SIZE;
  206. dst += AEGIS_BLOCK_SIZE;
  207. }
  208. }
  209. if (size > 0) {
  210. union aegis_block msg = {};
  211. memcpy(msg.bytes, src, size);
  212. tmp = state->blocks[2];
  213. crypto_aegis_block_and(&tmp, &state->blocks[3]);
  214. crypto_aegis_block_xor(&tmp, &state->blocks[4]);
  215. crypto_aegis_block_xor(&tmp, &state->blocks[1]);
  216. crypto_aegis_block_xor(&msg, &tmp);
  217. memset(msg.bytes + size, 0, AEGIS_BLOCK_SIZE - size);
  218. crypto_aegis128_update_a(state, &msg, false);
  219. memcpy(dst, msg.bytes, size);
  220. }
  221. }
  222. static void crypto_aegis128_process_ad(struct aegis_state *state,
  223. struct scatterlist *sg_src,
  224. unsigned int assoclen,
  225. bool do_simd)
  226. {
  227. struct scatter_walk walk;
  228. union aegis_block buf;
  229. unsigned int pos = 0;
  230. scatterwalk_start(&walk, sg_src);
  231. while (assoclen != 0) {
  232. unsigned int size = scatterwalk_next(&walk, assoclen);
  233. const u8 *src = walk.addr;
  234. unsigned int left = size;
  235. if (pos + size >= AEGIS_BLOCK_SIZE) {
  236. if (pos > 0) {
  237. unsigned int fill = AEGIS_BLOCK_SIZE - pos;
  238. memcpy(buf.bytes + pos, src, fill);
  239. crypto_aegis128_update_a(state, &buf, do_simd);
  240. pos = 0;
  241. left -= fill;
  242. src += fill;
  243. }
  244. crypto_aegis128_ad(state, src, left, do_simd);
  245. src += left & ~(AEGIS_BLOCK_SIZE - 1);
  246. left &= AEGIS_BLOCK_SIZE - 1;
  247. }
  248. memcpy(buf.bytes + pos, src, left);
  249. pos += left;
  250. assoclen -= size;
  251. scatterwalk_done_src(&walk, size);
  252. }
  253. if (pos > 0) {
  254. memset(buf.bytes + pos, 0, AEGIS_BLOCK_SIZE - pos);
  255. crypto_aegis128_update_a(state, &buf, do_simd);
  256. }
  257. }
  258. static __always_inline
  259. int crypto_aegis128_process_crypt(struct aegis_state *state,
  260. struct skcipher_walk *walk,
  261. void (*crypt)(struct aegis_state *state,
  262. u8 *dst,
  263. const u8 *src,
  264. unsigned int size))
  265. {
  266. int err = 0;
  267. while (walk->nbytes) {
  268. unsigned int nbytes = walk->nbytes;
  269. if (nbytes < walk->total)
  270. nbytes = round_down(nbytes, walk->stride);
  271. crypt(state, walk->dst.virt.addr, walk->src.virt.addr, nbytes);
  272. err = skcipher_walk_done(walk, walk->nbytes - nbytes);
  273. }
  274. return err;
  275. }
  276. static void crypto_aegis128_final(struct aegis_state *state,
  277. union aegis_block *tag_xor,
  278. u64 assoclen, u64 cryptlen)
  279. {
  280. u64 assocbits = assoclen * 8;
  281. u64 cryptbits = cryptlen * 8;
  282. union aegis_block tmp;
  283. unsigned int i;
  284. tmp.words64[0] = cpu_to_le64(assocbits);
  285. tmp.words64[1] = cpu_to_le64(cryptbits);
  286. crypto_aegis_block_xor(&tmp, &state->blocks[3]);
  287. for (i = 0; i < 7; i++)
  288. crypto_aegis128_update_a(state, &tmp, false);
  289. for (i = 0; i < AEGIS128_STATE_BLOCKS; i++)
  290. crypto_aegis_block_xor(tag_xor, &state->blocks[i]);
  291. }
  292. static int crypto_aegis128_setkey(struct crypto_aead *aead, const u8 *key,
  293. unsigned int keylen)
  294. {
  295. struct aegis_ctx *ctx = crypto_aead_ctx(aead);
  296. if (keylen != AEGIS128_KEY_SIZE)
  297. return -EINVAL;
  298. memcpy(ctx->key.bytes, key, AEGIS128_KEY_SIZE);
  299. return 0;
  300. }
  301. static int crypto_aegis128_setauthsize(struct crypto_aead *tfm,
  302. unsigned int authsize)
  303. {
  304. if (authsize > AEGIS128_MAX_AUTH_SIZE)
  305. return -EINVAL;
  306. if (authsize < AEGIS128_MIN_AUTH_SIZE)
  307. return -EINVAL;
  308. return 0;
  309. }
  310. static int crypto_aegis128_encrypt_generic(struct aead_request *req)
  311. {
  312. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  313. union aegis_block tag = {};
  314. unsigned int authsize = crypto_aead_authsize(tfm);
  315. struct aegis_ctx *ctx = crypto_aead_ctx(tfm);
  316. unsigned int cryptlen = req->cryptlen;
  317. struct skcipher_walk walk;
  318. struct aegis_state state;
  319. skcipher_walk_aead_encrypt(&walk, req, false);
  320. crypto_aegis128_init(&state, &ctx->key, req->iv);
  321. crypto_aegis128_process_ad(&state, req->src, req->assoclen, false);
  322. crypto_aegis128_process_crypt(&state, &walk,
  323. crypto_aegis128_encrypt_chunk);
  324. crypto_aegis128_final(&state, &tag, req->assoclen, cryptlen);
  325. scatterwalk_map_and_copy(tag.bytes, req->dst, req->assoclen + cryptlen,
  326. authsize, 1);
  327. return 0;
  328. }
  329. static int crypto_aegis128_decrypt_generic(struct aead_request *req)
  330. {
  331. static const u8 zeros[AEGIS128_MAX_AUTH_SIZE] = {};
  332. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  333. union aegis_block tag;
  334. unsigned int authsize = crypto_aead_authsize(tfm);
  335. unsigned int cryptlen = req->cryptlen - authsize;
  336. struct aegis_ctx *ctx = crypto_aead_ctx(tfm);
  337. struct skcipher_walk walk;
  338. struct aegis_state state;
  339. scatterwalk_map_and_copy(tag.bytes, req->src, req->assoclen + cryptlen,
  340. authsize, 0);
  341. skcipher_walk_aead_decrypt(&walk, req, false);
  342. crypto_aegis128_init(&state, &ctx->key, req->iv);
  343. crypto_aegis128_process_ad(&state, req->src, req->assoclen, false);
  344. crypto_aegis128_process_crypt(&state, &walk,
  345. crypto_aegis128_decrypt_chunk);
  346. crypto_aegis128_final(&state, &tag, req->assoclen, cryptlen);
  347. if (unlikely(crypto_memneq(tag.bytes, zeros, authsize))) {
  348. /*
  349. * From Chapter 4. 'Security Analysis' of the AEGIS spec [0]
  350. *
  351. * "3. If verification fails, the decrypted plaintext and the
  352. * wrong authentication tag should not be given as output."
  353. *
  354. * [0] https://competitions.cr.yp.to/round3/aegisv11.pdf
  355. */
  356. skcipher_walk_aead_decrypt(&walk, req, false);
  357. crypto_aegis128_process_crypt(NULL, &walk,
  358. crypto_aegis128_wipe_chunk);
  359. memzero_explicit(&tag, sizeof(tag));
  360. return -EBADMSG;
  361. }
  362. return 0;
  363. }
  364. static int crypto_aegis128_encrypt_simd(struct aead_request *req)
  365. {
  366. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  367. union aegis_block tag = {};
  368. unsigned int authsize = crypto_aead_authsize(tfm);
  369. struct aegis_ctx *ctx = crypto_aead_ctx(tfm);
  370. unsigned int cryptlen = req->cryptlen;
  371. struct skcipher_walk walk;
  372. struct aegis_state state;
  373. if (!aegis128_do_simd())
  374. return crypto_aegis128_encrypt_generic(req);
  375. skcipher_walk_aead_encrypt(&walk, req, false);
  376. crypto_aegis128_init_simd(&state, &ctx->key, req->iv);
  377. crypto_aegis128_process_ad(&state, req->src, req->assoclen, true);
  378. crypto_aegis128_process_crypt(&state, &walk,
  379. crypto_aegis128_encrypt_chunk_simd);
  380. crypto_aegis128_final_simd(&state, &tag, req->assoclen, cryptlen, 0);
  381. scatterwalk_map_and_copy(tag.bytes, req->dst, req->assoclen + cryptlen,
  382. authsize, 1);
  383. return 0;
  384. }
  385. static int crypto_aegis128_decrypt_simd(struct aead_request *req)
  386. {
  387. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  388. union aegis_block tag;
  389. unsigned int authsize = crypto_aead_authsize(tfm);
  390. unsigned int cryptlen = req->cryptlen - authsize;
  391. struct aegis_ctx *ctx = crypto_aead_ctx(tfm);
  392. struct skcipher_walk walk;
  393. struct aegis_state state;
  394. if (!aegis128_do_simd())
  395. return crypto_aegis128_decrypt_generic(req);
  396. scatterwalk_map_and_copy(tag.bytes, req->src, req->assoclen + cryptlen,
  397. authsize, 0);
  398. skcipher_walk_aead_decrypt(&walk, req, false);
  399. crypto_aegis128_init_simd(&state, &ctx->key, req->iv);
  400. crypto_aegis128_process_ad(&state, req->src, req->assoclen, true);
  401. crypto_aegis128_process_crypt(&state, &walk,
  402. crypto_aegis128_decrypt_chunk_simd);
  403. if (unlikely(crypto_aegis128_final_simd(&state, &tag, req->assoclen,
  404. cryptlen, authsize))) {
  405. skcipher_walk_aead_decrypt(&walk, req, false);
  406. crypto_aegis128_process_crypt(NULL, &walk,
  407. crypto_aegis128_wipe_chunk);
  408. return -EBADMSG;
  409. }
  410. return 0;
  411. }
  412. static struct aead_alg crypto_aegis128_alg_generic = {
  413. .setkey = crypto_aegis128_setkey,
  414. .setauthsize = crypto_aegis128_setauthsize,
  415. .encrypt = crypto_aegis128_encrypt_generic,
  416. .decrypt = crypto_aegis128_decrypt_generic,
  417. .ivsize = AEGIS128_NONCE_SIZE,
  418. .maxauthsize = AEGIS128_MAX_AUTH_SIZE,
  419. .chunksize = AEGIS_BLOCK_SIZE,
  420. .base.cra_blocksize = 1,
  421. .base.cra_ctxsize = sizeof(struct aegis_ctx),
  422. .base.cra_priority = 100,
  423. .base.cra_name = "aegis128",
  424. .base.cra_driver_name = "aegis128-generic",
  425. .base.cra_module = THIS_MODULE,
  426. };
  427. static struct aead_alg crypto_aegis128_alg_simd = {
  428. .setkey = crypto_aegis128_setkey,
  429. .setauthsize = crypto_aegis128_setauthsize,
  430. .encrypt = crypto_aegis128_encrypt_simd,
  431. .decrypt = crypto_aegis128_decrypt_simd,
  432. .ivsize = AEGIS128_NONCE_SIZE,
  433. .maxauthsize = AEGIS128_MAX_AUTH_SIZE,
  434. .chunksize = AEGIS_BLOCK_SIZE,
  435. .base.cra_blocksize = 1,
  436. .base.cra_ctxsize = sizeof(struct aegis_ctx),
  437. .base.cra_priority = 200,
  438. .base.cra_name = "aegis128",
  439. .base.cra_driver_name = "aegis128-simd",
  440. .base.cra_module = THIS_MODULE,
  441. };
  442. static int __init crypto_aegis128_module_init(void)
  443. {
  444. int ret;
  445. ret = crypto_register_aead(&crypto_aegis128_alg_generic);
  446. if (ret)
  447. return ret;
  448. if (IS_ENABLED(CONFIG_CRYPTO_AEGIS128_SIMD) &&
  449. crypto_aegis128_have_simd()) {
  450. ret = crypto_register_aead(&crypto_aegis128_alg_simd);
  451. if (ret) {
  452. crypto_unregister_aead(&crypto_aegis128_alg_generic);
  453. return ret;
  454. }
  455. static_branch_enable(&have_simd);
  456. }
  457. return 0;
  458. }
  459. static void __exit crypto_aegis128_module_exit(void)
  460. {
  461. if (IS_ENABLED(CONFIG_CRYPTO_AEGIS128_SIMD) &&
  462. crypto_aegis128_have_simd())
  463. crypto_unregister_aead(&crypto_aegis128_alg_simd);
  464. crypto_unregister_aead(&crypto_aegis128_alg_generic);
  465. }
  466. module_init(crypto_aegis128_module_init);
  467. module_exit(crypto_aegis128_module_exit);
  468. MODULE_LICENSE("GPL");
  469. MODULE_AUTHOR("Ondrej Mosnacek <omosnacek@gmail.com>");
  470. MODULE_DESCRIPTION("AEGIS-128 AEAD algorithm");
  471. MODULE_ALIAS_CRYPTO("aegis128");
  472. MODULE_ALIAS_CRYPTO("aegis128-generic");
  473. MODULE_ALIAS_CRYPTO("aegis128-simd");