1 // SPDX-License-Identifier: GPL-2.0
3 * ESSIV skcipher and aead template for block encryption
5 * This template encapsulates the ESSIV IV generation algorithm used by
6 * dm-crypt and fscrypt, which converts the initial vector for the skcipher
7 * used for block encryption, by encrypting it using the hash of the
8 * skcipher key as encryption key. Usually, the input IV is a 64-bit sector
9 * number in LE representation zero-padded to the size of the IV, but this
10 * is not assumed by this driver.
12 * The typical use of this template is to instantiate the skcipher
13 * 'essiv(cbc(aes),sha256)', which is the only instantiation used by
14 * fscrypt, and the most relevant one for dm-crypt. However, dm-crypt
15 * also permits ESSIV to be used in combination with the authenc template,
16 * e.g., 'essiv(authenc(hmac(sha256),cbc(aes)),sha256)', in which case
17 * we need to instantiate an aead that accepts the same special key format
18 * as the authenc template, and deals with the way the encrypted IV is
19 * embedded into the AAD area of the aead request. This means the AEAD
20 * flavor produced by this template is tightly coupled to the way dm-crypt
23 * Copyright (c) 2019 Linaro, Ltd. <ard.biesheuvel@linaro.org>
26 * adiantum length-preserving encryption mode
28 * Copyright 2018 Google LLC
31 #include <crypto/authenc.h>
32 #include <crypto/internal/aead.h>
33 #include <crypto/internal/hash.h>
34 #include <crypto/internal/skcipher.h>
35 #include <crypto/scatterwalk.h>
36 #include <linux/module.h>
40 struct essiv_instance_ctx {
42 struct crypto_skcipher_spawn skcipher_spawn;
43 struct crypto_aead_spawn aead_spawn;
45 char essiv_cipher_name[CRYPTO_MAX_ALG_NAME];
46 char shash_driver_name[CRYPTO_MAX_ALG_NAME];
49 struct essiv_tfm_ctx {
51 struct crypto_skcipher *skcipher;
52 struct crypto_aead *aead;
54 struct crypto_cipher *essiv_cipher;
55 struct crypto_shash *hash;
59 struct essiv_aead_request_ctx {
60 struct scatterlist sg[4];
62 struct aead_request aead_req;
65 static int essiv_skcipher_setkey(struct crypto_skcipher *tfm,
66 const u8 *key, unsigned int keylen)
68 struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
69 SHASH_DESC_ON_STACK(desc, tctx->hash);
70 u8 salt[HASH_MAX_DIGESTSIZE];
73 crypto_skcipher_clear_flags(tctx->u.skcipher, CRYPTO_TFM_REQ_MASK);
74 crypto_skcipher_set_flags(tctx->u.skcipher,
75 crypto_skcipher_get_flags(tfm) &
77 err = crypto_skcipher_setkey(tctx->u.skcipher, key, keylen);
81 desc->tfm = tctx->hash;
82 err = crypto_shash_digest(desc, key, keylen, salt);
86 crypto_cipher_clear_flags(tctx->essiv_cipher, CRYPTO_TFM_REQ_MASK);
87 crypto_cipher_set_flags(tctx->essiv_cipher,
88 crypto_skcipher_get_flags(tfm) &
90 return crypto_cipher_setkey(tctx->essiv_cipher, salt,
91 crypto_shash_digestsize(tctx->hash));
94 static int essiv_aead_setkey(struct crypto_aead *tfm, const u8 *key,
97 struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
98 SHASH_DESC_ON_STACK(desc, tctx->hash);
99 struct crypto_authenc_keys keys;
100 u8 salt[HASH_MAX_DIGESTSIZE];
103 crypto_aead_clear_flags(tctx->u.aead, CRYPTO_TFM_REQ_MASK);
104 crypto_aead_set_flags(tctx->u.aead, crypto_aead_get_flags(tfm) &
105 CRYPTO_TFM_REQ_MASK);
106 err = crypto_aead_setkey(tctx->u.aead, key, keylen);
110 if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
113 desc->tfm = tctx->hash;
114 err = crypto_shash_init(desc) ?:
115 crypto_shash_update(desc, keys.enckey, keys.enckeylen) ?:
116 crypto_shash_finup(desc, keys.authkey, keys.authkeylen, salt);
120 crypto_cipher_clear_flags(tctx->essiv_cipher, CRYPTO_TFM_REQ_MASK);
121 crypto_cipher_set_flags(tctx->essiv_cipher, crypto_aead_get_flags(tfm) &
122 CRYPTO_TFM_REQ_MASK);
123 return crypto_cipher_setkey(tctx->essiv_cipher, salt,
124 crypto_shash_digestsize(tctx->hash));
127 static int essiv_aead_setauthsize(struct crypto_aead *tfm,
128 unsigned int authsize)
130 struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
132 return crypto_aead_setauthsize(tctx->u.aead, authsize);
135 static void essiv_skcipher_done(struct crypto_async_request *areq, int err)
137 struct skcipher_request *req = areq->data;
139 skcipher_request_complete(req, err);
142 static int essiv_skcipher_crypt(struct skcipher_request *req, bool enc)
144 struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
145 const struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
146 struct skcipher_request *subreq = skcipher_request_ctx(req);
148 crypto_cipher_encrypt_one(tctx->essiv_cipher, req->iv, req->iv);
150 skcipher_request_set_tfm(subreq, tctx->u.skcipher);
151 skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
153 skcipher_request_set_callback(subreq, skcipher_request_flags(req),
154 essiv_skcipher_done, req);
156 return enc ? crypto_skcipher_encrypt(subreq) :
157 crypto_skcipher_decrypt(subreq);
160 static int essiv_skcipher_encrypt(struct skcipher_request *req)
162 return essiv_skcipher_crypt(req, true);
165 static int essiv_skcipher_decrypt(struct skcipher_request *req)
167 return essiv_skcipher_crypt(req, false);
170 static void essiv_aead_done(struct crypto_async_request *areq, int err)
172 struct aead_request *req = areq->data;
173 struct essiv_aead_request_ctx *rctx = aead_request_ctx(req);
176 aead_request_complete(req, err);
179 static int essiv_aead_crypt(struct aead_request *req, bool enc)
181 struct crypto_aead *tfm = crypto_aead_reqtfm(req);
182 const struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
183 struct essiv_aead_request_ctx *rctx = aead_request_ctx(req);
184 struct aead_request *subreq = &rctx->aead_req;
185 struct scatterlist *src = req->src;
188 crypto_cipher_encrypt_one(tctx->essiv_cipher, req->iv, req->iv);
191 * dm-crypt embeds the sector number and the IV in the AAD region, so
192 * we have to copy the converted IV into the right scatterlist before
196 if (req->src == req->dst || !enc) {
197 scatterwalk_map_and_copy(req->iv, req->dst,
198 req->assoclen - crypto_aead_ivsize(tfm),
199 crypto_aead_ivsize(tfm), 1);
201 u8 *iv = (u8 *)aead_request_ctx(req) + tctx->ivoffset;
202 int ivsize = crypto_aead_ivsize(tfm);
203 int ssize = req->assoclen - ivsize;
204 struct scatterlist *sg;
210 nents = sg_nents_for_len(req->src, ssize);
214 memcpy(iv, req->iv, ivsize);
215 sg_init_table(rctx->sg, 4);
217 if (unlikely(nents > 1)) {
219 * This is a case that rarely occurs in practice, but
220 * for correctness, we have to deal with it nonetheless.
222 rctx->assoc = kmalloc(ssize, GFP_ATOMIC);
226 scatterwalk_map_and_copy(rctx->assoc, req->src, 0,
228 sg_set_buf(rctx->sg, rctx->assoc, ssize);
230 sg_set_page(rctx->sg, sg_page(req->src), ssize,
234 sg_set_buf(rctx->sg + 1, iv, ivsize);
235 sg = scatterwalk_ffwd(rctx->sg + 2, req->src, req->assoclen);
236 if (sg != rctx->sg + 2)
237 sg_chain(rctx->sg, 3, sg);
242 aead_request_set_tfm(subreq, tctx->u.aead);
243 aead_request_set_ad(subreq, req->assoclen);
244 aead_request_set_callback(subreq, aead_request_flags(req),
245 essiv_aead_done, req);
246 aead_request_set_crypt(subreq, src, req->dst, req->cryptlen, req->iv);
248 err = enc ? crypto_aead_encrypt(subreq) :
249 crypto_aead_decrypt(subreq);
251 if (rctx->assoc && err != -EINPROGRESS)
256 static int essiv_aead_encrypt(struct aead_request *req)
258 return essiv_aead_crypt(req, true);
261 static int essiv_aead_decrypt(struct aead_request *req)
263 return essiv_aead_crypt(req, false);
266 static int essiv_init_tfm(struct essiv_instance_ctx *ictx,
267 struct essiv_tfm_ctx *tctx)
269 struct crypto_cipher *essiv_cipher;
270 struct crypto_shash *hash;
273 essiv_cipher = crypto_alloc_cipher(ictx->essiv_cipher_name, 0, 0);
274 if (IS_ERR(essiv_cipher))
275 return PTR_ERR(essiv_cipher);
277 hash = crypto_alloc_shash(ictx->shash_driver_name, 0, 0);
280 goto err_free_essiv_cipher;
283 tctx->essiv_cipher = essiv_cipher;
288 err_free_essiv_cipher:
289 crypto_free_cipher(essiv_cipher);
293 static int essiv_skcipher_init_tfm(struct crypto_skcipher *tfm)
295 struct skcipher_instance *inst = skcipher_alg_instance(tfm);
296 struct essiv_instance_ctx *ictx = skcipher_instance_ctx(inst);
297 struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
298 struct crypto_skcipher *skcipher;
301 skcipher = crypto_spawn_skcipher(&ictx->u.skcipher_spawn);
302 if (IS_ERR(skcipher))
303 return PTR_ERR(skcipher);
305 crypto_skcipher_set_reqsize(tfm, sizeof(struct skcipher_request) +
306 crypto_skcipher_reqsize(skcipher));
308 err = essiv_init_tfm(ictx, tctx);
310 crypto_free_skcipher(skcipher);
314 tctx->u.skcipher = skcipher;
318 static int essiv_aead_init_tfm(struct crypto_aead *tfm)
320 struct aead_instance *inst = aead_alg_instance(tfm);
321 struct essiv_instance_ctx *ictx = aead_instance_ctx(inst);
322 struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
323 struct crypto_aead *aead;
324 unsigned int subreq_size;
327 BUILD_BUG_ON(offsetofend(struct essiv_aead_request_ctx, aead_req) !=
328 sizeof(struct essiv_aead_request_ctx));
330 aead = crypto_spawn_aead(&ictx->u.aead_spawn);
332 return PTR_ERR(aead);
334 subreq_size = sizeof_field(struct essiv_aead_request_ctx, aead_req) +
335 crypto_aead_reqsize(aead);
337 tctx->ivoffset = offsetof(struct essiv_aead_request_ctx, aead_req) +
339 crypto_aead_set_reqsize(tfm, tctx->ivoffset + crypto_aead_ivsize(aead));
341 err = essiv_init_tfm(ictx, tctx);
343 crypto_free_aead(aead);
351 static void essiv_skcipher_exit_tfm(struct crypto_skcipher *tfm)
353 struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
355 crypto_free_skcipher(tctx->u.skcipher);
356 crypto_free_cipher(tctx->essiv_cipher);
357 crypto_free_shash(tctx->hash);
360 static void essiv_aead_exit_tfm(struct crypto_aead *tfm)
362 struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
364 crypto_free_aead(tctx->u.aead);
365 crypto_free_cipher(tctx->essiv_cipher);
366 crypto_free_shash(tctx->hash);
369 static void essiv_skcipher_free_instance(struct skcipher_instance *inst)
371 struct essiv_instance_ctx *ictx = skcipher_instance_ctx(inst);
373 crypto_drop_skcipher(&ictx->u.skcipher_spawn);
377 static void essiv_aead_free_instance(struct aead_instance *inst)
379 struct essiv_instance_ctx *ictx = aead_instance_ctx(inst);
381 crypto_drop_aead(&ictx->u.aead_spawn);
385 static bool parse_cipher_name(char *essiv_cipher_name, const char *cra_name)
390 /* find the last opening parens */
391 p = strrchr(cra_name, '(');
395 /* find the first closing parens in the tail of the string */
401 if (len >= CRYPTO_MAX_ALG_NAME)
404 memcpy(essiv_cipher_name, p, len);
405 essiv_cipher_name[len] = '\0';
409 static bool essiv_supported_algorithms(const char *essiv_cipher_name,
410 struct shash_alg *hash_alg,
413 struct crypto_alg *alg;
416 alg = crypto_alg_mod_lookup(essiv_cipher_name,
417 CRYPTO_ALG_TYPE_CIPHER,
418 CRYPTO_ALG_TYPE_MASK);
422 if (hash_alg->digestsize < alg->cra_cipher.cia_min_keysize ||
423 hash_alg->digestsize > alg->cra_cipher.cia_max_keysize)
426 if (ivsize != alg->cra_blocksize)
429 if (crypto_shash_alg_needs_key(hash_alg))
439 static int essiv_create(struct crypto_template *tmpl, struct rtattr **tb)
441 struct crypto_attr_type *algt;
442 const char *inner_cipher_name;
443 const char *shash_name;
444 struct skcipher_instance *skcipher_inst = NULL;
445 struct aead_instance *aead_inst = NULL;
446 struct crypto_instance *inst;
447 struct crypto_alg *base, *block_base;
448 struct essiv_instance_ctx *ictx;
449 struct skcipher_alg *skcipher_alg = NULL;
450 struct aead_alg *aead_alg = NULL;
451 struct crypto_alg *_hash_alg;
452 struct shash_alg *hash_alg;
458 algt = crypto_get_attr_type(tb);
460 return PTR_ERR(algt);
462 inner_cipher_name = crypto_attr_alg_name(tb[1]);
463 if (IS_ERR(inner_cipher_name))
464 return PTR_ERR(inner_cipher_name);
466 shash_name = crypto_attr_alg_name(tb[2]);
467 if (IS_ERR(shash_name))
468 return PTR_ERR(shash_name);
470 type = algt->type & algt->mask;
471 mask = crypto_requires_sync(algt->type, algt->mask);
474 case CRYPTO_ALG_TYPE_SKCIPHER:
475 skcipher_inst = kzalloc(sizeof(*skcipher_inst) +
476 sizeof(*ictx), GFP_KERNEL);
479 inst = skcipher_crypto_instance(skcipher_inst);
480 base = &skcipher_inst->alg.base;
481 ictx = crypto_instance_ctx(inst);
483 /* Symmetric cipher, e.g., "cbc(aes)" */
484 err = crypto_grab_skcipher(&ictx->u.skcipher_spawn, inst,
485 inner_cipher_name, 0, mask);
488 skcipher_alg = crypto_spawn_skcipher_alg(&ictx->u.skcipher_spawn);
489 block_base = &skcipher_alg->base;
490 ivsize = crypto_skcipher_alg_ivsize(skcipher_alg);
493 case CRYPTO_ALG_TYPE_AEAD:
494 aead_inst = kzalloc(sizeof(*aead_inst) +
495 sizeof(*ictx), GFP_KERNEL);
498 inst = aead_crypto_instance(aead_inst);
499 base = &aead_inst->alg.base;
500 ictx = crypto_instance_ctx(inst);
502 /* AEAD cipher, e.g., "authenc(hmac(sha256),cbc(aes))" */
503 err = crypto_grab_aead(&ictx->u.aead_spawn, inst,
504 inner_cipher_name, 0, mask);
507 aead_alg = crypto_spawn_aead_alg(&ictx->u.aead_spawn);
508 block_base = &aead_alg->base;
509 if (!strstarts(block_base->cra_name, "authenc(")) {
510 pr_warn("Only authenc() type AEADs are supported by ESSIV\n");
512 goto out_drop_skcipher;
514 ivsize = aead_alg->ivsize;
521 if (!parse_cipher_name(ictx->essiv_cipher_name, block_base->cra_name)) {
522 pr_warn("Failed to parse ESSIV cipher name from skcipher cra_name\n");
524 goto out_drop_skcipher;
527 /* Synchronous hash, e.g., "sha256" */
528 _hash_alg = crypto_alg_mod_lookup(shash_name,
529 CRYPTO_ALG_TYPE_SHASH,
530 CRYPTO_ALG_TYPE_MASK);
531 if (IS_ERR(_hash_alg)) {
532 err = PTR_ERR(_hash_alg);
533 goto out_drop_skcipher;
535 hash_alg = __crypto_shash_alg(_hash_alg);
537 /* Check the set of algorithms */
538 if (!essiv_supported_algorithms(ictx->essiv_cipher_name, hash_alg,
540 pr_warn("Unsupported essiv instantiation: essiv(%s,%s)\n",
541 block_base->cra_name, hash_alg->base.cra_name);
546 /* record the driver name so we can instantiate this exact algo later */
547 strlcpy(ictx->shash_driver_name, hash_alg->base.cra_driver_name,
548 CRYPTO_MAX_ALG_NAME);
550 /* Instance fields */
553 if (snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME,
554 "essiv(%s,%s)", block_base->cra_name,
555 hash_alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
557 if (snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME,
558 "essiv(%s,%s)", block_base->cra_driver_name,
559 hash_alg->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
562 base->cra_flags = block_base->cra_flags & CRYPTO_ALG_ASYNC;
563 base->cra_blocksize = block_base->cra_blocksize;
564 base->cra_ctxsize = sizeof(struct essiv_tfm_ctx);
565 base->cra_alignmask = block_base->cra_alignmask;
566 base->cra_priority = block_base->cra_priority;
568 if (type == CRYPTO_ALG_TYPE_SKCIPHER) {
569 skcipher_inst->alg.setkey = essiv_skcipher_setkey;
570 skcipher_inst->alg.encrypt = essiv_skcipher_encrypt;
571 skcipher_inst->alg.decrypt = essiv_skcipher_decrypt;
572 skcipher_inst->alg.init = essiv_skcipher_init_tfm;
573 skcipher_inst->alg.exit = essiv_skcipher_exit_tfm;
575 skcipher_inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(skcipher_alg);
576 skcipher_inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(skcipher_alg);
577 skcipher_inst->alg.ivsize = ivsize;
578 skcipher_inst->alg.chunksize = crypto_skcipher_alg_chunksize(skcipher_alg);
579 skcipher_inst->alg.walksize = crypto_skcipher_alg_walksize(skcipher_alg);
581 skcipher_inst->free = essiv_skcipher_free_instance;
583 err = skcipher_register_instance(tmpl, skcipher_inst);
585 aead_inst->alg.setkey = essiv_aead_setkey;
586 aead_inst->alg.setauthsize = essiv_aead_setauthsize;
587 aead_inst->alg.encrypt = essiv_aead_encrypt;
588 aead_inst->alg.decrypt = essiv_aead_decrypt;
589 aead_inst->alg.init = essiv_aead_init_tfm;
590 aead_inst->alg.exit = essiv_aead_exit_tfm;
592 aead_inst->alg.ivsize = ivsize;
593 aead_inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(aead_alg);
594 aead_inst->alg.chunksize = crypto_aead_alg_chunksize(aead_alg);
596 aead_inst->free = essiv_aead_free_instance;
598 err = aead_register_instance(tmpl, aead_inst);
604 crypto_mod_put(_hash_alg);
608 crypto_mod_put(_hash_alg);
610 if (type == CRYPTO_ALG_TYPE_SKCIPHER)
611 crypto_drop_skcipher(&ictx->u.skcipher_spawn);
613 crypto_drop_aead(&ictx->u.aead_spawn);
615 kfree(skcipher_inst);
620 /* essiv(cipher_name, shash_name) */
621 static struct crypto_template essiv_tmpl = {
623 .create = essiv_create,
624 .module = THIS_MODULE,
627 static int __init essiv_module_init(void)
629 return crypto_register_template(&essiv_tmpl);
632 static void __exit essiv_module_exit(void)
634 crypto_unregister_template(&essiv_tmpl);
637 subsys_initcall(essiv_module_init);
638 module_exit(essiv_module_exit);
640 MODULE_DESCRIPTION("ESSIV skcipher/aead wrapper for block encryption");
641 MODULE_LICENSE("GPL v2");
642 MODULE_ALIAS_CRYPTO("essiv");