gcm.h 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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( gcm_context *ctx, // caller-provided context ptr
  58. const uchar *key, // pointer to cipher key
  59. const uint keysize // size in bytes (must be 16, 24, 32 for
  60. // 128, 192 or 256-bit keys respectively)
  61. ); // returns 0 for success
  62. /******************************************************************************
  63. *
  64. * GCM_CRYPT_AND_TAG
  65. *
  66. * This either encrypts or decrypts the user-provided data and, either
  67. * way, generates an authentication tag of the requested length. It must be
  68. * called with a GCM context whose key has already been set with GCM_SETKEY.
  69. *
  70. * The user would typically call this explicitly to ENCRYPT a buffer of data
  71. * and optional associated data, and produce its an authentication tag.
  72. *
  73. * To reverse the process the user would typically call the companion
  74. * GCM_AUTH_DECRYPT function to decrypt data and verify a user-provided
  75. * authentication tag. The GCM_AUTH_DECRYPT function calls this function
  76. * to perform its decryption and tag generation, which it then compares.
  77. *
  78. ******************************************************************************/
  79. int gcm_crypt_and_tag(
  80. gcm_context *ctx, // gcm context with key already setup
  81. int mode, // cipher direction: ENCRYPT (1) or DECRYPT (0)
  82. const uchar *iv, // pointer to the 12-byte initialization vector
  83. size_t iv_len, // byte length if the IV. should always be 12
  84. const uchar *add, // pointer to the non-ciphered additional data
  85. size_t add_len, // byte length of the additional AEAD data
  86. const uchar *input, // pointer to the cipher data source
  87. uchar *output, // pointer to the cipher data destination
  88. size_t length, // byte length of the cipher data
  89. uchar *tag, // pointer to the tag to be generated
  90. size_t tag_len ); // byte length of the tag to be generated
  91. /******************************************************************************
  92. *
  93. * GCM_AUTH_DECRYPT
  94. *
  95. * This DECRYPTS a user-provided data buffer with optional associated data.
  96. * It then verifies a user-supplied authentication tag against the tag just
  97. * re-created during decryption to verify that the data has not been altered.
  98. *
  99. * This function calls GCM_CRYPT_AND_TAG (above) to perform the decryption
  100. * and authentication tag generation.
  101. *
  102. ******************************************************************************/
  103. int gcm_auth_decrypt(
  104. gcm_context *ctx, // gcm context with key already setup
  105. const uchar *iv, // pointer to the 12-byte initialization vector
  106. size_t iv_len, // byte length if the IV. should always be 12
  107. const uchar *add, // pointer to the non-ciphered additional data
  108. size_t add_len, // byte length of the additional AEAD data
  109. const uchar *input, // pointer to the cipher data source
  110. uchar *output, // pointer to the cipher data destination
  111. size_t length, // byte length of the cipher data
  112. const uchar *tag, // pointer to the tag to be authenticated
  113. size_t tag_len ); // byte length of the tag <= 16
  114. /******************************************************************************
  115. *
  116. * GCM_START
  117. *
  118. * Given a user-provided GCM context, this initializes it, sets the encryption
  119. * mode, and preprocesses the initialization vector and additional AEAD data.
  120. *
  121. ******************************************************************************/
  122. int gcm_start( gcm_context *ctx, // pointer to user-provided GCM context
  123. int mode, // ENCRYPT (1) or DECRYPT (0)
  124. const uchar *iv, // pointer to initialization vector
  125. size_t iv_len, // IV length in bytes (should == 12)
  126. const uchar *add, // pointer to additional AEAD data (NULL if none)
  127. size_t add_len ); // length of additional AEAD data (bytes)
  128. /******************************************************************************
  129. *
  130. * GCM_UPDATE
  131. *
  132. * This is called once or more to process bulk plaintext or ciphertext data.
  133. * We give this some number of bytes of input and it returns the same number
  134. * of output bytes. If called multiple times (which is fine) all but the final
  135. * invocation MUST be called with length mod 16 == 0. (Only the final call can
  136. * have a partial block length of < 128 bits.)
  137. *
  138. ******************************************************************************/
  139. int gcm_update( gcm_context *ctx, // pointer to user-provided GCM context
  140. size_t length, // length, in bytes, of data to process
  141. const uchar *input, // pointer to source data
  142. uchar *output ); // pointer to destination data
  143. /******************************************************************************
  144. *
  145. * GCM_FINISH
  146. *
  147. * This is called once after all calls to GCM_UPDATE to finalize the GCM.
  148. * It performs the final GHASH to produce the resulting authentication TAG.
  149. *
  150. ******************************************************************************/
  151. int gcm_finish( gcm_context *ctx, // pointer to user-provided GCM context
  152. uchar *tag, // ptr to tag buffer - NULL if tag_len = 0
  153. size_t tag_len ); // length, in bytes, of the tag-receiving buf
  154. /******************************************************************************
  155. *
  156. * GCM_ZERO_CTX
  157. *
  158. * The GCM context contains both the GCM context and the AES context.
  159. * This includes keying and key-related material which is security-
  160. * sensitive, so it MUST be zeroed after use. This function does that.
  161. *
  162. ******************************************************************************/
  163. void gcm_zero_ctx( gcm_context *ctx );
  164. #endif /* GCM_HEADER */