modes/ocb128.c: fix overstep.
[openssl.git] / crypto / modes / ocb128.c
index cbcb7f62dde79a3e16529d44f45b24227585c324..3a3f7a8d939e3c9c911dabd0010f1eb18a13cb26 100644 (file)
 
 #ifndef OPENSSL_NO_OCB
 
-union ublock {
-    unsigned char *chrblk;
-    OCB_BLOCK *ocbblk;
-};
-
 /*
  * Calculate the number of binary trailing zero's in any given number
  */
@@ -83,28 +78,24 @@ static u32 ocb_ntz(u64 n)
 /*
  * Shift a block of 16 bytes left by shift bits
  */
-static void ocb_block_lshift(OCB_BLOCK *in, size_t shift, OCB_BLOCK *out)
+static void ocb_block_lshift(const unsigned char *in, size_t shift,
+                             unsigned char *out)
 {
     unsigned char shift_mask;
     int i;
     unsigned char mask[15];
-    union ublock locin;
-    union ublock locout;
-
-    locin.ocbblk = in;
-    locout.ocbblk = out;
 
     shift_mask = 0xff;
     shift_mask <<= (8 - shift);
     for (i = 15; i >= 0; i--) {
         if (i > 0) {
-            mask[i - 1] = locin.chrblk[i] & shift_mask;
+            mask[i - 1] = in[i] & shift_mask;
             mask[i - 1] >>= 8 - shift;
         }
-        locout.chrblk[i] = locin.chrblk[i] << shift;
+        out[i] = in[i] << shift;
 
         if (i != 15) {
-            locout.chrblk[i] ^= mask[i];
+            out[i] ^= mask[i];
         }
     }
 }
