gcm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. #include "gcm.h"
  25. #include "aes.h"
  26. /******************************************************************************
  27. * ==== IMPLEMENTATION WARNING ====
  28. *
  29. * This code was developed for use within SQRL's fixed environmnent. Thus, it
  30. * is somewhat less "general purpose" than it would be if it were designed as
  31. * a general purpose AES-GCM library. Specifically, it bothers with almost NO
  32. * error checking on parameter limits, buffer bounds, etc. It assumes that it
  33. * is being invoked by its author or by someone who understands the values it
  34. * expects to receive. Its behavior will be undefined otherwise.
  35. *
  36. * All functions that might fail are defined to return 'ints' to indicate a
  37. * problem. Most do not do so now. But this allows for error propagation out
  38. * of internal functions if robust error checking should ever be desired.
  39. *
  40. ******************************************************************************/
  41. /* Calculating the "GHASH"
  42. *
  43. * There are many ways of calculating the so-called GHASH in software, each with
  44. * a traditional size vs performance tradeoff. The GHASH (Galois field hash) is
  45. * an intriguing construction which takes two 128-bit strings (also the cipher's
  46. * block size and the fundamental operation size for the system) and hashes them
  47. * into a third 128-bit result.
  48. *
  49. * Many implementation solutions have been worked out that use large precomputed
  50. * table lookups in place of more time consuming bit fiddling, and this approach
  51. * can be scaled easily upward or downward as needed to change the time/space
  52. * tradeoff. It's been studied extensively and there's a solid body of theory and
  53. * practice. For example, without using any lookup tables an implementation
  54. * might obtain 119 cycles per byte throughput, whereas using a simple, though
  55. * large, key-specific 64 kbyte 8-bit lookup table the performance jumps to 13
  56. * cycles per byte.
  57. *
  58. * And Intel's processors have, since 2010, included an instruction which does
  59. * the entire 128x128->128 bit job in just several 64x64->128 bit pieces.
  60. *
  61. * Since SQRL is interactive, and only processing a few 128-bit blocks, I've
  62. * settled upon a relatively slower but appealing small-table compromise which
  63. * folds a bunch of not only time consuming but also bit twiddling into a simple
  64. * 16-entry table which is attributed to Victor Shoup's 1996 work while at
  65. * Bellcore: "On Fast and Provably Secure MessageAuthentication Based on
  66. * Universal Hashing." See: http://www.shoup.net/papers/macs.pdf
  67. * See, also section 4.1 of the "gcm-revised-spec" cited above.
  68. */
  69. /*
  70. * This 16-entry table of pre-computed constants is used by the
  71. * GHASH multiplier to improve over a strictly table-free but
  72. * significantly slower 128x128 bit multiple within GF(2^128).
  73. */
  74. static const uint64_t last4[16] = {
  75. 0x0000, 0x1c20, 0x3840, 0x2460, 0x7080, 0x6ca0, 0x48c0, 0x54e0,
  76. 0xe100, 0xfd20, 0xd940, 0xc560, 0x9180, 0x8da0, 0xa9c0, 0xb5e0 };
  77. /*
  78. * Platform Endianness Neutralizing Load and Store Macro definitions
  79. * GCM wants platform-neutral Big Endian (BE) byte ordering
  80. */
  81. #define GET_UINT32_BE(n,b,i) { \
  82. (n) = ( (uint32_t) (b)[(i) ] << 24 ) \
  83. | ( (uint32_t) (b)[(i) + 1] << 16 ) \
  84. | ( (uint32_t) (b)[(i) + 2] << 8 ) \
  85. | ( (uint32_t) (b)[(i) + 3] ); }
  86. #define PUT_UINT32_BE(n,b,i) { \
  87. (b)[(i) ] = (uchar) ( (n) >> 24 ); \
  88. (b)[(i) + 1] = (uchar) ( (n) >> 16 ); \
  89. (b)[(i) + 2] = (uchar) ( (n) >> 8 ); \
  90. (b)[(i) + 3] = (uchar) ( (n) ); }
  91. /******************************************************************************
  92. *
  93. * GCM_INITIALIZE
  94. *
  95. * Must be called once to initialize the GCM library.
  96. *
  97. * At present, this only calls the AES keygen table generator, which expands
  98. * the AES keying tables for use. This is NOT A THREAD-SAFE function, so it
  99. * MUST be called during system initialization before a multi-threading
  100. * environment is running.
  101. *
  102. ******************************************************************************/
  103. int gcm_initialize( void )
  104. {
  105. aes_init_keygen_tables();
  106. return( 0 );
  107. }
  108. /******************************************************************************
  109. *
  110. * GCM_MULT
  111. *
  112. * Performs a GHASH operation on the 128-bit input vector 'x', setting
  113. * the 128-bit output vector to 'x' times H using our precomputed tables.
  114. * 'x' and 'output' are seen as elements of GCM's GF(2^128) Galois field.
  115. *
  116. ******************************************************************************/
  117. static void gcm_mult( gcm_context *ctx, // pointer to established context
  118. const uchar x[16], // pointer to 128-bit input vector
  119. uchar output[16] ) // pointer to 128-bit output vector
  120. {
  121. int i;
  122. uchar lo, hi, rem;
  123. uint64_t zh, zl;
  124. lo = (uchar)( x[15] & 0x0f );
  125. hi = (uchar)( x[15] >> 4 );
  126. zh = ctx->HH[lo];
  127. zl = ctx->HL[lo];
  128. for( i = 15; i >= 0; i-- ) {
  129. lo = (uchar) ( x[i] & 0x0f );
  130. hi = (uchar) ( x[i] >> 4 );
  131. if( i != 15 ) {
  132. rem = (uchar) ( zl & 0x0f );
  133. zl = ( zh << 60 ) | ( zl >> 4 );
  134. zh = ( zh >> 4 );
  135. zh ^= (uint64_t) last4[rem] << 48;
  136. zh ^= ctx->HH[lo];
  137. zl ^= ctx->HL[lo];
  138. }
  139. rem = (uchar) ( zl & 0x0f );
  140. zl = ( zh << 60 ) | ( zl >> 4 );
  141. zh = ( zh >> 4 );
  142. zh ^= (uint64_t) last4[rem] << 48;
  143. zh ^= ctx->HH[hi];
  144. zl ^= ctx->HL[hi];
  145. }
  146. PUT_UINT32_BE( zh >> 32, output, 0 );
  147. PUT_UINT32_BE( zh, output, 4 );
  148. PUT_UINT32_BE( zl >> 32, output, 8 );
  149. PUT_UINT32_BE( zl, output, 12 );
  150. }
  151. /******************************************************************************
  152. *
  153. * GCM_SETKEY
  154. *
  155. * This is called to set the AES-GCM key. It initializes the AES key
  156. * and populates the gcm context's pre-calculated HTables.
  157. *
  158. ******************************************************************************/
  159. int gcm_setkey( gcm_context *ctx, // pointer to caller-provided gcm context
  160. const uchar *key, // pointer to the AES encryption key
  161. const uint keysize) // size in bytes (must be 16, 24, 32 for
  162. // 128, 192 or 256-bit keys respectively)
  163. {
  164. int ret, i, j;
  165. uint64_t hi, lo;
  166. uint64_t vl, vh;
  167. unsigned char h[16];
  168. memset( ctx, 0, sizeof(gcm_context) ); // zero caller-provided GCM context
  169. memset( h, 0, 16 ); // initialize the block to encrypt
  170. // encrypt the null 128-bit block to generate a key-based value
  171. // which is then used to initialize our GHASH lookup tables
  172. if(( ret = aes_setkey( &ctx->aes_ctx, ENCRYPT, key, keysize )) != 0 )
  173. return( ret );
  174. if(( ret = aes_cipher( &ctx->aes_ctx, h, h )) != 0 )
  175. return( ret );
  176. GET_UINT32_BE( hi, h, 0 ); // pack h as two 64-bit ints, big-endian
  177. GET_UINT32_BE( lo, h, 4 );
  178. vh = (uint64_t) hi << 32 | lo;
  179. GET_UINT32_BE( hi, h, 8 );
  180. GET_UINT32_BE( lo, h, 12 );
  181. vl = (uint64_t) hi << 32 | lo;
  182. ctx->HL[8] = vl; // 8 = 1000 corresponds to 1 in GF(2^128)
  183. ctx->HH[8] = vh;
  184. ctx->HH[0] = 0; // 0 corresponds to 0 in GF(2^128)
  185. ctx->HL[0] = 0;
  186. for( i = 4; i > 0; i >>= 1 ) {
  187. uint32_t T = (uint32_t) ( vl & 1 ) * 0xe1000000U;
  188. vl = ( vh << 63 ) | ( vl >> 1 );
  189. vh = ( vh >> 1 ) ^ ( (uint64_t) T << 32);
  190. ctx->HL[i] = vl;
  191. ctx->HH[i] = vh;
  192. }
  193. for (i = 2; i < 16; i <<= 1 ) {
  194. uint64_t *HiL = ctx->HL + i, *HiH = ctx->HH + i;
  195. vh = *HiH;
  196. vl = *HiL;
  197. for( j = 1; j < i; j++ ) {
  198. HiH[j] = vh ^ ctx->HH[j];
  199. HiL[j] = vl ^ ctx->HL[j];
  200. }
  201. }
  202. return( 0 );
  203. }
  204. /******************************************************************************
  205. *
  206. * GCM processing occurs four phases: SETKEY, START, UPDATE and FINISH.
  207. *
  208. * SETKEY:
  209. *
  210. * START: Sets the Encryption/Decryption mode.
  211. * Accepts the initialization vector and additional data.
  212. *
  213. * UPDATE: Encrypts or decrypts the plaintext or ciphertext.
  214. *
  215. * FINISH: Performs a final GHASH to generate the authentication tag.
  216. *
  217. ******************************************************************************
  218. *
  219. * GCM_START
  220. *
  221. * Given a user-provided GCM context, this initializes it, sets the encryption
  222. * mode, and preprocesses the initialization vector and additional AEAD data.
  223. *
  224. ******************************************************************************/
  225. int gcm_start( gcm_context *ctx, // pointer to user-provided GCM context
  226. int mode, // GCM_ENCRYPT or GCM_DECRYPT
  227. const uchar *iv, // pointer to initialization vector
  228. size_t iv_len, // IV length in bytes (should == 12)
  229. const uchar *add, // ptr to additional AEAD data (NULL if none)
  230. size_t add_len ) // length of additional AEAD data (bytes)
  231. {
  232. int ret; // our error return if the AES encrypt fails
  233. uchar work_buf[16]; // XOR source built from provided IV if len != 16
  234. const uchar *p; // general purpose array pointer
  235. size_t use_len; // byte count to process, up to 16 bytes
  236. size_t i; // local loop iterator
  237. // since the context might be reused under the same key
  238. // we zero the working buffers for this next new process
  239. memset( ctx->y, 0x00, sizeof(ctx->y ) );
  240. memset( ctx->buf, 0x00, sizeof(ctx->buf) );
  241. ctx->len = 0;
  242. ctx->add_len = 0;
  243. ctx->mode = mode; // set the GCM encryption/decryption mode
  244. ctx->aes_ctx.mode = ENCRYPT; // GCM *always* runs AES in ENCRYPTION mode
  245. if( iv_len == 12 ) { // GCM natively uses a 12-byte, 96-bit IV
  246. memcpy( ctx->y, iv, iv_len ); // copy the IV to the top of the 'y' buff
  247. ctx->y[15] = 1; // start "counting" from 1 (not 0)
  248. }
  249. else // if we don't have a 12-byte IV, we GHASH whatever we've been given
  250. {
  251. memset( work_buf, 0x00, 16 ); // clear the working buffer
  252. PUT_UINT32_BE( iv_len * 8, work_buf, 12 ); // place the IV into buffer
  253. p = iv;
  254. while( iv_len > 0 ) {
  255. use_len = ( iv_len < 16 ) ? iv_len : 16;
  256. for( i = 0; i < use_len; i++ ) ctx->y[i] ^= p[i];
  257. gcm_mult( ctx, ctx->y, ctx->y );
  258. iv_len -= use_len;
  259. p += use_len;
  260. }
  261. for( i = 0; i < 16; i++ ) ctx->y[i] ^= work_buf[i];
  262. gcm_mult( ctx, ctx->y, ctx->y );
  263. }
  264. if( ( ret = aes_cipher( &ctx->aes_ctx, ctx->y, ctx->base_ectr ) ) != 0 )
  265. return( ret );
  266. ctx->add_len = add_len;
  267. p = add;
  268. while( add_len > 0 ) {
  269. use_len = ( add_len < 16 ) ? add_len : 16;
  270. for( i = 0; i < use_len; i++ ) ctx->buf[i] ^= p[i];
  271. gcm_mult( ctx, ctx->buf, ctx->buf );
  272. add_len -= use_len;
  273. p += use_len;
  274. }
  275. return( 0 );
  276. }
  277. /******************************************************************************
  278. *
  279. * GCM_UPDATE
  280. *
  281. * This is called once or more to process bulk plaintext or ciphertext data.
  282. * We give this some number of bytes of input and it returns the same number
  283. * of output bytes. If called multiple times (which is fine) all but the final
  284. * invocation MUST be called with length mod 16 == 0. (Only the final call can
  285. * have a partial block length of < 128 bits.)
  286. *
  287. ******************************************************************************/
  288. int gcm_update( gcm_context *ctx, // pointer to user-provided GCM context
  289. size_t length, // length, in bytes, of data to process
  290. const uchar *input, // pointer to source data
  291. uchar *output ) // pointer to destination data
  292. {
  293. int ret; // our error return if the AES encrypt fails
  294. uchar ectr[16]; // counter-mode cipher output for XORing
  295. size_t use_len; // byte count to process, up to 16 bytes
  296. size_t i; // local loop iterator
  297. ctx->len += length; // bump the GCM context's running length count
  298. while( length > 0 ) {
  299. // clamp the length to process at 16 bytes
  300. use_len = ( length < 16 ) ? length : 16;
  301. // increment the context's 128-bit IV||Counter 'y' vector
  302. for( i = 16; i > 12; i-- ) if( ++ctx->y[i - 1] != 0 ) break;
  303. // encrypt the context's 'y' vector under the established key
  304. if( ( ret = aes_cipher( &ctx->aes_ctx, ctx->y, ectr ) ) != 0 )
  305. return( ret );
  306. // encrypt or decrypt the input to the output
  307. if( ctx->mode == ENCRYPT )
  308. {
  309. for( i = 0; i < use_len; i++ ) {
  310. // XOR the cipher's ouptut vector (ectr) with our input
  311. output[i] = (uchar) ( ectr[i] ^ input[i] );
  312. // now we mix in our data into the authentication hash.
  313. // if we're ENcrypting we XOR in the post-XOR (output)
  314. // results, but if we're DEcrypting we XOR in the input
  315. // data
  316. ctx->buf[i] ^= output[i];
  317. }
  318. }
  319. else
  320. {
  321. for( i = 0; i < use_len; i++ ) {
  322. // but if we're DEcrypting we XOR in the input data first,
  323. // i.e. before saving to ouput data, otherwise if the input
  324. // and output buffer are the same (inplace decryption) we
  325. // would not get the correct auth tag
  326. ctx->buf[i] ^= input[i];
  327. // XOR the cipher's ouptut vector (ectr) with our input
  328. output[i] = (uchar) ( ectr[i] ^ input[i] );
  329. }
  330. }
  331. gcm_mult( ctx, ctx->buf, ctx->buf ); // perform a GHASH operation
  332. length -= use_len; // drop the remaining byte count to process
  333. input += use_len; // bump our input pointer forward
  334. output += use_len; // bump our output pointer forward
  335. }
  336. return( 0 );
  337. }
  338. /******************************************************************************
  339. *
  340. * GCM_FINISH
  341. *
  342. * This is called once after all calls to GCM_UPDATE to finalize the GCM.
  343. * It performs the final GHASH to produce the resulting authentication TAG.
  344. *
  345. ******************************************************************************/
  346. int gcm_finish( gcm_context *ctx, // pointer to user-provided GCM context
  347. uchar *tag, // pointer to buffer which receives the tag
  348. size_t tag_len ) // length, in bytes, of the tag-receiving buf
  349. {
  350. uchar work_buf[16];
  351. uint64_t orig_len = ctx->len * 8;
  352. uint64_t orig_add_len = ctx->add_len * 8;
  353. size_t i;
  354. if( tag_len != 0 ) memcpy( tag, ctx->base_ectr, tag_len );
  355. if( orig_len || orig_add_len ) {
  356. memset( work_buf, 0x00, 16 );
  357. PUT_UINT32_BE( ( orig_add_len >> 32 ), work_buf, 0 );
  358. PUT_UINT32_BE( ( orig_add_len ), work_buf, 4 );
  359. PUT_UINT32_BE( ( orig_len >> 32 ), work_buf, 8 );
  360. PUT_UINT32_BE( ( orig_len ), work_buf, 12 );
  361. for( i = 0; i < 16; i++ ) ctx->buf[i] ^= work_buf[i];
  362. gcm_mult( ctx, ctx->buf, ctx->buf );
  363. for( i = 0; i < tag_len; i++ ) tag[i] ^= ctx->buf[i];
  364. }
  365. return( 0 );
  366. }
  367. /******************************************************************************
  368. *
  369. * GCM_CRYPT_AND_TAG
  370. *
  371. * This either encrypts or decrypts the user-provided data and, either
  372. * way, generates an authentication tag of the requested length. It must be
  373. * called with a GCM context whose key has already been set with GCM_SETKEY.
  374. *
  375. * The user would typically call this explicitly to ENCRYPT a buffer of data
  376. * and optional associated data, and produce its an authentication tag.
  377. *
  378. * To reverse the process the user would typically call the companion
  379. * GCM_AUTH_DECRYPT function to decrypt data and verify a user-provided
  380. * authentication tag. The GCM_AUTH_DECRYPT function calls this function
  381. * to perform its decryption and tag generation, which it then compares.
  382. *
  383. ******************************************************************************/
  384. int gcm_crypt_and_tag(
  385. gcm_context *ctx, // gcm context with key already setup
  386. int mode, // cipher direction: GCM_ENCRYPT or GCM_DECRYPT
  387. const uchar *iv, // pointer to the 12-byte initialization vector
  388. size_t iv_len, // byte length if the IV. should always be 12
  389. const uchar *add, // pointer to the non-ciphered additional data
  390. size_t add_len, // byte length of the additional AEAD data
  391. const uchar *input, // pointer to the cipher data source
  392. uchar *output, // pointer to the cipher data destination
  393. size_t length, // byte length of the cipher data
  394. uchar *tag, // pointer to the tag to be generated
  395. size_t tag_len ) // byte length of the tag to be generated
  396. { /*
  397. assuming that the caller has already invoked gcm_setkey to
  398. prepare the gcm context with the keying material, we simply
  399. invoke each of the three GCM sub-functions in turn...
  400. */
  401. gcm_start ( ctx, mode, iv, iv_len, add, add_len );
  402. gcm_update ( ctx, length, input, output );
  403. gcm_finish ( ctx, tag, tag_len );
  404. return( 0 );
  405. }
  406. /******************************************************************************
  407. *
  408. * GCM_AUTH_DECRYPT
  409. *
  410. * This DECRYPTS a user-provided data buffer with optional associated data.
  411. * It then verifies a user-supplied authentication tag against the tag just
  412. * re-created during decryption to verify that the data has not been altered.
  413. *
  414. * This function calls GCM_CRYPT_AND_TAG (above) to perform the decryption
  415. * and authentication tag generation.
  416. *
  417. ******************************************************************************/
  418. int gcm_auth_decrypt(
  419. gcm_context *ctx, // gcm context with key already setup
  420. const uchar *iv, // pointer to the 12-byte initialization vector
  421. size_t iv_len, // byte length if the IV. should always be 12
  422. const uchar *add, // pointer to the non-ciphered additional data
  423. size_t add_len, // byte length of the additional AEAD data
  424. const uchar *input, // pointer to the cipher data source
  425. uchar *output, // pointer to the cipher data destination
  426. size_t length, // byte length of the cipher data
  427. const uchar *tag, // pointer to the tag to be authenticated
  428. size_t tag_len ) // byte length of the tag <= 16
  429. {
  430. uchar check_tag[16]; // the tag generated and returned by decryption
  431. int diff; // an ORed flag to detect authentication errors
  432. size_t i; // our local iterator
  433. /*
  434. we use GCM_DECRYPT_AND_TAG (above) to perform our decryption
  435. (which is an identical XORing to reverse the previous one)
  436. and also to re-generate the matching authentication tag
  437. */
  438. gcm_crypt_and_tag( ctx, DECRYPT, iv, iv_len, add, add_len,
  439. input, output, length, check_tag, tag_len );
  440. // now we verify the authentication tag in 'constant time'
  441. for( diff = 0, i = 0; i < tag_len; i++ )
  442. diff |= tag[i] ^ check_tag[i];
  443. if( diff != 0 ) { // see whether any bits differed?
  444. memset( output, 0, length ); // if so... wipe the output data
  445. return( GCM_AUTH_FAILURE ); // return GCM_AUTH_FAILURE
  446. }
  447. return( 0 );
  448. }
  449. /******************************************************************************
  450. *
  451. * GCM_ZERO_CTX
  452. *
  453. * The GCM context contains both the GCM context and the AES context.
  454. * This includes keying and key-related material which is security-
  455. * sensitive, so it MUST be zeroed after use. This function does that.
  456. *
  457. ******************************************************************************/
  458. void gcm_zero_ctx( gcm_context *ctx )
  459. {
  460. // zero the context originally provided to us
  461. memset( ctx, 0, sizeof( gcm_context ) );
  462. }