Absolute File Name: | /home/opencoverage/opencoverage/guest-scripts/openssh/src/umac.c |
Switch to Source code | Preprocessed file |
Line | Source | Count | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | - | |||||||||||||
2 | - | |||||||||||||
3 | - | |||||||||||||
4 | - | |||||||||||||
5 | - | |||||||||||||
6 | - | |||||||||||||
7 | - | |||||||||||||
8 | - | |||||||||||||
9 | - | |||||||||||||
10 | - | |||||||||||||
11 | typedef u_int8_t UINT8; | - | ||||||||||||
12 | typedef u_int16_t UINT16; | - | ||||||||||||
13 | typedef u_int32_t UINT32; | - | ||||||||||||
14 | typedef u_int64_t UINT64; | - | ||||||||||||
15 | typedef unsigned int UWORD; | - | ||||||||||||
16 | - | |||||||||||||
17 | - | |||||||||||||
18 | - | |||||||||||||
19 | typedef AES_KEY aes_int_key[1]; | - | ||||||||||||
20 | static void kdf(void *bufp, aes_int_key key, UINT8 ndx, int nbytes) | - | ||||||||||||
21 | { | - | ||||||||||||
22 | UINT8 in_buf[16] = {0}; | - | ||||||||||||
23 | UINT8 out_buf[16]; | - | ||||||||||||
24 | UINT8 *dst_buf = (UINT8 *)bufp; | - | ||||||||||||
25 | int i; | - | ||||||||||||
26 | - | |||||||||||||
27 | - | |||||||||||||
28 | in_buf[16 -9] = ndx; | - | ||||||||||||
29 | in_buf[16 -1] = i = 1; | - | ||||||||||||
30 | - | |||||||||||||
31 | while (nbytes >= 16
| 0 | ||||||||||||
32 | AES_encrypt((u_char *)(in_buf),(u_char *)(out_buf),(AES_KEY *)key); | - | ||||||||||||
33 | memcpy(dst_buf,out_buf,16); | - | ||||||||||||
34 | in_buf[16 -1] = ++i; | - | ||||||||||||
35 | nbytes -= 16; | - | ||||||||||||
36 | dst_buf += 16; | - | ||||||||||||
37 | } never executed: end of block | 0 | ||||||||||||
38 | if (nbytes
| 0 | ||||||||||||
39 | AES_encrypt((u_char *)(in_buf),(u_char *)(out_buf),(AES_KEY *)key); | - | ||||||||||||
40 | memcpy(dst_buf,out_buf,nbytes); | - | ||||||||||||
41 | } never executed: end of block | 0 | ||||||||||||
42 | explicit_bzero(in_buf, sizeof(in_buf)); | - | ||||||||||||
43 | explicit_bzero(out_buf, sizeof(out_buf)); | - | ||||||||||||
44 | } never executed: end of block | 0 | ||||||||||||
45 | - | |||||||||||||
46 | - | |||||||||||||
47 | - | |||||||||||||
48 | - | |||||||||||||
49 | - | |||||||||||||
50 | - | |||||||||||||
51 | - | |||||||||||||
52 | typedef struct { | - | ||||||||||||
53 | UINT8 cache[16]; | - | ||||||||||||
54 | UINT8 nonce[16]; | - | ||||||||||||
55 | aes_int_key prf_key; | - | ||||||||||||
56 | } pdf_ctx; | - | ||||||||||||
57 | - | |||||||||||||
58 | static void pdf_init(pdf_ctx *pc, aes_int_key prf_key) | - | ||||||||||||
59 | { | - | ||||||||||||
60 | UINT8 buf[16]; | - | ||||||||||||
61 | - | |||||||||||||
62 | kdf(buf, prf_key, 0, 16); | - | ||||||||||||
63 | AES_set_encrypt_key((const u_char *)(buf),16*8,pc->prf_key); | - | ||||||||||||
64 | - | |||||||||||||
65 | - | |||||||||||||
66 | memset(pc->nonce, 0, sizeof(pc->nonce)); | - | ||||||||||||
67 | AES_encrypt((u_char *)(pc->nonce),(u_char *)(pc->cache),(AES_KEY *)pc->prf_key); | - | ||||||||||||
68 | explicit_bzero(buf, sizeof(buf)); | - | ||||||||||||
69 | } never executed: end of block | 0 | ||||||||||||
70 | - | |||||||||||||
71 | static void pdf_gen_xor(pdf_ctx *pc, const UINT8 nonce[8], UINT8 buf[8]) | - | ||||||||||||
72 | { | - | ||||||||||||
73 | union { | - | ||||||||||||
74 | UINT8 tmp_nonce_lo[4]; | - | ||||||||||||
75 | UINT32 align; | - | ||||||||||||
76 | } t; | - | ||||||||||||
77 | - | |||||||||||||
78 | int ndx = nonce[7] & 1; | - | ||||||||||||
79 | - | |||||||||||||
80 | *(UINT32 *)t.tmp_nonce_lo = ((const UINT32 *)nonce)[1]; | - | ||||||||||||
81 | t.tmp_nonce_lo[3] &= ~1; | - | ||||||||||||
82 | - | |||||||||||||
83 | if ( (((
| 0 | ||||||||||||
84 | (((const
| 0 | ||||||||||||
85 | { | - | ||||||||||||
86 | ((UINT32 *)pc->nonce)[0] = ((const UINT32 *)nonce)[0]; | - | ||||||||||||
87 | ((UINT32 *)pc->nonce)[1] = ((UINT32 *)t.tmp_nonce_lo)[0]; | - | ||||||||||||
88 | AES_encrypt((u_char *)(pc->nonce),(u_char *)(pc->cache),(AES_KEY *)pc->prf_key); | - | ||||||||||||
89 | } never executed: end of block | 0 | ||||||||||||
90 | - | |||||||||||||
91 | - | |||||||||||||
92 | - | |||||||||||||
93 | - | |||||||||||||
94 | *((UINT64 *)buf) ^= ((UINT64 *)pc->cache)[ndx]; | - | ||||||||||||
95 | - | |||||||||||||
96 | - | |||||||||||||
97 | - | |||||||||||||
98 | - | |||||||||||||
99 | - | |||||||||||||
100 | - | |||||||||||||
101 | - | |||||||||||||
102 | } never executed: end of block | 0 | ||||||||||||
103 | typedef struct { | - | ||||||||||||
104 | UINT8 nh_key [1024 + 16 * ((8 / 4) - 1)]; | - | ||||||||||||
105 | UINT8 data [64]; | - | ||||||||||||
106 | int next_data_empty; | - | ||||||||||||
107 | int bytes_hashed; | - | ||||||||||||
108 | UINT64 state[(8 / 4)]; | - | ||||||||||||
109 | } nh_ctx; | - | ||||||||||||
110 | static void nh_aux(void *kp, const void *dp, void *hp, UINT32 dlen) | - | ||||||||||||
111 | - | |||||||||||||
112 | - | |||||||||||||
113 | - | |||||||||||||
114 | { | - | ||||||||||||
115 | UINT64 h1,h2; | - | ||||||||||||
116 | UWORD c = dlen / 32; | - | ||||||||||||
117 | UINT32 *k = (UINT32 *)kp; | - | ||||||||||||
118 | const UINT32 *d = (const UINT32 *)dp; | - | ||||||||||||
119 | UINT32 d0,d1,d2,d3,d4,d5,d6,d7; | - | ||||||||||||
120 | UINT32 k0,k1,k2,k3,k4,k5,k6,k7, | - | ||||||||||||
121 | k8,k9,k10,k11; | - | ||||||||||||
122 | - | |||||||||||||
123 | h1 = *((UINT64 *)hp); | - | ||||||||||||
124 | h2 = *((UINT64 *)hp + 1); | - | ||||||||||||
125 | k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3); | - | ||||||||||||
126 | do { | - | ||||||||||||
127 | d0 = (get_u32_le(d+0)); d1 = (get_u32_le(d+1)); | - | ||||||||||||
128 | d2 = (get_u32_le(d+2)); d3 = (get_u32_le(d+3)); | - | ||||||||||||
129 | d4 = (get_u32_le(d+4)); d5 = (get_u32_le(d+5)); | - | ||||||||||||
130 | d6 = (get_u32_le(d+6)); d7 = (get_u32_le(d+7)); | - | ||||||||||||
131 | k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7); | - | ||||||||||||
132 | k8 = *(k+8); k9 = *(k+9); k10 = *(k+10); k11 = *(k+11); | - | ||||||||||||
133 | - | |||||||||||||
134 | h1 += ((UINT64)((UINT64)(UINT32)((k0 + d0)) * (UINT64)(UINT32)((k4 + d4)))); | - | ||||||||||||
135 | h2 += ((UINT64)((UINT64)(UINT32)((k4 + d0)) * (UINT64)(UINT32)((k8 + d4)))); | - | ||||||||||||
136 | - | |||||||||||||
137 | h1 += ((UINT64)((UINT64)(UINT32)((k1 + d1)) * (UINT64)(UINT32)((k5 + d5)))); | - | ||||||||||||
138 | h2 += ((UINT64)((UINT64)(UINT32)((k5 + d1)) * (UINT64)(UINT32)((k9 + d5)))); | - | ||||||||||||
139 | - | |||||||||||||
140 | h1 += ((UINT64)((UINT64)(UINT32)((k2 + d2)) * (UINT64)(UINT32)((k6 + d6)))); | - | ||||||||||||
141 | h2 += ((UINT64)((UINT64)(UINT32)((k6 + d2)) * (UINT64)(UINT32)((k10 + d6)))); | - | ||||||||||||
142 | - | |||||||||||||
143 | h1 += ((UINT64)((UINT64)(UINT32)((k3 + d3)) * (UINT64)(UINT32)((k7 + d7)))); | - | ||||||||||||
144 | h2 += ((UINT64)((UINT64)(UINT32)((k7 + d3)) * (UINT64)(UINT32)((k11 + d7)))); | - | ||||||||||||
145 | - | |||||||||||||
146 | k0 = k8; k1 = k9; k2 = k10; k3 = k11; | - | ||||||||||||
147 | - | |||||||||||||
148 | d += 8; | - | ||||||||||||
149 | k += 8; | - | ||||||||||||
150 | } never executed: while (--end of block
| 0 | ||||||||||||
151 | ((UINT64 *)hp)[0] = h1; | - | ||||||||||||
152 | ((UINT64 *)hp)[1] = h2; | - | ||||||||||||
153 | } never executed: end of block | 0 | ||||||||||||
154 | static void nh_transform(nh_ctx *hc, const UINT8 *buf, UINT32 nbytes) | - | ||||||||||||
155 | - | |||||||||||||
156 | - | |||||||||||||
157 | - | |||||||||||||
158 | - | |||||||||||||
159 | - | |||||||||||||
160 | { | - | ||||||||||||
161 | UINT8 *key; | - | ||||||||||||
162 | - | |||||||||||||
163 | key = hc->nh_key + hc->bytes_hashed; | - | ||||||||||||
164 | nh_aux(key, buf, hc->state, nbytes); | - | ||||||||||||
165 | } never executed: end of block | 0 | ||||||||||||
166 | - | |||||||||||||
167 | - | |||||||||||||
168 | - | |||||||||||||
169 | - | |||||||||||||
170 | static void endian_convert(void *buf, UWORD bpw, UINT32 num_bytes) | - | ||||||||||||
171 | - | |||||||||||||
172 | - | |||||||||||||
173 | { | - | ||||||||||||
174 | UWORD iters = num_bytes / bpw; | - | ||||||||||||
175 | if (bpw == 4
| 0 | ||||||||||||
176 | UINT32 *p = (UINT32 *)buf; | - | ||||||||||||
177 | do { | - | ||||||||||||
178 | *p = get_u32(p); | - | ||||||||||||
179 | p++; | - | ||||||||||||
180 | } never executed: while (--end of block
| 0 | ||||||||||||
181 | } never executed: else if (bpw == 8end of block
| 0 | ||||||||||||
182 | UINT32 *p = (UINT32 *)buf; | - | ||||||||||||
183 | UINT32 t; | - | ||||||||||||
184 | do { | - | ||||||||||||
185 | t = get_u32(p+1); | - | ||||||||||||
186 | p[1] = get_u32(p); | - | ||||||||||||
187 | p[0] = t; | - | ||||||||||||
188 | p += 2; | - | ||||||||||||
189 | } never executed: while (--end of block
| 0 | ||||||||||||
190 | } never executed: end of block | 0 | ||||||||||||
191 | } never executed: end of block | 0 | ||||||||||||
192 | - | |||||||||||||
193 | - | |||||||||||||
194 | - | |||||||||||||
195 | - | |||||||||||||
196 | - | |||||||||||||
197 | - | |||||||||||||
198 | - | |||||||||||||
199 | static void nh_reset(nh_ctx *hc) | - | ||||||||||||
200 | - | |||||||||||||
201 | { | - | ||||||||||||
202 | hc->bytes_hashed = 0; | - | ||||||||||||
203 | hc->next_data_empty = 0; | - | ||||||||||||
204 | hc->state[0] = 0; | - | ||||||||||||
205 | - | |||||||||||||
206 | hc->state[1] = 0; | - | ||||||||||||
207 | } never executed: end of block | 0 | ||||||||||||
208 | - | |||||||||||||
209 | - | |||||||||||||
210 | - | |||||||||||||
211 | static void nh_init(nh_ctx *hc, aes_int_key prf_key) | - | ||||||||||||
212 | - | |||||||||||||
213 | { | - | ||||||||||||
214 | kdf(hc->nh_key, prf_key, 1, sizeof(hc->nh_key)); | - | ||||||||||||
215 | endian_convert((hc->nh_key),(4),(sizeof(hc->nh_key))); | - | ||||||||||||
216 | nh_reset(hc); | - | ||||||||||||
217 | } never executed: end of block | 0 | ||||||||||||
218 | - | |||||||||||||
219 | - | |||||||||||||
220 | - | |||||||||||||
221 | static void nh_update(nh_ctx *hc, const UINT8 *buf, UINT32 nbytes) | - | ||||||||||||
222 | - | |||||||||||||
223 | - | |||||||||||||
224 | { | - | ||||||||||||
225 | UINT32 i,j; | - | ||||||||||||
226 | - | |||||||||||||
227 | j = hc->next_data_empty; | - | ||||||||||||
228 | if ((
| 0 | ||||||||||||
229 | if (j
| 0 | ||||||||||||
230 | i = 64 - j; | - | ||||||||||||
231 | memcpy(hc->data+j, buf, i); | - | ||||||||||||
232 | nh_transform(hc,hc->data,64); | - | ||||||||||||
233 | nbytes -= i; | - | ||||||||||||
234 | buf += i; | - | ||||||||||||
235 | hc->bytes_hashed += 64; | - | ||||||||||||
236 | } never executed: end of block | 0 | ||||||||||||
237 | if (nbytes >= 64
| 0 | ||||||||||||
238 | i = nbytes & ~(64 - 1); | - | ||||||||||||
239 | nh_transform(hc, buf, i); | - | ||||||||||||
240 | nbytes -= i; | - | ||||||||||||
241 | buf += i; | - | ||||||||||||
242 | hc->bytes_hashed += i; | - | ||||||||||||
243 | } never executed: end of block | 0 | ||||||||||||
244 | j = 0; | - | ||||||||||||
245 | } never executed: end of block | 0 | ||||||||||||
246 | memcpy(hc->data + j, buf, nbytes); | - | ||||||||||||
247 | hc->next_data_empty = j + nbytes; | - | ||||||||||||
248 | } never executed: end of block | 0 | ||||||||||||
249 | - | |||||||||||||
250 | - | |||||||||||||
251 | - | |||||||||||||
252 | static void zero_pad(UINT8 *p, int nbytes) | - | ||||||||||||
253 | { | - | ||||||||||||
254 | - | |||||||||||||
255 | if (nbytes >= (int)sizeof(UWORD)
| 0 | ||||||||||||
256 | while ((
| 0 | ||||||||||||
257 | *p = 0; | - | ||||||||||||
258 | nbytes--; | - | ||||||||||||
259 | p++; | - | ||||||||||||
260 | } never executed: end of block | 0 | ||||||||||||
261 | while (nbytes >= (int)sizeof(UWORD)
| 0 | ||||||||||||
262 | *(UWORD *)p = 0; | - | ||||||||||||
263 | nbytes -= sizeof(UWORD); | - | ||||||||||||
264 | p += sizeof(UWORD); | - | ||||||||||||
265 | } never executed: end of block | 0 | ||||||||||||
266 | } never executed: end of block | 0 | ||||||||||||
267 | while (nbytes
| 0 | ||||||||||||
268 | *p = 0; | - | ||||||||||||
269 | nbytes--; | - | ||||||||||||
270 | p++; | - | ||||||||||||
271 | } never executed: end of block | 0 | ||||||||||||
272 | } never executed: end of block | 0 | ||||||||||||
273 | - | |||||||||||||
274 | - | |||||||||||||
275 | - | |||||||||||||
276 | static void nh_final(nh_ctx *hc, UINT8 *result) | - | ||||||||||||
277 | - | |||||||||||||
278 | - | |||||||||||||
279 | - | |||||||||||||
280 | - | |||||||||||||
281 | - | |||||||||||||
282 | - | |||||||||||||
283 | - | |||||||||||||
284 | { | - | ||||||||||||
285 | int nh_len, nbits; | - | ||||||||||||
286 | - | |||||||||||||
287 | if (hc->next_data_empty != 0
| 0 | ||||||||||||
288 | nh_len = ((hc->next_data_empty + (32 - 1)) & | - | ||||||||||||
289 | ~(32 - 1)); | - | ||||||||||||
290 | zero_pad(hc->data + hc->next_data_empty, | - | ||||||||||||
291 | nh_len - hc->next_data_empty); | - | ||||||||||||
292 | nh_transform(hc, hc->data, nh_len); | - | ||||||||||||
293 | hc->bytes_hashed += hc->next_data_empty; | - | ||||||||||||
294 | } never executed: else if (hc->bytes_hashed == 0end of block
| 0 | ||||||||||||
295 | nh_len = 32; | - | ||||||||||||
296 | zero_pad(hc->data, 32); | - | ||||||||||||
297 | nh_transform(hc, hc->data, nh_len); | - | ||||||||||||
298 | } never executed: end of block | 0 | ||||||||||||
299 | - | |||||||||||||
300 | nbits = (hc->bytes_hashed << 3); | - | ||||||||||||
301 | ((UINT64 *)result)[0] = ((UINT64 *)hc->state)[0] + nbits; | - | ||||||||||||
302 | - | |||||||||||||
303 | ((UINT64 *)result)[1] = ((UINT64 *)hc->state)[1] + nbits; | - | ||||||||||||
304 | - | |||||||||||||
305 | - | |||||||||||||
306 | - | |||||||||||||
307 | - | |||||||||||||
308 | - | |||||||||||||
309 | - | |||||||||||||
310 | - | |||||||||||||
311 | nh_reset(hc); | - | ||||||||||||
312 | } never executed: end of block | 0 | ||||||||||||
313 | - | |||||||||||||
314 | - | |||||||||||||
315 | - | |||||||||||||
316 | static void nh(nh_ctx *hc, const UINT8 *buf, UINT32 padded_len, | - | ||||||||||||
317 | UINT32 unpadded_len, UINT8 *result) | - | ||||||||||||
318 | - | |||||||||||||
319 | - | |||||||||||||
320 | - | |||||||||||||
321 | - | |||||||||||||
322 | { | - | ||||||||||||
323 | UINT32 nbits; | - | ||||||||||||
324 | - | |||||||||||||
325 | - | |||||||||||||
326 | nbits = (unpadded_len << 3); | - | ||||||||||||
327 | - | |||||||||||||
328 | ((UINT64 *)result)[0] = nbits; | - | ||||||||||||
329 | - | |||||||||||||
330 | ((UINT64 *)result)[1] = nbits; | - | ||||||||||||
331 | nh_aux(hc->nh_key, buf, result, padded_len); | - | ||||||||||||
332 | } never executed: end of block | 0 | ||||||||||||
333 | typedef struct uhash_ctx { | - | ||||||||||||
334 | nh_ctx hash; | - | ||||||||||||
335 | UINT64 poly_key_8[(8 / 4)]; | - | ||||||||||||
336 | UINT64 poly_accum[(8 / 4)]; | - | ||||||||||||
337 | UINT64 ip_keys[(8 / 4)*4]; | - | ||||||||||||
338 | UINT32 ip_trans[(8 / 4)]; | - | ||||||||||||
339 | UINT32 msg_len; | - | ||||||||||||
340 | - | |||||||||||||
341 | } uhash_ctx; | - | ||||||||||||
342 | typedef struct uhash_ctx *uhash_ctx_t; | - | ||||||||||||
343 | static UINT64 poly64(UINT64 cur, UINT64 key, UINT64 data) | - | ||||||||||||
344 | { | - | ||||||||||||
345 | UINT32 key_hi = (UINT32)(key >> 32), | - | ||||||||||||
346 | key_lo = (UINT32)key, | - | ||||||||||||
347 | cur_hi = (UINT32)(cur >> 32), | - | ||||||||||||
348 | cur_lo = (UINT32)cur, | - | ||||||||||||
349 | x_lo, | - | ||||||||||||
350 | x_hi; | - | ||||||||||||
351 | UINT64 X,T,res; | - | ||||||||||||
352 | - | |||||||||||||
353 | X = ((UINT64)((UINT64)(UINT32)(key_hi) * (UINT64)(UINT32)(cur_lo))) + ((UINT64)((UINT64)(UINT32)(cur_hi) * (UINT64)(UINT32)(key_lo))); | - | ||||||||||||
354 | x_lo = (UINT32)X; | - | ||||||||||||
355 | x_hi = (UINT32)(X >> 32); | - | ||||||||||||
356 | - | |||||||||||||
357 | res = (((UINT64)((UINT64)(UINT32)(key_hi) * (UINT64)(UINT32)(cur_hi))) + x_hi) * 59 + ((UINT64)((UINT64)(UINT32)(key_lo) * (UINT64)(UINT32)(cur_lo))); | - | ||||||||||||
358 | - | |||||||||||||
359 | T = ((UINT64)x_lo << 32); | - | ||||||||||||
360 | res += T; | - | ||||||||||||
361 | if (res < T
| 0 | ||||||||||||
362 | res += 59; never executed: res += 59; | 0 | ||||||||||||
363 | - | |||||||||||||
364 | res += data; | - | ||||||||||||
365 | if (res < data
| 0 | ||||||||||||
366 | res += 59; never executed: res += 59; | 0 | ||||||||||||
367 | - | |||||||||||||
368 | return never executed: res;return res; never executed: return res; | 0 | ||||||||||||
369 | } | - | ||||||||||||
370 | static void poly_hash(uhash_ctx_t hc, UINT32 data_in[]) | - | ||||||||||||
371 | { | - | ||||||||||||
372 | int i; | - | ||||||||||||
373 | UINT64 *data=(UINT64*)data_in; | - | ||||||||||||
374 | - | |||||||||||||
375 | for (i = 0; i < (8 / 4)
| 0 | ||||||||||||
376 | if ((
| 0 | ||||||||||||
377 | hc->poly_accum[i] = poly64(hc->poly_accum[i], | - | ||||||||||||
378 | hc->poly_key_8[i], ((UINT64)0xFFFFFFFFFFFFFFC5ull) - 1); | - | ||||||||||||
379 | hc->poly_accum[i] = poly64(hc->poly_accum[i], | - | ||||||||||||
380 | hc->poly_key_8[i], (data[i] - 59)); | - | ||||||||||||
381 | } never executed: else {end of block | 0 | ||||||||||||
382 | hc->poly_accum[i] = poly64(hc->poly_accum[i], | - | ||||||||||||
383 | hc->poly_key_8[i], data[i]); | - | ||||||||||||
384 | } never executed: end of block | 0 | ||||||||||||
385 | } | - | ||||||||||||
386 | } never executed: end of block | 0 | ||||||||||||
387 | static UINT64 ip_aux(UINT64 t, UINT64 *ipkp, UINT64 data) | - | ||||||||||||
388 | { | - | ||||||||||||
389 | t = t + ipkp[0] * (UINT64)(UINT16)(data >> 48); | - | ||||||||||||
390 | t = t + ipkp[1] * (UINT64)(UINT16)(data >> 32); | - | ||||||||||||
391 | t = t + ipkp[2] * (UINT64)(UINT16)(data >> 16); | - | ||||||||||||
392 | t = t + ipkp[3] * (UINT64)(UINT16)(data); | - | ||||||||||||
393 | - | |||||||||||||
394 | return never executed: t;return t; never executed: return t; | 0 | ||||||||||||
395 | } | - | ||||||||||||
396 | - | |||||||||||||
397 | static UINT32 ip_reduce_p36(UINT64 t) | - | ||||||||||||
398 | { | - | ||||||||||||
399 | - | |||||||||||||
400 | UINT64 ret; | - | ||||||||||||
401 | - | |||||||||||||
402 | ret = (t & ((UINT64)0x0000000FFFFFFFFFull)) + 5 * (t >> 36); | - | ||||||||||||
403 | if (ret >= ((UINT64)0x0000000FFFFFFFFBull)
| 0 | ||||||||||||
404 | ret -= ((UINT64)0x0000000FFFFFFFFBull); never executed: ret -= ((UINT64)0x0000000FFFFFFFFBull); | 0 | ||||||||||||
405 | - | |||||||||||||
406 | - | |||||||||||||
407 | return never executed: (UINT32)(ret);return (UINT32)(ret); never executed: return (UINT32)(ret); | 0 | ||||||||||||
408 | } | - | ||||||||||||
409 | - | |||||||||||||
410 | - | |||||||||||||
411 | - | |||||||||||||
412 | - | |||||||||||||
413 | - | |||||||||||||
414 | - | |||||||||||||
415 | static void ip_short(uhash_ctx_t ahc, UINT8 *nh_res, u_char *res) | - | ||||||||||||
416 | { | - | ||||||||||||
417 | UINT64 t; | - | ||||||||||||
418 | UINT64 *nhp = (UINT64 *)nh_res; | - | ||||||||||||
419 | - | |||||||||||||
420 | t = ip_aux(0,ahc->ip_keys, nhp[0]); | - | ||||||||||||
421 | put_u32((UINT32 *)res+0, ip_reduce_p36(t) ^ ahc->ip_trans[0]); | - | ||||||||||||
422 | - | |||||||||||||
423 | t = ip_aux(0,ahc->ip_keys+4, nhp[1]); | - | ||||||||||||
424 | put_u32((UINT32 *)res+1, ip_reduce_p36(t) ^ ahc->ip_trans[1]); | - | ||||||||||||
425 | } never executed: end of block | 0 | ||||||||||||
426 | - | |||||||||||||
427 | - | |||||||||||||
428 | - | |||||||||||||
429 | - | |||||||||||||
430 | - | |||||||||||||
431 | static void ip_long(uhash_ctx_t ahc, u_char *res) | - | ||||||||||||
432 | { | - | ||||||||||||
433 | int i; | - | ||||||||||||
434 | UINT64 t; | - | ||||||||||||
435 | - | |||||||||||||
436 | for (i = 0; i < (8 / 4)
| 0 | ||||||||||||
437 | - | |||||||||||||
438 | if (ahc->poly_accum[i] >= ((UINT64)0xFFFFFFFFFFFFFFC5ull)
| 0 | ||||||||||||
439 | ahc->poly_accum[i] -= ((UINT64)0xFFFFFFFFFFFFFFC5ull); never executed: ahc->poly_accum[i] -= ((UINT64)0xFFFFFFFFFFFFFFC5ull); | 0 | ||||||||||||
440 | t = ip_aux(0,ahc->ip_keys+(i*4), ahc->poly_accum[i]); | - | ||||||||||||
441 | put_u32((UINT32 *)res+i, ip_reduce_p36(t) ^ ahc->ip_trans[i]) | - | ||||||||||||
442 | ; | - | ||||||||||||
443 | } never executed: end of block | 0 | ||||||||||||
444 | } never executed: end of block | 0 | ||||||||||||
445 | - | |||||||||||||
446 | - | |||||||||||||
447 | - | |||||||||||||
448 | - | |||||||||||||
449 | - | |||||||||||||
450 | - | |||||||||||||
451 | - | |||||||||||||
452 | static int uhash_reset(uhash_ctx_t pc) | - | ||||||||||||
453 | { | - | ||||||||||||
454 | nh_reset(&pc->hash); | - | ||||||||||||
455 | pc->msg_len = 0; | - | ||||||||||||
456 | pc->poly_accum[0] = 1; | - | ||||||||||||
457 | - | |||||||||||||
458 | pc->poly_accum[1] = 1; | - | ||||||||||||
459 | - | |||||||||||||
460 | - | |||||||||||||
461 | - | |||||||||||||
462 | - | |||||||||||||
463 | - | |||||||||||||
464 | - | |||||||||||||
465 | - | |||||||||||||
466 | return never executed: 1;return 1; never executed: return 1; | 0 | ||||||||||||
467 | } | - | ||||||||||||
468 | static void uhash_init(uhash_ctx_t ahc, aes_int_key prf_key) | - | ||||||||||||
469 | { | - | ||||||||||||
470 | int i; | - | ||||||||||||
471 | UINT8 buf[(8*(8 / 4)+4)*sizeof(UINT64)]; | - | ||||||||||||
472 | - | |||||||||||||
473 | - | |||||||||||||
474 | memset(ahc, 0, sizeof(uhash_ctx)); | - | ||||||||||||
475 | - | |||||||||||||
476 | - | |||||||||||||
477 | nh_init(&ahc->hash, prf_key); | - | ||||||||||||
478 | - | |||||||||||||
479 | - | |||||||||||||
480 | kdf(buf, prf_key, 2, sizeof(buf)); | - | ||||||||||||
481 | for (i = 0; i < (8 / 4)
| 0 | ||||||||||||
482 | - | |||||||||||||
483 | - | |||||||||||||
484 | - | |||||||||||||
485 | - | |||||||||||||
486 | memcpy(ahc->poly_key_8+i, buf+24*i, 8); | - | ||||||||||||
487 | endian_convert((ahc->poly_key_8+i),(8),(8)); | - | ||||||||||||
488 | - | |||||||||||||
489 | ahc->poly_key_8[i] &= ((UINT64)0x01ffffffu << 32) + 0x01ffffffu; | - | ||||||||||||
490 | ahc->poly_accum[i] = 1; | - | ||||||||||||
491 | } never executed: end of block | 0 | ||||||||||||
492 | - | |||||||||||||
493 | - | |||||||||||||
494 | kdf(buf, prf_key, 3, sizeof(buf)); | - | ||||||||||||
495 | for (i = 0; i < (8 / 4)
| 0 | ||||||||||||
496 | memcpy(ahc->ip_keys+4*i, buf+(8*i+4)*sizeof(UINT64), never executed: memcpy(ahc->ip_keys+4*i, buf+(8*i+4)*sizeof(UINT64), 4*sizeof(UINT64)); | 0 | ||||||||||||
497 | 4*sizeof(UINT64)); never executed: memcpy(ahc->ip_keys+4*i, buf+(8*i+4)*sizeof(UINT64), 4*sizeof(UINT64)); | 0 | ||||||||||||
498 | endian_convert((ahc->ip_keys),(sizeof(UINT64)),(sizeof(ahc->ip_keys))) | - | ||||||||||||
499 | ; | - | ||||||||||||
500 | for (i = 0; i < (8 / 4)*4
| 0 | ||||||||||||
501 | ahc->ip_keys[i] %= ((UINT64)0x0000000FFFFFFFFBull); never executed: ahc->ip_keys[i] %= ((UINT64)0x0000000FFFFFFFFBull); | 0 | ||||||||||||
502 | - | |||||||||||||
503 | - | |||||||||||||
504 | - | |||||||||||||
505 | kdf(ahc->ip_trans, prf_key, 4, (8 / 4) * sizeof(UINT32)); | - | ||||||||||||
506 | endian_convert((ahc->ip_trans),(sizeof(UINT32)),((8 / 4) * sizeof(UINT32))) | - | ||||||||||||
507 | ; | - | ||||||||||||
508 | explicit_bzero(buf, sizeof(buf)); | - | ||||||||||||
509 | } never executed: end of block | 0 | ||||||||||||
510 | static int uhash_update(uhash_ctx_t ctx, const u_char *input, long len) | - | ||||||||||||
511 | - | |||||||||||||
512 | - | |||||||||||||
513 | - | |||||||||||||
514 | { | - | ||||||||||||
515 | UWORD bytes_hashed, bytes_remaining; | - | ||||||||||||
516 | UINT64 result_buf[(8 / 4)]; | - | ||||||||||||
517 | UINT8 *nh_result = (UINT8 *)&result_buf; | - | ||||||||||||
518 | - | |||||||||||||
519 | if (ctx->msg_len + len <= 1024
| 0 | ||||||||||||
520 | nh_update(&ctx->hash, (const UINT8 *)input, len); | - | ||||||||||||
521 | ctx->msg_len += len; | - | ||||||||||||
522 | } never executed: else {end of block | 0 | ||||||||||||
523 | - | |||||||||||||
524 | bytes_hashed = ctx->msg_len % 1024; | - | ||||||||||||
525 | if (ctx->msg_len == 1024
| 0 | ||||||||||||
526 | bytes_hashed = 1024; never executed: bytes_hashed = 1024; | 0 | ||||||||||||
527 | - | |||||||||||||
528 | if (bytes_hashed + len >= 1024
| 0 | ||||||||||||
529 | - | |||||||||||||
530 | - | |||||||||||||
531 | - | |||||||||||||
532 | - | |||||||||||||
533 | if (bytes_hashed
| 0 | ||||||||||||
534 | bytes_remaining = (1024 - bytes_hashed); | - | ||||||||||||
535 | nh_update(&ctx->hash, (const UINT8 *)input, bytes_remaining); | - | ||||||||||||
536 | nh_final(&ctx->hash, nh_result); | - | ||||||||||||
537 | ctx->msg_len += bytes_remaining; | - | ||||||||||||
538 | poly_hash(ctx,(UINT32 *)nh_result); | - | ||||||||||||
539 | len -= bytes_remaining; | - | ||||||||||||
540 | input += bytes_remaining; | - | ||||||||||||
541 | } never executed: end of block | 0 | ||||||||||||
542 | - | |||||||||||||
543 | - | |||||||||||||
544 | while (len >= 1024
| 0 | ||||||||||||
545 | nh(&ctx->hash, (const UINT8 *)input, 1024, | - | ||||||||||||
546 | 1024, nh_result); | - | ||||||||||||
547 | ctx->msg_len += 1024; | - | ||||||||||||
548 | len -= 1024; | - | ||||||||||||
549 | input += 1024; | - | ||||||||||||
550 | poly_hash(ctx,(UINT32 *)nh_result); | - | ||||||||||||
551 | } never executed: end of block | 0 | ||||||||||||
552 | } never executed: end of block | 0 | ||||||||||||
553 | - | |||||||||||||
554 | - | |||||||||||||
555 | if (len
| 0 | ||||||||||||
556 | nh_update(&ctx->hash, (const UINT8 *)input, len); | - | ||||||||||||
557 | ctx->msg_len += len; | - | ||||||||||||
558 | } never executed: end of block | 0 | ||||||||||||
559 | } never executed: end of block | 0 | ||||||||||||
560 | - | |||||||||||||
561 | return never executed: (1);return (1); never executed: return (1); | 0 | ||||||||||||
562 | } | - | ||||||||||||
563 | - | |||||||||||||
564 | - | |||||||||||||
565 | - | |||||||||||||
566 | static int uhash_final(uhash_ctx_t ctx, u_char *res) | - | ||||||||||||
567 | - | |||||||||||||
568 | { | - | ||||||||||||
569 | UINT64 result_buf[(8 / 4)]; | - | ||||||||||||
570 | UINT8 *nh_result = (UINT8 *)&result_buf; | - | ||||||||||||
571 | - | |||||||||||||
572 | if (ctx->msg_len > 1024
| 0 | ||||||||||||
573 | if (ctx->msg_len % 1024
| 0 | ||||||||||||
574 | nh_final(&ctx->hash, nh_result); | - | ||||||||||||
575 | poly_hash(ctx,(UINT32 *)nh_result); | - | ||||||||||||
576 | } never executed: end of block | 0 | ||||||||||||
577 | ip_long(ctx, res); | - | ||||||||||||
578 | } never executed: else {end of block | 0 | ||||||||||||
579 | nh_final(&ctx->hash, nh_result); | - | ||||||||||||
580 | ip_short(ctx,nh_result, res); | - | ||||||||||||
581 | } never executed: end of block | 0 | ||||||||||||
582 | uhash_reset(ctx); | - | ||||||||||||
583 | return never executed: (1);return (1); never executed: return (1); | 0 | ||||||||||||
584 | } | - | ||||||||||||
585 | struct umac_ctx { | - | ||||||||||||
586 | uhash_ctx hash; | - | ||||||||||||
587 | pdf_ctx pdf; | - | ||||||||||||
588 | void *free_ptr; | - | ||||||||||||
589 | } umac_ctx; | - | ||||||||||||
590 | int umac_delete(struct umac_ctx *ctx) | - | ||||||||||||
591 | - | |||||||||||||
592 | { | - | ||||||||||||
593 | if (ctx
| 0 | ||||||||||||
594 | if (16) | - | ||||||||||||
595 | ctx = (struct umac_ctx *)ctx->free_ptr; never executed: ctx = (struct umac_ctx *)ctx->free_ptr; | 0 | ||||||||||||
596 | explicit_bzero(ctx, sizeof(*ctx) + 16); | - | ||||||||||||
597 | free(ctx); | - | ||||||||||||
598 | } never executed: end of block | 0 | ||||||||||||
599 | return never executed: (1);return (1); never executed: return (1); | 0 | ||||||||||||
600 | } | - | ||||||||||||
601 | - | |||||||||||||
602 | - | |||||||||||||
603 | - | |||||||||||||
604 | struct umac_ctx *umac_new(const u_char key[]) | - | ||||||||||||
605 | - | |||||||||||||
606 | - | |||||||||||||
607 | - | |||||||||||||
608 | { | - | ||||||||||||
609 | struct umac_ctx *ctx, *octx; | - | ||||||||||||
610 | size_t bytes_to_add; | - | ||||||||||||
611 | aes_int_key prf_key; | - | ||||||||||||
612 | - | |||||||||||||
613 | octx = ctx = xcalloc(1, sizeof(*ctx) + 16); | - | ||||||||||||
614 | if (ctx
| 0 | ||||||||||||
615 | if (16) { | - | ||||||||||||
616 | bytes_to_add = 16 - | - | ||||||||||||
617 | ((ptrdiff_t)ctx & (16 - 1)); | - | ||||||||||||
618 | ctx = (struct umac_ctx *)((u_char *)ctx + bytes_to_add); | - | ||||||||||||
619 | } never executed: end of block | 0 | ||||||||||||
620 | ctx->free_ptr = octx; | - | ||||||||||||
621 | AES_set_encrypt_key((const u_char *)(key),16*8,prf_key); | - | ||||||||||||
622 | pdf_init(&ctx->pdf, prf_key); | - | ||||||||||||
623 | uhash_init(&ctx->hash, prf_key); | - | ||||||||||||
624 | explicit_bzero(prf_key, sizeof(prf_key)); | - | ||||||||||||
625 | } never executed: end of block | 0 | ||||||||||||
626 | - | |||||||||||||
627 | return never executed: (ctx);return (ctx); never executed: return (ctx); | 0 | ||||||||||||
628 | } | - | ||||||||||||
629 | - | |||||||||||||
630 | - | |||||||||||||
631 | - | |||||||||||||
632 | int umac_final(struct umac_ctx *ctx, u_char tag[], const u_char nonce[8]) | - | ||||||||||||
633 | - | |||||||||||||
634 | { | - | ||||||||||||
635 | uhash_final(&ctx->hash, (u_char *)tag); | - | ||||||||||||
636 | pdf_gen_xor(&ctx->pdf, (const UINT8 *)nonce, (UINT8 *)tag); | - | ||||||||||||
637 | - | |||||||||||||
638 | return never executed: (1);return (1); never executed: return (1); | 0 | ||||||||||||
639 | } | - | ||||||||||||
640 | - | |||||||||||||
641 | - | |||||||||||||
642 | - | |||||||||||||
643 | int umac_update(struct umac_ctx *ctx, const u_char *input, long len) | - | ||||||||||||
644 | - | |||||||||||||
645 | - | |||||||||||||
646 | - | |||||||||||||
647 | { | - | ||||||||||||
648 | uhash_update(&ctx->hash, input, len); | - | ||||||||||||
649 | return never executed: (1);return (1); never executed: return (1); | 0 | ||||||||||||
650 | } | - | ||||||||||||
Switch to Source code | Preprocessed file |