gcm128.c: add boundary condition checks.
[openssl.git] / crypto / modes / gcm128.c
index e4df61f8b6a24cc945a5057e7970e7cc34f1fddf..3f6b70df4b3607658408353324e3076a9d018732 100644 (file)
@@ -580,7 +580,7 @@ void gcm_ghash_4bit(u64 Xi[2],const u128 Htable[16],const u8 *inp,size_t len);
 /* GHASH_CHUNK is "stride parameter" missioned to mitigate cache
  * trashing effect. In other words idea is to hash data while it's
  * still in L1 cache after encryption pass... */
-#define GHASH_CHUNK       1024
+#define GHASH_CHUNK       (3*1024)
 #endif
 
 #else  /* TABLE_BITS */
@@ -657,7 +657,7 @@ struct gcm128_context {
        void (*gmult)(u64 Xi[2],const u128 Htable[16]);
        void (*ghash)(u64 Xi[2],const u128 Htable[16],const u8 *inp,size_t len);
 #endif
-       unsigned int res, pad;
+       unsigned int mres, ares;
        block128_f block;
        void *key;
 };
@@ -751,9 +751,10 @@ void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx,const unsigned char *iv,size_t len)
        ctx->Yi.u[1]  = 0;
        ctx->Xi.u[0]  = 0;
        ctx->Xi.u[1]  = 0;
-       ctx->len.u[0] = 0;
-       ctx->len.u[1] = 0;
-       ctx->res = 0;
+       ctx->len.u[0] = 0;      /* AAD length */
+       ctx->len.u[1] = 0;      /* message length */
+       ctx->ares = 0;
+       ctx->mres = 0;
 
        if (len==12) {
                memcpy(ctx->Yi.c,iv,12);
@@ -808,11 +809,32 @@ void CRYPTO_gcm128_setiv(GCM128_CONTEXT *ctx,const unsigned char *iv,size_t len)
                ctx->Yi.d[3] = ctr;
 }
 
-void CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx,const unsigned char *aad,size_t len)
+int CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx,const unsigned char *aad,size_t len)
 {
        size_t i;
+       unsigned int n;
+       u64 alen = ctx->len.u[0];
 
-       ctx->len.u[0] += len;
+       if (ctx->len.u[1]) return -2;
+
+       alen += len;
+       if (alen>(U64(1)<<61) || (sizeof(len)==8 && alen<len))
+               return -1;
+       ctx->len.u[0] = alen;
+
+       n = ctx->ares;
+       if (n) {
+               while (n && len) {
+                       ctx->Xi.c[n] ^= *(aad++);
+                       --len;
+                       n = (n+1)%16;
+               }
+               if (n==0) GCM_MUL(ctx,Xi);
+               else {
+                       ctx->ares = n;
+                       return 0;
+               }
+       }
 
 #ifdef GHASH
        if ((i = (len&(size_t)-16))) {
@@ -829,26 +851,43 @@ void CRYPTO_gcm128_aad(GCM128_CONTEXT *ctx,const unsigned char *aad,size_t len)
        }
 #endif
        if (len) {
+               n = (unsigned int)len;
                for (i=0; i<len; ++i) ctx->Xi.c[i] ^= aad[i];
-               GCM_MUL(ctx,Xi);
        }
+
+       ctx->ares = n;
+       return 0;
 }
 
-void CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
+int CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
                const unsigned char *in, unsigned char *out,
                size_t len)
 {
        const union { long one; char little; } is_endian = {1};
        unsigned int n, ctr;
        size_t i;
+       u64 mlen = ctx->len.u[1];
+
+#if 0
+       n = (unsigned int)mlen%16; /* alternative to ctx->mres */
+#endif
+       mlen += len;
+       if (mlen>((U64(1)<<36)-32) || (sizeof(len)==8 && mlen<len))
+               return -1;
+       ctx->len.u[1] = mlen;
+
+       if (ctx->ares) {
+               /* First call to encrypt finalizes GHASH(AAD) */
+               GCM_MUL(ctx,Xi);
+               ctx->ares = 0;
+       }
 
-       ctx->len.u[1] += len;
-       n   = ctx->res;
        if (is_endian.little)
                ctr = GETU32(ctx->Yi.c+12);
        else
                ctr = ctx->Yi.d[3];
 
+       n = ctx->mres;
 #if !defined(OPENSSL_SMALL_FOOTPRINT)
        if (16%sizeof(size_t) == 0) do {        /* always true actually */
                if (n) {
@@ -859,8 +898,8 @@ void CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
                        }
                        if (n==0) GCM_MUL(ctx,Xi);
                        else {
-                               ctx->res = n;
-                               return;
+                               ctx->mres = n;
+                               return 0;
                        }
                }
 #if defined(STRICT_ALIGNMENT)
@@ -938,8 +977,8 @@ void CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
                        }
                }
 
