adiantum.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Adiantum length-preserving encryption mode
  4. *
  5. * Copyright 2018 Google LLC
  6. */
  7. /*
  8. * Adiantum is a tweakable, length-preserving encryption mode designed for fast
  9. * and secure disk encryption, especially on CPUs without dedicated crypto
  10. * instructions. Adiantum encrypts each sector using the XChaCha12 stream
  11. * cipher, two passes of an ε-almost-∆-universal (ε-∆U) hash function based on
  12. * NH and Poly1305, and an invocation of the AES-256 block cipher on a single
  13. * 16-byte block. See the paper for details:
  14. *
  15. * Adiantum: length-preserving encryption for entry-level processors
  16. * (https://eprint.iacr.org/2018/720.pdf)
  17. *
  18. * For flexibility, this implementation also allows other ciphers:
  19. *
  20. * - Stream cipher: XChaCha12 or XChaCha20
  21. * - Block cipher: any with a 128-bit block size and 256-bit key
  22. *
  23. * This implementation doesn't currently allow other ε-∆U hash functions, i.e.
  24. * HPolyC is not supported. This is because Adiantum is ~20% faster than HPolyC
  25. * but still provably as secure, and also the ε-∆U hash function of HBSH is
  26. * formally defined to take two inputs (tweak, message) which makes it difficult
  27. * to wrap with the crypto_shash API. Rather, some details need to be handled
  28. * here. Nevertheless, if needed in the future, support for other ε-∆U hash
  29. * functions could be added here.
  30. */
  31. #include <crypto/b128ops.h>
  32. #include <crypto/chacha.h>
  33. #include <crypto/internal/hash.h>
  34. #include <crypto/internal/skcipher.h>
  35. #include <crypto/nhpoly1305.h>
  36. #include <crypto/scatterwalk.h>
  37. #include <linux/module.h>
  38. #include "internal.h"
  39. /*
  40. * Size of right-hand part of input data, in bytes; also the size of the block
  41. * cipher's block size and the hash function's output.
  42. */
  43. #define BLOCKCIPHER_BLOCK_SIZE 16
  44. /* Size of the block cipher key (K_E) in bytes */
  45. #define BLOCKCIPHER_KEY_SIZE 32
  46. /* Size of the hash key (K_H) in bytes */
  47. #define HASH_KEY_SIZE (POLY1305_BLOCK_SIZE + NHPOLY1305_KEY_SIZE)
  48. /*
  49. * The specification allows variable-length tweaks, but Linux's crypto API
  50. * currently only allows algorithms to support a single length. The "natural"
  51. * tweak length for Adiantum is 16, since that fits into one Poly1305 block for
  52. * the best performance. But longer tweaks are useful for fscrypt, to avoid
  53. * needing to derive per-file keys. So instead we use two blocks, or 32 bytes.
  54. */
  55. #define TWEAK_SIZE 32
  56. struct adiantum_instance_ctx {
  57. struct crypto_skcipher_spawn streamcipher_spawn;
  58. struct crypto_spawn blockcipher_spawn;
  59. struct crypto_shash_spawn hash_spawn;
  60. };
  61. struct adiantum_tfm_ctx {
  62. struct crypto_skcipher *streamcipher;
  63. struct crypto_cipher *blockcipher;
  64. struct crypto_shash *hash;
  65. struct poly1305_key header_hash_key;
  66. };
  67. struct adiantum_request_ctx {
  68. /*
  69. * Buffer for right-hand part of data, i.e.
  70. *
  71. * P_L => P_M => C_M => C_R when encrypting, or
  72. * C_R => C_M => P_M => P_L when decrypting.
  73. *
  74. * Also used to build the IV for the stream cipher.
  75. */
  76. union {
  77. u8 bytes[XCHACHA_IV_SIZE];
  78. __le32 words[XCHACHA_IV_SIZE / sizeof(__le32)];
  79. le128 bignum; /* interpret as element of Z/(2^{128}Z) */
  80. } rbuf;
  81. bool enc; /* true if encrypting, false if decrypting */
  82. /*
  83. * The result of the Poly1305 ε-∆U hash function applied to
  84. * (bulk length, tweak)
  85. */
  86. le128 header_hash;
  87. /* Sub-requests, must be last */
  88. union {
  89. struct shash_desc hash_desc;
  90. struct skcipher_request streamcipher_req;
  91. } u;
  92. };
  93. /*
  94. * Given the XChaCha stream key K_S, derive the block cipher key K_E and the
  95. * hash key K_H as follows:
  96. *
  97. * K_E || K_H || ... = XChaCha(key=K_S, nonce=1||0^191)
  98. *
  99. * Note that this denotes using bits from the XChaCha keystream, which here we
  100. * get indirectly by encrypting a buffer containing all 0's.
  101. */
  102. static int adiantum_setkey(struct crypto_skcipher *tfm, const u8 *key,
  103. unsigned int keylen)
  104. {
  105. struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
  106. struct {
  107. u8 iv[XCHACHA_IV_SIZE];
  108. u8 derived_keys[BLOCKCIPHER_KEY_SIZE + HASH_KEY_SIZE];
  109. struct scatterlist sg;
  110. struct crypto_wait wait;
  111. struct skcipher_request req; /* must be last */
  112. } *data;
  113. u8 *keyp;
  114. int err;
  115. /* Set the stream cipher key (K_S) */
  116. crypto_skcipher_clear_flags(tctx->streamcipher, CRYPTO_TFM_REQ_MASK);
  117. crypto_skcipher_set_flags(tctx->streamcipher,
  118. crypto_skcipher_get_flags(tfm) &
  119. CRYPTO_TFM_REQ_MASK);
  120. err = crypto_skcipher_setkey(tctx->streamcipher, key, keylen);
  121. crypto_skcipher_set_flags(tfm,
  122. crypto_skcipher_get_flags(tctx->streamcipher) &
  123. CRYPTO_TFM_RES_MASK);
  124. if (err)
  125. return err;
  126. /* Derive the subkeys */
  127. data = kzalloc(sizeof(*data) +
  128. crypto_skcipher_reqsize(tctx->streamcipher), GFP_KERNEL);
  129. if (!data)
  130. return -ENOMEM;
  131. data->iv[0] = 1;
  132. sg_init_one(&data->sg, data->derived_keys, sizeof(data->derived_keys));
  133. crypto_init_wait(&data->wait);
  134. skcipher_request_set_tfm(&data->req, tctx->streamcipher);
  135. skcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
  136. CRYPTO_TFM_REQ_MAY_BACKLOG,
  137. crypto_req_done, &data->wait);
  138. skcipher_request_set_crypt(&data->req, &data->sg, &data->sg,
  139. sizeof(data->derived_keys), data->iv);
  140. err = crypto_wait_req(crypto_skcipher_encrypt(&data->req), &data->wait);
  141. if (err)
  142. goto out;
  143. keyp = data->derived_keys;
  144. /* Set the block cipher key (K_E) */
  145. crypto_cipher_clear_flags(tctx->blockcipher, CRYPTO_TFM_REQ_MASK);
  146. crypto_cipher_set_flags(tctx->blockcipher,
  147. crypto_skcipher_get_flags(tfm) &
  148. CRYPTO_TFM_REQ_MASK);
  149. err = crypto_cipher_setkey(tctx->blockcipher, keyp,
  150. BLOCKCIPHER_KEY_SIZE);
  151. crypto_skcipher_set_flags(tfm,
  152. crypto_cipher_get_flags(tctx->blockcipher) &
  153. CRYPTO_TFM_RES_MASK);
  154. if (err)
  155. goto out;
  156. keyp += BLOCKCIPHER_KEY_SIZE;
  157. /* Set the hash key (K_H) */
  158. poly1305_core_setkey(&tctx->header_hash_key, keyp);
  159. keyp += POLY1305_BLOCK_SIZE;
  160. crypto_shash_clear_flags(tctx->hash, CRYPTO_TFM_REQ_MASK);
  161. crypto_shash_set_flags(tctx->hash, crypto_skcipher_get_flags(tfm) &
  162. CRYPTO_TFM_REQ_MASK);
  163. err = crypto_shash_setkey(tctx->hash, keyp, NHPOLY1305_KEY_SIZE);
  164. crypto_skcipher_set_flags(tfm, crypto_shash_get_flags(tctx->hash) &
  165. CRYPTO_TFM_RES_MASK);
  166. keyp += NHPOLY1305_KEY_SIZE;
  167. WARN_ON(keyp != &data->derived_keys[ARRAY_SIZE(data->derived_keys)]);
  168. out:
  169. kzfree(data);
  170. return err;
  171. }
  172. /* Addition in Z/(2^{128}Z) */
  173. static inline void le128_add(le128 *r, const le128 *v1, const le128 *v2)
  174. {
  175. u64 x = le64_to_cpu(v1->b);
  176. u64 y = le64_to_cpu(v2->b);
  177. r->b = cpu_to_le64(x + y);
  178. r->a = cpu_to_le64(le64_to_cpu(v1->a) + le64_to_cpu(v2->a) +
  179. (x + y < x));
  180. }
  181. /* Subtraction in Z/(2^{128}Z) */
  182. static inline void le128_sub(le128 *r, const le128 *v1, const le128 *v2)
  183. {
  184. u64 x = le64_to_cpu(v1->b);
  185. u64 y = le64_to_cpu(v2->b);
  186. r->b = cpu_to_le64(x - y);
  187. r->a = cpu_to_le64(le64_to_cpu(v1->a) - le64_to_cpu(v2->a) -
  188. (x - y > x));
  189. }
  190. /*
  191. * Apply the Poly1305 ε-∆U hash function to (bulk length, tweak) and save the
  192. * result to rctx->header_hash. This is the calculation
  193. *
  194. * H_T ← Poly1305_{K_T}(bin_{128}(|L|) || T)
  195. *
  196. * from the procedure in section 6.4 of the Adiantum paper. The resulting value
  197. * is reused in both the first and second hash steps. Specifically, it's added
  198. * to the result of an independently keyed ε-∆U hash function (for equal length
  199. * inputs only) taken over the left-hand part (the "bulk") of the message, to
  200. * give the overall Adiantum hash of the (tweak, left-hand part) pair.
  201. */
  202. static void adiantum_hash_header(struct skcipher_request *req)
  203. {
  204. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  205. const struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
  206. struct adiantum_request_ctx *rctx = skcipher_request_ctx(req);
  207. const unsigned int bulk_len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
  208. struct {
  209. __le64 message_bits;
  210. __le64 padding;
  211. } header = {
  212. .message_bits = cpu_to_le64((u64)bulk_len * 8)
  213. };
  214. struct poly1305_state state;
  215. poly1305_core_init(&state);
  216. BUILD_BUG_ON(sizeof(header) % POLY1305_BLOCK_SIZE != 0);
  217. poly1305_core_blocks(&state, &tctx->header_hash_key,
  218. &header, sizeof(header) / POLY1305_BLOCK_SIZE);
  219. BUILD_BUG_ON(TWEAK_SIZE % POLY1305_BLOCK_SIZE != 0);
  220. poly1305_core_blocks(&state, &tctx->header_hash_key, req->iv,
  221. TWEAK_SIZE / POLY1305_BLOCK_SIZE);
  222. poly1305_core_emit(&state, &rctx->header_hash);
  223. }
  224. /* Hash the left-hand part (the "bulk") of the message using NHPoly1305 */
  225. static int adiantum_hash_message(struct skcipher_request *req,
  226. struct scatterlist *sgl, le128 *digest)
  227. {
  228. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  229. const struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
  230. struct adiantum_request_ctx *rctx = skcipher_request_ctx(req);
  231. const unsigned int bulk_len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
  232. struct shash_desc *hash_desc = &rctx->u.hash_desc;
  233. struct sg_mapping_iter miter;
  234. unsigned int i, n;
  235. int err;
  236. hash_desc->tfm = tctx->hash;
  237. hash_desc->flags = 0;
  238. err = crypto_shash_init(hash_desc);
  239. if (err)
  240. return err;
  241. sg_miter_start(&miter, sgl, sg_nents(sgl),
  242. SG_MITER_FROM_SG | SG_MITER_ATOMIC);
  243. for (i = 0; i < bulk_len; i += n) {
  244. sg_miter_next(&miter);
  245. n = min_t(unsigned int, miter.length, bulk_len - i);
  246. err = crypto_shash_update(hash_desc, miter.addr, n);
  247. if (err)
  248. break;
  249. }
  250. sg_miter_stop(&miter);
  251. if (err)
  252. return err;
  253. return crypto_shash_final(hash_desc, (u8 *)digest);
  254. }
  255. /* Continue Adiantum encryption/decryption after the stream cipher step */
  256. static int adiantum_finish(struct skcipher_request *req)
  257. {
  258. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  259. const struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
  260. struct adiantum_request_ctx *rctx = skcipher_request_ctx(req);
  261. const unsigned int bulk_len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
  262. le128 digest;
  263. int err;
  264. /* If decrypting, decrypt C_M with the block cipher to get P_M */
  265. if (!rctx->enc)
  266. crypto_cipher_decrypt_one(tctx->blockcipher, rctx->rbuf.bytes,
  267. rctx->rbuf.bytes);
  268. /*
  269. * Second hash step
  270. * enc: C_R = C_M - H_{K_H}(T, C_L)
  271. * dec: P_R = P_M - H_{K_H}(T, P_L)
  272. */
  273. err = adiantum_hash_message(req, req->dst, &digest);
  274. if (err)
  275. return err;
  276. le128_add(&digest, &digest, &rctx->header_hash);
  277. le128_sub(&rctx->rbuf.bignum, &rctx->rbuf.bignum, &digest);
  278. scatterwalk_map_and_copy(&rctx->rbuf.bignum, req->dst,
  279. bulk_len, BLOCKCIPHER_BLOCK_SIZE, 1);
  280. return 0;
  281. }
  282. static void adiantum_streamcipher_done(struct crypto_async_request *areq,
  283. int err)
  284. {
  285. struct skcipher_request *req = areq->data;
  286. if (!err)
  287. err = adiantum_finish(req);
  288. skcipher_request_complete(req, err);
  289. }
  290. static int adiantum_crypt(struct skcipher_request *req, bool enc)
  291. {
  292. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  293. const struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
  294. struct adiantum_request_ctx *rctx = skcipher_request_ctx(req);
  295. const unsigned int bulk_len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
  296. unsigned int stream_len;
  297. le128 digest;
  298. int err;
  299. if (req->cryptlen < BLOCKCIPHER_BLOCK_SIZE)
  300. return -EINVAL;
  301. rctx->enc = enc;
  302. /*
  303. * First hash step
  304. * enc: P_M = P_R + H_{K_H}(T, P_L)
  305. * dec: C_M = C_R + H_{K_H}(T, C_L)
  306. */
  307. adiantum_hash_header(req);
  308. err = adiantum_hash_message(req, req->src, &digest);
  309. if (err)
  310. return err;
  311. le128_add(&digest, &digest, &rctx->header_hash);
  312. scatterwalk_map_and_copy(&rctx->rbuf.bignum, req->src,
  313. bulk_len, BLOCKCIPHER_BLOCK_SIZE, 0);
  314. le128_add(&rctx->rbuf.bignum, &rctx->rbuf.bignum, &digest);
  315. /* If encrypting, encrypt P_M with the block cipher to get C_M */
  316. if (enc)
  317. crypto_cipher_encrypt_one(tctx->blockcipher, rctx->rbuf.bytes,
  318. rctx->rbuf.bytes);
  319. /* Initialize the rest of the XChaCha IV (first part is C_M) */
  320. BUILD_BUG_ON(BLOCKCIPHER_BLOCK_SIZE != 16);
  321. BUILD_BUG_ON(XCHACHA_IV_SIZE != 32); /* nonce || stream position */
  322. rctx->rbuf.words[4] = cpu_to_le32(1);
  323. rctx->rbuf.words[5] = 0;
  324. rctx->rbuf.words[6] = 0;
  325. rctx->rbuf.words[7] = 0;
  326. /*
  327. * XChaCha needs to be done on all the data except the last 16 bytes;
  328. * for disk encryption that usually means 4080 or 496 bytes. But ChaCha
  329. * implementations tend to be most efficient when passed a whole number
  330. * of 64-byte ChaCha blocks, or sometimes even a multiple of 256 bytes.
  331. * And here it doesn't matter whether the last 16 bytes are written to,
  332. * as the second hash step will overwrite them. Thus, round the XChaCha
  333. * length up to the next 64-byte boundary if possible.
  334. */
  335. stream_len = bulk_len;
  336. if (round_up(stream_len, CHACHA_BLOCK_SIZE) <= req->cryptlen)
  337. stream_len = round_up(stream_len, CHACHA_BLOCK_SIZE);
  338. skcipher_request_set_tfm(&rctx->u.streamcipher_req, tctx->streamcipher);
  339. skcipher_request_set_crypt(&rctx->u.streamcipher_req, req->src,
  340. req->dst, stream_len, &rctx->rbuf);
  341. skcipher_request_set_callback(&rctx->u.streamcipher_req,
  342. req->base.flags,
  343. adiantum_streamcipher_done, req);
  344. return crypto_skcipher_encrypt(&rctx->u.streamcipher_req) ?:
  345. adiantum_finish(req);
  346. }
  347. static int adiantum_encrypt(struct skcipher_request *req)
  348. {
  349. return adiantum_crypt(req, true);
  350. }
  351. static int adiantum_decrypt(struct skcipher_request *req)
  352. {
  353. return adiantum_crypt(req, false);
  354. }
  355. static int adiantum_init_tfm(struct crypto_skcipher *tfm)
  356. {
  357. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  358. struct adiantum_instance_ctx *ictx = skcipher_instance_ctx(inst);
  359. struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
  360. struct crypto_skcipher *streamcipher;
  361. struct crypto_cipher *blockcipher;
  362. struct crypto_shash *hash;
  363. unsigned int subreq_size;
  364. int err;
  365. streamcipher = crypto_spawn_skcipher(&ictx->streamcipher_spawn);
  366. if (IS_ERR(streamcipher))
  367. return PTR_ERR(streamcipher);
  368. blockcipher = crypto_spawn_cipher(&ictx->blockcipher_spawn);
  369. if (IS_ERR(blockcipher)) {
  370. err = PTR_ERR(blockcipher);
  371. goto err_free_streamcipher;
  372. }
  373. hash = crypto_spawn_shash(&ictx->hash_spawn);
  374. if (IS_ERR(hash)) {
  375. err = PTR_ERR(hash);
  376. goto err_free_blockcipher;
  377. }
  378. tctx->streamcipher = streamcipher;
  379. tctx->blockcipher = blockcipher;
  380. tctx->hash = hash;
  381. BUILD_BUG_ON(offsetofend(struct adiantum_request_ctx, u) !=
  382. sizeof(struct adiantum_request_ctx));
  383. subreq_size = max(FIELD_SIZEOF(struct adiantum_request_ctx,
  384. u.hash_desc) +
  385. crypto_shash_descsize(hash),
  386. FIELD_SIZEOF(struct adiantum_request_ctx,
  387. u.streamcipher_req) +
  388. crypto_skcipher_reqsize(streamcipher));
  389. crypto_skcipher_set_reqsize(tfm,
  390. offsetof(struct adiantum_request_ctx, u) +
  391. subreq_size);
  392. return 0;
  393. err_free_blockcipher:
  394. crypto_free_cipher(blockcipher);
  395. err_free_streamcipher:
  396. crypto_free_skcipher(streamcipher);
  397. return err;
  398. }
  399. static void adiantum_exit_tfm(struct crypto_skcipher *tfm)
  400. {
  401. struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
  402. crypto_free_skcipher(tctx->streamcipher);
  403. crypto_free_cipher(tctx->blockcipher);
  404. crypto_free_shash(tctx->hash);
  405. }
  406. static void adiantum_free_instance(struct skcipher_instance *inst)
  407. {
  408. struct adiantum_instance_ctx *ictx = skcipher_instance_ctx(inst);
  409. crypto_drop_skcipher(&ictx->streamcipher_spawn);
  410. crypto_drop_spawn(&ictx->blockcipher_spawn);
  411. crypto_drop_shash(&ictx->hash_spawn);
  412. kfree(inst);
  413. }
  414. /*
  415. * Check for a supported set of inner algorithms.
  416. * See the comment at the beginning of this file.
  417. */
  418. static bool adiantum_supported_algorithms(struct skcipher_alg *streamcipher_alg,
  419. struct crypto_alg *blockcipher_alg,
  420. struct shash_alg *hash_alg)
  421. {
  422. if (strcmp(streamcipher_alg->base.cra_name, "xchacha12") != 0 &&
  423. strcmp(streamcipher_alg->base.cra_name, "xchacha20") != 0)
  424. return false;
  425. if (blockcipher_alg->cra_cipher.cia_min_keysize > BLOCKCIPHER_KEY_SIZE ||
  426. blockcipher_alg->cra_cipher.cia_max_keysize < BLOCKCIPHER_KEY_SIZE)
  427. return false;
  428. if (blockcipher_alg->cra_blocksize != BLOCKCIPHER_BLOCK_SIZE)
  429. return false;
  430. if (strcmp(hash_alg->base.cra_name, "nhpoly1305") != 0)
  431. return false;
  432. return true;
  433. }
  434. static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb)
  435. {
  436. struct crypto_attr_type *algt;
  437. const char *streamcipher_name;
  438. const char *blockcipher_name;
  439. const char *nhpoly1305_name;
  440. struct skcipher_instance *inst;
  441. struct adiantum_instance_ctx *ictx;
  442. struct skcipher_alg *streamcipher_alg;
  443. struct crypto_alg *blockcipher_alg;
  444. struct crypto_alg *_hash_alg;
  445. struct shash_alg *hash_alg;
  446. int err;
  447. algt = crypto_get_attr_type(tb);
  448. if (IS_ERR(algt))
  449. return PTR_ERR(algt);
  450. if ((algt->type ^ CRYPTO_ALG_TYPE_SKCIPHER) & algt->mask)
  451. return -EINVAL;
  452. streamcipher_name = crypto_attr_alg_name(tb[1]);
  453. if (IS_ERR(streamcipher_name))
  454. return PTR_ERR(streamcipher_name);
  455. blockcipher_name = crypto_attr_alg_name(tb[2]);
  456. if (IS_ERR(blockcipher_name))
  457. return PTR_ERR(blockcipher_name);
  458. nhpoly1305_name = crypto_attr_alg_name(tb[3]);
  459. if (nhpoly1305_name == ERR_PTR(-ENOENT))
  460. nhpoly1305_name = "nhpoly1305";
  461. if (IS_ERR(nhpoly1305_name))
  462. return PTR_ERR(nhpoly1305_name);
  463. inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
  464. if (!inst)
  465. return -ENOMEM;
  466. ictx = skcipher_instance_ctx(inst);
  467. /* Stream cipher, e.g. "xchacha12" */
  468. crypto_set_skcipher_spawn(&ictx->streamcipher_spawn,
  469. skcipher_crypto_instance(inst));
  470. err = crypto_grab_skcipher(&ictx->streamcipher_spawn, streamcipher_name,
  471. 0, crypto_requires_sync(algt->type,
  472. algt->mask));
  473. if (err)
  474. goto out_free_inst;
  475. streamcipher_alg = crypto_spawn_skcipher_alg(&ictx->streamcipher_spawn);
  476. /* Block cipher, e.g. "aes" */
  477. crypto_set_spawn(&ictx->blockcipher_spawn,
  478. skcipher_crypto_instance(inst));
  479. err = crypto_grab_spawn(&ictx->blockcipher_spawn, blockcipher_name,
  480. CRYPTO_ALG_TYPE_CIPHER, CRYPTO_ALG_TYPE_MASK);
  481. if (err)
  482. goto out_drop_streamcipher;
  483. blockcipher_alg = ictx->blockcipher_spawn.alg;
  484. /* NHPoly1305 ε-∆U hash function */
  485. _hash_alg = crypto_alg_mod_lookup(nhpoly1305_name,
  486. CRYPTO_ALG_TYPE_SHASH,
  487. CRYPTO_ALG_TYPE_MASK);
  488. if (IS_ERR(_hash_alg)) {
  489. err = PTR_ERR(_hash_alg);
  490. goto out_drop_blockcipher;
  491. }
  492. hash_alg = __crypto_shash_alg(_hash_alg);
  493. err = crypto_init_shash_spawn(&ictx->hash_spawn, hash_alg,
  494. skcipher_crypto_instance(inst));
  495. if (err)
  496. goto out_put_hash;
  497. /* Check the set of algorithms */
  498. if (!adiantum_supported_algorithms(streamcipher_alg, blockcipher_alg,
  499. hash_alg)) {
  500. pr_warn("Unsupported Adiantum instantiation: (%s,%s,%s)\n",
  501. streamcipher_alg->base.cra_name,
  502. blockcipher_alg->cra_name, hash_alg->base.cra_name);
  503. err = -EINVAL;
  504. goto out_drop_hash;
  505. }
  506. /* Instance fields */
  507. err = -ENAMETOOLONG;
  508. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
  509. "adiantum(%s,%s)", streamcipher_alg->base.cra_name,
  510. blockcipher_alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
  511. goto out_drop_hash;
  512. if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  513. "adiantum(%s,%s,%s)",
  514. streamcipher_alg->base.cra_driver_name,
  515. blockcipher_alg->cra_driver_name,
  516. hash_alg->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  517. goto out_drop_hash;
  518. inst->alg.base.cra_flags = streamcipher_alg->base.cra_flags &
  519. CRYPTO_ALG_ASYNC;
  520. inst->alg.base.cra_blocksize = BLOCKCIPHER_BLOCK_SIZE;
  521. inst->alg.base.cra_ctxsize = sizeof(struct adiantum_tfm_ctx);
  522. inst->alg.base.cra_alignmask = streamcipher_alg->base.cra_alignmask |
  523. hash_alg->base.cra_alignmask;
  524. /*
  525. * The block cipher is only invoked once per message, so for long
  526. * messages (e.g. sectors for disk encryption) its performance doesn't
  527. * matter as much as that of the stream cipher and hash function. Thus,
  528. * weigh the block cipher's ->cra_priority less.
  529. */
  530. inst->alg.base.cra_priority = (4 * streamcipher_alg->base.cra_priority +
  531. 2 * hash_alg->base.cra_priority +
  532. blockcipher_alg->cra_priority) / 7;
  533. inst->alg.setkey = adiantum_setkey;
  534. inst->alg.encrypt = adiantum_encrypt;
  535. inst->alg.decrypt = adiantum_decrypt;
  536. inst->alg.init = adiantum_init_tfm;
  537. inst->alg.exit = adiantum_exit_tfm;
  538. inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(streamcipher_alg);
  539. inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(streamcipher_alg);
  540. inst->alg.ivsize = TWEAK_SIZE;
  541. inst->free = adiantum_free_instance;
  542. err = skcipher_register_instance(tmpl, inst);
  543. if (err)
  544. goto out_drop_hash;
  545. crypto_mod_put(_hash_alg);
  546. return 0;
  547. out_drop_hash:
  548. crypto_drop_shash(&ictx->hash_spawn);
  549. out_put_hash:
  550. crypto_mod_put(_hash_alg);
  551. out_drop_blockcipher:
  552. crypto_drop_spawn(&ictx->blockcipher_spawn);
  553. out_drop_streamcipher:
  554. crypto_drop_skcipher(&ictx->streamcipher_spawn);
  555. out_free_inst:
  556. kfree(inst);
  557. return err;
  558. }
  559. /* adiantum(streamcipher_name, blockcipher_name [, nhpoly1305_name]) */
  560. static struct crypto_template adiantum_tmpl = {
  561. .name = "adiantum",
  562. .create = adiantum_create,
  563. .module = THIS_MODULE,
  564. };
  565. static int __init adiantum_module_init(void)
  566. {
  567. return crypto_register_template(&adiantum_tmpl);
  568. }
  569. static void __exit adiantum_module_exit(void)
  570. {
  571. crypto_unregister_template(&adiantum_tmpl);
  572. }
  573. module_init(adiantum_module_init);
  574. module_exit(adiantum_module_exit);
  575. MODULE_DESCRIPTION("Adiantum length-preserving encryption mode");
  576. MODULE_LICENSE("GPL v2");
  577. MODULE_AUTHOR("Eric Biggers <[email protected]>");
  578. MODULE_ALIAS_CRYPTO("adiantum");