gcm.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /**
  2. * \file gcm.h
  3. *
  4. * \brief Galois/Counter mode for 128-bit block ciphers
  5. *
  6. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  7. * SPDX-License-Identifier: GPL-2.0
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  22. *
  23. * This file is part of mbed TLS (https://tls.mbed.org)
  24. */
  25. #ifndef MBEDTLS_GCM_H
  26. #define MBEDTLS_GCM_H
  27. #include "cipher.h"
  28. #include <stdint.h>
  29. #define MBEDTLS_GCM_ENCRYPT 1
  30. #define MBEDTLS_GCM_DECRYPT 0
  31. #define MBEDTLS_ERR_GCM_AUTH_FAILED -0x0012 /**< Authenticated decryption failed. */
  32. #define MBEDTLS_ERR_GCM_BAD_INPUT -0x0014 /**< Bad input parameters to function. */
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. /**
  37. * \brief GCM context structure
  38. */
  39. typedef struct {
  40. mbedtls_cipher_context_t cipher_ctx;/*!< cipher context used */
  41. uint64_t HL[16]; /*!< Precalculated HTable */
  42. uint64_t HH[16]; /*!< Precalculated HTable */
  43. uint64_t len; /*!< Total data length */
  44. uint64_t add_len; /*!< Total add length */
  45. unsigned char base_ectr[16];/*!< First ECTR for tag */
  46. unsigned char y[16]; /*!< Y working value */
  47. unsigned char buf[16]; /*!< buf working value */
  48. int mode; /*!< Encrypt or Decrypt */
  49. }
  50. mbedtls_gcm_context;
  51. /**
  52. * \brief Initialize GCM context (just makes references valid)
  53. * Makes the context ready for mbedtls_gcm_setkey() or
  54. * mbedtls_gcm_free().
  55. *
  56. * \param ctx GCM context to initialize
  57. */
  58. void mbedtls_gcm_init( mbedtls_gcm_context *ctx );
  59. /**
  60. * \brief GCM initialization (encryption)
  61. *
  62. * \param ctx GCM context to be initialized
  63. * \param cipher cipher to use (a 128-bit block cipher)
  64. * \param key encryption key
  65. * \param keybits must be 128, 192 or 256
  66. *
  67. * \return 0 if successful, or a cipher specific error code
  68. */
  69. int mbedtls_gcm_setkey( mbedtls_gcm_context *ctx,
  70. mbedtls_cipher_id_t cipher,
  71. const unsigned char *key,
  72. unsigned int keybits );
  73. /**
  74. * \brief GCM buffer encryption/decryption using a block cipher
  75. *
  76. * \note On encryption, the output buffer can be the same as the input buffer.
  77. * On decryption, the output buffer cannot be the same as input buffer.
  78. * If buffers overlap, the output buffer must trail at least 8 bytes
  79. * behind the input buffer.
  80. *
  81. * \param ctx GCM context
  82. * \param mode MBEDTLS_GCM_ENCRYPT or MBEDTLS_GCM_DECRYPT
  83. * \param length length of the input data
  84. * \param iv initialization vector
  85. * \param iv_len length of IV
  86. * \param add additional data
  87. * \param add_len length of additional data
  88. * \param input buffer holding the input data
  89. * \param output buffer for holding the output data
  90. * \param tag_len length of the tag to generate
  91. * \param tag buffer for holding the tag
  92. *
  93. * \return 0 if successful
  94. */
  95. int mbedtls_gcm_crypt_and_tag( mbedtls_gcm_context *ctx,
  96. int mode,
  97. size_t length,
  98. const unsigned char *iv,
  99. size_t iv_len,
  100. const unsigned char *add,
  101. size_t add_len,
  102. const unsigned char *input,
  103. unsigned char *output,
  104. size_t tag_len,
  105. unsigned char *tag );
  106. /**
  107. * \brief GCM buffer authenticated decryption using a block cipher
  108. *
  109. * \note On decryption, the output buffer cannot be the same as input buffer.
  110. * If buffers overlap, the output buffer must trail at least 8 bytes
  111. * behind the input buffer.
  112. *
  113. * \param ctx GCM context
  114. * \param length length of the input data
  115. * \param iv initialization vector
  116. * \param iv_len length of IV
  117. * \param add additional data
  118. * \param add_len length of additional data
  119. * \param tag buffer holding the tag
  120. * \param tag_len length of the tag
  121. * \param input buffer holding the input data
  122. * \param output buffer for holding the output data
  123. *
  124. * \return 0 if successful and authenticated,
  125. * MBEDTLS_ERR_GCM_AUTH_FAILED if tag does not match
  126. */
  127. int mbedtls_gcm_auth_decrypt( mbedtls_gcm_context *ctx,
  128. size_t length,
  129. const unsigned char *iv,
  130. size_t iv_len,
  131. const unsigned char *add,
  132. size_t add_len,
  133. const unsigned char *tag,
  134. size_t tag_len,
  135. const unsigned char *input,
  136. unsigned char *output );
  137. /**
  138. * \brief Generic GCM stream start function
  139. *
  140. * \param ctx GCM context
  141. * \param mode MBEDTLS_GCM_ENCRYPT or MBEDTLS_GCM_DECRYPT
  142. * \param iv initialization vector
  143. * \param iv_len length of IV
  144. * \param add additional data (or NULL if length is 0)
  145. * \param add_len length of additional data
  146. *
  147. * \return 0 if successful
  148. */
  149. int mbedtls_gcm_starts( mbedtls_gcm_context *ctx,
  150. int mode,
  151. const unsigned char *iv,
  152. size_t iv_len,
  153. const unsigned char *add,
  154. size_t add_len );
  155. /**
  156. * \brief Generic GCM update function. Encrypts/decrypts using the
  157. * given GCM context. Expects input to be a multiple of 16
  158. * bytes! Only the last call before mbedtls_gcm_finish() can be less
  159. * than 16 bytes!
  160. *
  161. * \note On decryption, the output buffer cannot be the same as input buffer.
  162. * If buffers overlap, the output buffer must trail at least 8 bytes
  163. * behind the input buffer.
  164. *
  165. * \param ctx GCM context
  166. * \param length length of the input data
  167. * \param input buffer holding the input data
  168. * \param output buffer for holding the output data
  169. *
  170. * \return 0 if successful or MBEDTLS_ERR_GCM_BAD_INPUT
  171. */
  172. int mbedtls_gcm_update( mbedtls_gcm_context *ctx,
  173. size_t length,
  174. const unsigned char *input,
  175. unsigned char *output );
  176. /**
  177. * \brief Generic GCM finalisation function. Wraps up the GCM stream
  178. * and generates the tag. The tag can have a maximum length of
  179. * 16 bytes.
  180. *
  181. * \param ctx GCM context
  182. * \param tag buffer for holding the tag
  183. * \param tag_len length of the tag to generate (must be at least 4)
  184. *
  185. * \return 0 if successful or MBEDTLS_ERR_GCM_BAD_INPUT
  186. */
  187. int mbedtls_gcm_finish( mbedtls_gcm_context *ctx,
  188. unsigned char *tag,
  189. size_t tag_len );
  190. /**
  191. * \brief Free a GCM context and underlying cipher sub-context
  192. *
  193. * \param ctx GCM context to free
  194. */
  195. void mbedtls_gcm_free( mbedtls_gcm_context *ctx );
  196. /**
  197. * \brief Checkup routine
  198. *
  199. * \return 0 if successful, or 1 if the test failed
  200. */
  201. int mbedtls_gcm_self_test( int verbose );
  202. #ifdef __cplusplus
  203. }
  204. #endif
  205. #endif /* gcm.h */