auxFormat.h 4.69 KB
Newer Older
Zhao Xuefeng committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
#include<malloc.h>
#include"crypto_hash.h"
#include"api.h"
#include  <string.h>
#include <stdio.h>
#include <stdlib.h>
#define U32BIG(x) (x)

typedef unsigned char u8;
typedef unsigned int u32;
typedef unsigned long long u64;

#define ARR_SIZE(a) (sizeof((a))/sizeof((a[0])))
#define LOTR32(x,n) (((x)<<(n))|((x)>>(32-(n))))

#define ARR_SIZE(a) (sizeof((a))/sizeof((a[0])))
#define sbox(a, b, c, d,  f, g, h) \
{  \
	t1 = ~a; t2 = b & t1;t3 = c ^ t2; h = d ^ t3; t5 = b | c; t6 = d ^ t1; g = t5 ^ t6; t8 = b ^ d; t9 = t3 & t6; a = t8 ^ t9; t11 = g & t8; f = t3 ^ t11; \
}

#define U96_BIT_LOTR32_8(t0,t1,t2,t3,t4,t5){\
t3= LOTR32(t2, 2);\
t4 =LOTR32(t0, 3);\
t5 = LOTR32(t1, 3); \
}
#define U96_BIT_LOTR32_55(t0,t1,t2,t3,t4,t5){\
t3= LOTR32(t1, 18); \
t4 = LOTR32(t2, 18);\
t5 = LOTR32(t0, 19); \
}
#define ROUND384(lunNum) {\
s[0] ^= (constant7Format[lunNum] >> 6) & 0x3;\
s[1] ^= (constant7Format[lunNum] >> 3) & 0x7;\
s[2] ^= constant7Format[lunNum] & 0x7;\
sbox(s[0], s[3], s[6], s[9] , s_temp[3], s_temp[6], s_temp[9]);\
sbox(s[1], s[4], s[7], s[10], s[3]     , s_temp[7], s_temp[10]);\
sbox(s[2], s[5], s[8], s[11], s[4]     , s_temp[8], s_temp[11]);\
s[5] = LOTR32(s_temp[3], 1); \
U96_BIT_LOTR32_8(s_temp[6], s_temp [7], s_temp[ 8], s[6],  s[7], s[8]);\
U96_BIT_LOTR32_55(s_temp[9], s_temp[10], s_temp[11], s[9], s[10], s[11]);\
}

Wentao Zhang committed
44 45 46 47 48 49 50 51
#define puckU32ToThree_3(lo){\
u32 r0;\
r0 = (lo ^ (lo << 1)) & 0x14514514, lo ^= r0 ^ (r0 >> 1);\
r0 = (lo ^ (lo << 3)) & 0x10410410, lo ^= r0 ^ (r0 >> 3);\
r0 = (lo ^ (lo << 2)) & 0x00330330, lo ^= r0 ^ (r0 >> 2);\
r0 = (lo ^ (lo << 6)) & 0x00300300, lo ^= r0 ^ (r0 >> 6);\
r0 = (lo ^ (lo << 4)) & 0x000f0f00, lo ^= r0 ^ (r0 >> 4);\
r0 = (lo ^ (lo << 12)) & 0x000f0000, lo ^= r0 ^ (r0 >> 12);\
Zhao Xuefeng committed
52
}
Wentao Zhang committed
53 54 55 56 57 58 59 60
#define unpuckU32ToThree_3(lo){\
		u32 r0;\
