morus1280.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * The MORUS-1280 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 MORUS1280_WORD_SIZE 8
  24. #define MORUS1280_BLOCK_SIZE (MORUS_BLOCK_WORDS * MORUS1280_WORD_SIZE)
  25. #define MORUS1280_BLOCK_ALIGN (__alignof__(__le64))
  26. #define MORUS1280_ALIGNED(p) IS_ALIGNED((uintptr_t)p, MORUS1280_BLOCK_ALIGN)
  27. struct morus1280_block {
  28. u64 words[MORUS_BLOCK_WORDS];
  29. };
  30. union morus1280_block_in {
  31. __le64 words[MORUS_BLOCK_WORDS];
  32. u8 bytes[MORUS1280_BLOCK_SIZE];
  33. };
  34. struct morus1280_state {
  35. struct morus1280_block s[MORUS_STATE_BLOCKS];
  36. };
  37. struct morus1280_ctx {
  38. struct morus1280_block key;
  39. };
  40. struct morus1280_ops {
  41. int (*skcipher_walk_init)(struct skcipher_walk *walk,
  42. struct aead_request *req, bool atomic);
  43. void (*crypt_chunk)(struct morus1280_state *state,
  44. u8 *dst, const u8 *src, unsigned int size);
  45. };
  46. static const struct morus1280_block crypto_morus1280_const[1] = {
  47. { .words = {
  48. U64_C(0x0d08050302010100),
  49. U64_C(0x6279e99059372215),
  50. U64_C(0xf12fc26d55183ddb),
  51. U64_C(0xdd28b57342311120),
  52. } },
  53. };
  54. static void crypto_morus1280_round(struct morus1280_block *b0,
  55. struct morus1280_block *b1,
  56. struct morus1280_block *b2,
  57. struct morus1280_block *b3,
  58. struct morus1280_block *b4,
  59. const struct morus1280_block *m,
  60. unsigned int b, unsigned int w)
  61. {
  62. unsigned int i;
  63. struct morus1280_block tmp;
  64. for (i = 0; i < MORUS_BLOCK_WORDS; i++) {
  65. b0->words[i] ^= b1->words[i] & b2->words[i];
  66. b0->words[i] ^= b3->words[i];
  67. b0->words[i] ^= m->words[i];
  68. b0->words[i] = rol64(b0->words[i], b);
  69. }
  70. tmp = *b3;
  71. for (i = 0; i < MORUS_BLOCK_WORDS; i++)
  72. b3->words[(i + w) % MORUS_BLOCK_WORDS] = tmp.words[i];
  73. }
  74. static void crypto_morus1280_update(struct morus1280_state *state,
  75. const struct morus1280_block *m)
  76. {
  77. static const struct morus1280_block z = {};
  78. struct morus1280_block *s = state->s;
  79. crypto_morus1280_round(&s[0], &s[1], &s[2], &s[3], &s[4], &z, 13, 1);
  80. crypto_morus1280_round(&s[1], &s[2], &s[3], &s[4], &s[0], m, 46, 2);
  81. crypto_morus1280_round(&s[2], &s[3], &s[4], &s[0], &s[1], m, 38, 3);
  82. crypto_morus1280_round(&s[3], &s[4], &s[0], &s[1], &s[2], m, 7, 2);
  83. crypto_morus1280_round(&s[4], &s[0], &s[1], &s[2], &s[3], m, 4, 1);
  84. }
  85. static void crypto_morus1280_load_a(struct morus1280_block *dst, const u8 *src)
  86. {
  87. unsigned int i;
  88. for (i = 0; i < MORUS_BLOCK_WORDS; i++) {
  89. dst->words[i] = le64_to_cpu(*(const __le64 *)src);
  90. src += MORUS1280_WORD_SIZE;
  91. }
  92. }
  93. static void crypto_morus1280_load_u(struct morus1280_block *dst, const u8 *src)
  94. {
  95. unsigned int i;
  96. for (i = 0; i < MORUS_BLOCK_WORDS; i++) {
  97. dst->words[i] = get_unaligned_le64(src);
  98. src += MORUS1280_WORD_SIZE;
  99. }
  100. }
  101. static void crypto_morus1280_load(struct morus1280_block *dst, const u8 *src)
  102. {
  103. if (MORUS1280_ALIGNED(src))
  104. crypto_morus1280_load_a(dst, src);
  105. else
  106. crypto_morus1280_load_u(dst, src);
  107. }
  108. static void crypto_morus1280_store_a(u8 *dst, const struct morus1280_block *src)
  109. {
  110. unsigned int i;
  111. for (i = 0; i < MORUS_BLOCK_WORDS; i++) {
  112. *(__le64 *)dst = cpu_to_le64(src->words[i]);
  113. dst += MORUS1280_WORD_SIZE;
  114. }
  115. }
  116. static void crypto_morus1280_store_u(u8 *dst, const struct morus1280_block *src)
  117. {
  118. unsigned int i;
  119. for (i = 0; i < MORUS_BLOCK_WORDS; i++) {
  120. put_unaligned_le64(src->words[i], dst);
  121. dst += MORUS1280_WORD_SIZE;
  122. }
  123. }
  124. static void crypto_morus1280_store(u8 *dst, const struct morus1280_block *src)
  125. {
  126. if (MORUS1280_ALIGNED(dst))
  127. crypto_morus1280_store_a(dst, src);
  128. else
  129. crypto_morus1280_store_u(dst, src);
  130. }
  131. static void crypto_morus1280_ad(struct morus1280_state *state, const u8 *src,
  132. unsigned int size)
  133. {
  134. struct morus1280_block m;
  135. if (MORUS1280_ALIGNED(src)) {
  136. while (size >= MORUS1280_BLOCK_SIZE) {
  137. crypto_morus1280_load_a(&m, src);
  138. crypto_morus1280_update(state, &m);
  139. size -= MORUS1280_BLOCK_SIZE;
  140. src += MORUS1280_BLOCK_SIZE;
  141. }
  142. } else {
  143. while (size >= MORUS1280_BLOCK_SIZE) {
  144. crypto_morus1280_load_u(&m, src);
  145. crypto_morus1280_update(state, &m);
  146. size -= MORUS1280_BLOCK_SIZE;
  147. src += MORUS1280_BLOCK_SIZE;
  148. }
  149. }
  150. }
  151. static void crypto_morus1280_core(const struct morus1280_state *state,
  152. struct morus1280_block *blk)
  153. {
  154. unsigned int i;
  155. for (i = 0; i < MORUS_BLOCK_WORDS; i++)
  156. blk->words[(i + 3) % MORUS_BLOCK_WORDS] ^= state->s[1].words[i];
  157. for (i = 0; i < MORUS_BLOCK_WORDS; i++) {
  158. blk->words[i] ^= state->s[0].words[i];
  159. blk->words[i] ^= state->s[2].words[i] & state->s[3].words[i];
  160. }
  161. }
  162. static void crypto_morus1280_encrypt_chunk(struct morus1280_state *state,
  163. u8 *dst, const u8 *src,
  164. unsigned int size)
  165. {
  166. struct morus1280_block c, m;
  167. if (MORUS1280_ALIGNED(src) && MORUS1280_ALIGNED(dst)) {
  168. while (size >= MORUS1280_BLOCK_SIZE) {
  169. crypto_morus1280_load_a(&m, src);
  170. c = m;
  171. crypto_morus1280_core(state, &c);
  172. crypto_morus1280_store_a(dst, &c);
  173. crypto_morus1280_update(state, &m);
  174. src += MORUS1280_BLOCK_SIZE;
  175. dst += MORUS1280_BLOCK_SIZE;
  176. size -= MORUS1280_BLOCK_SIZE;
  177. }
  178. } else {
  179. while (size >= MORUS1280_BLOCK_SIZE) {
  180. crypto_morus1280_load_u(&m, src);
  181. c = m;
  182. crypto_morus1280_core(state, &c);
  183. crypto_morus1280_store_u(dst, &c);
  184. crypto_morus1280_update(state, &m);
  185. src += MORUS1280_BLOCK_SIZE;
  186. dst += MORUS1280_BLOCK_SIZE;
  187. size -= MORUS1280_BLOCK_SIZE;
  188. }
  189. }
  190. if (size > 0) {
  191. union morus1280_block_in tail;
  192. memcpy(tail.bytes, src, size);
  193. memset(tail.bytes + size, 0, MORUS1280_BLOCK_SIZE - size);
  194. crypto_morus1280_load_a(&m, tail.bytes);
  195. c = m;
  196. crypto_morus1280_core(state, &c);
  197. crypto_morus1280_store_a(tail.bytes, &c);
  198. crypto_morus1280_update(state, &m);
  199. memcpy(dst, tail.bytes, size);
  200. }
  201. }
  202. static void crypto_morus1280_decrypt_chunk(struct morus1280_state *state,
  203. u8 *dst, const u8 *src,
  204. unsigned int size)
  205. {
  206. struct morus1280_block m;
  207. if (MORUS1280_ALIGNED(src) && MORUS1280_ALIGNED(dst)) {
  208. while (size >= MORUS1280_BLOCK_SIZE) {
  209. crypto_morus1280_load_a(&m, src);
  210. crypto_morus1280_core(state, &m);
  211. crypto_morus1280_store_a(dst, &m);
  212. crypto_morus1280_update(state, &m);
  213. src += MORUS1280_BLOCK_SIZE;
  214. dst += MORUS1280_BLOCK_SIZE;
  215. size -= MORUS1280_BLOCK_SIZE;
  216. }
  217. } else {
  218. while (size >= MORUS1280_BLOCK_SIZE) {
  219. crypto_morus1280_load_u(&m, src);
  220. crypto_morus1280_core(state, &m);
  221. crypto_morus1280_store_u(dst, &m);
  222. crypto_morus1280_update(state, &m);
  223. src += MORUS1280_BLOCK_SIZE;
  224. dst += MORUS1280_BLOCK_SIZE;
  225. size -= MORUS1280_BLOCK_SIZE;
  226. }
  227. }
  228. if (size > 0) {
  229. union morus1280_block_in tail;
  230. memcpy(tail.bytes, src, size);
  231. memset(tail.bytes + size, 0, MORUS1280_BLOCK_SIZE - size);
  232. crypto_morus1280_load_a(&m, tail.bytes);
  233. crypto_morus1280_core(state, &m);
  234. crypto_morus1280_store_a(tail.bytes, &m);
  235. memset(tail.bytes + size, 0, MORUS1280_BLOCK_SIZE - size);
  236. crypto_morus1280_load_a(&m, tail.bytes);
  237. crypto_morus1280_update(state, &m);
  238. memcpy(dst, tail.bytes, size);
  239. }
  240. }
  241. static void crypto_morus1280_init(struct morus1280_state *state,
  242. const struct morus1280_block *key,
  243. const u8 *iv)
  244. {
  245. static const struct morus1280_block z = {};
  246. union morus1280_block_in tmp;
  247. unsigned int i;
  248. memcpy(tmp.bytes, iv, MORUS_NONCE_SIZE);
  249. memset(tmp.bytes + MORUS_NONCE_SIZE, 0,
  250. MORUS1280_BLOCK_SIZE - MORUS_NONCE_SIZE);
  251. crypto_morus1280_load(&state->s[0], tmp.bytes);
  252. state->s[1] = *key;
  253. for (i = 0; i < MORUS_BLOCK_WORDS; i++)
  254. state->s[2].words[i] = U64_C(0xFFFFFFFFFFFFFFFF);
  255. state->s[3] = z;
  256. state->s[4] = crypto_morus1280_const[0];
  257. for (i = 0; i < 16; i++)
  258. crypto_morus1280_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_morus1280_process_ad(struct morus1280_state *state,
  263. struct scatterlist *sg_src,
  264. unsigned int assoclen)
  265. {
  266. struct scatter_walk walk;
  267. struct morus1280_block m;
  268. union morus1280_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 >= MORUS1280_BLOCK_SIZE) {
  277. if (pos > 0) {
  278. unsigned int fill = MORUS1280_BLOCK_SIZE - pos;
  279. memcpy(buf.bytes + pos, src, fill);
  280. crypto_morus1280_load_a(&m, buf.bytes);
  281. crypto_morus1280_update(state, &m);
  282. pos = 0;
  283. left -= fill;
  284. src += fill;
  285. }
  286. crypto_morus1280_ad(state, src, left);
  287. src += left & ~(MORUS1280_BLOCK_SIZE - 1);
  288. left &= MORUS1280_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, MORUS1280_BLOCK_SIZE - pos);
  299. crypto_morus1280_load_a(&m, buf.bytes);
  300. crypto_morus1280_update(state, &m);
  301. }
  302. }
  303. static void crypto_morus1280_process_crypt(struct morus1280_state *state,
  304. struct aead_request *req,
  305. const struct morus1280_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_morus1280_final(struct morus1280_state *state,
  319. struct morus1280_block *tag_xor,
  320. u64 assoclen, u64 cryptlen)
  321. {
  322. struct morus1280_block tmp;
  323. unsigned int i;
  324. tmp.words[0] = assoclen * 8;
  325. tmp.words[1] = cryptlen * 8;
  326. tmp.words[2] = 0;
  327. tmp.words[3] = 0;
  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_morus1280_update(state, &tmp);
  332. crypto_morus1280_core(state, tag_xor);
  333. }
  334. static int crypto_morus1280_setkey(struct crypto_aead *aead, const u8 *key,
  335. unsigned int keylen)
  336. {
  337. struct morus1280_ctx *ctx = crypto_aead_ctx(aead);
  338. union morus1280_block_in tmp;
  339. if (keylen == MORUS1280_BLOCK_SIZE)
  340. crypto_morus1280_load(&ctx->key, key);
  341. else if (keylen == MORUS1280_BLOCK_SIZE / 2) {
  342. memcpy(tmp.bytes, key, keylen);
  343. memcpy(tmp.bytes + keylen, key, keylen);
  344. crypto_morus1280_load(&ctx->key, tmp.bytes);
  345. } else {
  346. crypto_aead_set_flags(aead, CRYPTO_TFM_RES_BAD_KEY_LEN);
  347. return -EINVAL;
  348. }
  349. return 0;
  350. }
  351. static int crypto_morus1280_setauthsize(struct crypto_aead *tfm,
  352. unsigned int authsize)
  353. {
  354. return (authsize <= MORUS_MAX_AUTH_SIZE) ? 0 : -EINVAL;
  355. }
  356. static void crypto_morus1280_crypt(struct aead_request *req,
  357. struct morus1280_block *tag_xor,
  358. unsigned int cryptlen,
  359. const struct morus1280_ops *ops)
  360. {
  361. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  362. struct morus1280_ctx *ctx = crypto_aead_ctx(tfm);
  363. struct morus1280_state state;
  364. crypto_morus1280_init(&state, &ctx->key, req->iv);
  365. crypto_morus1280_process_ad(&state, req->src, req->assoclen);
  366. crypto_morus1280_process_crypt(&state, req, ops);
  367. crypto_morus1280_final(&state, tag_xor, req->assoclen, cryptlen);
  368. }
  369. static int crypto_morus1280_encrypt(struct aead_request *req)
  370. {
  371. static const struct morus1280_ops ops = {
  372. .skcipher_walk_init = skcipher_walk_aead_encrypt,
  373. .crypt_chunk = crypto_morus1280_encrypt_chunk,
  374. };
  375. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  376. struct morus1280_block tag = {};
  377. union morus1280_block_in tag_out;
  378. unsigned int authsize = crypto_aead_authsize(tfm);
  379. unsigned int cryptlen = req->cryptlen;
  380. crypto_morus1280_crypt(req, &tag, cryptlen, &ops);
  381. crypto_morus1280_store(tag_out.bytes, &tag);
  382. scatterwalk_map_and_copy(tag_out.bytes, req->dst,
  383. req->assoclen + cryptlen, authsize, 1);
  384. return 0;
  385. }
  386. static int crypto_morus1280_decrypt(struct aead_request *req)
  387. {
  388. static const struct morus1280_ops ops = {
  389. .skcipher_walk_init = skcipher_walk_aead_decrypt,
  390. .crypt_chunk = crypto_morus1280_decrypt_chunk,
  391. };
  392. static const u8 zeros[MORUS1280_BLOCK_SIZE] = {};
  393. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  394. union morus1280_block_in tag_in;
  395. struct morus1280_block tag;
  396. unsigned int authsize = crypto_aead_authsize(tfm);
  397. unsigned int cryptlen = req->cryptlen - authsize;
  398. scatterwalk_map_and_copy(tag_in.bytes, req->src,
  399. req->assoclen + cryptlen, authsize, 0);
  400. crypto_morus1280_load(&tag, tag_in.bytes);
  401. crypto_morus1280_crypt(req, &tag, cryptlen, &ops);
  402. crypto_morus1280_store(tag_in.bytes, &tag);
  403. return crypto_memneq(tag_in.bytes, zeros, authsize) ? -EBADMSG : 0;
  404. }
  405. static int crypto_morus1280_init_tfm(struct crypto_aead *tfm)
  406. {
  407. return 0;
  408. }
  409. static void crypto_morus1280_exit_tfm(struct crypto_aead *tfm)
  410. {
  411. }
  412. static struct aead_alg crypto_morus1280_alg = {
  413. .setkey = crypto_morus1280_setkey,
  414. .setauthsize = crypto_morus1280_setauthsize,
  415. .encrypt = crypto_morus1280_encrypt,
  416. .decrypt = crypto_morus1280_decrypt,
  417. .init = crypto_morus1280_init_tfm,
  418. .exit = crypto_morus1280_exit_tfm,
  419. .ivsize = MORUS_NONCE_SIZE,
  420. .maxauthsize = MORUS_MAX_AUTH_SIZE,
  421. .chunksize = MORUS1280_BLOCK_SIZE,
  422. .base = {
  423. .cra_blocksize = 1,
  424. .cra_ctxsize = sizeof(struct morus1280_ctx),
  425. .cra_alignmask = 0,
  426. .cra_priority = 100,
  427. .cra_name = "morus1280",
  428. .cra_driver_name = "morus1280-generic",
  429. .cra_module = THIS_MODULE,
  430. }
  431. };
  432. static int __init crypto_morus1280_module_init(void)
  433. {
  434. return crypto_register_aead(&crypto_morus1280_alg);
  435. }
  436. static void __exit crypto_morus1280_module_exit(void)
  437. {
  438. crypto_unregister_aead(&crypto_morus1280_alg);
  439. }
  440. module_init(crypto_morus1280_module_init);
  441. module_exit(crypto_morus1280_module_exit);
  442. MODULE_LICENSE("GPL");
  443. MODULE_AUTHOR("Ondrej Mosnacek <omosnacek@gmail.com>");
  444. MODULE_DESCRIPTION("MORUS-1280 AEAD algorithm");
  445. MODULE_ALIAS_CRYPTO("morus1280");
  446. MODULE_ALIAS_CRYPTO("morus1280-generic");