poly1305.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /**
  2. * \file poly1305.h
  3. *
  4. * \brief This file contains Poly1305 definitions and functions.
  5. *
  6. * Poly1305 is a one-time message authenticator that can be used to
  7. * authenticate messages. Poly1305-AES was created by Daniel
  8. * Bernstein https://cr.yp.to/mac/poly1305-20050329.pdf The generic
  9. * Poly1305 algorithm (not tied to AES) was also standardized in RFC
  10. * 7539.
  11. *
  12. * \author Daniel King <damaki.gh@gmail.com>
  13. */
  14. /* Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved.
  15. * SPDX-License-Identifier: Apache-2.0
  16. *
  17. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  18. * not use this file except in compliance with the License.
  19. * You may obtain a copy of the License at
  20. *
  21. * http://www.apache.org/licenses/LICENSE-2.0
  22. *
  23. * Unless required by applicable law or agreed to in writing, software
  24. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  25. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  26. * See the License for the specific language governing permissions and
  27. * limitations under the License.
  28. *
  29. * This file is part of Mbed TLS (https://tls.mbed.org)
  30. */
  31. #ifndef MBEDTLS_POLY1305_H
  32. #define MBEDTLS_POLY1305_H
  33. #if !defined(MBEDTLS_CONFIG_FILE)
  34. #include "config.h"
  35. #else
  36. #include MBEDTLS_CONFIG_FILE
  37. #endif
  38. #include <stdint.h>
  39. #include <stddef.h>
  40. #define MBEDTLS_ERR_POLY1305_BAD_INPUT_DATA -0x0057 /**< Invalid input parameter(s). */
  41. /* MBEDTLS_ERR_POLY1305_FEATURE_UNAVAILABLE is deprecated and should not be
  42. * used. */
  43. #define MBEDTLS_ERR_POLY1305_FEATURE_UNAVAILABLE -0x0059 /**< Feature not available. For example, s part of the API is not implemented. */
  44. /* MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED is deprecated and should not be used.
  45. */
  46. #define MBEDTLS_ERR_POLY1305_HW_ACCEL_FAILED -0x005B /**< Poly1305 hardware accelerator failed. */
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. #if !defined(MBEDTLS_POLY1305_ALT)
  51. typedef struct mbedtls_poly1305_context
  52. {
  53. uint32_t r[4]; /** The value for 'r' (low 128 bits of the key). */
  54. uint32_t s[4]; /** The value for 's' (high 128 bits of the key). */
  55. uint32_t acc[5]; /** The accumulator number. */
  56. uint8_t queue[16]; /** The current partial block of data. */
  57. size_t queue_len; /** The number of bytes stored in 'queue'. */
  58. }
  59. mbedtls_poly1305_context;
  60. #else /* MBEDTLS_POLY1305_ALT */
  61. #include "poly1305_alt.h"
  62. #endif /* MBEDTLS_POLY1305_ALT */
  63. /**
  64. * \brief This function initializes the specified Poly1305 context.
  65. *
  66. * It must be the first API called before using
  67. * the context.
  68. *
  69. * It is usually followed by a call to
  70. * \c mbedtls_poly1305_starts(), then one or more calls to
  71. * \c mbedtls_poly1305_update(), then one call to
  72. * \c mbedtls_poly1305_finish(), then finally
  73. * \c mbedtls_poly1305_free().
  74. *
  75. * \param ctx The Poly1305 context to initialize. This must
  76. * not be \c NULL.
  77. */
  78. void mbedtls_poly1305_init( mbedtls_poly1305_context *ctx );
  79. /**
  80. * \brief This function releases and clears the specified
  81. * Poly1305 context.
  82. *
  83. * \param ctx The Poly1305 context to clear. This may be \c NULL, in which
  84. * case this function is a no-op. If it is not \c NULL, it must
  85. * point to an initialized Poly1305 context.
  86. */
  87. void mbedtls_poly1305_free( mbedtls_poly1305_context *ctx );
  88. /**
  89. * \brief This function sets the one-time authentication key.
  90. *
  91. * \warning The key must be unique and unpredictable for each
  92. * invocation of Poly1305.
  93. *
  94. * \param ctx The Poly1305 context to which the key should be bound.
  95. * This must be initialized.
  96. * \param key The buffer containing the \c 32 Byte (\c 256 Bit) key.
  97. *
  98. * \return \c 0 on success.
  99. * \return A negative error code on failure.
  100. */
  101. int mbedtls_poly1305_starts( mbedtls_poly1305_context *ctx,
  102. const unsigned char key[32] );
  103. /**
  104. * \brief This functions feeds an input buffer into an ongoing
  105. * Poly1305 computation.
  106. *
  107. * It is called between \c mbedtls_cipher_poly1305_starts() and
  108. * \c mbedtls_cipher_poly1305_finish().
  109. * It can be called repeatedly to process a stream of data.
  110. *
  111. * \param ctx The Poly1305 context to use for the Poly1305 operation.
  112. * This must be initialized and bound to a key.
  113. * \param ilen The length of the input data in Bytes.
  114. * Any value is accepted.
  115. * \param input The buffer holding the input data.
  116. * This pointer can be \c NULL if `ilen == 0`.
  117. *
  118. * \return \c 0 on success.
  119. * \return A negative error code on failure.
  120. */
  121. int mbedtls_poly1305_update( mbedtls_poly1305_context *ctx,
  122. const unsigned char *input,
  123. size_t ilen );
  124. /**
  125. * \brief This function generates the Poly1305 Message
  126. * Authentication Code (MAC).
  127. *
  128. * \param ctx The Poly1305 context to use for the Poly1305 operation.
  129. * This must be initialized and bound to a key.
  130. * \param mac The buffer to where the MAC is written. This must
  131. * be a writable buffer of length \c 16 Bytes.
  132. *
  133. * \return \c 0 on success.
  134. * \return A negative error code on failure.
  135. */
  136. int mbedtls_poly1305_finish( mbedtls_poly1305_context *ctx,
  137. unsigned char mac[16] );
  138. /**
  139. * \brief This function calculates the Poly1305 MAC of the input
  140. * buffer with the provided key.
  141. *
  142. * \warning The key must be unique and unpredictable for each
  143. * invocation of Poly1305.
  144. *
  145. * \param key The buffer containing the \c 32 Byte (\c 256 Bit) key.
  146. * \param ilen The length of the input data in Bytes.
  147. * Any value is accepted.
  148. * \param input The buffer holding the input data.
  149. * This pointer can be \c NULL if `ilen == 0`.
  150. * \param mac The buffer to where the MAC is written. This must be
  151. * a writable buffer of length \c 16 Bytes.
  152. *
  153. * \return \c 0 on success.
  154. * \return A negative error code on failure.
  155. */
  156. int mbedtls_poly1305_mac( const unsigned char key[32],
  157. const unsigned char *input,
  158. size_t ilen,
  159. unsigned char mac[16] );
  160. #if defined(MBEDTLS_SELF_TEST)
  161. /**
  162. * \brief The Poly1305 checkup routine.
  163. *
  164. * \return \c 0 on success.
  165. * \return \c 1 on failure.
  166. */
  167. int mbedtls_poly1305_self_test( int verbose );
  168. #endif /* MBEDTLS_SELF_TEST */
  169. #ifdef __cplusplus
  170. }
  171. #endif
  172. #endif /* MBEDTLS_POLY1305_H */