-               ctx->res = n;
-               return;
+               ctx->mres = n;
+               return 0;
        } while(0);
 #endif
        for (i=0;i<len;++i) {
@@ -957,24 +996,36 @@ void CRYPTO_gcm128_encrypt(GCM128_CONTEXT *ctx,
                        GCM_MUL(ctx,Xi);
        }
 
-       ctx->res = n;
+       ctx->mres = n;
+       return 0;
 }
 
-void CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
+int CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
                const unsigned char *in, unsigned char *out,
                size_t len)
 {
        const union { long one; char little; } is_endian = {1};
        unsigned int n, ctr;
        size_t i;
+       u64 mlen = ctx->len.u[1];
+
+       mlen += len;
+       if (mlen>((U64(1)<<36)-32) || (sizeof(len)==8 && mlen<len))
+               return -1;
+       ctx->len.u[1] = mlen;
+
+       if (ctx->ares) {
+               /* First call to decrypt finalizes GHASH(AAD) */
+               GCM_MUL(ctx,Xi);
+               ctx->ares = 0;
+       }
 
-       ctx->len.u[1] += len;
-       n   = ctx->res;
        if (is_endian.little)
                ctr = GETU32(ctx->Yi.c+12);
        else
                ctr = ctx->Yi.d[3];
 
+       n = ctx->mres;
 #if !defined(OPENSSL_SMALL_FOOTPRINT)
        if (16%sizeof(size_t) == 0) do {        /* always true actually */
                if (n) {
@@ -987,8 +1038,8 @@ void CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
                        }
                        if (n==0) GCM_MUL (ctx,Xi);
                        else {
-                               ctx->res = n;
-                               return;
+                               ctx->mres = n;
+                               return 0;
                        }
                }
 #if defined(STRICT_ALIGNMENT)
@@ -1067,8 +1118,8 @@ void CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
                        }
                }
 
-               ctx->res = n;
-               return;
+               ctx->mres = n;
+               return 0;
        } while(0);
 #endif
        for (i=0;i<len;++i) {
@@ -1082,14 +1133,204 @@ void CRYPTO_gcm128_decrypt(GCM128_CONTEXT *ctx,
                                ctx->Yi.d[3] = ctr;
                }
                c = in[i];
-               out[i] ^= ctx->EKi.c[n];
+               out[i] = c^ctx->EKi.c[n];
                ctx->Xi.c[n] ^= c;
                n = (n+1)%16;
                if (n==0)
                        GCM_MUL(ctx,Xi);
        }
 
