Path: blob/master/libs/tomcrypt/src/encauth/gcm/gcm_done.c
8799 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_done.c11GCM implementation, Terminate the stream, by Tom St Denis12*/13#include "tomcrypt.h"1415#ifdef LTC_GCM_MODE1617/**18Terminate a GCM stream19@param gcm The GCM state20@param tag [out] The destination for the MAC tag21@param taglen [in/out] The length of the MAC tag22@return CRYPT_OK on success23*/24int gcm_done(gcm_state *gcm,25unsigned char *tag, unsigned long *taglen)26{27unsigned long x;28int err;2930LTC_ARGCHK(gcm != NULL);31LTC_ARGCHK(tag != NULL);32LTC_ARGCHK(taglen != NULL);3334if (gcm->buflen > 16 || gcm->buflen < 0) {35return CRYPT_INVALID_ARG;36}3738if ((err = cipher_is_valid(gcm->cipher)) != CRYPT_OK) {39return err;40}4142if (gcm->mode == LTC_GCM_MODE_IV) {43/* let's process the IV */44if ((err = gcm_add_aad(gcm, NULL, 0)) != CRYPT_OK) return err;45}4647if (gcm->mode == LTC_GCM_MODE_AAD) {48/* let's process the AAD */49if ((err = gcm_process(gcm, NULL, 0, NULL, 0)) != CRYPT_OK) return err;50}5152if (gcm->mode != LTC_GCM_MODE_TEXT) {53return CRYPT_INVALID_ARG;54}5556/* handle remaining ciphertext */57if (gcm->buflen) {58gcm->pttotlen += gcm->buflen * CONST64(8);59gcm_mult_h(gcm, gcm->X);60}6162/* length */63STORE64H(gcm->totlen, gcm->buf);64STORE64H(gcm->pttotlen, gcm->buf+8);65for (x = 0; x < 16; x++) {66gcm->X[x] ^= gcm->buf[x];67}68gcm_mult_h(gcm, gcm->X);6970/* encrypt original counter */71if ((err = cipher_descriptor[gcm->cipher].ecb_encrypt(gcm->Y_0, gcm->buf, &gcm->K)) != CRYPT_OK) {72return err;73}74for (x = 0; x < 16 && x < *taglen; x++) {75tag[x] = gcm->buf[x] ^ gcm->X[x];76}77*taglen = x;7879cipher_descriptor[gcm->cipher].done(&gcm->K);8081return CRYPT_OK;82}8384#endif858687