blob: b70bfb8db09ed37ab08693ef8938cba7db3476f2 [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
Dave Rodgman16799db2023-11-02 19:47:20 +000017 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Daniel Kingadc32c02016-05-16 18:25:45 -030018 */
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020019
Daniel Kingadc32c02016-05-16 18:25:45 -030020#ifndef MBEDTLS_POLY1305_H
21#define MBEDTLS_POLY1305_H
Mateusz Starzyk846f0212021-05-19 19:44:07 +020022#include "mbedtls/private_access.h"
Daniel Kingadc32c02016-05-16 18:25:45 -030023
Bence Szépkútic662b362021-05-27 11:25:03 +020024#include "mbedtls/build_info.h"
Daniel Kingadc32c02016-05-16 18:25:45 -030025
26#include <stdint.h>
27#include <stddef.h>
28
Gilles Peskined2971572021-07-26 18:48:10 +020029/** Invalid input parameter(s). */
30#define MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA -0x0057
Ron Eldor9924bdc2018-10-04 10:59:13 +030031
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020032#ifdef __cplusplus
33extern "C" {
34#endif
35
Gilles Peskine449bd832023-01-11 14:50:10 +010036typedef struct mbedtls_poly1305_context {
Mateusz Starzyk846f0212021-05-19 19:44:07 +020037 uint32_t MBEDTLS_PRIVATE(r)[4]; /** The value for 'r' (low 128 bits of the key). */
38 uint32_t MBEDTLS_PRIVATE(s)[4]; /** The value for 's' (high 128 bits of the key). */
39 uint32_t MBEDTLS_PRIVATE(acc)[5]; /** The accumulator number. */
40 uint8_t MBEDTLS_PRIVATE(queue)[16]; /** The current partial block of data. */
41 size_t MBEDTLS_PRIVATE(queue_len); /** The number of bytes stored in 'queue'. */
Daniel Kingadc32c02016-05-16 18:25:45 -030042}
43mbedtls_poly1305_context;
44
45/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020046 * \brief This function initializes the specified Poly1305 context.
Daniel Kingadc32c02016-05-16 18:25:45 -030047 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020048 * It must be the first API called before using
49 * the context.
50 *
51 * It is usually followed by a call to
52 * \c mbedtls_poly1305_starts(), then one or more calls to
53 * \c mbedtls_poly1305_update(), then one call to
54 * \c mbedtls_poly1305_finish(), then finally
55 * \c mbedtls_poly1305_free().
56 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050057 * \param ctx The Poly1305 context to initialize. This must
58 * not be \c NULL.
Daniel Kingadc32c02016-05-16 18:25:45 -030059 */
Gilles Peskine449bd832023-01-11 14:50:10 +010060void mbedtls_poly1305_init(mbedtls_poly1305_context *ctx);
Daniel Kingadc32c02016-05-16 18:25:45 -030061
62/**
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050063 * \brief This function releases and clears the specified
64 * Poly1305 context.
Daniel Kingadc32c02016-05-16 18:25:45 -030065 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050066 * \param ctx The Poly1305 context to clear. This may be \c NULL, in which
67 * case this function is a no-op. If it is not \c NULL, it must
68 * point to an initialized Poly1305 context.
Daniel Kingadc32c02016-05-16 18:25:45 -030069 */
Gilles Peskine449bd832023-01-11 14:50:10 +010070void mbedtls_poly1305_free(mbedtls_poly1305_context *ctx);
Daniel Kingadc32c02016-05-16 18:25:45 -030071
72/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020073 * \brief This function sets the one-time authentication key.
Daniel Kingadc32c02016-05-16 18:25:45 -030074 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020075 * \warning The key must be unique and unpredictable for each
76 * invocation of Poly1305.
Daniel Kingadc32c02016-05-16 18:25:45 -030077 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020078 * \param ctx The Poly1305 context to which the key should be bound.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050079 * This must be initialized.
80 * \param key The buffer containing the \c 32 Byte (\c 256 Bit) key.
Daniel Kingadc32c02016-05-16 18:25:45 -030081 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020082 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050083 * \return A negative error code on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -030084 */
Gilles Peskine449bd832023-01-11 14:50:10 +010085int mbedtls_poly1305_starts(mbedtls_poly1305_context *ctx,
86 const unsigned char key[32]);
Daniel Kingadc32c02016-05-16 18:25:45 -030087
88/**
Manuel Pégourié-Gonnardd2db09f2018-06-04 12:31:12 +020089 * \brief This functions feeds an input buffer into an ongoing
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020090 * Poly1305 computation.
Daniel Kingadc32c02016-05-16 18:25:45 -030091 *
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +020092 * It is called between \c mbedtls_cipher_poly1305_starts() and
93 * \c mbedtls_cipher_poly1305_finish().
94 * It can be called repeatedly to process a stream of data.
Daniel Kingadc32c02016-05-16 18:25:45 -030095 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020096 * \param ctx The Poly1305 context to use for the Poly1305 operation.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050097 * This must be initialized and bound to a key.
98 * \param ilen The length of the input data in Bytes.
99 * Any value is accepted.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200100 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500101 * This pointer can be \c NULL if `ilen == 0`.
Daniel Kingadc32c02016-05-16 18:25:45 -0300102 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200103 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500104 * \return A negative error code on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300105 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100106int mbedtls_poly1305_update(mbedtls_poly1305_context *ctx,
107 const unsigned char *input,
108 size_t ilen);
Daniel Kingadc32c02016-05-16 18:25:45 -0300109
110/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200111 * \brief This function generates the Poly1305 Message
112 * Authentication Code (MAC).
Daniel Kingadc32c02016-05-16 18:25:45 -0300113 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200114 * \param ctx The Poly1305 context to use for the Poly1305 operation.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500115 * This must be initialized and bound to a key.
116 * \param mac The buffer to where the MAC is written. This must
117 * be a writable buffer of length \c 16 Bytes.
Daniel Kingadc32c02016-05-16 18:25:45 -0300118 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200119 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500120 * \return A negative error code on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300121 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100122int mbedtls_poly1305_finish(mbedtls_poly1305_context *ctx,
123 unsigned char mac[16]);
Daniel Kingadc32c02016-05-16 18:25:45 -0300124
Daniel Kingadc32c02016-05-16 18:25:45 -0300125/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200126 * \brief This function calculates the Poly1305 MAC of the input
127 * buffer with the provided key.
Daniel Kingadc32c02016-05-16 18:25:45 -0300128 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200129 * \warning The key must be unique and unpredictable for each
130 * invocation of Poly1305.
Daniel Kingadc32c02016-05-16 18:25:45 -0300131 *
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500132 * \param key The buffer containing the \c 32 Byte (\c 256 Bit) key.
133 * \param ilen The length of the input data in Bytes.
134 * Any value is accepted.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200135 * \param input The buffer holding the input data.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500136 * This pointer can be \c NULL if `ilen == 0`.
137 * \param mac The buffer to where the MAC is written. This must be
138 * a writable buffer of length \c 16 Bytes.
Daniel Kingadc32c02016-05-16 18:25:45 -0300139 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200140 * \return \c 0 on success.
Andrzej Kurekc470b6b2019-01-31 08:20:20 -0500141 * \return A negative error code on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300142 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100143int mbedtls_poly1305_mac(const unsigned char key[32],
144 const unsigned char *input,
145 size_t ilen,
146 unsigned char mac[16]);
Daniel Kingadc32c02016-05-16 18:25:45 -0300147
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200148#if defined(MBEDTLS_SELF_TEST)
Daniel Kingadc32c02016-05-16 18:25:45 -0300149/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200150 * \brief The Poly1305 checkup routine.
Daniel Kingadc32c02016-05-16 18:25:45 -0300151 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200152 * \return \c 0 on success.
153 * \return \c 1 on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300154 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100155int mbedtls_poly1305_self_test(int verbose);
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200156#endif /* MBEDTLS_SELF_TEST */
Daniel Kingadc32c02016-05-16 18:25:45 -0300157
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200158#ifdef __cplusplus
159}
160#endif
161
Daniel Kingadc32c02016-05-16 18:25:45 -0300162#endif /* MBEDTLS_POLY1305_H */