essiv.c 18 KB

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