# define BUG24 1
#endif
-#define F0_HACK 2
+#define F0_HACK 0
#define DEBUG 0
#define dump_x(a,b,c)
#endif
-static
-uint32_t bmw_small_s0(uint32_t x){
- uint32_t r;
- r = SHR32(x, 1)
- ^ SHL32(x, 3)
- ^ ROTL32(x, 4)
- ^ ROTR32(x, 13);
- return r;
-}
-
-static
-uint32_t bmw_small_s1(uint32_t x){
- uint32_t r;
- r = SHR32(x, 1)
- ^ SHL32(x, 2)
- ^ ROTL32(x, 8)
- ^ ROTR32(x, 9);
- return r;
-}
-
-static
-uint32_t bmw_small_s2(uint32_t x){
- uint32_t r;
- r = SHR32(x, 2)
- ^ SHL32(x, 1)
- ^ ROTL32(x, 12)
- ^ ROTR32(x, 7);
- return r;
-}
-
-static
-uint32_t bmw_small_s3(uint32_t x){
- uint32_t r;
- r = SHR32(x, 2)
- ^ SHL32(x, 2)
- ^ ROTL32(x, 15)
- ^ ROTR32(x, 3);
- return r;
-}
-
-static
-uint32_t bmw_small_s4(uint32_t x){
- uint32_t r;
- r = SHR32(x, 1)
- ^ x;
- return r;
-}
-
-static
-uint32_t bmw_small_s5(uint32_t x){
- uint32_t r;
- r = SHR32(x, 2)
- ^ x;
- return r;
-}
-
-static
-uint32_t bmw_small_r1(uint32_t x){
- uint32_t r;
- r = ROTL32(x, 3);
- return r;
-}
-
-static
-uint32_t bmw_small_r2(uint32_t x){
- uint32_t r;
- r = ROTL32(x, 7);
- return r;
-}
-
-static
-uint32_t bmw_small_r3(uint32_t x){
- uint32_t r;
- r = ROTL32(x, 13);
- return r;
-}
-
-static
-uint32_t bmw_small_r4(uint32_t x){
- uint32_t r;
- r = ROTL32(x, 16);
- return r;
-}
-
-static
-uint32_t bmw_small_r5(uint32_t x){
- uint32_t r;
- r = ROTR32(x, 13);
- return r;
-}
-
-static
-uint32_t bmw_small_r6(uint32_t x){
- uint32_t r;
- r = ROTR32(x, 9);
- return r;
-}
-
-static
-uint32_t bmw_small_r7(uint32_t x){
- uint32_t r;
- r = ROTR32(x, 5);
- return r;
-}
+#define S32_0(x) ( (SHR32((x), 1)) ^ \
+ (SHL32((x), 3)) ^ \
+ (ROTL32((x), 4)) ^ \
+ (ROTR32((x), 13)) )
+
+#define S32_1(x) ( (SHR32((x), 1)) ^ \
+ (SHL32((x), 2)) ^ \
+ (ROTL32((x), 8)) ^ \
+ (ROTR32((x), 9)) )
+
+#define S32_2(x) ( (SHR32((x), 2)) ^ \
+ (SHL32((x), 1)) ^ \
+ (ROTL32((x), 12)) ^ \
+ (ROTR32((x), 7)) )
+
+#define S32_3(x) ( (SHR32((x), 2)) ^ \
+ (SHL32((x), 2)) ^ \
+ (ROTL32((x), 15)) ^ \
+ (ROTR32((x), 3)) )
+
+#define S32_4(x) ( (SHR32((x), 1)) ^ (x))
+
+#define S32_5(x) ( (SHR32((x), 2)) ^ (x))
+
+#define R32_1(x) (ROTL32((x), 3))
+#define R32_2(x) (ROTL32((x), 7))
+#define R32_3(x) (ROTL32((x), 13))
+#define R32_4(x) (ROTL32((x), 16))
+#define R32_5(x) (ROTR32((x), 13))
+#define R32_6(x) (ROTR32((x), 9))
+#define R32_7(x) (ROTR32((x), 5))
/*
#define K 0x05555555L
static
static
uint32_t bmw_small_expand1(uint8_t j, const uint32_t* q, const void* m, const void* h){
- uint32_t(*s[])(uint32_t) = {bmw_small_s1, bmw_small_s2, bmw_small_s3, bmw_small_s0};
uint32_t r;
- uint8_t i;
/* r = 0x05555555*(j+16); */
-#if TWEAK
r = ( ROTL32(((uint32_t*)m)[j&0xf], ((j+0)&0xf)+1 )
+ ROTL32(((uint32_t*)m)[(j+3)&0xf], ((j+3)&0xf)+1 )
- ROTL32(((uint32_t*)m)[(j+10)&0xf], ((j+10)&0xf)+1 )
+ k_lut[j]
) ^ ((uint32_t*)h)[(j+7)&0xf];
-#else
- r = k_lut[j];
- r += ((uint32_t*)m)[j&0xf];
- r += ((uint32_t*)m)[(j+3)&0xf];
- r -= ((uint32_t*)m)[(j+10)&0xf];
-#endif
- for(i=0; i<16; ++i){
- r += s[i%4](q[j+i]);
- }
+ r += S32_1(q[j+ 0]) + S32_2(q[j+ 1]) + S32_3(q[j+ 2]) + S32_0(q[j+ 3])
+ + S32_1(q[j+ 4]) + S32_2(q[j+ 5]) + S32_3(q[j+ 6]) + S32_0(q[j+ 7])
+ + S32_1(q[j+ 8]) + S32_2(q[j+ 9]) + S32_3(q[j+10]) + S32_0(q[j+11])
+ + S32_1(q[j+12]) + S32_2(q[j+13]) + S32_3(q[j+14]) + S32_0(q[j+15]);
return r;
}
static
uint32_t bmw_small_expand2(uint8_t j, const uint32_t* q, const void* m, const void* h){
- uint32_t(*rf[])(uint32_t) = {bmw_small_r1, bmw_small_r2, bmw_small_r3,
- bmw_small_r4, bmw_small_r5, bmw_small_r6,
- bmw_small_r7};
- uint32_t r=0;
- uint8_t i;
- for(i=0; i<14; i+=2){
- r += q[j+i];
- }
- for(i=0; i<14; i+=2){
- r += rf[i/2](q[j+i+1]);
- }
-#if TWEAK
- r += bmw_small_s4(q[j+14]);
- r += bmw_small_s5(q[j+15]);
-#else
- r += bmw_small_s5(q[j+14]);
- r += bmw_small_s4(q[j+15]);
-#endif
-#if TWEAK
- r += ( ROTL32(((uint32_t*)m)[j&0xf], ((j+0)&0xf)+1 )
+ uint32_t r;
+ r = ( ROTL32(((uint32_t*)m)[j&0xf], ((j+0)&0xf)+1 )
+ ROTL32(((uint32_t*)m)[(j+3)&0xf], ((j+3)&0xf)+1 )
- ROTL32(((uint32_t*)m)[(j+10)&0xf], ((j+10)&0xf)+1 )
+ k_lut[j]
) ^ ((uint32_t*)h)[(j+7)&0xf];
-#else
- r += k_lut[j];
- r += ((uint32_t*)m)[j&0xf];
- r += ((uint32_t*)m)[(j+3)&0xf];
- r -= ((uint32_t*)m)[(j+10)&0xf];
-#endif
+ r += (q[j+ 0]) + R32_1(q[j+ 1]) + (q[j+ 2]) + R32_2(q[j+ 3])
+ + (q[j+ 4]) + R32_3(q[j+ 5]) + (q[j+ 6]) + R32_4(q[j+ 7])
+ + (q[j+ 8]) + R32_5(q[j+ 9]) + (q[j+10]) + R32_6(q[j+11])
+ + (q[j+12]) + R32_7(q[j+13]) + S32_4(q[j+14]) + S32_5(q[j+15]);
return r;
}
for(i=0; i<16; ++i){
q[i] = s[i%5](q[i]);
}
-#if TWEAK
for(i=0; i<16; ++i){
((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
}
for(i=0; i<16; ++i){
q[i] += h[(i+1)&0xf];
}
-#endif
}
#endif /* F0_HACK==2*/
for(i=0; i<16; ++i){
q[i] = s[i%5](q[i]);
}
-#if TWEAK
for(i=0; i<16; ++i){
((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
}
for(i=0; i<16; ++i){
q[i] += h[(i+1)&0xf];
}
-#endif /* TWEAK */
}
#endif /* F0_HACK==1 */
static
void bmw_small_f0(uint32_t* q, uint32_t* h, const void* m){
uint8_t i;
- uint32_t(*s[])(uint32_t)={ bmw_small_s0, bmw_small_s1, bmw_small_s2,
- bmw_small_s3, bmw_small_s4 };
for(i=0; i<16; ++i){
((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
}
q[14] = (h[ 3] - h[ 5] + h[ 8] - h[11] - h[12]);
q[15] = (h[12] - h[ 4] - h[ 6] - h[ 9] + h[13]);
dump_x(q, 16, 'W');
- for(i=0; i<16; ++i){
- q[i] = s[i%5](q[i]);
- }
-#if TWEAK
+ q[ 0] = S32_0(q[ 0]);
+ q[ 1] = S32_1(q[ 1]);
+ q[ 2] = S32_2(q[ 2]);
+ q[ 3] = S32_3(q[ 3]);
+ q[ 4] = S32_4(q[ 4]);
+ q[ 5] = S32_0(q[ 5]);
+ q[ 6] = S32_1(q[ 6]);
+ q[ 7] = S32_2(q[ 7]);
+ q[ 8] = S32_3(q[ 8]);
+ q[ 9] = S32_4(q[ 9]);
+ q[10] = S32_0(q[10]);
+ q[11] = S32_1(q[11]);
+ q[12] = S32_2(q[12]);
+ q[13] = S32_3(q[13]);
+ q[14] = S32_4(q[14]);
+ q[15] = S32_0(q[15]);
for(i=0; i<16; ++i){
((uint32_t*)h)[i] ^= ((uint32_t*)m)[i];
}
for(i=0; i<16; ++i){
q[i] += h[(i+1)&0xf];
}
-#endif /* TWEAK */
}
#endif /* F0_HACK==0 */
}
void bmw_small_lastBlock(bmw_small_ctx_t* ctx, const void* block, uint16_t length_b){
- uint8_t buffer[64];
+ union {
+ uint8_t v8[64];
+ uint32_t v32[16];
+ uint64_t v64[ 8];
+ } buffer;
while(length_b >= BMW_SMALL_BLOCKSIZE){
bmw_small_nextBlock(ctx, block);
length_b -= BMW_SMALL_BLOCKSIZE;
block = (uint8_t*)block + BMW_SMALL_BLOCKSIZE_B;
}
- memset(buffer, 0, 64);
- memcpy(buffer, block, (length_b+7)/8);
- buffer[length_b>>3] |= 0x80 >> (length_b&0x07);
+ memset(buffer.v8, 0, 64);
+ memcpy(buffer.v8, block, (length_b+7)/8);
+ buffer.v8[length_b>>3] |= 0x80 >> (length_b&0x07);
if(length_b+1>64*8-64){
- bmw_small_nextBlock(ctx, buffer);
- memset(buffer, 0, 64-8);
+ bmw_small_nextBlock(ctx, buffer.v8);
+ memset(buffer.v8, 0, 64-8);
ctx->counter -= 1;
}
- *((uint64_t*)&(buffer[64-8])) = (uint64_t)(ctx->counter*512LL)+(uint64_t)length_b;
- bmw_small_nextBlock(ctx, buffer);
+ buffer.v64[7] = (uint64_t)(ctx->counter*512LL)+(uint64_t)length_b;
+ bmw_small_nextBlock(ctx, buffer.v8);
#if TWEAK
uint8_t i;
uint32_t q[32];
- memset(buffer, 0xaa, 64);
+ memset(buffer.v8, 0xaa, 64);
for(i=0; i<16;++i){
- buffer[i*4] = i+0xa0;
+ buffer.v8[i*4] = i+0xa0;
}
-// dump_x(buffer, 16, 'A');
+// dump_x(buffer.v8, 16, 'A');
dump_x(ctx->h, 16, 'M');
- bmw_small_f0(q, (uint32_t*)buffer, ctx->h);
- dump_x(buffer, 16, 'a');
+ bmw_small_f0(q, buffer.v32, ctx->h);
+ dump_x(buffer.v8, 16, 'a');
dump_x(q, 16, 'Q');
- bmw_small_f1(q, ctx->h, (uint32_t*)buffer);
+ bmw_small_f1(q, ctx->h, buffer.v32);
dump_x(q, 32, 'Q');
- bmw_small_f2((uint32_t*)buffer, q, ctx->h);
- memcpy(ctx->h, buffer, 64);
+ bmw_small_f2(buffer.v32, q, ctx->h);
+ memcpy(ctx->h, buffer.v8, 64);
#endif
}