Mbed OS Reference
Loading...
Searching...
No Matches
poly1305.h
Go to the documentation of this file.
1/**
2 * \file poly1305.h
3 *
4 * \brief This file contains Poly1305 definitions and functions.
5 *
6 * 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/*
16 * Copyright The Mbed TLS Contributors
17 * 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.
30 */
31
32#ifndef MBEDTLS_POLY1305_H
33#define MBEDTLS_POLY1305_H
34
35#if !defined(MBEDTLS_CONFIG_FILE)
36#include "mbedtls/config.h"
37#else
38#include MBEDTLS_CONFIG_FILE
39#endif
40
41#include <stdint.h>
42#include <stddef.h>
43
44/**
45 * \addtogroup mbedtls
46 * \{
47 * \defgroup mbedtls_poly1305_module Poly1305
48 * \{
49 */
50
51#define MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA -0x0057 /**< Invalid input parameter(s). */
52
53/* MBEDTLS_ERR_POLY1305_FEATURE_UNAVAILABLE is deprecated and should not be
54 * used. */
55#define MBEDTLS_ERR_POLY1305_FEATURE_UNAVAILABLE -0x0059 /**< Feature not available. For example, s part of the API is not implemented. */
56
57/* MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED is deprecated and should not be used.
58 */
59#define MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED -0x005B /**< Poly1305 hardware accelerator failed. */
60
61#ifdef __cplusplus
62extern "C" {
63#endif
64
65#if !defined(MBEDTLS_POLY1305_ALT)
66
68{
69 uint32_t r[4]; /** The value for 'r' (low 128 bits of the key). */
70 uint32_t s[4]; /** The value for 's' (high 128 bits of the key). */
71 uint32_t acc[5]; /** The accumulator number. */
72 uint8_t queue[16]; /** The current partial block of data. */
73 size_t queue_len; /** The number of bytes stored in 'queue'. */
74}
76
77#else /* MBEDTLS_POLY1305_ALT */
78#include "poly1305_alt.h"
79#endif /* MBEDTLS_POLY1305_ALT */
80
81/**
82 * \brief This function initializes the specified Poly1305 context.
83 *
84 * It must be the first API called before using
85 * the context.
86 *
87 * It is usually followed by a call to
88 * \c mbedtls_poly1305_starts(), then one or more calls to
89 * \c mbedtls_poly1305_update(), then one call to
90 * \c mbedtls_poly1305_finish(), then finally
91 * \c mbedtls_poly1305_free().
92 *
93 * \param ctx The Poly1305 context to initialize. This must
94 * not be \c NULL.
95 */
97
98/**
99 * \brief This function releases and clears the specified
100 * Poly1305 context.
101 *
102 * \param ctx The Poly1305 context to clear. This may be \c NULL, in which
103 * case this function is a no-op. If it is not \c NULL, it must
104 * point to an initialized Poly1305 context.
105 */
107
108/**
109 * \brief This function sets the one-time authentication key.
110 *
111 * \warning The key must be unique and unpredictable for each
112 * invocation of Poly1305.
113 *
114 * \param ctx The Poly1305 context to which the key should be bound.
115 * This must be initialized.
116 * \param key The buffer containing the \c 32 Byte (\c 256 Bit) key.
117 *
118 * \return \c 0 on success.
119 * \return A negative error code on failure.
120 */
122 const unsigned char key[32] );
123
124/**
125 * \brief This functions feeds an input buffer into an ongoing
126 * Poly1305 computation.
127 *
128 * It is called between \c mbedtls_cipher_poly1305_starts() and
129 * \c mbedtls_cipher_poly1305_finish().
130 * It can be called repeatedly to process a stream of data.
131 *
132 * \param ctx The Poly1305 context to use for the Poly1305 operation.
133 * This must be initialized and bound to a key.
134 * \param ilen The length of the input data in Bytes.
135 * Any value is accepted.
136 * \param input The buffer holding the input data.
137 * This pointer can be \c NULL if `ilen == 0`.
138 *
139 * \return \c 0 on success.
140 * \return A negative error code on failure.
141 */
143 const unsigned char *input,
144 size_t ilen );
145
146/**
147 * \brief This function generates the Poly1305 Message
148 * Authentication Code (MAC).
149 *
150 * \param ctx The Poly1305 context to use for the Poly1305 operation.
151 * This must be initialized and bound to a key.
152 * \param mac The buffer to where the MAC is written. This must
153 * be a writable buffer of length \c 16 Bytes.
154 *
155 * \return \c 0 on success.
156 * \return A negative error code on failure.
157 */
159 unsigned char mac[16] );
160
161/**
162 * \brief This function calculates the Poly1305 MAC of the input
163 * buffer with the provided key.
164 *
165 * \warning The key must be unique and unpredictable for each
166 * invocation of Poly1305.
167 *
168 * \param key The buffer containing the \c 32 Byte (\c 256 Bit) key.
169 * \param ilen The length of the input data in Bytes.
170 * Any value is accepted.
171 * \param input The buffer holding the input data.
172 * This pointer can be \c NULL if `ilen == 0`.
173 * \param mac The buffer to where the MAC is written. This must be
174 * a writable buffer of length \c 16 Bytes.
175 *
176 * \return \c 0 on success.
177 * \return A negative error code on failure.
178 */
179int mbedtls_poly1305_mac( const unsigned char key[32],
180 const unsigned char *input,
181 size_t ilen,
182 unsigned char mac[16] );
183
184#if defined(MBEDTLS_SELF_TEST)
185/**
186 * \brief The Poly1305 checkup routine.
187 *
188 * \return \c 0 on success.
189 * \return \c 1 on failure.
190 */
191int mbedtls_poly1305_self_test( int verbose );
192#endif /* MBEDTLS_SELF_TEST */
193
194#ifdef __cplusplus
195}
196#endif
197
198/// \}
199/// \}
200
201#endif /* MBEDTLS_POLY1305_H */
Configuration options (set of defines)
int mbedtls_poly1305_update(mbedtls_poly1305_context *ctx, const unsigned char *input, size_t ilen)
This functions feeds an input buffer into an ongoing Poly1305 computation.
int mbedtls_poly1305_mac(const unsigned char key[32], const unsigned char *input, size_t ilen, unsigned char mac[16])
This function calculates the Poly1305 MAC of the input buffer with the provided key.
int mbedtls_poly1305_finish(mbedtls_poly1305_context *ctx, unsigned char mac[16])
This function generates the Poly1305 Message Authentication Code (MAC).
void mbedtls_poly1305_init(mbedtls_poly1305_context *ctx)
This function initializes the specified Poly1305 context.
int mbedtls_poly1305_starts(mbedtls_poly1305_context *ctx, const unsigned char key[32])
This function sets the one-time authentication key.
void mbedtls_poly1305_free(mbedtls_poly1305_context *ctx)
This function releases and clears the specified Poly1305 context.
size_t queue_len
The current partial block of data.
Definition: poly1305.h:73
uint8_t queue[16]
The accumulator number.
Definition: poly1305.h:72
uint32_t s[4]
The value for 'r' (low 128 bits of the key).
Definition: poly1305.h:70
uint32_t acc[5]
The value for 's' (high 128 bits of the key).
Definition: poly1305.h:71