^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 for the Poly1305 algorithm
^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 _CRYPTO_POLY1305_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #define _CRYPTO_POLY1305_H
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/crypto.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #define POLY1305_BLOCK_SIZE 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #define POLY1305_KEY_SIZE 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #define POLY1305_DIGEST_SIZE 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) /* The poly1305_key and poly1305_state types are mostly opaque and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) * implementation-defined. Limbs might be in base 2^64 or base 2^26, or
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) * different yet. The union type provided keeps these 64-bit aligned for the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) * case in which this is implemented using 64x64 multiplies.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) struct poly1305_key {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) u32 r[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) u64 r64[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) struct poly1305_core_key {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) struct poly1305_key key;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct poly1305_key precomputed_s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct poly1305_state {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) u32 h[5];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) u64 h64[3];
^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)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) struct poly1305_desc_ctx {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) /* partial buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) u8 buf[POLY1305_BLOCK_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) /* bytes used in partial buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) unsigned int buflen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* how many keys have been set in r[] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) unsigned short rset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) /* whether s[] has been set */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) bool sset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) /* finalize key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) u32 s[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) /* accumulator */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct poly1305_state h;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) /* key */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) union {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) struct poly1305_key opaque_r[CONFIG_CRYPTO_LIB_POLY1305_RSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct poly1305_core_key core_r;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) void poly1305_init_arch(struct poly1305_desc_ctx *desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) const u8 key[POLY1305_KEY_SIZE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) void poly1305_init_generic(struct poly1305_desc_ctx *desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) const u8 key[POLY1305_KEY_SIZE]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static inline void poly1305_init(struct poly1305_desc_ctx *desc, const u8 *key)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_POLY1305))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) poly1305_init_arch(desc, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) poly1305_init_generic(desc, key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) void poly1305_update_arch(struct poly1305_desc_ctx *desc, const u8 *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) unsigned int nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) void poly1305_update_generic(struct poly1305_desc_ctx *desc, const u8 *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) unsigned int nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static inline void poly1305_update(struct poly1305_desc_ctx *desc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) const u8 *src, unsigned int nbytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_POLY1305))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) poly1305_update_arch(desc, src, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) poly1305_update_generic(desc, src, nbytes);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) void poly1305_final_arch(struct poly1305_desc_ctx *desc, u8 *digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) void poly1305_final_generic(struct poly1305_desc_ctx *desc, u8 *digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) static inline void poly1305_final(struct poly1305_desc_ctx *desc, u8 *digest)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_POLY1305))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) poly1305_final_arch(desc, digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) poly1305_final_generic(desc, digest);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) #endif