gcm.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /******************************************************************************
  2. *
  3. * THIS SOURCE CODE IS HEREBY PLACED INTO THE PUBLIC DOMAIN FOR THE GOOD OF ALL
  4. *
  5. * This is a simple and straightforward implementation of AES-GCM authenticated
  6. * encryption. The focus of this work was correctness & accuracy. It is written
  7. * in straight 'C' without any particular focus upon optimization or speed. It
  8. * should be endian (memory byte order) neutral since the few places that care
  9. * are handled explicitly.
  10. *
  11. * This implementation of AES-GCM was created by Steven M. Gibson of GRC.com.
  12. *
  13. * It is intended for general purpose use, but was written in support of GRC's
  14. * reference implementation of the SQRL (Secure Quick Reliable Login) client.
  15. *
  16. * See: http://csrc.nist.gov/publications/nistpubs/800-38D/SP-800-38D.pdf
  17. * http://csrc.nist.gov/groups/ST/toolkit/BCM/documents/proposedmodes/ \
  18. * gcm/gcm-revised-spec.pdf
  19. *
  20. * NO COPYRIGHT IS CLAIMED IN THIS WORK, HOWEVER, NEITHER IS ANY WARRANTY MADE
  21. * REGARDING ITS FITNESS FOR ANY PARTICULAR PURPOSE. USE IT AT YOUR OWN RISK.
  22. *
  23. *******************************************************************************/
  24. #ifndef GCM_HEADER
  25. #define GCM_HEADER
  26. #define GCM_AUTH_FAILURE 0x55555555 // authentication failure
  27. #include "aes.h" // gcm_context includes aes_context
  28. #if defined(_MSC_VER)
  29. #include <basetsd.h>
  30. typedef unsigned int size_t; // use the right type for length declarations
  31. typedef UINT32 uint32_t;
  32. typedef UINT64 uint64_t;
  33. #else
  34. #include <stdint.h>
  35. #endif
  36. /******************************************************************************
  37. * GCM_CONTEXT : GCM context / holds keytables, instance data, and AES ctx
  38. ******************************************************************************/
  39. typedef struct {
  40. int mode; // cipher direction: encrypt/decrypt
  41. uint64_t len; // cipher data length processed so far
  42. uint64_t add_len; // total add data length
  43. uint64_t HL[16]; // precalculated lo-half HTable
  44. uint64_t HH[16]; // precalculated hi-half HTable
  45. uchar base_ectr[16]; // first counter-mode cipher output for tag
  46. uchar y[16]; // the current cipher-input IV|Counter value
  47. uchar buf[16]; // buf working value
  48. aes_context aes_ctx; // cipher context used
  49. } gcm_context;
  50. /******************************************************************************
  51. * GCM_CONTEXT : MUST be called once before ANY use of this library
  52. ******************************************************************************/
  53. int gcm_initialize(void);
  54. /******************************************************************************
  55. * GCM_SETKEY : sets the GCM (and AES) keying material for use
  56. ******************************************************************************/
  57. int gcm_setkey(
  58. gcm_context* ctx, // caller-provided context ptr
  59. const uchar* key, // pointer to cipher key
  60. const uint keysize // size in bytes (must be 16, 24, 32 for
  61. // 128, 192 or 256-bit keys respectively)
  62. ); // returns 0 for success
  63. /******************************************************************************
  64. *
  65. * GCM_CRYPT_AND_TAG
  66. *
  67. * This either encrypts or decrypts the user-provided data and, either
  68. * way, generates an authentication tag of the requested length. It must be
  69. * called with a GCM context whose key has already been set with GCM_SETKEY.
  70. *
  71. * The user would typically call this explicitly to ENCRYPT a buffer of data
  72. * and optional associated data, and produce its an authentication tag.
  73. *
  74. * To reverse the process the user would typically call the companion
  75. * GCM_AUTH_DECRYPT function to decrypt data and verify a user-provided
  76. * authentication tag. The GCM_AUTH_DECRYPT function calls this function
  77. * to perform its decryption and tag generation, which it then compares.
  78. *
  79. ******************************************************************************/
  80. int gcm_crypt_and_tag(
  81. gcm_context* ctx, // gcm context with key already setup
  82. int mode, // cipher direction: ENCRYPT (1) or DECRYPT (0)
  83. const uchar* iv, // pointer to the 12-byte initialization vector
  84. size_t iv_len, // byte length if the IV. should always be 12
  85. const uchar* add, // pointer to the non-ciphered additional data
  86. size_t add_len, // byte length of the additional AEAD data
  87. const uchar* input, // pointer to the cipher data source
  88. uchar* output, // pointer to the cipher data destination
  89. size_t length, // byte length of the cipher data
  90. uchar* tag, // pointer to the tag to be generated
  91. size_t tag_len); // byte length of the tag to be generated
  92. /******************************************************************************
  93. *
  94. * GCM_AUTH_DECRYPT
  95. *
  96. * This DECRYPTS a user-provided data buffer with optional associated data.
  97. * It then verifies a user-supplied authentication tag against the tag just
  98. * re-created during decryption to verify that the data has not been altered.
  99. *
  100. * This function calls GCM_CRYPT_AND_TAG (above) to perform the decryption
  101. * and authentication tag generation.
  102. *
  103. ******************************************************************************/
  104. int gcm_auth_decrypt(
  105. gcm_context* ctx, // gcm context with key already setup
  106. const uchar* iv, // pointer to the 12-byte initialization vector
  107. size_t iv_len, // byte length if the IV. should always be 12
  108. const uchar* add, // pointer to the non-ciphered additional data
  109. size_t add_len, // byte length of the additional AEAD data
  110. const uchar* input, // pointer to the cipher data source
  111. uchar* output, // pointer to the cipher data destination
  112. size_t length, // byte length of the cipher data
  113. const uchar* tag, // pointer to the tag to be authenticated
  114. size_t tag_len); // byte length of the tag <= 16
  115. /******************************************************************************
  116. *
  117. * GCM_START
  118. *
  119. * Given a user-provided GCM context, this initializes it, sets the encryption
  120. * mode, and preprocesses the initialization vector and additional AEAD data.
  121. *
  122. ******************************************************************************/
  123. int gcm_start(
  124. gcm_context* ctx, // pointer to user-provided GCM context
  125. int mode, // ENCRYPT (1) or DECRYPT (0)
  126. const uchar* iv, // pointer to initialization vector
  127. size_t iv_len, // IV length in bytes (should == 12)
  128. const uchar* add, // pointer to additional AEAD data (NULL if none)
  129. size_t add_len); // length of additional AEAD data (bytes)
  130. /******************************************************************************
  131. *
  132. * GCM_UPDATE
  133. *
  134. * This is called once or more to process bulk plaintext or ciphertext data.
  135. * We give this some number of bytes of input and it returns the same number
  136. * of output bytes. If called multiple times (which is fine) all but the final
  137. * invocation MUST be called with length mod 16 == 0. (Only the final call can
  138. * have a partial block length of < 128 bits.)
  139. *
  140. ******************************************************************************/
  141. int gcm_update(
  142. gcm_context* ctx, // pointer to user-provided GCM context
  143. size_t length, // length, in bytes, of data to process
  144. const uchar* input, // pointer to source data
  145. uchar* output); // pointer to destination data
  146. /******************************************************************************
  147. *
  148. * GCM_FINISH
  149. *
  150. * This is called once after all calls to GCM_UPDATE to finalize the GCM.
  151. * It performs the final GHASH to produce the resulting authentication TAG.
  152. *
  153. ******************************************************************************/
  154. int gcm_finish(
  155. gcm_context* ctx, // pointer to user-provided GCM context
  156. uchar* tag, // ptr to tag buffer - NULL if tag_len = 0
  157. size_t tag_len); // length, in bytes, of the tag-receiving buf
  158. /******************************************************************************
  159. *
  160. * GCM_ZERO_CTX
  161. *
  162. * The GCM context contains both the GCM context and the AES context.
  163. * This includes keying and key-related material which is security-
  164. * sensitive, so it MUST be zeroed after use. This function does that.
  165. *
  166. ******************************************************************************/
  167. void gcm_zero_ctx(gcm_context* ctx);
  168. #endif /* GCM_HEADER */