Path: blob/master/libs/tomcrypt/src/encauth/gcm/gcm_add_iv.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_add_iv.c11GCM implementation, add IV data to the state, by Tom St Denis12*/13#include "tomcrypt.h"1415#ifdef LTC_GCM_MODE1617/**18Add IV data to the GCM state19@param gcm The GCM state20@param IV The initial value data to add21@param IVlen The length of the IV22@return CRYPT_OK on success23*/24int gcm_add_iv(gcm_state *gcm,25const unsigned char *IV, unsigned long IVlen)26{27unsigned long x, y;28int err;2930LTC_ARGCHK(gcm != NULL);31if (IVlen > 0) {32LTC_ARGCHK(IV != NULL);33}3435/* must be in IV mode */36if (gcm->mode != LTC_GCM_MODE_IV) {37return CRYPT_INVALID_ARG;38}3940if (gcm->buflen >= 16 || gcm->buflen < 0) {41return CRYPT_INVALID_ARG;42}4344if ((err = cipher_is_valid(gcm->cipher)) != CRYPT_OK) {45return err;46}474849/* trip the ivmode flag */50if (IVlen + gcm->buflen > 12) {51gcm->ivmode |= 1;52}5354x = 0;55#ifdef LTC_FAST56if (gcm->buflen == 0) {57for (x = 0; x < (IVlen & ~15); x += 16) {58for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {59*(LTC_FAST_TYPE_PTR_CAST(&gcm->X[y])) ^= *(LTC_FAST_TYPE_PTR_CAST(&IV[x + y]));60}61gcm_mult_h(gcm, gcm->X);62gcm->totlen += 128;63}64IV += x;65}66#endif6768/* start adding IV data to the state */69for (; x < IVlen; x++) {70gcm->buf[gcm->buflen++] = *IV++;7172if (gcm->buflen == 16) {73/* GF mult it */74for (y = 0; y < 16; y++) {75gcm->X[y] ^= gcm->buf[y];76}77gcm_mult_h(gcm, gcm->X);78gcm->buflen = 0;79gcm->totlen += 128;80}81}8283return CRYPT_OK;84}8586#endif878889