gcm.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. #if !defined(MBEDTLS_CONFIG_FILE)
  35. #include "config.h"
  36. #else
  37. #include MBEDTLS_CONFIG_FILE
  38. #endif
  39. #include "cipher.h"
  40. #include <stdint.h>
  41. #define MBEDTLS_GCM_ENCRYPT 1
  42. #define MBEDTLS_GCM_DECRYPT 0
  43. #define MBEDTLS_ERR_GCM_AUTH_FAILED -0x0012 /**< Authenticated decryption failed. */
  44. /* MBEDTLS_ERR_GCM_HW_ACCEL_FAILED is deprecated and should not be used. */
  45. #define MBEDTLS_ERR_GCM_HW_ACCEL_FAILED -0x0013 /**< GCM hardware accelerator failed. */
  46. #define MBEDTLS_ERR_GCM_BAD_INPUT -0x0014 /**< Bad input parameters to function. */
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. #if !defined(MBEDTLS_GCM_ALT)
  51. /**
  52. * \brief The GCM context structure.
  53. */
  54. typedef struct mbedtls_gcm_context
  55. {
  56. mbedtls_cipher_context_t cipher_ctx; /*!< The cipher context used. */
  57. uint64_t HL[16]; /*!< Precalculated HTable low. */
  58. uint64_t HH[16]; /*!< Precalculated HTable high. */
  59. uint64_t len; /*!< The total length of the encrypted data. */
  60. uint64_t add_len; /*!< The total length of the additional data. */
  61. unsigned char base_ectr[16]; /*!< The first ECTR for tag. */
  62. unsigned char y[16]; /*!< The Y working value. */
  63. unsigned char buf[16]; /*!< The buf working value. */
  64. int mode; /*!< The operation to perform:
  65. #MBEDTLS_GCM_ENCRYPT or
  66. #MBEDTLS_GCM_DECRYPT. */
  67. }
  68. mbedtls_gcm_context;
  69. #else /* !MBEDTLS_GCM_ALT */
  70. #include "gcm_alt.h"
  71. #endif /* !MBEDTLS_GCM_ALT */
  72. /**
  73. * \brief This function initializes the specified GCM context,
  74. * to make references valid, and prepares the context
  75. * for mbedtls_gcm_setkey() or mbedtls_gcm_free().
  76. *
  77. * The function does not bind the GCM context to a particular
  78. * cipher, nor set the key. For this purpose, use
  79. * mbedtls_gcm_setkey().
  80. *
  81. * \param ctx The GCM context to initialize. This must not be \c NULL.
  82. */
  83. void mbedtls_gcm_init( mbedtls_gcm_context *ctx );
  84. /**
  85. * \brief This function associates a GCM context with a
  86. * cipher algorithm and a key.
  87. *
  88. * \param ctx The GCM context. This must be initialized.
  89. * \param cipher The 128-bit block cipher to use.
  90. * \param key The encryption key. This must be a readable buffer of at
  91. * least \p keybits bits.
  92. * \param keybits The key size in bits. Valid options are:
  93. * <ul><li>128 bits</li>
  94. * <li>192 bits</li>
  95. * <li>256 bits</li></ul>
  96. *
  97. * \return \c 0 on success.
  98. * \return A cipher-specific error code on failure.
  99. */
  100. int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx,
  101. mbedtls_cipher_id_t cipher,
  102. const unsigned char *key,
  103. unsigned int keybits );
  104. /**
  105. * \brief This function performs GCM encryption or decryption of a buffer.
  106. *
  107. * \note For encryption, the output buffer can be the same as the
  108. * input buffer. For decryption, the output buffer cannot be
  109. * the same as input buffer. If the buffers overlap, the output
  110. * buffer must trail at least 8 Bytes behind the input buffer.
  111. *
  112. * \warning When this function performs a decryption, it outputs the
  113. * authentication tag and does not verify that the data is
  114. * authentic. You should use this function to perform encryption
  115. * only. For decryption, use mbedtls_gcm_auth_decrypt() instead.
  116. *
  117. * \param ctx The GCM context to use for encryption or decryption. This
  118. * must be initialized.
  119. * \param mode The operation to perform:
  120. * - #MBEDTLS_GCM_ENCRYPT to perform authenticated encryption.
  121. * The ciphertext is written to \p output and the
  122. * authentication tag is written to \p tag.
  123. * - #MBEDTLS_GCM_DECRYPT to perform decryption.
  124. * The plaintext is written to \p output and the
  125. * authentication tag is written to \p tag.
  126. * Note that this mode is not recommended, because it does
  127. * not verify the authenticity of the data. For this reason,
  128. * you should use mbedtls_gcm_auth_decrypt() instead of
  129. * calling this function in decryption mode.
  130. * \param length The length of the input data, which is equal to the length
  131. * of the output data.
  132. * \param iv The initialization vector. This must be a readable buffer of
  133. * at least \p iv_len Bytes.
  134. * \param iv_len The length of the IV.
  135. * \param add The buffer holding the additional data. This must be of at
  136. * least that size in Bytes.
  137. * \param add_len The length of the additional data.
  138. * \param input The buffer holding the input data. If \p length is greater
  139. * than zero, this must be a readable buffer of at least that
  140. * size in Bytes.
  141. * \param output The buffer for holding the output data. If \p length is greater
  142. * than zero, this must be a writable buffer of at least that
  143. * size in Bytes.
  144. * \param tag_len The length of the tag to generate.
  145. * \param tag The buffer for holding the tag. This must be a readable
  146. * buffer of at least \p tag_len Bytes.
  147. *
  148. * \return \c 0 if the encryption or decryption was performed
  149. * successfully. Note that in #MBEDTLS_GCM_DECRYPT mode,
  150. * this does not indicate that the data is authentic.
  151. * \return #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths or pointers are
  152. * not valid or a cipher-specific error code if the encryption
  153. * or decryption failed.
  154. */
  155. int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
  156. int mode,
  157. size_t length,
  158. const unsigned char *iv,
  159. size_t iv_len,
  160. const unsigned char *add,
  161. size_t add_len,
  162. const unsigned char *input,
  163. unsigned char *output,
  164. size_t tag_len,
  165. unsigned char *tag );
  166. /**
  167. * \brief This function performs a GCM authenticated decryption of a
  168. * buffer.
  169. *
  170. * \note For decryption, the output buffer cannot be the same as
  171. * input buffer. If the buffers overlap, the output buffer
  172. * must trail at least 8 Bytes behind the input buffer.
  173. *
  174. * \param ctx The GCM context. This must be initialized.
  175. * \param length The length of the ciphertext to decrypt, which is also
  176. * the length of the decrypted plaintext.
  177. * \param iv The initialization vector. This must be a readable buffer
  178. * of at least \p iv_len Bytes.
  179. * \param iv_len The length of the IV.
  180. * \param add The buffer holding the additional data. This must be of at
  181. * least that size in Bytes.
  182. * \param add_len The length of the additional data.
  183. * \param tag The buffer holding the tag to verify. This must be a
  184. * readable buffer of at least \p tag_len Bytes.
  185. * \param tag_len The length of the tag to verify.
  186. * \param input The buffer holding the ciphertext. If \p length is greater
  187. * than zero, this must be a readable buffer of at least that
  188. * size.
  189. * \param output The buffer for holding the decrypted plaintext. If \p length
  190. * is greater than zero, this must be a writable buffer of at
  191. * least that size.
  192. *
  193. * \return \c 0 if successful and authenticated.
  194. * \return #MBEDTLS_ERR_GCM_AUTH_FAILED if the tag does not match.
  195. * \return #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths or pointers are
  196. * not valid or a cipher-specific error code if the decryption
  197. * failed.
  198. */
  199. int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx,
  200. size_t length,
  201. const unsigned char *iv,
  202. size_t iv_len,
  203. const unsigned char *add,
  204. size_t add_len,
  205. const unsigned char *tag,
  206. size_t tag_len,
  207. const unsigned char *input,
  208. unsigned char *output );
  209. /**
  210. * \brief This function starts a GCM encryption or decryption
  211. * operation.
  212. *
  213. * \param ctx The GCM context. This must be initialized.
  214. * \param mode The operation to perform: #MBEDTLS_GCM_ENCRYPT or
  215. * #MBEDTLS_GCM_DECRYPT.
  216. * \param iv The initialization vector. This must be a readable buffer of
  217. * at least \p iv_len Bytes.
  218. * \param iv_len The length of the IV.
  219. * \param add The buffer holding the additional data, or \c NULL
  220. * if \p add_len is \c 0.
  221. * \param add_len The length of the additional data. If \c 0,
  222. * \p add may be \c NULL.
  223. *
  224. * \return \c 0 on success.
  225. */
  226. int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
  227. int mode,
  228. const unsigned char *iv,
  229. size_t iv_len,
  230. const unsigned char *add,
  231. size_t add_len );
  232. /**
  233. * \brief This function feeds an input buffer into an ongoing GCM
  234. * encryption or decryption operation.
  235. *
  236. * ` The function expects input to be a multiple of 16
  237. * Bytes. Only the last call before calling
  238. * mbedtls_gcm_finish() can be less than 16 Bytes.
  239. *
  240. * \note For decryption, the output buffer cannot be the same as
  241. * input buffer. If the buffers overlap, the output buffer
  242. * must trail at least 8 Bytes behind the input buffer.
  243. *
  244. * \param ctx The GCM context. This must be initialized.
  245. * \param length The length of the input data. This must be a multiple of
  246. * 16 except in the last call before mbedtls_gcm_finish().
  247. * \param input The buffer holding the input data. If \p length is greater
  248. * than zero, this must be a readable buffer of at least that
  249. * size in Bytes.
  250. * \param output The buffer for holding the output data. If \p length is
  251. * greater than zero, this must be a writable buffer of at
  252. * least that size in Bytes.
  253. *
  254. * \return \c 0 on success.
  255. * \return #MBEDTLS_ERR_GCM_BAD_INPUT on failure.
  256. */
  257. int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
  258. size_t length,
  259. const unsigned char *input,
  260. unsigned char *output );
  261. /**
  262. * \brief This function finishes the GCM operation and generates
  263. * the authentication tag.
  264. *
  265. * It wraps up the GCM stream, and generates the
  266. * tag. The tag can have a maximum length of 16 Bytes.
  267. *
  268. * \param ctx The GCM context. This must be initialized.
  269. * \param tag The buffer for holding the tag. This must be a readable
  270. * buffer of at least \p tag_len Bytes.
  271. * \param tag_len The length of the tag to generate. This must be at least
  272. * four.
  273. *
  274. * \return \c 0 on success.
  275. * \return #MBEDTLS_ERR_GCM_BAD_INPUT on failure.
  276. */
  277. int mbedtls_gcm_finish( mbedtls_gcm_context *ctx,
  278. unsigned char *tag,
  279. size_t tag_len );
  280. /**
  281. * \brief This function clears a GCM context and the underlying
  282. * cipher sub-context.
  283. *
  284. * \param ctx The GCM context to clear. If this is \c NULL, the call has
  285. * no effect. Otherwise, this must be initialized.
  286. */
  287. void mbedtls_gcm_free( mbedtls_gcm_context *ctx );
  288. #if defined(MBEDTLS_SELF_TEST)
  289. /**
  290. * \brief The GCM checkup routine.
  291. *
  292. * \return \c 0 on success.
  293. * \return \c 1 on failure.
  294. */
  295. int mbedtls_gcm_self_test( int verbose );
  296. #endif /* MBEDTLS_SELF_TEST */
  297. #ifdef __cplusplus
  298. }
  299. #endif
  300. #endif /* gcm.h */