Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 1 | /** |
| 2 | * \file poly1305.h |
| 3 | * |
Manuel Pégourié-Gonnard | c7bc9e1 | 2018-06-18 10:30:30 +0200 | [diff] [blame] | 4 | * \brief This file contains Poly1305 definitions and functions. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 6 | * Poly1305 is a one-time message authenticator that can be used to |
| 7 | * authenticate messages. Poly1305-AES was created by Daniel |
| 8 | * Bernstein https://cr.yp.to/mac/poly1305-20050329.pdf The generic |
| 9 | * Poly1305 algorithm (not tied to AES) was also standardized in RFC |
| 10 | * 7539. |
| 11 | * |
| 12 | * \author Daniel King <damaki.gh@gmail.com> |
| 13 | */ |
| 14 | |
Bence Szépkúti | 8697465 | 2020-06-15 11:59:37 +0200 | [diff] [blame] | 15 | /* |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 16 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 17 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 18 | */ |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 19 | |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 20 | #ifndef MBEDTLS_POLY1305_H |
| 21 | #define MBEDTLS_POLY1305_H |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 22 | #include "mbedtls/private_access.h" |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 23 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 24 | #include "mbedtls/build_info.h" |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 25 | |
| 26 | #include <stdint.h> |
| 27 | #include <stddef.h> |
| 28 | |
Gilles Peskine | d297157 | 2021-07-26 18:48:10 +0200 | [diff] [blame] | 29 | /** Invalid input parameter(s). */ |
| 30 | #define MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA -0x0057 |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 31 | |
Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 32 | #ifdef __cplusplus |
| 33 | extern "C" { |
| 34 | #endif |
| 35 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 36 | typedef struct mbedtls_poly1305_context { |
Mateusz Starzyk | 846f021 | 2021-05-19 19:44:07 +0200 | [diff] [blame] | 37 | uint32_t MBEDTLS_PRIVATE(r)[4]; /** The value for 'r' (low 128 bits of the key). */ |
| 38 | uint32_t MBEDTLS_PRIVATE(s)[4]; /** The value for 's' (high 128 bits of the key). */ |
| 39 | uint32_t MBEDTLS_PRIVATE(acc)[5]; /** The accumulator number. */ |
| 40 | uint8_t MBEDTLS_PRIVATE(queue)[16]; /** The current partial block of data. */ |
| 41 | size_t MBEDTLS_PRIVATE(queue_len); /** The number of bytes stored in 'queue'. */ |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 42 | } |
| 43 | mbedtls_poly1305_context; |
| 44 | |
| 45 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 46 | * \brief This function initializes the specified Poly1305 context. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 47 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 48 | * It must be the first API called before using |
| 49 | * the context. |
| 50 | * |
| 51 | * It is usually followed by a call to |
| 52 | * \c mbedtls_poly1305_starts(), then one or more calls to |
| 53 | * \c mbedtls_poly1305_update(), then one call to |
| 54 | * \c mbedtls_poly1305_finish(), then finally |
| 55 | * \c mbedtls_poly1305_free(). |
| 56 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 57 | * \param ctx The Poly1305 context to initialize. This must |
| 58 | * not be \c NULL. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 59 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 60 | void mbedtls_poly1305_init(mbedtls_poly1305_context *ctx); |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 61 | |
| 62 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 63 | * \brief This function releases and clears the specified |
| 64 | * Poly1305 context. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 65 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 66 | * \param ctx The Poly1305 context to clear. This may be \c NULL, in which |
| 67 | * case this function is a no-op. If it is not \c NULL, it must |
| 68 | * point to an initialized Poly1305 context. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 69 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 70 | void mbedtls_poly1305_free(mbedtls_poly1305_context *ctx); |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 71 | |
| 72 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 73 | * \brief This function sets the one-time authentication key. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 74 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 75 | * \warning The key must be unique and unpredictable for each |
| 76 | * invocation of Poly1305. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 77 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 78 | * \param ctx The Poly1305 context to which the key should be bound. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 79 | * This must be initialized. |
| 80 | * \param key The buffer containing the \c 32 Byte (\c 256 Bit) key. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 81 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 82 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 83 | * \return A negative error code on failure. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 84 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | int mbedtls_poly1305_starts(mbedtls_poly1305_context *ctx, |
| 86 | const unsigned char key[32]); |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 87 | |
| 88 | /** |
Manuel Pégourié-Gonnard | d2db09f | 2018-06-04 12:31:12 +0200 | [diff] [blame] | 89 | * \brief This functions feeds an input buffer into an ongoing |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 90 | * Poly1305 computation. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 91 | * |
Manuel Pégourié-Gonnard | c7bc9e1 | 2018-06-18 10:30:30 +0200 | [diff] [blame] | 92 | * It is called between \c mbedtls_cipher_poly1305_starts() and |
| 93 | * \c mbedtls_cipher_poly1305_finish(). |
| 94 | * It can be called repeatedly to process a stream of data. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 95 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 96 | * \param ctx The Poly1305 context to use for the Poly1305 operation. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 97 | * This must be initialized and bound to a key. |
| 98 | * \param ilen The length of the input data in Bytes. |
| 99 | * Any value is accepted. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 100 | * \param input The buffer holding the input data. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 101 | * This pointer can be \c NULL if `ilen == 0`. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 102 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 103 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 104 | * \return A negative error code on failure. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 105 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 106 | int mbedtls_poly1305_update(mbedtls_poly1305_context *ctx, |
| 107 | const unsigned char *input, |
| 108 | size_t ilen); |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 109 | |
| 110 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 111 | * \brief This function generates the Poly1305 Message |
| 112 | * Authentication Code (MAC). |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 113 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 114 | * \param ctx The Poly1305 context to use for the Poly1305 operation. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 115 | * This must be initialized and bound to a key. |
| 116 | * \param mac The buffer to where the MAC is written. This must |
| 117 | * be a writable buffer of length \c 16 Bytes. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 118 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 119 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 120 | * \return A negative error code on failure. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 121 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 122 | int mbedtls_poly1305_finish(mbedtls_poly1305_context *ctx, |
| 123 | unsigned char mac[16]); |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 124 | |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 125 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 126 | * \brief This function calculates the Poly1305 MAC of the input |
| 127 | * buffer with the provided key. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 128 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 129 | * \warning The key must be unique and unpredictable for each |
| 130 | * invocation of Poly1305. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 131 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 132 | * \param key The buffer containing the \c 32 Byte (\c 256 Bit) key. |
| 133 | * \param ilen The length of the input data in Bytes. |
| 134 | * Any value is accepted. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 135 | * \param input The buffer holding the input data. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 136 | * This pointer can be \c NULL if `ilen == 0`. |
| 137 | * \param mac The buffer to where the MAC is written. This must be |
| 138 | * a writable buffer of length \c 16 Bytes. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 139 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 140 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 141 | * \return A negative error code on failure. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 142 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 143 | int mbedtls_poly1305_mac(const unsigned char key[32], |
| 144 | const unsigned char *input, |
| 145 | size_t ilen, |
| 146 | unsigned char mac[16]); |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 147 | |
Manuel Pégourié-Gonnard | c22e61a | 2018-05-24 13:51:05 +0200 | [diff] [blame] | 148 | #if defined(MBEDTLS_SELF_TEST) |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 149 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 150 | * \brief The Poly1305 checkup routine. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 151 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 152 | * \return \c 0 on success. |
| 153 | * \return \c 1 on failure. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 154 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | int mbedtls_poly1305_self_test(int verbose); |
Manuel Pégourié-Gonnard | c22e61a | 2018-05-24 13:51:05 +0200 | [diff] [blame] | 156 | #endif /* MBEDTLS_SELF_TEST */ |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 157 | |
Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 158 | #ifdef __cplusplus |
| 159 | } |
| 160 | #endif |
| 161 | |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 162 | #endif /* MBEDTLS_POLY1305_H */ |