1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Common values for the Poly1305 algorithm
4  */
5 
6 #ifndef _CRYPTO_POLY1305_H
7 #define _CRYPTO_POLY1305_H
8 
9 #include <linux/types.h>
10 
11 #define POLY1305_BLOCK_SIZE	16
12 #define POLY1305_KEY_SIZE	32
13 #define POLY1305_DIGEST_SIZE	16
14 
15 /* The poly1305_key and poly1305_state types are mostly opaque and
16  * implementation-defined. Limbs might be in base 2^64 or base 2^26, or
17  * different yet. The union type provided keeps these 64-bit aligned for the
18  * case in which this is implemented using 64x64 multiplies.
19  */
20 
21 struct poly1305_key {
22 	union {
23 		u32 r[5];
24 		u64 r64[3];
25 	};
26 };
27 
28 struct poly1305_core_key {
29 	struct poly1305_key key;
30 	struct poly1305_key precomputed_s;
31 };
32 
33 struct poly1305_state {
34 	union {
35 		u32 h[5];
36 		u64 h64[3];
37 	};
38 };
39 
40 /* Combined state for block function. */
41 struct poly1305_block_state {
42 	/* accumulator */
43 	struct poly1305_state h;
44 	/* key */
45 	union {
46 		struct poly1305_key opaque_r[CONFIG_CRYPTO_LIB_POLY1305_RSIZE];
47 		struct poly1305_core_key core_r;
48 	};
49 };
50 
51 struct poly1305_desc_ctx {
52 	/* partial buffer */
53 	u8 buf[POLY1305_BLOCK_SIZE];
54 	/* bytes used in partial buffer */
55 	unsigned int buflen;
56 	/* finalize key */
57 	u32 s[4];
58 	struct poly1305_block_state state;
59 };
60 
61 void poly1305_init(struct poly1305_desc_ctx *desc,
62 		   const u8 key[POLY1305_KEY_SIZE]);
63 void poly1305_update(struct poly1305_desc_ctx *desc,
64 		     const u8 *src, unsigned int nbytes);
65 void poly1305_final(struct poly1305_desc_ctx *desc, u8 *digest);
66 
67 #if IS_ENABLED(CONFIG_CRYPTO_ARCH_HAVE_LIB_POLY1305)
68 bool poly1305_is_arch_optimized(void);
69 #else
poly1305_is_arch_optimized(void)70 static inline bool poly1305_is_arch_optimized(void)
71 {
72 	return false;
73 }
74 #endif
75 
76 #endif
77