gcm.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. * \warning When this function performs a decryption, it outputs the
  99. * authentication tag and does not verify that the data is
  100. * authentic. You should use this function to perform encryption
  101. * only. For decryption, use mbedtls_gcm_auth_decrypt() instead.
  102. *
  103. * \param ctx The GCM context to use for encryption or decryption.
  104. * \param mode The operation to perform:
  105. * - #MBEDTLS_GCM_ENCRYPT to perform authenticated encryption.
  106. * The ciphertext is written to \p output and the
  107. * authentication tag is written to \p tag.
  108. * - #MBEDTLS_GCM_DECRYPT to perform decryption.
  109. * The plaintext is written to \p output and the
  110. * authentication tag is written to \p tag.
  111. * Note that this mode is not recommended, because it does
  112. * not verify the authenticity of the data. For this reason,
  113. * you should use mbedtls_gcm_auth_decrypt() instead of
  114. * calling this function in decryption mode.
  115. * \param length The length of the input data, which is equal to the length
  116. * of the output data.
  117. * \param iv The initialization vector.
  118. * \param iv_len The length of the IV.
  119. * \param add The buffer holding the additional data.
  120. * \param add_len The length of the additional data.
  121. * \param input The buffer holding the input data. Its size is \b length.
  122. * \param output The buffer for holding the output data. It must have room
  123. * for \b length bytes.
  124. * \param tag_len The length of the tag to generate.
  125. * \param tag The buffer for holding the tag.
  126. *
  127. * \return \c 0 if the encryption or decryption was performed
  128. * successfully. Note that in #MBEDTLS_GCM_DECRYPT mode,
  129. * this does not indicate that the data is authentic.
  130. * \return #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths are not valid.
  131. * \return #MBEDTLS_ERR_GCM_HW_ACCEL_FAILED or a cipher-specific
  132. * error code if the encryption or decryption failed.
  133. */
  134. int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
  135. int mode,
  136. size_t length,
  137. const unsigned char *iv,
  138. size_t iv_len,
  139. const unsigned char *add,
  140. size_t add_len,
  141. const unsigned char *input,
  142. unsigned char *output,
  143. size_t tag_len,
  144. unsigned char *tag );
  145. /**
  146. * \brief This function performs a GCM authenticated decryption of a
  147. * buffer.
  148. *
  149. * \note For decryption, the output buffer cannot be the same as input buffer.
  150. * If the buffers overlap, the output buffer must trail at least 8 Bytes
  151. * behind the input buffer.
  152. *
  153. * \param ctx The GCM context.
  154. * \param length The length of the ciphertext to decrypt, which is also
  155. * the length of the decrypted plaintext.
  156. * \param iv The initialization vector.
  157. * \param iv_len The length of the IV.
  158. * \param add The buffer holding the additional data.
  159. * \param add_len The length of the additional data.
  160. * \param tag The buffer holding the tag to verify.
  161. * \param tag_len The length of the tag to verify.
  162. * \param input The buffer holding the ciphertext. Its size is \b length.
  163. * \param output The buffer for holding the decrypted plaintext. It must
  164. * have room for \b length bytes.
  165. *
  166. * \return \c 0 if successful and authenticated.
  167. * \return #MBEDTLS_ERR_GCM_AUTH_FAILED if the tag does not match.
  168. * \return #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths are not valid.
  169. * \return #MBEDTLS_ERR_GCM_HW_ACCEL_FAILED or a cipher-specific
  170. * error code if the decryption failed.
  171. */
  172. int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx,
  173. size_t length,
  174. const unsigned char *iv,
  175. size_t iv_len,
  176. const unsigned char *add,
  177. size_t add_len,
  178. const unsigned char *tag,
  179. size_t tag_len,
  180. const unsigned char *input,
  181. unsigned char *output );
  182. /**
  183. * \brief This function starts a GCM encryption or decryption
  184. * operation.
  185. *
  186. * \param ctx The GCM context.
  187. * \param mode The operation to perform: #MBEDTLS_GCM_ENCRYPT or
  188. * #MBEDTLS_GCM_DECRYPT.
  189. * \param iv The initialization vector.
  190. * \param iv_len The length of the IV.
  191. * \param add The buffer holding the additional data, or NULL if \p add_len is 0.
  192. * \param add_len The length of the additional data. If 0, \p add is NULL.
  193. *
  194. * \return \c 0 on success.
  195. */
  196. int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
  197. int mode,
  198. const unsigned char *iv,
  199. size_t iv_len,
  200. const unsigned char *add,
  201. size_t add_len );
  202. /**
  203. * \brief This function feeds an input buffer into an ongoing GCM
  204. * encryption or decryption operation.
  205. *
  206. * ` The function expects input to be a multiple of 16
  207. * Bytes. Only the last call before calling
  208. * mbedtls_gcm_finish() can be less than 16 Bytes.
  209. *
  210. * \note For decryption, the output buffer cannot be the same as input buffer.
  211. * If the buffers overlap, the output buffer must trail at least 8 Bytes
  212. * behind the input buffer.
  213. *
  214. * \param ctx The GCM context.
  215. * \param length The length of the input data. This must be a multiple of 16 except in the last call before mbedtls_gcm_finish().
  216. * \param input The buffer holding the input data.
  217. * \param output The buffer for holding the output data.
  218. *
  219. * \return \c 0 on success, or #MBEDTLS_ERR_GCM_BAD_INPUT on failure.
  220. */
  221. int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
  222. size_t length,
  223. const unsigned char *input,
  224. unsigned char *output );
  225. /**
  226. * \brief This function finishes the GCM operation and generates
  227. * the authentication tag.
  228. *
  229. * It wraps up the GCM stream, and generates the
  230. * tag. The tag can have a maximum length of 16 Bytes.
  231. *
  232. * \param ctx The GCM context.
  233. * \param tag The buffer for holding the tag.
  234. * \param tag_len The length of the tag to generate. Must be at least four.
  235. *
  236. * \return \c 0 on success, or #MBEDTLS_ERR_GCM_BAD_INPUT on failure.
  237. */
  238. int mbedtls_gcm_finish( mbedtls_gcm_context *ctx,
  239. unsigned char *tag,
  240. size_t tag_len );
  241. /**
  242. * \brief This function clears a GCM context and the underlying
  243. * cipher sub-context.
  244. *
  245. * \param ctx The GCM context to clear.
  246. */
  247. void mbedtls_gcm_free( mbedtls_gcm_context *ctx );
  248. #ifdef __cplusplus
  249. }
  250. #endif
  251. #else /* !MBEDTLS_GCM_ALT */
  252. #include "gcm_alt.h"
  253. #endif /* !MBEDTLS_GCM_ALT */
  254. #ifdef __cplusplus
  255. extern "C" {
  256. #endif
  257. /**
  258. * \brief The GCM checkup routine.
  259. *
  260. * \return \c 0 on success, or \c 1 on failure.
  261. */
  262. int mbedtls_gcm_self_test( int verbose );
  263. #ifdef __cplusplus
  264. }
  265. #endif
  266. #endif /* gcm.h */