blob: 7b1faa51f3210a314bf957169a6b3136a150712d [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
34
35#if !defined(MBEDTLS_CONFIG_FILE)
Jaeden Ameroc49fbbf2019-07-04 20:01:14 +010036#include "mbedtls/config.h"
Daniel Kingadc32c02016-05-16 18:25:45 -030037#else
38#include MBEDTLS_CONFIG_FILE
39#endif
40
41#include <stdint.h>
42#include <stddef.h>
43
Gilles Peskinea3974432021-07-26 18:48:10 +020044/** Invalid input parameter(s). */
45#define MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA -0x0057
Ron Eldor9924bdc2018-10-04 10:59:13 +030046
47/* MBEDTLS_ERR_POLY1305_FEATURE_UNAVAILABLE is deprecated and should not be
48 * used. */
Gilles Peskinea3974432021-07-26 18:48:10 +020049/** Feature not available. For example, s part of the API is not implemented. */
50#define MBEDTLS_ERR_POLY1305_FEATURE_UNAVAILABLE -0x0059
Ron Eldor9924bdc2018-10-04 10:59:13 +030051
52/* MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED is deprecated and should not be used.
53 */
Gilles Peskinea3974432021-07-26 18:48:10 +020054/** Poly1305 hardware accelerator failed. */
55#define MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED -0x005B
Daniel Kingadc32c02016-05-16 18:25:45 -030056
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020057#ifdef __cplusplus
58extern "C" {
59#endif
60
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020061#if !defined(MBEDTLS_POLY1305_ALT)
62
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010063typedef struct mbedtls_poly1305_context {
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020064 uint32_t r[4]; /** The value for 'r' (low 128 bits of the key). */
65 uint32_t s[4]; /** The value for 's' (high 128 bits of the key). */
66 uint32_t acc[5]; /** The accumulator number. */
67 uint8_t queue[16]; /** The current partial block of data. */
68 size_t queue_len; /** The number of bytes stored in 'queue'. */
Daniel Kingadc32c02016-05-16 18:25:45 -030069}
70mbedtls_poly1305_context;
71
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020072#else /* MBEDTLS_POLY1305_ALT */
73#include "poly1305_alt.h"
74#endif /* MBEDTLS_POLY1305_ALT */
75
Daniel Kingadc32c02016-05-16 18:25:45 -030076/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020077 * \brief This function initializes the specified Poly1305 context.
Daniel Kingadc32c02016-05-16 18:25:45 -030078 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020079 * It must be the first API called before using
80 * the context.
81 *
82 * It is usually followed by a call to
83 * \c mbedtls_poly1305_starts(), then one or more calls to
84 * \c mbedtls_poly1305_update(), then one call to
85 * \c mbedtls_poly1305_finish(), then finally
86 * \c mbedtls_poly1305_free().
87 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050088 * \param ctx The Poly1305 context to initialize. This must
89 * not be \c NULL.
Daniel Kingadc32c02016-05-16 18:25:45 -030090 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091void mbedtls_poly1305_init(mbedtls_poly1305_context *ctx);
Daniel Kingadc32c02016-05-16 18:25:45 -030092
93/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050094 * \brief This function releases and clears the specified
95 * Poly1305 context.
Daniel Kingadc32c02016-05-16 18:25:45 -030096 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050097 * \param ctx The Poly1305 context to clear. This may be \c NULL, in which
98 * case this function is a no-op. If it is not \c NULL, it must
99 * point to an initialized Poly1305 context.
Daniel Kingadc32c02016-05-16 18:25:45 -0300100 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101void mbedtls_poly1305_free(mbedtls_poly1305_context *ctx);
Daniel Kingadc32c02016-05-16 18:25:45 -0300102
103/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200104 * \brief This function sets the one-time authentication key.
Daniel Kingadc32c02016-05-16 18:25:45 -0300105 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200106 * \warning The key must be unique and unpredictable for each
107 * invocation of Poly1305.
Daniel Kingadc32c02016-05-16 18:25:45 -0300108 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200109 * \param ctx The Poly1305 context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500110 * This must be initialized.
111 * \param key The buffer containing the \c 32 Byte (\c 256 Bit) key.
Daniel Kingadc32c02016-05-16 18:25:45 -0300112 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200113 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500114 * \return A negative error code on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300115 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116int mbedtls_poly1305_starts(mbedtls_poly1305_context *ctx,
117 const unsigned char key[32]);
Daniel Kingadc32c02016-05-16 18:25:45 -0300118
119/**
Manuel Pégourié-Gonnardd2db09f2018-06-04 12:31:12 +0200120 * \brief This functions feeds an input buffer into an ongoing
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200121 * Poly1305 computation.
Daniel Kingadc32c02016-05-16 18:25:45 -0300122 *
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200123 * It is called between \c mbedtls_cipher_poly1305_starts() and
124 * \c mbedtls_cipher_poly1305_finish().
125 * It can be called repeatedly to process a stream of data.
Daniel Kingadc32c02016-05-16 18:25:45 -0300126 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200127 * \param ctx The Poly1305 context to use for the Poly1305 operation.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500128 * This must be initialized and bound to a key.
129 * \param ilen The length of the input data in Bytes.
130 * Any value is accepted.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200131 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500132 * This pointer can be \c NULL if `ilen == 0`.
Daniel Kingadc32c02016-05-16 18:25:45 -0300133 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200134 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500135 * \return A negative error code on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300136 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137int mbedtls_poly1305_update(mbedtls_poly1305_context *ctx,
138 const unsigned char *input,
139 size_t ilen);
Daniel Kingadc32c02016-05-16 18:25:45 -0300140
141/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200142 * \brief This function generates the Poly1305 Message
143 * Authentication Code (MAC).
Daniel Kingadc32c02016-05-16 18:25:45 -0300144 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200145 * \param ctx The Poly1305 context to use for the Poly1305 operation.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500146 * This must be initialized and bound to a key.
147 * \param mac The buffer to where the MAC is written. This must
148 * be a writable buffer of length \c 16 Bytes.
Daniel Kingadc32c02016-05-16 18:25:45 -0300149 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200150 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500151 * \return A negative error code on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300152 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153int mbedtls_poly1305_finish(mbedtls_poly1305_context *ctx,
154 unsigned char mac[16]);
Daniel Kingadc32c02016-05-16 18:25:45 -0300155
Daniel Kingadc32c02016-05-16 18:25:45 -0300156/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200157 * \brief This function calculates the Poly1305 MAC of the input
158 * buffer with the provided key.
Daniel Kingadc32c02016-05-16 18:25:45 -0300159 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200160 * \warning The key must be unique and unpredictable for each
161 * invocation of Poly1305.
Daniel Kingadc32c02016-05-16 18:25:45 -0300162 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500163 * \param key The buffer containing the \c 32 Byte (\c 256 Bit) key.
164 * \param ilen The length of the input data in Bytes.
165 * Any value is accepted.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200166 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500167 * This pointer can be \c NULL if `ilen == 0`.
168 * \param mac The buffer to where the MAC is written. This must be
169 * a writable buffer of length \c 16 Bytes.
Daniel Kingadc32c02016-05-16 18:25:45 -0300170 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200171 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500172 * \return A negative error code on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300173 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174int mbedtls_poly1305_mac(const unsigned char key[32],
175 const unsigned char *input,
176 size_t ilen,
177 unsigned char mac[16]);
Daniel Kingadc32c02016-05-16 18:25:45 -0300178
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200179#if defined(MBEDTLS_SELF_TEST)
Daniel Kingadc32c02016-05-16 18:25:45 -0300180/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200181 * \brief The Poly1305 checkup routine.
Daniel Kingadc32c02016-05-16 18:25:45 -0300182 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200183 * \return \c 0 on success.
184 * \return \c 1 on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300185 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100186int mbedtls_poly1305_self_test(int verbose);
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200187#endif /* MBEDTLS_SELF_TEST */
Daniel Kingadc32c02016-05-16 18:25:45 -0300188
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200189#ifdef __cplusplus
190}
191#endif
192
Daniel Kingadc32c02016-05-16 18:25:45 -0300193#endif /* MBEDTLS_POLY1305_H */