blob: f0ec44c96833a66e575dc8719314dbf8abbb7e97 [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
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)
GuHaijun983acb72018-12-28 11:11:10 +080037#include "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
47/* MBEDTLS_ERR_POLY1305_FEATURE_UNAVAILABLE is deprecated and should not be
48 * used. */
Manuel Pégourié-Gonnardb8bd80a2018-05-09 09:54:51 +020049#define MBEDTLS_ERR_POLY1305_FEATURE_UNAVAILABLE -0x0059 /**< Feature not available. For example, s part of the API is not implemented. */
Ron Eldor9924bdc2018-10-04 10:59:13 +030050
51/* MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED is deprecated and should not be used.
52 */
Manuel Pégourié-Gonnard3798b6b2018-05-24 13:27:45 +020053#define MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED -0x005B /**< Poly1305 hardware accelerator failed. */
Daniel Kingadc32c02016-05-16 18:25:45 -030054
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +020055#ifdef __cplusplus
56extern "C" {
57#endif
58
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020059#if !defined(MBEDTLS_POLY1305_ALT)
60
Dawid Drozd428cc522018-07-24 10:02:47 +020061typedef struct mbedtls_poly1305_context
Daniel Kingadc32c02016-05-16 18:25:45 -030062{
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020063 uint32_t r[4]; /** The value for 'r' (low 128 bits of the key). */
64 uint32_t s[4]; /** The value for 's' (high 128 bits of the key). */
65 uint32_t acc[5]; /** The accumulator number. */
66 uint8_t queue[16]; /** The current partial block of data. */
67 size_t queue_len; /** The number of bytes stored in 'queue'. */
Daniel Kingadc32c02016-05-16 18:25:45 -030068}
69mbedtls_poly1305_context;
70
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020071#else /* MBEDTLS_POLY1305_ALT */
72#include "poly1305_alt.h"
73#endif /* MBEDTLS_POLY1305_ALT */
74
Daniel Kingadc32c02016-05-16 18:25:45 -030075/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020076 * \brief This function initializes the specified Poly1305 context.
Daniel Kingadc32c02016-05-16 18:25:45 -030077 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +020078 * It must be the first API called before using
79 * the context.
80 *
81 * It is usually followed by a call to
82 * \c mbedtls_poly1305_starts(), then one or more calls to
83 * \c mbedtls_poly1305_update(), then one call to
84 * \c mbedtls_poly1305_finish(), then finally
85 * \c mbedtls_poly1305_free().
86 *
Hanno Beckerad7581f2018-12-17 09:43:21 +000087 * \param ctx The Poly1305 context to initialize. This must
88 * not be \c NULL.
Daniel Kingadc32c02016-05-16 18:25:45 -030089 */
90void mbedtls_poly1305_init( mbedtls_poly1305_context *ctx );
91
92/**
Hanno Beckerad7581f2018-12-17 09:43:21 +000093 * \brief This function releases and clears the specified
94 * Poly1305 context.
Daniel Kingadc32c02016-05-16 18:25:45 -030095 *
Hanno Beckerad7581f2018-12-17 09:43:21 +000096 * \param ctx The Poly1305 context to clear. This may be \c NULL, in which
97 * case this function is a no-op. If it is not \c NULL, it must
98 * point to an initialized Poly1305 context.
Daniel Kingadc32c02016-05-16 18:25:45 -030099 */
100void mbedtls_poly1305_free( mbedtls_poly1305_context *ctx );
101
102/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200103 * \brief This function sets the one-time authentication key.
Daniel Kingadc32c02016-05-16 18:25:45 -0300104 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200105 * \warning The key must be unique and unpredictable for each
106 * invocation of Poly1305.
Daniel Kingadc32c02016-05-16 18:25:45 -0300107 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200108 * \param ctx The Poly1305 context to which the key should be bound.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000109 * This must be initialized.
110 * \param key The buffer containing the \c 32 Byte (\c 256 Bit) key.
Daniel Kingadc32c02016-05-16 18:25:45 -0300111 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200112 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000113 * \return A negative error code on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300114 */
Manuel Pégourié-Gonnard4edd51b2018-05-07 10:21:56 +0200115int mbedtls_poly1305_starts( mbedtls_poly1305_context *ctx,
Daniel Kingadc32c02016-05-16 18:25:45 -0300116 const unsigned char key[32] );
117
118/**
Manuel Pégourié-Gonnardd2db09f2018-06-04 12:31:12 +0200119 * \brief This functions feeds an input buffer into an ongoing
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200120 * Poly1305 computation.
Daniel Kingadc32c02016-05-16 18:25:45 -0300121 *
Manuel Pégourié-Gonnardc7bc9e12018-06-18 10:30:30 +0200122 * It is called between \c mbedtls_cipher_poly1305_starts() and
123 * \c mbedtls_cipher_poly1305_finish().
124 * It can be called repeatedly to process a stream of data.
Daniel Kingadc32c02016-05-16 18:25:45 -0300125 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200126 * \param ctx The Poly1305 context to use for the Poly1305 operation.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000127 * This must be initialized and bound to a key.
128 * \param ilen The length of the input data in Bytes.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000129 * Any value is accepted.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200130 * \param input The buffer holding the input data.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000131 * This pointer can be \c NULL if `ilen == 0`.
Daniel Kingadc32c02016-05-16 18:25:45 -0300132 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200133 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000134 * \return A negative error code on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300135 */
136int mbedtls_poly1305_update( mbedtls_poly1305_context *ctx,
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200137 const unsigned char *input,
138 size_t ilen );
Daniel Kingadc32c02016-05-16 18:25:45 -0300139
140/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200141 * \brief This function generates the Poly1305 Message
142 * Authentication Code (MAC).
Daniel Kingadc32c02016-05-16 18:25:45 -0300143 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200144 * \param ctx The Poly1305 context to use for the Poly1305 operation.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000145 * This must be initialized and bound to a key.
146 * \param mac The buffer to where the MAC is written. This must
147 * be a writable buffer of length \c 16 Bytes.
Daniel Kingadc32c02016-05-16 18:25:45 -0300148 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200149 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000150 * \return A negative error code on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300151 */
152int mbedtls_poly1305_finish( mbedtls_poly1305_context *ctx,
153 unsigned char mac[16] );
154
Daniel Kingadc32c02016-05-16 18:25:45 -0300155/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200156 * \brief This function calculates the Poly1305 MAC of the input
157 * buffer with the provided key.
Daniel Kingadc32c02016-05-16 18:25:45 -0300158 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200159 * \warning The key must be unique and unpredictable for each
160 * invocation of Poly1305.
Daniel Kingadc32c02016-05-16 18:25:45 -0300161 *
Hanno Beckerad7581f2018-12-17 09:43:21 +0000162 * \param key The buffer containing the \c 32 Byte (\c 256 Bit) key.
163 * \param ilen The length of the input data in Bytes.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000164 * Any value is accepted.
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200165 * \param input The buffer holding the input data.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000166 * This pointer can be \c NULL if `ilen == 0`.
Hanno Beckerad7581f2018-12-17 09:43:21 +0000167 * \param mac The buffer to where the MAC is written. This must be
168 * a writable buffer of length \c 16 Bytes.
Daniel Kingadc32c02016-05-16 18:25:45 -0300169 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200170 * \return \c 0 on success.
Hanno Beckerb3c10b32018-12-11 14:52:01 +0000171 * \return A negative error code on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300172 */
173int mbedtls_poly1305_mac( const unsigned char key[32],
Daniel Kingadc32c02016-05-16 18:25:45 -0300174 const unsigned char *input,
Manuel Pégourié-Gonnardb1ac5e72018-05-09 09:25:00 +0200175 size_t ilen,
Daniel Kingadc32c02016-05-16 18:25:45 -0300176 unsigned char mac[16] );
177
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200178#if defined(MBEDTLS_SELF_TEST)
Daniel Kingadc32c02016-05-16 18:25:45 -0300179/**
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200180 * \brief The Poly1305 checkup routine.
Daniel Kingadc32c02016-05-16 18:25:45 -0300181 *
Manuel Pégourié-Gonnardb500f8b2018-05-08 12:43:48 +0200182 * \return \c 0 on success.
183 * \return \c 1 on failure.
Daniel Kingadc32c02016-05-16 18:25:45 -0300184 */
185int mbedtls_poly1305_self_test( int verbose );
Manuel Pégourié-Gonnardc22e61a2018-05-24 13:51:05 +0200186#endif /* MBEDTLS_SELF_TEST */
Daniel Kingadc32c02016-05-16 18:25:45 -0300187
Manuel Pégourié-Gonnard823b7a02018-05-07 10:10:30 +0200188#ifdef __cplusplus
189}
190#endif
191
Daniel Kingadc32c02016-05-16 18:25:45 -0300192#endif /* MBEDTLS_POLY1305_H */