aegis256.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /*
  2. * The AEGIS-256 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 AEGIS256_NONCE_SIZE 32
  23. #define AEGIS256_STATE_BLOCKS 6
  24. #define AEGIS256_KEY_SIZE 32
  25. #define AEGIS256_MIN_AUTH_SIZE 8
  26. #define AEGIS256_MAX_AUTH_SIZE 16
  27. struct aegis_state {
  28. union aegis_block blocks[AEGIS256_STATE_BLOCKS];
  29. };
  30. struct aegis_ctx {
  31. union aegis_block key[AEGIS256_KEY_SIZE / AEGIS_BLOCK_SIZE];
  32. };
  33. struct aegis256_ops {
  34. int (*skcipher_walk_init)(struct skcipher_walk *walk,
  35. struct aead_request *req, bool atomic);
  36. void (*crypt_chunk)(struct aegis_state *state, u8 *dst,
  37. const u8 *src, unsigned int size);
  38. };
  39. static void crypto_aegis256_update(struct aegis_state *state)
  40. {
  41. union aegis_block tmp;
  42. unsigned int i;
  43. tmp = state->blocks[AEGIS256_STATE_BLOCKS - 1];
  44. for (i = AEGIS256_STATE_BLOCKS - 1; i > 0; i--)
  45. crypto_aegis_aesenc(&state->blocks[i], &state->blocks[i - 1],
  46. &state->blocks[i]);
  47. crypto_aegis_aesenc(&state->blocks[0], &tmp, &state->blocks[0]);
  48. }
  49. static void crypto_aegis256_update_a(struct aegis_state *state,
  50. const union aegis_block *msg)
  51. {
  52. crypto_aegis256_update(state);
  53. crypto_aegis_block_xor(&state->blocks[0], msg);
  54. }
  55. static void crypto_aegis256_update_u(struct aegis_state *state, const void *msg)
  56. {
  57. crypto_aegis256_update(state);
  58. crypto_xor(state->blocks[0].bytes, msg, AEGIS_BLOCK_SIZE);
  59. }
  60. static void crypto_aegis256_init(struct aegis_state *state,
  61. const union aegis_block *key,
  62. const u8 *iv)
  63. {
  64. union aegis_block key_iv[2];
  65. unsigned int i;
  66. key_iv[0] = key[0];
  67. key_iv[1] = key[1];
  68. crypto_xor(key_iv[0].bytes, iv + 0 * AEGIS_BLOCK_SIZE,
  69. AEGIS_BLOCK_SIZE);
  70. crypto_xor(key_iv[1].bytes, iv + 1 * AEGIS_BLOCK_SIZE,
  71. AEGIS_BLOCK_SIZE);
  72. state->blocks[0] = key_iv[0];
  73. state->blocks[1] = key_iv[1];
  74. state->blocks[2] = crypto_aegis_const[1];
  75. state->blocks[3] = crypto_aegis_const[0];
  76. state->blocks[4] = key[0];
  77. state->blocks[5] = key[1];
  78. crypto_aegis_block_xor(&state->blocks[4], &crypto_aegis_const[0]);
  79. crypto_aegis_block_xor(&state->blocks[5], &crypto_aegis_const[1]);
  80. for (i = 0; i < 4; i++) {
  81. crypto_aegis256_update_a(state, &key[0]);
  82. crypto_aegis256_update_a(state, &key[1]);
  83. crypto_aegis256_update_a(state, &key_iv[0]);
  84. crypto_aegis256_update_a(state, &key_iv[1]);
  85. }
  86. }
  87. static void crypto_aegis256_ad(struct aegis_state *state,
  88. const u8 *src, unsigned int size)
  89. {
  90. if (AEGIS_ALIGNED(src)) {
  91. const union aegis_block *src_blk =
  92. (const union aegis_block *)src;
  93. while (size >= AEGIS_BLOCK_SIZE) {
  94. crypto_aegis256_update_a(state, src_blk);
  95. size -= AEGIS_BLOCK_SIZE;
  96. src_blk++;
  97. }
  98. } else {
  99. while (size >= AEGIS_BLOCK_SIZE) {
  100. crypto_aegis256_update_u(state, src);
  101. size -= AEGIS_BLOCK_SIZE;
  102. src += AEGIS_BLOCK_SIZE;
  103. }
  104. }
  105. }
  106. static void crypto_aegis256_encrypt_chunk(struct aegis_state *state, u8 *dst,
  107. const u8 *src, unsigned int size)
  108. {
  109. union aegis_block tmp;
  110. if (AEGIS_ALIGNED(src) && AEGIS_ALIGNED(dst)) {
  111. while (size >= AEGIS_BLOCK_SIZE) {
  112. union aegis_block *dst_blk =
  113. (union aegis_block *)dst;
  114. const union aegis_block *src_blk =
  115. (const union aegis_block *)src;
  116. tmp = state->blocks[2];
  117. crypto_aegis_block_and(&tmp, &state->blocks[3]);
  118. crypto_aegis_block_xor(&tmp, &state->blocks[5]);
  119. crypto_aegis_block_xor(&tmp, &state->blocks[4]);
  120. crypto_aegis_block_xor(&tmp, &state->blocks[1]);
  121. crypto_aegis_block_xor(&tmp, src_blk);
  122. crypto_aegis256_update_a(state, src_blk);
  123. *dst_blk = tmp;
  124. size -= AEGIS_BLOCK_SIZE;
  125. src += AEGIS_BLOCK_SIZE;
  126. dst += AEGIS_BLOCK_SIZE;
  127. }
  128. } else {
  129. while (size >= AEGIS_BLOCK_SIZE) {
  130. tmp = state->blocks[2];
  131. crypto_aegis_block_and(&tmp, &state->blocks[3]);
  132. crypto_aegis_block_xor(&tmp, &state->blocks[5]);
  133. crypto_aegis_block_xor(&tmp, &state->blocks[4]);
  134. crypto_aegis_block_xor(&tmp, &state->blocks[1]);
  135. crypto_xor(tmp.bytes, src, AEGIS_BLOCK_SIZE);
  136. crypto_aegis256_update_u(state, src);
  137. memcpy(dst, tmp.bytes, AEGIS_BLOCK_SIZE);
  138. size -= AEGIS_BLOCK_SIZE;
  139. src += AEGIS_BLOCK_SIZE;
  140. dst += AEGIS_BLOCK_SIZE;
  141. }
  142. }
  143. if (size > 0) {
  144. union aegis_block msg = {};
  145. memcpy(msg.bytes, src, size);
  146. tmp = state->blocks[2];
  147. crypto_aegis_block_and(&tmp, &state->blocks[3]);
  148. crypto_aegis_block_xor(&tmp, &state->blocks[5]);
  149. crypto_aegis_block_xor(&tmp, &state->blocks[4]);
  150. crypto_aegis_block_xor(&tmp, &state->blocks[1]);
  151. crypto_aegis256_update_a(state, &msg);
  152. crypto_aegis_block_xor(&msg, &tmp);
  153. memcpy(dst, msg.bytes, size);
  154. }
  155. }
  156. static void crypto_aegis256_decrypt_chunk(struct aegis_state *state, u8 *dst,
  157. const u8 *src, unsigned int size)
  158. {
  159. union aegis_block tmp;
  160. if (AEGIS_ALIGNED(src) && AEGIS_ALIGNED(dst)) {
  161. while (size >= AEGIS_BLOCK_SIZE) {
  162. union aegis_block *dst_blk =
  163. (union aegis_block *)dst;
  164. const union aegis_block *src_blk =
  165. (const union aegis_block *)src;
  166. tmp = state->blocks[2];
  167. crypto_aegis_block_and(&tmp, &state->blocks[3]);
  168. crypto_aegis_block_xor(&tmp, &state->blocks[5]);
  169. crypto_aegis_block_xor(&tmp, &state->blocks[4]);
  170. crypto_aegis_block_xor(&tmp, &state->blocks[1]);
  171. crypto_aegis_block_xor(&tmp, src_blk);
  172. crypto_aegis256_update_a(state, &tmp);
  173. *dst_blk = tmp;
  174. size -= AEGIS_BLOCK_SIZE;
  175. src += AEGIS_BLOCK_SIZE;
  176. dst += AEGIS_BLOCK_SIZE;
  177. }
  178. } else {
  179. while (size >= AEGIS_BLOCK_SIZE) {
  180. tmp = state->blocks[2];
  181. crypto_aegis_block_and(&tmp, &state->blocks[3]);
  182. crypto_aegis_block_xor(&tmp, &state->blocks[5]);
  183. crypto_aegis_block_xor(&tmp, &state->blocks[4]);
  184. crypto_aegis_block_xor(&tmp, &state->blocks[1]);
  185. crypto_xor(tmp.bytes, src, AEGIS_BLOCK_SIZE);
  186. crypto_aegis256_update_a(state, &tmp);
  187. memcpy(dst, tmp.bytes, AEGIS_BLOCK_SIZE);
  188. size -= AEGIS_BLOCK_SIZE;
  189. src += AEGIS_BLOCK_SIZE;
  190. dst += AEGIS_BLOCK_SIZE;
  191. }
  192. }
  193. if (size > 0) {
  194. union aegis_block msg = {};
  195. memcpy(msg.bytes, src, size);
  196. tmp = state->blocks[2];
  197. crypto_aegis_block_and(&tmp, &state->blocks[3]);
  198. crypto_aegis_block_xor(&tmp, &state->blocks[5]);
  199. crypto_aegis_block_xor(&tmp, &state->blocks[4]);
  200. crypto_aegis_block_xor(&tmp, &state->blocks[1]);
  201. crypto_aegis_block_xor(&msg, &tmp);
  202. memset(msg.bytes + size, 0, AEGIS_BLOCK_SIZE - size);
  203. crypto_aegis256_update_a(state, &msg);
  204. memcpy(dst, msg.bytes, size);
  205. }
  206. }
  207. static void crypto_aegis256_process_ad(struct aegis_state *state,
  208. struct scatterlist *sg_src,
  209. unsigned int assoclen)
  210. {
  211. struct scatter_walk walk;
  212. union aegis_block buf;
  213. unsigned int pos = 0;
  214. scatterwalk_start(&walk, sg_src);
  215. while (assoclen != 0) {
  216. unsigned int size = scatterwalk_clamp(&walk, assoclen);
  217. unsigned int left = size;
  218. void *mapped = scatterwalk_map(&walk);
  219. const u8 *src = (const u8 *)mapped;
  220. if (pos + size >= AEGIS_BLOCK_SIZE) {
  221. if (pos > 0) {
  222. unsigned int fill = AEGIS_BLOCK_SIZE - pos;
  223. memcpy(buf.bytes + pos, src, fill);
  224. crypto_aegis256_update_a(state, &buf);
  225. pos = 0;
  226. left -= fill;
  227. src += fill;
  228. }
  229. crypto_aegis256_ad(state, src, left);
  230. src += left & ~(AEGIS_BLOCK_SIZE - 1);
  231. left &= AEGIS_BLOCK_SIZE - 1;
  232. }
  233. memcpy(buf.bytes + pos, src, left);
  234. pos += left;
  235. assoclen -= size;
  236. scatterwalk_unmap(mapped);
  237. scatterwalk_advance(&walk, size);
  238. scatterwalk_done(&walk, 0, assoclen);
  239. }
  240. if (pos > 0) {
  241. memset(buf.bytes + pos, 0, AEGIS_BLOCK_SIZE - pos);
  242. crypto_aegis256_update_a(state, &buf);
  243. }
  244. }
  245. static void crypto_aegis256_process_crypt(struct aegis_state *state,
  246. struct aead_request *req,
  247. const struct aegis256_ops *ops)
  248. {
  249. struct skcipher_walk walk;
  250. ops->skcipher_walk_init(&walk, req, false);
  251. while (walk.nbytes) {
  252. unsigned int nbytes = walk.nbytes;
  253. if (nbytes < walk.total)
  254. nbytes = round_down(nbytes, walk.stride);
  255. ops->crypt_chunk(state, walk.dst.virt.addr, walk.src.virt.addr,
  256. nbytes);
  257. skcipher_walk_done(&walk, walk.nbytes - nbytes);
  258. }
  259. }
  260. static void crypto_aegis256_final(struct aegis_state *state,
  261. union aegis_block *tag_xor,
  262. u64 assoclen, u64 cryptlen)
  263. {
  264. u64 assocbits = assoclen * 8;
  265. u64 cryptbits = cryptlen * 8;
  266. union aegis_block tmp;
  267. unsigned int i;
  268. tmp.words64[0] = cpu_to_le64(assocbits);
  269. tmp.words64[1] = cpu_to_le64(cryptbits);
  270. crypto_aegis_block_xor(&tmp, &state->blocks[3]);
  271. for (i = 0; i < 7; i++)
  272. crypto_aegis256_update_a(state, &tmp);
  273. for (i = 0; i < AEGIS256_STATE_BLOCKS; i++)
  274. crypto_aegis_block_xor(tag_xor, &state->blocks[i]);
  275. }
  276. static int crypto_aegis256_setkey(struct crypto_aead *aead, const u8 *key,
  277. unsigned int keylen)
  278. {
  279. struct aegis_ctx *ctx = crypto_aead_ctx(aead);
  280. if (keylen != AEGIS256_KEY_SIZE) {
  281. crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  282. return -EINVAL;
  283. }
  284. memcpy(ctx->key[0].bytes, key, AEGIS_BLOCK_SIZE);
  285. memcpy(ctx->key[1].bytes, key + AEGIS_BLOCK_SIZE,
  286. AEGIS_BLOCK_SIZE);
  287. return 0;
  288. }
  289. static int crypto_aegis256_setauthsize(struct crypto_aead *tfm,
  290. unsigned int authsize)
  291. {
  292. if (authsize > AEGIS256_MAX_AUTH_SIZE)
  293. return -EINVAL;
  294. if (authsize < AEGIS256_MIN_AUTH_SIZE)
  295. return -EINVAL;
  296. return 0;
  297. }
  298. static void crypto_aegis256_crypt(struct aead_request *req,
  299. union aegis_block *tag_xor,
  300. unsigned int cryptlen,
  301. const struct aegis256_ops *ops)
  302. {
  303. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  304. struct aegis_ctx *ctx = crypto_aead_ctx(tfm);
  305. struct aegis_state state;
  306. crypto_aegis256_init(&state, ctx->key, req->iv);
  307. crypto_aegis256_process_ad(&state, req->src, req->assoclen);
  308. crypto_aegis256_process_crypt(&state, req, ops);
  309. crypto_aegis256_final(&state, tag_xor, req->assoclen, cryptlen);
  310. }
  311. static int crypto_aegis256_encrypt(struct aead_request *req)
  312. {
  313. static const struct aegis256_ops ops = {
  314. .skcipher_walk_init = skcipher_walk_aead_encrypt,
  315. .crypt_chunk = crypto_aegis256_encrypt_chunk,
  316. };
  317. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  318. union aegis_block tag = {};
  319. unsigned int authsize = crypto_aead_authsize(tfm);
  320. unsigned int cryptlen = req->cryptlen;
  321. crypto_aegis256_crypt(req, &tag, cryptlen, &ops);
  322. scatterwalk_map_and_copy(tag.bytes, req->dst, req->assoclen + cryptlen,
  323. authsize, 1);
  324. return 0;
  325. }
  326. static int crypto_aegis256_decrypt(struct aead_request *req)
  327. {
  328. static const struct aegis256_ops ops = {
  329. .skcipher_walk_init = skcipher_walk_aead_decrypt,
  330. .crypt_chunk = crypto_aegis256_decrypt_chunk,
  331. };
  332. static const u8 zeros[AEGIS256_MAX_AUTH_SIZE] = {};
  333. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  334. union aegis_block tag;
  335. unsigned int authsize = crypto_aead_authsize(tfm);
  336. unsigned int cryptlen = req->cryptlen - authsize;
  337. scatterwalk_map_and_copy(tag.bytes, req->src, req->assoclen + cryptlen,
  338. authsize, 0);
  339. crypto_aegis256_crypt(req, &tag, cryptlen, &ops);
  340. return crypto_memneq(tag.bytes, zeros, authsize) ? -EBADMSG : 0;
  341. }
  342. static int crypto_aegis256_init_tfm(struct crypto_aead *tfm)
  343. {
  344. return 0;
  345. }
  346. static void crypto_aegis256_exit_tfm(struct crypto_aead *tfm)
  347. {
  348. }
  349. static struct aead_alg crypto_aegis256_alg = {
  350. .setkey = crypto_aegis256_setkey,
  351. .setauthsize = crypto_aegis256_setauthsize,
  352. .encrypt = crypto_aegis256_encrypt,
  353. .decrypt = crypto_aegis256_decrypt,
  354. .init = crypto_aegis256_init_tfm,
  355. .exit = crypto_aegis256_exit_tfm,
  356. .ivsize = AEGIS256_NONCE_SIZE,
  357. .maxauthsize = AEGIS256_MAX_AUTH_SIZE,
  358. .chunksize = AEGIS_BLOCK_SIZE,
  359. .base = {
  360. .cra_blocksize = 1,
  361. .cra_ctxsize = sizeof(struct aegis_ctx),
  362. .cra_alignmask = 0,
  363. .cra_priority = 100,
  364. .cra_name = "aegis256",
  365. .cra_driver_name = "aegis256-generic",
  366. .cra_module = THIS_MODULE,
  367. }
  368. };
  369. static int __init crypto_aegis256_module_init(void)
  370. {
  371. return crypto_register_aead(&crypto_aegis256_alg);
  372. }
  373. static void __exit crypto_aegis256_module_exit(void)
  374. {
  375. crypto_unregister_aead(&crypto_aegis256_alg);
  376. }
  377. module_init(crypto_aegis256_module_init);
  378. module_exit(crypto_aegis256_module_exit);
  379. MODULE_LICENSE("GPL");
  380. MODULE_AUTHOR("Ondrej Mosnacek <omosnacek@gmail.com>");
  381. MODULE_DESCRIPTION("AEGIS-256 AEAD algorithm");
  382. MODULE_ALIAS_CRYPTO("aegis256");
  383. MODULE_ALIAS_CRYPTO("aegis256-generic");