1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * NHPoly1305 - ε-almost-∆-universal hash function for Adiantum
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright 2018 Google LLC
6*4882a593Smuzhiyun */
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun /*
9*4882a593Smuzhiyun * "NHPoly1305" is the main component of Adiantum hashing.
10*4882a593Smuzhiyun * Specifically, it is the calculation
11*4882a593Smuzhiyun *
12*4882a593Smuzhiyun * H_L ← Poly1305_{K_L}(NH_{K_N}(pad_{128}(L)))
13*4882a593Smuzhiyun *
14*4882a593Smuzhiyun * from the procedure in section 6.4 of the Adiantum paper [1]. It is an
15*4882a593Smuzhiyun * ε-almost-∆-universal (ε-∆U) hash function for equal-length inputs over
16*4882a593Smuzhiyun * Z/(2^{128}Z), where the "∆" operation is addition. It hashes 1024-byte
17*4882a593Smuzhiyun * chunks of the input with the NH hash function [2], reducing the input length
18*4882a593Smuzhiyun * by 32x. The resulting NH digests are evaluated as a polynomial in
19*4882a593Smuzhiyun * GF(2^{130}-5), like in the Poly1305 MAC [3]. Note that the polynomial
20*4882a593Smuzhiyun * evaluation by itself would suffice to achieve the ε-∆U property; NH is used
21*4882a593Smuzhiyun * for performance since it's over twice as fast as Poly1305.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * This is *not* a cryptographic hash function; do not use it as such!
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * [1] Adiantum: length-preserving encryption for entry-level processors
26*4882a593Smuzhiyun * (https://eprint.iacr.org/2018/720.pdf)
27*4882a593Smuzhiyun * [2] UMAC: Fast and Secure Message Authentication
28*4882a593Smuzhiyun * (https://fastcrypto.org/umac/umac_proc.pdf)
29*4882a593Smuzhiyun * [3] The Poly1305-AES message-authentication code
30*4882a593Smuzhiyun * (https://cr.yp.to/mac/poly1305-20050329.pdf)
31*4882a593Smuzhiyun */
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #include <asm/unaligned.h>
34*4882a593Smuzhiyun #include <crypto/algapi.h>
35*4882a593Smuzhiyun #include <crypto/internal/hash.h>
36*4882a593Smuzhiyun #include <crypto/internal/poly1305.h>
37*4882a593Smuzhiyun #include <crypto/nhpoly1305.h>
38*4882a593Smuzhiyun #include <linux/crypto.h>
39*4882a593Smuzhiyun #include <linux/kernel.h>
40*4882a593Smuzhiyun #include <linux/module.h>
41*4882a593Smuzhiyun
nh_generic(const u32 * key,const u8 * message,size_t message_len,__le64 hash[NH_NUM_PASSES])42*4882a593Smuzhiyun static void nh_generic(const u32 *key, const u8 *message, size_t message_len,
43*4882a593Smuzhiyun __le64 hash[NH_NUM_PASSES])
44*4882a593Smuzhiyun {
45*4882a593Smuzhiyun u64 sums[4] = { 0, 0, 0, 0 };
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun BUILD_BUG_ON(NH_PAIR_STRIDE != 2);
48*4882a593Smuzhiyun BUILD_BUG_ON(NH_NUM_PASSES != 4);
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun while (message_len) {
51*4882a593Smuzhiyun u32 m0 = get_unaligned_le32(message + 0);
52*4882a593Smuzhiyun u32 m1 = get_unaligned_le32(message + 4);
53*4882a593Smuzhiyun u32 m2 = get_unaligned_le32(message + 8);
54*4882a593Smuzhiyun u32 m3 = get_unaligned_le32(message + 12);
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun sums[0] += (u64)(u32)(m0 + key[ 0]) * (u32)(m2 + key[ 2]);
57*4882a593Smuzhiyun sums[1] += (u64)(u32)(m0 + key[ 4]) * (u32)(m2 + key[ 6]);
58*4882a593Smuzhiyun sums[2] += (u64)(u32)(m0 + key[ 8]) * (u32)(m2 + key[10]);
59*4882a593Smuzhiyun sums[3] += (u64)(u32)(m0 + key[12]) * (u32)(m2 + key[14]);
60*4882a593Smuzhiyun sums[0] += (u64)(u32)(m1 + key[ 1]) * (u32)(m3 + key[ 3]);
61*4882a593Smuzhiyun sums[1] += (u64)(u32)(m1 + key[ 5]) * (u32)(m3 + key[ 7]);
62*4882a593Smuzhiyun sums[2] += (u64)(u32)(m1 + key[ 9]) * (u32)(m3 + key[11]);
63*4882a593Smuzhiyun sums[3] += (u64)(u32)(m1 + key[13]) * (u32)(m3 + key[15]);
64*4882a593Smuzhiyun key += NH_MESSAGE_UNIT / sizeof(key[0]);
65*4882a593Smuzhiyun message += NH_MESSAGE_UNIT;
66*4882a593Smuzhiyun message_len -= NH_MESSAGE_UNIT;
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun hash[0] = cpu_to_le64(sums[0]);
70*4882a593Smuzhiyun hash[1] = cpu_to_le64(sums[1]);
71*4882a593Smuzhiyun hash[2] = cpu_to_le64(sums[2]);
72*4882a593Smuzhiyun hash[3] = cpu_to_le64(sums[3]);
73*4882a593Smuzhiyun }
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun /* Pass the next NH hash value through Poly1305 */
process_nh_hash_value(struct nhpoly1305_state * state,const struct nhpoly1305_key * key)76*4882a593Smuzhiyun static void process_nh_hash_value(struct nhpoly1305_state *state,
77*4882a593Smuzhiyun const struct nhpoly1305_key *key)
78*4882a593Smuzhiyun {
79*4882a593Smuzhiyun BUILD_BUG_ON(NH_HASH_BYTES % POLY1305_BLOCK_SIZE != 0);
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun poly1305_core_blocks(&state->poly_state, &key->poly_key, state->nh_hash,
82*4882a593Smuzhiyun NH_HASH_BYTES / POLY1305_BLOCK_SIZE, 1);
83*4882a593Smuzhiyun }
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun /*
86*4882a593Smuzhiyun * Feed the next portion of the source data, as a whole number of 16-byte
87*4882a593Smuzhiyun * "NH message units", through NH and Poly1305. Each NH hash is taken over
88*4882a593Smuzhiyun * 1024 bytes, except possibly the final one which is taken over a multiple of
89*4882a593Smuzhiyun * 16 bytes up to 1024. Also, in the case where data is passed in misaligned
90*4882a593Smuzhiyun * chunks, we combine partial hashes; the end result is the same either way.
91*4882a593Smuzhiyun */
nhpoly1305_units(struct nhpoly1305_state * state,const struct nhpoly1305_key * key,const u8 * src,unsigned int srclen,nh_t nh_fn)92*4882a593Smuzhiyun static void nhpoly1305_units(struct nhpoly1305_state *state,
93*4882a593Smuzhiyun const struct nhpoly1305_key *key,
94*4882a593Smuzhiyun const u8 *src, unsigned int srclen, nh_t nh_fn)
95*4882a593Smuzhiyun {
96*4882a593Smuzhiyun do {
97*4882a593Smuzhiyun unsigned int bytes;
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun if (state->nh_remaining == 0) {
100*4882a593Smuzhiyun /* Starting a new NH message */
101*4882a593Smuzhiyun bytes = min_t(unsigned int, srclen, NH_MESSAGE_BYTES);
102*4882a593Smuzhiyun nh_fn(key->nh_key, src, bytes, state->nh_hash);
103*4882a593Smuzhiyun state->nh_remaining = NH_MESSAGE_BYTES - bytes;
104*4882a593Smuzhiyun } else {
105*4882a593Smuzhiyun /* Continuing a previous NH message */
106*4882a593Smuzhiyun __le64 tmp_hash[NH_NUM_PASSES];
107*4882a593Smuzhiyun unsigned int pos;
108*4882a593Smuzhiyun int i;
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun pos = NH_MESSAGE_BYTES - state->nh_remaining;
111*4882a593Smuzhiyun bytes = min(srclen, state->nh_remaining);
112*4882a593Smuzhiyun nh_fn(&key->nh_key[pos / 4], src, bytes, tmp_hash);
113*4882a593Smuzhiyun for (i = 0; i < NH_NUM_PASSES; i++)
114*4882a593Smuzhiyun le64_add_cpu(&state->nh_hash[i],
115*4882a593Smuzhiyun le64_to_cpu(tmp_hash[i]));
116*4882a593Smuzhiyun state->nh_remaining -= bytes;
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun if (state->nh_remaining == 0)
119*4882a593Smuzhiyun process_nh_hash_value(state, key);
120*4882a593Smuzhiyun src += bytes;
121*4882a593Smuzhiyun srclen -= bytes;
122*4882a593Smuzhiyun } while (srclen);
123*4882a593Smuzhiyun }
124*4882a593Smuzhiyun
crypto_nhpoly1305_setkey(struct crypto_shash * tfm,const u8 * key,unsigned int keylen)125*4882a593Smuzhiyun int crypto_nhpoly1305_setkey(struct crypto_shash *tfm,
126*4882a593Smuzhiyun const u8 *key, unsigned int keylen)
127*4882a593Smuzhiyun {
128*4882a593Smuzhiyun struct nhpoly1305_key *ctx = crypto_shash_ctx(tfm);
129*4882a593Smuzhiyun int i;
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun if (keylen != NHPOLY1305_KEY_SIZE)
132*4882a593Smuzhiyun return -EINVAL;
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun poly1305_core_setkey(&ctx->poly_key, key);
135*4882a593Smuzhiyun key += POLY1305_BLOCK_SIZE;
136*4882a593Smuzhiyun
137*4882a593Smuzhiyun for (i = 0; i < NH_KEY_WORDS; i++)
138*4882a593Smuzhiyun ctx->nh_key[i] = get_unaligned_le32(key + i * sizeof(u32));
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun return 0;
141*4882a593Smuzhiyun }
142*4882a593Smuzhiyun EXPORT_SYMBOL(crypto_nhpoly1305_setkey);
143*4882a593Smuzhiyun
crypto_nhpoly1305_init(struct shash_desc * desc)144*4882a593Smuzhiyun int crypto_nhpoly1305_init(struct shash_desc *desc)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun struct nhpoly1305_state *state = shash_desc_ctx(desc);
147*4882a593Smuzhiyun
148*4882a593Smuzhiyun poly1305_core_init(&state->poly_state);
149*4882a593Smuzhiyun state->buflen = 0;
150*4882a593Smuzhiyun state->nh_remaining = 0;
151*4882a593Smuzhiyun return 0;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun EXPORT_SYMBOL(crypto_nhpoly1305_init);
154*4882a593Smuzhiyun
crypto_nhpoly1305_update_helper(struct shash_desc * desc,const u8 * src,unsigned int srclen,nh_t nh_fn)155*4882a593Smuzhiyun int crypto_nhpoly1305_update_helper(struct shash_desc *desc,
156*4882a593Smuzhiyun const u8 *src, unsigned int srclen,
157*4882a593Smuzhiyun nh_t nh_fn)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun struct nhpoly1305_state *state = shash_desc_ctx(desc);
160*4882a593Smuzhiyun const struct nhpoly1305_key *key = crypto_shash_ctx(desc->tfm);
161*4882a593Smuzhiyun unsigned int bytes;
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun if (state->buflen) {
164*4882a593Smuzhiyun bytes = min(srclen, (int)NH_MESSAGE_UNIT - state->buflen);
165*4882a593Smuzhiyun memcpy(&state->buffer[state->buflen], src, bytes);
166*4882a593Smuzhiyun state->buflen += bytes;
167*4882a593Smuzhiyun if (state->buflen < NH_MESSAGE_UNIT)
168*4882a593Smuzhiyun return 0;
169*4882a593Smuzhiyun nhpoly1305_units(state, key, state->buffer, NH_MESSAGE_UNIT,
170*4882a593Smuzhiyun nh_fn);
171*4882a593Smuzhiyun state->buflen = 0;
172*4882a593Smuzhiyun src += bytes;
173*4882a593Smuzhiyun srclen -= bytes;
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun if (srclen >= NH_MESSAGE_UNIT) {
177*4882a593Smuzhiyun bytes = round_down(srclen, NH_MESSAGE_UNIT);
178*4882a593Smuzhiyun nhpoly1305_units(state, key, src, bytes, nh_fn);
179*4882a593Smuzhiyun src += bytes;
180*4882a593Smuzhiyun srclen -= bytes;
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun if (srclen) {
184*4882a593Smuzhiyun memcpy(state->buffer, src, srclen);
185*4882a593Smuzhiyun state->buflen = srclen;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun return 0;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun EXPORT_SYMBOL(crypto_nhpoly1305_update_helper);
190*4882a593Smuzhiyun
crypto_nhpoly1305_update(struct shash_desc * desc,const u8 * src,unsigned int srclen)191*4882a593Smuzhiyun int crypto_nhpoly1305_update(struct shash_desc *desc,
192*4882a593Smuzhiyun const u8 *src, unsigned int srclen)
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun return crypto_nhpoly1305_update_helper(desc, src, srclen, nh_generic);
195*4882a593Smuzhiyun }
196*4882a593Smuzhiyun EXPORT_SYMBOL(crypto_nhpoly1305_update);
197*4882a593Smuzhiyun
crypto_nhpoly1305_final_helper(struct shash_desc * desc,u8 * dst,nh_t nh_fn)198*4882a593Smuzhiyun int crypto_nhpoly1305_final_helper(struct shash_desc *desc, u8 *dst, nh_t nh_fn)
199*4882a593Smuzhiyun {
200*4882a593Smuzhiyun struct nhpoly1305_state *state = shash_desc_ctx(desc);
201*4882a593Smuzhiyun const struct nhpoly1305_key *key = crypto_shash_ctx(desc->tfm);
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun if (state->buflen) {
204*4882a593Smuzhiyun memset(&state->buffer[state->buflen], 0,
205*4882a593Smuzhiyun NH_MESSAGE_UNIT - state->buflen);
206*4882a593Smuzhiyun nhpoly1305_units(state, key, state->buffer, NH_MESSAGE_UNIT,
207*4882a593Smuzhiyun nh_fn);
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun if (state->nh_remaining)
211*4882a593Smuzhiyun process_nh_hash_value(state, key);
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun poly1305_core_emit(&state->poly_state, NULL, dst);
214*4882a593Smuzhiyun return 0;
215*4882a593Smuzhiyun }
216*4882a593Smuzhiyun EXPORT_SYMBOL(crypto_nhpoly1305_final_helper);
217*4882a593Smuzhiyun
crypto_nhpoly1305_final(struct shash_desc * desc,u8 * dst)218*4882a593Smuzhiyun int crypto_nhpoly1305_final(struct shash_desc *desc, u8 *dst)
219*4882a593Smuzhiyun {
220*4882a593Smuzhiyun return crypto_nhpoly1305_final_helper(desc, dst, nh_generic);
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun EXPORT_SYMBOL(crypto_nhpoly1305_final);
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun static struct shash_alg nhpoly1305_alg = {
225*4882a593Smuzhiyun .base.cra_name = "nhpoly1305",
226*4882a593Smuzhiyun .base.cra_driver_name = "nhpoly1305-generic",
227*4882a593Smuzhiyun .base.cra_priority = 100,
228*4882a593Smuzhiyun .base.cra_ctxsize = sizeof(struct nhpoly1305_key),
229*4882a593Smuzhiyun .base.cra_module = THIS_MODULE,
230*4882a593Smuzhiyun .digestsize = POLY1305_DIGEST_SIZE,
231*4882a593Smuzhiyun .init = crypto_nhpoly1305_init,
232*4882a593Smuzhiyun .update = crypto_nhpoly1305_update,
233*4882a593Smuzhiyun .final = crypto_nhpoly1305_final,
234*4882a593Smuzhiyun .setkey = crypto_nhpoly1305_setkey,
235*4882a593Smuzhiyun .descsize = sizeof(struct nhpoly1305_state),
236*4882a593Smuzhiyun };
237*4882a593Smuzhiyun
nhpoly1305_mod_init(void)238*4882a593Smuzhiyun static int __init nhpoly1305_mod_init(void)
239*4882a593Smuzhiyun {
240*4882a593Smuzhiyun return crypto_register_shash(&nhpoly1305_alg);
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun
nhpoly1305_mod_exit(void)243*4882a593Smuzhiyun static void __exit nhpoly1305_mod_exit(void)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun crypto_unregister_shash(&nhpoly1305_alg);
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun subsys_initcall(nhpoly1305_mod_init);
249*4882a593Smuzhiyun module_exit(nhpoly1305_mod_exit);
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun MODULE_DESCRIPTION("NHPoly1305 ε-almost-∆-universal hash function");
252*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
253*4882a593Smuzhiyun MODULE_AUTHOR("Eric Biggers <ebiggers@google.com>");
254*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("nhpoly1305");
255*4882a593Smuzhiyun MODULE_ALIAS_CRYPTO("nhpoly1305-generic");
256