gcm.h 12 KB

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