r0 = (lo ^ (lo << 12)) & 0x000f0000, lo ^= r0 ^ (r0 >> 12);\
r0 = (lo ^ (lo << 4)) & 0x000f0f00, lo ^= r0 ^ (r0 >> 4);\
r0 = (lo ^ (lo << 6)) & 0x00300300, lo ^= r0 ^ (r0 >> 6);\
r0 = (lo ^ (lo << 2)) & 0x00330330, lo ^= r0 ^ (r0 >> 2);\
r0 = (lo ^ (lo << 3)) & 0x10410410, lo ^= r0 ^ (r0 >> 3);\
r0 = (lo ^ (lo << 1)) & 0x14514514, lo ^= r0 ^ (r0 >> 1);\
Zhao Xuefeng committed
61
}
Wentao Zhang committed
62 63 64 65 66 67
#define packU32FormatToThreePacket( out,  in) {\
u32  t0 = U32BIG(((u32*)in)[0]);	\
puckU32ToThree_3(t0);	\
out[0] = ((((t0 & 0x30000000) >> 20) | ((t0 & 0xff0000) >> 16)));	\
out[1] = (((t0 & 0x80000000) >> 21) | ((t0 & 0xc000000) >> 18) | ((t0 & 0xff00) >> 8));	\
out[2] = (((t0 & 0x40000000) >> 20) | ((t0 & 0x03000000) >> 16) | (t0 & 0x000000ff));	\
Zhao Xuefeng committed
68
}
Wentao Zhang committed
69 70 71 72 73 74 75 76
#define unpackU32FormatToThreePacket(out, in) {\
u32 t[3] = { 0 }  ;\
u32 t0 = in[0], t1 = in[1], t2 = in[2];		 \
t[0] = ((t1 & 0x400) << 21) | ((t2 & 0x400) << 20) |		 \
((t0 & 0x300) << 20) | ((t1 & 0x300) << 18) | ((t2 & 0x300) << 16) |		 \
((t0 & 0xff) << 16) | ((t1 & 0xff) << 8) | (t2 & 0xff);		 \
unpuckU32ToThree_3(t[0]);		 \
*(u32*)(out) = t[0];	\
Zhao Xuefeng committed
77
}
Wentao Zhang committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91
#define packU96FormatToThreePacket( out,  in) {               \
	u32  t0 = U32BIG(((u32*)in)[0]), t1 = U32BIG(((u32*)in)[1]), t2 = U32BIG(((u32*)in)[2]);	\
	puckU32ToThree_3(t0);	\
	puckU32ToThree_3(t1);	\
	puckU32ToThree_3(t2);	\
	out[0] = (((t2 & 0x80000000)) | ((t2 & 0xc000000) << 3) | ((t2 & 0xff00) << 13)) |	\
	(((t1 & 0x40000000) >> 10) | ((t1 & 0x03000000) >> 6) | ((t1 & 0x000000ff) << 10)) |	\
	((((t0 & 0x30000000) >> 20) | ((t0 & 0xff0000) >> 16)));	\
	out[1] = (((t2 & 0x40000000) << 1) | ((t2 & 0x03000000) << 5) | ((t2 & 0x000000ff) << 21)) |	\
	((((t1 & 0x30000000) >> 9) | ((t1 & 0xff0000) >> 5)) |	\
	(((t0 & 0x80000000) >> 21) | ((t0 & 0xc000000) >> 18) | ((t0 & 0xff00) >> 8)));	\
	out[2] = ((((t2 & 0x30000000) << 2) | ((t2 & 0xff0000) << 6)) |	\
	(((t1 & 0x80000000) >> 10) | ((t1 & 0xc000000) >> 7) | ((t1 & 0xff00) << 3)) |	\
	(((t0 & 0x40000000) >> 20) | ((t0 & 0x03000000) >> 16) | (t0 & 0x000000ff)));	\
Zhao Xuefeng committed
92
}
Wentao Zhang committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

#define unpackU96FormatToThreePacket( out, in) {\
	u32 t[3] = { 0 };\
	u32 t0 = in[0], t1 = in[1], t2 = in[2];		 \
	t[0] = ((t1 & 0x400) << 21) | ((t2 & 0x400) << 20) |		 \
	((t0 & 0x300) << 20) | ((t1 & 0x300) << 18) | ((t2 & 0x300) << 16) |		 \
	((t0 & 0xff) << 16) | ((t1 & 0xff) << 8) | (t2 & 0xff);		 \
	t[1] = ((t2 & 0x200000) << 10) | ((t0 & 0x100000) << 10) | ((t1 & 0x180000) << 9) | ((t2 & 0x180000) << 7) |		 \
	((t0 & 0xc0000) << 6) | ((t1 & 0x7f800) << 5) | ((t2 & 0x7f800) >> 3) |		 \
	((t0 & 0x3fc00) >> 10);		 \
	t[2] = ((t0 & 0x80000000)) | ((t1 & 0x80000000) >> 1) | ((t2 & 0xc0000000) >> 2) |		 \
	((t0 & 0x60000000) >> 3) | ((t1 & 0x60000000) >> 5) | ((t2 & 0x3fc00000) >> 6) |		 \
	((t0 & 0x1fe00000) >> 13) | ((t1 & 0x1fe00000) >> 21);		 \
	unpuckU32ToThree_3(t[0]);		 \
	unpuckU32ToThree_3(t[1]);		 \
	unpuckU32ToThree_3(t[2]);		 \
	memcpy(out, t, 12 * sizeof(unsigned char));		 \
Zhao Xuefeng committed
110
}