gcm.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 The Mbed TLS Contributors
  16. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  17. */
  18. #ifndef MBEDTLS_GCM_H
  19. #define MBEDTLS_GCM_H
  20. #include "mbedtls/private_access.h"
  21. #include "mbedtls/build_info.h"
  22. #include "mbedtls/cipher.h"
  23. #if defined(MBEDTLS_BLOCK_CIPHER_C)
  24. #include "mbedtls/block_cipher.h"
  25. #endif
  26. #include <stdint.h>
  27. #define MBEDTLS_GCM_ENCRYPT 1
  28. #define MBEDTLS_GCM_DECRYPT 0
  29. /** Authenticated decryption failed. */
  30. #define MBEDTLS_ERR_GCM_AUTH_FAILED -0x0012
  31. /** Bad input parameters to function. */
  32. #define MBEDTLS_ERR_GCM_BAD_INPUT -0x0014
  33. /** An output buffer is too small. */
  34. #define MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL -0x0016
  35. #ifdef __cplusplus
  36. extern "C" {
  37. #endif
  38. #if !defined(MBEDTLS_GCM_ALT)
  39. #if defined(MBEDTLS_GCM_LARGE_TABLE)
  40. #define MBEDTLS_GCM_HTABLE_SIZE 256
  41. #else
  42. #define MBEDTLS_GCM_HTABLE_SIZE 16
  43. #endif
  44. /**
  45. * \brief The GCM context structure.
  46. */
  47. typedef struct mbedtls_gcm_context {
  48. #if defined(MBEDTLS_BLOCK_CIPHER_C)
  49. mbedtls_block_cipher_context_t MBEDTLS_PRIVATE(block_cipher_ctx); /*!< The cipher context used. */
  50. #else
  51. mbedtls_cipher_context_t MBEDTLS_PRIVATE(cipher_ctx); /*!< The cipher context used. */
  52. #endif
  53. uint64_t MBEDTLS_PRIVATE(H)[MBEDTLS_GCM_HTABLE_SIZE][2]; /*!< Precalculated HTable. */
  54. uint64_t MBEDTLS_PRIVATE(len); /*!< The total length of the encrypted data. */
  55. uint64_t MBEDTLS_PRIVATE(add_len); /*!< The total length of the additional data. */
  56. unsigned char MBEDTLS_PRIVATE(base_ectr)[16]; /*!< The first ECTR for tag. */
  57. unsigned char MBEDTLS_PRIVATE(y)[16]; /*!< The Y working value. */
  58. unsigned char MBEDTLS_PRIVATE(buf)[16]; /*!< The buf working value. */
  59. unsigned char MBEDTLS_PRIVATE(mode); /*!< The operation to perform:
  60. #MBEDTLS_GCM_ENCRYPT or
  61. #MBEDTLS_GCM_DECRYPT. */
  62. unsigned char MBEDTLS_PRIVATE(acceleration); /*!< The acceleration to use. */
  63. }
  64. mbedtls_gcm_context;
  65. #else /* !MBEDTLS_GCM_ALT */
  66. #include "gcm_alt.h"
  67. #endif /* !MBEDTLS_GCM_ALT */
  68. /**
  69. * \brief This function initializes the specified GCM context,
  70. * to make references valid, and prepares the context
  71. * for mbedtls_gcm_setkey() or mbedtls_gcm_free().
  72. *
  73. * The function does not bind the GCM context to a particular
  74. * cipher, nor set the key. For this purpose, use
  75. * mbedtls_gcm_setkey().
  76. *
  77. * \param ctx The GCM context to initialize. This must not be \c NULL.
  78. */
  79. void mbedtls_gcm_init(mbedtls_gcm_context *ctx);
  80. /**
  81. * \brief This function associates a GCM context with a
  82. * cipher algorithm and a key.
  83. *
  84. * \param ctx The GCM context. This must be initialized.
  85. * \param cipher The 128-bit block cipher to use.
  86. * \param key The encryption key. This must be a readable buffer of at
  87. * least \p keybits bits.
  88. * \param keybits The key size in bits. Valid options are:
  89. * <ul><li>128 bits</li>
  90. * <li>192 bits</li>
  91. * <li>256 bits</li></ul>
  92. *
  93. * \return \c 0 on success.
  94. * \return A cipher-specific error code on failure.
  95. */
  96. int mbedtls_gcm_setkey(mbedtls_gcm_context *ctx,
  97. mbedtls_cipher_id_t cipher,
  98. const unsigned char *key,
  99. unsigned int keybits);
  100. /**
  101. * \brief This function performs GCM encryption or decryption of a buffer.
  102. *
  103. * \note The output buffer \p output can be the same as the input
  104. * buffer \p input. If \p output is greater than \p input, they
  105. * cannot overlap.
  106. *
  107. * \warning When this function performs a decryption, it outputs the
  108. * authentication tag and does not verify that the data is
  109. * authentic. You should use this function to perform encryption
  110. * only. For decryption, use mbedtls_gcm_auth_decrypt() instead.
  111. *
  112. * \param ctx The GCM context to use for encryption or decryption. This
  113. * must be initialized.
  114. * \param mode The operation to perform:
  115. * - #MBEDTLS_GCM_ENCRYPT to perform authenticated encryption.
  116. * The ciphertext is written to \p output and the
  117. * authentication tag is written to \p tag.
  118. * - #MBEDTLS_GCM_DECRYPT to perform decryption.
  119. * The plaintext is written to \p output and the
  120. * authentication tag is written to \p tag.
  121. * Note that this mode is not recommended, because it does
  122. * not verify the authenticity of the data. For this reason,
  123. * you should use mbedtls_gcm_auth_decrypt() instead of
  124. * calling this function in decryption mode.
  125. * \param length The length of the input data, which is equal to the length
  126. * of the output data.
  127. * \param iv The initialization vector. This must be a readable buffer of
  128. * at least \p iv_len Bytes.
  129. * \param iv_len The length of the IV.
  130. * \param add The buffer holding the additional data. This must be of at
  131. * least that size in Bytes.
  132. * \param add_len The length of the additional data.
  133. * \param input The buffer holding the input data. If \p length is greater
  134. * than zero, this must be a readable buffer of at least that
  135. * size in Bytes.
  136. * \param output The buffer for holding the output data. If \p length is greater
  137. * than zero, this must be a writable buffer of at least that
  138. * size in Bytes.
  139. * \param tag_len The length of the tag to generate.
  140. * \param tag The buffer for holding the tag. This must be a writable
  141. * buffer of at least \p tag_len Bytes.
  142. *
  143. * \return \c 0 if the encryption or decryption was performed
  144. * successfully. Note that in #MBEDTLS_GCM_DECRYPT mode,
  145. * this does not indicate that the data is authentic.
  146. * \return #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths or pointers are
  147. * not valid or a cipher-specific error code if the encryption
  148. * or decryption failed.
  149. */
  150. int mbedtls_gcm_crypt_and_tag(mbedtls_gcm_context *ctx,
  151. int mode,
  152. size_t length,
  153. const unsigned char *iv,
  154. size_t iv_len,
  155. const unsigned char *add,
  156. size_t add_len,
  157. const unsigned char *input,
  158. unsigned char *output,
  159. size_t tag_len,
  160. unsigned char *tag);
  161. /**
  162. * \brief This function performs a GCM authenticated decryption of a
  163. * buffer.
  164. *
  165. * \note The output buffer \p output can be the same as the input
  166. * buffer \p input. If \p output is greater than \p input, they
  167. * cannot overlap. Implementations which require
  168. * MBEDTLS_GCM_ALT to be enabled may not provide support for
  169. * overlapping buffers.
  170. *
  171. * \param ctx The GCM context. This must be initialized.
  172. * \param length The length of the ciphertext to decrypt, which is also
  173. * the length of the decrypted plaintext.
  174. * \param iv The initialization vector. This must be a readable buffer
  175. * of at least \p iv_len Bytes.
  176. * \param iv_len The length of the IV.
  177. * \param add The buffer holding the additional data. This must be of at
  178. * least that size in Bytes.
  179. * \param add_len The length of the additional data.
  180. * \param tag The buffer holding the tag to verify. This must be a
  181. * readable buffer of at least \p tag_len Bytes.
  182. * \param tag_len The length of the tag to verify.
  183. * \param input The buffer holding the ciphertext. If \p length is greater
  184. * than zero, this must be a readable buffer of at least that
  185. * size.
  186. * \param output The buffer for holding the decrypted plaintext. If \p length
  187. * is greater than zero, this must be a writable buffer of at
  188. * least that size.
  189. *
  190. * \return \c 0 if successful and authenticated.
  191. * \return #MBEDTLS_ERR_GCM_AUTH_FAILED if the tag does not match.
  192. * \return #MBEDTLS_ERR_GCM_BAD_INPUT if the lengths or pointers are
  193. * not valid or a cipher-specific error code if the decryption
  194. * failed.
  195. */
  196. int mbedtls_gcm_auth_decrypt(mbedtls_gcm_context *ctx,
  197. size_t length,
  198. const unsigned char *iv,
  199. size_t iv_len,
  200. const unsigned char *add,
  201. size_t add_len,
  202. const unsigned char *tag,
  203. size_t tag_len,
  204. const unsigned char *input,
  205. unsigned char *output);
  206. /**
  207. * \brief This function starts a GCM encryption or decryption
  208. * operation.
  209. *
  210. * \param ctx The GCM context. This must be initialized.
  211. * \param mode The operation to perform: #MBEDTLS_GCM_ENCRYPT or
  212. * #MBEDTLS_GCM_DECRYPT.
  213. * \param iv The initialization vector. This must be a readable buffer of
  214. * at least \p iv_len Bytes.
  215. * \param iv_len The length of the IV.
  216. *
  217. * \return \c 0 on success.
  218. */
  219. int mbedtls_gcm_starts(mbedtls_gcm_context *ctx,
  220. int mode,
  221. const unsigned char *iv,
  222. size_t iv_len);
  223. /**
  224. * \brief This function feeds an input buffer as associated data
  225. * (authenticated but not encrypted data) in a GCM
  226. * encryption or decryption operation.
  227. *
  228. * Call this function after mbedtls_gcm_starts() to pass
  229. * the associated data. If the associated data is empty,
  230. * you do not need to call this function. You may not
  231. * call this function after calling mbedtls_cipher_update().
  232. *
  233. * \param ctx The GCM context. This must have been started with
  234. * mbedtls_gcm_starts() and must not have yet received
  235. * any input with mbedtls_gcm_update().
  236. * \param add The buffer holding the additional data, or \c NULL
  237. * if \p add_len is \c 0.
  238. * \param add_len The length of the additional data. If \c 0,
  239. * \p add may be \c NULL.
  240. *
  241. * \return \c 0 on success.
  242. */
  243. int mbedtls_gcm_update_ad(mbedtls_gcm_context *ctx,
  244. const unsigned char *add,
  245. size_t add_len);
  246. /**
  247. * \brief This function feeds an input buffer into an ongoing GCM
  248. * encryption or decryption operation.
  249. *
  250. * You may call this function zero, one or more times
  251. * to pass successive parts of the input: the plaintext to
  252. * encrypt, or the ciphertext (not including the tag) to
  253. * decrypt. After the last part of the input, call
  254. * mbedtls_gcm_finish().
  255. *
  256. * This function may produce output in one of the following
  257. * ways:
  258. * - Immediate output: the output length is always equal
  259. * to the input length.
  260. * - Buffered output: the output consists of a whole number
  261. * of 16-byte blocks. If the total input length so far
  262. * (not including associated data) is 16 \* *B* + *A*
  263. * with *A* < 16 then the total output length is 16 \* *B*.
  264. *
  265. * In particular:
  266. * - It is always correct to call this function with
  267. * \p output_size >= \p input_length + 15.
  268. * - If \p input_length is a multiple of 16 for all the calls
  269. * to this function during an operation, then it is
  270. * correct to use \p output_size = \p input_length.
  271. *
  272. * \note The output buffer \p output can be the same as the input
  273. * buffer \p input. If \p output is greater than \p input, they
  274. * cannot overlap. Implementations which require
  275. * MBEDTLS_GCM_ALT to be enabled may not provide support for
  276. * overlapping buffers.
  277. *
  278. * \param ctx The GCM context. This must be initialized.
  279. * \param input The buffer holding the input data. If \p input_length
  280. * is greater than zero, this must be a readable buffer
  281. * of at least \p input_length bytes.
  282. * \param input_length The length of the input data in bytes.
  283. * \param output The buffer for the output data. If \p output_size
  284. * is greater than zero, this must be a writable buffer of
  285. * of at least \p output_size bytes.
  286. * \param output_size The size of the output buffer in bytes.
  287. * See the function description regarding the output size.
  288. * \param output_length On success, \p *output_length contains the actual
  289. * length of the output written in \p output.
  290. * On failure, the content of \p *output_length is
  291. * unspecified.
  292. *
  293. * \return \c 0 on success.
  294. * \return #MBEDTLS_ERR_GCM_BAD_INPUT on failure:
  295. * total input length too long,
  296. * unsupported input/output buffer overlap detected,
  297. * or \p output_size too small.
  298. */
  299. int mbedtls_gcm_update(mbedtls_gcm_context *ctx,
  300. const unsigned char *input, size_t input_length,
  301. unsigned char *output, size_t output_size,
  302. size_t *output_length);
  303. /**
  304. * \brief This function finishes the GCM operation and generates
  305. * the authentication tag.
  306. *
  307. * It wraps up the GCM stream, and generates the
  308. * tag. The tag can have a maximum length of 16 Bytes.
  309. *
  310. * \param ctx The GCM context. This must be initialized.
  311. * \param tag The buffer for holding the tag. This must be a writable
  312. * buffer of at least \p tag_len Bytes.
  313. * \param tag_len The length of the tag to generate. This must be at least
  314. * four.
  315. * \param output The buffer for the final output.
  316. * If \p output_size is nonzero, this must be a writable
  317. * buffer of at least \p output_size bytes.
  318. * \param output_size The size of the \p output buffer in bytes.
  319. * This must be large enough for the output that
  320. * mbedtls_gcm_update() has not produced. In particular:
  321. * - If mbedtls_gcm_update() produces immediate output,
  322. * or if the total input size is a multiple of \c 16,
  323. * then mbedtls_gcm_finish() never produces any output,
  324. * so \p output_size can be \c 0.
  325. * - \p output_size never needs to be more than \c 15.
  326. * \param output_length On success, \p *output_length contains the actual
  327. * length of the output written in \p output.
  328. * On failure, the content of \p *output_length is
  329. * unspecified.
  330. *
  331. * \return \c 0 on success.
  332. * \return #MBEDTLS_ERR_GCM_BAD_INPUT on failure:
  333. * invalid value of \p tag_len,
  334. * or \p output_size too small.
  335. */
  336. int mbedtls_gcm_finish(mbedtls_gcm_context *ctx,
  337. unsigned char *output, size_t output_size,
  338. size_t *output_length,
  339. unsigned char *tag, size_t tag_len);
  340. /**
  341. * \brief This function clears a GCM context and the underlying
  342. * cipher sub-context.
  343. *
  344. * \param ctx The GCM context to clear. If this is \c NULL, the call has
  345. * no effect. Otherwise, this must be initialized.
  346. */
  347. void mbedtls_gcm_free(mbedtls_gcm_context *ctx);
  348. #if defined(MBEDTLS_SELF_TEST)
  349. /**
  350. * \brief The GCM checkup routine.
  351. *
  352. * \return \c 0 on success.
  353. * \return \c 1 on failure.
  354. */
  355. int mbedtls_gcm_self_test(int verbose);
  356. #endif /* MBEDTLS_SELF_TEST */
  357. #ifdef __cplusplus
  358. }
  359. #endif
  360. #endif /* gcm.h */