Eliminate redundant variable in Camellia CBC routine.
authorAndy Polyakov <appro@openssl.org>
Sat, 2 Dec 2006 11:52:50 +0000 (11:52 +0000)
committerAndy Polyakov <appro@openssl.org>
Sat, 2 Dec 2006 11:52:50 +0000 (11:52 +0000)
crypto/camellia/cmll_cbc.c

index 932c1d1be626dbd34803ef55fe1e69f5513ebdd2..4141a7b59bb733522af648452d359e4ac7402db7 100644 (file)
@@ -67,9 +67,9 @@ void Camellia_cbc_encrypt(const unsigned char *in, unsigned char *out,
 
        unsigned long n;
        unsigned long len = length;
-       unsigned char tmp[CAMELLIA_BLOCK_SIZE];
        const unsigned char *iv = ivec;
-       u32 t32[CAMELLIA_BLOCK_SIZE/sizeof(u32)];
+       union { u32 t32[CAMELLIA_BLOCK_SIZE/sizeof(u32)];
+               u8  t8 [CAMELLIA_BLOCK_SIZE]; } tmp;
        const union { long one; char little; } camellia_endian = {1};
 
 
@@ -127,14 +127,14 @@ void Camellia_cbc_encrypt(const unsigned char *in, unsigned char *out,
                                }
                        if (len)
                                {
-                               memcpy(tmp, in, CAMELLIA_BLOCK_SIZE);
+                               memcpy(tmp.t8, in, CAMELLIA_BLOCK_SIZE);
                                if (camellia_endian.little)
-                                       SWAP4WORD((u32 *)tmp);
-                               key->dec(key->rd_key, (u32 *)tmp);
+                                       SWAP4WORD(tmp.t32);
+                               key->dec(key->rd_key, tmp.t32);
                                if (camellia_endian.little)
-                                       SWAP4WORD((u32 *)tmp);
+                                       SWAP4WORD(tmp.t32);
                                for(n=0; n < len; ++n)
-                                       out[n] = tmp[n] ^ iv[n];
+                                       out[n] = tmp.t8[n] ^ iv[n];
                                iv = in;
                                }
                        memcpy(ivec,iv,CAMELLIA_BLOCK_SIZE);
@@ -143,21 +143,21 @@ void Camellia_cbc_encrypt(const unsigned char *in, unsigned char *out,
                        {
                        while (len >= CAMELLIA_BLOCK_SIZE)
                                {
-                               memcpy(tmp, in, CAMELLIA_BLOCK_SIZE);
+                               memcpy(tmp.t8, in, CAMELLIA_BLOCK_SIZE);
                                if (camellia_endian.little)
                                        SWAP4WORD((u32 *)out);
                                key->dec(key->rd_key, (u32 *)out);
                                if (camellia_endian.little)
                                        SWAP4WORD((u32 *)out);
                                XOR4WORD((u32 *)out, (u32 *)ivec);
-                               memcpy(ivec, tmp, CAMELLIA_BLOCK_SIZE);
+                               memcpy(ivec, tmp.t8, CAMELLIA_BLOCK_SIZE);
                                len -= CAMELLIA_BLOCK_SIZE;
                                in += CAMELLIA_BLOCK_SIZE;
                                out += CAMELLIA_BLOCK_SIZE;
                                }
                        if (len)
                                {
-                               memcpy(tmp, in, CAMELLIA_BLOCK_SIZE);
+                               memcpy(tmp.t8, in, CAMELLIA_BLOCK_SIZE);
                                if (camellia_endian.little)
                                        SWAP4WORD((u32 *)out);
                                key->dec(key->rd_key,(u32 *)out);
@@ -166,8 +166,8 @@ void Camellia_cbc_encrypt(const unsigned char *in, unsigned char *out,
                                for(n=0; n < len; ++n)
                                        out[n] ^= ivec[n];
                                for(n=len; n < CAMELLIA_BLOCK_SIZE; ++n)
-                                       out[n] = tmp[n];
-                               memcpy(ivec, tmp, CAMELLIA_BLOCK_SIZE);
+                                       out[n] = tmp.t8[n];
+                               memcpy(ivec, tmp.t8, CAMELLIA_BLOCK_SIZE);
                                }
                        }
                }
@@ -178,14 +178,13 @@ void Camellia_cbc_encrypt(const unsigned char *in, unsigned char *out,
                        while (len >= CAMELLIA_BLOCK_SIZE)
                                {
                                for(n=0; n < CAMELLIA_BLOCK_SIZE; ++n)
-                                       out[n] = in[n] ^ iv[n];
-                               memcpy(t32, out, CAMELLIA_BLOCK_SIZE);
+                                       tmp.t8[n] = in[n] ^ iv[n];
                                if (camellia_endian.little)
-                                       SWAP4WORD(t32);
-                               key->enc(key->rd_key, t32);
+                                       SWAP4WORD(tmp.t32);
+                               key->enc(key->rd_key, tmp.t32);
                                if (camellia_endian.little)
-                                       SWAP4WORD(t32);
-                               memcpy(out, t32, CAMELLIA_BLOCK_SIZE);
+                                       SWAP4WORD(tmp.t32);
+                               memcpy(out, tmp.t8, CAMELLIA_BLOCK_SIZE);
                                iv = out;
                                len -= CAMELLIA_BLOCK_SIZE;
                                in += CAMELLIA_BLOCK_SIZE;
@@ -194,16 +193,15 @@ void Camellia_cbc_encrypt(const unsigned char *in, unsigned char *out,
                        if (len)
                                {
                                for(n=0; n < len; ++n)
-                                       out[n] = in[n] ^ iv[n];
+                                       tmp.t8[n] = in[n] ^ iv[n];
                                for(n=len; n < CAMELLIA_BLOCK_SIZE; ++n)
-                                       out[n] = iv[n];
-                               memcpy(t32, out, CAMELLIA_BLOCK_SIZE);
+                                       tmp.t8[n] = iv[n];
                                if (camellia_endian.little)
-                                       SWAP4WORD(t32);
-                               key->enc(key->rd_key, t32);
+                                       SWAP4WORD(tmp.t32);
+                               key->enc(key->rd_key, tmp.t32);
                                if (camellia_endian.little)
-                                       SWAP4WORD(t32);
-                               memcpy(out, t32, CAMELLIA_BLOCK_SIZE);
+                                       SWAP4WORD(tmp.t32);
+                               memcpy(out, tmp.t8, CAMELLIA_BLOCK_SIZE);
                                iv = out;
                                }
                        memcpy(ivec,iv,CAMELLIA_BLOCK_SIZE);
@@ -212,15 +210,14 @@ void Camellia_cbc_encrypt(const unsigned char *in, unsigned char *out,
                        {
                        while (len >= CAMELLIA_BLOCK_SIZE)
                                {
-                               memcpy(t32,in,CAMELLIA_BLOCK_SIZE);
+                               memcpy(tmp.t8,in,CAMELLIA_BLOCK_SIZE);
                                if (camellia_endian.little)
-                                       SWAP4WORD(t32);
-                               key->dec(key->rd_key,t32);
+                                       SWAP4WORD(tmp.t32);
+                               key->dec(key->rd_key,tmp.t32);
                                if (camellia_endian.little)
-                                       SWAP4WORD(t32);
-                               memcpy(out,t32,CAMELLIA_BLOCK_SIZE);
+                                       SWAP4WORD(tmp.t32);
                                for(n=0; n < CAMELLIA_BLOCK_SIZE; ++n)
-                                       out[n] ^= iv[n];
+                                       out[n] = tmp.t8[n] ^ iv[n];
                                iv = in;
                                len -= CAMELLIA_BLOCK_SIZE;
                                in  += CAMELLIA_BLOCK_SIZE;
@@ -228,15 +225,14 @@ void Camellia_cbc_encrypt(const unsigned char *in, unsigned char *out,
                                }
                        if (len)
                                {
-                               memcpy(t32, in, CAMELLIA_BLOCK_SIZE);
+                               memcpy(tmp.t8, in, CAMELLIA_BLOCK_SIZE);
                                if (camellia_endian.little)
-                                       SWAP4WORD(t32);
-                               key->dec(key->rd_key, t32);
+                                       SWAP4WORD(tmp.t32);
+                               key->dec(key->rd_key, tmp.t32);
                                if (camellia_endian.little)
-                                       SWAP4WORD(t32);
-                               memcpy(out, t32, CAMELLIA_BLOCK_SIZE);
+                                       SWAP4WORD(tmp.t32);
                                for(n=0; n < len; ++n)
-                                       out[n] ^= iv[n];
+                                       out[n] = tmp.t8[n] ^ iv[n];
                                iv = in;
                                }
                        memcpy(ivec,iv,CAMELLIA_BLOCK_SIZE);
@@ -245,36 +241,32 @@ void Camellia_cbc_encrypt(const unsigned char *in, unsigned char *out,
                        {
                        while (len >= CAMELLIA_BLOCK_SIZE)
                                {
-                               memcpy(tmp, in, CAMELLIA_BLOCK_SIZE);
-                               memcpy(t32, in, CAMELLIA_BLOCK_SIZE);
+                               memcpy(tmp.t8, in, CAMELLIA_BLOCK_SIZE);
                                if (camellia_endian.little)
-                                       SWAP4WORD(t32);
-                               key->dec(key->rd_key, t32);
+                                       SWAP4WORD(tmp.t32);
+                               key->dec(key->rd_key, tmp.t32);
                                if (camellia_endian.little)
-                                       SWAP4WORD(t32);
-                               memcpy(out, t32, CAMELLIA_BLOCK_SIZE);
+                                       SWAP4WORD(tmp.t32);
                                for(n=0; n < CAMELLIA_BLOCK_SIZE; ++n)
-                                       out[n] ^= ivec[n];
-                               memcpy(ivec, tmp, CAMELLIA_BLOCK_SIZE);
+                                       tmp.t8[n] ^= ivec[n];
+                               memcpy(ivec, in, CAMELLIA_BLOCK_SIZE);
+                               memcpy(out, tmp.t8, CAMELLIA_BLOCK_SIZE);
                                len -= CAMELLIA_BLOCK_SIZE;
                                in += CAMELLIA_BLOCK_SIZE;
                                out += CAMELLIA_BLOCK_SIZE;
                                }
                        if (len)
                                {
-                               memcpy(tmp, in, CAMELLIA_BLOCK_SIZE);
-                               memcpy(t32, in, CAMELLIA_BLOCK_SIZE);
+                               memcpy(tmp.t8, in, CAMELLIA_BLOCK_SIZE);
                                if (camellia_endian.little)
-                                       SWAP4WORD(t32);
-                               key->dec(key->rd_key,t32);
+                                       SWAP4WORD(tmp.t32);
+                               key->dec(key->rd_key,tmp.t32);
                                if (camellia_endian.little)
-                                       SWAP4WORD(t32);
-                               memcpy(out, t32, CAMELLIA_BLOCK_SIZE);
+                                       SWAP4WORD(tmp.t32);
                                for(n=0; n < len; ++n)
-                                       out[n] ^= ivec[n];
-                               for(n=len; n < CAMELLIA_BLOCK_SIZE; ++n)
-                                       out[n] = tmp[n];
-                               memcpy(ivec, tmp, CAMELLIA_BLOCK_SIZE);
+                                       tmp.t8[n] ^= ivec[n];
+                               memcpy(ivec, in, CAMELLIA_BLOCK_SIZE);
+                               memcpy(out,tmp.t8,len);
                                }
                        }
                }