^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) * Common values and helper functions for the NHPoly1305 hash function.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #ifndef _NHPOLY1305_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #define _NHPOLY1305_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <crypto/hash.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <crypto/internal/poly1305.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) /* NH parameterization: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) /* Endianness: little */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) /* Word size: 32 bits (works well on NEON, SSE2, AVX2) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) /* Stride: 2 words (optimal on ARM32 NEON; works okay on other CPUs too) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define NH_PAIR_STRIDE 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define NH_MESSAGE_UNIT (NH_PAIR_STRIDE * 2 * sizeof(u32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) /* Num passes (Toeplitz iteration count): 4, to give ε = 2^{-128} */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #define NH_NUM_PASSES 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define NH_HASH_BYTES (NH_NUM_PASSES * sizeof(u64))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /* Max message size: 1024 bytes (32x compression factor) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #define NH_NUM_STRIDES 64
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define NH_MESSAGE_WORDS (NH_PAIR_STRIDE * 2 * NH_NUM_STRIDES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #define NH_MESSAGE_BYTES (NH_MESSAGE_WORDS * sizeof(u32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define NH_KEY_WORDS (NH_MESSAGE_WORDS + \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) NH_PAIR_STRIDE * 2 * (NH_NUM_PASSES - 1))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #define NH_KEY_BYTES (NH_KEY_WORDS * sizeof(u32))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define NHPOLY1305_KEY_SIZE (POLY1305_BLOCK_SIZE + NH_KEY_BYTES)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct nhpoly1305_key {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) struct poly1305_core_key poly_key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) u32 nh_key[NH_KEY_WORDS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct nhpoly1305_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* Running total of polynomial evaluation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) struct poly1305_state poly_state;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) /* Partial block buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) u8 buffer[NH_MESSAGE_UNIT];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) unsigned int buflen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) * Number of bytes remaining until the current NH message reaches
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) * NH_MESSAGE_BYTES. When nonzero, 'nh_hash' holds the partial NH hash.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) unsigned int nh_remaining;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) __le64 nh_hash[NH_NUM_PASSES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) typedef void (*nh_t)(const u32 *key, const u8 *message, size_t message_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) __le64 hash[NH_NUM_PASSES]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) int crypto_nhpoly1305_setkey(struct crypto_shash *tfm,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) const u8 *key, unsigned int keylen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) int crypto_nhpoly1305_init(struct shash_desc *desc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) int crypto_nhpoly1305_update(struct shash_desc *desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) const u8 *src, unsigned int srclen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) int crypto_nhpoly1305_update_helper(struct shash_desc *desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) const u8 *src, unsigned int srclen,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) nh_t nh_fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) int crypto_nhpoly1305_final(struct shash_desc *desc, u8 *dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) int crypto_nhpoly1305_final_helper(struct shash_desc *desc, u8 *dst,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) nh_t nh_fn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) #endif /* _NHPOLY1305_H */