morus640.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  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. crypto_morus640_load_a(&m, src);
  236. crypto_morus640_core(state, &m);
  237. crypto_morus640_store_a(tail.bytes, &m);
  238. memset(tail.bytes + size, 0, MORUS640_BLOCK_SIZE - size);
  239. crypto_morus640_load_a(&m, tail.bytes);
  240. crypto_morus640_update(state, &m);
  241. memcpy(dst, tail.bytes, size);
  242. }
  243. }
  244. static void crypto_morus640_init(struct morus640_state *state,
  245. const struct morus640_block *key,
  246. const u8 *iv)
  247. {
  248. static const struct morus640_block z = {};
  249. unsigned int i;
  250. crypto_morus640_load(&state->s[0], iv);
  251. state->s[1] = *key;
  252. for (i = 0; i < MORUS_BLOCK_WORDS; i++)
  253. state->s[2].words[i] = U32_C(0xFFFFFFFF);
  254. state->s[3] = crypto_morus640_const[0];
  255. state->s[4] = crypto_morus640_const[1];
  256. for (i = 0; i < 16; i++)
  257. crypto_morus640_update(state, &z);
  258. for (i = 0; i < MORUS_BLOCK_WORDS; i++)
  259. state->s[1].words[i] ^= key->words[i];
  260. }
  261. static void crypto_morus640_process_ad(struct morus640_state *state,
  262. struct scatterlist *sg_src,
  263. unsigned int assoclen)
  264. {
  265. struct scatter_walk walk;
  266. struct morus640_block m;
  267. union morus640_block_in buf;
  268. unsigned int pos = 0;
  269. scatterwalk_start(&walk, sg_src);
  270. while (assoclen != 0) {
  271. unsigned int size = scatterwalk_clamp(&walk, assoclen);
  272. unsigned int left = size;
  273. void *mapped = scatterwalk_map(&walk);
  274. const u8 *src = (const u8 *)mapped;
  275. if (pos + size >= MORUS640_BLOCK_SIZE) {
  276. if (pos > 0) {
  277. unsigned int fill = MORUS640_BLOCK_SIZE - pos;
  278. memcpy(buf.bytes + pos, src, fill);
  279. crypto_morus640_load_a(&m, buf.bytes);
  280. crypto_morus640_update(state, &m);
  281. pos = 0;
  282. left -= fill;
  283. src += fill;
  284. }
  285. crypto_morus640_ad(state, src, left);
  286. src += left & ~(MORUS640_BLOCK_SIZE - 1);
  287. left &= MORUS640_BLOCK_SIZE - 1;
  288. }
  289. memcpy(buf.bytes + pos, src, left);
  290. pos += left;
  291. assoclen -= size;
  292. scatterwalk_unmap(mapped);
  293. scatterwalk_advance(&walk, size);
  294. scatterwalk_done(&walk, 0, assoclen);
  295. }
  296. if (pos > 0) {
  297. memset(buf.bytes + pos, 0, MORUS640_BLOCK_SIZE - pos);
  298. crypto_morus640_load_a(&m, buf.bytes);
  299. crypto_morus640_update(state, &m);
  300. }
  301. }
  302. static void crypto_morus640_process_crypt(struct morus640_state *state,
  303. struct aead_request *req,
  304. const struct morus640_ops *ops)
  305. {
  306. struct skcipher_walk walk;
  307. u8 *dst;
  308. const u8 *src;
  309. ops->skcipher_walk_init(&walk, req, false);
  310. while (walk.nbytes) {
  311. src = walk.src.virt.addr;
  312. dst = walk.dst.virt.addr;
  313. ops->crypt_chunk(state, dst, src, walk.nbytes);
  314. skcipher_walk_done(&walk, 0);
  315. }
  316. }
  317. static void crypto_morus640_final(struct morus640_state *state,
  318. struct morus640_block *tag_xor,
  319. u64 assoclen, u64 cryptlen)
  320. {
  321. u64 assocbits = assoclen * 8;
  322. u64 cryptbits = cryptlen * 8;
  323. u32 assocbits_lo = (u32)assocbits;
  324. u32 assocbits_hi = (u32)(assocbits >> 32);
  325. u32 cryptbits_lo = (u32)cryptbits;
  326. u32 cryptbits_hi = (u32)(cryptbits >> 32);
  327. struct morus640_block tmp;
  328. unsigned int i;
  329. tmp.words[0] = cpu_to_le32(assocbits_lo);
  330. tmp.words[1] = cpu_to_le32(assocbits_hi);
  331. tmp.words[2] = cpu_to_le32(cryptbits_lo);
  332. tmp.words[3] = cpu_to_le32(cryptbits_hi);
  333. for (i = 0; i < MORUS_BLOCK_WORDS; i++)
  334. state->s[4].words[i] ^= state->s[0].words[i];
  335. for (i = 0; i < 10; i++)
  336. crypto_morus640_update(state, &tmp);
  337. crypto_morus640_core(state, tag_xor);
  338. }
  339. static int crypto_morus640_setkey(struct crypto_aead *aead, const u8 *key,
  340. unsigned int keylen)
  341. {
  342. struct morus640_ctx *ctx = crypto_aead_ctx(aead);
  343. if (keylen != MORUS640_BLOCK_SIZE) {
  344. crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  345. return -EINVAL;
  346. }
  347. crypto_morus640_load(&ctx->key, key);
  348. return 0;
  349. }
  350. static int crypto_morus640_setauthsize(struct crypto_aead *tfm,
  351. unsigned int authsize)
  352. {
  353. return (authsize <= MORUS_MAX_AUTH_SIZE) ? 0 : -EINVAL;
  354. }
  355. static void crypto_morus640_crypt(struct aead_request *req,
  356. struct morus640_block *tag_xor,
  357. unsigned int cryptlen,
  358. const struct morus640_ops *ops)
  359. {
  360. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  361. struct morus640_ctx *ctx = crypto_aead_ctx(tfm);
  362. struct morus640_state state;
  363. crypto_morus640_init(&state, &ctx->key, req->iv);
  364. crypto_morus640_process_ad(&state, req->src, req->assoclen);
  365. crypto_morus640_process_crypt(&state, req, ops);
  366. crypto_morus640_final(&state, tag_xor, req->assoclen, cryptlen);
  367. }
  368. static int crypto_morus640_encrypt(struct aead_request *req)
  369. {
  370. static const struct morus640_ops ops = {
  371. .skcipher_walk_init = skcipher_walk_aead_encrypt,
  372. .crypt_chunk = crypto_morus640_encrypt_chunk,
  373. };
  374. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  375. struct morus640_block tag = {};
  376. union morus640_block_in tag_out;
  377. unsigned int authsize = crypto_aead_authsize(tfm);
  378. unsigned int cryptlen = req->cryptlen;
  379. crypto_morus640_crypt(req, &tag, cryptlen, &ops);
  380. crypto_morus640_store(tag_out.bytes, &tag);
  381. scatterwalk_map_and_copy(tag_out.bytes, req->dst,
  382. req->assoclen + cryptlen, authsize, 1);
  383. return 0;
  384. }
  385. static int crypto_morus640_decrypt(struct aead_request *req)
  386. {
  387. static const struct morus640_ops ops = {
  388. .skcipher_walk_init = skcipher_walk_aead_decrypt,
  389. .crypt_chunk = crypto_morus640_decrypt_chunk,
  390. };
  391. static const u8 zeros[MORUS640_BLOCK_SIZE] = {};
  392. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  393. union morus640_block_in tag_in;
  394. struct morus640_block tag;
  395. unsigned int authsize = crypto_aead_authsize(tfm);
  396. unsigned int cryptlen = req->cryptlen - authsize;
  397. scatterwalk_map_and_copy(tag_in.bytes, req->src,
  398. req->assoclen + cryptlen, authsize, 0);
  399. crypto_morus640_load(&tag, tag_in.bytes);
  400. crypto_morus640_crypt(req, &tag, cryptlen, &ops);
  401. crypto_morus640_store(tag_in.bytes, &tag);
  402. return crypto_memneq(tag_in.bytes, zeros, authsize) ? -EBADMSG : 0;
  403. }
  404. static int crypto_morus640_init_tfm(struct crypto_aead *tfm)
  405. {
  406. return 0;
  407. }
  408. static void crypto_morus640_exit_tfm(struct crypto_aead *tfm)
  409. {
  410. }
  411. static struct aead_alg crypto_morus640_alg = {
  412. .setkey = crypto_morus640_setkey,
  413. .setauthsize = crypto_morus640_setauthsize,
  414. .encrypt = crypto_morus640_encrypt,
  415. .decrypt = crypto_morus640_decrypt,
  416. .init = crypto_morus640_init_tfm,
  417. .exit = crypto_morus640_exit_tfm,
  418. .ivsize = MORUS_NONCE_SIZE,
  419. .maxauthsize = MORUS_MAX_AUTH_SIZE,
  420. .chunksize = MORUS640_BLOCK_SIZE,
  421. .base = {
  422. .cra_blocksize = 1,
  423. .cra_ctxsize = sizeof(struct morus640_ctx),
  424. .cra_alignmask = 0,
  425. .cra_priority = 100,
  426. .cra_name = "morus640",
  427. .cra_driver_name = "morus640-generic",
  428. .cra_module = THIS_MODULE,
  429. }
  430. };
  431. static int __init crypto_morus640_module_init(void)
  432. {
  433. return crypto_register_aead(&crypto_morus640_alg);
  434. }
  435. static void __exit crypto_morus640_module_exit(void)
  436. {
  437. crypto_unregister_aead(&crypto_morus640_alg);
  438. }
  439. module_init(crypto_morus640_module_init);
  440. module_exit(crypto_morus640_module_exit);
  441. MODULE_LICENSE("GPL");
  442. MODULE_AUTHOR("Ondrej Mosnacek <omosnacek@gmail.com>");
  443. MODULE_DESCRIPTION("MORUS-640 AEAD algorithm");
  444. MODULE_ALIAS_CRYPTO("morus640");
  445. MODULE_ALIAS_CRYPTO("morus640-generic");