]> www.wagner.pp.ru Git - openssl-gost/engine.git/blob - gost_grasshopper_math.h
tcl_tests: ca.try: Ignore openssl crl exit status for 'corrupted CRL' test
[openssl-gost/engine.git] / gost_grasshopper_math.h
1 /*
2  * Maxim Tishkov 2016
3  * This file is distributed under the same license as OpenSSL
4  */
5
6 #ifndef GOST_GRASSHOPPER_MATH_H
7 #define GOST_GRASSHOPPER_MATH_H
8
9 #if defined(__cplusplus)
10 extern "C" {
11 #endif
12
13 #include "gost_grasshopper_defines.h"
14
15 #if defined(__SSE__) || defined(__SSE2__) || defined(__SSE2_MATH__) || defined(__SSE3__) || defined(__SSE_MATH__) \
16  || defined(__SSE4_1__)|| defined(__SSE4_2__)|| defined(__SSSE3__)
17 #define GRASSHOPPER_SSE_SUPPORTED
18 #endif
19
20 #define GRASSHOPPER_MIN_BITS 8
21 #define GRASSHOPPER_MAX_BITS 128
22
23 #if UINTPTR_MAX == 0xff
24 #define GRASSHOPPER_BITS 8
25 #elif UINTPTR_MAX == 0xffff
26 #define GRASSHOPPER_BITS 16
27 #elif UINTPTR_MAX == 0xffffffff
28 #define GRASSHOPPER_BITS 32
29 #elif UINTPTR_MAX == 0xffffffffffffffff
30 #define GRASSHOPPER_BITS 64
31 #endif
32
33 #define GRASSHOPPER_BIT_PARTS_8 (GRASSHOPPER_MAX_BITS / 8)
34 #define GRASSHOPPER_BIT_PARTS_16 (GRASSHOPPER_MAX_BITS / 16)
35 #define GRASSHOPPER_BIT_PARTS_32 (GRASSHOPPER_MAX_BITS / 32)
36 #define GRASSHOPPER_BIT_PARTS_64 (GRASSHOPPER_MAX_BITS / 64)
37
38 #define GRASSHOPPER_BIT_PARTS (GRASSHOPPER_MAX_BITS / GRASSHOPPER_BITS)
39 #define GRASSHOPPER_MAX_BIT_PARTS (GRASSHOPPER_MAX_BITS / GRASSHOPPER_MIN_BITS)
40
41 #define GRASSHOPPER_ACCESS_128_VALUE_8(key, part) ((key).b[(part)])
42 #define GRASSHOPPER_ACCESS_128_VALUE_16(key, part) ((key).w[(part)])
43 #define GRASSHOPPER_ACCESS_128_VALUE_32(key, part) ((key).d[(part)])
44 #define GRASSHOPPER_ACCESS_128_VALUE_64(key, part) ((key).q[(part)])
45
46 #if(GRASSHOPPER_BITS == 8)
47 #define GRASSHOPPER_ACCESS_128_VALUE GRASSHOPPER_ACCESS_128_VALUE_8
48 #elif(GRASSHOPPER_BITS == 16)
49 #define GRASSHOPPER_ACCESS_128_VALUE GRASSHOPPER_ACCESS_128_VALUE_16
50 #elif(GRASSHOPPER_BITS == 32)
51 #define GRASSHOPPER_ACCESS_128_VALUE GRASSHOPPER_ACCESS_128_VALUE_32
52 #elif(GRASSHOPPER_BITS == 64)
53 #define GRASSHOPPER_ACCESS_128_VALUE GRASSHOPPER_ACCESS_128_VALUE_64
54 #endif
55
56 static GRASSHOPPER_INLINE void grasshopper_zero128(grasshopper_w128_t* x) {
57 #if(GRASSHOPPER_BITS == 8 || GRASSHOPPER_BITS == 16)
58     memset(&x, 0, sizeof(x));
59 #else
60                 int i;
61     for (i = 0; i < GRASSHOPPER_BIT_PARTS; i++) {
62         GRASSHOPPER_ACCESS_128_VALUE(*x, i) = 0;
63     }
64 #endif
65 }
66
67 static GRASSHOPPER_INLINE void grasshopper_copy128(grasshopper_w128_t* to, const grasshopper_w128_t* from) {
68 #if(GRASSHOPPER_BITS == 8 || GRASSHOPPER_BITS == 16)
69     __builtin_memcpy(&to, &from, sizeof(grasshopper_w128_t));
70 #else
71                 int i;
72     for (i = 0; i < GRASSHOPPER_BIT_PARTS; i++) {
73         GRASSHOPPER_ACCESS_128_VALUE(*to, i) = GRASSHOPPER_ACCESS_128_VALUE(*from, i);
74     }
75 #endif
76 }
77
78 static GRASSHOPPER_INLINE void grasshopper_append128(grasshopper_w128_t* x, const grasshopper_w128_t* y) {
79                 int i;
80 #ifdef STRICT_ALIGNMENT
81     for (i = 0; i < 16; i++) {
82         GRASSHOPPER_ACCESS_128_VALUE_8(*x, i) ^= GRASSHOPPER_ACCESS_128_VALUE_8(*y, i);
83     }
84 #else
85     for (i = 0; i < GRASSHOPPER_BIT_PARTS; i++) {
86         GRASSHOPPER_ACCESS_128_VALUE(*x, i) ^= GRASSHOPPER_ACCESS_128_VALUE(*y, i);
87     }
88 #endif
89 }
90
91 static GRASSHOPPER_INLINE void grasshopper_plus128(grasshopper_w128_t* result, const grasshopper_w128_t* x,
92                                                const grasshopper_w128_t* y) {
93     grasshopper_copy128(result, x);
94     grasshopper_append128(result, y);
95 }
96
97 // result & x must be different
98 static GRASSHOPPER_INLINE void grasshopper_plus128multi(grasshopper_w128_t* result, const grasshopper_w128_t* x,
99                                                     const grasshopper_w128_t array[][256]) {
100                 int i;
101     grasshopper_zero128(result);
102     for (i = 0; i < GRASSHOPPER_MAX_BIT_PARTS; i++) {
103         grasshopper_append128(result, &array[i][GRASSHOPPER_ACCESS_128_VALUE_8(*x, i)]);
104     }
105 }
106
107 static GRASSHOPPER_INLINE void grasshopper_append128multi(grasshopper_w128_t* result, grasshopper_w128_t* x,
108                                                       const grasshopper_w128_t array[][256]) {
109     grasshopper_plus128multi(result, x, array);
110     grasshopper_copy128(x, result);
111 }
112
113 static GRASSHOPPER_INLINE void grasshopper_convert128(grasshopper_w128_t* x, const uint8_t* array) {
114                 int i;
115     for (i = 0; i < GRASSHOPPER_MAX_BIT_PARTS; i++) {
116         GRASSHOPPER_ACCESS_128_VALUE_8(*x, i) = array[GRASSHOPPER_ACCESS_128_VALUE_8(*x, i)];
117     }
118 }
119
120 #define GRASSHOPPER_GALOIS_POWER 8
121
122 #define GRASSHOPPER_GALOIS_FIELD_SIZE ((1 << GRASSHOPPER_GALOIS_POWER) - 1)
123
124 extern uint8_t grasshopper_galois_alpha_to[256];
125 extern uint8_t grasshopper_galois_index_of[256];
126
127 static GRASSHOPPER_INLINE uint8_t grasshopper_galois_mul(uint8_t x, uint8_t y) {
128     if (likely(x != 0 && y != 0)) {
129         return grasshopper_galois_alpha_to[(grasshopper_galois_index_of[x] + grasshopper_galois_index_of[y]) %
130                                          GRASSHOPPER_GALOIS_FIELD_SIZE];
131     } else {
132         return 0;
133     }
134 }
135
136 #if defined(__cplusplus)
137 }
138 #endif
139
140 #endif