Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 1 | /** |
| 2 | * \file poly1305.h |
| 3 | * |
| 4 | * \brief Poly1305 authenticator algorithm. |
| 5 | * |
| 6 | * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved |
| 7 | * SPDX-License-Identifier: Apache-2.0 |
| 8 | * |
| 9 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 10 | * not use this file except in compliance with the License. |
| 11 | * You may obtain a copy of the License at |
| 12 | * |
| 13 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 14 | * |
| 15 | * Unless required by applicable law or agreed to in writing, software |
| 16 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 17 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 18 | * See the License for the specific language governing permissions and |
| 19 | * limitations under the License. |
| 20 | * |
| 21 | * This file is part of mbed TLS (https://tls.mbed.org) |
| 22 | */ |
| 23 | #ifndef MBEDTLS_POLY1305_H |
| 24 | #define MBEDTLS_POLY1305_H |
| 25 | |
| 26 | #if !defined(MBEDTLS_CONFIG_FILE) |
| 27 | #include "mbedtls/config.h" |
| 28 | #else |
| 29 | #include MBEDTLS_CONFIG_FILE |
| 30 | #endif |
| 31 | |
| 32 | #include <stdint.h> |
| 33 | #include <stddef.h> |
| 34 | |
| 35 | #if !defined(MBEDTLS_POLY1305_ALT) |
| 36 | |
| 37 | #define MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA -0x0041 /**< Invalid input parameter(s). */ |
| 38 | |
| 39 | typedef struct |
| 40 | { |
| 41 | uint32_t r[4]; /** Stores the value for 'r' (low 128 bits of the key) */ |
| 42 | uint32_t s[4]; /** Stores the value for 's' (high 128 bits of the key) */ |
| 43 | uint32_t acc[5]; /** Accumulator number */ |
| 44 | uint8_t queue[16]; /** Stores partial block data */ |
| 45 | size_t queue_len; /** Number of bytes stored in 'queue'. Always less than 16 */ |
| 46 | } |
| 47 | mbedtls_poly1305_context; |
| 48 | |
| 49 | /** |
| 50 | * \brief Initialize a Poly1305 context |
| 51 | * |
| 52 | * \param ctx The Poly1305 context to be initialized |
| 53 | */ |
| 54 | void mbedtls_poly1305_init( mbedtls_poly1305_context *ctx ); |
| 55 | |
| 56 | /** |
| 57 | * \brief Clear a Poly1305 context |
| 58 | * |
| 59 | * \param ctx The Poly1305 context to be cleared |
| 60 | */ |
| 61 | void mbedtls_poly1305_free( mbedtls_poly1305_context *ctx ); |
| 62 | |
| 63 | /** |
| 64 | * \brief Set the Poly1305 authentication key. |
| 65 | * |
| 66 | * \warning The key should be unique, and \b MUST be |
| 67 | * unpredictable for each invocation of Poly1305. |
| 68 | * |
| 69 | * \param ctx The Poly1305 context. |
| 70 | * \param key Buffer containing the 256-bit key. |
| 71 | * |
| 72 | * \return MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA is returned if ctx |
| 73 | * or key are NULL. |
| 74 | * Otherwise, 0 is returned to indicate success. |
| 75 | */ |
| 76 | int mbedtls_poly1305_setkey( mbedtls_poly1305_context *ctx, |
| 77 | const unsigned char key[32] ); |
| 78 | |
| 79 | /** |
| 80 | * \brief Process data with Poly1305. |
| 81 | * |
| 82 | * This function can be called multiple times to process |
| 83 | * a stream of data. |
| 84 | * |
| 85 | * \param ctx The Poly1305 context. |
| 86 | * \param ilen The input length (in bytes). Any value is accepted. |
| 87 | * \param input Buffer containing the input data to Process. |
Daniel King | a310c5e | 2016-05-17 15:56:26 -0300 | [diff] [blame] | 88 | * This pointer can be NULL if ilen == 0. |
Daniel King | adc32c0 | 2016-05-16 18:25:45 -0300 | [diff] [blame] | 89 | * |
| 90 | * \return MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA is returned if ctx |
| 91 | * or input are NULL. |
| 92 | * Otherwise, 0 is returned to indicate success. |
| 93 | */ |
| 94 | int mbedtls_poly1305_update( mbedtls_poly1305_context *ctx, |
| 95 | size_t ilen, |
| 96 | const unsigned char *input ); |
| 97 | |
| 98 | /** |
| 99 | * \brief Generate the Poly1305 MAC. |
| 100 | * |
| 101 | * \param ctx The Poly1305 context. |
| 102 | * \param mac Buffer to where the MAC is written. Must be big enough |
| 103 | * to hold the 16-byte MAC. |
| 104 | * |
| 105 | * \return MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA is returned if ctx |
| 106 | * or mac are NULL. |
| 107 | * Otherwise, 0 is returned to indicate success. |
| 108 | */ |
| 109 | int mbedtls_poly1305_finish( mbedtls_poly1305_context *ctx, |
| 110 | unsigned char mac[16] ); |
| 111 | |
| 112 | #else /* MBEDTLS_POLY1305_ALT */ |
| 113 | #include "poly1305_alt.h" |
| 114 | #endif /* MBEDTLS_POLY1305_ALT */ |
| 115 | |
| 116 | /** |
| 117 | * \brief Generate the Poly1305 MAC of some data with the given key. |
| 118 | * |
| 119 | * \warning The key should be unique, and \b MUST be |
| 120 | * unpredictable for each invocation of Poly1305. |
| 121 | * |
| 122 | * \param key Buffer containing the 256-bit (32 bytes) key. |
| 123 | * \param ilen The length of the input data (in bytes). |
| 124 | * \param input Buffer containing the input data to process. |
| 125 | * \param mac Buffer to where the 128-bit (16 bytes) MAC is written. |
| 126 | * |
| 127 | * \return MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA is returned if key, |
| 128 | * input, or mac are NULL. |
| 129 | * Otherwise, 0 is returned to indicate success. |
| 130 | */ |
| 131 | int mbedtls_poly1305_mac( const unsigned char key[32], |
| 132 | size_t ilen, |
| 133 | const unsigned char *input, |
| 134 | unsigned char mac[16] ); |
| 135 | |
| 136 | /** |
| 137 | * \brief Checkup routine |
| 138 | * |
| 139 | * \return 0 if successful, or 1 if the test failed |
| 140 | */ |
| 141 | int mbedtls_poly1305_self_test( int verbose ); |
| 142 | |
| 143 | #endif /* MBEDTLS_POLY1305_H */ |