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, Add AAD data to the stream, by Tom St Denis
19 Add AAD to the GCM state
20 @param gcm The GCM state
21 @param adata The additional authentication data to add to the GCM state
22 @param adatalen The length of the AAD data.
23 @return CRYPT_OK on success
25 int gcm_add_aad(gcm_state *gcm,
26 const unsigned char *adata, unsigned long adatalen)
34 LTC_ARGCHK(gcm != NULL);
36 LTC_ARGCHK(adata != NULL);
39 if (gcm->buflen > 16 || gcm->buflen < 0) {
40 return CRYPT_INVALID_ARG;
43 if ((err = cipher_is_valid(gcm->cipher)) != CRYPT_OK) {
48 if (gcm->mode == LTC_GCM_MODE_IV) {
49 /* IV length must be > 0 */
50 if (gcm->buflen == 0 && gcm->totlen == 0) return CRYPT_ERROR;
51 /* let's process the IV */
52 if (gcm->ivmode || gcm->buflen != 12) {
53 for (x = 0; x < (unsigned long)gcm->buflen; x++) {
54 gcm->X[x] ^= gcm->buf[x];
57 gcm->totlen += gcm->buflen * CONST64(8);
58 gcm_mult_h(gcm, gcm->X);
61 /* mix in the length */
63 STORE64H(gcm->totlen, gcm->buf+8);
64 for (x = 0; x < 16; x++) {
65 gcm->X[x] ^= gcm->buf[x];
67 gcm_mult_h(gcm, gcm->X);
69 /* copy counter out */
70 XMEMCPY(gcm->Y, gcm->X, 16);
73 XMEMCPY(gcm->Y, gcm->buf, 12);
79 XMEMCPY(gcm->Y_0, gcm->Y, 16);
80 zeromem(gcm->buf, 16);
83 gcm->mode = LTC_GCM_MODE_AAD;
86 if (gcm->mode != LTC_GCM_MODE_AAD || gcm->buflen >= 16) {
87 return CRYPT_INVALID_ARG;
92 if (gcm->buflen == 0) {
93 for (x = 0; x < (adatalen & ~15); x += 16) {
94 for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {
95 *(LTC_FAST_TYPE_PTR_CAST(&gcm->X[y])) ^= *(LTC_FAST_TYPE_PTR_CAST(&adata[x + y]));
97 gcm_mult_h(gcm, gcm->X);
105 /* start adding AAD data to the state */
106 for (; x < adatalen; x++) {
107 gcm->X[gcm->buflen++] ^= *adata++;
109 if (gcm->buflen == 16) {
111 gcm_mult_h(gcm, gcm->X);
122 /* ref: $Format:%D$ */
123 /* git commit: $Format:%H$ */
124 /* commit time: $Format:%ai$ */