blob: 4d253858e15c093f2def35bb9e66660f6f6ddc6a [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úti86974652020-06-15 11:59:37 +020015/*
Bence Szépkúti1e148272020-08-07 13:07:28 +020016 * Copyright The Mbed TLS Contributors
Daniel Kingadc32c02016-05-16 18:25:45 -030017 * SPDX-License-Identifier: Apache-2.0
18 *
19 * Licensed under the Apache License, Version 2.0 (the "License"); you may
20 * not use this file except in compliance with the License.
21 * You may obtain a copy of the License at
22 *
23 * http://www.apache.org/licenses/LICENSE-2.0
24 *
25 * Unless required by applicable law or agreed to in writing, software
26 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
27 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28 * See the License for the specific language governing permissions and
29 * limitations under the License.
Daniel Kingadc32c02016-05-16 18:25:45 -030030 */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020031
Daniel Kingadc32c02016-05-16 18:25:45 -030032#ifndef MBEDTLS_POLY1305_H
33#define MBEDTLS_POLY1305_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020034#include "mbedtls/private_access.h"
Daniel Kingadc32c02016-05-16 18:25:45 -030035
36#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010037#include "mbedtls/config.h"
Daniel Kingadc32c02016-05-16 18:25:45 -030038#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). */
Ron Eldor9924bdc2018-10-04 10:59:13 +030046
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020047#ifdef __cplusplus
48extern "C" {
49#endif
50
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020051#if !defined(MBEDTLS_POLY1305_ALT)
52
Dawid Drozd428cc522018-07-24 10:02:47 +020053typedef struct mbedtls_poly1305_context
Daniel Kingadc32c02016-05-16 18:25:45 -030054{
Mateusz Starzyk846f0212021-05-19 19:44:07 +020055 uint32_t MBEDTLS_PRIVATE(r)[4]; /** The value for 'r' (low 128 bits of the key). */
56 uint32_t MBEDTLS_PRIVATE(s)[4]; /** The value for 's' (high 128 bits of the key). */
57 uint32_t MBEDTLS_PRIVATE(acc)[5]; /** The accumulator number. */
58 uint8_t MBEDTLS_PRIVATE(queue)[16]; /** The current partial block of data. */
59 size_t MBEDTLS_PRIVATE(queue_len); /** The number of bytes stored in 'queue'. */
Daniel Kingadc32c02016-05-16 18:25:45 -030060}
61mbedtls_poly1305_context;
62
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020063#else /* MBEDTLS_POLY1305_ALT */
64#include "poly1305_alt.h"
65#endif /* MBEDTLS_POLY1305_ALT */
66
Daniel Kingadc32c02016-05-16 18:25:45 -030067/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020068 * \brief This function initializes the specified Poly1305 context.
Daniel Kingadc32c02016-05-16 18:25:45 -030069 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020070 * 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 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050079 * \param ctx The Poly1305 context to initialize. This must
80 * not be \c NULL.
Daniel Kingadc32c02016-05-16 18:25:45 -030081 */
82void mbedtls_poly1305_init( mbedtls_poly1305_context *ctx );
83
84/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050085 * \brief This function releases and clears the specified
86 * Poly1305 context.
Daniel Kingadc32c02016-05-16 18:25:45 -030087 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050088 * \param ctx The Poly1305 context to clear. This may be \c NULL, in which
89 * case this function is a no-op. If it is not \c NULL, it must
90 * point to an initialized Poly1305 context.
Daniel Kingadc32c02016-05-16 18:25:45 -030091 */
92void mbedtls_poly1305_free( mbedtls_poly1305_context *ctx );
93
94/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020095 * \brief This function sets the one-time authentication key.
Daniel Kingadc32c02016-05-16 18:25:45 -030096 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020097 * \warning The key must be unique and unpredictable for each
98 * invocation of Poly1305.
Daniel Kingadc32c02016-05-16 18:25:45 -030099 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200100 * \param ctx The Poly1305 context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500101 * This must be initialized.
102 * \param key The buffer containing the \c 32 Byte (\c 256 Bit) key.
Daniel Kingadc32c02016-05-16 18:25:45 -0300103 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200104 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500105 * \return A negative error code on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300106 */
Manuel Pégourié-Gonnard4edd51b2018-05-07 10:21:56 +0200107int mbedtls_poly1305_starts( mbedtls_poly1305_context *ctx,
Daniel Kingadc32c02016-05-16 18:25:45 -0300108 const unsigned char key[32] );
109
110/**
Manuel Pégourié-Gonnardd2db09f2018-06-04 12:31:12 +0200111 * \brief This functions feeds an input buffer into an ongoing
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200112 * Poly1305 computation.
Daniel Kingadc32c02016-05-16 18:25:45 -0300113 *
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200114 * It is called between \c mbedtls_cipher_poly1305_starts() and
115 * \c mbedtls_cipher_poly1305_finish().
116 * It can be called repeatedly to process a stream of data.
Daniel Kingadc32c02016-05-16 18:25:45 -0300117 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200118 * \param ctx The Poly1305 context to use for the Poly1305 operation.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500119 * This must be initialized and bound to a key.
120 * \param ilen The length of the input data in Bytes.
121 * Any value is accepted.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200122 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500123 * This pointer can be \c NULL if `ilen == 0`.
Daniel Kingadc32c02016-05-16 18:25:45 -0300124 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200125 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500126 * \return A negative error code on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300127 */
128int mbedtls_poly1305_update( mbedtls_poly1305_context *ctx,
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200129 const unsigned char *input,
130 size_t ilen );
Daniel Kingadc32c02016-05-16 18:25:45 -0300131
132/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200133 * \brief This function generates the Poly1305 Message
134 * Authentication Code (MAC).
Daniel Kingadc32c02016-05-16 18:25:45 -0300135 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200136 * \param ctx The Poly1305 context to use for the Poly1305 operation.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500137 * This must be initialized and bound to a key.
138 * \param mac The buffer to where the MAC is written. This must
139 * be a writable buffer of length \c 16 Bytes.
Daniel Kingadc32c02016-05-16 18:25:45 -0300140 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200141 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500142 * \return A negative error code on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300143 */
144int mbedtls_poly1305_finish( mbedtls_poly1305_context *ctx,
145 unsigned char mac[16] );
146
Daniel Kingadc32c02016-05-16 18:25:45 -0300147/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200148 * \brief This function calculates the Poly1305 MAC of the input
149 * buffer with the provided key.
Daniel Kingadc32c02016-05-16 18:25:45 -0300150 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200151 * \warning The key must be unique and unpredictable for each
152 * invocation of Poly1305.
Daniel Kingadc32c02016-05-16 18:25:45 -0300153 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500154 * \param key The buffer containing the \c 32 Byte (\c 256 Bit) key.
155 * \param ilen The length of the input data in Bytes.
156 * Any value is accepted.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200157 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500158 * This pointer can be \c NULL if `ilen == 0`.
159 * \param mac The buffer to where the MAC is written. This must be
160 * a writable buffer of length \c 16 Bytes.
Daniel Kingadc32c02016-05-16 18:25:45 -0300161 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200162 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500163 * \return A negative error code on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300164 */
165int mbedtls_poly1305_mac( const unsigned char key[32],
Daniel Kingadc32c02016-05-16 18:25:45 -0300166 const unsigned char *input,
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200167 size_t ilen,
Daniel Kingadc32c02016-05-16 18:25:45 -0300168 unsigned char mac[16] );
169
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200170#if defined(MBEDTLS_SELF_TEST)
Daniel Kingadc32c02016-05-16 18:25:45 -0300171/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200172 * \brief The Poly1305 checkup routine.
Daniel Kingadc32c02016-05-16 18:25:45 -0300173 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200174 * \return \c 0 on success.
175 * \return \c 1 on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300176 */
177int mbedtls_poly1305_self_test( int verbose );
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200178#endif /* MBEDTLS_SELF_TEST */
Daniel Kingadc32c02016-05-16 18:25:45 -0300179
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200180#ifdef __cplusplus
181}
182#endif
183
Daniel Kingadc32c02016-05-16 18:25:45 -0300184#endif /* MBEDTLS_POLY1305_H */