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 IV data to the state, by Tom St Denis
19 Add IV data to the GCM state
20 @param gcm The GCM state
21 @param IV The initial value data to add
22 @param IVlen The length of the IV
23 @return CRYPT_OK on success
25 int gcm_add_iv(gcm_state *gcm,
26 const unsigned char *IV, unsigned long IVlen)
31 LTC_ARGCHK(gcm != NULL);
33 LTC_ARGCHK(IV != NULL);
36 /* must be in IV mode */
37 if (gcm->mode != LTC_GCM_MODE_IV) {
38 return CRYPT_INVALID_ARG;
41 if (gcm->buflen >= 16 || gcm->buflen < 0) {
42 return CRYPT_INVALID_ARG;
45 if ((err = cipher_is_valid(gcm->cipher)) != CRYPT_OK) {
50 /* trip the ivmode flag */
51 if (IVlen + gcm->buflen > 12) {
57 if (gcm->buflen == 0) {
58 for (x = 0; x < (IVlen & ~15); x += 16) {
59 for (y = 0; y < 16; y += sizeof(LTC_FAST_TYPE)) {
60 *(LTC_FAST_TYPE_PTR_CAST(&gcm->X[y])) ^= *(LTC_FAST_TYPE_PTR_CAST(&IV[x + y]));
62 gcm_mult_h(gcm, gcm->X);
69 /* start adding IV data to the state */
70 for (; x < IVlen; x++) {
71 gcm->buf[gcm->buflen++] = *IV++;
73 if (gcm->buflen == 16) {
75 for (y = 0; y < 16; y++) {
76 gcm->X[y] ^= gcm->buf[y];
78 gcm_mult_h(gcm, gcm->X);
90 /* ref: $Format:%D$ */
91 /* git commit: $Format:%H$ */
92 /* commit time: $Format:%ai$ */