-       ctx->res = n;
+       ctx->mres = n;
+       return 0;
+}
+
+int CRYPTO_gcm128_encrypt_ctr32(GCM128_CONTEXT *ctx,
+               const unsigned char *in, unsigned char *out,
+               size_t len, ctr128_f stream)
+{
+       const union { long one; char little; } is_endian = {1};
+       unsigned int n, ctr;
+       size_t i;
+       u64 mlen = ctx->len.u[1];
+
+       mlen += len;
+       if (mlen>((U64(1)<<36)-32) || (sizeof(len)==8 && mlen<len))
+               return -1;
+       ctx->len.u[1] = mlen;
+
+       if (ctx->ares) {
+               /* First call to encrypt finalizes GHASH(AAD) */
+               GCM_MUL(ctx,Xi);
+               ctx->ares = 0;
+       }
+
+       if (is_endian.little)
+               ctr = GETU32(ctx->Yi.c+12);
+       else
+               ctr = ctx->Yi.d[3];
+
+       n = ctx->mres;
+       if (n) {
+               while (n && len) {
+                       ctx->Xi.c[n] ^= *(out++) = *(in++)^ctx->EKi.c[n];
+                       --len;
+                       n = (n+1)%16;
+               }
+               if (n==0) GCM_MUL(ctx,Xi);
+               else {
+                       ctx->mres = n;
+                       return 0;
+               }
+       }
+#if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
+       while (len>=GHASH_CHUNK) {
+               (*stream)(in,out,GHASH_CHUNK/16,ctx->key,ctx->Yi.c);
+               ctr += GHASH_CHUNK/16;
+               if (is_endian.little)
+                       PUTU32(ctx->Yi.c+12,ctr);
+               else
+                       ctx->Yi.d[3] = ctr;
+               GHASH(ctx,out,GHASH_CHUNK);
+               out += GHASH_CHUNK;
+               in  += GHASH_CHUNK;
+               len -= GHASH_CHUNK;
+       }
+#endif
+       if ((i = (len&(size_t)-16))) {
+               size_t j=i/16;
+
+               (*stream)(in,out,j,ctx->key,ctx->Yi.c);
+               ctr += (unsigned int)j;
+               if (is_endian.little)
+                       PUTU32(ctx->Yi.c+12,ctr);
+               else
+                       ctx->Yi.d[3] = ctr;
+               in  += i;
+               len -= i;
+#if defined(GHASH)
+               GHASH(ctx,out,i);
+               out += i;
+#else
+               while (j--) {
+                       for (i=0;i<16;++i) ctx->Xi.c[i] ^= out[i];
+                       GCM_MUL(ctx,Xi);
+                       out += 16;
+               }
+#endif
+       }
+       if (len) {
+               (*ctx->block)(ctx->Yi.c,ctx->EKi.c,ctx->key);
+               ++ctr;
+               if (is_endian.little)
+                       PUTU32(ctx->Yi.c+12,ctr);
+               else
+                       ctx->Yi.d[3] = ctr;
+               while (len--) {
+                       ctx->Xi.c[n] ^= out[n] = in[n]^ctx->EKi.c[n];
+                       ++n;
+               }
+       }
+
+       ctx->mres = n;
+       return 0;
+}
+
+int CRYPTO_gcm128_decrypt_ctr32(GCM128_CONTEXT *ctx,
+               const unsigned char *in, unsigned char *out,
+               size_t len,ctr128_f stream)
+{
+       const union { long one; char little; } is_endian = {1};
+       unsigned int n, ctr;
+       size_t i;
+       u64 mlen = ctx->len.u[1];
+
+       mlen += len;
+       if (mlen>((U64(1)<<36)-32) || (sizeof(len)==8 && mlen<len))
+               return -1;
+       ctx->len.u[1] = mlen;
+
+       if (ctx->ares) {
+               /* First call to decrypt finalizes GHASH(AAD) */
+               GCM_MUL(ctx,Xi);
+               ctx->ares = 0;
+       }
+
+       if (is_endian.little)
+               ctr = GETU32(ctx->Yi.c+12);
+       else
+               ctr = ctx->Yi.d[3];
+
+       n = ctx->mres;
+       if (n) {
+               while (n && len) {
+                       u8 c = *(in++);
+                       *(out++) = c^ctx->EKi.c[n];
+                       ctx->Xi.c[n] ^= c;
+                       --len;
+                       n = (n+1)%16;
+               }
+               if (n==0) GCM_MUL (ctx,Xi);
+               else {
+                       ctx->mres = n;
+                       return 0;
+               }
+       }
+#if defined(GHASH) && !defined(OPENSSL_SMALL_FOOTPRINT)
+       while (len>=GHASH_CHUNK) {
+               GHASH(ctx,in,GHASH_CHUNK);
+               (*stream)(in,out,GHASH_CHUNK/16,ctx->key,ctx->Yi.c);
+               ctr += GHASH_CHUNK/16;
+               if (is_endian.little)
+                       PUTU32(ctx->Yi.c+12,ctr);
+               else
+                       ctx->Yi.d[3] = ctr;
+               out += GHASH_CHUNK;
+               in  += GHASH_CHUNK;
+               len -= GHASH_CHUNK;
+       }
+#endif
+       if ((i = (len&(size_t)-16))) {
+               size_t j=i/16;
+
+#if defined(GHASH)
+               GHASH(ctx,in,i);
+#else
+               while (j--) {
+                       size_t k;
+                       for (k=0;k<16;++k) ctx->Xi.c[k] ^= in[k];
+                       GCM_MUL(ctx,Xi);
+                       in += 16;
+               }
+               j   = i/16;
+               in -= i;
+#endif
+               (*stream)(in,out,j,ctx->key,ctx->Yi.c);
+               ctr += (unsigned int)j;
+               if (is_endian.little)
+                       PUTU32(ctx->Yi.c+12,ctr);
+               else
+                       ctx->Yi.d[3] = ctr;
+               out += i;
+               in  += i;
+               len -= i;
+       }
+       if (len) {
+               (*ctx->block)(ctx->Yi.c,ctx->EKi.c,ctx->key);
+               ++ctr;
+               if (is_endian.little)
+                       PUTU32(ctx->Yi.c+12,ctr);
+               else
+                       ctx->Yi.d[3] = ctr;
+               while (len--) {
+                       u8 c = in[n];
+                       ctx->Xi.c[n] ^= c;
+                       out[n] = c^ctx->EKi.c[n];
+                       ++n;
+               }
+       }
+
+       ctx->mres = n;
+       return 0;
 }
 
 int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx,const unsigned char *tag,
