Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-27 09:37:33

0001 /**
0002  * \file poly1305.h
0003  *
0004  * \brief   This file contains Poly1305 definitions and functions.
0005  *
0006  *          Poly1305 is a one-time message authenticator that can be used to
0007  *          authenticate messages. Poly1305-AES was created by Daniel
0008  *          Bernstein https://cr.yp.to/mac/poly1305-20050329.pdf The generic
0009  *          Poly1305 algorithm (not tied to AES) was also standardized in RFC
0010  *          7539.
0011  *
0012  * \author Daniel King <damaki.gh@gmail.com>
0013  */
0014 
0015 /*
0016  *  Copyright The Mbed TLS Contributors
0017  *  SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
0018  */
0019 
0020 #ifndef MBEDTLS_POLY1305_H
0021 #define MBEDTLS_POLY1305_H
0022 #include "mbedtls/private_access.h"
0023 
0024 #include "mbedtls/build_info.h"
0025 
0026 #include <stdint.h>
0027 #include <stddef.h>
0028 
0029 /** Invalid input parameter(s). */
0030 #define MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA         -0x0057
0031 
0032 #ifdef __cplusplus
0033 extern "C" {
0034 #endif
0035 
0036 #if !defined(MBEDTLS_POLY1305_ALT)
0037 
0038 typedef struct mbedtls_poly1305_context {
0039     uint32_t MBEDTLS_PRIVATE(r)[4];      /** The value for 'r' (low 128 bits of the key). */
0040     uint32_t MBEDTLS_PRIVATE(s)[4];      /** The value for 's' (high 128 bits of the key). */
0041     uint32_t MBEDTLS_PRIVATE(acc)[5];    /** The accumulator number. */
0042     uint8_t MBEDTLS_PRIVATE(queue)[16];  /** The current partial block of data. */
0043     size_t MBEDTLS_PRIVATE(queue_len);   /** The number of bytes stored in 'queue'. */
0044 }
0045 mbedtls_poly1305_context;
0046 
0047 #else  /* MBEDTLS_POLY1305_ALT */
0048 #include "poly1305_alt.h"
0049 #endif /* MBEDTLS_POLY1305_ALT */
0050 
0051 /**
0052  * \brief           This function initializes the specified Poly1305 context.
0053  *
0054  *                  It must be the first API called before using
0055  *                  the context.
0056  *
0057  *                  It is usually followed by a call to
0058  *                  \c mbedtls_poly1305_starts(), then one or more calls to
0059  *                  \c mbedtls_poly1305_update(), then one call to
0060  *                  \c mbedtls_poly1305_finish(), then finally
0061  *                  \c mbedtls_poly1305_free().
0062  *
0063  * \param ctx       The Poly1305 context to initialize. This must
0064  *                  not be \c NULL.
0065  */
0066 void mbedtls_poly1305_init(mbedtls_poly1305_context *ctx);
0067 
0068 /**
0069  * \brief           This function releases and clears the specified
0070  *                  Poly1305 context.
0071  *
0072  * \param ctx       The Poly1305 context to clear. This may be \c NULL, in which
0073  *                  case this function is a no-op. If it is not \c NULL, it must
0074  *                  point to an initialized Poly1305 context.
0075  */
0076 void mbedtls_poly1305_free(mbedtls_poly1305_context *ctx);
0077 
0078 /**
0079  * \brief           This function sets the one-time authentication key.
0080  *
0081  * \warning         The key must be unique and unpredictable for each
0082  *                  invocation of Poly1305.
0083  *
0084  * \param ctx       The Poly1305 context to which the key should be bound.
0085  *                  This must be initialized.
0086  * \param key       The buffer containing the \c 32 Byte (\c 256 Bit) key.
0087  *
0088  * \return          \c 0 on success.
0089  * \return          A negative error code on failure.
0090  */
0091 int mbedtls_poly1305_starts(mbedtls_poly1305_context *ctx,
0092                             const unsigned char key[32]);
0093 
0094 /**
0095  * \brief           This functions feeds an input buffer into an ongoing
0096  *                  Poly1305 computation.
0097  *
0098  *                  It is called between \c mbedtls_cipher_poly1305_starts() and
0099  *                  \c mbedtls_cipher_poly1305_finish().
0100  *                  It can be called repeatedly to process a stream of data.
0101  *
0102  * \param ctx       The Poly1305 context to use for the Poly1305 operation.
0103  *                  This must be initialized and bound to a key.
0104  * \param ilen      The length of the input data in Bytes.
0105  *                  Any value is accepted.
0106  * \param input     The buffer holding the input data.
0107  *                  This pointer can be \c NULL if `ilen == 0`.
0108  *
0109  * \return          \c 0 on success.
0110  * \return          A negative error code on failure.
0111  */
0112 int mbedtls_poly1305_update(mbedtls_poly1305_context *ctx,
0113                             const unsigned char *input,
0114                             size_t ilen);
0115 
0116 /**
0117  * \brief           This function generates the Poly1305 Message
0118  *                  Authentication Code (MAC).
0119  *
0120  * \param ctx       The Poly1305 context to use for the Poly1305 operation.
0121  *                  This must be initialized and bound to a key.
0122  * \param mac       The buffer to where the MAC is written. This must
0123  *                  be a writable buffer of length \c 16 Bytes.
0124  *
0125  * \return          \c 0 on success.
0126  * \return          A negative error code on failure.
0127  */
0128 int mbedtls_poly1305_finish(mbedtls_poly1305_context *ctx,
0129                             unsigned char mac[16]);
0130 
0131 /**
0132  * \brief           This function calculates the Poly1305 MAC of the input
0133  *                  buffer with the provided key.
0134  *
0135  * \warning         The key must be unique and unpredictable for each
0136  *                  invocation of Poly1305.
0137  *
0138  * \param key       The buffer containing the \c 32 Byte (\c 256 Bit) key.
0139  * \param ilen      The length of the input data in Bytes.
0140  *                  Any value is accepted.
0141  * \param input     The buffer holding the input data.
0142  *                  This pointer can be \c NULL if `ilen == 0`.
0143  * \param mac       The buffer to where the MAC is written. This must be
0144  *                  a writable buffer of length \c 16 Bytes.
0145  *
0146  * \return          \c 0 on success.
0147  * \return          A negative error code on failure.
0148  */
0149 int mbedtls_poly1305_mac(const unsigned char key[32],
0150                          const unsigned char *input,
0151                          size_t ilen,
0152                          unsigned char mac[16]);
0153 
0154 #if defined(MBEDTLS_SELF_TEST)
0155 /**
0156  * \brief           The Poly1305 checkup routine.
0157  *
0158  * \return          \c 0 on success.
0159  * \return          \c 1 on failure.
0160  */
0161 int mbedtls_poly1305_self_test(int verbose);
0162 #endif /* MBEDTLS_SELF_TEST */
0163 
0164 #ifdef __cplusplus
0165 }
0166 #endif
0167 
0168 #endif /* MBEDTLS_POLY1305_H */