hctr2.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * HCTR2 length-preserving encryption mode
  4. *
  5. * Copyright 2021 Google LLC
  6. */
  7. /*
  8. * HCTR2 is a length-preserving encryption mode that is efficient on
  9. * processors with instructions to accelerate AES and carryless
  10. * multiplication, e.g. x86 processors with AES-NI and CLMUL, and ARM
  11. * processors with the ARMv8 crypto extensions.
  12. *
  13. * For more details, see the paper: "Length-preserving encryption with HCTR2"
  14. * (https://eprint.iacr.org/2021/1441.pdf)
  15. */
  16. #include <crypto/internal/cipher.h>
  17. #include <crypto/internal/skcipher.h>
  18. #include <crypto/polyval.h>
  19. #include <crypto/scatterwalk.h>
  20. #include <linux/module.h>
  21. #define BLOCKCIPHER_BLOCK_SIZE 16
  22. /*
  23. * The specification allows variable-length tweaks, but Linux's crypto API
  24. * currently only allows algorithms to support a single length. The "natural"
  25. * tweak length for HCTR2 is 16, since that fits into one POLYVAL block for
  26. * the best performance. But longer tweaks are useful for fscrypt, to avoid
  27. * needing to derive per-file keys. So instead we use two blocks, or 32 bytes.
  28. */
  29. #define TWEAK_SIZE 32
  30. struct hctr2_instance_ctx {
  31. struct crypto_cipher_spawn blockcipher_spawn;
  32. struct crypto_skcipher_spawn xctr_spawn;
  33. };
  34. struct hctr2_tfm_ctx {
  35. struct crypto_cipher *blockcipher;
  36. struct crypto_skcipher *xctr;
  37. struct polyval_key poly_key;
  38. struct polyval_elem hashed_tweaklens[2];
  39. u8 L[BLOCKCIPHER_BLOCK_SIZE];
  40. };
  41. struct hctr2_request_ctx {
  42. u8 first_block[BLOCKCIPHER_BLOCK_SIZE];
  43. u8 xctr_iv[BLOCKCIPHER_BLOCK_SIZE];
  44. struct scatterlist *bulk_part_dst;
  45. struct scatterlist *bulk_part_src;
  46. struct scatterlist sg_src[2];
  47. struct scatterlist sg_dst[2];
  48. struct polyval_elem hashed_tweak;
  49. /*
  50. * skcipher sub-request size is unknown at compile-time, so it needs to
  51. * go after the members with known sizes.
  52. */
  53. union {
  54. struct polyval_ctx poly_ctx;
  55. struct skcipher_request xctr_req;
  56. } u;
  57. };
  58. /*
  59. * The input data for each HCTR2 hash step begins with a 16-byte block that
  60. * contains the tweak length and a flag that indicates whether the input is evenly
  61. * divisible into blocks. Since this implementation only supports one tweak
  62. * length, we precompute the two hash states resulting from hashing the two
  63. * possible values of this initial block. This reduces by one block the amount of
  64. * data that needs to be hashed for each encryption/decryption
  65. *
  66. * These precomputed hashes are stored in hctr2_tfm_ctx.
  67. */
  68. static void hctr2_hash_tweaklens(struct hctr2_tfm_ctx *tctx)
  69. {
  70. struct polyval_ctx ctx;
  71. for (int has_remainder = 0; has_remainder < 2; has_remainder++) {
  72. const __le64 tweak_length_block[2] = {
  73. cpu_to_le64(TWEAK_SIZE * 8 * 2 + 2 + has_remainder),
  74. };
  75. polyval_init(&ctx, &tctx->poly_key);
  76. polyval_update(&ctx, (const u8 *)&tweak_length_block,
  77. sizeof(tweak_length_block));
  78. static_assert(sizeof(tweak_length_block) == POLYVAL_BLOCK_SIZE);
  79. polyval_export_blkaligned(
  80. &ctx, &tctx->hashed_tweaklens[has_remainder]);
  81. }
  82. memzero_explicit(&ctx, sizeof(ctx));
  83. }
  84. static int hctr2_setkey(struct crypto_skcipher *tfm, const u8 *key,
  85. unsigned int keylen)
  86. {
  87. struct hctr2_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
  88. u8 hbar[BLOCKCIPHER_BLOCK_SIZE];
  89. int err;
  90. crypto_cipher_clear_flags(tctx->blockcipher, CRYPTO_TFM_REQ_MASK);
  91. crypto_cipher_set_flags(tctx->blockcipher,
  92. crypto_skcipher_get_flags(tfm) &
  93. CRYPTO_TFM_REQ_MASK);
  94. err = crypto_cipher_setkey(tctx->blockcipher, key, keylen);
  95. if (err)
  96. return err;
  97. crypto_skcipher_clear_flags(tctx->xctr, CRYPTO_TFM_REQ_MASK);
  98. crypto_skcipher_set_flags(tctx->xctr,
  99. crypto_skcipher_get_flags(tfm) &
  100. CRYPTO_TFM_REQ_MASK);
  101. err = crypto_skcipher_setkey(tctx->xctr, key, keylen);
  102. if (err)
  103. return err;
  104. memset(hbar, 0, sizeof(hbar));
  105. crypto_cipher_encrypt_one(tctx->blockcipher, hbar, hbar);
  106. memset(tctx->L, 0, sizeof(tctx->L));
  107. tctx->L[0] = 0x01;
  108. crypto_cipher_encrypt_one(tctx->blockcipher, tctx->L, tctx->L);
  109. static_assert(sizeof(hbar) == POLYVAL_BLOCK_SIZE);
  110. polyval_preparekey(&tctx->poly_key, hbar);
  111. memzero_explicit(hbar, sizeof(hbar));
  112. hctr2_hash_tweaklens(tctx);
  113. return 0;
  114. }
  115. static void hctr2_hash_tweak(struct skcipher_request *req)
  116. {
  117. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  118. const struct hctr2_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
  119. struct hctr2_request_ctx *rctx = skcipher_request_ctx(req);
  120. struct polyval_ctx *poly_ctx = &rctx->u.poly_ctx;
  121. bool has_remainder = req->cryptlen % POLYVAL_BLOCK_SIZE;
  122. polyval_import_blkaligned(poly_ctx, &tctx->poly_key,
  123. &tctx->hashed_tweaklens[has_remainder]);
  124. polyval_update(poly_ctx, req->iv, TWEAK_SIZE);
  125. // Store the hashed tweak, since we need it when computing both
  126. // H(T || N) and H(T || V).
  127. static_assert(TWEAK_SIZE % POLYVAL_BLOCK_SIZE == 0);
  128. polyval_export_blkaligned(poly_ctx, &rctx->hashed_tweak);
  129. }
  130. static void hctr2_hash_message(struct skcipher_request *req,
  131. struct scatterlist *sgl,
  132. u8 digest[POLYVAL_DIGEST_SIZE])
  133. {
  134. static const u8 padding = 0x1;
  135. struct hctr2_request_ctx *rctx = skcipher_request_ctx(req);
  136. struct polyval_ctx *poly_ctx = &rctx->u.poly_ctx;
  137. const unsigned int bulk_len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
  138. struct sg_mapping_iter miter;
  139. int i;
  140. int n = 0;
  141. sg_miter_start(&miter, sgl, sg_nents(sgl),
  142. SG_MITER_FROM_SG | SG_MITER_ATOMIC);
  143. for (i = 0; i < bulk_len; i += n) {
  144. sg_miter_next(&miter);
  145. n = min_t(unsigned int, miter.length, bulk_len - i);
  146. polyval_update(poly_ctx, miter.addr, n);
  147. }
  148. sg_miter_stop(&miter);
  149. if (req->cryptlen % BLOCKCIPHER_BLOCK_SIZE)
  150. polyval_update(poly_ctx, &padding, 1);
  151. polyval_final(poly_ctx, digest);
  152. }
  153. static int hctr2_finish(struct skcipher_request *req)
  154. {
  155. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  156. const struct hctr2_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
  157. struct hctr2_request_ctx *rctx = skcipher_request_ctx(req);
  158. struct polyval_ctx *poly_ctx = &rctx->u.poly_ctx;
  159. u8 digest[POLYVAL_DIGEST_SIZE];
  160. // U = UU ^ H(T || V)
  161. // or M = MM ^ H(T || N)
  162. polyval_import_blkaligned(poly_ctx, &tctx->poly_key,
  163. &rctx->hashed_tweak);
  164. hctr2_hash_message(req, rctx->bulk_part_dst, digest);
  165. crypto_xor(rctx->first_block, digest, BLOCKCIPHER_BLOCK_SIZE);
  166. // Copy U (or M) into dst scatterlist
  167. scatterwalk_map_and_copy(rctx->first_block, req->dst,
  168. 0, BLOCKCIPHER_BLOCK_SIZE, 1);
  169. return 0;
  170. }
  171. static void hctr2_xctr_done(void *data, int err)
  172. {
  173. struct skcipher_request *req = data;
  174. if (!err)
  175. err = hctr2_finish(req);
  176. skcipher_request_complete(req, err);
  177. }
  178. static int hctr2_crypt(struct skcipher_request *req, bool enc)
  179. {
  180. struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
  181. const struct hctr2_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
  182. struct hctr2_request_ctx *rctx = skcipher_request_ctx(req);
  183. u8 digest[POLYVAL_DIGEST_SIZE];
  184. int bulk_len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
  185. // Requests must be at least one block
  186. if (req->cryptlen < BLOCKCIPHER_BLOCK_SIZE)
  187. return -EINVAL;
  188. // Copy M (or U) into a temporary buffer
  189. scatterwalk_map_and_copy(rctx->first_block, req->src,
  190. 0, BLOCKCIPHER_BLOCK_SIZE, 0);
  191. // Create scatterlists for N and V
  192. rctx->bulk_part_src = scatterwalk_ffwd(rctx->sg_src, req->src,
  193. BLOCKCIPHER_BLOCK_SIZE);
  194. rctx->bulk_part_dst = scatterwalk_ffwd(rctx->sg_dst, req->dst,
  195. BLOCKCIPHER_BLOCK_SIZE);
  196. // MM = M ^ H(T || N)
  197. // or UU = U ^ H(T || V)
  198. hctr2_hash_tweak(req);
  199. hctr2_hash_message(req, rctx->bulk_part_src, digest);
  200. crypto_xor(digest, rctx->first_block, BLOCKCIPHER_BLOCK_SIZE);
  201. // UU = E(MM)
  202. // or MM = D(UU)
  203. if (enc)
  204. crypto_cipher_encrypt_one(tctx->blockcipher, rctx->first_block,
  205. digest);
  206. else
  207. crypto_cipher_decrypt_one(tctx->blockcipher, rctx->first_block,
  208. digest);
  209. // S = MM ^ UU ^ L
  210. crypto_xor(digest, rctx->first_block, BLOCKCIPHER_BLOCK_SIZE);
  211. crypto_xor_cpy(rctx->xctr_iv, digest, tctx->L, BLOCKCIPHER_BLOCK_SIZE);
  212. // V = XCTR(S, N)
  213. // or N = XCTR(S, V)
  214. skcipher_request_set_tfm(&rctx->u.xctr_req, tctx->xctr);
  215. skcipher_request_set_crypt(&rctx->u.xctr_req, rctx->bulk_part_src,
  216. rctx->bulk_part_dst, bulk_len,
  217. rctx->xctr_iv);
  218. skcipher_request_set_callback(&rctx->u.xctr_req,
  219. req->base.flags,
  220. hctr2_xctr_done, req);
  221. return crypto_skcipher_encrypt(&rctx->u.xctr_req) ?:
  222. hctr2_finish(req);
  223. }
  224. static int hctr2_encrypt(struct skcipher_request *req)
  225. {
  226. return hctr2_crypt(req, true);
  227. }
  228. static int hctr2_decrypt(struct skcipher_request *req)
  229. {
  230. return hctr2_crypt(req, false);
  231. }
  232. static int hctr2_init_tfm(struct crypto_skcipher *tfm)
  233. {
  234. struct skcipher_instance *inst = skcipher_alg_instance(tfm);
  235. struct hctr2_instance_ctx *ictx = skcipher_instance_ctx(inst);
  236. struct hctr2_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
  237. struct crypto_skcipher *xctr;
  238. struct crypto_cipher *blockcipher;
  239. int err;
  240. xctr = crypto_spawn_skcipher(&ictx->xctr_spawn);
  241. if (IS_ERR(xctr))
  242. return PTR_ERR(xctr);
  243. blockcipher = crypto_spawn_cipher(&ictx->blockcipher_spawn);
  244. if (IS_ERR(blockcipher)) {
  245. err = PTR_ERR(blockcipher);
  246. goto err_free_xctr;
  247. }
  248. tctx->xctr = xctr;
  249. tctx->blockcipher = blockcipher;
  250. BUILD_BUG_ON(offsetofend(struct hctr2_request_ctx, u) !=
  251. sizeof(struct hctr2_request_ctx));
  252. crypto_skcipher_set_reqsize(
  253. tfm, max(sizeof(struct hctr2_request_ctx),
  254. offsetofend(struct hctr2_request_ctx, u.xctr_req) +
  255. crypto_skcipher_reqsize(xctr)));
  256. return 0;
  257. err_free_xctr:
  258. crypto_free_skcipher(xctr);
  259. return err;
  260. }
  261. static void hctr2_exit_tfm(struct crypto_skcipher *tfm)
  262. {
  263. struct hctr2_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
  264. crypto_free_cipher(tctx->blockcipher);
  265. crypto_free_skcipher(tctx->xctr);
  266. }
  267. static void hctr2_free_instance(struct skcipher_instance *inst)
  268. {
  269. struct hctr2_instance_ctx *ictx = skcipher_instance_ctx(inst);
  270. crypto_drop_cipher(&ictx->blockcipher_spawn);
  271. crypto_drop_skcipher(&ictx->xctr_spawn);
  272. kfree(inst);
  273. }
  274. static int hctr2_create_common(struct crypto_template *tmpl, struct rtattr **tb,
  275. const char *xctr_name)
  276. {
  277. struct skcipher_alg_common *xctr_alg;
  278. u32 mask;
  279. struct skcipher_instance *inst;
  280. struct hctr2_instance_ctx *ictx;
  281. struct crypto_alg *blockcipher_alg;
  282. char blockcipher_name[CRYPTO_MAX_ALG_NAME];
  283. int len;
  284. int err;
  285. err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
  286. if (err)
  287. return err;
  288. inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
  289. if (!inst)
  290. return -ENOMEM;
  291. ictx = skcipher_instance_ctx(inst);
  292. /* Stream cipher, xctr(block_cipher) */
  293. err = crypto_grab_skcipher(&ictx->xctr_spawn,
  294. skcipher_crypto_instance(inst),
  295. xctr_name, 0, mask);
  296. if (err)
  297. goto err_free_inst;
  298. xctr_alg = crypto_spawn_skcipher_alg_common(&ictx->xctr_spawn);
  299. err = -EINVAL;
  300. if (strncmp(xctr_alg->base.cra_name, "xctr(", 5))
  301. goto err_free_inst;
  302. len = strscpy(blockcipher_name, xctr_alg->base.cra_name + 5,
  303. sizeof(blockcipher_name));
  304. if (len < 1)
  305. goto err_free_inst;
  306. if (blockcipher_name[len - 1] != ')')
  307. goto err_free_inst;
  308. blockcipher_name[len - 1] = 0;
  309. /* Block cipher, e.g. "aes" */
  310. err = crypto_grab_cipher(&ictx->blockcipher_spawn,
  311. skcipher_crypto_instance(inst),
  312. blockcipher_name, 0, mask);
  313. if (err)
  314. goto err_free_inst;
  315. blockcipher_alg = crypto_spawn_cipher_alg(&ictx->blockcipher_spawn);
  316. /* Require blocksize of 16 bytes */
  317. err = -EINVAL;
  318. if (blockcipher_alg->cra_blocksize != BLOCKCIPHER_BLOCK_SIZE)
  319. goto err_free_inst;
  320. /* Instance fields */
  321. err = -ENAMETOOLONG;
  322. if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, "hctr2(%s)",
  323. blockcipher_alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
  324. goto err_free_inst;
  325. if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
  326. "hctr2_base(%s,polyval-lib)",
  327. xctr_alg->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
  328. goto err_free_inst;
  329. inst->alg.base.cra_blocksize = BLOCKCIPHER_BLOCK_SIZE;
  330. inst->alg.base.cra_ctxsize = sizeof(struct hctr2_tfm_ctx);
  331. inst->alg.base.cra_alignmask = xctr_alg->base.cra_alignmask;
  332. inst->alg.base.cra_priority = (2 * xctr_alg->base.cra_priority +
  333. blockcipher_alg->cra_priority) /
  334. 3;
  335. inst->alg.setkey = hctr2_setkey;
  336. inst->alg.encrypt = hctr2_encrypt;
  337. inst->alg.decrypt = hctr2_decrypt;
  338. inst->alg.init = hctr2_init_tfm;
  339. inst->alg.exit = hctr2_exit_tfm;
  340. inst->alg.min_keysize = xctr_alg->min_keysize;
  341. inst->alg.max_keysize = xctr_alg->max_keysize;
  342. inst->alg.ivsize = TWEAK_SIZE;
  343. inst->free = hctr2_free_instance;
  344. err = skcipher_register_instance(tmpl, inst);
  345. if (err) {
  346. err_free_inst:
  347. hctr2_free_instance(inst);
  348. }
  349. return err;
  350. }
  351. static int hctr2_create_base(struct crypto_template *tmpl, struct rtattr **tb)
  352. {
  353. const char *xctr_name;
  354. const char *polyval_name;
  355. xctr_name = crypto_attr_alg_name(tb[1]);
  356. if (IS_ERR(xctr_name))
  357. return PTR_ERR(xctr_name);
  358. polyval_name = crypto_attr_alg_name(tb[2]);
  359. if (IS_ERR(polyval_name))
  360. return PTR_ERR(polyval_name);
  361. if (strcmp(polyval_name, "polyval") != 0 &&
  362. strcmp(polyval_name, "polyval-lib") != 0)
  363. return -ENOENT;
  364. return hctr2_create_common(tmpl, tb, xctr_name);
  365. }
  366. static int hctr2_create(struct crypto_template *tmpl, struct rtattr **tb)
  367. {
  368. const char *blockcipher_name;
  369. char xctr_name[CRYPTO_MAX_ALG_NAME];
  370. blockcipher_name = crypto_attr_alg_name(tb[1]);
  371. if (IS_ERR(blockcipher_name))
  372. return PTR_ERR(blockcipher_name);
  373. if (snprintf(xctr_name, CRYPTO_MAX_ALG_NAME, "xctr(%s)",
  374. blockcipher_name) >= CRYPTO_MAX_ALG_NAME)
  375. return -ENAMETOOLONG;
  376. return hctr2_create_common(tmpl, tb, xctr_name);
  377. }
  378. static struct crypto_template hctr2_tmpls[] = {
  379. {
  380. /* hctr2_base(xctr_name, polyval_name) */
  381. .name = "hctr2_base",
  382. .create = hctr2_create_base,
  383. .module = THIS_MODULE,
  384. }, {
  385. /* hctr2(blockcipher_name) */
  386. .name = "hctr2",
  387. .create = hctr2_create,
  388. .module = THIS_MODULE,
  389. }
  390. };
  391. static int __init hctr2_module_init(void)
  392. {
  393. return crypto_register_templates(hctr2_tmpls, ARRAY_SIZE(hctr2_tmpls));
  394. }
  395. static void __exit hctr2_module_exit(void)
  396. {
  397. return crypto_unregister_templates(hctr2_tmpls,
  398. ARRAY_SIZE(hctr2_tmpls));
  399. }
  400. module_init(hctr2_module_init);
  401. module_exit(hctr2_module_exit);
  402. MODULE_DESCRIPTION("HCTR2 length-preserving encryption mode");
  403. MODULE_LICENSE("GPL v2");
  404. MODULE_ALIAS_CRYPTO("hctr2");
  405. MODULE_IMPORT_NS("CRYPTO_INTERNAL");