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