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 | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 15 | /* |
| 16 | * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved. |
| 17 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 18 | * |
| 19 | * This file is provided under the Apache License 2.0, or the |
| 20 | * GNU General Public License v2.0 or later. |
| 21 | * |
| 22 | * ********** |
| 23 | * Apache License 2.0: |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 24 | * |
| 25 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 26 | * not use this file except in compliance with the License. |
| 27 | * You may obtain a copy of the License at |
| 28 | * |
| 29 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 30 | * |
| 31 | * Unless required by applicable law or agreed to in writing, software |
| 32 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 33 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 34 | * See the License for the specific language governing permissions and |
| 35 | * limitations under the License. |
| 36 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame^] | 37 | * ********** |
| 38 | * |
| 39 | * ********** |
| 40 | * GNU General Public License v2.0 or later: |
| 41 | * |
| 42 | * This program is free software; you can redistribute it and/or modify |
| 43 | * it under the terms of the GNU General Public License as published by |
| 44 | * the Free Software Foundation; either version 2 of the License, or |
| 45 | * (at your option) any later version. |
| 46 | * |
| 47 | * This program is distributed in the hope that it will be useful, |
| 48 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 49 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 50 | * GNU General Public License for more details. |
| 51 | * |
| 52 | * You should have received a copy of the GNU General Public License along |
| 53 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 54 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 55 | * |
| 56 | * ********** |
| 57 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 58 | * This file is part of Mbed TLS (https://tls.mbed.org) |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 59 | */ |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 60 | |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 61 | #ifndef MBEDTLS_POLY1305_H |
| 62 | #define MBEDTLS_POLY1305_H |
| 63 | |
| 64 | #if !defined(MBEDTLS_CONFIG_FILE) |
GuHaijun | 983acb7 | 2018-12-28 11:11:10 +0800 | [diff] [blame] | 65 | #include "config.h" |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 66 | #else |
| 67 | #include MBEDTLS_CONFIG_FILE |
| 68 | #endif |
| 69 | |
| 70 | #include <stdint.h> |
| 71 | #include <stddef.h> |
| 72 | |
Manuel Pégourié-Gonnard | b8bd80a | 2018-05-09 09:54:51 +0200 | [diff] [blame] | 73 | #define MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA -0x0057 /**< Invalid input parameter(s). */ |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 74 | |
| 75 | /* MBEDTLS_ERR_POLY1305_FEATURE_UNAVAILABLE is deprecated and should not be |
| 76 | * used. */ |
Manuel Pégourié-Gonnard | b8bd80a | 2018-05-09 09:54:51 +0200 | [diff] [blame] | 77 | #define MBEDTLS_ERR_POLY1305_FEATURE_UNAVAILABLE -0x0059 /**< Feature not available. For example, s part of the API is not implemented. */ |
Ron Eldor | 9924bdc | 2018-10-04 10:59:13 +0300 | [diff] [blame] | 78 | |
| 79 | /* MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED is deprecated and should not be used. |
| 80 | */ |
Manuel Pégourié-Gonnard | 3798b6b | 2018-05-24 13:27:45 +0200 | [diff] [blame] | 81 | #define MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED -0x005B /**< Poly1305 hardware accelerator failed. */ |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 82 | |
Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 83 | #ifdef __cplusplus |
| 84 | extern "C" { |
| 85 | #endif |
| 86 | |
Manuel Pégourié-Gonnard | 95d0bdb | 2018-05-07 09:58:35 +0200 | [diff] [blame] | 87 | #if !defined(MBEDTLS_POLY1305_ALT) |
| 88 | |
Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 89 | typedef struct mbedtls_poly1305_context |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 90 | { |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 91 | uint32_t r[4]; /** The value for 'r' (low 128 bits of the key). */ |
| 92 | uint32_t s[4]; /** The value for 's' (high 128 bits of the key). */ |
| 93 | uint32_t acc[5]; /** The accumulator number. */ |
| 94 | uint8_t queue[16]; /** The current partial block of data. */ |
| 95 | size_t queue_len; /** The number of bytes stored in 'queue'. */ |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 96 | } |
| 97 | mbedtls_poly1305_context; |
| 98 | |
Manuel Pégourié-Gonnard | 95d0bdb | 2018-05-07 09:58:35 +0200 | [diff] [blame] | 99 | #else /* MBEDTLS_POLY1305_ALT */ |
| 100 | #include "poly1305_alt.h" |
| 101 | #endif /* MBEDTLS_POLY1305_ALT */ |
| 102 | |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 103 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 104 | * \brief This function initializes the specified Poly1305 context. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 105 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 106 | * It must be the first API called before using |
| 107 | * the context. |
| 108 | * |
| 109 | * It is usually followed by a call to |
| 110 | * \c mbedtls_poly1305_starts(), then one or more calls to |
| 111 | * \c mbedtls_poly1305_update(), then one call to |
| 112 | * \c mbedtls_poly1305_finish(), then finally |
| 113 | * \c mbedtls_poly1305_free(). |
| 114 | * |
Hanno Becker | ad7581f | 2018-12-17 09:43:21 +0000 | [diff] [blame] | 115 | * \param ctx The Poly1305 context to initialize. This must |
| 116 | * not be \c NULL. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 117 | */ |
| 118 | void mbedtls_poly1305_init( mbedtls_poly1305_context *ctx ); |
| 119 | |
| 120 | /** |
Hanno Becker | ad7581f | 2018-12-17 09:43:21 +0000 | [diff] [blame] | 121 | * \brief This function releases and clears the specified |
| 122 | * Poly1305 context. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 123 | * |
Hanno Becker | ad7581f | 2018-12-17 09:43:21 +0000 | [diff] [blame] | 124 | * \param ctx The Poly1305 context to clear. This may be \c NULL, in which |
| 125 | * case this function is a no-op. If it is not \c NULL, it must |
| 126 | * point to an initialized Poly1305 context. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 127 | */ |
| 128 | void mbedtls_poly1305_free( mbedtls_poly1305_context *ctx ); |
| 129 | |
| 130 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 131 | * \brief This function sets the one-time authentication key. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 132 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 133 | * \warning The key must be unique and unpredictable for each |
| 134 | * invocation of Poly1305. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 135 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 136 | * \param ctx The Poly1305 context to which the key should be bound. |
Hanno Becker | ad7581f | 2018-12-17 09:43:21 +0000 | [diff] [blame] | 137 | * This must be initialized. |
| 138 | * \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] | 139 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 140 | * \return \c 0 on success. |
Hanno Becker | b3c10b3 | 2018-12-11 14:52:01 +0000 | [diff] [blame] | 141 | * \return A negative error code on failure. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 142 | */ |
Manuel Pégourié-Gonnard | 4edd51b | 2018-05-07 10:21:56 +0200 | [diff] [blame] | 143 | int mbedtls_poly1305_starts( mbedtls_poly1305_context *ctx, |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 144 | const unsigned char key[32] ); |
| 145 | |
| 146 | /** |
Manuel Pégourié-Gonnard | d2db09f | 2018-06-04 12:31:12 +0200 | [diff] [blame] | 147 | * \brief This functions feeds an input buffer into an ongoing |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 148 | * Poly1305 computation. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 149 | * |
Manuel Pégourié-Gonnard | c7bc9e1 | 2018-06-18 10:30:30 +0200 | [diff] [blame] | 150 | * It is called between \c mbedtls_cipher_poly1305_starts() and |
| 151 | * \c mbedtls_cipher_poly1305_finish(). |
| 152 | * It can be called repeatedly to process a stream of data. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 153 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 154 | * \param ctx The Poly1305 context to use for the Poly1305 operation. |
Hanno Becker | ad7581f | 2018-12-17 09:43:21 +0000 | [diff] [blame] | 155 | * This must be initialized and bound to a key. |
| 156 | * \param ilen The length of the input data in Bytes. |
Hanno Becker | b3c10b3 | 2018-12-11 14:52:01 +0000 | [diff] [blame] | 157 | * Any value is accepted. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 158 | * \param input The buffer holding the input data. |
Hanno Becker | b3c10b3 | 2018-12-11 14:52:01 +0000 | [diff] [blame] | 159 | * This pointer can be \c NULL if `ilen == 0`. |
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 | * \return \c 0 on success. |
Hanno Becker | b3c10b3 | 2018-12-11 14:52:01 +0000 | [diff] [blame] | 162 | * \return A negative error code on failure. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 163 | */ |
| 164 | int mbedtls_poly1305_update( mbedtls_poly1305_context *ctx, |
Manuel Pégourié-Gonnard | b1ac5e7 | 2018-05-09 09:25:00 +0200 | [diff] [blame] | 165 | const unsigned char *input, |
| 166 | size_t ilen ); |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 167 | |
| 168 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 169 | * \brief This function generates the Poly1305 Message |
| 170 | * Authentication Code (MAC). |
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 | * \param ctx The Poly1305 context to use for the Poly1305 operation. |
Hanno Becker | ad7581f | 2018-12-17 09:43:21 +0000 | [diff] [blame] | 173 | * This must be initialized and bound to a key. |
| 174 | * \param mac The buffer to where the MAC is written. This must |
| 175 | * be a writable buffer of length \c 16 Bytes. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 176 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 177 | * \return \c 0 on success. |
Hanno Becker | b3c10b3 | 2018-12-11 14:52:01 +0000 | [diff] [blame] | 178 | * \return A negative error code on failure. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 179 | */ |
| 180 | int mbedtls_poly1305_finish( mbedtls_poly1305_context *ctx, |
| 181 | unsigned char mac[16] ); |
| 182 | |
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 | * \brief This function calculates the Poly1305 MAC of the input |
| 185 | * buffer with the provided key. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 186 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 187 | * \warning The key must be unique and unpredictable for each |
| 188 | * invocation of Poly1305. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 189 | * |
Hanno Becker | ad7581f | 2018-12-17 09:43:21 +0000 | [diff] [blame] | 190 | * \param key The buffer containing the \c 32 Byte (\c 256 Bit) key. |
| 191 | * \param ilen The length of the input data in Bytes. |
Hanno Becker | b3c10b3 | 2018-12-11 14:52:01 +0000 | [diff] [blame] | 192 | * Any value is accepted. |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 193 | * \param input The buffer holding the input data. |
Hanno Becker | b3c10b3 | 2018-12-11 14:52:01 +0000 | [diff] [blame] | 194 | * This pointer can be \c NULL if `ilen == 0`. |
Hanno Becker | ad7581f | 2018-12-17 09:43:21 +0000 | [diff] [blame] | 195 | * \param mac The buffer to where the MAC is written. This must be |
| 196 | * a writable buffer of length \c 16 Bytes. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 197 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 198 | * \return \c 0 on success. |
Hanno Becker | b3c10b3 | 2018-12-11 14:52:01 +0000 | [diff] [blame] | 199 | * \return A negative error code on failure. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 200 | */ |
| 201 | int mbedtls_poly1305_mac( const unsigned char key[32], |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 202 | const unsigned char *input, |
Manuel Pégourié-Gonnard | b1ac5e7 | 2018-05-09 09:25:00 +0200 | [diff] [blame] | 203 | size_t ilen, |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 204 | unsigned char mac[16] ); |
| 205 | |
Manuel Pégourié-Gonnard | c22e61a | 2018-05-24 13:51:05 +0200 | [diff] [blame] | 206 | #if defined(MBEDTLS_SELF_TEST) |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 207 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 208 | * \brief The Poly1305 checkup routine. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 209 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 210 | * \return \c 0 on success. |
| 211 | * \return \c 1 on failure. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 212 | */ |
| 213 | int mbedtls_poly1305_self_test( int verbose ); |
Manuel Pégourié-Gonnard | c22e61a | 2018-05-24 13:51:05 +0200 | [diff] [blame] | 214 | #endif /* MBEDTLS_SELF_TEST */ |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 215 | |
Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 216 | #ifdef __cplusplus |
| 217 | } |
| 218 | #endif |
| 219 | |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 220 | #endif /* MBEDTLS_POLY1305_H */ |