1 /* LibTomCrypt, modular cryptographic library -- Tom St Denis
3 * LibTomCrypt is a library that provides various cryptographic
4 * algorithms in a highly modular and flexible manner.
6 * The library is free for all purposes without any express
12 GCM implementation, process a packet, by Tom St Denis
19 Process an entire GCM packet in one call.
20 @param cipher Index of cipher to use
21 @param key The secret key
22 @param keylen The length of the secret key
23 @param IV The initialization vector
24 @param IVlen The length of the initialization vector
25 @param adata The additional authentication data (header)
26 @param adatalen The length of the adata
27 @param pt The plaintext
28 @param ptlen The length of the plaintext (ciphertext length is the same)
29 @param ct The ciphertext
30 @param tag [out] The MAC tag
31 @param taglen [in/out] The MAC tag length
32 @param direction Encrypt or Decrypt mode (GCM_ENCRYPT or GCM_DECRYPT)
33 @return CRYPT_OK on success
35 int gcm_memory( int cipher,
36 const unsigned char *key, unsigned long keylen,
37 const unsigned char *IV, unsigned long IVlen,
38 const unsigned char *adata, unsigned long adatalen,
39 unsigned char *pt, unsigned long ptlen,
41 unsigned char *tag, unsigned long *taglen,
48 if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {
52 if (cipher_descriptor[cipher].accel_gcm_memory != NULL) {
53 return cipher_descriptor[cipher].accel_gcm_memory
65 #ifndef LTC_GCM_TABLES_SSE2
66 orig = gcm = XMALLOC(sizeof(*gcm));
68 orig = gcm = XMALLOC(sizeof(*gcm) + 16);
74 /* Force GCM to be on a multiple of 16 so we can use 128-bit aligned operations
75 * note that we only modify gcm and keep orig intact. This code is not portable
76 * but again it's only for SSE2 anyways, so who cares?
78 #ifdef LTC_GCM_TABLES_SSE2
79 if ((unsigned long)gcm & 15) {
80 gcm = (gcm_state *)((unsigned long)gcm + (16 - ((unsigned long)gcm & 15)));
84 if ((err = gcm_init(gcm, cipher, key, keylen)) != CRYPT_OK) {
87 if ((err = gcm_add_iv(gcm, IV, IVlen)) != CRYPT_OK) {
90 if ((err = gcm_add_aad(gcm, adata, adatalen)) != CRYPT_OK) {
93 if ((err = gcm_process(gcm, pt, ptlen, ct, direction)) != CRYPT_OK) {
96 err = gcm_done(gcm, tag, taglen);
104 /* ref: $Format:%D$ */
105 /* git commit: $Format:%H$ */
106 /* commit time: $Format:%ai$ */