Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 1 | /** |
| 2 | * \file poly1305.h |
| 3 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame^] | 4 | * \brief This file containts 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 | |
| 15 | /* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 16 | * SPDX-License-Identifier: Apache-2.0 |
| 17 | * |
| 18 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 19 | * not use this file except in compliance with the License. |
| 20 | * You may obtain a copy of the License at |
| 21 | * |
| 22 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 23 | * |
| 24 | * Unless required by applicable law or agreed to in writing, software |
| 25 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 26 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 27 | * See the License for the specific language governing permissions and |
| 28 | * limitations under the License. |
| 29 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame^] | 30 | * This file is part of Mbed TLS (https://tls.mbed.org) |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 31 | */ |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame^] | 32 | |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 33 | #ifndef MBEDTLS_POLY1305_H |
| 34 | #define MBEDTLS_POLY1305_H |
| 35 | |
| 36 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 37 | #include "mbedtls/config.h" |
| 38 | #else |
| 39 | #include MBEDTLS_CONFIG_FILE |
| 40 | #endif |
| 41 | |
| 42 | #include <stdint.h> |
| 43 | #include <stddef.h> |
| 44 | |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 45 | #define MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA -0x0041 /**< Invalid input parameter(s). */ |
| 46 | |
Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 47 | #ifdef __cplusplus |
| 48 | extern "C" { |
| 49 | #endif |
| 50 | |
Manuel Pégourié-Gonnard | 95d0bdb | 2018-05-07 09:58:35 +0200 | [diff] [blame] | 51 | #if !defined(MBEDTLS_POLY1305_ALT) |
| 52 | |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 53 | typedef struct |
| 54 | { |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame^] | 55 | uint32_t r[4]; /** The value for 'r' (low 128 bits of the key). */ |
| 56 | uint32_t s[4]; /** The value for 's' (high 128 bits of the key). */ |
| 57 | uint32_t acc[5]; /** The accumulator number. */ |
| 58 | uint8_t queue[16]; /** The current partial block of data. */ |
| 59 | size_t queue_len; /** The number of bytes stored in 'queue'. */ |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 60 | } |
| 61 | mbedtls_poly1305_context; |
| 62 | |
Manuel Pégourié-Gonnard | 95d0bdb | 2018-05-07 09:58:35 +0200 | [diff] [blame] | 63 | #else /* MBEDTLS_POLY1305_ALT */ |
| 64 | #include "poly1305_alt.h" |
| 65 | #endif /* MBEDTLS_POLY1305_ALT */ |
| 66 | |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 67 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame^] | 68 | * \brief This function initializes the specified Poly1305 context. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 69 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame^] | 70 | * It must be the first API called before using |
| 71 | * the context. |
| 72 | * |
| 73 | * It is usually followed by a call to |
| 74 | * \c mbedtls_poly1305_starts(), then one or more calls to |
| 75 | * \c mbedtls_poly1305_update(), then one call to |
| 76 | * \c mbedtls_poly1305_finish(), then finally |
| 77 | * \c mbedtls_poly1305_free(). |
| 78 | * |
| 79 | * \param ctx The Poly1305 context to initialize. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 80 | */ |
| 81 | void mbedtls_poly1305_init( mbedtls_poly1305_context *ctx ); |
| 82 | |
| 83 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame^] | 84 | * \brief This function releases and clears the specified Poly1305 context. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 85 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame^] | 86 | * \param ctx The Poly1305 context to clear. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 87 | */ |
| 88 | void mbedtls_poly1305_free( mbedtls_poly1305_context *ctx ); |
| 89 | |
| 90 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame^] | 91 | * \brief This function sets the one-time authentication key. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 92 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame^] | 93 | * \warning The key must be unique and unpredictable for each |
| 94 | * invocation of Poly1305. |
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 which the key should be bound. |
| 97 | * \param key The buffer containing the 256-bit key. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 98 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame^] | 99 | * \return \c 0 on success. |
| 100 | * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA |
| 101 | * if ctx or key are NULL. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 102 | */ |
Manuel Pégourié-Gonnard | 4edd51b | 2018-05-07 10:21:56 +0200 | [diff] [blame] | 103 | int mbedtls_poly1305_starts( mbedtls_poly1305_context *ctx, |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 104 | const unsigned char key[32] ); |
| 105 | |
| 106 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame^] | 107 | * \brief This functions feeds an input bufer into an ongoing |
| 108 | * Poly1305 computation. |
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 | * It is called between \c mbedtls_cipher_cmac_starts() and |
| 111 | * \c mbedtls_cipher_cmac_finish(). |
| 112 | * Can be called repeatedly to process a stream of data. |
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. |
| 115 | * \param ilen The length of the input data (in bytes). Any value is accepted. |
| 116 | * \param input The buffer holding the input data. |
| 117 | * This pointer can be NULL if ilen == 0. |
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. |
| 120 | * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA |
| 121 | * if ctx or input are NULL. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 122 | */ |
| 123 | int mbedtls_poly1305_update( mbedtls_poly1305_context *ctx, |
| 124 | size_t ilen, |
| 125 | const unsigned char *input ); |
| 126 | |
| 127 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame^] | 128 | * \brief This function generates the Poly1305 Message |
| 129 | * Authentication Code (MAC). |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 130 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame^] | 131 | * \param ctx The Poly1305 context to use for the Poly1305 operation. |
| 132 | * \param mac The buffer to where the MAC is written. Must be big enough |
| 133 | * to hold the 16-byte MAC. |
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. |
| 136 | * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA |
| 137 | * if ctx or mac are NULL. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 138 | */ |
| 139 | int mbedtls_poly1305_finish( mbedtls_poly1305_context *ctx, |
| 140 | unsigned char mac[16] ); |
| 141 | |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 142 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame^] | 143 | * \brief This function calculates the Poly1305 MAC of the input |
| 144 | * buffer with the provided key. |
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 | * \warning The key must be unique and unpredictable for each |
| 147 | * invocation of Poly1305. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 148 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame^] | 149 | * \param key The buffer containing the 256-bit key. |
| 150 | * \param ilen The length of the input data (in bytes). Any value is accepted. |
| 151 | * \param input The buffer holding the input data. |
| 152 | * This pointer can be NULL if ilen == 0. |
| 153 | * \param mac The buffer to where the MAC is written. Must be big enough |
| 154 | * to hold the 16-byte MAC. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 155 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame^] | 156 | * \return \c 0 on success. |
| 157 | * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA |
| 158 | * if key, input, or mac are NULL. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 159 | */ |
| 160 | int mbedtls_poly1305_mac( const unsigned char key[32], |
| 161 | size_t ilen, |
| 162 | const unsigned char *input, |
| 163 | unsigned char mac[16] ); |
| 164 | |
| 165 | /** |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame^] | 166 | * \brief The Poly1305 checkup routine. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 167 | * |
Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame^] | 168 | * \return \c 0 on success. |
| 169 | * \return \c 1 on failure. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 170 | */ |
| 171 | int mbedtls_poly1305_self_test( int verbose ); |
| 172 | |
Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 173 | #ifdef __cplusplus |
| 174 | } |
| 175 | #endif |
| 176 | |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 177 | #endif /* MBEDTLS_POLY1305_H */ |