@@ -1099,7 +1340,7 @@ int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx,const unsigned char *tag,
        u64 alen = ctx->len.u[0]<<3;
        u64 clen = ctx->len.u[1]<<3;
 
-       if (ctx->res)
+       if (ctx->mres)
                GCM_MUL(ctx,Xi);
 
        if (is_endian.little) {
@@ -1130,6 +1371,12 @@ int CRYPTO_gcm128_finish(GCM128_CONTEXT *ctx,const unsigned char *tag,
                return -1;
 }
 
+void CRYPTO_gcm128_tag(GCM128_CONTEXT *ctx, unsigned char *tag, size_t len)
+{
+       CRYPTO_gcm128_finish(ctx, NULL, 0);
+       memcpy(tag, ctx->Xi.c, len<=sizeof(ctx->Xi.c)?len:sizeof(ctx->Xi.c));
+}
+
 GCM128_CONTEXT *CRYPTO_gcm128_new(void *key, block128_f block)
 {
        GCM128_CONTEXT *ret;
@@ -1370,12 +1617,14 @@ static const u8 IV18[]={0x93,0x13,0x22,0x5d,0xf8,0x84,0x06,0xe5,0x55,0x90,0x9c,0
        AES_set_encrypt_key(K##n,sizeof(K##n)*8,&key);          \
        CRYPTO_gcm128_init(&ctx,&key,(block128_f)AES_encrypt);  \
        CRYPTO_gcm128_setiv(&ctx,IV##n,sizeof(IV##n));          \
+       memset(out,0,sizeof(out));                              \
        if (A##n) CRYPTO_gcm128_aad(&ctx,A##n,sizeof(A##n));    \
        if (P##n) CRYPTO_gcm128_encrypt(&ctx,P##n,out,sizeof(out));     \
        if (CRYPTO_gcm128_finish(&ctx,T##n,16) ||               \
            (C##n && memcmp(out,C##n,sizeof(out))))             \
-               ret++, printf ("encrypt test#%d failed.\n",n);\
+               ret++, printf ("encrypt test#%d failed.\n",n);  \
        CRYPTO_gcm128_setiv(&ctx,IV##n,sizeof(IV##n));          \
+       memset(out,0,sizeof(out));                              \
        if (A##n) CRYPTO_gcm128_aad(&ctx,A##n,sizeof(A##n));    \
        if (C##n) CRYPTO_gcm128_decrypt(&ctx,C##n,out,sizeof(out));     \
        if (CRYPTO_gcm128_finish(&ctx,T##n,16) ||               \
@@ -1424,11 +1673,11 @@ int main()
        gcm_t = OPENSSL_rdtsc() - start;
 
        CRYPTO_ctr128_encrypt(buf.c,buf.c,sizeof(buf),
-                       &key,ctx.Yi.c,ctx.EKi.c,&ctx.res,
+                       &key,ctx.Yi.c,ctx.EKi.c,&ctx.mres,
                        (block128_f)AES_encrypt);
        start = OPENSSL_rdtsc();
        CRYPTO_ctr128_encrypt(buf.c,buf.c,sizeof(buf),
-                       &key,ctx.Yi.c,ctx.EKi.c,&ctx.res,
+                       &key,ctx.Yi.c,ctx.EKi.c,&ctx.mres,
                        (block128_f)AES_encrypt);
        ctr_t = OPENSSL_rdtsc() - start;