essiv.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  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. #include <crypto/authenc.h>
  31. #include <crypto/internal/aead.h>
  32. #include <crypto/internal/cipher.h>
  33. #include <crypto/internal/hash.h>
  34. #include <crypto/internal/skcipher.h>
  35. #include <crypto/scatterwalk.h>
  36. #include <linux/module.h>
  37. #include "internal.h"
  38. struct essiv_instance_ctx {
  39. union {
  40. struct crypto_skcipher_spawn skcipher_spawn;
  41. struct crypto_aead_spawn aead_spawn;
  42. } u;
  43. char essiv_cipher_name[CRYPTO_MAX_ALG_NAME];
  44. char shash_driver_name[CRYPTO_MAX_ALG_NAME];
  45. };
  46. struct essiv_tfm_ctx {
  47. union {
  48. struct crypto_skcipher *skcipher;
  49. struct crypto_aead *aead;
  50. } u;
  51. struct crypto_cipher *essiv_cipher;
  52. struct crypto_shash *hash;
  53. int ivoffset;
  54. };
  55. struct essiv_aead_request_ctx {
  56. struct scatterlist sg[4];
  57. u8 *assoc;
  58. struct aead_request aead_req;
  59. };
  60. static int essiv_skcipher_setkey(struct crypto_skcipher *tfm,
  61. const u8 *key, unsigned int keylen)
  62. {
  63. struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
  64. u8 salt[HASH_MAX_DIGESTSIZE];
  65. int err;
  66. crypto_skcipher_clear_flags(tctx->u.skcipher, CRYPTO_TFM_REQ_MASK);
  67. crypto_skcipher_set_flags(tctx->u.skcipher,
  68. crypto_skcipher_get_flags(tfm) &
  69. CRYPTO_TFM_REQ_MASK);
  70. err = crypto_skcipher_setkey(tctx->u.skcipher, key, keylen);
  71. if (err)
  72. return err;
  73. err = crypto_shash_tfm_digest(tctx->hash, key, keylen, salt);
  74. if (err)
  75. return err;
  76. crypto_cipher_clear_flags(tctx->essiv_cipher, CRYPTO_TFM_REQ_MASK);
  77. crypto_cipher_set_flags(tctx->essiv_cipher,
  78. crypto_skcipher_get_flags(tfm) &
  79. CRYPTO_TFM_REQ_MASK);
  80. return crypto_cipher_setkey(tctx->essiv_cipher, salt,
  81. crypto_shash_digestsize(tctx->hash));
  82. }
  83. static int essiv_aead_setkey(struct crypto_aead *tfm, const u8 *key,
  84. unsigned int keylen)
  85. {
  86. struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
  87. SHASH_DESC_ON_STACK(desc, tctx->hash);
  88. struct crypto_authenc_keys keys;
  89. u8 salt[HASH_MAX_DIGESTSIZE];
  90. int err;
  91. crypto_aead_clear_flags(tctx->u.aead, CRYPTO_TFM_REQ_MASK);
  92. crypto_aead_set_flags(tctx->u.aead, crypto_aead_get_flags(tfm) &
  93. CRYPTO_TFM_REQ_MASK);
  94. err = crypto_aead_setkey(tctx->u.aead, key, keylen);
  95. if (err)
  96. return err;
  97. if (crypto_authenc_extractkeys(&keys, key, keylen) != 0)
  98. return -EINVAL;
  99. desc->tfm = tctx->hash;
  100. err = crypto_shash_init(desc) ?:
  101. crypto_shash_update(desc, keys.enckey, keys.enckeylen) ?:
  102. crypto_shash_finup(desc, keys.authkey, keys.authkeylen, salt);
  103. if (err)
  104. return err;
  105. crypto_cipher_clear_flags(tctx->essiv_cipher, CRYPTO_TFM_REQ_MASK);
  106. crypto_cipher_set_flags(tctx->essiv_cipher, crypto_aead_get_flags(tfm) &
  107. CRYPTO_TFM_REQ_MASK);
  108. return crypto_cipher_setkey(tctx->essiv_cipher, salt,
  109. crypto_shash_digestsize(tctx->hash));
  110. }
  111. static int essiv_aead_setauthsize(struct crypto_aead *tfm,
  112. unsigned int authsize)
  113. {
  114. struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
  115. return crypto_aead_setauthsize(tctx->u.aead, authsize);
  116. }
  117. static void essiv_skcipher_done(void *data, int err)
  118. {
  119. struct skcipher_request *req = data;
  120. skcipher_request_complete(req, err);
  121. }
  122. static int essiv_skcipher_crypt(struct skcipher_request *req, bool enc)
  123. {
  124. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  125. const struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
  126. struct skcipher_request *subreq = skcipher_request_ctx(req);
  127. crypto_cipher_encrypt_one(tctx->essiv_cipher, req->iv, req->iv);
  128. skcipher_request_set_tfm(subreq, tctx->u.skcipher);
  129. skcipher_request_set_crypt(subreq, req->src, req->dst, req->cryptlen,
  130. req->iv);
  131. skcipher_request_set_callback(subreq, skcipher_request_flags(req),
  132. essiv_skcipher_done, req);
  133. return enc ? crypto_skcipher_encrypt(subreq) :
  134. crypto_skcipher_decrypt(subreq);
  135. }
  136. static int essiv_skcipher_encrypt(struct skcipher_request *req)
  137. {
  138. return essiv_skcipher_crypt(req, true);
  139. }
  140. static int essiv_skcipher_decrypt(struct skcipher_request *req)
  141. {
  142. return essiv_skcipher_crypt(req, false);
  143. }
  144. static void essiv_aead_done(void *data, int err)
  145. {
  146. struct aead_request *req = data;
  147. struct essiv_aead_request_ctx *rctx = aead_request_ctx(req);
  148. if (err == -EINPROGRESS)
  149. goto out;
  150. kfree(rctx->assoc);
  151. out:
  152. aead_request_complete(req, err);
  153. }
  154. static int essiv_aead_crypt(struct aead_request *req, bool enc)
  155. {
  156. struct crypto_aead *tfm = crypto_aead_reqtfm(req);
  157. const struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
  158. struct essiv_aead_request_ctx *rctx = aead_request_ctx(req);
  159. struct aead_request *subreq = &rctx->aead_req;
  160. int ivsize = crypto_aead_ivsize(tfm);
  161. int ssize = req->assoclen - ivsize;
  162. struct scatterlist *src = req->src;
  163. int err;
  164. if (ssize < 0)
  165. return -EINVAL;
  166. crypto_cipher_encrypt_one(tctx->essiv_cipher, req->iv, req->iv);
  167. /*
  168. * dm-crypt embeds the sector number and the IV in the AAD region, so
  169. * we have to copy the converted IV into the right scatterlist before
  170. * we pass it on.
  171. */
  172. rctx->assoc = NULL;
  173. if (req->src == req->dst || !enc) {
  174. scatterwalk_map_and_copy(req->iv, req->dst, ssize, ivsize, 1);
  175. } else {
  176. u8 *iv = (u8 *)aead_request_ctx(req) + tctx->ivoffset;
  177. struct scatterlist *sg;
  178. int nents;
  179. nents = sg_nents_for_len(req->src, ssize);
  180. if (nents < 0)
  181. return -EINVAL;
  182. memcpy(iv, req->iv, ivsize);
  183. sg_init_table(rctx->sg, 4);
  184. if (unlikely(nents > 1)) {
  185. /*
  186. * This is a case that rarely occurs in practice, but
  187. * for correctness, we have to deal with it nonetheless.
  188. */
  189. rctx->assoc = kmalloc(ssize, GFP_ATOMIC);
  190. if (!rctx->assoc)
  191. return -ENOMEM;
  192. scatterwalk_map_and_copy(rctx->assoc, req->src, 0,
  193. ssize, 0);
  194. sg_set_buf(rctx->sg, rctx->assoc, ssize);
  195. } else {
  196. sg_set_page(rctx->sg, sg_page(req->src), ssize,
  197. req->src->offset);
  198. }
  199. sg_set_buf(rctx->sg + 1, iv, ivsize);
  200. sg = scatterwalk_ffwd(rctx->sg + 2, req->src, req->assoclen);
  201. if (sg != rctx->sg + 2)
  202. sg_chain(rctx->sg, 3, sg);
  203. src = rctx->sg;
  204. }
  205. aead_request_set_tfm(subreq, tctx->u.aead);
  206. aead_request_set_ad(subreq, req->assoclen);
  207. aead_request_set_callback(subreq, aead_request_flags(req),
  208. essiv_aead_done, req);
  209. aead_request_set_crypt(subreq, src, req->dst, req->cryptlen, req->iv);
  210. err = enc ? crypto_aead_encrypt(subreq) :
  211. crypto_aead_decrypt(subreq);
  212. if (rctx->assoc && err != -EINPROGRESS && err != -EBUSY)
  213. kfree(rctx->assoc);
  214. return err;
  215. }
  216. static int essiv_aead_encrypt(struct aead_request *req)
  217. {
  218. return essiv_aead_crypt(req, true);
  219. }
  220. static int essiv_aead_decrypt(struct aead_request *req)
  221. {
  222. return essiv_aead_crypt(req, false);
  223. }
  224. static int essiv_init_tfm(struct essiv_instance_ctx *ictx,
  225. struct essiv_tfm_ctx *tctx)
  226. {
  227. struct crypto_cipher *essiv_cipher;
  228. struct crypto_shash *hash;
  229. int err;
  230. essiv_cipher = crypto_alloc_cipher(ictx->essiv_cipher_name, 0, 0);
  231. if (IS_ERR(essiv_cipher))
  232. return PTR_ERR(essiv_cipher);
  233. hash = crypto_alloc_shash(ictx->shash_driver_name, 0, 0);
  234. if (IS_ERR(hash)) {
  235. err = PTR_ERR(hash);
  236. goto err_free_essiv_cipher;
  237. }
  238. tctx->essiv_cipher = essiv_cipher;
  239. tctx->hash = hash;
  240. return 0;
  241. err_free_essiv_cipher:
  242. crypto_free_cipher(essiv_cipher);
  243. return err;
  244. }
  245. static int essiv_skcipher_init_tfm(struct crypto_skcipher *tfm)
  246. {
  247. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  248. struct essiv_instance_ctx *ictx = skcipher_instance_ctx(inst);
  249. struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
  250. struct crypto_skcipher *skcipher;
  251. int err;
  252. skcipher = crypto_spawn_skcipher(&ictx->u.skcipher_spawn);
  253. if (IS_ERR(skcipher))
  254. return PTR_ERR(skcipher);
  255. crypto_skcipher_set_reqsize(tfm, sizeof(struct skcipher_request) +
  256. crypto_skcipher_reqsize(skcipher));
  257. err = essiv_init_tfm(ictx, tctx);
  258. if (err) {
  259. crypto_free_skcipher(skcipher);
  260. return err;
  261. }
  262. tctx->u.skcipher = skcipher;
  263. return 0;
  264. }
  265. static int essiv_aead_init_tfm(struct crypto_aead *tfm)
  266. {
  267. struct aead_instance *inst = aead_alg_instance(tfm);
  268. struct essiv_instance_ctx *ictx = aead_instance_ctx(inst);
  269. struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
  270. struct crypto_aead *aead;
  271. unsigned int subreq_size;
  272. int err;
  273. BUILD_BUG_ON(offsetofend(struct essiv_aead_request_ctx, aead_req) !=
  274. sizeof(struct essiv_aead_request_ctx));
  275. aead = crypto_spawn_aead(&ictx->u.aead_spawn);
  276. if (IS_ERR(aead))
  277. return PTR_ERR(aead);
  278. subreq_size = sizeof_field(struct essiv_aead_request_ctx, aead_req) +
  279. crypto_aead_reqsize(aead);
  280. tctx->ivoffset = offsetof(struct essiv_aead_request_ctx, aead_req) +
  281. subreq_size;
  282. crypto_aead_set_reqsize(tfm, tctx->ivoffset + crypto_aead_ivsize(aead));
  283. err = essiv_init_tfm(ictx, tctx);
  284. if (err) {
  285. crypto_free_aead(aead);
  286. return err;
  287. }
  288. tctx->u.aead = aead;
  289. return 0;
  290. }
  291. static void essiv_skcipher_exit_tfm(struct crypto_skcipher *tfm)
  292. {
  293. struct essiv_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
  294. crypto_free_skcipher(tctx->u.skcipher);
  295. crypto_free_cipher(tctx->essiv_cipher);
  296. crypto_free_shash(tctx->hash);
  297. }
  298. static void essiv_aead_exit_tfm(struct crypto_aead *tfm)
  299. {
  300. struct essiv_tfm_ctx *tctx = crypto_aead_ctx(tfm);
  301. crypto_free_aead(tctx->u.aead);
  302. crypto_free_cipher(tctx->essiv_cipher);
  303. crypto_free_shash(tctx->hash);
  304. }
  305. static void essiv_skcipher_free_instance(struct skcipher_instance *inst)
  306. {
  307. struct essiv_instance_ctx *ictx = skcipher_instance_ctx(inst);
  308. crypto_drop_skcipher(&ictx->u.skcipher_spawn);
  309. kfree(inst);
  310. }
  311. static void essiv_aead_free_instance(struct aead_instance *inst)
  312. {
  313. struct essiv_instance_ctx *ictx = aead_instance_ctx(inst);
  314. crypto_drop_aead(&ictx->u.aead_spawn);
  315. kfree(inst);
  316. }
  317. static bool parse_cipher_name(char *essiv_cipher_name, const char *cra_name)
  318. {
  319. const char *p, *q;
  320. int len;
  321. /* find the last opening parens */
  322. p = strrchr(cra_name, '(');
  323. if (!p++)
  324. return false;
  325. /* find the first closing parens in the tail of the string */
  326. q = strchr(p, ')');
  327. if (!q)
  328. return false;
  329. len = q - p;
  330. if (len >= CRYPTO_MAX_ALG_NAME)
  331. return false;
  332. memcpy(essiv_cipher_name, p, len);
  333. essiv_cipher_name[len] = '\0';
  334. return true;
  335. }
  336. static bool essiv_supported_algorithms(const char *essiv_cipher_name,
  337. struct shash_alg *hash_alg,
  338. int ivsize)
  339. {
  340. struct crypto_alg *alg;
  341. bool ret = false;
  342. alg = crypto_alg_mod_lookup(essiv_cipher_name,
  343. CRYPTO_ALG_TYPE_CIPHER,
  344. CRYPTO_ALG_TYPE_MASK);
  345. if (IS_ERR(alg))
  346. return false;
  347. if (hash_alg->digestsize < alg->cra_cipher.cia_min_keysize ||
  348. hash_alg->digestsize > alg->cra_cipher.cia_max_keysize)
  349. goto out;
  350. if (ivsize != alg->cra_blocksize)
  351. goto out;
  352. if (crypto_shash_alg_needs_key(hash_alg))
  353. goto out;
  354. ret = true;
  355. out:
  356. crypto_mod_put(alg);
  357. return ret;
  358. }
  359. static int essiv_create(struct crypto_template *tmpl, struct rtattr **tb)
  360. {
  361. struct skcipher_alg_common *skcipher_alg = NULL;
  362. struct crypto_attr_type *algt;
  363. const char *inner_cipher_name;
  364. const char *shash_name;
  365. struct skcipher_instance *skcipher_inst = NULL;
  366. struct aead_instance *aead_inst = NULL;
  367. struct crypto_instance *inst;
  368. struct crypto_alg *base, *block_base;
  369. struct essiv_instance_ctx *ictx;
  370. struct aead_alg *aead_alg = NULL;
  371. struct crypto_alg *_hash_alg;
  372. struct shash_alg *hash_alg;
  373. int ivsize;
  374. u32 type;
  375. u32 mask;
  376. int err;
  377. algt = crypto_get_attr_type(tb);
  378. if (IS_ERR(algt))
  379. return PTR_ERR(algt);
  380. inner_cipher_name = crypto_attr_alg_name(tb[1]);
  381. if (IS_ERR(inner_cipher_name))
  382. return PTR_ERR(inner_cipher_name);
  383. shash_name = crypto_attr_alg_name(tb[2]);
  384. if (IS_ERR(shash_name))
  385. return PTR_ERR(shash_name);
  386. type = algt->type & algt->mask;
  387. mask = crypto_algt_inherited_mask(algt);
  388. switch (type) {
  389. case CRYPTO_ALG_TYPE_LSKCIPHER:
  390. skcipher_inst = kzalloc(sizeof(*skcipher_inst) +
  391. sizeof(*ictx), GFP_KERNEL);
  392. if (!skcipher_inst)
  393. return -ENOMEM;
  394. inst = skcipher_crypto_instance(skcipher_inst);
  395. base = &skcipher_inst->alg.base;
  396. ictx = crypto_instance_ctx(inst);
  397. /* Symmetric cipher, e.g., "cbc(aes)" */
  398. err = crypto_grab_skcipher(&ictx->u.skcipher_spawn, inst,
  399. inner_cipher_name, 0, mask);
  400. if (err)
  401. goto out_free_inst;
  402. skcipher_alg = crypto_spawn_skcipher_alg_common(
  403. &ictx->u.skcipher_spawn);
  404. block_base = &skcipher_alg->base;
  405. ivsize = skcipher_alg->ivsize;
  406. break;
  407. case CRYPTO_ALG_TYPE_AEAD:
  408. aead_inst = kzalloc(sizeof(*aead_inst) +
  409. sizeof(*ictx), GFP_KERNEL);
  410. if (!aead_inst)
  411. return -ENOMEM;
  412. inst = aead_crypto_instance(aead_inst);
  413. base = &aead_inst->alg.base;
  414. ictx = crypto_instance_ctx(inst);
  415. /* AEAD cipher, e.g., "authenc(hmac(sha256),cbc(aes))" */
  416. err = crypto_grab_aead(&ictx->u.aead_spawn, inst,
  417. inner_cipher_name, 0, mask);
  418. if (err)
  419. goto out_free_inst;
  420. aead_alg = crypto_spawn_aead_alg(&ictx->u.aead_spawn);
  421. block_base = &aead_alg->base;
  422. if (!strstarts(block_base->cra_name, "authenc(")) {
  423. pr_warn("Only authenc() type AEADs are supported by ESSIV\n");
  424. err = -EINVAL;
  425. goto out_drop_skcipher;
  426. }
  427. ivsize = aead_alg->ivsize;
  428. break;
  429. default:
  430. return -EINVAL;
  431. }
  432. if (!parse_cipher_name(ictx->essiv_cipher_name, block_base->cra_name)) {
  433. pr_warn("Failed to parse ESSIV cipher name from skcipher cra_name\n");
  434. err = -EINVAL;
  435. goto out_drop_skcipher;
  436. }
  437. /* Synchronous hash, e.g., "sha256" */
  438. _hash_alg = crypto_alg_mod_lookup(shash_name,
  439. CRYPTO_ALG_TYPE_SHASH,
  440. CRYPTO_ALG_TYPE_MASK | mask);
  441. if (IS_ERR(_hash_alg)) {
  442. err = PTR_ERR(_hash_alg);
  443. goto out_drop_skcipher;
  444. }
  445. hash_alg = __crypto_shash_alg(_hash_alg);
  446. /* Check the set of algorithms */
  447. if (!essiv_supported_algorithms(ictx->essiv_cipher_name, hash_alg,
  448. ivsize)) {
  449. pr_warn("Unsupported essiv instantiation: essiv(%s,%s)\n",
  450. block_base->cra_name, hash_alg->base.cra_name);
  451. err = -EINVAL;
  452. goto out_free_hash;
  453. }
  454. /* record the driver name so we can instantiate this exact algo later */
  455. strscpy(ictx->shash_driver_name, hash_alg->base.cra_driver_name,
  456. CRYPTO_MAX_ALG_NAME);
  457. /* Instance fields */
  458. err = -ENAMETOOLONG;
  459. if (snprintf(base->cra_name, CRYPTO_MAX_ALG_NAME,
  460. "essiv(%s,%s)", block_base->cra_name,
  461. hash_alg->base.cra_name) >= CRYPTO_MAX_ALG_NAME)
  462. goto out_free_hash;
  463. if (snprintf(base->cra_driver_name, CRYPTO_MAX_ALG_NAME,
  464. "essiv(%s,%s)", block_base->cra_driver_name,
  465. hash_alg->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  466. goto out_free_hash;
  467. /*
  468. * hash_alg wasn't gotten via crypto_grab*(), so we need to inherit its
  469. * flags manually.
  470. */
  471. base->cra_flags |= (hash_alg->base.cra_flags &
  472. CRYPTO_ALG_INHERITED_FLAGS);
  473. base->cra_blocksize = block_base->cra_blocksize;
  474. base->cra_ctxsize = sizeof(struct essiv_tfm_ctx);
  475. base->cra_alignmask = block_base->cra_alignmask;
  476. base->cra_priority = block_base->cra_priority;
  477. if (type == CRYPTO_ALG_TYPE_LSKCIPHER) {
  478. skcipher_inst->alg.setkey = essiv_skcipher_setkey;
  479. skcipher_inst->alg.encrypt = essiv_skcipher_encrypt;
  480. skcipher_inst->alg.decrypt = essiv_skcipher_decrypt;
  481. skcipher_inst->alg.init = essiv_skcipher_init_tfm;
  482. skcipher_inst->alg.exit = essiv_skcipher_exit_tfm;
  483. skcipher_inst->alg.min_keysize = skcipher_alg->min_keysize;
  484. skcipher_inst->alg.max_keysize = skcipher_alg->max_keysize;
  485. skcipher_inst->alg.ivsize = ivsize;
  486. skcipher_inst->alg.chunksize = skcipher_alg->chunksize;
  487. skcipher_inst->free = essiv_skcipher_free_instance;
  488. err = skcipher_register_instance(tmpl, skcipher_inst);
  489. } else {
  490. aead_inst->alg.setkey = essiv_aead_setkey;
  491. aead_inst->alg.setauthsize = essiv_aead_setauthsize;
  492. aead_inst->alg.encrypt = essiv_aead_encrypt;
  493. aead_inst->alg.decrypt = essiv_aead_decrypt;
  494. aead_inst->alg.init = essiv_aead_init_tfm;
  495. aead_inst->alg.exit = essiv_aead_exit_tfm;
  496. aead_inst->alg.ivsize = ivsize;
  497. aead_inst->alg.maxauthsize = crypto_aead_alg_maxauthsize(aead_alg);
  498. aead_inst->alg.chunksize = crypto_aead_alg_chunksize(aead_alg);
  499. aead_inst->free = essiv_aead_free_instance;
  500. err = aead_register_instance(tmpl, aead_inst);
  501. }
  502. if (err)
  503. goto out_free_hash;
  504. crypto_mod_put(_hash_alg);
  505. return 0;
  506. out_free_hash:
  507. crypto_mod_put(_hash_alg);
  508. out_drop_skcipher:
  509. if (type == CRYPTO_ALG_TYPE_LSKCIPHER)
  510. crypto_drop_skcipher(&ictx->u.skcipher_spawn);
  511. else
  512. crypto_drop_aead(&ictx->u.aead_spawn);
  513. out_free_inst:
  514. kfree(skcipher_inst);
  515. kfree(aead_inst);
  516. return err;
  517. }
  518. /* essiv(cipher_name, shash_name) */
  519. static struct crypto_template essiv_tmpl = {
  520. .name = "essiv",
  521. .create = essiv_create,
  522. .module = THIS_MODULE,
  523. };
  524. static int __init essiv_module_init(void)
  525. {
  526. return crypto_register_template(&essiv_tmpl);
  527. }
  528. static void __exit essiv_module_exit(void)
  529. {
  530. crypto_unregister_template(&essiv_tmpl);
  531. }
  532. subsys_initcall(essiv_module_init);
  533. module_exit(essiv_module_exit);
  534. MODULE_DESCRIPTION("ESSIV skcipher/aead wrapper for block encryption");
  535. MODULE_LICENSE("GPL v2");
  536. MODULE_ALIAS_CRYPTO("essiv");
  537. MODULE_IMPORT_NS(CRYPTO_INTERNAL);