]> git.cryptolib.org Git - arm-crypto-lib.git/blobdiff - sha2/sha2_small_common.c
switching to dedicated endian switching function
[arm-crypto-lib.git] / sha2 / sha2_small_common.c
index 7dad795f492fb44bc981cfa5593688c406ef76a6..3c30cb9fa5ecc63fa599983c79134a272cdba069 100644 (file)
@@ -40,20 +40,21 @@ uint32_t rotl32( uint32_t x, uint8_t n){
 /*************************************************************************/
 
 // #define CHANGE_ENDIAN32(x) (((x)<<24) | ((x)>>24) | (((x)& 0x0000ff00)<<8) | (((x)& 0x00ff0000)>>8))
+/*
 static
 uint32_t change_endian32(uint32_t x){
        return (((x)<<24) | ((x)>>24) | (((x)& 0x0000ff00)<<8) | (((x)& 0x00ff0000)>>8));
 }
-
+*/
 
 /* sha256 functions as macros for speed and size, cause they are called only once */
 
 #define CH(x,y,z)  (((x)&(y)) ^ ((~(x))&(z)))
 #define MAJ(x,y,z) (((x)&(y)) ^ ((x)&(z)) ^ ((y)&(z)))
 
-#define SIGMA0(x) (rotr32((x),2) ^ rotr32((x),13) ^ rotl32((x),10))
-#define SIGMA1(x) (rotr32((x),6) ^ rotr32((x),11) ^ rotl32((x),7))
-#define SIGMA_a(x) (rotr32((x),7)  ^ rotl32((x),14) ^ ((x)>>3))
+#define SIGMA_0(x) (rotr32((x), 2) ^ rotr32((x),13) ^ rotl32((x),10))
+#define SIGMA_1(x) (rotr32((x), 6) ^ rotr32((x),11) ^ rotl32((x),7))
+#define SIGMA_a(x) (rotr32((x), 7) ^ rotl32((x),14) ^ ((x)>>3))
 #define SIGMA_b(x) (rotl32((x),15) ^ rotl32((x),13) ^ ((x)>>10))
 
 const
@@ -68,21 +69,28 @@ uint32_t k[]={
        0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
 };
 
-
+static
+void load_endian32_changed(uint8_t* dest, uint8_t* src, uint16_t words){
+       while(words--){
+               *dest++ = src[3];
+               *dest++ = src[2];
+               *dest++ = src[1];
+               *dest++ = src[0];
+               src += 4;
+       }
+}
 
 /**
  * block must be, 512, Bit = 64, Byte, long !!!
  */
 void sha2_small_common_nextBlock (sha2_small_common_ctx_t *state, const void* block){
-       uint32_t w[16], wx;     /* this is 256, byte, large, */
+       uint32_t w[16], wx;
        uint8_t  i;
        uint32_t a[8],t1,t2;
 
        /* init w */
 #if defined LITTLE_ENDIAN
-       for (i=0; i<16; ++i){
-               w[i]= change_endian32(((uint32_t*)block)[i]);
-       }
+       load_endian32_changed((uint8_t*)w, (uint8_t*)block, 16);
 #elif defined BIG_ENDIAN
                memcpy((void*)w, block, 64);
 #endif
@@ -103,8 +111,8 @@ void sha2_small_common_nextBlock (sha2_small_common_ctx_t *state, const void* bl
                        memmove(&(w[0]), &(w[1]), 15*4);
                        w[15] = wx;
                }
-               t1 = a[7] + SIGMA1(a[4]) + CH(a[4],a[5],a[6]) + k[i] + wx;
-               t2 = SIGMA0(a[0]) + MAJ(a[0],a[1],a[2]);
+               t1 = a[7] + SIGMA_1(a[4]) + CH(a[4],a[5],a[6]) + k[i] + wx;
+               t2 = SIGMA_0(a[0]) + MAJ(a[0],a[1],a[2]);
                memmove(&(a[1]), &(a[0]), 7*4);         /* a[7]=a[6]; a[6]=a[5]; a[5]=a[4]; a[4]=a[3]; a[3]=a[2]; a[2]=a[1]; a[1]=a[0]; */
                a[4] += t1;
                a[0] = t1 + t2;