Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers poly1305.h Source File

poly1305.h

Go to the documentation of this file.
00001 /**
00002  * \file poly1305.h
00003  *
00004  * \brief   This file contains Poly1305 definitions and functions.
00005  *
00006  *          Poly1305 is a one-time message authenticator that can be used to
00007  *          authenticate messages. Poly1305-AES was created by Daniel
00008  *          Bernstein https://cr.yp.to/mac/poly1305-20050329.pdf The generic
00009  *          Poly1305 algorithm (not tied to AES) was also standardized in RFC
00010  *          7539.
00011  *
00012  * \author Daniel King <damaki.gh@gmail.com>
00013  */
00014 
00015 /*  Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved.
00016  *  SPDX-License-Identifier: Apache-2.0
00017  *
00018  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00019  *  not use this file except in compliance with the License.
00020  *  You may obtain a copy of the License at
00021  *
00022  *  http://www.apache.org/licenses/LICENSE-2.0
00023  *
00024  *  Unless required by applicable law or agreed to in writing, software
00025  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00026  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00027  *  See the License for the specific language governing permissions and
00028  *  limitations under the License.
00029  *
00030  *  This file is part of Mbed TLS (https://tls.mbed.org)
00031  */
00032 
00033 #ifndef MBEDTLS_POLY1305_H
00034 #define MBEDTLS_POLY1305_H
00035 
00036 #if !defined(MBEDTLS_CONFIG_FILE)
00037 #include "mbedtls/config.h"
00038 #else
00039 #include MBEDTLS_CONFIG_FILE
00040 #endif
00041 
00042 #include <stdint.h>
00043 #include <stddef.h>
00044 
00045 #define MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA         -0x0057 /**< Invalid input parameter(s). */
00046 
00047 /* MBEDTLS_ERR_POLY1305_FEATURE_UNAVAILABLE is deprecated and should not be
00048  * used. */
00049 #define MBEDTLS_ERR_POLY1305_FEATURE_UNAVAILABLE    -0x0059 /**< Feature not available. For example, s part of the API is not implemented. */
00050 
00051 /* MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED is deprecated and should not be used.
00052  */
00053 #define MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED        -0x005B  /**< Poly1305 hardware accelerator failed. */
00054 
00055 #ifdef __cplusplus
00056 extern "C" {
00057 #endif
00058 
00059 #if !defined(MBEDTLS_POLY1305_ALT)
00060 
00061 typedef struct mbedtls_poly1305_context
00062 {
00063     uint32_t r[4];      /** The value for 'r' (low 128 bits of the key). */
00064     uint32_t s[4];      /** The value for 's' (high 128 bits of the key). */
00065     uint32_t acc[5];    /** The accumulator number. */
00066     uint8_t queue[16];  /** The current partial block of data. */
00067     size_t queue_len;   /** The number of bytes stored in 'queue'. */
00068 }
00069 mbedtls_poly1305_context;
00070 
00071 #else  /* MBEDTLS_POLY1305_ALT */
00072 #include "poly1305_alt.h"
00073 #endif /* MBEDTLS_POLY1305_ALT */
00074 
00075 /**
00076  * \brief           This function initializes the specified Poly1305 context.
00077  *
00078  *                  It must be the first API called before using
00079  *                  the context.
00080  *
00081  *                  It is usually followed by a call to
00082  *                  \c mbedtls_poly1305_starts(), then one or more calls to
00083  *                  \c mbedtls_poly1305_update(), then one call to
00084  *                  \c mbedtls_poly1305_finish(), then finally
00085  *                  \c mbedtls_poly1305_free().
00086  *
00087  * \param ctx       The Poly1305 context to initialize. This must
00088  *                  not be \c NULL.
00089  */
00090 void mbedtls_poly1305_init( mbedtls_poly1305_context *ctx );
00091 
00092 /**
00093  * \brief           This function releases and clears the specified
00094  *                  Poly1305 context.
00095  *
00096  * \param ctx       The Poly1305 context to clear. This may be \c NULL, in which
00097  *                  case this function is a no-op. If it is not \c NULL, it must
00098  *                  point to an initialized Poly1305 context.
00099  */
00100 void mbedtls_poly1305_free( mbedtls_poly1305_context *ctx );
00101 
00102 /**
00103  * \brief           This function sets the one-time authentication key.
00104  *
00105  * \warning         The key must be unique and unpredictable for each
00106  *                  invocation of Poly1305.
00107  *
00108  * \param ctx       The Poly1305 context to which the key should be bound.
00109  *                  This must be initialized.
00110  * \param key       The buffer containing the \c 32 Byte (\c 256 Bit) key.
00111  *
00112  * \return          \c 0 on success.
00113  * \return          A negative error code on failure.
00114  */
00115 int mbedtls_poly1305_starts( mbedtls_poly1305_context *ctx,
00116                              const unsigned char key[32] );
00117 
00118 /**
00119  * \brief           This functions feeds an input buffer into an ongoing
00120  *                  Poly1305 computation.
00121  *
00122  *                  It is called between \c mbedtls_cipher_poly1305_starts() and
00123  *                  \c mbedtls_cipher_poly1305_finish().
00124  *                  It can be called repeatedly to process a stream of data.
00125  *
00126  * \param ctx       The Poly1305 context to use for the Poly1305 operation.
00127  *                  This must be initialized and bound to a key.
00128  * \param ilen      The length of the input data in Bytes.
00129  *                  Any value is accepted.
00130  * \param input     The buffer holding the input data.
00131  *                  This pointer can be \c NULL if `ilen == 0`.
00132  *
00133  * \return          \c 0 on success.
00134  * \return          A negative error code on failure.
00135  */
00136 int mbedtls_poly1305_update( mbedtls_poly1305_context *ctx,
00137                              const unsigned char *input,
00138                              size_t ilen );
00139 
00140 /**
00141  * \brief           This function generates the Poly1305 Message
00142  *                  Authentication Code (MAC).
00143  *
00144  * \param ctx       The Poly1305 context to use for the Poly1305 operation.
00145  *                  This must be initialized and bound to a key.
00146  * \param mac       The buffer to where the MAC is written. This must
00147  *                  be a writable buffer of length \c 16 Bytes.
00148  *
00149  * \return          \c 0 on success.
00150  * \return          A negative error code on failure.
00151  */
00152 int mbedtls_poly1305_finish( mbedtls_poly1305_context *ctx,
00153                              unsigned char mac[16] );
00154 
00155 /**
00156  * \brief           This function calculates the Poly1305 MAC of the input
00157  *                  buffer with the provided key.
00158  *
00159  * \warning         The key must be unique and unpredictable for each
00160  *                  invocation of Poly1305.
00161  *
00162  * \param key       The buffer containing the \c 32 Byte (\c 256 Bit) key.
00163  * \param ilen      The length of the input data in Bytes.
00164  *                  Any value is accepted.
00165  * \param input     The buffer holding the input data.
00166  *                  This pointer can be \c NULL if `ilen == 0`.
00167  * \param mac       The buffer to where the MAC is written. This must be
00168  *                  a writable buffer of length \c 16 Bytes.
00169  *
00170  * \return          \c 0 on success.
00171  * \return          A negative error code on failure.
00172  */
00173 int mbedtls_poly1305_mac( const unsigned char key[32],
00174                           const unsigned char *input,
00175                           size_t ilen,
00176                           unsigned char mac[16] );
00177 
00178 #if defined(MBEDTLS_SELF_TEST)
00179 /**
00180  * \brief           The Poly1305 checkup routine.
00181  *
00182  * \return          \c 0 on success.
00183  * \return          \c 1 on failure.
00184  */
00185 int mbedtls_poly1305_self_test( int verbose );
00186 #endif /* MBEDTLS_SELF_TEST */
00187 
00188 #ifdef __cplusplus
00189 }
00190 #endif
00191 
00192 #endif /* MBEDTLS_POLY1305_H */