xref: /openbmc/linux/include/crypto/poly1305.h (revision 8d195e7a)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
22546f811SMartin Willi /*
32546f811SMartin Willi  * Common values for the Poly1305 algorithm
42546f811SMartin Willi  */
52546f811SMartin Willi 
62546f811SMartin Willi #ifndef _CRYPTO_POLY1305_H
72546f811SMartin Willi #define _CRYPTO_POLY1305_H
82546f811SMartin Willi 
92546f811SMartin Willi #include <linux/types.h>
102546f811SMartin Willi #include <linux/crypto.h>
112546f811SMartin Willi 
122546f811SMartin Willi #define POLY1305_BLOCK_SIZE	16
132546f811SMartin Willi #define POLY1305_KEY_SIZE	32
142546f811SMartin Willi #define POLY1305_DIGEST_SIZE	16
152546f811SMartin Willi 
161c08a104SJason A. Donenfeld /* The poly1305_key and poly1305_state types are mostly opaque and
171c08a104SJason A. Donenfeld  * implementation-defined. Limbs might be in base 2^64 or base 2^26, or
181c08a104SJason A. Donenfeld  * different yet. The union type provided keeps these 64-bit aligned for the
191c08a104SJason A. Donenfeld  * case in which this is implemented using 64x64 multiplies.
201c08a104SJason A. Donenfeld  */
211c08a104SJason A. Donenfeld 
22878afc35SEric Biggers struct poly1305_key {
231c08a104SJason A. Donenfeld 	union {
241c08a104SJason A. Donenfeld 		u32 r[5];
251c08a104SJason A. Donenfeld 		u64 r64[3];
261c08a104SJason A. Donenfeld 	};
271c08a104SJason A. Donenfeld };
281c08a104SJason A. Donenfeld 
291c08a104SJason A. Donenfeld struct poly1305_core_key {
301c08a104SJason A. Donenfeld 	struct poly1305_key key;
311c08a104SJason A. Donenfeld 	struct poly1305_key precomputed_s;
32878afc35SEric Biggers };
33878afc35SEric Biggers 
34878afc35SEric Biggers struct poly1305_state {
351c08a104SJason A. Donenfeld 	union {
361c08a104SJason A. Donenfeld 		u32 h[5];
371c08a104SJason A. Donenfeld 		u64 h64[3];
381c08a104SJason A. Donenfeld 	};
39878afc35SEric Biggers };
40878afc35SEric Biggers 
412546f811SMartin Willi struct poly1305_desc_ctx {
422546f811SMartin Willi 	/* partial buffer */
432546f811SMartin Willi 	u8 buf[POLY1305_BLOCK_SIZE];
442546f811SMartin Willi 	/* bytes used in partial buffer */
452546f811SMartin Willi 	unsigned int buflen;
46ad8f5b88SArd Biesheuvel 	/* how many keys have been set in r[] */
47ad8f5b88SArd Biesheuvel 	unsigned short rset;
48ad8f5b88SArd Biesheuvel 	/* whether s[] has been set */
492546f811SMartin Willi 	bool sset;
50ad8f5b88SArd Biesheuvel 	/* finalize key */
51ad8f5b88SArd Biesheuvel 	u32 s[4];
52ad8f5b88SArd Biesheuvel 	/* accumulator */
53ad8f5b88SArd Biesheuvel 	struct poly1305_state h;
54ad8f5b88SArd Biesheuvel 	/* key */
551c08a104SJason A. Donenfeld 	union {
561c08a104SJason A. Donenfeld 		struct poly1305_key opaque_r[CONFIG_CRYPTO_LIB_POLY1305_RSIZE];
571c08a104SJason A. Donenfeld 		struct poly1305_core_key core_r;
581c08a104SJason A. Donenfeld 	};
592546f811SMartin Willi };
602546f811SMartin Willi 
61*8d195e7aSArnd Bergmann void poly1305_init_arch(struct poly1305_desc_ctx *desc,
62*8d195e7aSArnd Bergmann 			const u8 key[POLY1305_KEY_SIZE]);
63*8d195e7aSArnd Bergmann void poly1305_init_generic(struct poly1305_desc_ctx *desc,
64*8d195e7aSArnd Bergmann 			   const u8 key[POLY1305_KEY_SIZE]);
65a1d93064SArd Biesheuvel 
poly1305_init(struct poly1305_desc_ctx * desc,const u8 * key)66a1d93064SArd Biesheuvel static inline void poly1305_init(struct poly1305_desc_ctx *desc, const u8 *key)
67a1d93064SArd Biesheuvel {
68a1d93064SArd Biesheuvel 	if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_POLY1305))
69a1d93064SArd Biesheuvel 		poly1305_init_arch(desc, key);
70a1d93064SArd Biesheuvel 	else
71a1d93064SArd Biesheuvel 		poly1305_init_generic(desc, key);
72a1d93064SArd Biesheuvel }
73a1d93064SArd Biesheuvel 
74a1d93064SArd Biesheuvel void poly1305_update_arch(struct poly1305_desc_ctx *desc, const u8 *src,
75a1d93064SArd Biesheuvel 			  unsigned int nbytes);
76a1d93064SArd Biesheuvel void poly1305_update_generic(struct poly1305_desc_ctx *desc, const u8 *src,
77a1d93064SArd Biesheuvel 			     unsigned int nbytes);
78a1d93064SArd Biesheuvel 
poly1305_update(struct poly1305_desc_ctx * desc,const u8 * src,unsigned int nbytes)79a1d93064SArd Biesheuvel static inline void poly1305_update(struct poly1305_desc_ctx *desc,
80a1d93064SArd Biesheuvel 				   const u8 *src, unsigned int nbytes)
81a1d93064SArd Biesheuvel {
82a1d93064SArd Biesheuvel 	if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_POLY1305))
83a1d93064SArd Biesheuvel 		poly1305_update_arch(desc, src, nbytes);
84a1d93064SArd Biesheuvel 	else
85a1d93064SArd Biesheuvel 		poly1305_update_generic(desc, src, nbytes);
86a1d93064SArd Biesheuvel }
87a1d93064SArd Biesheuvel 
88a1d93064SArd Biesheuvel void poly1305_final_arch(struct poly1305_desc_ctx *desc, u8 *digest);
89a1d93064SArd Biesheuvel void poly1305_final_generic(struct poly1305_desc_ctx *desc, u8 *digest);
90a1d93064SArd Biesheuvel 
poly1305_final(struct poly1305_desc_ctx * desc,u8 * digest)91a1d93064SArd Biesheuvel static inline void poly1305_final(struct poly1305_desc_ctx *desc, u8 *digest)
92a1d93064SArd Biesheuvel {
93a1d93064SArd Biesheuvel 	if (IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_POLY1305))
94a1d93064SArd Biesheuvel 		poly1305_final_arch(desc, digest);
95a1d93064SArd Biesheuvel 	else
96a1d93064SArd Biesheuvel 		poly1305_final_generic(desc, digest);
97a1d93064SArd Biesheuvel }
98a1d93064SArd Biesheuvel 
992546f811SMartin Willi #endif
100