blob: 915f8ab0d258438ae2f7f5137ffbfc662f6f410a [file] [log] [blame]
Daniel Kingadc32c02016-05-16 18:25:45 -03001/**
2 * \file poly1305.h
3 *
4 * \brief Poly1305 authenticator algorithm.
5 *
6 * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
7 * SPDX-License-Identifier: Apache-2.0
8 *
9 * Licensed under the Apache License, Version 2.0 (the "License"); you may
10 * not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
17 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 *
21 * This file is part of mbed TLS (https://tls.mbed.org)
22 */
23#ifndef MBEDTLS_POLY1305_H
24#define MBEDTLS_POLY1305_H
25
26#if !defined(MBEDTLS_CONFIG_FILE)
27#include "mbedtls/config.h"
28#else
29#include MBEDTLS_CONFIG_FILE
30#endif
31
32#include <stdint.h>
33#include <stddef.h>
34
Daniel Kingadc32c02016-05-16 18:25:45 -030035#define MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA -0x0041 /**< Invalid input parameter(s). */
36
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020037#if !defined(MBEDTLS_POLY1305_ALT)
38
Daniel Kingadc32c02016-05-16 18:25:45 -030039typedef struct
40{
41 uint32_t r[4]; /** Stores the value for 'r' (low 128 bits of the key) */
42 uint32_t s[4]; /** Stores the value for 's' (high 128 bits of the key) */
43 uint32_t acc[5]; /** Accumulator number */
44 uint8_t queue[16]; /** Stores partial block data */
45 size_t queue_len; /** Number of bytes stored in 'queue'. Always less than 16 */
46}
47mbedtls_poly1305_context;
48
Manuel Pégourié-Gonnard95d0bdb2018-05-07 09:58:35 +020049#else /* MBEDTLS_POLY1305_ALT */
50#include "poly1305_alt.h"
51#endif /* MBEDTLS_POLY1305_ALT */
52
Daniel Kingadc32c02016-05-16 18:25:45 -030053/**
54 * \brief Initialize a Poly1305 context
55 *
56 * \param ctx The Poly1305 context to be initialized
57 */
58void mbedtls_poly1305_init( mbedtls_poly1305_context *ctx );
59
60/**
61 * \brief Clear a Poly1305 context
62 *
63 * \param ctx The Poly1305 context to be cleared
64 */
65void mbedtls_poly1305_free( mbedtls_poly1305_context *ctx );
66
67/**
68 * \brief Set the Poly1305 authentication key.
69 *
70 * \warning The key should be unique, and \b MUST be
71 * unpredictable for each invocation of Poly1305.
72 *
73 * \param ctx The Poly1305 context.
74 * \param key Buffer containing the 256-bit key.
75 *
76 * \return MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA is returned if ctx
77 * or key are NULL.
78 * Otherwise, 0 is returned to indicate success.
79 */
80int mbedtls_poly1305_setkey( mbedtls_poly1305_context *ctx,
81 const unsigned char key[32] );
82
83/**
84 * \brief Process data with Poly1305.
85 *
86 * This function can be called multiple times to process
87 * a stream of data.
88 *
89 * \param ctx The Poly1305 context.
90 * \param ilen The input length (in bytes). Any value is accepted.
91 * \param input Buffer containing the input data to Process.
Daniel Kinga310c5e2016-05-17 15:56:26 -030092 * This pointer can be NULL if ilen == 0.
Daniel Kingadc32c02016-05-16 18:25:45 -030093 *
94 * \return MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA is returned if ctx
95 * or input are NULL.
96 * Otherwise, 0 is returned to indicate success.
97 */
98int mbedtls_poly1305_update( mbedtls_poly1305_context *ctx,
99 size_t ilen,
100 const unsigned char *input );
101
102/**
103 * \brief Generate the Poly1305 MAC.
104 *
105 * \param ctx The Poly1305 context.
106 * \param mac Buffer to where the MAC is written. Must be big enough
107 * to hold the 16-byte MAC.
108 *
109 * \return MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA is returned if ctx
110 * or mac are NULL.
111 * Otherwise, 0 is returned to indicate success.
112 */
113int mbedtls_poly1305_finish( mbedtls_poly1305_context *ctx,
114 unsigned char mac[16] );
115
Daniel Kingadc32c02016-05-16 18:25:45 -0300116/**
117 * \brief Generate the Poly1305 MAC of some data with the given key.
118 *
119 * \warning The key should be unique, and \b MUST be
120 * unpredictable for each invocation of Poly1305.
121 *
122 * \param key Buffer containing the 256-bit (32 bytes) key.
123 * \param ilen The length of the input data (in bytes).
124 * \param input Buffer containing the input data to process.
125 * \param mac Buffer to where the 128-bit (16 bytes) MAC is written.
126 *
127 * \return MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA is returned if key,
128 * input, or mac are NULL.
129 * Otherwise, 0 is returned to indicate success.
130 */
131int mbedtls_poly1305_mac( const unsigned char key[32],
132 size_t ilen,
133 const unsigned char *input,
134 unsigned char mac[16] );
135
136/**
137 * \brief Checkup routine
138 *
139 * \return 0 if successful, or 1 if the test failed
140 */
141int mbedtls_poly1305_self_test( int verbose );
142
143#endif /* MBEDTLS_POLY1305_H */