17ff554ceSNathan Huckleberry // SPDX-License-Identifier: GPL-2.0
27ff554ceSNathan Huckleberry /*
37ff554ceSNathan Huckleberry * HCTR2 length-preserving encryption mode
47ff554ceSNathan Huckleberry *
57ff554ceSNathan Huckleberry * Copyright 2021 Google LLC
67ff554ceSNathan Huckleberry */
77ff554ceSNathan Huckleberry
87ff554ceSNathan Huckleberry
97ff554ceSNathan Huckleberry /*
107ff554ceSNathan Huckleberry * HCTR2 is a length-preserving encryption mode that is efficient on
117ff554ceSNathan Huckleberry * processors with instructions to accelerate AES and carryless
127ff554ceSNathan Huckleberry * multiplication, e.g. x86 processors with AES-NI and CLMUL, and ARM
137ff554ceSNathan Huckleberry * processors with the ARMv8 crypto extensions.
147ff554ceSNathan Huckleberry *
157ff554ceSNathan Huckleberry * For more details, see the paper: "Length-preserving encryption with HCTR2"
167ff554ceSNathan Huckleberry * (https://eprint.iacr.org/2021/1441.pdf)
177ff554ceSNathan Huckleberry */
187ff554ceSNathan Huckleberry
197ff554ceSNathan Huckleberry #include <crypto/internal/cipher.h>
207ff554ceSNathan Huckleberry #include <crypto/internal/hash.h>
217ff554ceSNathan Huckleberry #include <crypto/internal/skcipher.h>
227ff554ceSNathan Huckleberry #include <crypto/polyval.h>
237ff554ceSNathan Huckleberry #include <crypto/scatterwalk.h>
247ff554ceSNathan Huckleberry #include <linux/module.h>
257ff554ceSNathan Huckleberry
267ff554ceSNathan Huckleberry #define BLOCKCIPHER_BLOCK_SIZE 16
277ff554ceSNathan Huckleberry
287ff554ceSNathan Huckleberry /*
297ff554ceSNathan Huckleberry * The specification allows variable-length tweaks, but Linux's crypto API
307ff554ceSNathan Huckleberry * currently only allows algorithms to support a single length. The "natural"
317ff554ceSNathan Huckleberry * tweak length for HCTR2 is 16, since that fits into one POLYVAL block for
327ff554ceSNathan Huckleberry * the best performance. But longer tweaks are useful for fscrypt, to avoid
337ff554ceSNathan Huckleberry * needing to derive per-file keys. So instead we use two blocks, or 32 bytes.
347ff554ceSNathan Huckleberry */
357ff554ceSNathan Huckleberry #define TWEAK_SIZE 32
367ff554ceSNathan Huckleberry
377ff554ceSNathan Huckleberry struct hctr2_instance_ctx {
387ff554ceSNathan Huckleberry struct crypto_cipher_spawn blockcipher_spawn;
397ff554ceSNathan Huckleberry struct crypto_skcipher_spawn xctr_spawn;
407ff554ceSNathan Huckleberry struct crypto_shash_spawn polyval_spawn;
417ff554ceSNathan Huckleberry };
427ff554ceSNathan Huckleberry
437ff554ceSNathan Huckleberry struct hctr2_tfm_ctx {
447ff554ceSNathan Huckleberry struct crypto_cipher *blockcipher;
457ff554ceSNathan Huckleberry struct crypto_skcipher *xctr;
467ff554ceSNathan Huckleberry struct crypto_shash *polyval;
477ff554ceSNathan Huckleberry u8 L[BLOCKCIPHER_BLOCK_SIZE];
487ff554ceSNathan Huckleberry int hashed_tweak_offset;
497ff554ceSNathan Huckleberry /*
507ff554ceSNathan Huckleberry * This struct is allocated with extra space for two exported hash
517ff554ceSNathan Huckleberry * states. Since the hash state size is not known at compile-time, we
527ff554ceSNathan Huckleberry * can't add these to the struct directly.
537ff554ceSNathan Huckleberry *
547ff554ceSNathan Huckleberry * hashed_tweaklen_divisible;
557ff554ceSNathan Huckleberry * hashed_tweaklen_remainder;
567ff554ceSNathan Huckleberry */
577ff554ceSNathan Huckleberry };
587ff554ceSNathan Huckleberry
597ff554ceSNathan Huckleberry struct hctr2_request_ctx {
607ff554ceSNathan Huckleberry u8 first_block[BLOCKCIPHER_BLOCK_SIZE];
617ff554ceSNathan Huckleberry u8 xctr_iv[BLOCKCIPHER_BLOCK_SIZE];
627ff554ceSNathan Huckleberry struct scatterlist *bulk_part_dst;
637ff554ceSNathan Huckleberry struct scatterlist *bulk_part_src;
647ff554ceSNathan Huckleberry struct scatterlist sg_src[2];
657ff554ceSNathan Huckleberry struct scatterlist sg_dst[2];
667ff554ceSNathan Huckleberry /*
677ff554ceSNathan Huckleberry * Sub-request sizes are unknown at compile-time, so they need to go
687ff554ceSNathan Huckleberry * after the members with known sizes.
697ff554ceSNathan Huckleberry */
707ff554ceSNathan Huckleberry union {
717ff554ceSNathan Huckleberry struct shash_desc hash_desc;
727ff554ceSNathan Huckleberry struct skcipher_request xctr_req;
737ff554ceSNathan Huckleberry } u;
747ff554ceSNathan Huckleberry /*
757ff554ceSNathan Huckleberry * This struct is allocated with extra space for one exported hash
767ff554ceSNathan Huckleberry * state. Since the hash state size is not known at compile-time, we
777ff554ceSNathan Huckleberry * can't add it to the struct directly.
787ff554ceSNathan Huckleberry *
797ff554ceSNathan Huckleberry * hashed_tweak;
807ff554ceSNathan Huckleberry */
817ff554ceSNathan Huckleberry };
827ff554ceSNathan Huckleberry
hctr2_hashed_tweaklen(const struct hctr2_tfm_ctx * tctx,bool has_remainder)837ff554ceSNathan Huckleberry static inline u8 *hctr2_hashed_tweaklen(const struct hctr2_tfm_ctx *tctx,
847ff554ceSNathan Huckleberry bool has_remainder)
857ff554ceSNathan Huckleberry {
867ff554ceSNathan Huckleberry u8 *p = (u8 *)tctx + sizeof(*tctx);
877ff554ceSNathan Huckleberry
887ff554ceSNathan Huckleberry if (has_remainder) /* For messages not a multiple of block length */
897ff554ceSNathan Huckleberry p += crypto_shash_statesize(tctx->polyval);
907ff554ceSNathan Huckleberry return p;
917ff554ceSNathan Huckleberry }
927ff554ceSNathan Huckleberry
hctr2_hashed_tweak(const struct hctr2_tfm_ctx * tctx,struct hctr2_request_ctx * rctx)937ff554ceSNathan Huckleberry static inline u8 *hctr2_hashed_tweak(const struct hctr2_tfm_ctx *tctx,
947ff554ceSNathan Huckleberry struct hctr2_request_ctx *rctx)
957ff554ceSNathan Huckleberry {
967ff554ceSNathan Huckleberry return (u8 *)rctx + tctx->hashed_tweak_offset;
977ff554ceSNathan Huckleberry }
987ff554ceSNathan Huckleberry
997ff554ceSNathan Huckleberry /*
1007ff554ceSNathan Huckleberry * The input data for each HCTR2 hash step begins with a 16-byte block that
1017ff554ceSNathan Huckleberry * contains the tweak length and a flag that indicates whether the input is evenly
1027ff554ceSNathan Huckleberry * divisible into blocks. Since this implementation only supports one tweak
1037ff554ceSNathan Huckleberry * length, we precompute the two hash states resulting from hashing the two
1047ff554ceSNathan Huckleberry * possible values of this initial block. This reduces by one block the amount of
1057ff554ceSNathan Huckleberry * data that needs to be hashed for each encryption/decryption
1067ff554ceSNathan Huckleberry *
1077ff554ceSNathan Huckleberry * These precomputed hashes are stored in hctr2_tfm_ctx.
1087ff554ceSNathan Huckleberry */
hctr2_hash_tweaklen(struct hctr2_tfm_ctx * tctx,bool has_remainder)1097ff554ceSNathan Huckleberry static int hctr2_hash_tweaklen(struct hctr2_tfm_ctx *tctx, bool has_remainder)
1107ff554ceSNathan Huckleberry {
1117ff554ceSNathan Huckleberry SHASH_DESC_ON_STACK(shash, tfm->polyval);
1127ff554ceSNathan Huckleberry __le64 tweak_length_block[2];
1137ff554ceSNathan Huckleberry int err;
1147ff554ceSNathan Huckleberry
1157ff554ceSNathan Huckleberry shash->tfm = tctx->polyval;
1167ff554ceSNathan Huckleberry memset(tweak_length_block, 0, sizeof(tweak_length_block));
1177ff554ceSNathan Huckleberry
1187ff554ceSNathan Huckleberry tweak_length_block[0] = cpu_to_le64(TWEAK_SIZE * 8 * 2 + 2 + has_remainder);
1197ff554ceSNathan Huckleberry err = crypto_shash_init(shash);
1207ff554ceSNathan Huckleberry if (err)
1217ff554ceSNathan Huckleberry return err;
1227ff554ceSNathan Huckleberry err = crypto_shash_update(shash, (u8 *)tweak_length_block,
1237ff554ceSNathan Huckleberry POLYVAL_BLOCK_SIZE);
1247ff554ceSNathan Huckleberry if (err)
1257ff554ceSNathan Huckleberry return err;
1267ff554ceSNathan Huckleberry return crypto_shash_export(shash, hctr2_hashed_tweaklen(tctx, has_remainder));
1277ff554ceSNathan Huckleberry }
1287ff554ceSNathan Huckleberry
hctr2_setkey(struct crypto_skcipher * tfm,const u8 * key,unsigned int keylen)1297ff554ceSNathan Huckleberry static int hctr2_setkey(struct crypto_skcipher *tfm, const u8 *key,
1307ff554ceSNathan Huckleberry unsigned int keylen)
1317ff554ceSNathan Huckleberry {
1327ff554ceSNathan Huckleberry struct hctr2_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
1337ff554ceSNathan Huckleberry u8 hbar[BLOCKCIPHER_BLOCK_SIZE];
1347ff554ceSNathan Huckleberry int err;
1357ff554ceSNathan Huckleberry
1367ff554ceSNathan Huckleberry crypto_cipher_clear_flags(tctx->blockcipher, CRYPTO_TFM_REQ_MASK);
1377ff554ceSNathan Huckleberry crypto_cipher_set_flags(tctx->blockcipher,
1387ff554ceSNathan Huckleberry crypto_skcipher_get_flags(tfm) &
1397ff554ceSNathan Huckleberry CRYPTO_TFM_REQ_MASK);
1407ff554ceSNathan Huckleberry err = crypto_cipher_setkey(tctx->blockcipher, key, keylen);
1417ff554ceSNathan Huckleberry if (err)
1427ff554ceSNathan Huckleberry return err;
1437ff554ceSNathan Huckleberry
1447ff554ceSNathan Huckleberry crypto_skcipher_clear_flags(tctx->xctr, CRYPTO_TFM_REQ_MASK);
1457ff554ceSNathan Huckleberry crypto_skcipher_set_flags(tctx->xctr,
1467ff554ceSNathan Huckleberry crypto_skcipher_get_flags(tfm) &
1477ff554ceSNathan Huckleberry CRYPTO_TFM_REQ_MASK);
1487ff554ceSNathan Huckleberry err = crypto_skcipher_setkey(tctx->xctr, key, keylen);
1497ff554ceSNathan Huckleberry if (err)
1507ff554ceSNathan Huckleberry return err;
1517ff554ceSNathan Huckleberry
1527ff554ceSNathan Huckleberry memset(hbar, 0, sizeof(hbar));
1537ff554ceSNathan Huckleberry crypto_cipher_encrypt_one(tctx->blockcipher, hbar, hbar);
1547ff554ceSNathan Huckleberry
1557ff554ceSNathan Huckleberry memset(tctx->L, 0, sizeof(tctx->L));
1567ff554ceSNathan Huckleberry tctx->L[0] = 0x01;
1577ff554ceSNathan Huckleberry crypto_cipher_encrypt_one(tctx->blockcipher, tctx->L, tctx->L);
1587ff554ceSNathan Huckleberry
1597ff554ceSNathan Huckleberry crypto_shash_clear_flags(tctx->polyval, CRYPTO_TFM_REQ_MASK);
1607ff554ceSNathan Huckleberry crypto_shash_set_flags(tctx->polyval, crypto_skcipher_get_flags(tfm) &
1617ff554ceSNathan Huckleberry CRYPTO_TFM_REQ_MASK);
1627ff554ceSNathan Huckleberry err = crypto_shash_setkey(tctx->polyval, hbar, BLOCKCIPHER_BLOCK_SIZE);
1637ff554ceSNathan Huckleberry if (err)
1647ff554ceSNathan Huckleberry return err;
1657ff554ceSNathan Huckleberry memzero_explicit(hbar, sizeof(hbar));
1667ff554ceSNathan Huckleberry
1677ff554ceSNathan Huckleberry return hctr2_hash_tweaklen(tctx, true) ?: hctr2_hash_tweaklen(tctx, false);
1687ff554ceSNathan Huckleberry }
1697ff554ceSNathan Huckleberry
hctr2_hash_tweak(struct skcipher_request * req)1707ff554ceSNathan Huckleberry static int hctr2_hash_tweak(struct skcipher_request *req)
1717ff554ceSNathan Huckleberry {
1727ff554ceSNathan Huckleberry struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
1737ff554ceSNathan Huckleberry const struct hctr2_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
1747ff554ceSNathan Huckleberry struct hctr2_request_ctx *rctx = skcipher_request_ctx(req);
1757ff554ceSNathan Huckleberry struct shash_desc *hash_desc = &rctx->u.hash_desc;
1767ff554ceSNathan Huckleberry int err;
1777ff554ceSNathan Huckleberry bool has_remainder = req->cryptlen % POLYVAL_BLOCK_SIZE;
1787ff554ceSNathan Huckleberry
1797ff554ceSNathan Huckleberry hash_desc->tfm = tctx->polyval;
1807ff554ceSNathan Huckleberry err = crypto_shash_import(hash_desc, hctr2_hashed_tweaklen(tctx, has_remainder));
1817ff554ceSNathan Huckleberry if (err)
1827ff554ceSNathan Huckleberry return err;
1837ff554ceSNathan Huckleberry err = crypto_shash_update(hash_desc, req->iv, TWEAK_SIZE);
1847ff554ceSNathan Huckleberry if (err)
1857ff554ceSNathan Huckleberry return err;
1867ff554ceSNathan Huckleberry
1877ff554ceSNathan Huckleberry // Store the hashed tweak, since we need it when computing both
1887ff554ceSNathan Huckleberry // H(T || N) and H(T || V).
1897ff554ceSNathan Huckleberry return crypto_shash_export(hash_desc, hctr2_hashed_tweak(tctx, rctx));
1907ff554ceSNathan Huckleberry }
1917ff554ceSNathan Huckleberry
hctr2_hash_message(struct skcipher_request * req,struct scatterlist * sgl,u8 digest[POLYVAL_DIGEST_SIZE])1927ff554ceSNathan Huckleberry static int hctr2_hash_message(struct skcipher_request *req,
1937ff554ceSNathan Huckleberry struct scatterlist *sgl,
1947ff554ceSNathan Huckleberry u8 digest[POLYVAL_DIGEST_SIZE])
1957ff554ceSNathan Huckleberry {
1967ff554ceSNathan Huckleberry static const u8 padding[BLOCKCIPHER_BLOCK_SIZE] = { 0x1 };
1977ff554ceSNathan Huckleberry struct hctr2_request_ctx *rctx = skcipher_request_ctx(req);
1987ff554ceSNathan Huckleberry struct shash_desc *hash_desc = &rctx->u.hash_desc;
1997ff554ceSNathan Huckleberry const unsigned int bulk_len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
2007ff554ceSNathan Huckleberry struct sg_mapping_iter miter;
2017ff554ceSNathan Huckleberry unsigned int remainder = bulk_len % BLOCKCIPHER_BLOCK_SIZE;
2027ff554ceSNathan Huckleberry int i;
2037ff554ceSNathan Huckleberry int err = 0;
2047ff554ceSNathan Huckleberry int n = 0;
2057ff554ceSNathan Huckleberry
2067ff554ceSNathan Huckleberry sg_miter_start(&miter, sgl, sg_nents(sgl),
2077ff554ceSNathan Huckleberry SG_MITER_FROM_SG | SG_MITER_ATOMIC);
2087ff554ceSNathan Huckleberry for (i = 0; i < bulk_len; i += n) {
2097ff554ceSNathan Huckleberry sg_miter_next(&miter);
2107ff554ceSNathan Huckleberry n = min_t(unsigned int, miter.length, bulk_len - i);
2117ff554ceSNathan Huckleberry err = crypto_shash_update(hash_desc, miter.addr, n);
2127ff554ceSNathan Huckleberry if (err)
2137ff554ceSNathan Huckleberry break;
2147ff554ceSNathan Huckleberry }
2157ff554ceSNathan Huckleberry sg_miter_stop(&miter);
2167ff554ceSNathan Huckleberry
2177ff554ceSNathan Huckleberry if (err)
2187ff554ceSNathan Huckleberry return err;
2197ff554ceSNathan Huckleberry
2207ff554ceSNathan Huckleberry if (remainder) {
2217ff554ceSNathan Huckleberry err = crypto_shash_update(hash_desc, padding,
2227ff554ceSNathan Huckleberry BLOCKCIPHER_BLOCK_SIZE - remainder);
2237ff554ceSNathan Huckleberry if (err)
2247ff554ceSNathan Huckleberry return err;
2257ff554ceSNathan Huckleberry }
2267ff554ceSNathan Huckleberry return crypto_shash_final(hash_desc, digest);
2277ff554ceSNathan Huckleberry }
2287ff554ceSNathan Huckleberry
hctr2_finish(struct skcipher_request * req)2297ff554ceSNathan Huckleberry static int hctr2_finish(struct skcipher_request *req)
2307ff554ceSNathan Huckleberry {
2317ff554ceSNathan Huckleberry struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2327ff554ceSNathan Huckleberry const struct hctr2_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
2337ff554ceSNathan Huckleberry struct hctr2_request_ctx *rctx = skcipher_request_ctx(req);
2347ff554ceSNathan Huckleberry u8 digest[POLYVAL_DIGEST_SIZE];
2357ff554ceSNathan Huckleberry struct shash_desc *hash_desc = &rctx->u.hash_desc;
2367ff554ceSNathan Huckleberry int err;
2377ff554ceSNathan Huckleberry
2387ff554ceSNathan Huckleberry // U = UU ^ H(T || V)
2397ff554ceSNathan Huckleberry // or M = MM ^ H(T || N)
2407ff554ceSNathan Huckleberry hash_desc->tfm = tctx->polyval;
2417ff554ceSNathan Huckleberry err = crypto_shash_import(hash_desc, hctr2_hashed_tweak(tctx, rctx));
2427ff554ceSNathan Huckleberry if (err)
2437ff554ceSNathan Huckleberry return err;
2447ff554ceSNathan Huckleberry err = hctr2_hash_message(req, rctx->bulk_part_dst, digest);
2457ff554ceSNathan Huckleberry if (err)
2467ff554ceSNathan Huckleberry return err;
2477ff554ceSNathan Huckleberry crypto_xor(rctx->first_block, digest, BLOCKCIPHER_BLOCK_SIZE);
2487ff554ceSNathan Huckleberry
2497ff554ceSNathan Huckleberry // Copy U (or M) into dst scatterlist
2507ff554ceSNathan Huckleberry scatterwalk_map_and_copy(rctx->first_block, req->dst,
2517ff554ceSNathan Huckleberry 0, BLOCKCIPHER_BLOCK_SIZE, 1);
2527ff554ceSNathan Huckleberry return 0;
2537ff554ceSNathan Huckleberry }
2547ff554ceSNathan Huckleberry
hctr2_xctr_done(void * data,int err)255255e48ebSHerbert Xu static void hctr2_xctr_done(void *data, int err)
2567ff554ceSNathan Huckleberry {
257255e48ebSHerbert Xu struct skcipher_request *req = data;
2587ff554ceSNathan Huckleberry
2597ff554ceSNathan Huckleberry if (!err)
2607ff554ceSNathan Huckleberry err = hctr2_finish(req);
2617ff554ceSNathan Huckleberry
2627ff554ceSNathan Huckleberry skcipher_request_complete(req, err);
2637ff554ceSNathan Huckleberry }
2647ff554ceSNathan Huckleberry
hctr2_crypt(struct skcipher_request * req,bool enc)2657ff554ceSNathan Huckleberry static int hctr2_crypt(struct skcipher_request *req, bool enc)
2667ff554ceSNathan Huckleberry {
2677ff554ceSNathan Huckleberry struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
2687ff554ceSNathan Huckleberry const struct hctr2_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
2697ff554ceSNathan Huckleberry struct hctr2_request_ctx *rctx = skcipher_request_ctx(req);
2707ff554ceSNathan Huckleberry u8 digest[POLYVAL_DIGEST_SIZE];
2717ff554ceSNathan Huckleberry int bulk_len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
2727ff554ceSNathan Huckleberry int err;
2737ff554ceSNathan Huckleberry
2747ff554ceSNathan Huckleberry // Requests must be at least one block
2757ff554ceSNathan Huckleberry if (req->cryptlen < BLOCKCIPHER_BLOCK_SIZE)
2767ff554ceSNathan Huckleberry return -EINVAL;
2777ff554ceSNathan Huckleberry
2787ff554ceSNathan Huckleberry // Copy M (or U) into a temporary buffer
2797ff554ceSNathan Huckleberry scatterwalk_map_and_copy(rctx->first_block, req->src,
2807ff554ceSNathan Huckleberry 0, BLOCKCIPHER_BLOCK_SIZE, 0);
2817ff554ceSNathan Huckleberry
2827ff554ceSNathan Huckleberry // Create scatterlists for N and V
2837ff554ceSNathan Huckleberry rctx->bulk_part_src = scatterwalk_ffwd(rctx->sg_src, req->src,
2847ff554ceSNathan Huckleberry BLOCKCIPHER_BLOCK_SIZE);
2857ff554ceSNathan Huckleberry rctx->bulk_part_dst = scatterwalk_ffwd(rctx->sg_dst, req->dst,
2867ff554ceSNathan Huckleberry BLOCKCIPHER_BLOCK_SIZE);
2877ff554ceSNathan Huckleberry
2887ff554ceSNathan Huckleberry // MM = M ^ H(T || N)
2897ff554ceSNathan Huckleberry // or UU = U ^ H(T || V)
2907ff554ceSNathan Huckleberry err = hctr2_hash_tweak(req);
2917ff554ceSNathan Huckleberry if (err)
2927ff554ceSNathan Huckleberry return err;
2937ff554ceSNathan Huckleberry err = hctr2_hash_message(req, rctx->bulk_part_src, digest);
2947ff554ceSNathan Huckleberry if (err)
2957ff554ceSNathan Huckleberry return err;
2967ff554ceSNathan Huckleberry crypto_xor(digest, rctx->first_block, BLOCKCIPHER_BLOCK_SIZE);
2977ff554ceSNathan Huckleberry
2987ff554ceSNathan Huckleberry // UU = E(MM)
2997ff554ceSNathan Huckleberry // or MM = D(UU)
3007ff554ceSNathan Huckleberry if (enc)
3017ff554ceSNathan Huckleberry crypto_cipher_encrypt_one(tctx->blockcipher, rctx->first_block,
3027ff554ceSNathan Huckleberry digest);
3037ff554ceSNathan Huckleberry else
3047ff554ceSNathan Huckleberry crypto_cipher_decrypt_one(tctx->blockcipher, rctx->first_block,
3057ff554ceSNathan Huckleberry digest);
3067ff554ceSNathan Huckleberry
3077ff554ceSNathan Huckleberry // S = MM ^ UU ^ L
3087ff554ceSNathan Huckleberry crypto_xor(digest, rctx->first_block, BLOCKCIPHER_BLOCK_SIZE);
3097ff554ceSNathan Huckleberry crypto_xor_cpy(rctx->xctr_iv, digest, tctx->L, BLOCKCIPHER_BLOCK_SIZE);
3107ff554ceSNathan Huckleberry
3117ff554ceSNathan Huckleberry // V = XCTR(S, N)
3127ff554ceSNathan Huckleberry // or N = XCTR(S, V)
3137ff554ceSNathan Huckleberry skcipher_request_set_tfm(&rctx->u.xctr_req, tctx->xctr);
3147ff554ceSNathan Huckleberry skcipher_request_set_crypt(&rctx->u.xctr_req, rctx->bulk_part_src,
3157ff554ceSNathan Huckleberry rctx->bulk_part_dst, bulk_len,
3167ff554ceSNathan Huckleberry rctx->xctr_iv);
3177ff554ceSNathan Huckleberry skcipher_request_set_callback(&rctx->u.xctr_req,
3187ff554ceSNathan Huckleberry req->base.flags,
3197ff554ceSNathan Huckleberry hctr2_xctr_done, req);
3207ff554ceSNathan Huckleberry return crypto_skcipher_encrypt(&rctx->u.xctr_req) ?:
3217ff554ceSNathan Huckleberry hctr2_finish(req);
3227ff554ceSNathan Huckleberry }
3237ff554ceSNathan Huckleberry
hctr2_encrypt(struct skcipher_request * req)3247ff554ceSNathan Huckleberry static int hctr2_encrypt(struct skcipher_request *req)
3257ff554ceSNathan Huckleberry {
3267ff554ceSNathan Huckleberry return hctr2_crypt(req, true);
3277ff554ceSNathan Huckleberry }
3287ff554ceSNathan Huckleberry
hctr2_decrypt(struct skcipher_request * req)3297ff554ceSNathan Huckleberry static int hctr2_decrypt(struct skcipher_request *req)
3307ff554ceSNathan Huckleberry {
3317ff554ceSNathan Huckleberry return hctr2_crypt(req, false);
3327ff554ceSNathan Huckleberry }
3337ff554ceSNathan Huckleberry
hctr2_init_tfm(struct crypto_skcipher * tfm)3347ff554ceSNathan Huckleberry static int hctr2_init_tfm(struct crypto_skcipher *tfm)
3357ff554ceSNathan Huckleberry {
3367ff554ceSNathan Huckleberry struct skcipher_instance *inst = skcipher_alg_instance(tfm);
3377ff554ceSNathan Huckleberry struct hctr2_instance_ctx *ictx = skcipher_instance_ctx(inst);
3387ff554ceSNathan Huckleberry struct hctr2_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
3397ff554ceSNathan Huckleberry struct crypto_skcipher *xctr;
3407ff554ceSNathan Huckleberry struct crypto_cipher *blockcipher;
3417ff554ceSNathan Huckleberry struct crypto_shash *polyval;
3427ff554ceSNathan Huckleberry unsigned int subreq_size;
3437ff554ceSNathan Huckleberry int err;
3447ff554ceSNathan Huckleberry
3457ff554ceSNathan Huckleberry xctr = crypto_spawn_skcipher(&ictx->xctr_spawn);
3467ff554ceSNathan Huckleberry if (IS_ERR(xctr))
3477ff554ceSNathan Huckleberry return PTR_ERR(xctr);
3487ff554ceSNathan Huckleberry
3497ff554ceSNathan Huckleberry blockcipher = crypto_spawn_cipher(&ictx->blockcipher_spawn);
3507ff554ceSNathan Huckleberry if (IS_ERR(blockcipher)) {
3517ff554ceSNathan Huckleberry err = PTR_ERR(blockcipher);
3527ff554ceSNathan Huckleberry goto err_free_xctr;
3537ff554ceSNathan Huckleberry }
3547ff554ceSNathan Huckleberry
3557ff554ceSNathan Huckleberry polyval = crypto_spawn_shash(&ictx->polyval_spawn);
3567ff554ceSNathan Huckleberry if (IS_ERR(polyval)) {
3577ff554ceSNathan Huckleberry err = PTR_ERR(polyval);
3587ff554ceSNathan Huckleberry goto err_free_blockcipher;
3597ff554ceSNathan Huckleberry }
3607ff554ceSNathan Huckleberry
3617ff554ceSNathan Huckleberry tctx->xctr = xctr;
3627ff554ceSNathan Huckleberry tctx->blockcipher = blockcipher;
3637ff554ceSNathan Huckleberry tctx->polyval = polyval;
3647ff554ceSNathan Huckleberry
3657ff554ceSNathan Huckleberry BUILD_BUG_ON(offsetofend(struct hctr2_request_ctx, u) !=
3667ff554ceSNathan Huckleberry sizeof(struct hctr2_request_ctx));
3677ff554ceSNathan Huckleberry subreq_size = max(sizeof_field(struct hctr2_request_ctx, u.hash_desc) +
3687ff554ceSNathan Huckleberry crypto_shash_descsize(polyval),
3697ff554ceSNathan Huckleberry sizeof_field(struct hctr2_request_ctx, u.xctr_req) +
3707ff554ceSNathan Huckleberry crypto_skcipher_reqsize(xctr));
3717ff554ceSNathan Huckleberry
3727ff554ceSNathan Huckleberry tctx->hashed_tweak_offset = offsetof(struct hctr2_request_ctx, u) +
3737ff554ceSNathan Huckleberry subreq_size;
3747ff554ceSNathan Huckleberry crypto_skcipher_set_reqsize(tfm, tctx->hashed_tweak_offset +
3757ff554ceSNathan Huckleberry crypto_shash_statesize(polyval));
3767ff554ceSNathan Huckleberry return 0;
3777ff554ceSNathan Huckleberry
3787ff554ceSNathan Huckleberry err_free_blockcipher:
3797ff554ceSNathan Huckleberry crypto_free_cipher(blockcipher);
3807ff554ceSNathan Huckleberry err_free_xctr:
3817ff554ceSNathan Huckleberry crypto_free_skcipher(xctr);
3827ff554ceSNathan Huckleberry return err;
3837ff554ceSNathan Huckleberry }
3847ff554ceSNathan Huckleberry
hctr2_exit_tfm(struct crypto_skcipher * tfm)3857ff554ceSNathan Huckleberry static void hctr2_exit_tfm(struct crypto_skcipher *tfm)
3867ff554ceSNathan Huckleberry {
3877ff554ceSNathan Huckleberry struct hctr2_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
3887ff554ceSNathan Huckleberry
3897ff554ceSNathan Huckleberry crypto_free_cipher(tctx->blockcipher);
3907ff554ceSNathan Huckleberry crypto_free_skcipher(tctx->xctr);
3917ff554ceSNathan Huckleberry crypto_free_shash(tctx->polyval);
3927ff554ceSNathan Huckleberry }
3937ff554ceSNathan Huckleberry
hctr2_free_instance(struct skcipher_instance * inst)3947ff554ceSNathan Huckleberry static void hctr2_free_instance(struct skcipher_instance *inst)
3957ff554ceSNathan Huckleberry {
3967ff554ceSNathan Huckleberry struct hctr2_instance_ctx *ictx = skcipher_instance_ctx(inst);
3977ff554ceSNathan Huckleberry
3987ff554ceSNathan Huckleberry crypto_drop_cipher(&ictx->blockcipher_spawn);
3997ff554ceSNathan Huckleberry crypto_drop_skcipher(&ictx->xctr_spawn);
4007ff554ceSNathan Huckleberry crypto_drop_shash(&ictx->polyval_spawn);
4017ff554ceSNathan Huckleberry kfree(inst);
4027ff554ceSNathan Huckleberry }
4037ff554ceSNathan Huckleberry
hctr2_create_common(struct crypto_template * tmpl,struct rtattr ** tb,const char * xctr_name,const char * polyval_name)4047ff554ceSNathan Huckleberry static int hctr2_create_common(struct crypto_template *tmpl,
4057ff554ceSNathan Huckleberry struct rtattr **tb,
4067ff554ceSNathan Huckleberry const char *xctr_name,
4077ff554ceSNathan Huckleberry const char *polyval_name)
4087ff554ceSNathan Huckleberry {
409c4c6bb6eSHerbert Xu struct skcipher_alg_common *xctr_alg;
4107ff554ceSNathan Huckleberry u32 mask;
4117ff554ceSNathan Huckleberry struct skcipher_instance *inst;
4127ff554ceSNathan Huckleberry struct hctr2_instance_ctx *ictx;
4137ff554ceSNathan Huckleberry struct crypto_alg *blockcipher_alg;
4147ff554ceSNathan Huckleberry struct shash_alg *polyval_alg;
4157ff554ceSNathan Huckleberry char blockcipher_name[CRYPTO_MAX_ALG_NAME];
4167ff554ceSNathan Huckleberry int len;
4177ff554ceSNathan Huckleberry int err;
4187ff554ceSNathan Huckleberry
4197ff554ceSNathan Huckleberry err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
4207ff554ceSNathan Huckleberry if (err)
4217ff554ceSNathan Huckleberry return err;
4227ff554ceSNathan Huckleberry
4237ff554ceSNathan Huckleberry inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
4247ff554ceSNathan Huckleberry if (!inst)
4257ff554ceSNathan Huckleberry return -ENOMEM;
4267ff554ceSNathan Huckleberry ictx = skcipher_instance_ctx(inst);
4277ff554ceSNathan Huckleberry
4287ff554ceSNathan Huckleberry /* Stream cipher, xctr(block_cipher) */
4297ff554ceSNathan Huckleberry err = crypto_grab_skcipher(&ictx->xctr_spawn,
4307ff554ceSNathan Huckleberry skcipher_crypto_instance(inst),
4317ff554ceSNathan Huckleberry xctr_name, 0, mask);
4327ff554ceSNathan Huckleberry if (err)
4337ff554ceSNathan Huckleberry goto err_free_inst;
434c4c6bb6eSHerbert Xu xctr_alg = crypto_spawn_skcipher_alg_common(&ictx->xctr_spawn);
4357ff554ceSNathan Huckleberry
4367ff554ceSNathan Huckleberry err = -EINVAL;
4377ff554ceSNathan Huckleberry if (strncmp(xctr_alg->base.cra_name, "xctr(", 5))
4387ff554ceSNathan Huckleberry goto err_free_inst;
4397ff554ceSNathan Huckleberry len = strscpy(blockcipher_name, xctr_alg->base.cra_name + 5,
4407ff554ceSNathan Huckleberry sizeof(blockcipher_name));
4417ff554ceSNathan Huckleberry if (len < 1)
4427ff554ceSNathan Huckleberry goto err_free_inst;
4437ff554ceSNathan Huckleberry if (blockcipher_name[len - 1] != ')')
4447ff554ceSNathan Huckleberry goto err_free_inst;
4457ff554ceSNathan Huckleberry blockcipher_name[len - 1] = 0;
4467ff554ceSNathan Huckleberry
4477ff554ceSNathan Huckleberry /* Block cipher, e.g. "aes" */
4487ff554ceSNathan Huckleberry err = crypto_grab_cipher(&ictx->blockcipher_spawn,
4497ff554ceSNathan Huckleberry skcipher_crypto_instance(inst),
4507ff554ceSNathan Huckleberry blockcipher_name, 0, mask);
4517ff554ceSNathan Huckleberry if (err)
4527ff554ceSNathan Huckleberry goto err_free_inst;
4537ff554ceSNathan Huckleberry blockcipher_alg = crypto_spawn_cipher_alg(&ictx->blockcipher_spawn);
4547ff554ceSNathan Huckleberry
4557ff554ceSNathan Huckleberry /* Require blocksize of 16 bytes */
4567ff554ceSNathan Huckleberry err = -EINVAL;
4577ff554ceSNathan Huckleberry if (blockcipher_alg->cra_blocksize != BLOCKCIPHER_BLOCK_SIZE)
4587ff554ceSNathan Huckleberry goto err_free_inst;
4597ff554ceSNathan Huckleberry
4607ff554ceSNathan Huckleberry /* Polyval ε-∆U hash function */
4617ff554ceSNathan Huckleberry err = crypto_grab_shash(&ictx->polyval_spawn,
4627ff554ceSNathan Huckleberry skcipher_crypto_instance(inst),
4637ff554ceSNathan Huckleberry polyval_name, 0, mask);
4647ff554ceSNathan Huckleberry if (err)
4657ff554ceSNathan Huckleberry goto err_free_inst;
4667ff554ceSNathan Huckleberry polyval_alg = crypto_spawn_shash_alg(&ictx->polyval_spawn);
4677ff554ceSNathan Huckleberry
4687ff554ceSNathan Huckleberry /* Ensure Polyval is being used */
4697ff554ceSNathan Huckleberry err = -EINVAL;
4707ff554ceSNathan Huckleberry if (strcmp(polyval_alg->base.cra_name, "polyval") != 0)
4717ff554ceSNathan Huckleberry goto err_free_inst;
4727ff554ceSNathan Huckleberry
4737ff554ceSNathan Huckleberry /* Instance fields */
4747ff554ceSNathan Huckleberry
4757ff554ceSNathan Huckleberry err = -ENAMETOOLONG;
4767ff554ceSNathan Huckleberry if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME, "hctr2(%s)",
4777ff554ceSNathan Huckleberry blockcipher_alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
4787ff554ceSNathan Huckleberry goto err_free_inst;
4797ff554ceSNathan Huckleberry if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
4807ff554ceSNathan Huckleberry "hctr2_base(%s,%s)",
4817ff554ceSNathan Huckleberry xctr_alg->base.cra_driver_name,
4827ff554ceSNathan Huckleberry polyval_alg->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
4837ff554ceSNathan Huckleberry goto err_free_inst;
4847ff554ceSNathan Huckleberry
4857ff554ceSNathan Huckleberry inst->alg.base.cra_blocksize = BLOCKCIPHER_BLOCK_SIZE;
4867ff554ceSNathan Huckleberry inst->alg.base.cra_ctxsize = sizeof(struct hctr2_tfm_ctx) +
4877ff554ceSNathan Huckleberry polyval_alg->statesize * 2;
488*f6f1514cSEric Biggers inst->alg.base.cra_alignmask = xctr_alg->base.cra_alignmask;
4897ff554ceSNathan Huckleberry /*
4907ff554ceSNathan Huckleberry * The hash function is called twice, so it is weighted higher than the
4917ff554ceSNathan Huckleberry * xctr and blockcipher.
4927ff554ceSNathan Huckleberry */
4937ff554ceSNathan Huckleberry inst->alg.base.cra_priority = (2 * xctr_alg->base.cra_priority +
4947ff554ceSNathan Huckleberry 4 * polyval_alg->base.cra_priority +
4957ff554ceSNathan Huckleberry blockcipher_alg->cra_priority) / 7;
4967ff554ceSNathan Huckleberry
4977ff554ceSNathan Huckleberry inst->alg.setkey = hctr2_setkey;
4987ff554ceSNathan Huckleberry inst->alg.encrypt = hctr2_encrypt;
4997ff554ceSNathan Huckleberry inst->alg.decrypt = hctr2_decrypt;
5007ff554ceSNathan Huckleberry inst->alg.init = hctr2_init_tfm;
5017ff554ceSNathan Huckleberry inst->alg.exit = hctr2_exit_tfm;
502c4c6bb6eSHerbert Xu inst->alg.min_keysize = xctr_alg->min_keysize;
503c4c6bb6eSHerbert Xu inst->alg.max_keysize = xctr_alg->max_keysize;
5047ff554ceSNathan Huckleberry inst->alg.ivsize = TWEAK_SIZE;
5057ff554ceSNathan Huckleberry
5067ff554ceSNathan Huckleberry inst->free = hctr2_free_instance;
5077ff554ceSNathan Huckleberry
5087ff554ceSNathan Huckleberry err = skcipher_register_instance(tmpl, inst);
5097ff554ceSNathan Huckleberry if (err) {
5107ff554ceSNathan Huckleberry err_free_inst:
5117ff554ceSNathan Huckleberry hctr2_free_instance(inst);
5127ff554ceSNathan Huckleberry }
5137ff554ceSNathan Huckleberry return err;
5147ff554ceSNathan Huckleberry }
5157ff554ceSNathan Huckleberry
hctr2_create_base(struct crypto_template * tmpl,struct rtattr ** tb)5167ff554ceSNathan Huckleberry static int hctr2_create_base(struct crypto_template *tmpl, struct rtattr **tb)
5177ff554ceSNathan Huckleberry {
5187ff554ceSNathan Huckleberry const char *xctr_name;
5197ff554ceSNathan Huckleberry const char *polyval_name;
5207ff554ceSNathan Huckleberry
5217ff554ceSNathan Huckleberry xctr_name = crypto_attr_alg_name(tb[1]);
5227ff554ceSNathan Huckleberry if (IS_ERR(xctr_name))
5237ff554ceSNathan Huckleberry return PTR_ERR(xctr_name);
5247ff554ceSNathan Huckleberry
5257ff554ceSNathan Huckleberry polyval_name = crypto_attr_alg_name(tb[2]);
5267ff554ceSNathan Huckleberry if (IS_ERR(polyval_name))
5277ff554ceSNathan Huckleberry return PTR_ERR(polyval_name);
5287ff554ceSNathan Huckleberry
5297ff554ceSNathan Huckleberry return hctr2_create_common(tmpl, tb, xctr_name, polyval_name);
5307ff554ceSNathan Huckleberry }
5317ff554ceSNathan Huckleberry
hctr2_create(struct crypto_template * tmpl,struct rtattr ** tb)5327ff554ceSNathan Huckleberry static int hctr2_create(struct crypto_template *tmpl, struct rtattr **tb)
5337ff554ceSNathan Huckleberry {
5347ff554ceSNathan Huckleberry const char *blockcipher_name;
5357ff554ceSNathan Huckleberry char xctr_name[CRYPTO_MAX_ALG_NAME];
5367ff554ceSNathan Huckleberry
5377ff554ceSNathan Huckleberry blockcipher_name = crypto_attr_alg_name(tb[1]);
5387ff554ceSNathan Huckleberry if (IS_ERR(blockcipher_name))
5397ff554ceSNathan Huckleberry return PTR_ERR(blockcipher_name);
5407ff554ceSNathan Huckleberry
5417ff554ceSNathan Huckleberry if (snprintf(xctr_name, CRYPTO_MAX_ALG_NAME, "xctr(%s)",
5427ff554ceSNathan Huckleberry blockcipher_name) >= CRYPTO_MAX_ALG_NAME)
5437ff554ceSNathan Huckleberry return -ENAMETOOLONG;
5447ff554ceSNathan Huckleberry
5457ff554ceSNathan Huckleberry return hctr2_create_common(tmpl, tb, xctr_name, "polyval");
5467ff554ceSNathan Huckleberry }
5477ff554ceSNathan Huckleberry
5487ff554ceSNathan Huckleberry static struct crypto_template hctr2_tmpls[] = {
5497ff554ceSNathan Huckleberry {
5507ff554ceSNathan Huckleberry /* hctr2_base(xctr_name, polyval_name) */
5517ff554ceSNathan Huckleberry .name = "hctr2_base",
5527ff554ceSNathan Huckleberry .create = hctr2_create_base,
5537ff554ceSNathan Huckleberry .module = THIS_MODULE,
5547ff554ceSNathan Huckleberry }, {
5557ff554ceSNathan Huckleberry /* hctr2(blockcipher_name) */
5567ff554ceSNathan Huckleberry .name = "hctr2",
5577ff554ceSNathan Huckleberry .create = hctr2_create,
5587ff554ceSNathan Huckleberry .module = THIS_MODULE,
5597ff554ceSNathan Huckleberry }
5607ff554ceSNathan Huckleberry };
5617ff554ceSNathan Huckleberry
hctr2_module_init(void)5627ff554ceSNathan Huckleberry static int __init hctr2_module_init(void)
5637ff554ceSNathan Huckleberry {
5647ff554ceSNathan Huckleberry return crypto_register_templates(hctr2_tmpls, ARRAY_SIZE(hctr2_tmpls));
5657ff554ceSNathan Huckleberry }
5667ff554ceSNathan Huckleberry
hctr2_module_exit(void)5677ff554ceSNathan Huckleberry static void __exit hctr2_module_exit(void)
5687ff554ceSNathan Huckleberry {
5697ff554ceSNathan Huckleberry return crypto_unregister_templates(hctr2_tmpls,
5707ff554ceSNathan Huckleberry ARRAY_SIZE(hctr2_tmpls));
5717ff554ceSNathan Huckleberry }
5727ff554ceSNathan Huckleberry
5737ff554ceSNathan Huckleberry subsys_initcall(hctr2_module_init);
5747ff554ceSNathan Huckleberry module_exit(hctr2_module_exit);
5757ff554ceSNathan Huckleberry
5767ff554ceSNathan Huckleberry MODULE_DESCRIPTION("HCTR2 length-preserving encryption mode");
5777ff554ceSNathan Huckleberry MODULE_LICENSE("GPL v2");
5787ff554ceSNathan Huckleberry MODULE_ALIAS_CRYPTO("hctr2");
5797ff554ceSNathan Huckleberry MODULE_IMPORT_NS(CRYPTO_INTERNAL);
580