Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:50

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 "tf-psa-crypto/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         PSA_ERROR_INVALID_ARGUMENT
0031 
0032 #ifdef __cplusplus
0033 extern "C" {
0034 #endif
0035 
0036 typedef struct mbedtls_poly1305_context {
0037     uint32_t MBEDTLS_PRIVATE(r)[4];      /** The value for 'r' (low 128 bits of the key). */
0038     uint32_t MBEDTLS_PRIVATE(s)[4];      /** The value for 's' (high 128 bits of the key). */
0039     uint32_t MBEDTLS_PRIVATE(acc)[5];    /** The accumulator number. */
0040     uint8_t MBEDTLS_PRIVATE(queue)[16];  /** The current partial block of data. */
0041     size_t MBEDTLS_PRIVATE(queue_len);   /** The number of bytes stored in 'queue'. */
0042 }
0043 mbedtls_poly1305_context;
0044 
0045 #if defined(MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS)
0046 /**
0047  * \brief           This function initializes the specified Poly1305 context.
0048  *
0049  *                  It must be the first API called before using
0050  *                  the context.
0051  *
0052  *                  It is usually followed by a call to
0053  *                  \c mbedtls_poly1305_starts(), then one or more calls to
0054  *                  \c mbedtls_poly1305_update(), then one call to
0055  *                  \c mbedtls_poly1305_finish(), then finally
0056  *                  \c mbedtls_poly1305_free().
0057  *
0058  * \param ctx       The Poly1305 context to initialize. This must
0059  *                  not be \c NULL.
0060  */
0061 void mbedtls_poly1305_init(mbedtls_poly1305_context *ctx);
0062 
0063 /**
0064  * \brief           This function releases and clears the specified
0065  *                  Poly1305 context.
0066  *
0067  * \param ctx       The Poly1305 context to clear. This may be \c NULL, in which
0068  *                  case this function is a no-op. If it is not \c NULL, it must
0069  *                  point to an initialized Poly1305 context.
0070  */
0071 void mbedtls_poly1305_free(mbedtls_poly1305_context *ctx);
0072 
0073 /**
0074  * \brief           This function sets the one-time authentication key.
0075  *
0076  * \warning         The key must be unique and unpredictable for each
0077  *                  invocation of Poly1305.
0078  *
0079  * \param ctx       The Poly1305 context to which the key should be bound.
0080  *                  This must be initialized.
0081  * \param key       The buffer containing the \c 32 Byte (\c 256 Bit) key.
0082  *
0083  * \return          \c 0 on success.
0084  * \return          A negative error code on failure.
0085  */
0086 int mbedtls_poly1305_starts(mbedtls_poly1305_context *ctx,
0087                             const unsigned char key[32]);
0088 
0089 /**
0090  * \brief           This functions feeds an input buffer into an ongoing
0091  *                  Poly1305 computation.
0092  *
0093  *                  It is called between \c mbedtls_cipher_poly1305_starts() and
0094  *                  \c mbedtls_cipher_poly1305_finish().
0095  *                  It can be called repeatedly to process a stream of data.
0096  *
0097  * \param ctx       The Poly1305 context to use for the Poly1305 operation.
0098  *                  This must be initialized and bound to a key.
0099  * \param ilen      The length of the input data in Bytes.
0100  *                  Any value is accepted.
0101  * \param input     The buffer holding the input data.
0102  *                  This pointer can be \c NULL if `ilen == 0`.
0103  *
0104  * \return          \c 0 on success.
0105  * \return          A negative error code on failure.
0106  */
0107 int mbedtls_poly1305_update(mbedtls_poly1305_context *ctx,
0108                             const unsigned char *input,
0109                             size_t ilen);
0110 
0111 /**
0112  * \brief           This function generates the Poly1305 Message
0113  *                  Authentication Code (MAC).
0114  *
0115  * \param ctx       The Poly1305 context to use for the Poly1305 operation.
0116  *                  This must be initialized and bound to a key.
0117  * \param mac       The buffer to where the MAC is written. This must
0118  *                  be a writable buffer of length \c 16 Bytes.
0119  *
0120  * \return          \c 0 on success.
0121  * \return          A negative error code on failure.
0122  */
0123 int mbedtls_poly1305_finish(mbedtls_poly1305_context *ctx,
0124                             unsigned char mac[16]);
0125 
0126 /**
0127  * \brief           This function calculates the Poly1305 MAC of the input
0128  *                  buffer with the provided key.
0129  *
0130  * \warning         The key must be unique and unpredictable for each
0131  *                  invocation of Poly1305.
0132  *
0133  * \param key       The buffer containing the \c 32 Byte (\c 256 Bit) key.
0134  * \param ilen      The length of the input data in Bytes.
0135  *                  Any value is accepted.
0136  * \param input     The buffer holding the input data.
0137  *                  This pointer can be \c NULL if `ilen == 0`.
0138  * \param mac       The buffer to where the MAC is written. This must be
0139  *                  a writable buffer of length \c 16 Bytes.
0140  *
0141  * \return          \c 0 on success.
0142  * \return          A negative error code on failure.
0143  */
0144 int mbedtls_poly1305_mac(const unsigned char key[32],
0145                          const unsigned char *input,
0146                          size_t ilen,
0147                          unsigned char mac[16]);
0148 
0149 #if defined(MBEDTLS_SELF_TEST)
0150 /**
0151  * \brief           The Poly1305 checkup routine.
0152  *
0153  * \return          \c 0 on success.
0154  * \return          \c 1 on failure.
0155  */
0156 int mbedtls_poly1305_self_test(int verbose);
0157 #endif /* MBEDTLS_SELF_TEST */
0158 
0159 #endif /* MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS */
0160 
0161 #ifdef __cplusplus
0162 }
0163 #endif
0164 
0165 #endif /* MBEDTLS_POLY1305_H */