Ard Biesheuvel | be1eb7f | 2019-08-19 17:17:33 +0300 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * ESSIV skcipher and aead template for block encryption |
| 4 | * |
| 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. |
| 11 | * |
| 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 |
| 21 | * happens to use it. |
| 22 | * |
| 23 | * Copyright (c) 2019 Linaro, Ltd. <ard.biesheuvel@linaro.org> |
| 24 | * |
| 25 | * Heavily based on: |
| 26 | * adiantum length-preserving encryption mode |
| 27 | * |
| 28 | * Copyright 2018 Google LLC |
| 29 | */ |
| 30 | |
| 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> |
| 37 | |
| 38 | #include "internal.h" |
| 39 | |
| 40 | struct essiv_instance_ctx { |
| 41 | union { |
| 42 | struct crypto_skcipher_spawn skcipher_spawn; |
| 43 | struct crypto_aead_spawn aead_spawn; |
| 44 | } u; |
| 45 | char essiv_cipher_name[CRYPTO_MAX_ALG_NAME]; |
| 46 | char shash_driver_name[CRYPTO_MAX_ALG_NAME]; |
| 47 | }; |
| 48 | |
| 49 | struct essiv_tfm_ctx { |
| 50 | union { |
| 51 | struct crypto_skcipher *skcipher; |
| 52 | struct crypto_aead *aead; |
| 53 | } u; |
| 54 | struct crypto_cipher *essiv_cipher; |
| 55 | struct crypto_shash *hash; |
| 56 | int ivoffset; |
| 57 | }; |
| 58 | |
| 59 | struct essiv_aead_request_ctx { |
| 60 | struct scatterlist sg[4]; |
| 61 | u8 *assoc; |
| 62 | struct aead_request aead_req; |
| 63 | }; |
| 64 | |
| 65 | static int essiv_skcipher_setkey(struct crypto_skcipher *tfm, |
| 66 | const u8 *key, unsigned int keylen) |
| 67 | { |
| 68 | struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm); |
| 69 | SHASH_DESC_ON_STACK(desc, tctx->hash); |
| 70 | u8 salt[HASH_MAX_DIGESTSIZE]; |
| 71 | int err; |
| 72 | |
| 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) & |
| 76 | CRYPTO_TFM_REQ_MASK); |
| 77 | err = crypto_skcipher_setkey(tctx->u.skcipher, key, keylen); |
| 78 | crypto_skcipher_set_flags(tfm, |
| 79 | crypto_skcipher_get_flags(tctx->u.skcipher) & |
| 80 | CRYPTO_TFM_RES_MASK); |
| 81 | if (err) |
| 82 | return err; |
| 83 | |
| 84 | desc->tfm = tctx->hash; |
| 85 | err = crypto_shash_digest(desc, key, keylen, salt); |
| 86 | if (err) |
| 87 | return err; |
| 88 | |
| 89 | crypto_cipher_clear_flags(tctx->essiv_cipher, CRYPTO_TFM_REQ_MASK); |
| 90 | crypto_cipher_set_flags(tctx->essiv_cipher, |
| 91 | crypto_skcipher_get_flags(tfm) & |
| 92 | CRYPTO_TFM_REQ_MASK); |
| 93 | err = crypto_cipher_setkey(tctx->essiv_cipher, salt, |
| 94 | crypto_shash_digestsize(tctx->hash)); |
| 95 | crypto_skcipher_set_flags(tfm, |
| 96 | crypto_cipher_get_flags(tctx->essiv_cipher) & |
| 97 | CRYPTO_TFM_RES_MASK); |
| 98 | |
| 99 | return err; |
| 100 | } |
| 101 | |
| 102 | static int essiv_aead_setkey(struct crypto_aead *tfm, const u8 *key, |
| 103 | unsigned int keylen) |
| 104 | { |
| 105 | struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm); |
| 106 | SHASH_DESC_ON_STACK(desc, tctx->hash); |
| 107 | struct crypto_authenc_keys keys; |
| 108 | u8 salt[HASH_MAX_DIGESTSIZE]; |
| 109 | int err; |
| 110 | |
| 111 | crypto_aead_clear_flags(tctx->u.aead, CRYPTO_TFM_REQ_MASK); |
| 112 | crypto_aead_set_flags(tctx->u.aead, crypto_aead_get_flags(tfm) & |
| 113 | CRYPTO_TFM_REQ_MASK); |
| 114 | err = crypto_aead_setkey(tctx->u.aead, key, keylen); |
| 115 | crypto_aead_set_flags(tfm, crypto_aead_get_flags(tctx->u.aead) & |
| 116 | CRYPTO_TFM_RES_MASK); |
| 117 | if (err) |
| 118 | return err; |
| 119 | |
Eric Biggers | 674f368 | 2019-12-30 21:19:36 -0600 | [diff] [blame^] | 120 | if (crypto_authenc_extractkeys(&keys, key, keylen) != 0) |
Ard Biesheuvel | be1eb7f | 2019-08-19 17:17:33 +0300 | [diff] [blame] | 121 | return -EINVAL; |
Ard Biesheuvel | be1eb7f | 2019-08-19 17:17:33 +0300 | [diff] [blame] | 122 | |
| 123 | desc->tfm = tctx->hash; |
| 124 | err = crypto_shash_init(desc) ?: |
| 125 | crypto_shash_update(desc, keys.enckey, keys.enckeylen) ?: |
| 126 | crypto_shash_finup(desc, keys.authkey, keys.authkeylen, salt); |
| 127 | if (err) |
| 128 | return err; |
| 129 | |
| 130 | crypto_cipher_clear_flags(tctx->essiv_cipher, CRYPTO_TFM_REQ_MASK); |
| 131 | crypto_cipher_set_flags(tctx->essiv_cipher, crypto_aead_get_flags(tfm) & |
| 132 | CRYPTO_TFM_REQ_MASK); |
| 133 | err = crypto_cipher_setkey(tctx->essiv_cipher, salt, |
| 134 | crypto_shash_digestsize(tctx->hash)); |
| 135 | crypto_aead_set_flags(tfm, crypto_cipher_get_flags(tctx->essiv_cipher) & |
| 136 | CRYPTO_TFM_RES_MASK); |
| 137 | |
| 138 | return err; |
| 139 | } |
| 140 | |
| 141 | static int essiv_aead_setauthsize(struct crypto_aead *tfm, |
| 142 | unsigned int authsize) |
| 143 | { |
| 144 | struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm); |
| 145 | |
| 146 | return crypto_aead_setauthsize(tctx->u.aead, authsize); |
| 147 | } |
| 148 | |
| 149 | static void essiv_skcipher_done(struct crypto_async_request *areq, int err) |
| 150 | { |
| 151 | struct skcipher_request *req = areq->data; |
| 152 | |
| 153 | skcipher_request_complete(req, err); |
| 154 | } |
| 155 | |
| 156 | static int essiv_skcipher_crypt(struct skcipher_request *req, bool enc) |
| 157 | { |
| 158 | struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req); |
| 159 | const struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm); |
| 160 | struct skcipher_request *subreq = skcipher_request_ctx(req); |
| 161 | |
| 162 | crypto_cipher_encrypt_one(tctx->essiv_cipher, req->iv, req->iv); |
| 163 | |
| 164 | skcipher_request_set_tfm(subreq, tctx->u.skcipher); |
| 165 | skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen, |
| 166 | req->iv); |
| 167 | skcipher_request_set_callback(subreq, skcipher_request_flags(req), |
| 168 | essiv_skcipher_done, req); |
| 169 | |
| 170 | return enc ? crypto_skcipher_encrypt(subreq) : |
| 171 | crypto_skcipher_decrypt(subreq); |
| 172 | } |
| 173 | |
| 174 | static int essiv_skcipher_encrypt(struct skcipher_request *req) |
| 175 | { |
| 176 | return essiv_skcipher_crypt(req, true); |
| 177 | } |
| 178 | |
| 179 | static int essiv_skcipher_decrypt(struct skcipher_request *req) |
| 180 | { |
| 181 | return essiv_skcipher_crypt(req, false); |
| 182 | } |
| 183 | |
| 184 | static void essiv_aead_done(struct crypto_async_request *areq, int err) |
| 185 | { |
| 186 | struct aead_request *req = areq->data; |
| 187 | struct essiv_aead_request_ctx *rctx = aead_request_ctx(req); |
| 188 | |
Chen Wandun | e18036d | 2019-11-16 14:51:00 +0800 | [diff] [blame] | 189 | kfree(rctx->assoc); |
Ard Biesheuvel | be1eb7f | 2019-08-19 17:17:33 +0300 | [diff] [blame] | 190 | aead_request_complete(req, err); |
| 191 | } |
| 192 | |
| 193 | static int essiv_aead_crypt(struct aead_request *req, bool enc) |
| 194 | { |
| 195 | struct crypto_aead *tfm = crypto_aead_reqtfm(req); |
| 196 | const struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm); |
| 197 | struct essiv_aead_request_ctx *rctx = aead_request_ctx(req); |
| 198 | struct aead_request *subreq = &rctx->aead_req; |
| 199 | struct scatterlist *src = req->src; |
| 200 | int err; |
| 201 | |
| 202 | crypto_cipher_encrypt_one(tctx->essiv_cipher, req->iv, req->iv); |
| 203 | |
| 204 | /* |
| 205 | * dm-crypt embeds the sector number and the IV in the AAD region, so |
| 206 | * we have to copy the converted IV into the right scatterlist before |
| 207 | * we pass it on. |
| 208 | */ |
| 209 | rctx->assoc = NULL; |
| 210 | if (req->src == req->dst || !enc) { |
| 211 | scatterwalk_map_and_copy(req->iv, req->dst, |
| 212 | req->assoclen - crypto_aead_ivsize(tfm), |
| 213 | crypto_aead_ivsize(tfm), 1); |
| 214 | } else { |
| 215 | u8 *iv = (u8 *)aead_request_ctx(req) + tctx->ivoffset; |
| 216 | int ivsize = crypto_aead_ivsize(tfm); |
| 217 | int ssize = req->assoclen - ivsize; |
| 218 | struct scatterlist *sg; |
| 219 | int nents; |
| 220 | |
| 221 | if (ssize < 0) |
| 222 | return -EINVAL; |
| 223 | |
| 224 | nents = sg_nents_for_len(req->src, ssize); |
| 225 | if (nents < 0) |
| 226 | return -EINVAL; |
| 227 | |
| 228 | memcpy(iv, req->iv, ivsize); |
| 229 | sg_init_table(rctx->sg, 4); |
| 230 | |
| 231 | if (unlikely(nents > 1)) { |
| 232 | /* |
| 233 | * This is a case that rarely occurs in practice, but |
| 234 | * for correctness, we have to deal with it nonetheless. |
| 235 | */ |
| 236 | rctx->assoc = kmalloc(ssize, GFP_ATOMIC); |
| 237 | if (!rctx->assoc) |
| 238 | return -ENOMEM; |
| 239 | |
| 240 | scatterwalk_map_and_copy(rctx->assoc, req->src, 0, |
| 241 | ssize, 0); |
| 242 | sg_set_buf(rctx->sg, rctx->assoc, ssize); |
| 243 | } else { |
| 244 | sg_set_page(rctx->sg, sg_page(req->src), ssize, |
| 245 | req->src->offset); |
| 246 | } |
| 247 | |
| 248 | sg_set_buf(rctx->sg + 1, iv, ivsize); |
| 249 | sg = scatterwalk_ffwd(rctx->sg + 2, req->src, req->assoclen); |
| 250 | if (sg != rctx->sg + 2) |
| 251 | sg_chain(rctx->sg, 3, sg); |
| 252 | |
| 253 | src = rctx->sg; |
| 254 | } |
| 255 | |
| 256 | aead_request_set_tfm(subreq, tctx->u.aead); |
| 257 | aead_request_set_ad(subreq, req->assoclen); |
| 258 | aead_request_set_callback(subreq, aead_request_flags(req), |
| 259 | essiv_aead_done, req); |
| 260 | aead_request_set_crypt(subreq, src, req->dst, req->cryptlen, req->iv); |
| 261 | |
| 262 | err = enc ? crypto_aead_encrypt(subreq) : |
| 263 | crypto_aead_decrypt(subreq); |
| 264 | |
| 265 | if (rctx->assoc && err != -EINPROGRESS) |
| 266 | kfree(rctx->assoc); |
| 267 | return err; |
| 268 | } |
| 269 | |
| 270 | static int essiv_aead_encrypt(struct aead_request *req) |
| 271 | { |
| 272 | return essiv_aead_crypt(req, true); |
| 273 | } |
| 274 | |
| 275 | static int essiv_aead_decrypt(struct aead_request *req) |
| 276 | { |
| 277 | return essiv_aead_crypt(req, false); |
| 278 | } |
| 279 | |
| 280 | static int essiv_init_tfm(struct essiv_instance_ctx *ictx, |
| 281 | struct essiv_tfm_ctx *tctx) |
| 282 | { |
| 283 | struct crypto_cipher *essiv_cipher; |
| 284 | struct crypto_shash *hash; |
| 285 | int err; |
| 286 | |
| 287 | essiv_cipher = crypto_alloc_cipher(ictx->essiv_cipher_name, 0, 0); |
| 288 | if (IS_ERR(essiv_cipher)) |
| 289 | return PTR_ERR(essiv_cipher); |
| 290 | |
| 291 | hash = crypto_alloc_shash(ictx->shash_driver_name, 0, 0); |
| 292 | if (IS_ERR(hash)) { |
| 293 | err = PTR_ERR(hash); |
| 294 | goto err_free_essiv_cipher; |
| 295 | } |
| 296 | |
| 297 | tctx->essiv_cipher = essiv_cipher; |
| 298 | tctx->hash = hash; |
| 299 | |
| 300 | return 0; |
| 301 | |
| 302 | err_free_essiv_cipher: |
| 303 | crypto_free_cipher(essiv_cipher); |
| 304 | return err; |
| 305 | } |
| 306 | |
| 307 | static int essiv_skcipher_init_tfm(struct crypto_skcipher *tfm) |
| 308 | { |
| 309 | struct skcipher_instance *inst = skcipher_alg_instance(tfm); |
| 310 | struct essiv_instance_ctx *ictx = skcipher_instance_ctx(inst); |
| 311 | struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm); |
| 312 | struct crypto_skcipher *skcipher; |
| 313 | int err; |
| 314 | |
| 315 | skcipher = crypto_spawn_skcipher(&ictx->u.skcipher_spawn); |
| 316 | if (IS_ERR(skcipher)) |
| 317 | return PTR_ERR(skcipher); |
| 318 | |
| 319 | crypto_skcipher_set_reqsize(tfm, sizeof(struct skcipher_request) + |
| 320 | crypto_skcipher_reqsize(skcipher)); |
| 321 | |
| 322 | err = essiv_init_tfm(ictx, tctx); |
| 323 | if (err) { |
| 324 | crypto_free_skcipher(skcipher); |
| 325 | return err; |
| 326 | } |
| 327 | |
| 328 | tctx->u.skcipher = skcipher; |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static int essiv_aead_init_tfm(struct crypto_aead *tfm) |
| 333 | { |
| 334 | struct aead_instance *inst = aead_alg_instance(tfm); |
| 335 | struct essiv_instance_ctx *ictx = aead_instance_ctx(inst); |
| 336 | struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm); |
| 337 | struct crypto_aead *aead; |
| 338 | unsigned int subreq_size; |
| 339 | int err; |
| 340 | |
| 341 | BUILD_BUG_ON(offsetofend(struct essiv_aead_request_ctx, aead_req) != |
| 342 | sizeof(struct essiv_aead_request_ctx)); |
| 343 | |
| 344 | aead = crypto_spawn_aead(&ictx->u.aead_spawn); |
| 345 | if (IS_ERR(aead)) |
| 346 | return PTR_ERR(aead); |
| 347 | |
| 348 | subreq_size = FIELD_SIZEOF(struct essiv_aead_request_ctx, aead_req) + |
| 349 | crypto_aead_reqsize(aead); |
| 350 | |
| 351 | tctx->ivoffset = offsetof(struct essiv_aead_request_ctx, aead_req) + |
| 352 | subreq_size; |
| 353 | crypto_aead_set_reqsize(tfm, tctx->ivoffset + crypto_aead_ivsize(aead)); |
| 354 | |
| 355 | err = essiv_init_tfm(ictx, tctx); |
| 356 | if (err) { |
| 357 | crypto_free_aead(aead); |
| 358 | return err; |
| 359 | } |
| 360 | |
| 361 | tctx->u.aead = aead; |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | static void essiv_skcipher_exit_tfm(struct crypto_skcipher *tfm) |
| 366 | { |
| 367 | struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm); |
| 368 | |
| 369 | crypto_free_skcipher(tctx->u.skcipher); |
| 370 | crypto_free_cipher(tctx->essiv_cipher); |
| 371 | crypto_free_shash(tctx->hash); |
| 372 | } |
| 373 | |
| 374 | static void essiv_aead_exit_tfm(struct crypto_aead *tfm) |
| 375 | { |
| 376 | struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm); |
| 377 | |
| 378 | crypto_free_aead(tctx->u.aead); |
| 379 | crypto_free_cipher(tctx->essiv_cipher); |
| 380 | crypto_free_shash(tctx->hash); |
| 381 | } |
| 382 | |
| 383 | static void essiv_skcipher_free_instance(struct skcipher_instance *inst) |
| 384 | { |
| 385 | struct essiv_instance_ctx *ictx = skcipher_instance_ctx(inst); |
| 386 | |
| 387 | crypto_drop_skcipher(&ictx->u.skcipher_spawn); |
| 388 | kfree(inst); |
| 389 | } |
| 390 | |
| 391 | static void essiv_aead_free_instance(struct aead_instance *inst) |
| 392 | { |
| 393 | struct essiv_instance_ctx *ictx = aead_instance_ctx(inst); |
| 394 | |
| 395 | crypto_drop_aead(&ictx->u.aead_spawn); |
| 396 | kfree(inst); |
| 397 | } |
| 398 | |
| 399 | static bool parse_cipher_name(char *essiv_cipher_name, const char *cra_name) |
| 400 | { |
| 401 | const char *p, *q; |
| 402 | int len; |
| 403 | |
| 404 | /* find the last opening parens */ |
| 405 | p = strrchr(cra_name, '('); |
| 406 | if (!p++) |
| 407 | return false; |
| 408 | |
| 409 | /* find the first closing parens in the tail of the string */ |
| 410 | q = strchr(p, ')'); |
| 411 | if (!q) |
| 412 | return false; |
| 413 | |
| 414 | len = q - p; |
| 415 | if (len >= CRYPTO_MAX_ALG_NAME) |
| 416 | return false; |
| 417 | |
| 418 | memcpy(essiv_cipher_name, p, len); |
| 419 | essiv_cipher_name[len] = '\0'; |
| 420 | return true; |
| 421 | } |
| 422 | |
| 423 | static bool essiv_supported_algorithms(const char *essiv_cipher_name, |
| 424 | struct shash_alg *hash_alg, |
| 425 | int ivsize) |
| 426 | { |
| 427 | struct crypto_alg *alg; |
| 428 | bool ret = false; |
| 429 | |
| 430 | alg = crypto_alg_mod_lookup(essiv_cipher_name, |
| 431 | CRYPTO_ALG_TYPE_CIPHER, |
| 432 | CRYPTO_ALG_TYPE_MASK); |
| 433 | if (IS_ERR(alg)) |
| 434 | return false; |
| 435 | |
| 436 | if (hash_alg->digestsize < alg->cra_cipher.cia_min_keysize || |
| 437 | hash_alg->digestsize > alg->cra_cipher.cia_max_keysize) |
| 438 | goto out; |
| 439 | |
| 440 | if (ivsize != alg->cra_blocksize) |
| 441 | goto out; |
| 442 | |
Eric Biggers | c288178 | 2019-11-29 11:35:22 -0800 | [diff] [blame] | 443 | if (crypto_shash_alg_needs_key(hash_alg)) |
Ard Biesheuvel | be1eb7f | 2019-08-19 17:17:33 +0300 | [diff] [blame] | 444 | goto out; |
| 445 | |
| 446 | ret = true; |
| 447 | |
| 448 | out: |
| 449 | crypto_mod_put(alg); |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | static int essiv_create(struct crypto_template *tmpl, struct rtattr **tb) |
| 454 | { |
| 455 | struct crypto_attr_type *algt; |
| 456 | const char *inner_cipher_name; |
| 457 | const char *shash_name; |
| 458 | struct skcipher_instance *skcipher_inst = NULL; |
| 459 | struct aead_instance *aead_inst = NULL; |
| 460 | struct crypto_instance *inst; |
| 461 | struct crypto_alg *base, *block_base; |
| 462 | struct essiv_instance_ctx *ictx; |
| 463 | struct skcipher_alg *skcipher_alg = NULL; |
| 464 | struct aead_alg *aead_alg = NULL; |
| 465 | struct crypto_alg *_hash_alg; |
| 466 | struct shash_alg *hash_alg; |
| 467 | int ivsize; |
| 468 | u32 type; |
| 469 | int err; |
| 470 | |
| 471 | algt = crypto_get_attr_type(tb); |
| 472 | if (IS_ERR(algt)) |
| 473 | return PTR_ERR(algt); |
| 474 | |
| 475 | inner_cipher_name = crypto_attr_alg_name(tb[1]); |
| 476 | if (IS_ERR(inner_cipher_name)) |
| 477 | return PTR_ERR(inner_cipher_name); |
| 478 | |
| 479 | shash_name = crypto_attr_alg_name(tb[2]); |
| 480 | if (IS_ERR(shash_name)) |
| 481 | return PTR_ERR(shash_name); |
| 482 | |
| 483 | type = algt->type & algt->mask; |
| 484 | |
| 485 | switch (type) { |
Eric Biggers | c65058b | 2019-10-25 12:41:12 -0700 | [diff] [blame] | 486 | case CRYPTO_ALG_TYPE_SKCIPHER: |
Ard Biesheuvel | be1eb7f | 2019-08-19 17:17:33 +0300 | [diff] [blame] | 487 | skcipher_inst = kzalloc(sizeof(*skcipher_inst) + |
| 488 | sizeof(*ictx), GFP_KERNEL); |
| 489 | if (!skcipher_inst) |
| 490 | return -ENOMEM; |
| 491 | inst = skcipher_crypto_instance(skcipher_inst); |
| 492 | base = &skcipher_inst->alg.base; |
| 493 | ictx = crypto_instance_ctx(inst); |
| 494 | |
| 495 | /* Symmetric cipher, e.g., "cbc(aes)" */ |
| 496 | crypto_set_skcipher_spawn(&ictx->u.skcipher_spawn, inst); |
| 497 | err = crypto_grab_skcipher(&ictx->u.skcipher_spawn, |
| 498 | inner_cipher_name, 0, |
| 499 | crypto_requires_sync(algt->type, |
| 500 | algt->mask)); |
| 501 | if (err) |
| 502 | goto out_free_inst; |
| 503 | skcipher_alg = crypto_spawn_skcipher_alg(&ictx->u.skcipher_spawn); |
| 504 | block_base = &skcipher_alg->base; |
| 505 | ivsize = crypto_skcipher_alg_ivsize(skcipher_alg); |
| 506 | break; |
| 507 | |
| 508 | case CRYPTO_ALG_TYPE_AEAD: |
| 509 | aead_inst = kzalloc(sizeof(*aead_inst) + |
| 510 | sizeof(*ictx), GFP_KERNEL); |
| 511 | if (!aead_inst) |
| 512 | return -ENOMEM; |
| 513 | inst = aead_crypto_instance(aead_inst); |
| 514 | base = &aead_inst->alg.base; |
| 515 | ictx = crypto_instance_ctx(inst); |
| 516 | |
| 517 | /* AEAD cipher, e.g., "authenc(hmac(sha256),cbc(aes))" */ |
| 518 | crypto_set_aead_spawn(&ictx->u.aead_spawn, inst); |
| 519 | err = crypto_grab_aead(&ictx->u.aead_spawn, |
| 520 | inner_cipher_name, 0, |
| 521 | crypto_requires_sync(algt->type, |
| 522 | algt->mask)); |
| 523 | if (err) |
| 524 | goto out_free_inst; |
| 525 | aead_alg = crypto_spawn_aead_alg(&ictx->u.aead_spawn); |
| 526 | block_base = &aead_alg->base; |
| 527 | if (!strstarts(block_base->cra_name, "authenc(")) { |
| 528 | pr_warn("Only authenc() type AEADs are supported by ESSIV\n"); |
| 529 | err = -EINVAL; |
| 530 | goto out_drop_skcipher; |
| 531 | } |
| 532 | ivsize = aead_alg->ivsize; |
| 533 | break; |
| 534 | |
| 535 | default: |
| 536 | return -EINVAL; |
| 537 | } |
| 538 | |
| 539 | if (!parse_cipher_name(ictx->essiv_cipher_name, block_base->cra_name)) { |
| 540 | pr_warn("Failed to parse ESSIV cipher name from skcipher cra_name\n"); |
| 541 | err = -EINVAL; |
| 542 | goto out_drop_skcipher; |
| 543 | } |
| 544 | |
| 545 | /* Synchronous hash, e.g., "sha256" */ |
| 546 | _hash_alg = crypto_alg_mod_lookup(shash_name, |
| 547 | CRYPTO_ALG_TYPE_SHASH, |
| 548 | CRYPTO_ALG_TYPE_MASK); |
| 549 | if (IS_ERR(_hash_alg)) { |
| 550 | err = PTR_ERR(_hash_alg); |
| 551 | goto out_drop_skcipher; |
| 552 | } |
| 553 | hash_alg = __crypto_shash_alg(_hash_alg); |
| 554 | |
| 555 | /* Check the set of algorithms */ |
| 556 | if (!essiv_supported_algorithms(ictx->essiv_cipher_name, hash_alg, |
| 557 | ivsize)) { |
| 558 | pr_warn("Unsupported essiv instantiation: essiv(%s,%s)\n", |
| 559 | block_base->cra_name, hash_alg->base.cra_name); |
| 560 | err = -EINVAL; |
| 561 | goto out_free_hash; |
| 562 | } |
| 563 | |
| 564 | /* record the driver name so we can instantiate this exact algo later */ |
| 565 | strlcpy(ictx->shash_driver_name, hash_alg->base.cra_driver_name, |
| 566 | CRYPTO_MAX_ALG_NAME); |
| 567 | |
| 568 | /* Instance fields */ |
| 569 | |
| 570 | err = -ENAMETOOLONG; |
| 571 | if (snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME, |
| 572 | "essiv(%s,%s)", block_base->cra_name, |
| 573 | hash_alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME) |
| 574 | goto out_free_hash; |
| 575 | if (snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME, |
| 576 | "essiv(%s,%s)", block_base->cra_driver_name, |
| 577 | hash_alg->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME) |
| 578 | goto out_free_hash; |
| 579 | |
| 580 | base->cra_flags = block_base->cra_flags & CRYPTO_ALG_ASYNC; |
| 581 | base->cra_blocksize = block_base->cra_blocksize; |
| 582 | base->cra_ctxsize = sizeof(struct essiv_tfm_ctx); |
| 583 | base->cra_alignmask = block_base->cra_alignmask; |
| 584 | base->cra_priority = block_base->cra_priority; |
| 585 | |
Eric Biggers | c65058b | 2019-10-25 12:41:12 -0700 | [diff] [blame] | 586 | if (type == CRYPTO_ALG_TYPE_SKCIPHER) { |
Ard Biesheuvel | be1eb7f | 2019-08-19 17:17:33 +0300 | [diff] [blame] | 587 | skcipher_inst->alg.setkey = essiv_skcipher_setkey; |
| 588 | skcipher_inst->alg.encrypt = essiv_skcipher_encrypt; |
| 589 | skcipher_inst->alg.decrypt = essiv_skcipher_decrypt; |
| 590 | skcipher_inst->alg.init = essiv_skcipher_init_tfm; |
| 591 | skcipher_inst->alg.exit = essiv_skcipher_exit_tfm; |
| 592 | |
| 593 | skcipher_inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(skcipher_alg); |
| 594 | skcipher_inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(skcipher_alg); |
| 595 | skcipher_inst->alg.ivsize = ivsize; |
| 596 | skcipher_inst->alg.chunksize = crypto_skcipher_alg_chunksize(skcipher_alg); |
| 597 | skcipher_inst->alg.walksize = crypto_skcipher_alg_walksize(skcipher_alg); |
| 598 | |
| 599 | skcipher_inst->free = essiv_skcipher_free_instance; |
| 600 | |
| 601 | err = skcipher_register_instance(tmpl, skcipher_inst); |
| 602 | } else { |
| 603 | aead_inst->alg.setkey = essiv_aead_setkey; |
| 604 | aead_inst->alg.setauthsize = essiv_aead_setauthsize; |
| 605 | aead_inst->alg.encrypt = essiv_aead_encrypt; |
| 606 | aead_inst->alg.decrypt = essiv_aead_decrypt; |
| 607 | aead_inst->alg.init = essiv_aead_init_tfm; |
| 608 | aead_inst->alg.exit = essiv_aead_exit_tfm; |
| 609 | |
| 610 | aead_inst->alg.ivsize = ivsize; |
| 611 | aead_inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(aead_alg); |
| 612 | aead_inst->alg.chunksize = crypto_aead_alg_chunksize(aead_alg); |
| 613 | |
| 614 | aead_inst->free = essiv_aead_free_instance; |
| 615 | |
| 616 | err = aead_register_instance(tmpl, aead_inst); |
| 617 | } |
| 618 | |
| 619 | if (err) |
| 620 | goto out_free_hash; |
| 621 | |
| 622 | crypto_mod_put(_hash_alg); |
| 623 | return 0; |
| 624 | |
| 625 | out_free_hash: |
| 626 | crypto_mod_put(_hash_alg); |
| 627 | out_drop_skcipher: |
Eric Biggers | c65058b | 2019-10-25 12:41:12 -0700 | [diff] [blame] | 628 | if (type == CRYPTO_ALG_TYPE_SKCIPHER) |
Ard Biesheuvel | be1eb7f | 2019-08-19 17:17:33 +0300 | [diff] [blame] | 629 | crypto_drop_skcipher(&ictx->u.skcipher_spawn); |
| 630 | else |
| 631 | crypto_drop_aead(&ictx->u.aead_spawn); |
| 632 | out_free_inst: |
| 633 | kfree(skcipher_inst); |
| 634 | kfree(aead_inst); |
| 635 | return err; |
| 636 | } |
| 637 | |
| 638 | /* essiv(cipher_name, shash_name) */ |
| 639 | static struct crypto_template essiv_tmpl = { |
| 640 | .name = "essiv", |
| 641 | .create = essiv_create, |
| 642 | .module = THIS_MODULE, |
| 643 | }; |
| 644 | |
| 645 | static int __init essiv_module_init(void) |
| 646 | { |
| 647 | return crypto_register_template(&essiv_tmpl); |
| 648 | } |
| 649 | |
| 650 | static void __exit essiv_module_exit(void) |
| 651 | { |
| 652 | crypto_unregister_template(&essiv_tmpl); |
| 653 | } |
| 654 | |
| 655 | subsys_initcall(essiv_module_init); |
| 656 | module_exit(essiv_module_exit); |
| 657 | |
| 658 | MODULE_DESCRIPTION("ESSIV skcipher/aead wrapper for block encryption"); |
| 659 | MODULE_LICENSE("GPL v2"); |
| 660 | MODULE_ALIAS_CRYPTO("essiv"); |