aegis128l.c 14 KB

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