morus640.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /*
  2. * The MORUS-640 Authenticated-Encryption Algorithm
  3. *
  4. * Copyright (c) 2016-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 <asm/unaligned.h>
  13. #include <crypto/algapi.h>
  14. #include <crypto/internal/aead.h>
  15. #include <crypto/internal/skcipher.h>
  16. #include <crypto/morus_common.h>
  17. #include <crypto/scatterwalk.h>
  18. #include <linux/err.h>
  19. #include <linux/init.h>
  20. #include <linux/kernel.h>
  21. #include <linux/module.h>
  22. #include <linux/scatterlist.h>
  23. #define MORUS640_WORD_SIZE 4
  24. #define MORUS640_BLOCK_SIZE (MORUS_BLOCK_WORDS * MORUS640_WORD_SIZE)
  25. #define MORUS640_BLOCK_ALIGN (__alignof__(__le32))
  26. #define MORUS640_ALIGNED(p) IS_ALIGNED((uintptr_t)p, MORUS640_BLOCK_ALIGN)
  27. struct morus640_block {
  28. u32 words[MORUS_BLOCK_WORDS];
  29. };
  30. union morus640_block_in {
  31. __le32 words[MORUS_BLOCK_WORDS];
  32. u8 bytes[MORUS640_BLOCK_SIZE];
  33. };
  34. struct morus640_state {
  35. struct morus640_block s[MORUS_STATE_BLOCKS];
  36. };
  37. struct morus640_ctx {
  38. struct morus640_block key;
  39. };
  40. struct morus640_ops {
  41. int (*skcipher_walk_init)(struct skcipher_walk *walk,
  42. struct aead_request *req, bool atomic);
  43. void (*crypt_chunk)(struct morus640_state *state,
  44. u8 *dst, const u8 *src, unsigned int size);
  45. };
  46. static const struct morus640_block crypto_morus640_const[2] = {
  47. { .words = {
  48. U32_C(0x02010100),
  49. U32_C(0x0d080503),
  50. U32_C(0x59372215),
  51. U32_C(0x6279e990),
  52. } },
  53. { .words = {
  54. U32_C(0x55183ddb),
  55. U32_C(0xf12fc26d),
  56. U32_C(0x42311120),
  57. U32_C(0xdd28b573),
  58. } },
  59. };
  60. static void crypto_morus640_round(struct morus640_block *b0,
  61. struct morus640_block *b1,
  62. struct morus640_block *b2,
  63. struct morus640_block *b3,
  64. struct morus640_block *b4,
  65. const struct morus640_block *m,
  66. unsigned int b, unsigned int w)
  67. {
  68. unsigned int i;
  69. struct morus640_block tmp;
  70. for (i = 0; i < MORUS_BLOCK_WORDS; i++) {
  71. b0->words[i] ^= b1->words[i] & b2->words[i];
  72. b0->words[i] ^= b3->words[i];
  73. b0->words[i] ^= m->words[i];
  74. b0->words[i] = rol32(b0->words[i], b);
  75. }
  76. tmp = *b3;
  77. for (i = 0; i < MORUS_BLOCK_WORDS; i++)
  78. b3->words[(i + w) % MORUS_BLOCK_WORDS] = tmp.words[i];
  79. }
  80. static void crypto_morus640_update(struct morus640_state *state,
  81. const struct morus640_block *m)
  82. {
  83. static const struct morus640_block z = {};
  84. struct morus640_block *s = state->s;
  85. crypto_morus640_round(&s[0], &s[1], &s[2], &s[3], &s[4], &z, 5, 1);
  86. crypto_morus640_round(&s[1], &s[2], &s[3], &s[4], &s[0], m, 31, 2);
  87. crypto_morus640_round(&s[2], &s[3], &s[4], &s[0], &s[1], m, 7, 3);
  88. crypto_morus640_round(&s[3], &s[4], &s[0], &s[1], &s[2], m, 22, 2);
  89. crypto_morus640_round(&s[4], &s[0], &s[1], &s[2], &s[3], m, 13, 1);
  90. }
  91. static void crypto_morus640_load_a(struct morus640_block *dst, const u8 *src)
  92. {
  93. unsigned int i;
  94. for (i = 0; i < MORUS_BLOCK_WORDS; i++) {
  95. dst->words[i] = le32_to_cpu(*(const __le32 *)src);
  96. src += MORUS640_WORD_SIZE;
  97. }
  98. }
  99. static void crypto_morus640_load_u(struct morus640_block *dst, const u8 *src)
  100. {
  101. unsigned int i;
  102. for (i = 0; i < MORUS_BLOCK_WORDS; i++) {
  103. dst->words[i] = get_unaligned_le32(src);
  104. src += MORUS640_WORD_SIZE;
  105. }
  106. }
  107. static void crypto_morus640_load(struct morus640_block *dst, const u8 *src)
  108. {
  109. if (MORUS640_ALIGNED(src))
  110. crypto_morus640_load_a(dst, src);
  111. else
  112. crypto_morus640_load_u(dst, src);
  113. }
  114. static void crypto_morus640_store_a(u8 *dst, const struct morus640_block *src)
  115. {
  116. unsigned int i;
  117. for (i = 0; i < MORUS_BLOCK_WORDS; i++) {
  118. *(__le32 *)dst = cpu_to_le32(src->words[i]);
  119. dst += MORUS640_WORD_SIZE;
  120. }
  121. }
  122. static void crypto_morus640_store_u(u8 *dst, const struct morus640_block *src)
  123. {
  124. unsigned int i;
  125. for (i = 0; i < MORUS_BLOCK_WORDS; i++) {
  126. put_unaligned_le32(src->words[i], dst);
  127. dst += MORUS640_WORD_SIZE;
  128. }
  129. }
  130. static void crypto_morus640_store(u8 *dst, const struct morus640_block *src)
  131. {
  132. if (MORUS640_ALIGNED(dst))
  133. crypto_morus640_store_a(dst, src);
  134. else
  135. crypto_morus640_store_u(dst, src);
  136. }
  137. static void crypto_morus640_ad(struct morus640_state *state, const u8 *src,
  138. unsigned int size)
  139. {
  140. struct morus640_block m;
  141. if (MORUS640_ALIGNED(src)) {
  142. while (size >= MORUS640_BLOCK_SIZE) {
  143. crypto_morus640_load_a(&m, src);
  144. crypto_morus640_update(state, &m);
  145. size -= MORUS640_BLOCK_SIZE;
  146. src += MORUS640_BLOCK_SIZE;
  147. }
  148. } else {
  149. while (size >= MORUS640_BLOCK_SIZE) {
  150. crypto_morus640_load_u(&m, src);
  151. crypto_morus640_update(state, &m);
  152. size -= MORUS640_BLOCK_SIZE;
  153. src += MORUS640_BLOCK_SIZE;
  154. }
  155. }
  156. }
  157. static void crypto_morus640_core(const struct morus640_state *state,
  158. struct morus640_block *blk)
  159. {
  160. unsigned int i;
  161. for (i = 0; i < MORUS_BLOCK_WORDS; i++)
  162. blk->words[(i + 3) % MORUS_BLOCK_WORDS] ^= state->s[1].words[i];
  163. for (i = 0; i < MORUS_BLOCK_WORDS; i++) {
  164. blk->words[i] ^= state->s[0].words[i];
  165. blk->words[i] ^= state->s[2].words[i] & state->s[3].words[i];
  166. }
  167. }
  168. static void crypto_morus640_encrypt_chunk(struct morus640_state *state, u8 *dst,
  169. const u8 *src, unsigned int size)
  170. {
  171. struct morus640_block c, m;
  172. if (MORUS640_ALIGNED(src) && MORUS640_ALIGNED(dst)) {
  173. while (size >= MORUS640_BLOCK_SIZE) {
  174. crypto_morus640_load_a(&m, src);
  175. c = m;
  176. crypto_morus640_core(state, &c);
  177. crypto_morus640_store_a(dst, &c);
  178. crypto_morus640_update(state, &m);
  179. src += MORUS640_BLOCK_SIZE;
  180. dst += MORUS640_BLOCK_SIZE;
  181. size -= MORUS640_BLOCK_SIZE;
  182. }
  183. } else {
  184. while (size >= MORUS640_BLOCK_SIZE) {
  185. crypto_morus640_load_u(&m, src);
  186. c = m;
  187. crypto_morus640_core(state, &c);
  188. crypto_morus640_store_u(dst, &c);
  189. crypto_morus640_update(state, &m);
  190. src += MORUS640_BLOCK_SIZE;
  191. dst += MORUS640_BLOCK_SIZE;
  192. size -= MORUS640_BLOCK_SIZE;
  193. }
  194. }
  195. if (size > 0) {
  196. union morus640_block_in tail;
  197. memcpy(tail.bytes, src, size);
  198. memset(tail.bytes + size, 0, MORUS640_BLOCK_SIZE - size);
  199. crypto_morus640_load_a(&m, tail.bytes);
  200. c = m;
  201. crypto_morus640_core(state, &c);
  202. crypto_morus640_store_a(tail.bytes, &c);
  203. crypto_morus640_update(state, &m);
  204. memcpy(dst, tail.bytes, size);
  205. }
  206. }
  207. static void crypto_morus640_decrypt_chunk(struct morus640_state *state, u8 *dst,
  208. const u8 *src, unsigned int size)
  209. {
  210. struct morus640_block m;
  211. if (MORUS640_ALIGNED(src) && MORUS640_ALIGNED(dst)) {
  212. while (size >= MORUS640_BLOCK_SIZE) {
  213. crypto_morus640_load_a(&m, src);
  214. crypto_morus640_core(state, &m);
  215. crypto_morus640_store_a(dst, &m);
  216. crypto_morus640_update(state, &m);
  217. src += MORUS640_BLOCK_SIZE;
  218. dst += MORUS640_BLOCK_SIZE;
  219. size -= MORUS640_BLOCK_SIZE;
  220. }
  221. } else {
  222. while (size >= MORUS640_BLOCK_SIZE) {
  223. crypto_morus640_load_u(&m, src);
  224. crypto_morus640_core(state, &m);
  225. crypto_morus640_store_u(dst, &m);
  226. crypto_morus640_update(state, &m);
  227. src += MORUS640_BLOCK_SIZE;
  228. dst += MORUS640_BLOCK_SIZE;
  229. size -= MORUS640_BLOCK_SIZE;
  230. }
  231. }
  232. if (size > 0) {
  233. union morus640_block_in tail;
  234. memcpy(tail.bytes, src, size);
  235. memset(tail.bytes + size, 0, MORUS640_BLOCK_SIZE - size);
  236. crypto_morus640_load_a(&m, tail.bytes);
  237. crypto_morus640_core(state, &m);
  238. crypto_morus640_store_a(tail.bytes, &m);
  239. memset(tail.bytes + size, 0, MORUS640_BLOCK_SIZE - size);
  240. crypto_morus640_load_a(&m, tail.bytes);
  241. crypto_morus640_update(state, &m);
  242. memcpy(dst, tail.bytes, size);
  243. }
  244. }
  245. static void crypto_morus640_init(struct morus640_state *state,
  246. const struct morus640_block *key,
  247. const u8 *iv)
  248. {
  249. static const struct morus640_block z = {};
  250. unsigned int i;
  251. crypto_morus640_load(&state->s[0], iv);
  252. state->s[1] = *key;
  253. for (i = 0; i < MORUS_BLOCK_WORDS; i++)
  254. state->s[2].words[i] = U32_C(0xFFFFFFFF);
  255. state->s[3] = crypto_morus640_const[0];
  256. state->s[4] = crypto_morus640_const[1];
  257. for (i = 0; i < 16; i++)
  258. crypto_morus640_update(state, &z);
  259. for (i = 0; i < MORUS_BLOCK_WORDS; i++)
  260. state->s[1].words[i] ^= key->words[i];
  261. }
  262. static void crypto_morus640_process_ad(struct morus640_state *state,
  263. struct scatterlist *sg_src,
  264. unsigned int assoclen)
  265. {
  266. struct scatter_walk walk;
  267. struct morus640_block m;
  268. union morus640_block_in buf;
  269. unsigned int pos = 0;
  270. scatterwalk_start(&walk, sg_src);
  271. while (assoclen != 0) {
  272. unsigned int size = scatterwalk_clamp(&walk, assoclen);
  273. unsigned int left = size;
  274. void *mapped = scatterwalk_map(&walk);
  275. const u8 *src = (const u8 *)mapped;
  276. if (pos + size >= MORUS640_BLOCK_SIZE) {
  277. if (pos > 0) {
  278. unsigned int fill = MORUS640_BLOCK_SIZE - pos;
  279. memcpy(buf.bytes + pos, src, fill);
  280. crypto_morus640_load_a(&m, buf.bytes);
  281. crypto_morus640_update(state, &m);
  282. pos = 0;
  283. left -= fill;
  284. src += fill;
  285. }
  286. crypto_morus640_ad(state, src, left);
  287. src += left & ~(MORUS640_BLOCK_SIZE - 1);
  288. left &= MORUS640_BLOCK_SIZE - 1;
  289. }
  290. memcpy(buf.bytes + pos, src, left);
  291. pos += left;
  292. assoclen -= size;
  293. scatterwalk_unmap(mapped);
  294. scatterwalk_advance(&walk, size);
  295. scatterwalk_done(&walk, 0, assoclen);
  296. }
  297. if (pos > 0) {
  298. memset(buf.bytes + pos, 0, MORUS640_BLOCK_SIZE - pos);
  299. crypto_morus640_load_a(&m, buf.bytes);
  300. crypto_morus640_update(state, &m);
  301. }
  302. }
  303. static void crypto_morus640_process_crypt(struct morus640_state *state,
  304. struct aead_request *req,
  305. const struct morus640_ops *ops)
  306. {
  307. struct skcipher_walk walk;
  308. ops->skcipher_walk_init(&walk, req, false);
  309. while (walk.nbytes) {
  310. unsigned int nbytes = walk.nbytes;
  311. if (nbytes < walk.total)
  312. nbytes = round_down(nbytes, walk.stride);
  313. ops->crypt_chunk(state, walk.dst.virt.addr, walk.src.virt.addr,
  314. nbytes);
  315. skcipher_walk_done(&walk, walk.nbytes - nbytes);
  316. }
  317. }
  318. static void crypto_morus640_final(struct morus640_state *state,
  319. struct morus640_block *tag_xor,
  320. u64 assoclen, u64 cryptlen)
  321. {
  322. struct morus640_block tmp;
  323. unsigned int i;
  324. tmp.words[0] = lower_32_bits(assoclen * 8);
  325. tmp.words[1] = upper_32_bits(assoclen * 8);
  326. tmp.words[2] = lower_32_bits(cryptlen * 8);
  327. tmp.words[3] = upper_32_bits(cryptlen * 8);
  328. for (i = 0; i < MORUS_BLOCK_WORDS; i++)
  329. state->s[4].words[i] ^= state->s[0].words[i];
  330. for (i = 0; i < 10; i++)
  331. crypto_morus640_update(state, &tmp);
  332. crypto_morus640_core(state, tag_xor);
  333. }
  334. static int crypto_morus640_setkey(struct crypto_aead *aead, const u8 *key,
  335. unsigned int keylen)
  336. {
  337. struct morus640_ctx *ctx = crypto_aead_ctx(aead);
  338. if (keylen != MORUS640_BLOCK_SIZE) {
  339. crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  340. return -EINVAL;
  341. }
  342. crypto_morus640_load(&ctx->key, key);
  343. return 0;
  344. }
  345. static int crypto_morus640_setauthsize(struct crypto_aead *tfm,
  346. unsigned int authsize)
  347. {
  348. return (authsize <= MORUS_MAX_AUTH_SIZE) ? 0 : -EINVAL;
  349. }
  350. static void crypto_morus640_crypt(struct aead_request *req,
  351. struct morus640_block *tag_xor,
  352. unsigned int cryptlen,
  353. const struct morus640_ops *ops)
  354. {
  355. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  356. struct morus640_ctx *ctx = crypto_aead_ctx(tfm);
  357. struct morus640_state state;
  358. crypto_morus640_init(&state, &ctx->key, req->iv);
  359. crypto_morus640_process_ad(&state, req->src, req->assoclen);
  360. crypto_morus640_process_crypt(&state, req, ops);
  361. crypto_morus640_final(&state, tag_xor, req->assoclen, cryptlen);
  362. }
  363. static int crypto_morus640_encrypt(struct aead_request *req)
  364. {
  365. static const struct morus640_ops ops = {
  366. .skcipher_walk_init = skcipher_walk_aead_encrypt,
  367. .crypt_chunk = crypto_morus640_encrypt_chunk,
  368. };
  369. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  370. struct morus640_block tag = {};
  371. union morus640_block_in tag_out;
  372. unsigned int authsize = crypto_aead_authsize(tfm);
  373. unsigned int cryptlen = req->cryptlen;
  374. crypto_morus640_crypt(req, &tag, cryptlen, &ops);
  375. crypto_morus640_store(tag_out.bytes, &tag);
  376. scatterwalk_map_and_copy(tag_out.bytes, req->dst,
  377. req->assoclen + cryptlen, authsize, 1);
  378. return 0;
  379. }
  380. static int crypto_morus640_decrypt(struct aead_request *req)
  381. {
  382. static const struct morus640_ops ops = {
  383. .skcipher_walk_init = skcipher_walk_aead_decrypt,
  384. .crypt_chunk = crypto_morus640_decrypt_chunk,
  385. };
  386. static const u8 zeros[MORUS640_BLOCK_SIZE] = {};
  387. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  388. union morus640_block_in tag_in;
  389. struct morus640_block tag;
  390. unsigned int authsize = crypto_aead_authsize(tfm);
  391. unsigned int cryptlen = req->cryptlen - authsize;
  392. scatterwalk_map_and_copy(tag_in.bytes, req->src,
  393. req->assoclen + cryptlen, authsize, 0);
  394. crypto_morus640_load(&tag, tag_in.bytes);
  395. crypto_morus640_crypt(req, &tag, cryptlen, &ops);
  396. crypto_morus640_store(tag_in.bytes, &tag);
  397. return crypto_memneq(tag_in.bytes, zeros, authsize) ? -EBADMSG : 0;
  398. }
  399. static int crypto_morus640_init_tfm(struct crypto_aead *tfm)
  400. {
  401. return 0;
  402. }
  403. static void crypto_morus640_exit_tfm(struct crypto_aead *tfm)
  404. {
  405. }
  406. static struct aead_alg crypto_morus640_alg = {
  407. .setkey = crypto_morus640_setkey,
  408. .setauthsize = crypto_morus640_setauthsize,
  409. .encrypt = crypto_morus640_encrypt,
  410. .decrypt = crypto_morus640_decrypt,
  411. .init = crypto_morus640_init_tfm,
  412. .exit = crypto_morus640_exit_tfm,
  413. .ivsize = MORUS_NONCE_SIZE,
  414. .maxauthsize = MORUS_MAX_AUTH_SIZE,
  415. .chunksize = MORUS640_BLOCK_SIZE,
  416. .base = {
  417. .cra_blocksize = 1,
  418. .cra_ctxsize = sizeof(struct morus640_ctx),
  419. .cra_alignmask = 0,
  420. .cra_priority = 100,
  421. .cra_name = "morus640",
  422. .cra_driver_name = "morus640-generic",
  423. .cra_module = THIS_MODULE,
  424. }
  425. };
  426. static int __init crypto_morus640_module_init(void)
  427. {
  428. return crypto_register_aead(&crypto_morus640_alg);
  429. }
  430. static void __exit crypto_morus640_module_exit(void)
  431. {
  432. crypto_unregister_aead(&crypto_morus640_alg);
  433. }
  434. module_init(crypto_morus640_module_init);
  435. module_exit(crypto_morus640_module_exit);
  436. MODULE_LICENSE("GPL");
  437. MODULE_AUTHOR("Ondrej Mosnacek <omosnacek@gmail.com>");
  438. MODULE_DESCRIPTION("MORUS-640 AEAD algorithm");
  439. MODULE_ALIAS_CRYPTO("morus640");
  440. MODULE_ALIAS_CRYPTO("morus640-generic");