xref: /openbmc/linux/crypto/adiantum.c (revision 453431a5)
1059c2a4dSEric Biggers // SPDX-License-Identifier: GPL-2.0
2059c2a4dSEric Biggers /*
3059c2a4dSEric Biggers  * Adiantum length-preserving encryption mode
4059c2a4dSEric Biggers  *
5059c2a4dSEric Biggers  * Copyright 2018 Google LLC
6059c2a4dSEric Biggers  */
7059c2a4dSEric Biggers 
8059c2a4dSEric Biggers /*
9059c2a4dSEric Biggers  * Adiantum is a tweakable, length-preserving encryption mode designed for fast
10059c2a4dSEric Biggers  * and secure disk encryption, especially on CPUs without dedicated crypto
11059c2a4dSEric Biggers  * instructions.  Adiantum encrypts each sector using the XChaCha12 stream
12c6018e1aSEric Biggers  * cipher, two passes of an ε-almost-∆-universal (ε-∆U) hash function based on
13059c2a4dSEric Biggers  * NH and Poly1305, and an invocation of the AES-256 block cipher on a single
14059c2a4dSEric Biggers  * 16-byte block.  See the paper for details:
15059c2a4dSEric Biggers  *
16059c2a4dSEric Biggers  *	Adiantum: length-preserving encryption for entry-level processors
17059c2a4dSEric Biggers  *      (https://eprint.iacr.org/2018/720.pdf)
18059c2a4dSEric Biggers  *
19059c2a4dSEric Biggers  * For flexibility, this implementation also allows other ciphers:
20059c2a4dSEric Biggers  *
21059c2a4dSEric Biggers  *	- Stream cipher: XChaCha12 or XChaCha20
22059c2a4dSEric Biggers  *	- Block cipher: any with a 128-bit block size and 256-bit key
23059c2a4dSEric Biggers  *
24c6018e1aSEric Biggers  * This implementation doesn't currently allow other ε-∆U hash functions, i.e.
25059c2a4dSEric Biggers  * HPolyC is not supported.  This is because Adiantum is ~20% faster than HPolyC
26c6018e1aSEric Biggers  * but still provably as secure, and also the ε-∆U hash function of HBSH is
27059c2a4dSEric Biggers  * formally defined to take two inputs (tweak, message) which makes it difficult
28059c2a4dSEric Biggers  * to wrap with the crypto_shash API.  Rather, some details need to be handled
29c6018e1aSEric Biggers  * here.  Nevertheless, if needed in the future, support for other ε-∆U hash
30059c2a4dSEric Biggers  * functions could be added here.
31059c2a4dSEric Biggers  */
32059c2a4dSEric Biggers 
33059c2a4dSEric Biggers #include <crypto/b128ops.h>
34059c2a4dSEric Biggers #include <crypto/chacha.h>
35059c2a4dSEric Biggers #include <crypto/internal/hash.h>
3648ea8c6eSArd Biesheuvel #include <crypto/internal/poly1305.h>
37059c2a4dSEric Biggers #include <crypto/internal/skcipher.h>
38059c2a4dSEric Biggers #include <crypto/nhpoly1305.h>
39059c2a4dSEric Biggers #include <crypto/scatterwalk.h>
40059c2a4dSEric Biggers #include <linux/module.h>
41059c2a4dSEric Biggers 
42059c2a4dSEric Biggers /*
43c6018e1aSEric Biggers  * Size of right-hand part of input data, in bytes; also the size of the block
44059c2a4dSEric Biggers  * cipher's block size and the hash function's output.
45059c2a4dSEric Biggers  */
46059c2a4dSEric Biggers #define BLOCKCIPHER_BLOCK_SIZE		16
47059c2a4dSEric Biggers 
48059c2a4dSEric Biggers /* Size of the block cipher key (K_E) in bytes */
49059c2a4dSEric Biggers #define BLOCKCIPHER_KEY_SIZE		32
50059c2a4dSEric Biggers 
51059c2a4dSEric Biggers /* Size of the hash key (K_H) in bytes */
52059c2a4dSEric Biggers #define HASH_KEY_SIZE		(POLY1305_BLOCK_SIZE + NHPOLY1305_KEY_SIZE)
53059c2a4dSEric Biggers 
54059c2a4dSEric Biggers /*
55059c2a4dSEric Biggers  * The specification allows variable-length tweaks, but Linux's crypto API
56059c2a4dSEric Biggers  * currently only allows algorithms to support a single length.  The "natural"
57059c2a4dSEric Biggers  * tweak length for Adiantum is 16, since that fits into one Poly1305 block for
58059c2a4dSEric Biggers  * the best performance.  But longer tweaks are useful for fscrypt, to avoid
59059c2a4dSEric Biggers  * needing to derive per-file keys.  So instead we use two blocks, or 32 bytes.
60059c2a4dSEric Biggers  */
61059c2a4dSEric Biggers #define TWEAK_SIZE		32
62059c2a4dSEric Biggers 
63059c2a4dSEric Biggers struct adiantum_instance_ctx {
64059c2a4dSEric Biggers 	struct crypto_skcipher_spawn streamcipher_spawn;
65ba448407SEric Biggers 	struct crypto_cipher_spawn blockcipher_spawn;
66059c2a4dSEric Biggers 	struct crypto_shash_spawn hash_spawn;
67059c2a4dSEric Biggers };
68059c2a4dSEric Biggers 
69059c2a4dSEric Biggers struct adiantum_tfm_ctx {
70059c2a4dSEric Biggers 	struct crypto_skcipher *streamcipher;
71059c2a4dSEric Biggers 	struct crypto_cipher *blockcipher;
72059c2a4dSEric Biggers 	struct crypto_shash *hash;
731c08a104SJason A. Donenfeld 	struct poly1305_core_key header_hash_key;
74059c2a4dSEric Biggers };
75059c2a4dSEric Biggers 
76059c2a4dSEric Biggers struct adiantum_request_ctx {
77059c2a4dSEric Biggers 
78059c2a4dSEric Biggers 	/*
79c6018e1aSEric Biggers 	 * Buffer for right-hand part of data, i.e.
80059c2a4dSEric Biggers 	 *
81059c2a4dSEric Biggers 	 *    P_L => P_M => C_M => C_R when encrypting, or
82059c2a4dSEric Biggers 	 *    C_R => C_M => P_M => P_L when decrypting.
83059c2a4dSEric Biggers 	 *
84059c2a4dSEric Biggers 	 * Also used to build the IV for the stream cipher.
85059c2a4dSEric Biggers 	 */
86059c2a4dSEric Biggers 	union {
87059c2a4dSEric Biggers 		u8 bytes[XCHACHA_IV_SIZE];
88059c2a4dSEric Biggers 		__le32 words[XCHACHA_IV_SIZE / sizeof(__le32)];
89059c2a4dSEric Biggers 		le128 bignum;	/* interpret as element of Z/(2^{128}Z) */
90059c2a4dSEric Biggers 	} rbuf;
91059c2a4dSEric Biggers 
92059c2a4dSEric Biggers 	bool enc; /* true if encrypting, false if decrypting */
93059c2a4dSEric Biggers 
94059c2a4dSEric Biggers 	/*
95c6018e1aSEric Biggers 	 * The result of the Poly1305 ε-∆U hash function applied to
96c6018e1aSEric Biggers 	 * (bulk length, tweak)
97059c2a4dSEric Biggers 	 */
98059c2a4dSEric Biggers 	le128 header_hash;
99059c2a4dSEric Biggers 
100059c2a4dSEric Biggers 	/* Sub-requests, must be last */
101059c2a4dSEric Biggers 	union {
102059c2a4dSEric Biggers 		struct shash_desc hash_desc;
103059c2a4dSEric Biggers 		struct skcipher_request streamcipher_req;
104059c2a4dSEric Biggers 	} u;
105059c2a4dSEric Biggers };
106059c2a4dSEric Biggers 
107059c2a4dSEric Biggers /*
108059c2a4dSEric Biggers  * Given the XChaCha stream key K_S, derive the block cipher key K_E and the
109059c2a4dSEric Biggers  * hash key K_H as follows:
110059c2a4dSEric Biggers  *
111059c2a4dSEric Biggers  *     K_E || K_H || ... = XChaCha(key=K_S, nonce=1||0^191)
112059c2a4dSEric Biggers  *
113059c2a4dSEric Biggers  * Note that this denotes using bits from the XChaCha keystream, which here we
114059c2a4dSEric Biggers  * get indirectly by encrypting a buffer containing all 0's.
115059c2a4dSEric Biggers  */
116059c2a4dSEric Biggers static int adiantum_setkey(struct crypto_skcipher *tfm, const u8 *key,
117059c2a4dSEric Biggers 			   unsigned int keylen)
118059c2a4dSEric Biggers {
119059c2a4dSEric Biggers 	struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
120059c2a4dSEric Biggers 	struct {
121059c2a4dSEric Biggers 		u8 iv[XCHACHA_IV_SIZE];
122059c2a4dSEric Biggers 		u8 derived_keys[BLOCKCIPHER_KEY_SIZE + HASH_KEY_SIZE];
123059c2a4dSEric Biggers 		struct scatterlist sg;
124059c2a4dSEric Biggers 		struct crypto_wait wait;
125059c2a4dSEric Biggers 		struct skcipher_request req; /* must be last */
126059c2a4dSEric Biggers 	} *data;
127059c2a4dSEric Biggers 	u8 *keyp;
128059c2a4dSEric Biggers 	int err;
129059c2a4dSEric Biggers 
130059c2a4dSEric Biggers 	/* Set the stream cipher key (K_S) */
131059c2a4dSEric Biggers 	crypto_skcipher_clear_flags(tctx->streamcipher, CRYPTO_TFM_REQ_MASK);
132059c2a4dSEric Biggers 	crypto_skcipher_set_flags(tctx->streamcipher,
133059c2a4dSEric Biggers 				  crypto_skcipher_get_flags(tfm) &
134059c2a4dSEric Biggers 				  CRYPTO_TFM_REQ_MASK);
135059c2a4dSEric Biggers 	err = crypto_skcipher_setkey(tctx->streamcipher, key, keylen);
136059c2a4dSEric Biggers 	if (err)
137059c2a4dSEric Biggers 		return err;
138059c2a4dSEric Biggers 
139059c2a4dSEric Biggers 	/* Derive the subkeys */
140059c2a4dSEric Biggers 	data = kzalloc(sizeof(*data) +
141059c2a4dSEric Biggers 		       crypto_skcipher_reqsize(tctx->streamcipher), GFP_KERNEL);
142059c2a4dSEric Biggers 	if (!data)
143059c2a4dSEric Biggers 		return -ENOMEM;
144059c2a4dSEric Biggers 	data->iv[0] = 1;
145059c2a4dSEric Biggers 	sg_init_one(&data->sg, data->derived_keys, sizeof(data->derived_keys));
146059c2a4dSEric Biggers 	crypto_init_wait(&data->wait);
147059c2a4dSEric Biggers 	skcipher_request_set_tfm(&data->req, tctx->streamcipher);
148059c2a4dSEric Biggers 	skcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
149059c2a4dSEric Biggers 						  CRYPTO_TFM_REQ_MAY_BACKLOG,
150059c2a4dSEric Biggers 				      crypto_req_done, &data->wait);
151059c2a4dSEric Biggers 	skcipher_request_set_crypt(&data->req, &data->sg, &data->sg,
152059c2a4dSEric Biggers 				   sizeof(data->derived_keys), data->iv);
153059c2a4dSEric Biggers 	err = crypto_wait_req(crypto_skcipher_encrypt(&data->req), &data->wait);
154059c2a4dSEric Biggers 	if (err)
155059c2a4dSEric Biggers 		goto out;
156059c2a4dSEric Biggers 	keyp = data->derived_keys;
157059c2a4dSEric Biggers 
158059c2a4dSEric Biggers 	/* Set the block cipher key (K_E) */
159059c2a4dSEric Biggers 	crypto_cipher_clear_flags(tctx->blockcipher, CRYPTO_TFM_REQ_MASK);
160059c2a4dSEric Biggers 	crypto_cipher_set_flags(tctx->blockcipher,
161059c2a4dSEric Biggers 				crypto_skcipher_get_flags(tfm) &
162059c2a4dSEric Biggers 				CRYPTO_TFM_REQ_MASK);
163059c2a4dSEric Biggers 	err = crypto_cipher_setkey(tctx->blockcipher, keyp,
164059c2a4dSEric Biggers 				   BLOCKCIPHER_KEY_SIZE);
165059c2a4dSEric Biggers 	if (err)
166059c2a4dSEric Biggers 		goto out;
167059c2a4dSEric Biggers 	keyp += BLOCKCIPHER_KEY_SIZE;
168059c2a4dSEric Biggers 
169059c2a4dSEric Biggers 	/* Set the hash key (K_H) */
170059c2a4dSEric Biggers 	poly1305_core_setkey(&tctx->header_hash_key, keyp);
171059c2a4dSEric Biggers 	keyp += POLY1305_BLOCK_SIZE;
172059c2a4dSEric Biggers 
173059c2a4dSEric Biggers 	crypto_shash_clear_flags(tctx->hash, CRYPTO_TFM_REQ_MASK);
174059c2a4dSEric Biggers 	crypto_shash_set_flags(tctx->hash, crypto_skcipher_get_flags(tfm) &
175059c2a4dSEric Biggers 					   CRYPTO_TFM_REQ_MASK);
176059c2a4dSEric Biggers 	err = crypto_shash_setkey(tctx->hash, keyp, NHPOLY1305_KEY_SIZE);
177059c2a4dSEric Biggers 	keyp += NHPOLY1305_KEY_SIZE;
178059c2a4dSEric Biggers 	WARN_ON(keyp != &data->derived_keys[ARRAY_SIZE(data->derived_keys)]);
179059c2a4dSEric Biggers out:
180453431a5SWaiman Long 	kfree_sensitive(data);
181059c2a4dSEric Biggers 	return err;
182059c2a4dSEric Biggers }
183059c2a4dSEric Biggers 
184059c2a4dSEric Biggers /* Addition in Z/(2^{128}Z) */
185059c2a4dSEric Biggers static inline void le128_add(le128 *r, const le128 *v1, const le128 *v2)
186059c2a4dSEric Biggers {
187059c2a4dSEric Biggers 	u64 x = le64_to_cpu(v1->b);
188059c2a4dSEric Biggers 	u64 y = le64_to_cpu(v2->b);
189059c2a4dSEric Biggers 
190059c2a4dSEric Biggers 	r->b = cpu_to_le64(x + y);
191059c2a4dSEric Biggers 	r->a = cpu_to_le64(le64_to_cpu(v1->a) + le64_to_cpu(v2->a) +
192059c2a4dSEric Biggers 			   (x + y < x));
193059c2a4dSEric Biggers }
194059c2a4dSEric Biggers 
195059c2a4dSEric Biggers /* Subtraction in Z/(2^{128}Z) */
196059c2a4dSEric Biggers static inline void le128_sub(le128 *r, const le128 *v1, const le128 *v2)
197059c2a4dSEric Biggers {
198059c2a4dSEric Biggers 	u64 x = le64_to_cpu(v1->b);
199059c2a4dSEric Biggers 	u64 y = le64_to_cpu(v2->b);
200059c2a4dSEric Biggers 
201059c2a4dSEric Biggers 	r->b = cpu_to_le64(x - y);
202059c2a4dSEric Biggers 	r->a = cpu_to_le64(le64_to_cpu(v1->a) - le64_to_cpu(v2->a) -
203059c2a4dSEric Biggers 			   (x - y > x));
204059c2a4dSEric Biggers }
205059c2a4dSEric Biggers 
206059c2a4dSEric Biggers /*
207c6018e1aSEric Biggers  * Apply the Poly1305 ε-∆U hash function to (bulk length, tweak) and save the
208c6018e1aSEric Biggers  * result to rctx->header_hash.  This is the calculation
209059c2a4dSEric Biggers  *
210c6018e1aSEric Biggers  *	H_T ← Poly1305_{K_T}(bin_{128}(|L|) || T)
211c6018e1aSEric Biggers  *
212c6018e1aSEric Biggers  * from the procedure in section 6.4 of the Adiantum paper.  The resulting value
213c6018e1aSEric Biggers  * is reused in both the first and second hash steps.  Specifically, it's added
214c6018e1aSEric Biggers  * to the result of an independently keyed ε-∆U hash function (for equal length
215c6018e1aSEric Biggers  * inputs only) taken over the left-hand part (the "bulk") of the message, to
216c6018e1aSEric Biggers  * give the overall Adiantum hash of the (tweak, left-hand part) pair.
217059c2a4dSEric Biggers  */
218059c2a4dSEric Biggers static void adiantum_hash_header(struct skcipher_request *req)
219059c2a4dSEric Biggers {
220059c2a4dSEric Biggers 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
221059c2a4dSEric Biggers 	const struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
222059c2a4dSEric Biggers 	struct adiantum_request_ctx *rctx = skcipher_request_ctx(req);
223059c2a4dSEric Biggers 	const unsigned int bulk_len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
224059c2a4dSEric Biggers 	struct {
225059c2a4dSEric Biggers 		__le64 message_bits;
226059c2a4dSEric Biggers 		__le64 padding;
227059c2a4dSEric Biggers 	} header = {
228059c2a4dSEric Biggers 		.message_bits = cpu_to_le64((u64)bulk_len * 8)
229059c2a4dSEric Biggers 	};
230059c2a4dSEric Biggers 	struct poly1305_state state;
231059c2a4dSEric Biggers 
232059c2a4dSEric Biggers 	poly1305_core_init(&state);
233059c2a4dSEric Biggers 
234059c2a4dSEric Biggers 	BUILD_BUG_ON(sizeof(header) % POLY1305_BLOCK_SIZE != 0);
235059c2a4dSEric Biggers 	poly1305_core_blocks(&state, &tctx->header_hash_key,
23648ea8c6eSArd Biesheuvel 			     &header, sizeof(header) / POLY1305_BLOCK_SIZE, 1);
237059c2a4dSEric Biggers 
238059c2a4dSEric Biggers 	BUILD_BUG_ON(TWEAK_SIZE % POLY1305_BLOCK_SIZE != 0);
239059c2a4dSEric Biggers 	poly1305_core_blocks(&state, &tctx->header_hash_key, req->iv,
24048ea8c6eSArd Biesheuvel 			     TWEAK_SIZE / POLY1305_BLOCK_SIZE, 1);
241059c2a4dSEric Biggers 
2421c08a104SJason A. Donenfeld 	poly1305_core_emit(&state, NULL, &rctx->header_hash);
243059c2a4dSEric Biggers }
244059c2a4dSEric Biggers 
245c6018e1aSEric Biggers /* Hash the left-hand part (the "bulk") of the message using NHPoly1305 */
246059c2a4dSEric Biggers static int adiantum_hash_message(struct skcipher_request *req,
247059c2a4dSEric Biggers 				 struct scatterlist *sgl, le128 *digest)
248059c2a4dSEric Biggers {
249059c2a4dSEric Biggers 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
250059c2a4dSEric Biggers 	const struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
251059c2a4dSEric Biggers 	struct adiantum_request_ctx *rctx = skcipher_request_ctx(req);
252059c2a4dSEric Biggers 	const unsigned int bulk_len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
253059c2a4dSEric Biggers 	struct shash_desc *hash_desc = &rctx->u.hash_desc;
254059c2a4dSEric Biggers 	struct sg_mapping_iter miter;
255059c2a4dSEric Biggers 	unsigned int i, n;
256059c2a4dSEric Biggers 	int err;
257059c2a4dSEric Biggers 
258059c2a4dSEric Biggers 	hash_desc->tfm = tctx->hash;
259059c2a4dSEric Biggers 
260059c2a4dSEric Biggers 	err = crypto_shash_init(hash_desc);
261059c2a4dSEric Biggers 	if (err)
262059c2a4dSEric Biggers 		return err;
263059c2a4dSEric Biggers 
264059c2a4dSEric Biggers 	sg_miter_start(&miter, sgl, sg_nents(sgl),
265059c2a4dSEric Biggers 		       SG_MITER_FROM_SG | SG_MITER_ATOMIC);
266059c2a4dSEric Biggers 	for (i = 0; i < bulk_len; i += n) {
267059c2a4dSEric Biggers 		sg_miter_next(&miter);
268059c2a4dSEric Biggers 		n = min_t(unsigned int, miter.length, bulk_len - i);
269059c2a4dSEric Biggers 		err = crypto_shash_update(hash_desc, miter.addr, n);
270059c2a4dSEric Biggers 		if (err)
271059c2a4dSEric Biggers 			break;
272059c2a4dSEric Biggers 	}
273059c2a4dSEric Biggers 	sg_miter_stop(&miter);
274059c2a4dSEric Biggers 	if (err)
275059c2a4dSEric Biggers 		return err;
276059c2a4dSEric Biggers 
277059c2a4dSEric Biggers 	return crypto_shash_final(hash_desc, (u8 *)digest);
278059c2a4dSEric Biggers }
279059c2a4dSEric Biggers 
280059c2a4dSEric Biggers /* Continue Adiantum encryption/decryption after the stream cipher step */
281059c2a4dSEric Biggers static int adiantum_finish(struct skcipher_request *req)
282059c2a4dSEric Biggers {
283059c2a4dSEric Biggers 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
284059c2a4dSEric Biggers 	const struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
285059c2a4dSEric Biggers 	struct adiantum_request_ctx *rctx = skcipher_request_ctx(req);
286059c2a4dSEric Biggers 	const unsigned int bulk_len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
287059c2a4dSEric Biggers 	le128 digest;
288059c2a4dSEric Biggers 	int err;
289059c2a4dSEric Biggers 
290059c2a4dSEric Biggers 	/* If decrypting, decrypt C_M with the block cipher to get P_M */
291059c2a4dSEric Biggers 	if (!rctx->enc)
292059c2a4dSEric Biggers 		crypto_cipher_decrypt_one(tctx->blockcipher, rctx->rbuf.bytes,
293059c2a4dSEric Biggers 					  rctx->rbuf.bytes);
294059c2a4dSEric Biggers 
295059c2a4dSEric Biggers 	/*
296059c2a4dSEric Biggers 	 * Second hash step
297059c2a4dSEric Biggers 	 *	enc: C_R = C_M - H_{K_H}(T, C_L)
298059c2a4dSEric Biggers 	 *	dec: P_R = P_M - H_{K_H}(T, P_L)
299059c2a4dSEric Biggers 	 */
300059c2a4dSEric Biggers 	err = adiantum_hash_message(req, req->dst, &digest);
301059c2a4dSEric Biggers 	if (err)
302059c2a4dSEric Biggers 		return err;
303059c2a4dSEric Biggers 	le128_add(&digest, &digest, &rctx->header_hash);
304059c2a4dSEric Biggers 	le128_sub(&rctx->rbuf.bignum, &rctx->rbuf.bignum, &digest);
305059c2a4dSEric Biggers 	scatterwalk_map_and_copy(&rctx->rbuf.bignum, req->dst,
306059c2a4dSEric Biggers 				 bulk_len, BLOCKCIPHER_BLOCK_SIZE, 1);
307059c2a4dSEric Biggers 	return 0;
308059c2a4dSEric Biggers }
309059c2a4dSEric Biggers 
310059c2a4dSEric Biggers static void adiantum_streamcipher_done(struct crypto_async_request *areq,
311059c2a4dSEric Biggers 				       int err)
312059c2a4dSEric Biggers {
313059c2a4dSEric Biggers 	struct skcipher_request *req = areq->data;
314059c2a4dSEric Biggers 
315059c2a4dSEric Biggers 	if (!err)
316059c2a4dSEric Biggers 		err = adiantum_finish(req);
317059c2a4dSEric Biggers 
318059c2a4dSEric Biggers 	skcipher_request_complete(req, err);
319059c2a4dSEric Biggers }
320059c2a4dSEric Biggers 
321059c2a4dSEric Biggers static int adiantum_crypt(struct skcipher_request *req, bool enc)
322059c2a4dSEric Biggers {
323059c2a4dSEric Biggers 	struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
324059c2a4dSEric Biggers 	const struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
325059c2a4dSEric Biggers 	struct adiantum_request_ctx *rctx = skcipher_request_ctx(req);
326059c2a4dSEric Biggers 	const unsigned int bulk_len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
327059c2a4dSEric Biggers 	unsigned int stream_len;
328059c2a4dSEric Biggers 	le128 digest;
329059c2a4dSEric Biggers 	int err;
330059c2a4dSEric Biggers 
331059c2a4dSEric Biggers 	if (req->cryptlen < BLOCKCIPHER_BLOCK_SIZE)
332059c2a4dSEric Biggers 		return -EINVAL;
333059c2a4dSEric Biggers 
334059c2a4dSEric Biggers 	rctx->enc = enc;
335059c2a4dSEric Biggers 
336059c2a4dSEric Biggers 	/*
337059c2a4dSEric Biggers 	 * First hash step
338059c2a4dSEric Biggers 	 *	enc: P_M = P_R + H_{K_H}(T, P_L)
339059c2a4dSEric Biggers 	 *	dec: C_M = C_R + H_{K_H}(T, C_L)
340059c2a4dSEric Biggers 	 */
341059c2a4dSEric Biggers 	adiantum_hash_header(req);
342059c2a4dSEric Biggers 	err = adiantum_hash_message(req, req->src, &digest);
343059c2a4dSEric Biggers 	if (err)
344059c2a4dSEric Biggers 		return err;
345059c2a4dSEric Biggers 	le128_add(&digest, &digest, &rctx->header_hash);
346059c2a4dSEric Biggers 	scatterwalk_map_and_copy(&rctx->rbuf.bignum, req->src,
347059c2a4dSEric Biggers 				 bulk_len, BLOCKCIPHER_BLOCK_SIZE, 0);
348059c2a4dSEric Biggers 	le128_add(&rctx->rbuf.bignum, &rctx->rbuf.bignum, &digest);
349059c2a4dSEric Biggers 
350059c2a4dSEric Biggers 	/* If encrypting, encrypt P_M with the block cipher to get C_M */
351059c2a4dSEric Biggers 	if (enc)
352059c2a4dSEric Biggers 		crypto_cipher_encrypt_one(tctx->blockcipher, rctx->rbuf.bytes,
353059c2a4dSEric Biggers 					  rctx->rbuf.bytes);
354059c2a4dSEric Biggers 
355059c2a4dSEric Biggers 	/* Initialize the rest of the XChaCha IV (first part is C_M) */
356059c2a4dSEric Biggers 	BUILD_BUG_ON(BLOCKCIPHER_BLOCK_SIZE != 16);
357059c2a4dSEric Biggers 	BUILD_BUG_ON(XCHACHA_IV_SIZE != 32);	/* nonce || stream position */
358059c2a4dSEric Biggers 	rctx->rbuf.words[4] = cpu_to_le32(1);
359059c2a4dSEric Biggers 	rctx->rbuf.words[5] = 0;
360059c2a4dSEric Biggers 	rctx->rbuf.words[6] = 0;
361059c2a4dSEric Biggers 	rctx->rbuf.words[7] = 0;
362059c2a4dSEric Biggers 
363059c2a4dSEric Biggers 	/*
364059c2a4dSEric Biggers 	 * XChaCha needs to be done on all the data except the last 16 bytes;
365059c2a4dSEric Biggers 	 * for disk encryption that usually means 4080 or 496 bytes.  But ChaCha
366059c2a4dSEric Biggers 	 * implementations tend to be most efficient when passed a whole number
367059c2a4dSEric Biggers 	 * of 64-byte ChaCha blocks, or sometimes even a multiple of 256 bytes.
368059c2a4dSEric Biggers 	 * And here it doesn't matter whether the last 16 bytes are written to,
369059c2a4dSEric Biggers 	 * as the second hash step will overwrite them.  Thus, round the XChaCha
370059c2a4dSEric Biggers 	 * length up to the next 64-byte boundary if possible.
371059c2a4dSEric Biggers 	 */
372059c2a4dSEric Biggers 	stream_len = bulk_len;
373059c2a4dSEric Biggers 	if (round_up(stream_len, CHACHA_BLOCK_SIZE) <= req->cryptlen)
374059c2a4dSEric Biggers 		stream_len = round_up(stream_len, CHACHA_BLOCK_SIZE);
375059c2a4dSEric Biggers 
376059c2a4dSEric Biggers 	skcipher_request_set_tfm(&rctx->u.streamcipher_req, tctx->streamcipher);
377059c2a4dSEric Biggers 	skcipher_request_set_crypt(&rctx->u.streamcipher_req, req->src,
378059c2a4dSEric Biggers 				   req->dst, stream_len, &rctx->rbuf);
379059c2a4dSEric Biggers 	skcipher_request_set_callback(&rctx->u.streamcipher_req,
380059c2a4dSEric Biggers 				      req->base.flags,
381059c2a4dSEric Biggers 				      adiantum_streamcipher_done, req);
382059c2a4dSEric Biggers 	return crypto_skcipher_encrypt(&rctx->u.streamcipher_req) ?:
383059c2a4dSEric Biggers 		adiantum_finish(req);
384059c2a4dSEric Biggers }
385059c2a4dSEric Biggers 
386059c2a4dSEric Biggers static int adiantum_encrypt(struct skcipher_request *req)
387059c2a4dSEric Biggers {
388059c2a4dSEric Biggers 	return adiantum_crypt(req, true);
389059c2a4dSEric Biggers }
390059c2a4dSEric Biggers 
391059c2a4dSEric Biggers static int adiantum_decrypt(struct skcipher_request *req)
392059c2a4dSEric Biggers {
393059c2a4dSEric Biggers 	return adiantum_crypt(req, false);
394059c2a4dSEric Biggers }
395059c2a4dSEric Biggers 
396059c2a4dSEric Biggers static int adiantum_init_tfm(struct crypto_skcipher *tfm)
397059c2a4dSEric Biggers {
398059c2a4dSEric Biggers 	struct skcipher_instance *inst = skcipher_alg_instance(tfm);
399059c2a4dSEric Biggers 	struct adiantum_instance_ctx *ictx = skcipher_instance_ctx(inst);
400059c2a4dSEric Biggers 	struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
401059c2a4dSEric Biggers 	struct crypto_skcipher *streamcipher;
402059c2a4dSEric Biggers 	struct crypto_cipher *blockcipher;
403059c2a4dSEric Biggers 	struct crypto_shash *hash;
404059c2a4dSEric Biggers 	unsigned int subreq_size;
405059c2a4dSEric Biggers 	int err;
406059c2a4dSEric Biggers 
407059c2a4dSEric Biggers 	streamcipher = crypto_spawn_skcipher(&ictx->streamcipher_spawn);
408059c2a4dSEric Biggers 	if (IS_ERR(streamcipher))
409059c2a4dSEric Biggers 		return PTR_ERR(streamcipher);
410059c2a4dSEric Biggers 
411059c2a4dSEric Biggers 	blockcipher = crypto_spawn_cipher(&ictx->blockcipher_spawn);
412059c2a4dSEric Biggers 	if (IS_ERR(blockcipher)) {
413059c2a4dSEric Biggers 		err = PTR_ERR(blockcipher);
414059c2a4dSEric Biggers 		goto err_free_streamcipher;
415059c2a4dSEric Biggers 	}
416059c2a4dSEric Biggers 
417059c2a4dSEric Biggers 	hash = crypto_spawn_shash(&ictx->hash_spawn);
418059c2a4dSEric Biggers 	if (IS_ERR(hash)) {
419059c2a4dSEric Biggers 		err = PTR_ERR(hash);
420059c2a4dSEric Biggers 		goto err_free_blockcipher;
421059c2a4dSEric Biggers 	}
422059c2a4dSEric Biggers 
423059c2a4dSEric Biggers 	tctx->streamcipher = streamcipher;
424059c2a4dSEric Biggers 	tctx->blockcipher = blockcipher;
425059c2a4dSEric Biggers 	tctx->hash = hash;
426059c2a4dSEric Biggers 
427059c2a4dSEric Biggers 	BUILD_BUG_ON(offsetofend(struct adiantum_request_ctx, u) !=
428059c2a4dSEric Biggers 		     sizeof(struct adiantum_request_ctx));
429c593642cSPankaj Bharadiya 	subreq_size = max(sizeof_field(struct adiantum_request_ctx,
430059c2a4dSEric Biggers 				       u.hash_desc) +
431059c2a4dSEric Biggers 			  crypto_shash_descsize(hash),
432c593642cSPankaj Bharadiya 			  sizeof_field(struct adiantum_request_ctx,
433059c2a4dSEric Biggers 				       u.streamcipher_req) +
434059c2a4dSEric Biggers 			  crypto_skcipher_reqsize(streamcipher));
435059c2a4dSEric Biggers 
436059c2a4dSEric Biggers 	crypto_skcipher_set_reqsize(tfm,
437059c2a4dSEric Biggers 				    offsetof(struct adiantum_request_ctx, u) +
438059c2a4dSEric Biggers 				    subreq_size);
439059c2a4dSEric Biggers 	return 0;
440059c2a4dSEric Biggers 
441059c2a4dSEric Biggers err_free_blockcipher:
442059c2a4dSEric Biggers 	crypto_free_cipher(blockcipher);
443059c2a4dSEric Biggers err_free_streamcipher:
444059c2a4dSEric Biggers 	crypto_free_skcipher(streamcipher);
445059c2a4dSEric Biggers 	return err;
446059c2a4dSEric Biggers }
447059c2a4dSEric Biggers 
448059c2a4dSEric Biggers static void adiantum_exit_tfm(struct crypto_skcipher *tfm)
449059c2a4dSEric Biggers {
450059c2a4dSEric Biggers 	struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
451059c2a4dSEric Biggers 
452059c2a4dSEric Biggers 	crypto_free_skcipher(tctx->streamcipher);
453059c2a4dSEric Biggers 	crypto_free_cipher(tctx->blockcipher);
454059c2a4dSEric Biggers 	crypto_free_shash(tctx->hash);
455059c2a4dSEric Biggers }
456059c2a4dSEric Biggers 
457059c2a4dSEric Biggers static void adiantum_free_instance(struct skcipher_instance *inst)
458059c2a4dSEric Biggers {
459059c2a4dSEric Biggers 	struct adiantum_instance_ctx *ictx = skcipher_instance_ctx(inst);
460059c2a4dSEric Biggers 
461059c2a4dSEric Biggers 	crypto_drop_skcipher(&ictx->streamcipher_spawn);
462ba448407SEric Biggers 	crypto_drop_cipher(&ictx->blockcipher_spawn);
463059c2a4dSEric Biggers 	crypto_drop_shash(&ictx->hash_spawn);
464059c2a4dSEric Biggers 	kfree(inst);
465059c2a4dSEric Biggers }
466059c2a4dSEric Biggers 
467059c2a4dSEric Biggers /*
468059c2a4dSEric Biggers  * Check for a supported set of inner algorithms.
469059c2a4dSEric Biggers  * See the comment at the beginning of this file.
470059c2a4dSEric Biggers  */
471059c2a4dSEric Biggers static bool adiantum_supported_algorithms(struct skcipher_alg *streamcipher_alg,
472059c2a4dSEric Biggers 					  struct crypto_alg *blockcipher_alg,
473059c2a4dSEric Biggers 					  struct shash_alg *hash_alg)
474059c2a4dSEric Biggers {
475059c2a4dSEric Biggers 	if (strcmp(streamcipher_alg->base.cra_name, "xchacha12") != 0 &&
476059c2a4dSEric Biggers 	    strcmp(streamcipher_alg->base.cra_name, "xchacha20") != 0)
477059c2a4dSEric Biggers 		return false;
478059c2a4dSEric Biggers 
479059c2a4dSEric Biggers 	if (blockcipher_alg->cra_cipher.cia_min_keysize > BLOCKCIPHER_KEY_SIZE ||
480059c2a4dSEric Biggers 	    blockcipher_alg->cra_cipher.cia_max_keysize < BLOCKCIPHER_KEY_SIZE)
481059c2a4dSEric Biggers 		return false;
482059c2a4dSEric Biggers 	if (blockcipher_alg->cra_blocksize != BLOCKCIPHER_BLOCK_SIZE)
483059c2a4dSEric Biggers 		return false;
484059c2a4dSEric Biggers 
485059c2a4dSEric Biggers 	if (strcmp(hash_alg->base.cra_name, "nhpoly1305") != 0)
486059c2a4dSEric Biggers 		return false;
487059c2a4dSEric Biggers 
488059c2a4dSEric Biggers 	return true;
489059c2a4dSEric Biggers }
490059c2a4dSEric Biggers 
491059c2a4dSEric Biggers static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb)
492059c2a4dSEric Biggers {
493b9f76dddSEric Biggers 	u32 mask;
494059c2a4dSEric Biggers 	const char *nhpoly1305_name;
495059c2a4dSEric Biggers 	struct skcipher_instance *inst;
496059c2a4dSEric Biggers 	struct adiantum_instance_ctx *ictx;
497059c2a4dSEric Biggers 	struct skcipher_alg *streamcipher_alg;
498059c2a4dSEric Biggers 	struct crypto_alg *blockcipher_alg;
499059c2a4dSEric Biggers 	struct shash_alg *hash_alg;
500059c2a4dSEric Biggers 	int err;
501059c2a4dSEric Biggers 
5027bcb2c99SEric Biggers 	err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
5037bcb2c99SEric Biggers 	if (err)
5047bcb2c99SEric Biggers 		return err;
505059c2a4dSEric Biggers 
506059c2a4dSEric Biggers 	inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
507059c2a4dSEric Biggers 	if (!inst)
508059c2a4dSEric Biggers 		return -ENOMEM;
509059c2a4dSEric Biggers 	ictx = skcipher_instance_ctx(inst);
510059c2a4dSEric Biggers 
511059c2a4dSEric Biggers 	/* Stream cipher, e.g. "xchacha12" */
512b9f76dddSEric Biggers 	err = crypto_grab_skcipher(&ictx->streamcipher_spawn,
513b9f76dddSEric Biggers 				   skcipher_crypto_instance(inst),
514ba448407SEric Biggers 				   crypto_attr_alg_name(tb[1]), 0, mask);
515059c2a4dSEric Biggers 	if (err)
516ba448407SEric Biggers 		goto err_free_inst;
517059c2a4dSEric Biggers 	streamcipher_alg = crypto_spawn_skcipher_alg(&ictx->streamcipher_spawn);
518059c2a4dSEric Biggers 
519059c2a4dSEric Biggers 	/* Block cipher, e.g. "aes" */
520ba448407SEric Biggers 	err = crypto_grab_cipher(&ictx->blockcipher_spawn,
521de95c957SEric Biggers 				 skcipher_crypto_instance(inst),
522ba448407SEric Biggers 				 crypto_attr_alg_name(tb[2]), 0, mask);
523059c2a4dSEric Biggers 	if (err)
524ba448407SEric Biggers 		goto err_free_inst;
525ba448407SEric Biggers 	blockcipher_alg = crypto_spawn_cipher_alg(&ictx->blockcipher_spawn);
526059c2a4dSEric Biggers 
527c6018e1aSEric Biggers 	/* NHPoly1305 ε-∆U hash function */
528ba448407SEric Biggers 	nhpoly1305_name = crypto_attr_alg_name(tb[3]);
529ba448407SEric Biggers 	if (nhpoly1305_name == ERR_PTR(-ENOENT))
530ba448407SEric Biggers 		nhpoly1305_name = "nhpoly1305";
531ba448407SEric Biggers 	err = crypto_grab_shash(&ictx->hash_spawn,
532ba448407SEric Biggers 				skcipher_crypto_instance(inst),
533ba448407SEric Biggers 				nhpoly1305_name, 0, mask);
53400c9fe37SEric Biggers 	if (err)
535ba448407SEric Biggers 		goto err_free_inst;
536ba448407SEric Biggers 	hash_alg = crypto_spawn_shash_alg(&ictx->hash_spawn);
537059c2a4dSEric Biggers 
538059c2a4dSEric Biggers 	/* Check the set of algorithms */
539059c2a4dSEric Biggers 	if (!adiantum_supported_algorithms(streamcipher_alg, blockcipher_alg,
540059c2a4dSEric Biggers 					   hash_alg)) {
541059c2a4dSEric Biggers 		pr_warn("Unsupported Adiantum instantiation: (%s,%s,%s)\n",
542059c2a4dSEric Biggers 			streamcipher_alg->base.cra_name,
543059c2a4dSEric Biggers 			blockcipher_alg->cra_name, hash_alg->base.cra_name);
544059c2a4dSEric Biggers 		err = -EINVAL;
545ba448407SEric Biggers 		goto err_free_inst;
546059c2a4dSEric Biggers 	}
547059c2a4dSEric Biggers 
548059c2a4dSEric Biggers 	/* Instance fields */
549059c2a4dSEric Biggers 
550059c2a4dSEric Biggers 	err = -ENAMETOOLONG;
551059c2a4dSEric Biggers 	if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
552059c2a4dSEric Biggers 		     "adiantum(%s,%s)", streamcipher_alg->base.cra_name,
553059c2a4dSEric Biggers 		     blockcipher_alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
554ba448407SEric Biggers 		goto err_free_inst;
555059c2a4dSEric Biggers 	if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
556059c2a4dSEric Biggers 		     "adiantum(%s,%s,%s)",
557059c2a4dSEric Biggers 		     streamcipher_alg->base.cra_driver_name,
558059c2a4dSEric Biggers 		     blockcipher_alg->cra_driver_name,
559059c2a4dSEric Biggers 		     hash_alg->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
560ba448407SEric Biggers 		goto err_free_inst;
561059c2a4dSEric Biggers 
562059c2a4dSEric Biggers 	inst->alg.base.cra_blocksize = BLOCKCIPHER_BLOCK_SIZE;
563059c2a4dSEric Biggers 	inst->alg.base.cra_ctxsize = sizeof(struct adiantum_tfm_ctx);
564059c2a4dSEric Biggers 	inst->alg.base.cra_alignmask = streamcipher_alg->base.cra_alignmask |
565059c2a4dSEric Biggers 				       hash_alg->base.cra_alignmask;
566059c2a4dSEric Biggers 	/*
567059c2a4dSEric Biggers 	 * The block cipher is only invoked once per message, so for long
568059c2a4dSEric Biggers 	 * messages (e.g. sectors for disk encryption) its performance doesn't
569059c2a4dSEric Biggers 	 * matter as much as that of the stream cipher and hash function.  Thus,
570059c2a4dSEric Biggers 	 * weigh the block cipher's ->cra_priority less.
571059c2a4dSEric Biggers 	 */
572059c2a4dSEric Biggers 	inst->alg.base.cra_priority = (4 * streamcipher_alg->base.cra_priority +
573059c2a4dSEric Biggers 				       2 * hash_alg->base.cra_priority +
574059c2a4dSEric Biggers 				       blockcipher_alg->cra_priority) / 7;
575059c2a4dSEric Biggers 
576059c2a4dSEric Biggers 	inst->alg.setkey = adiantum_setkey;
577059c2a4dSEric Biggers 	inst->alg.encrypt = adiantum_encrypt;
578059c2a4dSEric Biggers 	inst->alg.decrypt = adiantum_decrypt;
579059c2a4dSEric Biggers 	inst->alg.init = adiantum_init_tfm;
580059c2a4dSEric Biggers 	inst->alg.exit = adiantum_exit_tfm;
581059c2a4dSEric Biggers 	inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(streamcipher_alg);
582059c2a4dSEric Biggers 	inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(streamcipher_alg);
583059c2a4dSEric Biggers 	inst->alg.ivsize = TWEAK_SIZE;
584059c2a4dSEric Biggers 
585059c2a4dSEric Biggers 	inst->free = adiantum_free_instance;
586059c2a4dSEric Biggers 
587059c2a4dSEric Biggers 	err = skcipher_register_instance(tmpl, inst);
588ba448407SEric Biggers 	if (err) {
589ba448407SEric Biggers err_free_inst:
590ba448407SEric Biggers 		adiantum_free_instance(inst);
591ba448407SEric Biggers 	}
592059c2a4dSEric Biggers 	return err;
593059c2a4dSEric Biggers }
594059c2a4dSEric Biggers 
595059c2a4dSEric Biggers /* adiantum(streamcipher_name, blockcipher_name [, nhpoly1305_name]) */
596059c2a4dSEric Biggers static struct crypto_template adiantum_tmpl = {
597059c2a4dSEric Biggers 	.name = "adiantum",
598059c2a4dSEric Biggers 	.create = adiantum_create,
599059c2a4dSEric Biggers 	.module = THIS_MODULE,
600059c2a4dSEric Biggers };
601059c2a4dSEric Biggers 
602059c2a4dSEric Biggers static int __init adiantum_module_init(void)
603059c2a4dSEric Biggers {
604059c2a4dSEric Biggers 	return crypto_register_template(&adiantum_tmpl);
605059c2a4dSEric Biggers }
606059c2a4dSEric Biggers 
607059c2a4dSEric Biggers static void __exit adiantum_module_exit(void)
608059c2a4dSEric Biggers {
609059c2a4dSEric Biggers 	crypto_unregister_template(&adiantum_tmpl);
610059c2a4dSEric Biggers }
611059c2a4dSEric Biggers 
612c4741b23SEric Biggers subsys_initcall(adiantum_module_init);
613059c2a4dSEric Biggers module_exit(adiantum_module_exit);
614059c2a4dSEric Biggers 
615059c2a4dSEric Biggers MODULE_DESCRIPTION("Adiantum length-preserving encryption mode");
616059c2a4dSEric Biggers MODULE_LICENSE("GPL v2");
617059c2a4dSEric Biggers MODULE_AUTHOR("Eric Biggers <ebiggers@google.com>");
618059c2a4dSEric Biggers MODULE_ALIAS_CRYPTO("adiantum");
619