xref: /linux/include/crypto/nh.h (revision a4e573db06a4e8c519ec4c42f8e1249a0853367a)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * NH hash function for Adiantum
4  */
5 
6 #ifndef _CRYPTO_NH_H
7 #define _CRYPTO_NH_H
8 
9 #include <linux/types.h>
10 
11 /* NH parameterization: */
12 
13 /* Endianness: little */
14 /* Word size: 32 bits (works well on NEON, SSE2, AVX2) */
15 
16 /* Stride: 2 words (optimal on ARM32 NEON; works okay on other CPUs too) */
17 #define NH_PAIR_STRIDE		2
18 #define NH_MESSAGE_UNIT		(NH_PAIR_STRIDE * 2 * sizeof(u32))
19 
20 /* Num passes (Toeplitz iteration count): 4, to give ε = 2^{-128} */
21 #define NH_NUM_PASSES		4
22 #define NH_HASH_BYTES		(NH_NUM_PASSES * sizeof(u64))
23 
24 /* Max message size: 1024 bytes (32x compression factor) */
25 #define NH_NUM_STRIDES		64
26 #define NH_MESSAGE_WORDS	(NH_PAIR_STRIDE * 2 * NH_NUM_STRIDES)
27 #define NH_MESSAGE_BYTES	(NH_MESSAGE_WORDS * sizeof(u32))
28 #define NH_KEY_WORDS		(NH_MESSAGE_WORDS + \
29 				 NH_PAIR_STRIDE * 2 * (NH_NUM_PASSES - 1))
30 #define NH_KEY_BYTES		(NH_KEY_WORDS * sizeof(u32))
31 
32 /**
33  * nh() - NH hash function for Adiantum
34  * @key: The key.  @message_len + 48 bytes of it are used.  This is NH_KEY_BYTES
35  *	 if @message_len has its maximum length of NH_MESSAGE_BYTES.
36  * @message: The message
37  * @message_len: The message length in bytes.  Must be a multiple of 16
38  *		 (NH_MESSAGE_UNIT) and at most 1024 (NH_MESSAGE_BYTES).
39  * @hash: (output) The resulting hash value
40  *
41  * Note: the pseudocode for NH in the Adiantum paper iterates over 1024-byte
42  * segments of the message, computes a 32-byte hash for each, and returns all
43  * the hashes concatenated together.  In contrast, this function just hashes one
44  * segment and returns one hash.  It's the caller's responsibility to call this
45  * function for each 1024-byte segment and collect all the hashes.
46  *
47  * Context: Any context.
48  */
49 void nh(const u32 *key, const u8 *message, size_t message_len,
50 	__le64 hash[NH_NUM_PASSES]);
51 
52 #endif /* _CRYPTO_NH_H */
53