Path: blob/master/libs/tomcrypt/src/encauth/gcm/gcm_process.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_process.c11GCM implementation, process message data, by Tom St Denis12*/13#include "tomcrypt.h"1415#ifdef LTC_GCM_MODE1617/**18Process plaintext/ciphertext through GCM19@param gcm The GCM state20@param pt The plaintext21@param ptlen The plaintext length (ciphertext length is the same)22@param ct The ciphertext23@param direction Encrypt or Decrypt mode (GCM_ENCRYPT or GCM_DECRYPT)24@return CRYPT_OK on success25*/26int gcm_process(gcm_state *gcm,27unsigned char *pt, unsigned long ptlen,28unsigned char *ct,29int direction)30{31unsigned long x;32int y, err;33unsigned char b;3435LTC_ARGCHK(gcm != NULL);36if (ptlen > 0) {37LTC_ARGCHK(pt != NULL);38LTC_ARGCHK(ct != NULL);39}4041if (gcm->buflen > 16 || gcm->buflen < 0) {42return CRYPT_INVALID_ARG;43}4445if ((err = cipher_is_valid(gcm->cipher)) != CRYPT_OK) {46return err;47}4849/* 0xFFFFFFFE0 = ((2^39)-256)/8 */50if (gcm->pttotlen / 8 + (ulong64)gcm->buflen + (ulong64)ptlen >= CONST64(0xFFFFFFFE0)) {51return CRYPT_INVALID_ARG;52}5354if (gcm->mode == LTC_GCM_MODE_IV) {55/* let's process the IV */56if ((err = gcm_add_aad(gcm, NULL, 0)) != CRYPT_OK) return err;57}5859/* in AAD mode? */60if (gcm->mode == LTC_GCM_MODE_AAD) {61/* let's process the AAD */62if (gcm->buflen) {63gcm->totlen += gcm->buflen * CONST64(8);64gcm_mult_h(gcm, gcm->X);65}6667/* increment counter */68for (y = 15; y >= 12; y--) {69if (++gcm->Y[y] & 255) { break; }70}71/* encrypt the counter */72if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {73return err;74}7576gcm->buflen = 0;77gcm->mode = LTC_GCM_MODE_TEXT;78}7980if (gcm->mode != LTC_GCM_MODE_TEXT) {81return CRYPT_INVALID_ARG;82}8384x = 0;85#ifdef LTC_FAST86if (gcm->buflen == 0) {87if (direction == GCM_ENCRYPT) {88for (x = 0; x < (ptlen & ~15); x += 16) {89/* ctr encrypt */90for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {91*(LTC_FAST_TYPE_PTR_CAST(&ct[x + y])) = *(LTC_FAST_TYPE_PTR_CAST(&pt[x+y])) ^ *(LTC_FAST_TYPE_PTR_CAST(&gcm->buf[y]));92*(LTC_FAST_TYPE_PTR_CAST(&gcm->X[y])) ^= *(LTC_FAST_TYPE_PTR_CAST(&ct[x+y]));93}94/* GMAC it */95gcm->pttotlen += 128;96gcm_mult_h(gcm, gcm->X);97/* increment counter */98for (y = 15; y >= 12; y--) {99if (++gcm->Y[y] & 255) { break; }100}101if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {102return err;103}104}105} else {106for (x = 0; x < (ptlen & ~15); x += 16) {107/* ctr encrypt */108for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {109*(LTC_FAST_TYPE_PTR_CAST(&gcm->X[y])) ^= *(LTC_FAST_TYPE_PTR_CAST(&ct[x+y]));110*(LTC_FAST_TYPE_PTR_CAST(&pt[x + y])) = *(LTC_FAST_TYPE_PTR_CAST(&ct[x+y])) ^ *(LTC_FAST_TYPE_PTR_CAST(&gcm->buf[y]));111}112/* GMAC it */113gcm->pttotlen += 128;114gcm_mult_h(gcm, gcm->X);115/* increment counter */116for (y = 15; y >= 12; y--) {117if (++gcm->Y[y] & 255) { break; }118}119if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {120return err;121}122}123}124}125#endif126127/* process text */128for (; x < ptlen; x++) {129if (gcm->buflen == 16) {130gcm->pttotlen += 128;131gcm_mult_h(gcm, gcm->X);132133/* increment counter */134for (y = 15; y >= 12; y--) {135if (++gcm->Y[y] & 255) { break; }136}137if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y, gcm->buf, &gcm->K)) != CRYPT_OK) {138return err;139}140gcm->buflen = 0;141}142143if (direction == GCM_ENCRYPT) {144b = ct[x] = pt[x] ^ gcm->buf[gcm->buflen];145} else {146b = ct[x];147pt[x] = ct[x] ^ gcm->buf[gcm->buflen];148}149gcm->X[gcm->buflen++] ^= b;150}151152return CRYPT_OK;153}154155#endif156157158