Debian Codegraph @

src/modexp_utils.c

raw symbols SCIP
1 #include "modexp_utils.h"
2 #include "siphash.h"
3 #include "endianess.h"
4
5 #include "siphash.c"
6
7 void expand_seed(uint64_t seed_in, void* seed_out, size_t out_len)
8 {
9 uint8_t counter[4];
10 uint8_t seed_in_b[16];
11 uint32_t i;
12
13 for (i=0; i<8; i++) {
14 seed_in_b[2*i] = seed_in_b[2*i+1] = (uint8_t)(seed_in >> (i*8));
15 }
16
17 #define SIPHASH_LEN 16
18
19 for (i=0 ;; i++, out_len-=SIPHASH_LEN) {
20 STORE_U32_LITTLE(counter, i);
21 if (out_len<SIPHASH_LEN)
22 break;
23 siphash(counter, 4, seed_in_b, seed_out, SIPHASH_LEN);
24 seed_out = (uint8_t*)seed_out + SIPHASH_LEN;
25 }
26
27 if (out_len>0) {
28 uint8_t buffer[SIPHASH_LEN];
29 siphash(counter, 4, seed_in_b, buffer, SIPHASH_LEN);
30 memcpy(seed_out, buffer, out_len);
31 }
32
33 #undef SIPHASH_LEN
34 }
35
36 struct BitWindow_LR init_bit_window_lr(unsigned window_size, const uint8_t *exp, size_t exp_len)
37 {
38 struct BitWindow_LR bw;
39
40 bw.window_size = window_size;
41 bw.nr_windows = (unsigned)((exp_len*8+window_size-1)/window_size);
42
43 bw.tg = (unsigned)((exp_len*8) % window_size);
44 if (bw.tg == 0) {
45 bw.tg = window_size;
46 }
47
48 bw.available = 8;
49 bw.scan_exp = 0;
50 bw.exp = exp;
51
52 return bw;
53 }
54
55 struct BitWindow_RL init_bit_window_rl(unsigned window_size, const uint8_t *exp, size_t exp_len)
56 {
57 struct BitWindow_RL bw;
58
59 bw.window_size = window_size;
60 bw.nr_windows = (unsigned)((exp_len*8+window_size-1)/window_size);
61
62 bw.bytes_left = (unsigned)exp_len;
63 bw.bits_left = 8;
64 bw.cursor = exp + (exp_len-1);
65
66 return bw;
67 }
68
69 unsigned get_next_digit_lr(struct BitWindow_LR *bw)
70 {
71 unsigned tc, index;
72
73 /** Possibly move to the next byte **/
74 if (bw->available == 0) {
75 bw->available = 8;
76 bw->scan_exp++;
77 }
78
79 /** Try to consume as much as possible from the current byte **/
80 tc = MIN(bw->tg, bw->available);
81
82 index = ((unsigned)bw->exp[bw->scan_exp] >> ((unsigned)bw->available - tc)) & ((1U << tc) - 1);
83
84 bw->available -= tc;
85 bw->tg -= tc;
86
87 /** A few bits (<8) might still be needed from the next byte **/
88 if (bw->tg > 0) {
89 bw->scan_exp++;
90 index = (index << bw->tg) | ((unsigned)bw->exp[bw->scan_exp] >> (8 - bw->tg));
91 bw->available = 8 - bw->tg;
92 }
93
94 bw->tg = bw->window_size;
95
96 return index;
97 }
98
99 unsigned get_next_digit_rl(struct BitWindow_RL *bw)
100 {
101 unsigned res, tg, bits_used;
102
103 if (bw->bytes_left == 0)
104 return 0;
105
106 assert(bw->bits_left > 0);
107
108 res = (unsigned)(*(bw->cursor) >> (8 - bw->bits_left)) & (unsigned)((1U<<bw->window_size) - 1);
109 bits_used = MIN(bw->bits_left, bw->window_size);
110
111 tg = bw->window_size - bits_used;
112 bw->bits_left -= bits_used;
113
114 if (bw->bits_left == 0) {
115 bw->bits_left = 8;
116 if (--bw->bytes_left == 0)
117 return res;
118 bw->cursor--;
119 }
120
121 if (tg>0) {
122 res |= (*(bw->cursor) & ((1U<<tg) - 1)) << bits_used;
123 bw->bits_left -= tg;
124 }
125
126 return res;
127 }
128
129 #define CACHE_LINE_SIZE 64U
130
131 /**
132 * Spread a number of equally-sized arrays in memory, to minimize cache
133 * side-channel when one of them has to be accessed through a secret index.
134 *
135 * Each array is broken up in pieces, and pieces from all array collected
136 * and fit into one or more 64 byte cache lines. One cache line contains
137 * one piece taken from each array at the same offset.
138 *
139 * We assume that access to a byte in the cache line can be performed without
140 * leaking its position.
141 *
142 * nr_array is a power of two, 64 at most
143 */
144 int scatter(ProtMemory** pprot, const void *arrays[], uint8_t nr_arrays, size_t array_len, uint64_t seed)
145 {
146 ProtMemory *prot;
147 unsigned piece_len;
148 unsigned cache_lines;
149 unsigned remaining;
150 unsigned i, j;
151 unsigned mask;
152
153 if (nr_arrays>CACHE_LINE_SIZE || (nr_arrays & 1) == 1 || array_len == 0)
154 return ERR_VALUE;
155
156 for (i=nr_arrays; (i & 1)==0; i>>=1);
157 if (i != 1)
158 return ERR_VALUE;
159
160 piece_len = CACHE_LINE_SIZE / nr_arrays;
161 cache_lines = ((unsigned)array_len + piece_len - 1) / piece_len;
162
163 *pprot = prot = (ProtMemory*)calloc(1, sizeof(ProtMemory));
164 if (NULL == prot)
165 return ERR_MEMORY;
166
167 prot->scramble = (uint16_t*)calloc(cache_lines, sizeof(prot->scramble[0]));
168 if (NULL == prot->scramble) {
169 free(prot);
170 return ERR_MEMORY;
171 }
172 expand_seed(seed, prot->scramble, cache_lines*sizeof(prot->scramble[0]));
173
174 prot->scattered = (uint8_t*)align_alloc(cache_lines*CACHE_LINE_SIZE, CACHE_LINE_SIZE);
175 if (NULL == prot->scattered) {
176 free(prot->scramble);
177 free(prot);
178 return ERR_MEMORY;
179 }
180
181 prot->nr_arrays = nr_arrays;
182 prot->array_len = (unsigned)array_len;
183
184 remaining = (unsigned)array_len;
185 mask = (unsigned)(nr_arrays - 1);
186
187 for (i=0; i<cache_lines; i++) {
188 uint8_t *cache_line;
189 unsigned offset;
190
191 cache_line = (uint8_t*)prot->scattered + i*CACHE_LINE_SIZE;
192 offset = i*piece_len;
193
194 for (j=0; j<nr_arrays; j++) {
195 unsigned s;
196 unsigned obf;
197 uint8_t *dst, *src;
198
199 obf = (j*((prot->scramble[i] >> 8) | 1) + (prot->scramble[i] & 0xFF)) & mask;
200
201 s = MIN(piece_len, remaining);
202 dst = cache_line + piece_len*obf;
203 src = (uint8_t*)arrays[j] + offset;
204 memcpy(dst, src, s);
205 }
206
207 remaining -= piece_len;
208 }
209
210 return 0;
211 }
212
213 void gather(void *out, const ProtMemory *prot, unsigned index)
214 {
215 unsigned piece_len;
216 unsigned cache_lines;
217 unsigned remaining;
218 unsigned offset;
219 unsigned i;
220 unsigned mask;
221
222 piece_len = CACHE_LINE_SIZE / prot->nr_arrays;
223 cache_lines = (prot->array_len + piece_len - 1) / piece_len;
224
225 remaining = prot->array_len;
226 mask = prot->nr_arrays - 1;
227 offset = 0;
228
229 for (i=0; i<cache_lines; i++) {
230 uint8_t *cache_line;
231 unsigned obf;
232
233 obf = (index*((prot->scramble[i] >> 8) | 1) + (prot->scramble[i] & 0xFF)) & mask;
234
235 cache_line = (uint8_t*)prot->scattered + i*CACHE_LINE_SIZE;
236 memcpy((uint8_t*)out + offset, cache_line + piece_len*obf, MIN(piece_len, remaining));
237
238 remaining -= piece_len;
239 offset += piece_len;
240 }
241 }
242
243 void free_scattered(ProtMemory *prot)
244 {
245 if (prot) {
246 free(prot->scramble);
247 align_free(prot->scattered);
248 }
249 free(prot);
250 }