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 |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 17 | * SPDX-License-Identifier: Apache-2.0 |
| 18 | * |
| 19 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 20 | * not use this file except in compliance with the License. |
| 21 | * You may obtain a copy of the License at |
| 22 | * |
| 23 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 24 | * |
| 25 | * Unless required by applicable law or agreed to in writing, software |
| 26 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 27 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 28 | * See the License for the specific language governing permissions and |
| 29 | * limitations under the License. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 30 | */ |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 31 | |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 32 | #ifndef MBEDTLS_POLY1305_H |
| 33 | #define MBEDTLS_POLY1305_H |
| 34 | |
| 35 | #if !defined(MBEDTLS_CONFIG_FILE) |
Jaeden Amero | c49fbbf | 2019-07-04 20:01:14 +0100 | [diff] [blame] | 36 | #include "mbedtls/config.h" |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 37 | #else |
| 38 | #include MBEDTLS_CONFIG_FILE |
| 39 | #endif |
| 40 | |
| 41 | #include <stdint.h> |
| 42 | #include <stddef.h> |
| 43 | |
Gilles Peskine | a397443 | 2021-07-26 18:48:10 +0200 | [diff] [blame^] | 44 | /** Invalid input parameter(s). */ |
| 45 | #define MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA -0x0057 |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 46 | |
| 47 | /* MBEDTLS_ERR_POLY1305_FEATURE_UNAVAILABLE is deprecated and should not be |
| 48 | * used. */ |
Gilles Peskine | a397443 | 2021-07-26 18:48:10 +0200 | [diff] [blame^] | 49 | /** Feature not available. For example, s part of the API is not implemented. */ |
| 50 | #define MBEDTLS_ERR_POLY1305_FEATURE_UNAVAILABLE -0x0059 |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 51 | |
| 52 | /* MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED is deprecated and should not be used. |
| 53 | */ |
Gilles Peskine | a397443 | 2021-07-26 18:48:10 +0200 | [diff] [blame^] | 54 | /** Poly1305 hardware accelerator failed. */ |
| 55 | #define MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED -0x005B |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 56 | |
Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 57 | #ifdef __cplusplus |
| 58 | extern "C" { |
| 59 | #endif |
| 60 | |
Manuel Pégourié-Gonnard | 95d0bdb | 2018-05-07 09:58:35 +0200 | [diff] [blame] | 61 | #if !defined(MBEDTLS_POLY1305_ALT) |
| 62 | |
Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 63 | typedef struct mbedtls_poly1305_context |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 64 | { |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 65 | uint32_t r[4]; /** The value for 'r' (low 128 bits of the key). */ |
| 66 | uint32_t s[4]; /** The value for 's' (high 128 bits of the key). */ |
| 67 | uint32_t acc[5]; /** The accumulator number. */ |
| 68 | uint8_t queue[16]; /** The current partial block of data. */ |
| 69 | size_t queue_len; /** The number of bytes stored in 'queue'. */ |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 70 | } |
| 71 | mbedtls_poly1305_context; |
| 72 | |
Manuel Pégourié-Gonnard | 95d0bdb | 2018-05-07 09:58:35 +0200 | [diff] [blame] | 73 | #else /* MBEDTLS_POLY1305_ALT */ |
| 74 | #include "poly1305_alt.h" |
| 75 | #endif /* MBEDTLS_POLY1305_ALT */ |
| 76 | |
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 | * \brief This function initializes the specified Poly1305 context. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 79 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 80 | * It must be the first API called before using |
| 81 | * the context. |
| 82 | * |
| 83 | * It is usually followed by a call to |
| 84 | * \c mbedtls_poly1305_starts(), then one or more calls to |
| 85 | * \c mbedtls_poly1305_update(), then one call to |
| 86 | * \c mbedtls_poly1305_finish(), then finally |
| 87 | * \c mbedtls_poly1305_free(). |
| 88 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 89 | * \param ctx The Poly1305 context to initialize. This must |
| 90 | * not be \c NULL. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 91 | */ |
| 92 | void mbedtls_poly1305_init( mbedtls_poly1305_context *ctx ); |
| 93 | |
| 94 | /** |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 95 | * \brief This function releases and clears the specified |
| 96 | * Poly1305 context. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 97 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 98 | * \param ctx The Poly1305 context to clear. This may be \c NULL, in which |
| 99 | * case this function is a no-op. If it is not \c NULL, it must |
| 100 | * point to an initialized Poly1305 context. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 101 | */ |
| 102 | void mbedtls_poly1305_free( mbedtls_poly1305_context *ctx ); |
| 103 | |
| 104 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 105 | * \brief This function sets the one-time authentication key. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 106 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 107 | * \warning The key must be unique and unpredictable for each |
| 108 | * invocation of Poly1305. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 109 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 110 | * \param ctx The Poly1305 context to which the key should be bound. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 111 | * This must be initialized. |
| 112 | * \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] | 113 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 114 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 115 | * \return A negative error code on failure. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 116 | */ |
Manuel Pégourié-Gonnard | 4edd51b | 2018-05-07 10:21:56 +0200 | [diff] [blame] | 117 | int mbedtls_poly1305_starts( mbedtls_poly1305_context *ctx, |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 118 | const unsigned char key[32] ); |
| 119 | |
| 120 | /** |
Manuel Pégourié-Gonnard | d2db09f | 2018-06-04 12:31:12 +0200 | [diff] [blame] | 121 | * \brief This functions feeds an input buffer into an ongoing |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 122 | * Poly1305 computation. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 123 | * |
Manuel Pégourié-Gonnard | c7bc9e1 | 2018-06-18 10:30:30 +0200 | [diff] [blame] | 124 | * It is called between \c mbedtls_cipher_poly1305_starts() and |
| 125 | * \c mbedtls_cipher_poly1305_finish(). |
| 126 | * It can be called repeatedly to process a stream of data. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 127 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 128 | * \param ctx The Poly1305 context to use for the Poly1305 operation. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 129 | * This must be initialized and bound to a key. |
| 130 | * \param ilen The length of the input data in Bytes. |
| 131 | * Any value is accepted. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 132 | * \param input The buffer holding the input data. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 133 | * This pointer can be \c NULL if `ilen == 0`. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 134 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 135 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 136 | * \return A negative error code on failure. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 137 | */ |
| 138 | int mbedtls_poly1305_update( mbedtls_poly1305_context *ctx, |
Manuel Pégourié-Gonnard | b1ac5e7 | 2018-05-09 09:25:00 +0200 | [diff] [blame] | 139 | const unsigned char *input, |
| 140 | size_t ilen ); |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 141 | |
| 142 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 143 | * \brief This function generates the Poly1305 Message |
| 144 | * Authentication Code (MAC). |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 145 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 146 | * \param ctx The Poly1305 context to use for the Poly1305 operation. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 147 | * This must be initialized and bound to a key. |
| 148 | * \param mac The buffer to where the MAC is written. This must |
| 149 | * be a writable buffer of length \c 16 Bytes. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 150 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 151 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 152 | * \return A negative error code on failure. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 153 | */ |
| 154 | int mbedtls_poly1305_finish( mbedtls_poly1305_context *ctx, |
| 155 | unsigned char mac[16] ); |
| 156 | |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 157 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 158 | * \brief This function calculates the Poly1305 MAC of the input |
| 159 | * buffer with the provided key. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 160 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 161 | * \warning The key must be unique and unpredictable for each |
| 162 | * invocation of Poly1305. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 163 | * |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 164 | * \param key The buffer containing the \c 32 Byte (\c 256 Bit) key. |
| 165 | * \param ilen The length of the input data in Bytes. |
| 166 | * Any value is accepted. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 167 | * \param input The buffer holding the input data. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 168 | * This pointer can be \c NULL if `ilen == 0`. |
| 169 | * \param mac The buffer to where the MAC is written. This must be |
| 170 | * a writable buffer of length \c 16 Bytes. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 171 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 172 | * \return \c 0 on success. |
Andrzej Kurek | c470b6b | 2019-01-31 08:20:20 -0500 | [diff] [blame] | 173 | * \return A negative error code on failure. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 174 | */ |
| 175 | int mbedtls_poly1305_mac( const unsigned char key[32], |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 176 | const unsigned char *input, |
Manuel Pégourié-Gonnard | b1ac5e7 | 2018-05-09 09:25:00 +0200 | [diff] [blame] | 177 | size_t ilen, |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 178 | unsigned char mac[16] ); |
| 179 | |
Manuel Pégourié-Gonnard | c22e61a | 2018-05-24 13:51:05 +0200 | [diff] [blame] | 180 | #if defined(MBEDTLS_SELF_TEST) |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 181 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 182 | * \brief The Poly1305 checkup routine. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 183 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 184 | * \return \c 0 on success. |
| 185 | * \return \c 1 on failure. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 186 | */ |
| 187 | int mbedtls_poly1305_self_test( int verbose ); |
Manuel Pégourié-Gonnard | c22e61a | 2018-05-24 13:51:05 +0200 | [diff] [blame] | 188 | #endif /* MBEDTLS_SELF_TEST */ |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 189 | |
Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 190 | #ifdef __cplusplus |
| 191 | } |
| 192 | #endif |
| 193 | |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 194 | #endif /* MBEDTLS_POLY1305_H */ |