crypto/modes: strict aliasing fixes from master.
authorAndy Polyakov <appro@openssl.org>
Sun, 6 Apr 2014 15:23:55 +0000 (17:23 +0200)
committerAndy Polyakov <appro@openssl.org>
Sun, 6 Apr 2014 15:23:55 +0000 (17:23 +0200)
crypto/modes/cbc128.c
crypto/modes/cts128.c

index 8f8bd563b96cf1b6c40bdb0515d6c92df448fd0d..64b24778867c21bbb47989e77f90e585340217d5 100644 (file)
@@ -121,7 +121,7 @@ void CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out,
                        unsigned char ivec[16], block128_f block)
 {
        size_t n;
-       union { size_t align; unsigned char c[16]; } tmp;
+       union { size_t t[16/sizeof(size_t)]; unsigned char c[16]; } tmp;
 
        assert(in && out && key && ivec);
 
@@ -141,11 +141,13 @@ void CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out,
                                out += 16;
                        }
                }
-               else {
+               else  if (16%sizeof(size_t) == 0) { /* always true */
                        while (len>=16) {
+                               size_t *out_t=(size_t *)out, *iv_t=(size_t *)iv;
+
                                (*block)(in, out, key);
-                               for(n=0; n<16; n+=sizeof(size_t))
-                                       *(size_t *)(out+n) ^= *(size_t *)(iv+n);
+                               for(n=0; n<16/sizeof(size_t); n++)
+                                       out_t[n] ^= iv_t[n];
                                iv = in;
                                len -= 16;
                                in  += 16;
@@ -169,15 +171,16 @@ void CRYPTO_cbc128_decrypt(const unsigned char *in, unsigned char *out,
                                out += 16;
                        }
                }
-               else {
-                       size_t c;
+               else if (16%sizeof(size_t) == 0) { /* always true */
                        while (len>=16) {
+                               size_t c, *out_t=(size_t *)out, *ivec_t=(size_t *)ivec;
+                               const size_t *in_t=(const size_t *)in;
+
                                (*block)(in, tmp.c, key);
-                               for(n=0; n<16; n+=sizeof(size_t)) {
-                                       c = *(size_t *)(in+n);
-                                       *(size_t *)(out+n) =
-                                       *(size_t *)(tmp.c+n) ^ *(size_t *)(ivec+n);
-                                       *(size_t *)(ivec+n) = c;
+                               for(n=0; n<16/sizeof(size_t); n++) {
+                                       c = in_t[n];
+                                       out_t[n] = tmp.t[n] ^ ivec_t[n];
+                                       ivec_t[n] = c;
                                }
                                len -= 16;
                                in  += 16;
index e0430f9fdcba1694b7cad769b82aa3e5e976a2f2..4a087a03af970391d5119ac1cfc3a63842a02d0f 100644 (file)
@@ -78,12 +78,8 @@ size_t CRYPTO_cts128_encrypt(const unsigned char *in, unsigned char *out,
        (*cbc)(in,out-16,residue,key,ivec,1);
        memcpy(out,tmp.c,residue);
 #else
-       {
-       size_t n;
-       for (n=0; n<16; n+=sizeof(size_t))
-               *(size_t *)(tmp.c+n) = 0;
+       memset(tmp.c,0,sizeof(tmp));
        memcpy(tmp.c,in,residue);
-       }
        memcpy(out,out-16,residue);
        (*cbc)(tmp.c,out-16,16,key,ivec,1);
 #endif
@@ -112,8 +108,7 @@ size_t CRYPTO_cts128_decrypt_block(const unsigned char *in, unsigned char *out,
 
        (*block)(in,tmp.c+16,key);
 
-       for (n=0; n<16; n+=sizeof(size_t))
-               *(size_t *)(tmp.c+n) = *(size_t *)(tmp.c+16+n);
+       memcpy(tmp.c,tmp.c+16,16);
        memcpy(tmp.c,in+16,residue);
        (*block)(tmp.c,tmp.c,key);
 
@@ -131,7 +126,7 @@ size_t CRYPTO_cts128_decrypt_block(const unsigned char *in, unsigned char *out,
 size_t CRYPTO_cts128_decrypt(const unsigned char *in, unsigned char *out,
                        size_t len, const void *key,
                        unsigned char ivec[16], cbc128_f cbc)
-{      size_t residue, n;
+{      size_t residue;
        union { size_t align; unsigned char c[32]; } tmp;
 
        assert (in && out && key && ivec);
@@ -148,8 +143,7 @@ size_t CRYPTO_cts128_decrypt(const unsigned char *in, unsigned char *out,
                out += len;
        }
 
-       for (n=16; n<32; n+=sizeof(size_t))
-               *(size_t *)(tmp.c+n) = 0;
+       memset(tmp.c,0,sizeof(tmp));
        /* this places in[16] at &tmp.c[16] and decrypted block at &tmp.c[0] */
        (*cbc)(in,tmp.c,16,key,tmp.c+16,0);