@@ -115,23 +106,18 @@ static void ocb_block_lshift(OCB_BLOCK *in, size_t shift, OCB_BLOCK *out)
 static void ocb_double(OCB_BLOCK *in, OCB_BLOCK *out)
 {
     unsigned char mask;
-    union ublock locin;
-    union ublock locout;
-
-    locin.ocbblk = in;
-    locout.ocbblk = out;
 
     /*
      * Calculate the mask based on the most significant bit. There are more
      * efficient ways to do this - but this way is constant time
      */
-    mask = locin.chrblk[0] & 0x80;
+    mask = in->c[0] & 0x80;
     mask >>= 7;
     mask *= 135;
 
-    ocb_block_lshift(in, 1, out);
+    ocb_block_lshift(in->c, 1, out->c);
 
-    locout.chrblk[15] ^= mask;
+    out->c[15] ^= mask;
 }
 
 /*
@@ -153,20 +139,34 @@ static void ocb_block_xor(const unsigned char *in1,
  */
 static OCB_BLOCK *ocb_lookup_l(OCB128_CONTEXT *ctx, size_t idx)
 {
-    if (idx <= ctx->l_index) {
+    size_t l_index = ctx->l_index;
+
+    if (idx <= l_index) {
         return ctx->l + idx;
     }
 
     /* We don't have it - so calculate it */
-    ctx->l_index++;
-    if (ctx->l_index == ctx->max_l_index) {
-        ctx->max_l_index *= 2;
+    if (idx >= ctx->max_l_index) {
+        /*
+         * Each additional entry allows to process almost double as
+         * much data, so that in linear world the table will need to
+         * be expanded with smaller and smaller increments. Originally
+         * it was doubling in size, which was a waste. Growing it
+         * linearly is not formally optimal, but is simpler to implement.
+         * We grow table by minimally required 4*n that would accommodate
+         * the index.
+         */
+        ctx->max_l_index += (idx - ctx->max_l_index + 4) & ~3;
         ctx->l =
             OPENSSL_realloc(ctx->l, ctx->max_l_index * sizeof(OCB_BLOCK));
         if (!ctx->l)
             return NULL;
     }
-    ocb_double(ctx->l + (idx - 1), ctx->l + idx);
+    while (l_index < idx) {
+        ocb_double(ctx->l + l_index, ctx->l + l_index + 1);
+        l_index++;
+    }
+    ctx->l_index = l_index;
 
     return ctx->l + idx;
 }
@@ -177,13 +177,7 @@ static OCB_BLOCK *ocb_lookup_l(OCB128_CONTEXT *ctx, size_t idx)
 static void ocb_encrypt(OCB128_CONTEXT *ctx, OCB_BLOCK *in, OCB_BLOCK *out,
                         void *keyenc)
 {
-    union ublock locin;
-    union ublock locout;
-
-    locin.ocbblk = in;
-    locout.ocbblk = out;
-
-    ctx->encrypt(locin.chrblk, locout.chrblk, keyenc);
+    ctx->encrypt(in->c, out->c, keyenc);
 }
 
 /*
@@ -192,13 +186,7 @@ static void ocb_encrypt(OCB128_CONTEXT *ctx, OCB_BLOCK *in, OCB_BLOCK *out,
 static void ocb_decrypt(OCB128_CONTEXT *ctx, OCB_BLOCK *in, OCB_BLOCK *out,
                         void *keydec)
 {
-    union ublock locin;
-    union ublock locout;
-
-    locin.ocbblk = in;
-    locout.ocbblk = out;
-
-    ctx->decrypt(locin.chrblk, locout.chrblk, keydec);
+    ctx->decrypt(in->c, out->c, keydec);
 }
 
 /*
@@ -210,7 +198,7 @@ OCB128_CONTEXT *CRYPTO_ocb128_new(void *keyenc, void *keydec,
     OCB128_CONTEXT *octx;
     int ret;
 
-    if ((octx = (OCB128_CONTEXT *)OPENSSL_malloc(sizeof(OCB128_CONTEXT)))) {
+    if ((octx = OPENSSL_malloc(sizeof(*octx))) != NULL) {
         ret = CRYPTO_ocb128_init(octx, keyenc, keydec, encrypt, decrypt);
         if (ret)
             return octx;
@@ -226,13 +214,11 @@ OCB128_CONTEXT *CRYPTO_ocb128_new(void *keyenc, void *keydec,
 int CRYPTO_ocb128_init(OCB128_CONTEXT *ctx, void *keyenc, void *keydec,
                        block128_f encrypt, block128_f decrypt)
 {
-    /* Clear everything to NULLs */
     memset(ctx, 0, sizeof(*ctx));
-
     ctx->l_index = 0;
-    ctx->max_l_index = 1;
+    ctx->max_l_index = 5;
     ctx->l = OPENSSL_malloc(ctx->max_l_index * 16);
-    if (!ctx->l)
+    if (ctx->l == NULL)
         return 0;
 
     /*
@@ -254,6 +240,13 @@ int CRYPTO_ocb128_init(OCB128_CONTEXT *ctx, void *keyenc, void *keydec,
     /* L_0 = double(L_$) */
     ocb_double(&ctx->l_dollar, ctx->l);
 
+    /* L_{i} = double(L_{i-1}) */
+    ocb_double(ctx->l, ctx->l+1);
+    ocb_double(ctx->l+1, ctx->l+2);
+    ocb_double(ctx->l+2, ctx->l+3);
+    ocb_double(ctx->l+3, ctx->l+4);
+    ctx->l_index = 4;   /* enough to process up to 496 bytes */
+
     return 1;
 }
 
@@ -270,7 +263,7 @@ int CRYPTO_ocb128_copy_ctx(OCB128_CONTEXT *dest, OCB128_CONTEXT *src,
         dest->keydec = keydec;
     if (src->l) {
         dest->l = OPENSSL_malloc(src->max_l_index * 16);
-        if (!dest->l)
+        if (dest->l == NULL)
             return 0;
         memcpy(dest->l, src->l, (src->l_index + 1) * 16);
     }
@@ -286,9 +279,6 @@ int CRYPTO_ocb128_setiv(OCB128_CONTEXT *ctx, const unsigned char *iv,
     unsigned char ktop[16], tmp[16], mask;
     unsigned char stretch[24], nonce[16];
     size_t bottom, shift;
-    union ublock offset;
-
-    offset.ocbblk = &ctx->offset;
 
     /*
      * Spec says IV is 120 bits or fewer - it allows non byte aligned lengths.
@@ -318,11 +308,10 @@ int CRYPTO_ocb128_setiv(OCB128_CONTEXT *ctx, const unsigned char *iv,
 
     /* Offset_0 = Stretch[1+bottom..128+bottom] */
     shift = bottom % 8;
-    ocb_block_lshift((OCB_BLOCK *)(stretch + (bottom / 8)), shift,
-                     &ctx->offset);
+    ocb_block_lshift(stretch + (bottom / 8), shift, ctx->offset.c);
     mask = 0xff;
     mask <<= 8 - shift;
-    offset.chrblk[15] |=
+    ctx->offset.c[15] |=
         (*(stretch + (bottom / 8) + 16) & mask) >> (8 - shift);
 
     return 1;
@@ -374,8 +363,8 @@ int CRYPTO_ocb128_aad(OCB128_CONTEXT *ctx, const unsigned char *aad,
         ocb_block16_xor(&ctx->offset_aad, &ctx->l_star, &ctx->offset_aad);
 
         /* CipherInput = (A_* || 1 || zeros(127-bitlen(A_*))) xor Offset_* */
-        memset((void *)&tmp1, 0, 16);
-        memcpy((void *)&tmp1, aad + (num_blocks * 16), last_len);
+        memset(&tmp1, 0, 16);
+        memcpy(&tmp1, aad + (num_blocks * 16), last_len);
         ((unsigned char *)&tmp1)[last_len] = 0x80;
         ocb_block16_xor(&ctx->offset_aad, &tmp1, &tmp2);
 
@@ -425,14 +414,14 @@ int CRYPTO_ocb128_encrypt(OCB128_CONTEXT *ctx,
 
         /* C_i = Offset_i xor ENCIPHER(K, P_i xor Offset_i) */
         inblock = (OCB_BLOCK *)(in + ((i - ctx->blocks_processed - 1) * 16));
-        ocb_block16_xor(&ctx->offset, inblock, &tmp1);
+        ocb_block16_xor_misaligned(&ctx->offset, inblock, &tmp1);
+        /* Checksum_i = Checksum_{i-1} xor P_i */
+        ocb_block16_xor_misaligned(&ctx->checksum, inblock, &ctx->checksum);
         ocb_encrypt(ctx, &tmp1, &tmp2, ctx->keyenc);
         outblock =
             (OCB_BLOCK *)(out + ((i - ctx->blocks_processed - 1) * 16));
-        ocb_block16_xor(&ctx->offset, &tmp2, outblock);
+        ocb_block16_xor_misaligned(&ctx->offset, &tmp2, outblock);
 
-        /* Checksum_i = Checksum_{i-1} xor P_i */
-        ocb_block16_xor(&ctx->checksum, inblock, &ctx->checksum);
     }
 
     /*
@@ -453,8 +442,8 @@ int CRYPTO_ocb128_encrypt(OCB128_CONTEXT *ctx,
                       out + (num_blocks * 16));
 
         /* Checksum_* = Checksum_m xor (P_* || 1 || zeros(127-bitlen(P_*))) */
-        memset((void *)&tmp1, 0, 16);
-        memcpy((void *)&tmp1, in + (len / 16) * 16, last_len);
+        memset(&tmp1, 0, 16);
+        memcpy(&tmp1, in + (len / 16) * 16, last_len);
         ((unsigned char *)(&tmp1))[last_len] = 0x80;
         ocb_block16_xor(&ctx->checksum, &tmp1, &ctx->checksum);
     }
@@ -498,14 +487,14 @@ int CRYPTO_ocb128_decrypt(OCB128_CONTEXT *ctx,
 
         /* P_i = Offset_i xor DECIPHER(K, C_i xor Offset_i) */
         inblock = (OCB_BLOCK *)(in + ((i - ctx->blocks_processed - 1) * 16));
-        ocb_block16_xor(&ctx->offset, inblock, &tmp1);
+        ocb_block16_xor_misaligned(&ctx->offset, inblock, &tmp1);
         ocb_decrypt(ctx, &tmp1, &tmp2, ctx->keydec);
         outblock =
             (OCB_BLOCK *)(out + ((i - ctx->blocks_processed - 1) * 16));
-        ocb_block16_xor(&ctx->offset, &tmp2, outblock);
+        ocb_block16_xor_misaligned(&ctx->offset, &tmp2, outblock);
 
         /* Checksum_i = Checksum_{i-1} xor P_i */
-        ocb_block16_xor(&ctx->checksum, outblock, &ctx->checksum);
+        ocb_block16_xor_misaligned(&ctx->checksum, outblock, &ctx->checksum);
     }
 
     /*
@@ -526,8 +515,8 @@ int CRYPTO_ocb128_decrypt(OCB128_CONTEXT *ctx,
                       out + (num_blocks * 16));
 
         /* Checksum_* = Checksum_m xor (P_* || 1 || zeros(127-bitlen(P_*))) */
-        memset((void *)&tmp1, 0, 16);
-        memcpy((void *)&tmp1, out + (len / 16) * 16, last_len);
+        memset(&tmp1, 0, 16);
+        memcpy(&tmp1, out + (len / 16) * 16, last_len);
         ((unsigned char *)(&tmp1))[last_len] = 0x80;
         ocb_block16_xor(&ctx->checksum, &tmp1, &ctx->checksum);
     }
@@ -588,10 +577,7 @@ int CRYPTO_ocb128_tag(OCB128_CONTEXT *ctx, unsigned char *tag, size_t len)
 void CRYPTO_ocb128_cleanup(OCB128_CONTEXT *ctx)
 {
     if (ctx) {
-        if (ctx->l) {
-            OPENSSL_cleanse(ctx->l, ctx->max_l_index * 16);
-            OPENSSL_free(ctx->l);
-        }
+        OPENSSL_clear_free(ctx->l, ctx->max_l_index * 16);
         OPENSSL_cleanse(ctx, sizeof(*ctx));
     }
 }