aegis128.c 12 KB

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