blob: 1aa55aeee48cd958e29691d34cfbf89a343a1454 [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
35#if !defined(MBEDTLS_POLY1305_ALT)
36
37#define MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA -0x0041 /**< Invalid input parameter(s). */
38
39typedef 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
49/**
50 * \brief Initialize a Poly1305 context
51 *
52 * \param ctx The Poly1305 context to be initialized
53 */
54void mbedtls_poly1305_init( mbedtls_poly1305_context *ctx );
55
56/**
57 * \brief Clear a Poly1305 context
58 *
59 * \param ctx The Poly1305 context to be cleared
60 */
61void mbedtls_poly1305_free( mbedtls_poly1305_context *ctx );
62
63/**
64 * \brief Set the Poly1305 authentication key.
65 *
66 * \warning The key should be unique, and \b MUST be
67 * unpredictable for each invocation of Poly1305.
68 *
69 * \param ctx The Poly1305 context.
70 * \param key Buffer containing the 256-bit key.
71 *
72 * \return MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA is returned if ctx
73 * or key are NULL.
74 * Otherwise, 0 is returned to indicate success.
75 */
76int mbedtls_poly1305_setkey( mbedtls_poly1305_context *ctx,
77 const unsigned char key[32] );
78
79/**
80 * \brief Process data with Poly1305.
81 *
82 * This function can be called multiple times to process
83 * a stream of data.
84 *
85 * \param ctx The Poly1305 context.
86 * \param ilen The input length (in bytes). Any value is accepted.
87 * \param input Buffer containing the input data to Process.
88 *
89 * \return MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA is returned if ctx
90 * or input are NULL.
91 * Otherwise, 0 is returned to indicate success.
92 */
93int mbedtls_poly1305_update( mbedtls_poly1305_context *ctx,
94 size_t ilen,
95 const unsigned char *input );
96
97/**
98 * \brief Generate the Poly1305 MAC.
99 *
100 * \param ctx The Poly1305 context.
101 * \param mac Buffer to where the MAC is written. Must be big enough
102 * to hold the 16-byte MAC.
103 *
104 * \return MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA is returned if ctx
105 * or mac are NULL.
106 * Otherwise, 0 is returned to indicate success.
107 */
108int mbedtls_poly1305_finish( mbedtls_poly1305_context *ctx,
109 unsigned char mac[16] );
110
111#else /* MBEDTLS_POLY1305_ALT */
112#include "poly1305_alt.h"
113#endif /* MBEDTLS_POLY1305_ALT */
114
115/**
116 * \brief Generate the Poly1305 MAC of some data with the given key.
117 *
118 * \warning The key should be unique, and \b MUST be
119 * unpredictable for each invocation of Poly1305.
120 *
121 * \param key Buffer containing the 256-bit (32 bytes) key.
122 * \param ilen The length of the input data (in bytes).
123 * \param input Buffer containing the input data to process.
124 * \param mac Buffer to where the 128-bit (16 bytes) MAC is written.
125 *
126 * \return MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA is returned if key,
127 * input, or mac are NULL.
128 * Otherwise, 0 is returned to indicate success.
129 */
130int mbedtls_poly1305_mac( const unsigned char key[32],
131 size_t ilen,
132 const unsigned char *input,
133 unsigned char mac[16] );
134
135/**
136 * \brief Checkup routine
137 *
138 * \return 0 if successful, or 1 if the test failed
139 */
140int mbedtls_poly1305_self_test( int verbose );
141
142#endif /* MBEDTLS_POLY1305_H */