Martin Willi | 2546f81 | 2015-07-16 19:14:05 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Common values for the Poly1305 algorithm |
| 3 | */ |
| 4 | |
| 5 | #ifndef _CRYPTO_POLY1305_H |
| 6 | #define _CRYPTO_POLY1305_H |
| 7 | |
| 8 | #include <linux/types.h> |
| 9 | #include <linux/crypto.h> |
| 10 | |
| 11 | #define POLY1305_BLOCK_SIZE 16 |
| 12 | #define POLY1305_KEY_SIZE 32 |
| 13 | #define POLY1305_DIGEST_SIZE 16 |
| 14 | |
Eric Biggers | 888679d | 2018-11-16 17:26:27 -0800 | [diff] [blame] | 15 | struct poly1305_key { |
| 16 | u32 r[5]; /* key, base 2^26 */ |
| 17 | }; |
| 18 | |
| 19 | struct poly1305_state { |
| 20 | u32 h[5]; /* accumulator, base 2^26 */ |
| 21 | }; |
| 22 | |
Martin Willi | 2546f81 | 2015-07-16 19:14:05 +0200 | [diff] [blame] | 23 | struct poly1305_desc_ctx { |
| 24 | /* key */ |
Eric Biggers | 888679d | 2018-11-16 17:26:27 -0800 | [diff] [blame] | 25 | struct poly1305_key r; |
Martin Willi | 2546f81 | 2015-07-16 19:14:05 +0200 | [diff] [blame] | 26 | /* finalize key */ |
| 27 | u32 s[4]; |
| 28 | /* accumulator */ |
Eric Biggers | 888679d | 2018-11-16 17:26:27 -0800 | [diff] [blame] | 29 | struct poly1305_state h; |
Martin Willi | 2546f81 | 2015-07-16 19:14:05 +0200 | [diff] [blame] | 30 | /* partial buffer */ |
| 31 | u8 buf[POLY1305_BLOCK_SIZE]; |
| 32 | /* bytes used in partial buffer */ |
| 33 | unsigned int buflen; |
| 34 | /* r key has been set */ |
| 35 | bool rset; |
| 36 | /* s key has been set */ |
| 37 | bool sset; |
| 38 | }; |
| 39 | |
Eric Biggers | cc786be | 2018-11-16 17:26:28 -0800 | [diff] [blame] | 40 | /* |
| 41 | * Poly1305 core functions. These implement the ε-almost-∆-universal hash |
| 42 | * function underlying the Poly1305 MAC, i.e. they don't add an encrypted nonce |
| 43 | * ("s key") at the end. They also only support block-aligned inputs. |
| 44 | */ |
| 45 | void poly1305_core_setkey(struct poly1305_key *key, const u8 *raw_key); |
| 46 | static inline void poly1305_core_init(struct poly1305_state *state) |
| 47 | { |
| 48 | memset(state->h, 0, sizeof(state->h)); |
| 49 | } |
| 50 | void poly1305_core_blocks(struct poly1305_state *state, |
| 51 | const struct poly1305_key *key, |
| 52 | const void *src, unsigned int nblocks); |
| 53 | void poly1305_core_emit(const struct poly1305_state *state, void *dst); |
| 54 | |
| 55 | /* Crypto API helper functions for the Poly1305 MAC */ |
Martin Willi | 2546f81 | 2015-07-16 19:14:05 +0200 | [diff] [blame] | 56 | int crypto_poly1305_init(struct shash_desc *desc); |
Martin Willi | 2546f81 | 2015-07-16 19:14:05 +0200 | [diff] [blame] | 57 | unsigned int crypto_poly1305_setdesckey(struct poly1305_desc_ctx *dctx, |
| 58 | const u8 *src, unsigned int srclen); |
| 59 | int crypto_poly1305_update(struct shash_desc *desc, |
| 60 | const u8 *src, unsigned int srclen); |
| 61 | int crypto_poly1305_final(struct shash_desc *desc, u8 *dst); |
| 62 | |
| 63 | #endif |