Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

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