essiv.c 18 KB

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