Path: blob/master/libs/tomcrypt/src/encauth/gcm/gcm_memory.c
5972 views
/* LibTomCrypt, modular cryptographic library -- Tom St Denis1*2* LibTomCrypt is a library that provides various cryptographic3* algorithms in a highly modular and flexible manner.4*5* The library is free for all purposes without any express6* guarantee it works.7*/89/**10@file gcm_memory.c11GCM implementation, process a packet, by Tom St Denis12*/13#include "tomcrypt.h"1415#ifdef LTC_GCM_MODE1617/**18Process an entire GCM packet in one call.19@param cipher Index of cipher to use20@param key The secret key21@param keylen The length of the secret key22@param IV The initialization vector23@param IVlen The length of the initialization vector24@param adata The additional authentication data (header)25@param adatalen The length of the adata26@param pt The plaintext27@param ptlen The length of the plaintext (ciphertext length is the same)28@param ct The ciphertext29@param tag [out] The MAC tag30@param taglen [in/out] The MAC tag length31@param direction Encrypt or Decrypt mode (GCM_ENCRYPT or GCM_DECRYPT)32@return CRYPT_OK on success33*/34int gcm_memory( int cipher,35const unsigned char *key, unsigned long keylen,36const unsigned char *IV, unsigned long IVlen,37const unsigned char *adata, unsigned long adatalen,38unsigned char *pt, unsigned long ptlen,39unsigned char *ct,40unsigned char *tag, unsigned long *taglen,41int direction)42{43void *orig;44gcm_state *gcm;45int err;4647if ((err = cipher_is_valid(cipher)) != CRYPT_OK) {48return err;49}5051if (cipher_descriptor[cipher].accel_gcm_memory != NULL) {52return cipher_descriptor[cipher].accel_gcm_memory53(key, keylen,54IV, IVlen,55adata, adatalen,56pt, ptlen,57ct,58tag, taglen,59direction);60}61626364#ifndef LTC_GCM_TABLES_SSE265orig = gcm = XMALLOC(sizeof(*gcm));66#else67orig = gcm = XMALLOC(sizeof(*gcm) + 16);68#endif69if (gcm == NULL) {70return CRYPT_MEM;71}7273/* Force GCM to be on a multiple of 16 so we can use 128-bit aligned operations74* note that we only modify gcm and keep orig intact. This code is not portable75* but again it's only for SSE2 anyways, so who cares?76*/77#ifdef LTC_GCM_TABLES_SSE278if ((unsigned long)gcm & 15) {79gcm = (gcm_state *)((unsigned long)gcm + (16 - ((unsigned long)gcm & 15)));80}81#endif8283if ((err = gcm_init(gcm, cipher, key, keylen)) != CRYPT_OK) {84goto LTC_ERR;85}86if ((err = gcm_add_iv(gcm, IV, IVlen)) != CRYPT_OK) {87goto LTC_ERR;88}89if ((err = gcm_add_aad(gcm, adata, adatalen)) != CRYPT_OK) {90goto LTC_ERR;91}92if ((err = gcm_process(gcm, pt, ptlen, ct, direction)) != CRYPT_OK) {93goto LTC_ERR;94}95err = gcm_done(gcm, tag, taglen);96LTC_ERR:97XFREE(orig);98return err;99}100#endif101102103