blob: ea69dba57618385b4d80904440eb0decd5f2b4f2 [file] [log] [blame]
Daniel Kingadc32c02016-05-16 18:25:45 -03001/**
2 * \file poly1305.h
3 *
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +02004 * \brief This file contains 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
Bence Szépkútif744bd72020-06-05 13:02:18 +020015/*
Bence Szépkútia2947ac2020-08-19 16:37:36 +020016 * Copyright The Mbed TLS Contributors
Bence Szépkútif744bd72020-06-05 13:02:18 +020017 * 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 Kingadc32c02016-05-16 18:25:45 -030024 *
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útif744bd72020-06-05 13:02:18 +020037 * **********
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 * **********
Daniel Kingadc32c02016-05-16 18:25:45 -030057 */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020058
Daniel Kingadc32c02016-05-16 18:25:45 -030059#ifndef MBEDTLS_POLY1305_H
60#define MBEDTLS_POLY1305_H
61
62#if !defined(MBEDTLS_CONFIG_FILE)
GuHaijun983acb72018-12-28 11:11:10 +080063#include "config.h"
Daniel Kingadc32c02016-05-16 18:25:45 -030064#else
65#include MBEDTLS_CONFIG_FILE
66#endif
67
68#include <stdint.h>
69#include <stddef.h>
70
Gilles Peskine1990fab2021-07-26 18:48:10 +020071/** Invalid input parameter(s). */
72#define MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA -0x0057
Ron Eldor9924bdc2018-10-04 10:59:13 +030073
74/* MBEDTLS_ERR_POLY1305_FEATURE_UNAVAILABLE is deprecated and should not be
75 * used. */
Gilles Peskine1990fab2021-07-26 18:48:10 +020076/** Feature not available. For example, s part of the API is not implemented. */
77#define MBEDTLS_ERR_POLY1305_FEATURE_UNAVAILABLE -0x0059
Ron Eldor9924bdc2018-10-04 10:59:13 +030078
79/* MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED is deprecated and should not be used.
80 */
Gilles Peskine1990fab2021-07-26 18:48:10 +020081/** Poly1305 hardware accelerator failed. */
82#define MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED -0x005B
Daniel Kingadc32c02016-05-16 18:25:45 -030083
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020084#ifdef __cplusplus
85extern "C" {
86#endif
87
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020088#if !defined(MBEDTLS_POLY1305_ALT)
89
Dawid Drozd428cc522018-07-24 10:02:47 +020090typedef struct mbedtls_poly1305_context
Daniel Kingadc32c02016-05-16 18:25:45 -030091{
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020092 uint32_t r[4]; /** The value for 'r' (low 128 bits of the key). */
93 uint32_t s[4]; /** The value for 's' (high 128 bits of the key). */
94 uint32_t acc[5]; /** The accumulator number. */
95 uint8_t queue[16]; /** The current partial block of data. */
96 size_t queue_len; /** The number of bytes stored in 'queue'. */
Daniel Kingadc32c02016-05-16 18:25:45 -030097}
98mbedtls_poly1305_context;
99
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +0200100#else /* MBEDTLS_POLY1305_ALT */
101#include "poly1305_alt.h"
102#endif /* MBEDTLS_POLY1305_ALT */
103
Daniel Kingadc32c02016-05-16 18:25:45 -0300104/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200105 * \brief This function initializes the specified Poly1305 context.
Daniel Kingadc32c02016-05-16 18:25:45 -0300106 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200107 * It must be the first API called before using
108 * the context.
109 *
110 * It is usually followed by a call to
111 * \c mbedtls_poly1305_starts(), then one or more calls to
112 * \c mbedtls_poly1305_update(), then one call to
113 * \c mbedtls_poly1305_finish(), then finally
114 * \c mbedtls_poly1305_free().
115 *
Hanno Beckerad7581f2018-12-17 09:43:21 +0000116 * \param ctx The Poly1305 context to initialize. This must
117 * not be \c NULL.
Daniel Kingadc32c02016-05-16 18:25:45 -0300118 */
119void mbedtls_poly1305_init( mbedtls_poly1305_context *ctx );
120
121/**
Hanno Beckerad7581f2018-12-17 09:43:21 +0000122 * \brief This function releases and clears the specified
123 * Poly1305 context.
Daniel Kingadc32c02016-05-16 18:25:45 -0300124 *
Hanno Beckerad7581f2018-12-17 09:43:21 +0000125 * \param ctx The Poly1305 context to clear. This may be \c NULL, in which
126 * case this function is a no-op. If it is not \c NULL, it must
127 * point to an initialized Poly1305 context.
Daniel Kingadc32c02016-05-16 18:25:45 -0300128 */
129void mbedtls_poly1305_free( mbedtls_poly1305_context *ctx );
130
131/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200132 * \brief This function sets the one-time authentication key.
Daniel Kingadc32c02016-05-16 18:25:45 -0300133 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200134 * \warning The key must be unique and unpredictable for each
135 * invocation of Poly1305.
Daniel Kingadc32c02016-05-16 18:25:45 -0300136 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200137 * \param ctx The Poly1305 context to which the key should be bound.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000138 * This must be initialized.
139 * \param key The buffer containing the \c 32 Byte (\c 256 Bit) key.
Daniel Kingadc32c02016-05-16 18:25:45 -0300140 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200141 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000142 * \return A negative error code on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300143 */
Manuel Pégourié-Gonnard4edd51b2018-05-07 10:21:56 +0200144int mbedtls_poly1305_starts( mbedtls_poly1305_context *ctx,
Daniel Kingadc32c02016-05-16 18:25:45 -0300145 const unsigned char key[32] );
146
147/**
Manuel Pégourié-Gonnardd2db09f2018-06-04 12:31:12 +0200148 * \brief This functions feeds an input buffer into an ongoing
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200149 * Poly1305 computation.
Daniel Kingadc32c02016-05-16 18:25:45 -0300150 *
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200151 * It is called between \c mbedtls_cipher_poly1305_starts() and
152 * \c mbedtls_cipher_poly1305_finish().
153 * It can be called repeatedly to process a stream of data.
Daniel Kingadc32c02016-05-16 18:25:45 -0300154 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200155 * \param ctx The Poly1305 context to use for the Poly1305 operation.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000156 * This must be initialized and bound to a key.
157 * \param ilen The length of the input data in Bytes.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000158 * Any value is accepted.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200159 * \param input The buffer holding the input data.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000160 * This pointer can be \c NULL if `ilen == 0`.
Daniel Kingadc32c02016-05-16 18:25:45 -0300161 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200162 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000163 * \return A negative error code on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300164 */
165int mbedtls_poly1305_update( mbedtls_poly1305_context *ctx,
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200166 const unsigned char *input,
167 size_t ilen );
Daniel Kingadc32c02016-05-16 18:25:45 -0300168
169/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200170 * \brief This function generates the Poly1305 Message
171 * Authentication Code (MAC).
Daniel Kingadc32c02016-05-16 18:25:45 -0300172 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200173 * \param ctx The Poly1305 context to use for the Poly1305 operation.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000174 * This must be initialized and bound to a key.
175 * \param mac The buffer to where the MAC is written. This must
176 * be a writable buffer of length \c 16 Bytes.
Daniel Kingadc32c02016-05-16 18:25:45 -0300177 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200178 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000179 * \return A negative error code on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300180 */
181int mbedtls_poly1305_finish( mbedtls_poly1305_context *ctx,
182 unsigned char mac[16] );
183
Daniel Kingadc32c02016-05-16 18:25:45 -0300184/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200185 * \brief This function calculates the Poly1305 MAC of the input
186 * buffer with the provided key.
Daniel Kingadc32c02016-05-16 18:25:45 -0300187 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200188 * \warning The key must be unique and unpredictable for each
189 * invocation of Poly1305.
Daniel Kingadc32c02016-05-16 18:25:45 -0300190 *
Hanno Beckerad7581f2018-12-17 09:43:21 +0000191 * \param key The buffer containing the \c 32 Byte (\c 256 Bit) key.
192 * \param ilen The length of the input data in Bytes.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000193 * Any value is accepted.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200194 * \param input The buffer holding the input data.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000195 * This pointer can be \c NULL if `ilen == 0`.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000196 * \param mac The buffer to where the MAC is written. This must be
197 * a writable buffer of length \c 16 Bytes.
Daniel Kingadc32c02016-05-16 18:25:45 -0300198 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200199 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000200 * \return A negative error code on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300201 */
202int mbedtls_poly1305_mac( const unsigned char key[32],
Daniel Kingadc32c02016-05-16 18:25:45 -0300203 const unsigned char *input,
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200204 size_t ilen,
Daniel Kingadc32c02016-05-16 18:25:45 -0300205 unsigned char mac[16] );
206
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200207#if defined(MBEDTLS_SELF_TEST)
Daniel Kingadc32c02016-05-16 18:25:45 -0300208/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200209 * \brief The Poly1305 checkup routine.
Daniel Kingadc32c02016-05-16 18:25:45 -0300210 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200211 * \return \c 0 on success.
212 * \return \c 1 on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300213 */
214int mbedtls_poly1305_self_test( int verbose );
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200215#endif /* MBEDTLS_SELF_TEST */
Daniel Kingadc32c02016-05-16 18:25:45 -0300216
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200217#ifdef __cplusplus
218}
219#endif
220
Daniel Kingadc32c02016-05-16 18:25:45 -0300221#endif /* MBEDTLS_POLY1305_H */