| 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 |  | 
|  | 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 |  | 
| Manuel Pégourié-Gonnard | b8bd80a | 2018-05-09 09:54:51 +0200 | [diff] [blame] | 45 | #define MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA         -0x0057 /**< Invalid input parameter(s). */ | 
| 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. */ | 
| Manuel Pégourié-Gonnard | b8bd80a | 2018-05-09 09:54:51 +0200 | [diff] [blame] | 49 | #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] | 50 |  | 
|  | 51 | /* MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED is deprecated and should not be used. | 
|  | 52 | */ | 
| Manuel Pégourié-Gonnard | 3798b6b | 2018-05-24 13:27:45 +0200 | [diff] [blame] | 53 | #define MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED        -0x005B  /**< Poly1305 hardware accelerator failed. */ | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 54 |  | 
| Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 55 | #ifdef __cplusplus | 
|  | 56 | extern "C" { | 
|  | 57 | #endif | 
|  | 58 |  | 
| Manuel Pégourié-Gonnard | 95d0bdb | 2018-05-07 09:58:35 +0200 | [diff] [blame] | 59 | #if !defined(MBEDTLS_POLY1305_ALT) | 
|  | 60 |  | 
| Dawid Drozd | 428cc52 | 2018-07-24 10:02:47 +0200 | [diff] [blame] | 61 | typedef struct mbedtls_poly1305_context | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 62 | { | 
| Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 63 | uint32_t r[4];      /** The value for 'r' (low 128 bits of the key). */ | 
|  | 64 | uint32_t s[4];      /** The value for 's' (high 128 bits of the key). */ | 
|  | 65 | uint32_t acc[5];    /** The accumulator number. */ | 
|  | 66 | uint8_t queue[16];  /** The current partial block of data. */ | 
|  | 67 | size_t queue_len;   /** The number of bytes stored in 'queue'. */ | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 68 | } | 
|  | 69 | mbedtls_poly1305_context; | 
|  | 70 |  | 
| Manuel Pégourié-Gonnard | 95d0bdb | 2018-05-07 09:58:35 +0200 | [diff] [blame] | 71 | #else  /* MBEDTLS_POLY1305_ALT */ | 
|  | 72 | #include "poly1305_alt.h" | 
|  | 73 | #endif /* MBEDTLS_POLY1305_ALT */ | 
|  | 74 |  | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 75 | /** | 
| Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 76 | * \brief           This function initializes the specified Poly1305 context. | 
| 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 | *                  It must be the first API called before using | 
|  | 79 | *                  the context. | 
|  | 80 | * | 
|  | 81 | *                  It is usually followed by a call to | 
|  | 82 | *                  \c mbedtls_poly1305_starts(), then one or more calls to | 
|  | 83 | *                  \c mbedtls_poly1305_update(), then one call to | 
|  | 84 | *                  \c mbedtls_poly1305_finish(), then finally | 
|  | 85 | *                  \c mbedtls_poly1305_free(). | 
|  | 86 | * | 
|  | 87 | * \param ctx       The Poly1305 context to initialize. | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 88 | */ | 
|  | 89 | void mbedtls_poly1305_init( mbedtls_poly1305_context *ctx ); | 
|  | 90 |  | 
|  | 91 | /** | 
| Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 92 | * \brief           This function releases and clears the specified Poly1305 context. | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 93 | * | 
| Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 94 | * \param ctx       The Poly1305 context to clear. | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 95 | */ | 
|  | 96 | void mbedtls_poly1305_free( mbedtls_poly1305_context *ctx ); | 
|  | 97 |  | 
|  | 98 | /** | 
| Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 99 | * \brief           This function sets the one-time authentication key. | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 100 | * | 
| Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 101 | * \warning         The key must be unique and unpredictable for each | 
|  | 102 | *                  invocation of Poly1305. | 
| 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 | * \param ctx       The Poly1305 context to which the key should be bound. | 
|  | 105 | * \param key       The buffer containing the 256-bit 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 | * \return          \c 0 on success. | 
|  | 108 | * \return          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA | 
|  | 109 | *                  if ctx or key are NULL. | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 110 | */ | 
| Manuel Pégourié-Gonnard | 4edd51b | 2018-05-07 10:21:56 +0200 | [diff] [blame] | 111 | int mbedtls_poly1305_starts( mbedtls_poly1305_context *ctx, | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 112 | const unsigned char key[32] ); | 
|  | 113 |  | 
|  | 114 | /** | 
| Manuel Pégourié-Gonnard | d2db09f | 2018-06-04 12:31:12 +0200 | [diff] [blame] | 115 | * \brief           This functions feeds an input buffer into an ongoing | 
| Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 116 | *                  Poly1305 computation. | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 117 | * | 
| Manuel Pégourié-Gonnard | c7bc9e1 | 2018-06-18 10:30:30 +0200 | [diff] [blame] | 118 | *                  It is called between \c mbedtls_cipher_poly1305_starts() and | 
|  | 119 | *                  \c mbedtls_cipher_poly1305_finish(). | 
|  | 120 | *                  It can be called repeatedly to process a stream of data. | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 121 | * | 
| Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 122 | * \param ctx       The Poly1305 context to use for the Poly1305 operation. | 
|  | 123 | * \param ilen      The length of the input data (in bytes). Any value is accepted. | 
|  | 124 | * \param input     The buffer holding the input data. | 
|  | 125 | *                  This pointer can be NULL if ilen == 0. | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 126 | * | 
| Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 127 | * \return          \c 0 on success. | 
|  | 128 | * \return          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA | 
|  | 129 | *                  if ctx or input are NULL. | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 130 | */ | 
|  | 131 | int mbedtls_poly1305_update( mbedtls_poly1305_context *ctx, | 
| Manuel Pégourié-Gonnard | b1ac5e7 | 2018-05-09 09:25:00 +0200 | [diff] [blame] | 132 | const unsigned char *input, | 
|  | 133 | size_t ilen ); | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 134 |  | 
|  | 135 | /** | 
| Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 136 | * \brief           This function generates the Poly1305 Message | 
|  | 137 | *                  Authentication Code (MAC). | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 138 | * | 
| Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 139 | * \param ctx       The Poly1305 context to use for the Poly1305 operation. | 
|  | 140 | * \param mac       The buffer to where the MAC is written. Must be big enough | 
|  | 141 | *                  to hold the 16-byte MAC. | 
| 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 | * \return          \c 0 on success. | 
|  | 144 | * \return          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA | 
|  | 145 | *                  if ctx or mac are NULL. | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 146 | */ | 
|  | 147 | int mbedtls_poly1305_finish( mbedtls_poly1305_context *ctx, | 
|  | 148 | unsigned char mac[16] ); | 
|  | 149 |  | 
| 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 | * \brief           This function calculates the Poly1305 MAC of the input | 
|  | 152 | *                  buffer with the provided key. | 
| 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 | * \warning         The key must be unique and unpredictable for each | 
|  | 155 | *                  invocation of Poly1305. | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 156 | * | 
| Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 157 | * \param key       The buffer containing the 256-bit key. | 
|  | 158 | * \param ilen      The length of the input data (in bytes). Any value is accepted. | 
|  | 159 | * \param input     The buffer holding the input data. | 
|  | 160 | *                  This pointer can be NULL if ilen == 0. | 
|  | 161 | * \param mac       The buffer to where the MAC is written. Must be big enough | 
|  | 162 | *                  to hold the 16-byte MAC. | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 163 | * | 
| Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 164 | * \return          \c 0 on success. | 
|  | 165 | * \return          #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA | 
|  | 166 | *                  if key, input, or mac are NULL. | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 167 | */ | 
|  | 168 | int mbedtls_poly1305_mac( const unsigned char key[32], | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 169 | const unsigned char *input, | 
| Manuel Pégourié-Gonnard | b1ac5e7 | 2018-05-09 09:25:00 +0200 | [diff] [blame] | 170 | size_t ilen, | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 171 | unsigned char mac[16] ); | 
|  | 172 |  | 
| Manuel Pégourié-Gonnard | c22e61a | 2018-05-24 13:51:05 +0200 | [diff] [blame] | 173 | #if defined(MBEDTLS_SELF_TEST) | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 174 | /** | 
| Manuel Pégourié-Gonnard | b500f8b | 2018-05-08 12:43:48 +0200 | [diff] [blame] | 175 | * \brief           The Poly1305 checkup routine. | 
| 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. | 
|  | 178 | * \return          \c 1 on failure. | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 179 | */ | 
|  | 180 | int mbedtls_poly1305_self_test( int verbose ); | 
| Manuel Pégourié-Gonnard | c22e61a | 2018-05-24 13:51:05 +0200 | [diff] [blame] | 181 | #endif /* MBEDTLS_SELF_TEST */ | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 182 |  | 
| Manuel Pégourié-Gonnard | 823b7a0 | 2018-05-07 10:10:30 +0200 | [diff] [blame] | 183 | #ifdef __cplusplus | 
|  | 184 | } | 
|  | 185 | #endif | 
|  | 186 |  | 
| Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 187 | #endif /* MBEDTLS_POLY1305_H */ |