^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Adiantum length-preserving encryption mode
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright 2018 Google LLC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Adiantum is a tweakable, length-preserving encryption mode designed for fast
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * and secure disk encryption, especially on CPUs without dedicated crypto
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * instructions. Adiantum encrypts each sector using the XChaCha12 stream
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * cipher, two passes of an ε-almost-∆-universal (ε-∆U) hash function based on
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * NH and Poly1305, and an invocation of the AES-256 block cipher on a single
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * 16-byte block. See the paper for details:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) * Adiantum: length-preserving encryption for entry-level processors
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * (https://eprint.iacr.org/2018/720.pdf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * For flexibility, this implementation also allows other ciphers:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) * - Stream cipher: XChaCha12 or XChaCha20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) * - Block cipher: any with a 128-bit block size and 256-bit key
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * This implementation doesn't currently allow other ε-∆U hash functions, i.e.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) * HPolyC is not supported. This is because Adiantum is ~20% faster than HPolyC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * but still provably as secure, and also the ε-∆U hash function of HBSH is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * formally defined to take two inputs (tweak, message) which makes it difficult
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * to wrap with the crypto_shash API. Rather, some details need to be handled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * here. Nevertheless, if needed in the future, support for other ε-∆U hash
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) * functions could be added here.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #include <crypto/b128ops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #include <crypto/chacha.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) #include <crypto/internal/cipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) #include <crypto/internal/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #include <crypto/internal/poly1305.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #include <crypto/internal/skcipher.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #include <crypto/nhpoly1305.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #include <crypto/scatterwalk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) * Size of right-hand part of input data, in bytes; also the size of the block
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) * cipher's block size and the hash function's output.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define BLOCKCIPHER_BLOCK_SIZE 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /* Size of the block cipher key (K_E) in bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) #define BLOCKCIPHER_KEY_SIZE 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /* Size of the hash key (K_H) in bytes */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) #define HASH_KEY_SIZE (POLY1305_BLOCK_SIZE + NHPOLY1305_KEY_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) * The specification allows variable-length tweaks, but Linux's crypto API
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * currently only allows algorithms to support a single length. The "natural"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * tweak length for Adiantum is 16, since that fits into one Poly1305 block for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) * the best performance. But longer tweaks are useful for fscrypt, to avoid
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) * needing to derive per-file keys. So instead we use two blocks, or 32 bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #define TWEAK_SIZE 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) struct adiantum_instance_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) struct crypto_skcipher_spawn streamcipher_spawn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) struct crypto_cipher_spawn blockcipher_spawn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct crypto_shash_spawn hash_spawn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct adiantum_tfm_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) struct crypto_skcipher *streamcipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) struct crypto_cipher *blockcipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct crypto_shash *hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) struct poly1305_core_key header_hash_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) struct adiantum_request_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) * Buffer for right-hand part of data, i.e.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) * P_L => P_M => C_M => C_R when encrypting, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) * C_R => C_M => P_M => P_L when decrypting.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) * Also used to build the IV for the stream cipher.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) u8 bytes[XCHACHA_IV_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) __le32 words[XCHACHA_IV_SIZE / sizeof(__le32)];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) le128 bignum; /* interpret as element of Z/(2^{128}Z) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) } rbuf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) bool enc; /* true if encrypting, false if decrypting */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * The result of the Poly1305 ε-∆U hash function applied to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * (bulk length, tweak)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) le128 header_hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) /* Sub-requests, must be last */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) struct shash_desc hash_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) struct skcipher_request streamcipher_req;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) } u;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) * Given the XChaCha stream key K_S, derive the block cipher key K_E and the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) * hash key K_H as follows:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) * K_E || K_H || ... = XChaCha(key=K_S, nonce=1||0^191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) * Note that this denotes using bits from the XChaCha keystream, which here we
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) * get indirectly by encrypting a buffer containing all 0's.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) static int adiantum_setkey(struct crypto_skcipher *tfm, const u8 *key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) unsigned int keylen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) u8 iv[XCHACHA_IV_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) u8 derived_keys[BLOCKCIPHER_KEY_SIZE + HASH_KEY_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) struct scatterlist sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) struct crypto_wait wait;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) struct skcipher_request req; /* must be last */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) } *data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) u8 *keyp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) /* Set the stream cipher key (K_S) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) crypto_skcipher_clear_flags(tctx->streamcipher, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) crypto_skcipher_set_flags(tctx->streamcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) crypto_skcipher_get_flags(tfm) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) err = crypto_skcipher_setkey(tctx->streamcipher, key, keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) /* Derive the subkeys */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) data = kzalloc(sizeof(*data) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) crypto_skcipher_reqsize(tctx->streamcipher), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) data->iv[0] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) sg_init_one(&data->sg, data->derived_keys, sizeof(data->derived_keys));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) crypto_init_wait(&data->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) skcipher_request_set_tfm(&data->req, tctx->streamcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) skcipher_request_set_callback(&data->req, CRYPTO_TFM_REQ_MAY_SLEEP |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) CRYPTO_TFM_REQ_MAY_BACKLOG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) crypto_req_done, &data->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) skcipher_request_set_crypt(&data->req, &data->sg, &data->sg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) sizeof(data->derived_keys), data->iv);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) err = crypto_wait_req(crypto_skcipher_encrypt(&data->req), &data->wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) keyp = data->derived_keys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /* Set the block cipher key (K_E) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) crypto_cipher_clear_flags(tctx->blockcipher, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) crypto_cipher_set_flags(tctx->blockcipher,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) crypto_skcipher_get_flags(tfm) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) err = crypto_cipher_setkey(tctx->blockcipher, keyp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) BLOCKCIPHER_KEY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) keyp += BLOCKCIPHER_KEY_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) /* Set the hash key (K_H) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) poly1305_core_setkey(&tctx->header_hash_key, keyp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) keyp += POLY1305_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) crypto_shash_clear_flags(tctx->hash, CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) crypto_shash_set_flags(tctx->hash, crypto_skcipher_get_flags(tfm) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) CRYPTO_TFM_REQ_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) err = crypto_shash_setkey(tctx->hash, keyp, NHPOLY1305_KEY_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) keyp += NHPOLY1305_KEY_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) WARN_ON(keyp != &data->derived_keys[ARRAY_SIZE(data->derived_keys)]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) kfree_sensitive(data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) /* Addition in Z/(2^{128}Z) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) static inline void le128_add(le128 *r, const le128 *v1, const le128 *v2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) u64 x = le64_to_cpu(v1->b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) u64 y = le64_to_cpu(v2->b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) r->b = cpu_to_le64(x + y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) r->a = cpu_to_le64(le64_to_cpu(v1->a) + le64_to_cpu(v2->a) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) (x + y < x));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) /* Subtraction in Z/(2^{128}Z) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) static inline void le128_sub(le128 *r, const le128 *v1, const le128 *v2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) u64 x = le64_to_cpu(v1->b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) u64 y = le64_to_cpu(v2->b);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) r->b = cpu_to_le64(x - y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) r->a = cpu_to_le64(le64_to_cpu(v1->a) - le64_to_cpu(v2->a) -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) (x - y > x));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) * Apply the Poly1305 ε-∆U hash function to (bulk length, tweak) and save the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) * result to rctx->header_hash. This is the calculation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) * H_T ← Poly1305_{K_T}(bin_{128}(|L|) || T)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) * from the procedure in section 6.4 of the Adiantum paper. The resulting value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) * is reused in both the first and second hash steps. Specifically, it's added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) * to the result of an independently keyed ε-∆U hash function (for equal length
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) * inputs only) taken over the left-hand part (the "bulk") of the message, to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) * give the overall Adiantum hash of the (tweak, left-hand part) pair.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) static void adiantum_hash_header(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) const struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct adiantum_request_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) const unsigned int bulk_len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) __le64 message_bits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) __le64 padding;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) } header = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) .message_bits = cpu_to_le64((u64)bulk_len * 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) struct poly1305_state state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) poly1305_core_init(&state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) BUILD_BUG_ON(sizeof(header) % POLY1305_BLOCK_SIZE != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) poly1305_core_blocks(&state, &tctx->header_hash_key,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) &header, sizeof(header) / POLY1305_BLOCK_SIZE, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) BUILD_BUG_ON(TWEAK_SIZE % POLY1305_BLOCK_SIZE != 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) poly1305_core_blocks(&state, &tctx->header_hash_key, req->iv,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) TWEAK_SIZE / POLY1305_BLOCK_SIZE, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) poly1305_core_emit(&state, NULL, &rctx->header_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) /* Hash the left-hand part (the "bulk") of the message using NHPoly1305 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) static int adiantum_hash_message(struct skcipher_request *req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) struct scatterlist *sgl, le128 *digest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) const struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) struct adiantum_request_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) const unsigned int bulk_len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) struct shash_desc *hash_desc = &rctx->u.hash_desc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) struct sg_mapping_iter miter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) unsigned int i, n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) hash_desc->tfm = tctx->hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) err = crypto_shash_init(hash_desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) sg_miter_start(&miter, sgl, sg_nents(sgl),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) SG_MITER_FROM_SG | SG_MITER_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) for (i = 0; i < bulk_len; i += n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) sg_miter_next(&miter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) n = min_t(unsigned int, miter.length, bulk_len - i);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) err = crypto_shash_update(hash_desc, miter.addr, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) sg_miter_stop(&miter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) return crypto_shash_final(hash_desc, (u8 *)digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) /* Continue Adiantum encryption/decryption after the stream cipher step */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) static int adiantum_finish(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) const struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) struct adiantum_request_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) const unsigned int bulk_len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) le128 digest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) /* If decrypting, decrypt C_M with the block cipher to get P_M */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (!rctx->enc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) crypto_cipher_decrypt_one(tctx->blockcipher, rctx->rbuf.bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) rctx->rbuf.bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) * Second hash step
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) * enc: C_R = C_M - H_{K_H}(T, C_L)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) * dec: P_R = P_M - H_{K_H}(T, P_L)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) err = adiantum_hash_message(req, req->dst, &digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) le128_add(&digest, &digest, &rctx->header_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) le128_sub(&rctx->rbuf.bignum, &rctx->rbuf.bignum, &digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) scatterwalk_map_and_copy(&rctx->rbuf.bignum, req->dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) bulk_len, BLOCKCIPHER_BLOCK_SIZE, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static void adiantum_streamcipher_done(struct crypto_async_request *areq,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) struct skcipher_request *req = areq->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (!err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) err = adiantum_finish(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) skcipher_request_complete(req, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) static int adiantum_crypt(struct skcipher_request *req, bool enc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) const struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) struct adiantum_request_ctx *rctx = skcipher_request_ctx(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) const unsigned int bulk_len = req->cryptlen - BLOCKCIPHER_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) unsigned int stream_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) le128 digest;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if (req->cryptlen < BLOCKCIPHER_BLOCK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) rctx->enc = enc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) * First hash step
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) * enc: P_M = P_R + H_{K_H}(T, P_L)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) * dec: C_M = C_R + H_{K_H}(T, C_L)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) adiantum_hash_header(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) err = adiantum_hash_message(req, req->src, &digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) le128_add(&digest, &digest, &rctx->header_hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) scatterwalk_map_and_copy(&rctx->rbuf.bignum, req->src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) bulk_len, BLOCKCIPHER_BLOCK_SIZE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) le128_add(&rctx->rbuf.bignum, &rctx->rbuf.bignum, &digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) /* If encrypting, encrypt P_M with the block cipher to get C_M */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) if (enc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) crypto_cipher_encrypt_one(tctx->blockcipher, rctx->rbuf.bytes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) rctx->rbuf.bytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) /* Initialize the rest of the XChaCha IV (first part is C_M) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) BUILD_BUG_ON(BLOCKCIPHER_BLOCK_SIZE != 16);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) BUILD_BUG_ON(XCHACHA_IV_SIZE != 32); /* nonce || stream position */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) rctx->rbuf.words[4] = cpu_to_le32(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) rctx->rbuf.words[5] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) rctx->rbuf.words[6] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) rctx->rbuf.words[7] = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) * XChaCha needs to be done on all the data except the last 16 bytes;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) * for disk encryption that usually means 4080 or 496 bytes. But ChaCha
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) * implementations tend to be most efficient when passed a whole number
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) * of 64-byte ChaCha blocks, or sometimes even a multiple of 256 bytes.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) * And here it doesn't matter whether the last 16 bytes are written to,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) * as the second hash step will overwrite them. Thus, round the XChaCha
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) * length up to the next 64-byte boundary if possible.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) stream_len = bulk_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) if (round_up(stream_len, CHACHA_BLOCK_SIZE) <= req->cryptlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) stream_len = round_up(stream_len, CHACHA_BLOCK_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) skcipher_request_set_tfm(&rctx->u.streamcipher_req, tctx->streamcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) skcipher_request_set_crypt(&rctx->u.streamcipher_req, req->src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) req->dst, stream_len, &rctx->rbuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) skcipher_request_set_callback(&rctx->u.streamcipher_req,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) req->base.flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) adiantum_streamcipher_done, req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) return crypto_skcipher_encrypt(&rctx->u.streamcipher_req) ?:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) adiantum_finish(req);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) static int adiantum_encrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) return adiantum_crypt(req, true);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) static int adiantum_decrypt(struct skcipher_request *req)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) return adiantum_crypt(req, false);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) static int adiantum_init_tfm(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) struct skcipher_instance *inst = skcipher_alg_instance(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) struct adiantum_instance_ctx *ictx = skcipher_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) struct crypto_skcipher *streamcipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) struct crypto_cipher *blockcipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) struct crypto_shash *hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) unsigned int subreq_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) streamcipher = crypto_spawn_skcipher(&ictx->streamcipher_spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) if (IS_ERR(streamcipher))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) return PTR_ERR(streamcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) blockcipher = crypto_spawn_cipher(&ictx->blockcipher_spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) if (IS_ERR(blockcipher)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) err = PTR_ERR(blockcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) goto err_free_streamcipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) hash = crypto_spawn_shash(&ictx->hash_spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) if (IS_ERR(hash)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) err = PTR_ERR(hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 421) goto err_free_blockcipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 422) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 423)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 424) tctx->streamcipher = streamcipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 425) tctx->blockcipher = blockcipher;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 426) tctx->hash = hash;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 427)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 428) BUILD_BUG_ON(offsetofend(struct adiantum_request_ctx, u) !=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 429) sizeof(struct adiantum_request_ctx));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 430) subreq_size = max(sizeof_field(struct adiantum_request_ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 431) u.hash_desc) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 432) crypto_shash_descsize(hash),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 433) sizeof_field(struct adiantum_request_ctx,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 434) u.streamcipher_req) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 435) crypto_skcipher_reqsize(streamcipher));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 436)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 437) crypto_skcipher_set_reqsize(tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 438) offsetof(struct adiantum_request_ctx, u) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 439) subreq_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 440) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 441)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 442) err_free_blockcipher:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 443) crypto_free_cipher(blockcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 444) err_free_streamcipher:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 445) crypto_free_skcipher(streamcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 446) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 448)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 449) static void adiantum_exit_tfm(struct crypto_skcipher *tfm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 450) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 451) struct adiantum_tfm_ctx *tctx = crypto_skcipher_ctx(tfm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 452)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 453) crypto_free_skcipher(tctx->streamcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 454) crypto_free_cipher(tctx->blockcipher);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 455) crypto_free_shash(tctx->hash);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 456) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 457)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 458) static void adiantum_free_instance(struct skcipher_instance *inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 459) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 460) struct adiantum_instance_ctx *ictx = skcipher_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 461)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 462) crypto_drop_skcipher(&ictx->streamcipher_spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 463) crypto_drop_cipher(&ictx->blockcipher_spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 464) crypto_drop_shash(&ictx->hash_spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 465) kfree(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 466) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 467)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 468) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 469) * Check for a supported set of inner algorithms.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 470) * See the comment at the beginning of this file.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 471) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 472) static bool adiantum_supported_algorithms(struct skcipher_alg *streamcipher_alg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 473) struct crypto_alg *blockcipher_alg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 474) struct shash_alg *hash_alg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 475) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 476) if (strcmp(streamcipher_alg->base.cra_name, "xchacha12") != 0 &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 477) strcmp(streamcipher_alg->base.cra_name, "xchacha20") != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 478) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 479)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 480) if (blockcipher_alg->cra_cipher.cia_min_keysize > BLOCKCIPHER_KEY_SIZE ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 481) blockcipher_alg->cra_cipher.cia_max_keysize < BLOCKCIPHER_KEY_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 482) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 483) if (blockcipher_alg->cra_blocksize != BLOCKCIPHER_BLOCK_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 484) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 485)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 486) if (strcmp(hash_alg->base.cra_name, "nhpoly1305") != 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 487) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 488)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 489) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 490) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 491)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 492) static int adiantum_create(struct crypto_template *tmpl, struct rtattr **tb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 493) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 494) u32 mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 495) const char *nhpoly1305_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 496) struct skcipher_instance *inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 497) struct adiantum_instance_ctx *ictx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 498) struct skcipher_alg *streamcipher_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 499) struct crypto_alg *blockcipher_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 500) struct shash_alg *hash_alg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 501) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 502)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 503) err = crypto_check_attr_type(tb, CRYPTO_ALG_TYPE_SKCIPHER, &mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 504) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 505) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 506)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 507) inst = kzalloc(sizeof(*inst) + sizeof(*ictx), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 508) if (!inst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 509) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 510) ictx = skcipher_instance_ctx(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 511)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 512) /* Stream cipher, e.g. "xchacha12" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 513) err = crypto_grab_skcipher(&ictx->streamcipher_spawn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 514) skcipher_crypto_instance(inst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 515) crypto_attr_alg_name(tb[1]), 0, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 516) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 517) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 518) streamcipher_alg = crypto_spawn_skcipher_alg(&ictx->streamcipher_spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 519)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 520) /* Block cipher, e.g. "aes" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 521) err = crypto_grab_cipher(&ictx->blockcipher_spawn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 522) skcipher_crypto_instance(inst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 523) crypto_attr_alg_name(tb[2]), 0, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 524) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 525) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 526) blockcipher_alg = crypto_spawn_cipher_alg(&ictx->blockcipher_spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 527)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 528) /* NHPoly1305 ε-∆U hash function */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 529) nhpoly1305_name = crypto_attr_alg_name(tb[3]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 530) if (nhpoly1305_name == ERR_PTR(-ENOENT))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 531) nhpoly1305_name = "nhpoly1305";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 532) err = crypto_grab_shash(&ictx->hash_spawn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 533) skcipher_crypto_instance(inst),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 534) nhpoly1305_name, 0, mask);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 535) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 536) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 537) hash_alg = crypto_spawn_shash_alg(&ictx->hash_spawn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 538)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 539) /* Check the set of algorithms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 540) if (!adiantum_supported_algorithms(streamcipher_alg, blockcipher_alg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 541) hash_alg)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 542) pr_warn("Unsupported Adiantum instantiation: (%s,%s,%s)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 543) streamcipher_alg->base.cra_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 544) blockcipher_alg->cra_name, hash_alg->base.cra_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 545) err = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 546) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 547) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 548)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 549) /* Instance fields */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 550)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 551) err = -ENAMETOOLONG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 552) if (snprintf(inst->alg.base.cra_name, CRYPTO_MAX_ALG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 553) "adiantum(%s,%s)", streamcipher_alg->base.cra_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 554) blockcipher_alg->cra_name) >= CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 555) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 556) if (snprintf(inst->alg.base.cra_driver_name, CRYPTO_MAX_ALG_NAME,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 557) "adiantum(%s,%s,%s)",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 558) streamcipher_alg->base.cra_driver_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 559) blockcipher_alg->cra_driver_name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 560) hash_alg->base.cra_driver_name) >= CRYPTO_MAX_ALG_NAME)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 561) goto err_free_inst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 562)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 563) inst->alg.base.cra_blocksize = BLOCKCIPHER_BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 564) inst->alg.base.cra_ctxsize = sizeof(struct adiantum_tfm_ctx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 565) inst->alg.base.cra_alignmask = streamcipher_alg->base.cra_alignmask |
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 566) hash_alg->base.cra_alignmask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 567) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 568) * The block cipher is only invoked once per message, so for long
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 569) * messages (e.g. sectors for disk encryption) its performance doesn't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 570) * matter as much as that of the stream cipher and hash function. Thus,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 571) * weigh the block cipher's ->cra_priority less.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 572) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 573) inst->alg.base.cra_priority = (4 * streamcipher_alg->base.cra_priority +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 574) 2 * hash_alg->base.cra_priority +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 575) blockcipher_alg->cra_priority) / 7;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 576)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 577) inst->alg.setkey = adiantum_setkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 578) inst->alg.encrypt = adiantum_encrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 579) inst->alg.decrypt = adiantum_decrypt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 580) inst->alg.init = adiantum_init_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 581) inst->alg.exit = adiantum_exit_tfm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 582) inst->alg.min_keysize = crypto_skcipher_alg_min_keysize(streamcipher_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 583) inst->alg.max_keysize = crypto_skcipher_alg_max_keysize(streamcipher_alg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 584) inst->alg.ivsize = TWEAK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 585)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 586) inst->free = adiantum_free_instance;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 587)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 588) err = skcipher_register_instance(tmpl, inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 589) if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 590) err_free_inst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 591) adiantum_free_instance(inst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 592) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 593) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 594) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 595)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 596) /* adiantum(streamcipher_name, blockcipher_name [, nhpoly1305_name]) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 597) static struct crypto_template adiantum_tmpl = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 598) .name = "adiantum",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 599) .create = adiantum_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 600) .module = THIS_MODULE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 601) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 602)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 603) static int __init adiantum_module_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 604) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 605) return crypto_register_template(&adiantum_tmpl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 606) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 607)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 608) static void __exit adiantum_module_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 609) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 610) crypto_unregister_template(&adiantum_tmpl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 611) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 612)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 613) subsys_initcall(adiantum_module_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 614) module_exit(adiantum_module_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 615)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 616) MODULE_DESCRIPTION("Adiantum length-preserving encryption mode");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 617) MODULE_LICENSE("GPL v2");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 618) MODULE_AUTHOR("Eric Biggers <ebiggers@google.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 619) MODULE_ALIAS_CRYPTO("adiantum");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 620) MODULE_IMPORT_NS(CRYPTO_INTERNAL);