Debian Codegraph @

src/ghash_portable.c

raw symbols SCIP
1 /*
2 * galois.c: arithmetic in Galois Fields
3 *
4 * ===================================================================
5 * The contents of this file are dedicated to the public domain. To
6 * the extent that dedication to the public domain is not available,
7 * everyone is granted a worldwide, perpetual, royalty-free,
8 * non-exclusive license to exercise all rights associated with the
9 * contents of this file for any purpose whatsoever.
10 * No rights are reserved.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
13 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
14 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
16 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
17 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 * SOFTWARE.
20 * ===================================================================
21 */
22
23 #include "common.h"
24 #include "endianess.h"
25
26 FAKE_INIT(ghash_portable)
27
28 #define ALIGNMENT 32
29
30 /**
31 * A V table is a 4096 bytes table that will contain the expanded
32 * GHASH key (H). It is used to speed up the GF(128) multiplication Z = X*H.
33 *
34 * The table contains 128 entries, one for each bit of X.
35 * Each entry takes 32 bytes and can fit into the cache line of a modern
36 * processor. If we assume that access to memory mapped to the same
37 * cache line is somewhat constant, we can make GHASH robust again
38 * cache timing attacks.
39 */
40 typedef uint64_t t_v_tables[128][2][2];
41
42 /**
43 * To ensure that the V table is aligned to a 32-byte memory boundary,
44 * we allocate a larger piece of memory and carve the V table from there.
45 */
46 typedef struct {
47 uint8_t buffer[sizeof(t_v_tables)+ALIGNMENT];
48 int offset;
49 } t_exp_key;
50
51 /**
52 * Create a V table. V[i] is the value H*x^i (i=0..127).
53 * \param h The 16 byte GHASH key
54 * \param tables A pointer to an allocated V table
55 */
56 static void make_v_tables(const uint8_t h[16], t_v_tables *tables)
57 {
58 uint64_t (*cur)[2];
59 int i;
60
61 memset(tables, 0, sizeof(t_v_tables));
62
63 cur = &((*tables)[0][1]);
64
65 (*cur)[0] = LOAD_U64_BIG(&h[0]);
66 (*cur)[1] = LOAD_U64_BIG(&h[8]);
67
68 for (i=1; i<128; i++) {
69 uint64_t c;
70 uint64_t (*next)[2];
71
72 next = &((*tables)[i][1]);
73
74 /** v = (v&1)*0xE1000000000000000000000000000000L ^ (v>>1) **/
75 c = (*cur)[1]&1 ? 0xE100000000000000ULL : 0;
76 (*next)[1] = (*cur)[1]>>1 | (*cur)[0]<<63;
77 (*next)[0] = (*cur)[0]>>1 ^ c;
78
79 cur = next;
80 }
81 }
82
83 /**
84 * Multiply two elements of GF(2**128) using the reducing polynomial
85 * (x^128 + x^7 + x^2 + x + 1).
86 *
87 * \param out The 16 byte buffer that will receive the result
88 * \param key_tables One factor, expanded into a V table
89 * \param x The other factor (16 bytes)
90 */
91 static void gcm_mult2(uint8_t out[16], const t_v_tables *key_tables, const uint8_t x[16])
92 {
93 int i, bit_scan_128;
94 uint64_t z[2];
95
96 z[0] = z[1] = 0;
97 bit_scan_128 = 0;
98 for (i=0; i<16; i++) {
99 unsigned xi;
100 int j;
101
102 xi = x[i];
103 for (j=0; j<8; j++) {
104 unsigned bit;
105
106 bit = xi>>7 & 1; /** Constant time */
107 z[0] ^= (*key_tables)[bit_scan_128][bit][0];
108 z[1] ^= (*key_tables)[bit_scan_128][bit][1];
109
110 xi <<= 1;
111 bit_scan_128++;
112 }
113 }
114
115 STORE_U64_BIG(out, z[0]);
116 STORE_U64_BIG(out+8, z[1]);
117 }
118
119 /**
120 * Compute the GHASH of a piece of data given an arbitrary Y_0,
121 * as specified in NIST SP 800 38D.
122 *
123 * \param y_out The resulting GHASH (16 bytes).
124 * \param block_data Pointer to the data to hash.
125 * \param len Length of the data to hash (multiple of 16).
126 * \param y_in The initial Y (Y_0, 16 bytes).
127 * \param exp_key The expanded hash key (16*256*16 bytes + alignment).
128 *
129 * y_out and y_int can point to the same buffer.
130 */
131 EXPORT_SYM int ghash_portable(
132 uint8_t y_out[16],
133 const uint8_t block_data[],
134 size_t len,
135 const uint8_t y_in[16],
136 const t_exp_key *exp_key
137 )
138 {
139 unsigned i;
140 const t_v_tables *v_tables;
141
142 if (NULL==y_out || NULL==block_data || NULL==y_in || NULL==exp_key)
143 return ERR_NULL;
144
145 if (len % 16)
146 return ERR_NOT_ENOUGH_DATA;
147
148 v_tables = (const t_v_tables*)(exp_key->buffer + exp_key->offset);
149
150 memcpy(y_out, y_in, 16);
151 for (i=0; i<len; i+=16) {
152 unsigned j;
153 uint8_t x[16];
154
155 for (j=0; j<16; j++) {
156 x[j] = y_out[j] ^ block_data[i+j];
157 }
158 gcm_mult2(y_out, v_tables, x);
159 }
160
161 return 0;
162 }
163
164 /**
165 * Expand the AES key into a Python (byte) string object.
166 */
167 EXPORT_SYM int ghash_expand_portable(const uint8_t h[16], t_exp_key **ghash_tables)
168 {
169 t_exp_key *exp_key;
170
171 if (NULL==h || NULL==ghash_tables)
172 return ERR_NULL;
173
174 *ghash_tables = exp_key = calloc(1, sizeof(t_exp_key));
175 if (NULL == exp_key)
176 return ERR_MEMORY;
177
178 exp_key->offset = ALIGNMENT - (int)((uintptr_t)exp_key->buffer & (ALIGNMENT-1));
179 make_v_tables(h, (t_v_tables*)(exp_key->buffer + exp_key->offset));
180
181 return 0;
182 }
183
184 EXPORT_SYM int ghash_destroy_portable(t_exp_key *ghash_tables)
185 {
186 free(ghash_tables);
187 return 0;
188 }