blob: 021a3a0de368fd1ce26129f1bf859b7caeea06d4 [file] [log] [blame]
Daniel Kingadc32c02016-05-16 18:25:45 -03001/**
2 * \file poly1305.h
3 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +02004 * \brief This file containts Poly1305 definitions and functions.
Daniel Kingadc32c02016-05-16 18:25:45 -03005 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +02006 * 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 Kingadc32c02016-05-16 18:25:45 -030016 * 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é-Gonnardb500f8b2018-05-08 12:43:48 +020030 * This file is part of Mbed TLS (https://tls.mbed.org)
Daniel Kingadc32c02016-05-16 18:25:45 -030031 */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020032
Daniel Kingadc32c02016-05-16 18:25:45 -030033#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é-Gonnardb8bd80a2018-05-09 09:54:51 +020045#define MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA -0x0057 /**< Invalid input parameter(s). */
46#define MBEDTLS_ERR_POLY1305_FEATURE_UNAVAILABLE -0x0059 /**< Feature not available. For example, s part of the API is not implemented. */
Daniel Kingadc32c02016-05-16 18:25:45 -030047
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020048#ifdef __cplusplus
49extern "C" {
50#endif
51
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020052#if !defined(MBEDTLS_POLY1305_ALT)
53
Daniel Kingadc32c02016-05-16 18:25:45 -030054typedef struct
55{
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020056 uint32_t r[4]; /** The value for 'r' (low 128 bits of the key). */
57 uint32_t s[4]; /** The value for 's' (high 128 bits of the key). */
58 uint32_t acc[5]; /** The accumulator number. */
59 uint8_t queue[16]; /** The current partial block of data. */
60 size_t queue_len; /** The number of bytes stored in 'queue'. */
Daniel Kingadc32c02016-05-16 18:25:45 -030061}
62mbedtls_poly1305_context;
63
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020064#else /* MBEDTLS_POLY1305_ALT */
65#include "poly1305_alt.h"
66#endif /* MBEDTLS_POLY1305_ALT */
67
Daniel Kingadc32c02016-05-16 18:25:45 -030068/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020069 * \brief This function initializes the specified Poly1305 context.
Daniel Kingadc32c02016-05-16 18:25:45 -030070 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020071 * It must be the first API called before using
72 * the context.
73 *
74 * It is usually followed by a call to
75 * \c mbedtls_poly1305_starts(), then one or more calls to
76 * \c mbedtls_poly1305_update(), then one call to
77 * \c mbedtls_poly1305_finish(), then finally
78 * \c mbedtls_poly1305_free().
79 *
80 * \param ctx The Poly1305 context to initialize.
Daniel Kingadc32c02016-05-16 18:25:45 -030081 */
82void mbedtls_poly1305_init( mbedtls_poly1305_context *ctx );
83
84/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020085 * \brief This function releases and clears the specified Poly1305 context.
Daniel Kingadc32c02016-05-16 18:25:45 -030086 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020087 * \param ctx The Poly1305 context to clear.
Daniel Kingadc32c02016-05-16 18:25:45 -030088 */
89void mbedtls_poly1305_free( mbedtls_poly1305_context *ctx );
90
91/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020092 * \brief This function sets the one-time authentication key.
Daniel Kingadc32c02016-05-16 18:25:45 -030093 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020094 * \warning The key must be unique and unpredictable for each
95 * invocation of Poly1305.
Daniel Kingadc32c02016-05-16 18:25:45 -030096 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020097 * \param ctx The Poly1305 context to which the key should be bound.
98 * \param key The buffer containing the 256-bit key.
Daniel Kingadc32c02016-05-16 18:25:45 -030099 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200100 * \return \c 0 on success.
101 * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
102 * if ctx or key are NULL.
Daniel Kingadc32c02016-05-16 18:25:45 -0300103 */
Manuel Pégourié-Gonnard4edd51b2018-05-07 10:21:56 +0200104int mbedtls_poly1305_starts( mbedtls_poly1305_context *ctx,
Daniel Kingadc32c02016-05-16 18:25:45 -0300105 const unsigned char key[32] );
106
107/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200108 * \brief This functions feeds an input bufer into an ongoing
109 * Poly1305 computation.
Daniel Kingadc32c02016-05-16 18:25:45 -0300110 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200111 * It is called between \c mbedtls_cipher_cmac_starts() and
112 * \c mbedtls_cipher_cmac_finish().
113 * Can be called repeatedly to process a stream of data.
Daniel Kingadc32c02016-05-16 18:25:45 -0300114 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200115 * \param ctx The Poly1305 context to use for the Poly1305 operation.
116 * \param ilen The length of the input data (in bytes). Any value is accepted.
117 * \param input The buffer holding the input data.
118 * This pointer can be NULL if ilen == 0.
Daniel Kingadc32c02016-05-16 18:25:45 -0300119 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200120 * \return \c 0 on success.
121 * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
122 * if ctx or input are NULL.
Daniel Kingadc32c02016-05-16 18:25:45 -0300123 */
124int mbedtls_poly1305_update( mbedtls_poly1305_context *ctx,
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200125 const unsigned char *input,
126 size_t ilen );
Daniel Kingadc32c02016-05-16 18:25:45 -0300127
128/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200129 * \brief This function generates the Poly1305 Message
130 * Authentication Code (MAC).
Daniel Kingadc32c02016-05-16 18:25:45 -0300131 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200132 * \param ctx The Poly1305 context to use for the Poly1305 operation.
133 * \param mac The buffer to where the MAC is written. Must be big enough
134 * to hold the 16-byte MAC.
Daniel Kingadc32c02016-05-16 18:25:45 -0300135 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200136 * \return \c 0 on success.
137 * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
138 * if ctx or mac are NULL.
Daniel Kingadc32c02016-05-16 18:25:45 -0300139 */
140int mbedtls_poly1305_finish( mbedtls_poly1305_context *ctx,
141 unsigned char mac[16] );
142
Daniel Kingadc32c02016-05-16 18:25:45 -0300143/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200144 * \brief This function calculates the Poly1305 MAC of the input
145 * buffer with the provided key.
Daniel Kingadc32c02016-05-16 18:25:45 -0300146 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200147 * \warning The key must be unique and unpredictable for each
148 * invocation of Poly1305.
Daniel Kingadc32c02016-05-16 18:25:45 -0300149 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200150 * \param key The buffer containing the 256-bit key.
151 * \param ilen The length of the input data (in bytes). Any value is accepted.
152 * \param input The buffer holding the input data.
153 * This pointer can be NULL if ilen == 0.
154 * \param mac The buffer to where the MAC is written. Must be big enough
155 * to hold the 16-byte MAC.
Daniel Kingadc32c02016-05-16 18:25:45 -0300156 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200157 * \return \c 0 on success.
158 * \return #MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA
159 * if key, input, or mac are NULL.
Daniel Kingadc32c02016-05-16 18:25:45 -0300160 */
161int mbedtls_poly1305_mac( const unsigned char key[32],
Daniel Kingadc32c02016-05-16 18:25:45 -0300162 const unsigned char *input,
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200163 size_t ilen,
Daniel Kingadc32c02016-05-16 18:25:45 -0300164 unsigned char mac[16] );
165
166/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200167 * \brief The Poly1305 checkup routine.
Daniel Kingadc32c02016-05-16 18:25:45 -0300168 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200169 * \return \c 0 on success.
170 * \return \c 1 on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300171 */
172int mbedtls_poly1305_self_test( int verbose );
173
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200174#ifdef __cplusplus
175}
176#endif
177
Daniel Kingadc32c02016-05-16 18:25:45 -0300178#endif /* MBEDTLS_POLY1305_H */