gcm.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /**
  2. * \file gcm.h
  3. *
  4. * \brief Galois/Counter Mode (GCM) for 128-bit block ciphers, as defined
  5. * in <em>D. McGrew, J. Viega, The Galois/Counter Mode of Operation
  6. * (GCM), Natl. Inst. Stand. Technol.</em>
  7. *
  8. * For more information on GCM, see <em>NIST SP 800-38D: Recommendation for
  9. * Block Cipher Modes of Operation: Galois/Counter Mode (GCM) and GMAC</em>.
  10. *
  11. */
  12. /*
  13. * Copyright (C) 2006-2018, Arm Limited (or its affiliates), All Rights Reserved
  14. * SPDX-License-Identifier: Apache-2.0
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  17. * not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * http://www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. *
  28. * This file is part of Mbed TLS (https://tls.mbed.org)
  29. */
  30. #ifndef MBEDTLS_GCM_H
  31. #define MBEDTLS_GCM_H
  32. #include "cipher.h"
  33. #include <stdint.h>
  34. #define MBEDTLS_GCM_ENCRYPT 1
  35. #define MBEDTLS_GCM_DECRYPT 0
  36. #define MBEDTLS_ERR_GCM_AUTH_FAILED -0x0012 /**< Authenticated decryption failed. */
  37. #define MBEDTLS_ERR_GCM_HW_ACCEL_FAILED -0x0013 /**< GCM hardware accelerator failed. */
  38. #define MBEDTLS_ERR_GCM_BAD_INPUT -0x0014 /**< Bad input parameters to function. */
  39. #if !defined(MBEDTLS_GCM_ALT)
  40. #ifdef __cplusplus
  41. extern "C" {
  42. #endif
  43. /**
  44. * \brief The GCM context structure.
  45. */
  46. typedef struct {
  47. mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
  48. uint64_t HL[16]; /*!< Precalculated HTable low. */
  49. uint64_t HH[16]; /*!< Precalculated HTable high. */
  50. uint64_t len; /*!< The total length of the encrypted data. */
  51. uint64_t add_len; /*!< The total length of the additional data. */
  52. unsigned char base_ectr[16]; /*!< The first ECTR for tag. */
  53. unsigned char y[16]; /*!< The Y working value. */
  54. unsigned char buf[16]; /*!< The buf working value. */
  55. int mode; /*!< The operation to perform:
  56. #MBEDTLS_GCM_ENCRYPT or
  57. #MBEDTLS_GCM_DECRYPT. */
  58. }
  59. mbedtls_gcm_context;
  60. /**
  61. * \brief This function initializes the specified GCM context,
  62. * to make references valid, and prepares the context
  63. * for mbedtls_gcm_setkey() or mbedtls_gcm_free().
  64. *
  65. * The function does not bind the GCM context to a particular
  66. * cipher, nor set the key. For this purpose, use
  67. * mbedtls_gcm_setkey().
  68. *
  69. * \param ctx The GCM context to initialize.
  70. */
  71. void mbedtls_gcm_init( mbedtls_gcm_context *ctx );
  72. /**
  73. * \brief This function associates a GCM context with a
  74. * cipher algorithm and a key.
  75. *
  76. * \param ctx The GCM context to initialize.
  77. * \param cipher The 128-bit block cipher to use.
  78. * \param key The encryption key.
  79. * \param keybits The key size in bits. Valid options are:
  80. * <ul><li>128 bits</li>
  81. * <li>192 bits</li>
  82. * <li>256 bits</li></ul>
  83. *
  84. * \return \c 0 on success, or a cipher specific error code.
  85. */
  86. int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx,
  87. mbedtls_cipher_id_t cipher,
  88. const unsigned char *key,
  89. unsigned int keybits );
  90. /**
  91. * \brief This function performs GCM encryption or decryption of a buffer.
  92. *
  93. * \note For encryption, the output buffer can be the same as the input buffer.
  94. * For decryption, the output buffer cannot be the same as input buffer.
  95. * If the buffers overlap, the output buffer must trail at least 8 Bytes
  96. * behind the input buffer.
  97. *
  98. * \param ctx The GCM context to use for encryption or decryption.
  99. * \param mode The operation to perform: #MBEDTLS_GCM_ENCRYPT or
  100. * #MBEDTLS_GCM_DECRYPT.
  101. * \param length The length of the input data. This must be a multiple of 16 except in the last call before mbedtls_gcm_finish().
  102. * \param iv The initialization vector.
  103. * \param iv_len The length of the IV.
  104. * \param add The buffer holding the additional data.
  105. * \param add_len The length of the additional data.
  106. * \param input The buffer holding the input data.
  107. * \param output The buffer for holding the output data.
  108. * \param tag_len The length of the tag to generate.
  109. * \param tag The buffer for holding the tag.
  110. *
  111. * \return \c 0 on success.
  112. */
  113. int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
  114. int mode,
  115. size_t length,
  116. const unsigned char *iv,
  117. size_t iv_len,
  118. const unsigned char *add,
  119. size_t add_len,
  120. const unsigned char *input,
  121. unsigned char *output,
  122. size_t tag_len,
  123. unsigned char *tag );
  124. /**
  125. * \brief This function performs a GCM authenticated decryption of a
  126. * buffer.
  127. *
  128. * \note For decryption, the output buffer cannot be the same as input buffer.
  129. * If the buffers overlap, the output buffer must trail at least 8 Bytes
  130. * behind the input buffer.
  131. *
  132. * \param ctx The GCM context.
  133. * \param length The length of the input data. This must be a multiple of 16 except in the last call before mbedtls_gcm_finish().
  134. * \param iv The initialization vector.
  135. * \param iv_len The length of the IV.
  136. * \param add The buffer holding the additional data.
  137. * \param add_len The length of the additional data.
  138. * \param tag The buffer holding the tag.
  139. * \param tag_len The length of the tag.
  140. * \param input The buffer holding the input data.
  141. * \param output The buffer for holding the output data.
  142. *
  143. * \return 0 if successful and authenticated, or
  144. * #MBEDTLS_ERR_GCM_AUTH_FAILED if tag does not match.
  145. */
  146. int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx,
  147. size_t length,
  148. const unsigned char *iv,
  149. size_t iv_len,
  150. const unsigned char *add,
  151. size_t add_len,
  152. const unsigned char *tag,
  153. size_t tag_len,
  154. const unsigned char *input,
  155. unsigned char *output );
  156. /**
  157. * \brief This function starts a GCM encryption or decryption
  158. * operation.
  159. *
  160. * \param ctx The GCM context.
  161. * \param mode The operation to perform: #MBEDTLS_GCM_ENCRYPT or
  162. * #MBEDTLS_GCM_DECRYPT.
  163. * \param iv The initialization vector.
  164. * \param iv_len The length of the IV.
  165. * \param add The buffer holding the additional data, or NULL if \p add_len is 0.
  166. * \param add_len The length of the additional data. If 0, \p add is NULL.
  167. *
  168. * \return \c 0 on success.
  169. */
  170. int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
  171. int mode,
  172. const unsigned char *iv,
  173. size_t iv_len,
  174. const unsigned char *add,
  175. size_t add_len );
  176. /**
  177. * \brief This function feeds an input buffer into an ongoing GCM
  178. * encryption or decryption operation.
  179. *
  180. * ` The function expects input to be a multiple of 16
  181. * Bytes. Only the last call before calling
  182. * mbedtls_gcm_finish() can be less than 16 Bytes.
  183. *
  184. * \note For decryption, the output buffer cannot be the same as input buffer.
  185. * If the buffers overlap, the output buffer must trail at least 8 Bytes
  186. * behind the input buffer.
  187. *
  188. * \param ctx The GCM context.
  189. * \param length The length of the input data. This must be a multiple of 16 except in the last call before mbedtls_gcm_finish().
  190. * \param input The buffer holding the input data.
  191. * \param output The buffer for holding the output data.
  192. *
  193. * \return \c 0 on success, or #MBEDTLS_ERR_GCM_BAD_INPUT on failure.
  194. */
  195. int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
  196. size_t length,
  197. const unsigned char *input,
  198. unsigned char *output );
  199. /**
  200. * \brief This function finishes the GCM operation and generates
  201. * the authentication tag.
  202. *
  203. * It wraps up the GCM stream, and generates the
  204. * tag. The tag can have a maximum length of 16 Bytes.
  205. *
  206. * \param ctx The GCM context.
  207. * \param tag The buffer for holding the tag.
  208. * \param tag_len The length of the tag to generate. Must be at least four.
  209. *
  210. * \return \c 0 on success, or #MBEDTLS_ERR_GCM_BAD_INPUT on failure.
  211. */
  212. int mbedtls_gcm_finish( mbedtls_gcm_context *ctx,
  213. unsigned char *tag,
  214. size_t tag_len );
  215. /**
  216. * \brief This function clears a GCM context and the underlying
  217. * cipher sub-context.
  218. *
  219. * \param ctx The GCM context to clear.
  220. */
  221. void mbedtls_gcm_free( mbedtls_gcm_context *ctx );
  222. #ifdef __cplusplus
  223. }
  224. #endif
  225. #else /* !MBEDTLS_GCM_ALT */
  226. #include "gcm_alt.h"
  227. #endif /* !MBEDTLS_GCM_ALT */
  228. #ifdef __cplusplus
  229. extern "C" {
  230. #endif
  231. /**
  232. * \brief The GCM checkup routine.
  233. *
  234. * \return \c 0 on success, or \c 1 on failure.
  235. */
  236. int mbedtls_gcm_self_test( int verbose );
  237. #ifdef __cplusplus
  238. }
  239. #endif
  240. #endif /* gcm.h */