aegis256.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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. u8 *src, *dst;
  251. unsigned int chunksize;
  252. ops->skcipher_walk_init(&walk, req, false);
  253. while (walk.nbytes) {
  254. src = walk.src.virt.addr;
  255. dst = walk.dst.virt.addr;
  256. chunksize = walk.nbytes;
  257. ops->crypt_chunk(state, dst, src, chunksize);
  258. skcipher_walk_done(&walk, 0);
  259. }
  260. }
  261. static void crypto_aegis256_final(struct aegis_state *state,
  262. union aegis_block *tag_xor,
  263. u64 assoclen, u64 cryptlen)
  264. {
  265. u64 assocbits = assoclen * 8;
  266. u64 cryptbits = cryptlen * 8;
  267. union aegis_block tmp;
  268. unsigned int i;
  269. tmp.words64[0] = cpu_to_le64(assocbits);
  270. tmp.words64[1] = cpu_to_le64(cryptbits);
  271. crypto_aegis_block_xor(&tmp, &state->blocks[3]);
  272. for (i = 0; i < 7; i++)
  273. crypto_aegis256_update_a(state, &tmp);
  274. for (i = 0; i < AEGIS256_STATE_BLOCKS; i++)
  275. crypto_aegis_block_xor(tag_xor, &state->blocks[i]);
  276. }
  277. static int crypto_aegis256_setkey(struct crypto_aead *aead, const u8 *key,
  278. unsigned int keylen)
  279. {
  280. struct aegis_ctx *ctx = crypto_aead_ctx(aead);
  281. if (keylen != AEGIS256_KEY_SIZE) {
  282. crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  283. return -EINVAL;
  284. }
  285. memcpy(ctx->key[0].bytes, key, AEGIS_BLOCK_SIZE);
  286. memcpy(ctx->key[1].bytes, key + AEGIS_BLOCK_SIZE,
  287. AEGIS_BLOCK_SIZE);
  288. return 0;
  289. }
  290. static int crypto_aegis256_setauthsize(struct crypto_aead *tfm,
  291. unsigned int authsize)
  292. {
  293. if (authsize > AEGIS256_MAX_AUTH_SIZE)
  294. return -EINVAL;
  295. if (authsize < AEGIS256_MIN_AUTH_SIZE)
  296. return -EINVAL;
  297. return 0;
  298. }
  299. static void crypto_aegis256_crypt(struct aead_request *req,
  300. union aegis_block *tag_xor,
  301. unsigned int cryptlen,
  302. const struct aegis256_ops *ops)
  303. {
  304. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  305. struct aegis_ctx *ctx = crypto_aead_ctx(tfm);
  306. struct aegis_state state;
  307. crypto_aegis256_init(&state, ctx->key, req->iv);
  308. crypto_aegis256_process_ad(&state, req->src, req->assoclen);
  309. crypto_aegis256_process_crypt(&state, req, ops);
  310. crypto_aegis256_final(&state, tag_xor, req->assoclen, cryptlen);
  311. }
  312. static int crypto_aegis256_encrypt(struct aead_request *req)
  313. {
  314. static const struct aegis256_ops ops = {
  315. .skcipher_walk_init = skcipher_walk_aead_encrypt,
  316. .crypt_chunk = crypto_aegis256_encrypt_chunk,
  317. };
  318. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  319. union aegis_block tag = {};
  320. unsigned int authsize = crypto_aead_authsize(tfm);
  321. unsigned int cryptlen = req->cryptlen;
  322. crypto_aegis256_crypt(req, &tag, cryptlen, &ops);
  323. scatterwalk_map_and_copy(tag.bytes, req->dst, req->assoclen + cryptlen,
  324. authsize, 1);
  325. return 0;
  326. }
  327. static int crypto_aegis256_decrypt(struct aead_request *req)
  328. {
  329. static const struct aegis256_ops ops = {
  330. .skcipher_walk_init = skcipher_walk_aead_decrypt,
  331. .crypt_chunk = crypto_aegis256_decrypt_chunk,
  332. };
  333. static const u8 zeros[AEGIS256_MAX_AUTH_SIZE] = {};
  334. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  335. union aegis_block tag;
  336. unsigned int authsize = crypto_aead_authsize(tfm);
  337. unsigned int cryptlen = req->cryptlen - authsize;
  338. scatterwalk_map_and_copy(tag.bytes, req->src, req->assoclen + cryptlen,
  339. authsize, 0);
  340. crypto_aegis256_crypt(req, &tag, cryptlen, &ops);
  341. return crypto_memneq(tag.bytes, zeros, authsize) ? -EBADMSG : 0;
  342. }
  343. static int crypto_aegis256_init_tfm(struct crypto_aead *tfm)
  344. {
  345. return 0;
  346. }
  347. static void crypto_aegis256_exit_tfm(struct crypto_aead *tfm)
  348. {
  349. }
  350. static struct aead_alg crypto_aegis256_alg = {
  351. .setkey = crypto_aegis256_setkey,
  352. .setauthsize = crypto_aegis256_setauthsize,
  353. .encrypt = crypto_aegis256_encrypt,
  354. .decrypt = crypto_aegis256_decrypt,
  355. .init = crypto_aegis256_init_tfm,
  356. .exit = crypto_aegis256_exit_tfm,
  357. .ivsize = AEGIS256_NONCE_SIZE,
  358. .maxauthsize = AEGIS256_MAX_AUTH_SIZE,
  359. .chunksize = AEGIS_BLOCK_SIZE,
  360. .base = {
  361. .cra_blocksize = 1,
  362. .cra_ctxsize = sizeof(struct aegis_ctx),
  363. .cra_alignmask = 0,
  364. .cra_priority = 100,
  365. .cra_name = "aegis256",
  366. .cra_driver_name = "aegis256-generic",
  367. .cra_module = THIS_MODULE,
  368. }
  369. };
  370. static int __init crypto_aegis256_module_init(void)
  371. {
  372. return crypto_register_aead(&crypto_aegis256_alg);
  373. }
  374. static void __exit crypto_aegis256_module_exit(void)
  375. {
  376. crypto_unregister_aead(&crypto_aegis256_alg);
  377. }
  378. module_init(crypto_aegis256_module_init);
  379. module_exit(crypto_aegis256_module_exit);
  380. MODULE_LICENSE("GPL");
  381. MODULE_AUTHOR("Ondrej Mosnacek <omosnacek@gmail.com>");
  382. MODULE_DESCRIPTION("AEGIS-256 AEAD algorithm");
  383. MODULE_ALIAS_CRYPTO("aegis256");
  384. MODULE_ALIAS_CRYPTO("aegis256-generic");