34 #if defined( USE_INLINING )
35 # if defined( _MSC_VER )
36 # define gf_decl __inline
37 # elif defined( __GNUC__ ) || defined( __GNU_LIBRARY__ )
38 # define gf_decl static inline
40 # define gf_decl static
45 # define _t1(n) bswap ## n ## _block(x, x)
47 # define _t2(n) bswap ## n ## _block(x, x); bswap ## n ## _block(r, r)
51 #define gf_m(n,x) gf_mulx ## n ## x
52 #define gf_mulx1(x) gf_m(1,x)
53 #define gf_mulx4(x) gf_m(4,x)
54 #define gf_mulx8(x) gf_m(8,x)
56 #define MASK(x) ((x) * (UNIT_CAST(-1,UNIT_BITS) / 0xff))
58 #define DATA_256(q) {\
59 q(0x00), q(0x01), q(0x02), q(0x03), q(0x04), q(0x05), q(0x06), q(0x07),\
60 q(0x08), q(0x09), q(0x0a), q(0x0b), q(0x0c), q(0x0d), q(0x0e), q(0x0f),\
61 q(0x10), q(0x11), q(0x12), q(0x13), q(0x14), q(0x15), q(0x16), q(0x17),\
62 q(0x18), q(0x19), q(0x1a), q(0x1b), q(0x1c), q(0x1d), q(0x1e), q(0x1f),\
63 q(0x20), q(0x21), q(0x22), q(0x23), q(0x24), q(0x25), q(0x26), q(0x27),\
64 q(0x28), q(0x29), q(0x2a), q(0x2b), q(0x2c), q(0x2d), q(0x2e), q(0x2f),\
65 q(0x30), q(0x31), q(0x32), q(0x33), q(0x34), q(0x35), q(0x36), q(0x37),\
66 q(0x38), q(0x39), q(0x3a), q(0x3b), q(0x3c), q(0x3d), q(0x3e), q(0x3f),\
67 q(0x40), q(0x41), q(0x42), q(0x43), q(0x44), q(0x45), q(0x46), q(0x47),\
68 q(0x48), q(0x49), q(0x4a), q(0x4b), q(0x4c), q(0x4d), q(0x4e), q(0x4f),\
69 q(0x50), q(0x51), q(0x52), q(0x53), q(0x54), q(0x55), q(0x56), q(0x57),\
70 q(0x58), q(0x59), q(0x5a), q(0x5b), q(0x5c), q(0x5d), q(0x5e), q(0x5f),\
71 q(0x60), q(0x61), q(0x62), q(0x63), q(0x64), q(0x65), q(0x66), q(0x67),\
72 q(0x68), q(0x69), q(0x6a), q(0x6b), q(0x6c), q(0x6d), q(0x6e), q(0x6f),\
73 q(0x70), q(0x71), q(0x72), q(0x73), q(0x74), q(0x75), q(0x76), q(0x77),\
74 q(0x78), q(0x79), q(0x7a), q(0x7b), q(0x7c), q(0x7d), q(0x7e), q(0x7f),\
75 q(0x80), q(0x81), q(0x82), q(0x83), q(0x84), q(0x85), q(0x86), q(0x87),\
76 q(0x88), q(0x89), q(0x8a), q(0x8b), q(0x8c), q(0x8d), q(0x8e), q(0x8f),\
77 q(0x90), q(0x91), q(0x92), q(0x93), q(0x94), q(0x95), q(0x96), q(0x97),\
78 q(0x98), q(0x99), q(0x9a), q(0x9b), q(0x9c), q(0x9d), q(0x9e), q(0x9f),\
79 q(0xa0), q(0xa1), q(0xa2), q(0xa3), q(0xa4), q(0xa5), q(0xa6), q(0xa7),\
80 q(0xa8), q(0xa9), q(0xaa), q(0xab), q(0xac), q(0xad), q(0xae), q(0xaf),\
81 q(0xb0), q(0xb1), q(0xb2), q(0xb3), q(0xb4), q(0xb5), q(0xb6), q(0xb7),\
82 q(0xb8), q(0xb9), q(0xba), q(0xbb), q(0xbc), q(0xbd), q(0xbe), q(0xbf),\
83 q(0xc0), q(0xc1), q(0xc2), q(0xc3), q(0xc4), q(0xc5), q(0xc6), q(0xc7),\
84 q(0xc8), q(0xc9), q(0xca), q(0xcb), q(0xcc), q(0xcd), q(0xce), q(0xcf),\
85 q(0xd0), q(0xd1), q(0xd2), q(0xd3), q(0xd4), q(0xd5), q(0xd6), q(0xd7),\
86 q(0xd8), q(0xd9), q(0xda), q(0xdb), q(0xdc), q(0xdd), q(0xde), q(0xdf),\
87 q(0xe0), q(0xe1), q(0xe2), q(0xe3), q(0xe4), q(0xe5), q(0xe6), q(0xe7),\
88 q(0xe8), q(0xe9), q(0xea), q(0xeb), q(0xec), q(0xed), q(0xee), q(0xef),\
89 q(0xf0), q(0xf1), q(0xf2), q(0xf3), q(0xf4), q(0xf5), q(0xf6), q(0xf7),\
90 q(0xf8), q(0xf9), q(0xfa), q(0xfb), q(0xfc), q(0xfd), q(0xfe), q(0xff) }
101 #if defined( GF_MODE_BL ) || defined( GF_MODE_LL )
103 #define gf_uint16_xor(i) ( \
104 (i & 0x01 ? xx(00,87) : 0) ^ (i & 0x02 ? xx(01,0e) : 0) ^ \
105 (i & 0x04 ? xx(02,1c) : 0) ^ (i & 0x08 ? xx(04,38) : 0) ^ \
106 (i & 0x10 ? xx(08,70) : 0) ^ (i & 0x20 ? xx(10,e0) : 0) ^ \
107 (i & 0x40 ? xx(21,c0) : 0) ^ (i & 0x80 ? xx(43,80) : 0) )
111 X_0 = 0x01, X_1 = 0x02, X_2 = 0x04, X_3 = 0x08, X_4 = 0x10, X_5 = 0x20, X_6 = 0x40, X_7 = 0x80
114 #elif defined( GF_MODE_BB ) || defined( GF_MODE_LB )
116 #define gf_uint16_xor(i) ( \
117 (i & 0x80 ? xx(e1,00) : 0) ^ (i & 0x40 ? xx(70,80) : 0) ^ \
118 (i & 0x20 ? xx(38,40) : 0) ^ (i & 0x10 ? xx(1c,20) : 0) ^ \
119 (i & 0x08 ? xx(0e,10) : 0) ^ (i & 0x04 ? xx(07,08) : 0) ^ \
120 (i & 0x02 ? xx(03,84) : 0) ^ (i & 0x01 ? xx(01,c2) : 0) )
124 X_0 = 0x80, X_1 = 0x40, X_2 = 0x20, X_3 = 0x10, X_4 = 0x08, X_5 = 0x04, X_6 = 0x02, X_7 = 0x01
128 #error Galois Field representation has not been set
131 #if defined( GF_MODE_BL ) || defined( GF_MODE_LB )
133 #if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
134 # define xx(p,q) 0x##q##p
136 # define xx(p,q) 0x##p##q
139 #elif defined( GF_MODE_BB ) || defined( GF_MODE_LL )
141 #if (PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN)
142 # define xx(p,q) 0x##p##q
144 # define xx(p,q) 0x##q##p
148 #error Galois Field representation has not been set
155 #if PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN
159 #define f1_bl(n,r,x) r[n] = (x[n] << 1) & ~MASK(0x01) | ((x[n] >> 15) | (!n ? x[n+1] << 49 : 0)) & MASK(0x01)
160 #define f4_bl(n,r,x) r[n] = (x[n] << 4) & ~MASK(0x0f) | ((x[n] >> 12) | (!n ? x[n+1] << 52 : 0)) & MASK(0x0f)
161 #define f8_bl(n,r,x) r[n] = (x[n] >> 8) | (!n ? x[n+1] << 56 : 0)
167 UNIT_PTR(r)[1] ^= ((gf_unit_t)_tt) << 48;
174 UNIT_PTR(x)[1] ^= ((gf_unit_t)_tt) << 48;
181 UNIT_PTR(x)[1] ^= ((gf_unit_t)_tt) << 48;
184 #elif UNIT_BITS == 32
186 #define f1_bl(n,r,x) r[n] = (x[n] << 1) & ~MASK(0x01) | ((x[n] >> 15) | (n < 3 ? x[n+1] << 17 : 0)) & MASK(0x01)
187 #define f4_bl(n,r,x) r[n] = (x[n] << 4) & ~MASK(0x0f) | ((x[n] >> 12) | (n < 3 ? x[n+1] << 20 : 0)) & MASK(0x0f)
188 #define f8_bl(n,r,x) r[n] = (x[n] >> 8) | (n < 3 ? x[n+1] << 24 : 0)
194 UNIT_PTR(r)[3] ^= ((gf_unit_t)_tt) << 16;
201 UNIT_PTR(x)[3] ^= ((gf_unit_t)_tt) << 16;
208 UNIT_PTR(x)[3] ^= ((gf_unit_t)_tt) << 16;
213 #define f1_bl(n,r,x) r[n] = (x[n] << 1) | (n < 15 ? x[n+1] >> 7 : 0)
214 #define f4_bl(n,r,x) r[n] = (x[n] << 4) | (n < 15 ? x[n+1] >> 4 : 0)
241 #elif PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN
245 #define f1_bl(n,r,x) r[n] = (x[n] << 1) | (!n ? x[n+1] >> 63 : 0)
246 #define f4_bl(n,r,x) r[n] = (x[n] << 4) | (!n ? x[n+1] >> 60 : 0)
247 #define f8_bl(n,r,x) r[n] = (x[n] << 8) | (!n ? x[n+1] >> 56 : 0)
270 #elif UNIT_BITS == 32
272 #define f1_bl(n,r,x) r[n] = (x[n] << 1) | (n < 3 ? x[n+1] >> 31 : 0)
273 #define f4_bl(n,r,x) r[n] = (x[n] << 4) | (n < 3 ? x[n+1] >> 28 : 0)
274 #define f8_bl(n,r,x) r[n] = (x[n] << 8) | (n < 3 ? x[n+1] >> 24 : 0)
299 #define f1_bl(n,r,x) r[n] = (x[n] << 1) | (n < 15 ? x[n+1] >> 7 : 0)
300 #define f4_bl(n,r,x) r[n] = (x[n] << 4) | (n < 15 ? x[n+1] >> 4 : 0)
328 # error Platform byte order has not been set.
333 #if PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN
337 #define f1_bb(n,r,x) r[n] = (x[n] >> 1) | (!n ? x[n+1] << 63 : 0)
338 #define f4_bb(n,r,x) r[n] = (x[n] >> 4) | (!n ? x[n+1] << 60 : 0)
339 #define f8_bb(n,r,x) r[n] = (x[n] >> 8) | (!n ? x[n+1] << 56 : 0)
345 UNIT_PTR(r)[1] ^= ((gf_unit_t)_tt) << 48;
352 UNIT_PTR(x)[1] ^= ((gf_unit_t)_tt) << 48;
359 UNIT_PTR(x)[1] ^= ((gf_unit_t)_tt) << 48;
362 #elif UNIT_BITS == 32
364 #define f1_bb(n,r,x) r[n] = (x[n] >> 1) | (n < 3 ? x[n+1] << 31 : 0)
365 #define f4_bb(n,r,x) r[n] = (x[n] >> 4) | (n < 3 ? x[n+1] << 28 : 0)
366 #define f8_bb(n,r,x) r[n] = (x[n] >> 8) | (n < 3 ? x[n+1] << 24 : 0)
372 UNIT_PTR(r)[3] ^= ((gf_unit_t)_tt) << 16;
379 UNIT_PTR(x)[3] ^= ((gf_unit_t)_tt) << 16;
386 UNIT_PTR(x)[3] ^= ((gf_unit_t)_tt) << 16;
391 #define f1_bb(n,r,x) r[n] = (x[n] >> 1) | (n < 15 ? x[n+1] << 7 : 0)
392 #define f4_bb(n,r,x) r[n] = (x[n] >> 4) | (n < 15 ? x[n+1] << 4 : 0)
419 #elif PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN
423 #define f1_bb(n,r,x) r[n] = (x[n] >> 1) & ~MASK(0x80) | ((x[n] << 15) | (!n ? x[n+1] >> 49 : 0)) & MASK(0x80)
424 #define f4_bb(n,r,x) r[n] = (x[n] >> 4) & ~MASK(0xf0) | ((x[n] << 12) | (!n ? x[n+1] >> 52 : 0)) & MASK(0xf0)
425 #define f8_bb(n,r,x) r[n] = (x[n] >> 8) & ~MASK(0xff) | ((x[n] << 8) | (!n ? x[n+1] >> 56 : 0)) & MASK(0xff)
448 #elif UNIT_BITS == 32
450 #define f1_bb(n,r,x) r[n] = (x[n] >> 1) & ~MASK(0x80) | ((x[n] << 15) | (n < 3 ? x[n+1] >> 17 : 0)) & MASK(0x80)
451 #define f4_bb(n,r,x) r[n] = (x[n] >> 4) & ~MASK(0xf0) | ((x[n] << 12) | (n < 3 ? x[n+1] >> 20 : 0)) & MASK(0xf0)
452 #define f8_bb(n,r,x) r[n] = (x[n] >> 8) & ~MASK(0xff) | ((x[n] << 8) | (n < 3 ? x[n+1] >> 24 : 0)) & MASK(0xff)
477 #define f1_bb(n,r,x) r[n] = (x[n] >> 1) | (n < 15 ? x[n+1] << 7 : 0)
478 #define f4_bb(n,r,x) r[n] = (x[n] >> 4) | (n < 15 ? x[n+1] << 4 : 0)
506 # error Platform byte order has not been set.
511 #if PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN
515 #define f1_ll(n,r,x) r[n] = (x[n] << 1) | (n ? x[n-1] >> 63 : 0)
516 #define f4_ll(n,r,x) r[n] = (x[n] << 4) | (n ? x[n-1] >> 60 : 0)
517 #define f8_ll(n,r,x) r[n] = (x[n] << 8) | (n ? x[n-1] >> 56 : 0)
540 #elif UNIT_BITS == 32
542 #define f1_ll(n,r,x) r[n] = (x[n] << 1) | (n ? x[n-1] >> 31 : 0)
543 #define f4_ll(n,r,x) r[n] = (x[n] << 4) | (n ? x[n-1] >> 28 : 0)
544 #define f8_ll(n,r,x) r[n] = (x[n] << 8) | (n ? x[n-1] >> 24 : 0)
569 #define f1_ll(n,r,x) r[n] = (x[n] << 1) | (n ? x[n-1] >> 7 : 0)
570 #define f4_ll(n,r,x) r[n] = (x[n] << 4) | (n ? x[n-1] >> 4 : 0)
597 #elif PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN
601 #define f1_ll(n,r,x) r[n] = (x[n] << 1) & ~MASK(0x01) | ((x[n] >> 15) | (n ? x[n-1] << 49 : 0)) & MASK(0x01)
602 #define f4_ll(n,r,x) r[n] = (x[n] << 4) & ~MASK(0x0f) | ((x[n] >> 12) | (n ? x[n-1] << 52 : 0)) & MASK(0x0f)
603 #define f8_ll(n,r,x) r[n] = (x[n] << 8) & ~MASK(0xff) | ((x[n] >> 8) | (n ? x[n-1] << 56 : 0)) & MASK(0xff)
609 UNIT_PTR(r)[0] ^= ((gf_unit_t)_tt) << 48;
616 UNIT_PTR(x)[0] ^= ((gf_unit_t)_tt) << 48;
623 UNIT_PTR(x)[0] ^= ((gf_unit_t)_tt) << 48;
626 #elif UNIT_BITS == 32
628 #define f1_ll(n,r,x) r[n] = (x[n] << 1) & ~MASK(0x01) | ((x[n] >> 15) | (n ? x[n-1] << 17 : 0)) & MASK(0x01)
629 #define f4_ll(n,r,x) r[n] = (x[n] << 4) & ~MASK(0x0f) | ((x[n] >> 12) | (n ? x[n-1] << 20 : 0)) & MASK(0x0f)
630 #define f8_ll(n,r,x) r[n] = (x[n] << 8) & ~MASK(0xff) | ((x[n] >> 8) | (n ? x[n-1] << 24 : 0)) & MASK(0xff)
636 UNIT_PTR(r)[0] ^= ((gf_unit_t)_tt) << 16;
643 UNIT_PTR(x)[0] ^= ((gf_unit_t)_tt) << 16;
650 UNIT_PTR(x)[0] ^= ((gf_unit_t)_tt) << 16;
655 #define f1_ll(n,r,x) r[n] = (x[n] << 1) | (n ? x[n-1] >> 7 : 0)
656 #define f4_ll(n,r,x) r[n] = (x[n] << 4) | (n ? x[n-1] >> 4 : 0)
684 # error Platform byte order has not been set.
689 #if PLATFORM_BYTE_ORDER == IS_LITTLE_ENDIAN
693 #define f1_lb(n,r,x) r[n] = (x[n] >> 1) & ~MASK(0x80) | ((x[n] << 15) | (n ? x[n-1] >> 49 : 0)) & MASK(0x80)
694 #define f4_lb(n,r,x) r[n] = (x[n] >> 4) & ~MASK(0xf0) | ((x[n] << 12) | (n ? x[n-1] >> 52 : 0)) & MASK(0xf0)
695 #define f8_lb(n,r,x) r[n] = (x[n] << 8) | (n ? x[n-1] >> 56 : 0)
718 #elif UNIT_BITS == 32
720 #define f1_lb(n,r,x) r[n] = (x[n] >> 1) & ~MASK(0x80) | ((x[n] << 15) | (n ? x[n-1] >> 17 : 0)) & MASK(0x80)
721 #define f4_lb(n,r,x) r[n] = (x[n] >> 4) & ~MASK(0xf0) | ((x[n] << 12) | (n ? x[n-1] >> 20 : 0)) & MASK(0xf0)
722 #define f8_lb(n,r,x) r[n] = (x[n] << 8) | (n ? x[n-1] >> 24 : 0)
747 #define f1_lb(n,r,x) r[n] = (x[n] >> 1) | (n ? x[n-1] << 7 : 0)
748 #define f4_lb(n,r,x) r[n] = (x[n] >> 4) | (n ? x[n-1] << 4 : 0)
775 #elif PLATFORM_BYTE_ORDER == IS_BIG_ENDIAN
779 #define f1_lb(n,r,x) r[n] = (x[n] >> 1) | (n ? x[n-1] << 63 : 0)
780 #define f4_lb(n,r,x) r[n] = (x[n] >> 4) | (n ? x[n-1] << 60 : 0)
781 #define f8_lb(n,r,x) x[n] = (x[n] >> 8) | (n ? x[n-1] << 56 : 0)
787 UNIT_PTR(r)[0] ^= ((gf_unit_t)_tt)<< 48;
794 UNIT_PTR(x)[0] ^= ((gf_unit_t)_tt) << 48;
801 UNIT_PTR(x)[0] ^= ((gf_unit_t)_tt) << 48;
804 #elif UNIT_BITS == 32
806 #define f1_lb(n,r,x) r[n] = (x[n] >> 1) | (n ? x[n-1] << 31 : 0)
807 #define f4_lb(n,r,x) r[n] = (x[n] >> 4) | (n ? x[n-1] << 28 : 0)
808 #define f8_lb(n,r,x) r[n] = (x[n] >> 8) | (n ? x[n-1] << 24 : 0)
814 UNIT_PTR(r)[0] ^= ((gf_unit_t)_tt) << 16;
821 UNIT_PTR(x)[0] ^= ((gf_unit_t)_tt) << 16;
828 UNIT_PTR(x)[0] ^= ((gf_unit_t)_tt) << 16;
833 #define f1_lb(n,r,x) r[n] = (x[n] >> 1) | (n ? x[n-1] << 7 : 0)
834 #define f4_lb(n,r,x) r[n] = (x[n] >> 4) | (n ? x[n-1] << 4 : 0)
862 # error Platform byte order has not been set.