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