xref: /linux/crypto/nhpoly1305.c (revision c6018e1a00b5c70610cdfb3650cc5622c917ed17)
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  *
12*c6018e1aSEric Biggers  *	H_L ← Poly1305_{K_L}(NH_{K_N}(pad_{128}(L)))
1326609a21SEric Biggers  *
14*c6018e1aSEric Biggers  * from the procedure in section 6.4 of the Adiantum paper [1].  It is an
15*c6018e1aSEric 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
20*c6018e1aSEric 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>
3626609a21SEric Biggers #include <crypto/nhpoly1305.h>
3726609a21SEric Biggers #include <linux/crypto.h>
3826609a21SEric Biggers #include <linux/kernel.h>
3926609a21SEric Biggers #include <linux/module.h>
4026609a21SEric Biggers 
4126609a21SEric Biggers static void nh_generic(const u32 *key, const u8 *message, size_t message_len,
4226609a21SEric Biggers 		       __le64 hash[NH_NUM_PASSES])
4326609a21SEric Biggers {
4426609a21SEric Biggers 	u64 sums[4] = { 0, 0, 0, 0 };
4526609a21SEric Biggers 
4626609a21SEric Biggers 	BUILD_BUG_ON(NH_PAIR_STRIDE != 2);
4726609a21SEric Biggers 	BUILD_BUG_ON(NH_NUM_PASSES != 4);
4826609a21SEric Biggers 
4926609a21SEric Biggers 	while (message_len) {
5026609a21SEric Biggers 		u32 m0 = get_unaligned_le32(message + 0);
5126609a21SEric Biggers 		u32 m1 = get_unaligned_le32(message + 4);
5226609a21SEric Biggers 		u32 m2 = get_unaligned_le32(message + 8);
5326609a21SEric Biggers 		u32 m3 = get_unaligned_le32(message + 12);
5426609a21SEric Biggers 
5526609a21SEric Biggers 		sums[0] += (u64)(u32)(m0 + key[ 0]) * (u32)(m2 + key[ 2]);
5626609a21SEric Biggers 		sums[1] += (u64)(u32)(m0 + key[ 4]) * (u32)(m2 + key[ 6]);
5726609a21SEric Biggers 		sums[2] += (u64)(u32)(m0 + key[ 8]) * (u32)(m2 + key[10]);
5826609a21SEric Biggers 		sums[3] += (u64)(u32)(m0 + key[12]) * (u32)(m2 + key[14]);
5926609a21SEric Biggers 		sums[0] += (u64)(u32)(m1 + key[ 1]) * (u32)(m3 + key[ 3]);
6026609a21SEric Biggers 		sums[1] += (u64)(u32)(m1 + key[ 5]) * (u32)(m3 + key[ 7]);
6126609a21SEric Biggers 		sums[2] += (u64)(u32)(m1 + key[ 9]) * (u32)(m3 + key[11]);
6226609a21SEric Biggers 		sums[3] += (u64)(u32)(m1 + key[13]) * (u32)(m3 + key[15]);
6326609a21SEric Biggers 		key += NH_MESSAGE_UNIT / sizeof(key[0]);
6426609a21SEric Biggers 		message += NH_MESSAGE_UNIT;
6526609a21SEric Biggers 		message_len -= NH_MESSAGE_UNIT;
6626609a21SEric Biggers 	}
6726609a21SEric Biggers 
6826609a21SEric Biggers 	hash[0] = cpu_to_le64(sums[0]);
6926609a21SEric Biggers 	hash[1] = cpu_to_le64(sums[1]);
7026609a21SEric Biggers 	hash[2] = cpu_to_le64(sums[2]);
7126609a21SEric Biggers 	hash[3] = cpu_to_le64(sums[3]);
7226609a21SEric Biggers }
7326609a21SEric Biggers 
7426609a21SEric Biggers /* Pass the next NH hash value through Poly1305 */
7526609a21SEric Biggers static void process_nh_hash_value(struct nhpoly1305_state *state,
7626609a21SEric Biggers 				  const struct nhpoly1305_key *key)
7726609a21SEric Biggers {
7826609a21SEric Biggers 	BUILD_BUG_ON(NH_HASH_BYTES % POLY1305_BLOCK_SIZE != 0);
7926609a21SEric Biggers 
8026609a21SEric Biggers 	poly1305_core_blocks(&state->poly_state, &key->poly_key, state->nh_hash,
8126609a21SEric Biggers 			     NH_HASH_BYTES / POLY1305_BLOCK_SIZE);
8226609a21SEric Biggers }
8326609a21SEric Biggers 
8426609a21SEric Biggers /*
8526609a21SEric Biggers  * Feed the next portion of the source data, as a whole number of 16-byte
8626609a21SEric Biggers  * "NH message units", through NH and Poly1305.  Each NH hash is taken over
8726609a21SEric Biggers  * 1024 bytes, except possibly the final one which is taken over a multiple of
8826609a21SEric Biggers  * 16 bytes up to 1024.  Also, in the case where data is passed in misaligned
8926609a21SEric Biggers  * chunks, we combine partial hashes; the end result is the same either way.
9026609a21SEric Biggers  */
9126609a21SEric Biggers static void nhpoly1305_units(struct nhpoly1305_state *state,
9226609a21SEric Biggers 			     const struct nhpoly1305_key *key,
9326609a21SEric Biggers 			     const u8 *src, unsigned int srclen, nh_t nh_fn)
9426609a21SEric Biggers {
9526609a21SEric Biggers 	do {
9626609a21SEric Biggers 		unsigned int bytes;
9726609a21SEric Biggers 
9826609a21SEric Biggers 		if (state->nh_remaining == 0) {
9926609a21SEric Biggers 			/* Starting a new NH message */
10026609a21SEric Biggers 			bytes = min_t(unsigned int, srclen, NH_MESSAGE_BYTES);
10126609a21SEric Biggers 			nh_fn(key->nh_key, src, bytes, state->nh_hash);
10226609a21SEric Biggers 			state->nh_remaining = NH_MESSAGE_BYTES - bytes;
10326609a21SEric Biggers 		} else {
10426609a21SEric Biggers 			/* Continuing a previous NH message */
10526609a21SEric Biggers 			__le64 tmp_hash[NH_NUM_PASSES];
10626609a21SEric Biggers 			unsigned int pos;
10726609a21SEric Biggers 			int i;
10826609a21SEric Biggers 
10926609a21SEric Biggers 			pos = NH_MESSAGE_BYTES - state->nh_remaining;
11026609a21SEric Biggers 			bytes = min(srclen, state->nh_remaining);
11126609a21SEric Biggers 			nh_fn(&key->nh_key[pos / 4], src, bytes, tmp_hash);
11226609a21SEric Biggers 			for (i = 0; i < NH_NUM_PASSES; i++)
11326609a21SEric Biggers 				le64_add_cpu(&state->nh_hash[i],
11426609a21SEric Biggers 					     le64_to_cpu(tmp_hash[i]));
11526609a21SEric Biggers 			state->nh_remaining -= bytes;
11626609a21SEric Biggers 		}
11726609a21SEric Biggers 		if (state->nh_remaining == 0)
11826609a21SEric Biggers 			process_nh_hash_value(state, key);
11926609a21SEric Biggers 		src += bytes;
12026609a21SEric Biggers 		srclen -= bytes;
12126609a21SEric Biggers 	} while (srclen);
12226609a21SEric Biggers }
12326609a21SEric Biggers 
12426609a21SEric Biggers int crypto_nhpoly1305_setkey(struct crypto_shash *tfm,
12526609a21SEric Biggers 			     const u8 *key, unsigned int keylen)
12626609a21SEric Biggers {
12726609a21SEric Biggers 	struct nhpoly1305_key *ctx = crypto_shash_ctx(tfm);
12826609a21SEric Biggers 	int i;
12926609a21SEric Biggers 
13026609a21SEric Biggers 	if (keylen != NHPOLY1305_KEY_SIZE)
13126609a21SEric Biggers 		return -EINVAL;
13226609a21SEric Biggers 
13326609a21SEric Biggers 	poly1305_core_setkey(&ctx->poly_key, key);
13426609a21SEric Biggers 	key += POLY1305_BLOCK_SIZE;
13526609a21SEric Biggers 
13626609a21SEric Biggers 	for (i = 0; i < NH_KEY_WORDS; i++)
13726609a21SEric Biggers 		ctx->nh_key[i] = get_unaligned_le32(key + i * sizeof(u32));
13826609a21SEric Biggers 
13926609a21SEric Biggers 	return 0;
14026609a21SEric Biggers }
14126609a21SEric Biggers EXPORT_SYMBOL(crypto_nhpoly1305_setkey);
14226609a21SEric Biggers 
14326609a21SEric Biggers int crypto_nhpoly1305_init(struct shash_desc *desc)
14426609a21SEric Biggers {
14526609a21SEric Biggers 	struct nhpoly1305_state *state = shash_desc_ctx(desc);
14626609a21SEric Biggers 
14726609a21SEric Biggers 	poly1305_core_init(&state->poly_state);
14826609a21SEric Biggers 	state->buflen = 0;
14926609a21SEric Biggers 	state->nh_remaining = 0;
15026609a21SEric Biggers 	return 0;
15126609a21SEric Biggers }
15226609a21SEric Biggers EXPORT_SYMBOL(crypto_nhpoly1305_init);
15326609a21SEric Biggers 
15426609a21SEric Biggers int crypto_nhpoly1305_update_helper(struct shash_desc *desc,
15526609a21SEric Biggers 				    const u8 *src, unsigned int srclen,
15626609a21SEric Biggers 				    nh_t nh_fn)
15726609a21SEric Biggers {
15826609a21SEric Biggers 	struct nhpoly1305_state *state = shash_desc_ctx(desc);
15926609a21SEric Biggers 	const struct nhpoly1305_key *key = crypto_shash_ctx(desc->tfm);
16026609a21SEric Biggers 	unsigned int bytes;
16126609a21SEric Biggers 
16226609a21SEric Biggers 	if (state->buflen) {
16326609a21SEric Biggers 		bytes = min(srclen, (int)NH_MESSAGE_UNIT - state->buflen);
16426609a21SEric Biggers 		memcpy(&state->buffer[state->buflen], src, bytes);
16526609a21SEric Biggers 		state->buflen += bytes;
16626609a21SEric Biggers 		if (state->buflen < NH_MESSAGE_UNIT)
16726609a21SEric Biggers 			return 0;
16826609a21SEric Biggers 		nhpoly1305_units(state, key, state->buffer, NH_MESSAGE_UNIT,
16926609a21SEric Biggers 				 nh_fn);
17026609a21SEric Biggers 		state->buflen = 0;
17126609a21SEric Biggers 		src += bytes;
17226609a21SEric Biggers 		srclen -= bytes;
17326609a21SEric Biggers 	}
17426609a21SEric Biggers 
17526609a21SEric Biggers 	if (srclen >= NH_MESSAGE_UNIT) {
17626609a21SEric Biggers 		bytes = round_down(srclen, NH_MESSAGE_UNIT);
17726609a21SEric Biggers 		nhpoly1305_units(state, key, src, bytes, nh_fn);
17826609a21SEric Biggers 		src += bytes;
17926609a21SEric Biggers 		srclen -= bytes;
18026609a21SEric Biggers 	}
18126609a21SEric Biggers 
18226609a21SEric Biggers 	if (srclen) {
18326609a21SEric Biggers 		memcpy(state->buffer, src, srclen);
18426609a21SEric Biggers 		state->buflen = srclen;
18526609a21SEric Biggers 	}
18626609a21SEric Biggers 	return 0;
18726609a21SEric Biggers }
18826609a21SEric Biggers EXPORT_SYMBOL(crypto_nhpoly1305_update_helper);
18926609a21SEric Biggers 
19026609a21SEric Biggers int crypto_nhpoly1305_update(struct shash_desc *desc,
19126609a21SEric Biggers 			     const u8 *src, unsigned int srclen)
19226609a21SEric Biggers {
19326609a21SEric Biggers 	return crypto_nhpoly1305_update_helper(desc, src, srclen, nh_generic);
19426609a21SEric Biggers }
19526609a21SEric Biggers EXPORT_SYMBOL(crypto_nhpoly1305_update);
19626609a21SEric Biggers 
19726609a21SEric Biggers int crypto_nhpoly1305_final_helper(struct shash_desc *desc, u8 *dst, nh_t nh_fn)
19826609a21SEric Biggers {
19926609a21SEric Biggers 	struct nhpoly1305_state *state = shash_desc_ctx(desc);
20026609a21SEric Biggers 	const struct nhpoly1305_key *key = crypto_shash_ctx(desc->tfm);
20126609a21SEric Biggers 
20226609a21SEric Biggers 	if (state->buflen) {
20326609a21SEric Biggers 		memset(&state->buffer[state->buflen], 0,
20426609a21SEric Biggers 		       NH_MESSAGE_UNIT - state->buflen);
20526609a21SEric Biggers 		nhpoly1305_units(state, key, state->buffer, NH_MESSAGE_UNIT,
20626609a21SEric Biggers 				 nh_fn);
20726609a21SEric Biggers 	}
20826609a21SEric Biggers 
20926609a21SEric Biggers 	if (state->nh_remaining)
21026609a21SEric Biggers 		process_nh_hash_value(state, key);
21126609a21SEric Biggers 
21226609a21SEric Biggers 	poly1305_core_emit(&state->poly_state, dst);
21326609a21SEric Biggers 	return 0;
21426609a21SEric Biggers }
21526609a21SEric Biggers EXPORT_SYMBOL(crypto_nhpoly1305_final_helper);
21626609a21SEric Biggers 
21726609a21SEric Biggers int crypto_nhpoly1305_final(struct shash_desc *desc, u8 *dst)
21826609a21SEric Biggers {
21926609a21SEric Biggers 	return crypto_nhpoly1305_final_helper(desc, dst, nh_generic);
22026609a21SEric Biggers }
22126609a21SEric Biggers EXPORT_SYMBOL(crypto_nhpoly1305_final);
22226609a21SEric Biggers 
22326609a21SEric Biggers static struct shash_alg nhpoly1305_alg = {
22426609a21SEric Biggers 	.base.cra_name		= "nhpoly1305",
22526609a21SEric Biggers 	.base.cra_driver_name	= "nhpoly1305-generic",
22626609a21SEric Biggers 	.base.cra_priority	= 100,
22726609a21SEric Biggers 	.base.cra_ctxsize	= sizeof(struct nhpoly1305_key),
22826609a21SEric Biggers 	.base.cra_module	= THIS_MODULE,
22926609a21SEric Biggers 	.digestsize		= POLY1305_DIGEST_SIZE,
23026609a21SEric Biggers 	.init			= crypto_nhpoly1305_init,
23126609a21SEric Biggers 	.update			= crypto_nhpoly1305_update,
23226609a21SEric Biggers 	.final			= crypto_nhpoly1305_final,
23326609a21SEric Biggers 	.setkey			= crypto_nhpoly1305_setkey,
23426609a21SEric Biggers 	.descsize		= sizeof(struct nhpoly1305_state),
23526609a21SEric Biggers };
23626609a21SEric Biggers 
23726609a21SEric Biggers static int __init nhpoly1305_mod_init(void)
23826609a21SEric Biggers {
23926609a21SEric Biggers 	return crypto_register_shash(&nhpoly1305_alg);
24026609a21SEric Biggers }
24126609a21SEric Biggers 
24226609a21SEric Biggers static void __exit nhpoly1305_mod_exit(void)
24326609a21SEric Biggers {
24426609a21SEric Biggers 	crypto_unregister_shash(&nhpoly1305_alg);
24526609a21SEric Biggers }
24626609a21SEric Biggers 
24726609a21SEric Biggers module_init(nhpoly1305_mod_init);
24826609a21SEric Biggers module_exit(nhpoly1305_mod_exit);
24926609a21SEric Biggers 
25026609a21SEric Biggers MODULE_DESCRIPTION("NHPoly1305 ε-almost-∆-universal hash function");
25126609a21SEric Biggers MODULE_LICENSE("GPL v2");
25226609a21SEric Biggers MODULE_AUTHOR("Eric Biggers <ebiggers@google.com>");
25326609a21SEric Biggers MODULE_ALIAS_CRYPTO("nhpoly1305");
25426609a21SEric Biggers MODULE_ALIAS_CRYPTO("nhpoly1305-generic");
255