xref: /linux/crypto/nhpoly1305.c (revision 48ea8c6ebc96bc0990e12ee1c43d0832c23576bb)
126609a21SEric Biggers // SPDX-License-Identifier: GPL-2.0
226609a21SEric Biggers /*
326609a21SEric Biggers  * NHPoly1305 - ε-almost-∆-universal hash function for Adiantum
426609a21SEric Biggers  *
526609a21SEric Biggers  * Copyright 2018 Google LLC
626609a21SEric Biggers  */
726609a21SEric Biggers 
826609a21SEric Biggers /*
926609a21SEric Biggers  * "NHPoly1305" is the main component of Adiantum hashing.
1026609a21SEric Biggers  * Specifically, it is the calculation
1126609a21SEric Biggers  *
12c6018e1aSEric Biggers  *	H_L ← Poly1305_{K_L}(NH_{K_N}(pad_{128}(L)))
1326609a21SEric Biggers  *
14c6018e1aSEric Biggers  * from the procedure in section 6.4 of the Adiantum paper [1].  It is an
15c6018e1aSEric Biggers  * ε-almost-∆-universal (ε-∆U) hash function for equal-length inputs over
1626609a21SEric Biggers  * Z/(2^{128}Z), where the "∆" operation is addition.  It hashes 1024-byte
1726609a21SEric Biggers  * chunks of the input with the NH hash function [2], reducing the input length
1826609a21SEric Biggers  * by 32x.  The resulting NH digests are evaluated as a polynomial in
1926609a21SEric Biggers  * GF(2^{130}-5), like in the Poly1305 MAC [3].  Note that the polynomial
20c6018e1aSEric Biggers  * evaluation by itself would suffice to achieve the ε-∆U property; NH is used
2126609a21SEric Biggers  * for performance since it's over twice as fast as Poly1305.
2226609a21SEric Biggers  *
2326609a21SEric Biggers  * This is *not* a cryptographic hash function; do not use it as such!
2426609a21SEric Biggers  *
2526609a21SEric Biggers  * [1] Adiantum: length-preserving encryption for entry-level processors
2626609a21SEric Biggers  *     (https://eprint.iacr.org/2018/720.pdf)
2726609a21SEric Biggers  * [2] UMAC: Fast and Secure Message Authentication
2826609a21SEric Biggers  *     (https://fastcrypto.org/umac/umac_proc.pdf)
2926609a21SEric Biggers  * [3] The Poly1305-AES message-authentication code
3026609a21SEric Biggers  *     (https://cr.yp.to/mac/poly1305-20050329.pdf)
3126609a21SEric Biggers  */
3226609a21SEric Biggers 
3326609a21SEric Biggers #include <asm/unaligned.h>
3426609a21SEric Biggers #include <crypto/algapi.h>
3526609a21SEric Biggers #include <crypto/internal/hash.h>
36*48ea8c6eSArd Biesheuvel #include <crypto/internal/poly1305.h>
3726609a21SEric Biggers #include <crypto/nhpoly1305.h>
3826609a21SEric Biggers #include <linux/crypto.h>
3926609a21SEric Biggers #include <linux/kernel.h>
4026609a21SEric Biggers #include <linux/module.h>
4126609a21SEric Biggers 
4226609a21SEric Biggers static void nh_generic(const u32 *key, const u8 *message, size_t message_len,
4326609a21SEric Biggers 		       __le64 hash[NH_NUM_PASSES])
4426609a21SEric Biggers {
4526609a21SEric Biggers 	u64 sums[4] = { 0, 0, 0, 0 };
4626609a21SEric Biggers 
4726609a21SEric Biggers 	BUILD_BUG_ON(NH_PAIR_STRIDE != 2);
4826609a21SEric Biggers 	BUILD_BUG_ON(NH_NUM_PASSES != 4);
4926609a21SEric Biggers 
5026609a21SEric Biggers 	while (message_len) {
5126609a21SEric Biggers 		u32 m0 = get_unaligned_le32(message + 0);
5226609a21SEric Biggers 		u32 m1 = get_unaligned_le32(message + 4);
5326609a21SEric Biggers 		u32 m2 = get_unaligned_le32(message + 8);
5426609a21SEric Biggers 		u32 m3 = get_unaligned_le32(message + 12);
5526609a21SEric Biggers 
5626609a21SEric Biggers 		sums[0] += (u64)(u32)(m0 + key[ 0]) * (u32)(m2 + key[ 2]);
5726609a21SEric Biggers 		sums[1] += (u64)(u32)(m0 + key[ 4]) * (u32)(m2 + key[ 6]);
5826609a21SEric Biggers 		sums[2] += (u64)(u32)(m0 + key[ 8]) * (u32)(m2 + key[10]);
5926609a21SEric Biggers 		sums[3] += (u64)(u32)(m0 + key[12]) * (u32)(m2 + key[14]);
6026609a21SEric Biggers 		sums[0] += (u64)(u32)(m1 + key[ 1]) * (u32)(m3 + key[ 3]);
6126609a21SEric Biggers 		sums[1] += (u64)(u32)(m1 + key[ 5]) * (u32)(m3 + key[ 7]);
6226609a21SEric Biggers 		sums[2] += (u64)(u32)(m1 + key[ 9]) * (u32)(m3 + key[11]);
6326609a21SEric Biggers 		sums[3] += (u64)(u32)(m1 + key[13]) * (u32)(m3 + key[15]);
6426609a21SEric Biggers 		key += NH_MESSAGE_UNIT / sizeof(key[0]);
6526609a21SEric Biggers 		message += NH_MESSAGE_UNIT;
6626609a21SEric Biggers 		message_len -= NH_MESSAGE_UNIT;
6726609a21SEric Biggers 	}
6826609a21SEric Biggers 
6926609a21SEric Biggers 	hash[0] = cpu_to_le64(sums[0]);
7026609a21SEric Biggers 	hash[1] = cpu_to_le64(sums[1]);
7126609a21SEric Biggers 	hash[2] = cpu_to_le64(sums[2]);
7226609a21SEric Biggers 	hash[3] = cpu_to_le64(sums[3]);
7326609a21SEric Biggers }
7426609a21SEric Biggers 
7526609a21SEric Biggers /* Pass the next NH hash value through Poly1305 */
7626609a21SEric Biggers static void process_nh_hash_value(struct nhpoly1305_state *state,
7726609a21SEric Biggers 				  const struct nhpoly1305_key *key)
7826609a21SEric Biggers {
7926609a21SEric Biggers 	BUILD_BUG_ON(NH_HASH_BYTES % POLY1305_BLOCK_SIZE != 0);
8026609a21SEric Biggers 
8126609a21SEric Biggers 	poly1305_core_blocks(&state->poly_state, &key->poly_key, state->nh_hash,
82*48ea8c6eSArd Biesheuvel 			     NH_HASH_BYTES / POLY1305_BLOCK_SIZE, 1);
8326609a21SEric Biggers }
8426609a21SEric Biggers 
8526609a21SEric Biggers /*
8626609a21SEric Biggers  * Feed the next portion of the source data, as a whole number of 16-byte
8726609a21SEric Biggers  * "NH message units", through NH and Poly1305.  Each NH hash is taken over
8826609a21SEric Biggers  * 1024 bytes, except possibly the final one which is taken over a multiple of
8926609a21SEric Biggers  * 16 bytes up to 1024.  Also, in the case where data is passed in misaligned
9026609a21SEric Biggers  * chunks, we combine partial hashes; the end result is the same either way.
9126609a21SEric Biggers  */
9226609a21SEric Biggers static void nhpoly1305_units(struct nhpoly1305_state *state,
9326609a21SEric Biggers 			     const struct nhpoly1305_key *key,
9426609a21SEric Biggers 			     const u8 *src, unsigned int srclen, nh_t nh_fn)
9526609a21SEric Biggers {
9626609a21SEric Biggers 	do {
9726609a21SEric Biggers 		unsigned int bytes;
9826609a21SEric Biggers 
9926609a21SEric Biggers 		if (state->nh_remaining == 0) {
10026609a21SEric Biggers 			/* Starting a new NH message */
10126609a21SEric Biggers 			bytes = min_t(unsigned int, srclen, NH_MESSAGE_BYTES);
10226609a21SEric Biggers 			nh_fn(key->nh_key, src, bytes, state->nh_hash);
10326609a21SEric Biggers 			state->nh_remaining = NH_MESSAGE_BYTES - bytes;
10426609a21SEric Biggers 		} else {
10526609a21SEric Biggers 			/* Continuing a previous NH message */
10626609a21SEric Biggers 			__le64 tmp_hash[NH_NUM_PASSES];
10726609a21SEric Biggers 			unsigned int pos;
10826609a21SEric Biggers 			int i;
10926609a21SEric Biggers 
11026609a21SEric Biggers 			pos = NH_MESSAGE_BYTES - state->nh_remaining;
11126609a21SEric Biggers 			bytes = min(srclen, state->nh_remaining);
11226609a21SEric Biggers 			nh_fn(&key->nh_key[pos / 4], src, bytes, tmp_hash);
11326609a21SEric Biggers 			for (i = 0; i < NH_NUM_PASSES; i++)
11426609a21SEric Biggers 				le64_add_cpu(&state->nh_hash[i],
11526609a21SEric Biggers 					     le64_to_cpu(tmp_hash[i]));
11626609a21SEric Biggers 			state->nh_remaining -= bytes;
11726609a21SEric Biggers 		}
11826609a21SEric Biggers 		if (state->nh_remaining == 0)
11926609a21SEric Biggers 			process_nh_hash_value(state, key);
12026609a21SEric Biggers 		src += bytes;
12126609a21SEric Biggers 		srclen -= bytes;
12226609a21SEric Biggers 	} while (srclen);
12326609a21SEric Biggers }
12426609a21SEric Biggers 
12526609a21SEric Biggers int crypto_nhpoly1305_setkey(struct crypto_shash *tfm,
12626609a21SEric Biggers 			     const u8 *key, unsigned int keylen)
12726609a21SEric Biggers {
12826609a21SEric Biggers 	struct nhpoly1305_key *ctx = crypto_shash_ctx(tfm);
12926609a21SEric Biggers 	int i;
13026609a21SEric Biggers 
13126609a21SEric Biggers 	if (keylen != NHPOLY1305_KEY_SIZE)
13226609a21SEric Biggers 		return -EINVAL;
13326609a21SEric Biggers 
13426609a21SEric Biggers 	poly1305_core_setkey(&ctx->poly_key, key);
13526609a21SEric Biggers 	key += POLY1305_BLOCK_SIZE;
13626609a21SEric Biggers 
13726609a21SEric Biggers 	for (i = 0; i < NH_KEY_WORDS; i++)
13826609a21SEric Biggers 		ctx->nh_key[i] = get_unaligned_le32(key + i * sizeof(u32));
13926609a21SEric Biggers 
14026609a21SEric Biggers 	return 0;
14126609a21SEric Biggers }
14226609a21SEric Biggers EXPORT_SYMBOL(crypto_nhpoly1305_setkey);
14326609a21SEric Biggers 
14426609a21SEric Biggers int crypto_nhpoly1305_init(struct shash_desc *desc)
14526609a21SEric Biggers {
14626609a21SEric Biggers 	struct nhpoly1305_state *state = shash_desc_ctx(desc);
14726609a21SEric Biggers 
14826609a21SEric Biggers 	poly1305_core_init(&state->poly_state);
14926609a21SEric Biggers 	state->buflen = 0;
15026609a21SEric Biggers 	state->nh_remaining = 0;
15126609a21SEric Biggers 	return 0;
15226609a21SEric Biggers }
15326609a21SEric Biggers EXPORT_SYMBOL(crypto_nhpoly1305_init);
15426609a21SEric Biggers 
15526609a21SEric Biggers int crypto_nhpoly1305_update_helper(struct shash_desc *desc,
15626609a21SEric Biggers 				    const u8 *src, unsigned int srclen,
15726609a21SEric Biggers 				    nh_t nh_fn)
15826609a21SEric Biggers {
15926609a21SEric Biggers 	struct nhpoly1305_state *state = shash_desc_ctx(desc);
16026609a21SEric Biggers 	const struct nhpoly1305_key *key = crypto_shash_ctx(desc->tfm);
16126609a21SEric Biggers 	unsigned int bytes;
16226609a21SEric Biggers 
16326609a21SEric Biggers 	if (state->buflen) {
16426609a21SEric Biggers 		bytes = min(srclen, (int)NH_MESSAGE_UNIT - state->buflen);
16526609a21SEric Biggers 		memcpy(&state->buffer[state->buflen], src, bytes);
16626609a21SEric Biggers 		state->buflen += bytes;
16726609a21SEric Biggers 		if (state->buflen < NH_MESSAGE_UNIT)
16826609a21SEric Biggers 			return 0;
16926609a21SEric Biggers 		nhpoly1305_units(state, key, state->buffer, NH_MESSAGE_UNIT,
17026609a21SEric Biggers 				 nh_fn);
17126609a21SEric Biggers 		state->buflen = 0;
17226609a21SEric Biggers 		src += bytes;
17326609a21SEric Biggers 		srclen -= bytes;
17426609a21SEric Biggers 	}
17526609a21SEric Biggers 
17626609a21SEric Biggers 	if (srclen >= NH_MESSAGE_UNIT) {
17726609a21SEric Biggers 		bytes = round_down(srclen, NH_MESSAGE_UNIT);
17826609a21SEric Biggers 		nhpoly1305_units(state, key, src, bytes, nh_fn);
17926609a21SEric Biggers 		src += bytes;
18026609a21SEric Biggers 		srclen -= bytes;
18126609a21SEric Biggers 	}
18226609a21SEric Biggers 
18326609a21SEric Biggers 	if (srclen) {
18426609a21SEric Biggers 		memcpy(state->buffer, src, srclen);
18526609a21SEric Biggers 		state->buflen = srclen;
18626609a21SEric Biggers 	}
18726609a21SEric Biggers 	return 0;
18826609a21SEric Biggers }
18926609a21SEric Biggers EXPORT_SYMBOL(crypto_nhpoly1305_update_helper);
19026609a21SEric Biggers 
19126609a21SEric Biggers int crypto_nhpoly1305_update(struct shash_desc *desc,
19226609a21SEric Biggers 			     const u8 *src, unsigned int srclen)
19326609a21SEric Biggers {
19426609a21SEric Biggers 	return crypto_nhpoly1305_update_helper(desc, src, srclen, nh_generic);
19526609a21SEric Biggers }
19626609a21SEric Biggers EXPORT_SYMBOL(crypto_nhpoly1305_update);
19726609a21SEric Biggers 
19826609a21SEric Biggers int crypto_nhpoly1305_final_helper(struct shash_desc *desc, u8 *dst, nh_t nh_fn)
19926609a21SEric Biggers {
20026609a21SEric Biggers 	struct nhpoly1305_state *state = shash_desc_ctx(desc);
20126609a21SEric Biggers 	const struct nhpoly1305_key *key = crypto_shash_ctx(desc->tfm);
20226609a21SEric Biggers 
20326609a21SEric Biggers 	if (state->buflen) {
20426609a21SEric Biggers 		memset(&state->buffer[state->buflen], 0,
20526609a21SEric Biggers 		       NH_MESSAGE_UNIT - state->buflen);
20626609a21SEric Biggers 		nhpoly1305_units(state, key, state->buffer, NH_MESSAGE_UNIT,
20726609a21SEric Biggers 				 nh_fn);
20826609a21SEric Biggers 	}
20926609a21SEric Biggers 
21026609a21SEric Biggers 	if (state->nh_remaining)
21126609a21SEric Biggers 		process_nh_hash_value(state, key);
21226609a21SEric Biggers 
21326609a21SEric Biggers 	poly1305_core_emit(&state->poly_state, dst);
21426609a21SEric Biggers 	return 0;
21526609a21SEric Biggers }
21626609a21SEric Biggers EXPORT_SYMBOL(crypto_nhpoly1305_final_helper);
21726609a21SEric Biggers 
21826609a21SEric Biggers int crypto_nhpoly1305_final(struct shash_desc *desc, u8 *dst)
21926609a21SEric Biggers {
22026609a21SEric Biggers 	return crypto_nhpoly1305_final_helper(desc, dst, nh_generic);
22126609a21SEric Biggers }
22226609a21SEric Biggers EXPORT_SYMBOL(crypto_nhpoly1305_final);
22326609a21SEric Biggers 
22426609a21SEric Biggers static struct shash_alg nhpoly1305_alg = {
22526609a21SEric Biggers 	.base.cra_name		= "nhpoly1305",
22626609a21SEric Biggers 	.base.cra_driver_name	= "nhpoly1305-generic",
22726609a21SEric Biggers 	.base.cra_priority	= 100,
22826609a21SEric Biggers 	.base.cra_ctxsize	= sizeof(struct nhpoly1305_key),
22926609a21SEric Biggers 	.base.cra_module	= THIS_MODULE,
23026609a21SEric Biggers 	.digestsize		= POLY1305_DIGEST_SIZE,
23126609a21SEric Biggers 	.init			= crypto_nhpoly1305_init,
23226609a21SEric Biggers 	.update			= crypto_nhpoly1305_update,
23326609a21SEric Biggers 	.final			= crypto_nhpoly1305_final,
23426609a21SEric Biggers 	.setkey			= crypto_nhpoly1305_setkey,
23526609a21SEric Biggers 	.descsize		= sizeof(struct nhpoly1305_state),
23626609a21SEric Biggers };
23726609a21SEric Biggers 
23826609a21SEric Biggers static int __init nhpoly1305_mod_init(void)
23926609a21SEric Biggers {
24026609a21SEric Biggers 	return crypto_register_shash(&nhpoly1305_alg);
24126609a21SEric Biggers }
24226609a21SEric Biggers 
24326609a21SEric Biggers static void __exit nhpoly1305_mod_exit(void)
24426609a21SEric Biggers {
24526609a21SEric Biggers 	crypto_unregister_shash(&nhpoly1305_alg);
24626609a21SEric Biggers }
24726609a21SEric Biggers 
248c4741b23SEric Biggers subsys_initcall(nhpoly1305_mod_init);
24926609a21SEric Biggers module_exit(nhpoly1305_mod_exit);
25026609a21SEric Biggers 
25126609a21SEric Biggers MODULE_DESCRIPTION("NHPoly1305 ε-almost-∆-universal hash function");
25226609a21SEric Biggers MODULE_LICENSE("GPL v2");
25326609a21SEric Biggers MODULE_AUTHOR("Eric Biggers <ebiggers@google.com>");
25426609a21SEric Biggers MODULE_ALIAS_CRYPTO("nhpoly1305");
25526609a21SEric Biggers MODULE_ALIAS_CRYPTO("nhpoly1305-generic");
256