]>
Commit | Line | Data |
---|---|---|
fda78dbf | 1 | /* $OpenBSD: umac.c,v 1.1 2007/06/07 19:37:34 pvalchev Exp $ */ |
2 | /* ----------------------------------------------------------------------- | |
3 | * | |
4 | * umac.c -- C Implementation UMAC Message Authentication | |
5 | * | |
6 | * Version 0.93b of rfc4418.txt -- 2006 July 18 | |
7 | * | |
8 | * For a full description of UMAC message authentication see the UMAC | |
9 | * world-wide-web page at http://www.cs.ucdavis.edu/~rogaway/umac | |
10 | * Please report bugs and suggestions to the UMAC webpage. | |
11 | * | |
12 | * Copyright (c) 1999-2006 Ted Krovetz | |
13 | * | |
14 | * Permission to use, copy, modify, and distribute this software and | |
15 | * its documentation for any purpose and with or without fee, is hereby | |
16 | * granted provided that the above copyright notice appears in all copies | |
17 | * and in supporting documentation, and that the name of the copyright | |
18 | * holder not be used in advertising or publicity pertaining to | |
19 | * distribution of the software without specific, written prior permission. | |
20 | * | |
21 | * Comments should be directed to Ted Krovetz (tdk@acm.org) | |
22 | * | |
23 | * ---------------------------------------------------------------------- */ | |
24 | ||
25 | /* ////////////////////// IMPORTANT NOTES ///////////////////////////////// | |
26 | * | |
27 | * 1) This version does not work properly on messages larger than 16MB | |
28 | * | |
29 | * 2) If you set the switch to use SSE2, then all data must be 16-byte | |
30 | * aligned | |
31 | * | |
32 | * 3) When calling the function umac(), it is assumed that msg is in | |
33 | * a writable buffer of length divisible by 32 bytes. The message itself | |
34 | * does not have to fill the entire buffer, but bytes beyond msg may be | |
35 | * zeroed. | |
36 | * | |
37 | * 4) Three free AES implementations are supported by this implementation of | |
38 | * UMAC. Paulo Barreto's version is in the public domain and can be found | |
39 | * at http://www.esat.kuleuven.ac.be/~rijmen/rijndael/ (search for | |
40 | * "Barreto"). The only two files needed are rijndael-alg-fst.c and | |
41 | * rijndael-alg-fst.h. Brian Gladman's version is distributed with the GNU | |
42 | * Public lisence at http://fp.gladman.plus.com/AES/index.htm. It | |
43 | * includes a fast IA-32 assembly version. The OpenSSL crypo library is | |
44 | * the third. | |
45 | * | |
46 | * 5) With FORCE_C_ONLY flags set to 0, incorrect results are sometimes | |
47 | * produced under gcc with optimizations set -O3 or higher. Dunno why. | |
48 | * | |
49 | /////////////////////////////////////////////////////////////////////// */ | |
50 | ||
51 | /* ---------------------------------------------------------------------- */ | |
52 | /* --- User Switches ---------------------------------------------------- */ | |
53 | /* ---------------------------------------------------------------------- */ | |
54 | ||
55 | #define UMAC_OUTPUT_LEN 8 /* Alowable: 4, 8, 12, 16 */ | |
56 | /* #define FORCE_C_ONLY 1 ANSI C and 64-bit integers req'd */ | |
57 | /* #define AES_IMPLEMENTAION 1 1 = OpenSSL, 2 = Barreto, 3 = Gladman */ | |
58 | /* #define SSE2 0 Is SSE2 is available? */ | |
59 | /* #define RUN_TESTS 0 Run basic correctness/speed tests */ | |
60 | /* #define UMAC_AE_SUPPORT 0 Enable auhthenticated encrytion */ | |
61 | ||
62 | /* ---------------------------------------------------------------------- */ | |
63 | /* -- Global Includes --------------------------------------------------- */ | |
64 | /* ---------------------------------------------------------------------- */ | |
65 | ||
66 | #include "includes.h" | |
67 | #include <sys/types.h> | |
68 | ||
69 | #include "umac.h" | |
70 | #include <string.h> | |
71 | #include <stdlib.h> | |
72 | #include <stddef.h> | |
73 | ||
74 | /* ---------------------------------------------------------------------- */ | |
75 | /* --- Primitive Data Types --- */ | |
76 | /* ---------------------------------------------------------------------- */ | |
77 | ||
78 | /* The following assumptions may need change on your system */ | |
79 | typedef u_int8_t UINT8; /* 1 byte */ | |
80 | typedef u_int16_t UINT16; /* 2 byte */ | |
81 | typedef u_int32_t UINT32; /* 4 byte */ | |
82 | typedef u_int64_t UINT64; /* 8 bytes */ | |
83 | typedef unsigned int UWORD; /* Register */ | |
84 | ||
85 | /* ---------------------------------------------------------------------- */ | |
86 | /* --- Constants -------------------------------------------------------- */ | |
87 | /* ---------------------------------------------------------------------- */ | |
88 | ||
89 | #define UMAC_KEY_LEN 16 /* UMAC takes 16 bytes of external key */ | |
90 | ||
91 | /* Message "words" are read from memory in an endian-specific manner. */ | |
92 | /* For this implementation to behave correctly, __LITTLE_ENDIAN__ must */ | |
93 | /* be set true if the host computer is little-endian. */ | |
94 | ||
95 | #if BYTE_ORDER == LITTLE_ENDIAN | |
96 | #define __LITTLE_ENDIAN__ 1 | |
97 | #else | |
98 | #define __LITTLE_ENDIAN__ 0 | |
99 | #endif | |
100 | ||
101 | /* ---------------------------------------------------------------------- */ | |
102 | /* ---------------------------------------------------------------------- */ | |
103 | /* ----- Architecture Specific ------------------------------------------ */ | |
104 | /* ---------------------------------------------------------------------- */ | |
105 | /* ---------------------------------------------------------------------- */ | |
106 | ||
107 | ||
108 | /* ---------------------------------------------------------------------- */ | |
109 | /* ---------------------------------------------------------------------- */ | |
110 | /* ----- Primitive Routines --------------------------------------------- */ | |
111 | /* ---------------------------------------------------------------------- */ | |
112 | /* ---------------------------------------------------------------------- */ | |
113 | ||
114 | ||
115 | /* ---------------------------------------------------------------------- */ | |
116 | /* --- 32-bit by 32-bit to 64-bit Multiplication ------------------------ */ | |
117 | /* ---------------------------------------------------------------------- */ | |
118 | ||
119 | #define MUL64(a,b) ((UINT64)((UINT64)(UINT32)(a) * (UINT64)(UINT32)(b))) | |
120 | ||
121 | /* ---------------------------------------------------------------------- */ | |
122 | /* --- Endian Conversion --- Forcing assembly on some platforms */ | |
123 | /* ---------------------------------------------------------------------- */ | |
124 | ||
125 | #if HAVE_SWAP32 | |
126 | #define LOAD_UINT32_REVERSED(p) (swap32(*(UINT32 *)(p))) | |
127 | #define STORE_UINT32_REVERSED(p,v) (*(UINT32 *)(p) = swap32(v)) | |
128 | #else /* HAVE_SWAP32 */ | |
129 | ||
130 | static UINT32 LOAD_UINT32_REVERSED(void *ptr) | |
131 | { | |
132 | UINT32 temp = *(UINT32 *)ptr; | |
133 | temp = (temp >> 24) | ((temp & 0x00FF0000) >> 8 ) | |
134 | | ((temp & 0x0000FF00) << 8 ) | (temp << 24); | |
135 | return (UINT32)temp; | |
136 | } | |
137 | ||
138 | static void STORE_UINT32_REVERSED(void *ptr, UINT32 x) | |
139 | { | |
140 | UINT32 i = (UINT32)x; | |
141 | *(UINT32 *)ptr = (i >> 24) | ((i & 0x00FF0000) >> 8 ) | |
142 | | ((i & 0x0000FF00) << 8 ) | (i << 24); | |
143 | } | |
144 | #endif /* HAVE_SWAP32 */ | |
145 | ||
146 | /* The following definitions use the above reversal-primitives to do the right | |
147 | * thing on endian specific load and stores. | |
148 | */ | |
149 | ||
150 | #if (__LITTLE_ENDIAN__) | |
151 | #define LOAD_UINT32_LITTLE(ptr) (*(UINT32 *)(ptr)) | |
152 | #define STORE_UINT32_BIG(ptr,x) STORE_UINT32_REVERSED(ptr,x) | |
153 | #else | |
154 | #define LOAD_UINT32_LITTLE(ptr) LOAD_UINT32_REVERSED(ptr) | |
155 | #define STORE_UINT32_BIG(ptr,x) (*(UINT32 *)(ptr) = (UINT32)(x)) | |
156 | #endif | |
157 | ||
158 | /* ---------------------------------------------------------------------- */ | |
159 | /* ---------------------------------------------------------------------- */ | |
160 | /* ----- Begin KDF & PDF Section ---------------------------------------- */ | |
161 | /* ---------------------------------------------------------------------- */ | |
162 | /* ---------------------------------------------------------------------- */ | |
163 | ||
164 | /* UMAC uses AES with 16 byte block and key lengths */ | |
165 | #define AES_BLOCK_LEN 16 | |
166 | ||
167 | /* OpenSSL's AES */ | |
168 | #include "openbsd-compat/openssl-compat.h" | |
169 | #ifndef USE_BUILTIN_RIJNDAEL | |
170 | # include <openssl/aes.h> | |
171 | #endif | |
172 | typedef AES_KEY aes_int_key[1]; | |
173 | #define aes_encryption(in,out,int_key) \ | |
174 | AES_encrypt((u_char *)(in),(u_char *)(out),(AES_KEY *)int_key) | |
175 | #define aes_key_setup(key,int_key) \ | |
176 | AES_set_encrypt_key((u_char *)(key),UMAC_KEY_LEN*8,int_key) | |
177 | ||
178 | /* The user-supplied UMAC key is stretched using AES in a counter | |
179 | * mode to supply all random bits needed by UMAC. The kdf function takes | |
180 | * an AES internal key representation 'key' and writes a stream of | |
181 | * 'nbytes' bytes to the memory pointed at by 'buffer_ptr'. Each distinct | |
182 | * 'ndx' causes a distinct byte stream. | |
183 | */ | |
184 | static void kdf(void *buffer_ptr, aes_int_key key, UINT8 ndx, int nbytes) | |
185 | { | |
186 | UINT8 in_buf[AES_BLOCK_LEN] = {0}; | |
187 | UINT8 out_buf[AES_BLOCK_LEN]; | |
188 | UINT8 *dst_buf = (UINT8 *)buffer_ptr; | |
189 | int i; | |
190 | ||
191 | /* Setup the initial value */ | |
192 | in_buf[AES_BLOCK_LEN-9] = ndx; | |
193 | in_buf[AES_BLOCK_LEN-1] = i = 1; | |
194 | ||
195 | while (nbytes >= AES_BLOCK_LEN) { | |
196 | aes_encryption(in_buf, out_buf, key); | |
197 | memcpy(dst_buf,out_buf,AES_BLOCK_LEN); | |
198 | in_buf[AES_BLOCK_LEN-1] = ++i; | |
199 | nbytes -= AES_BLOCK_LEN; | |
200 | dst_buf += AES_BLOCK_LEN; | |
201 | } | |
202 | if (nbytes) { | |
203 | aes_encryption(in_buf, out_buf, key); | |
204 | memcpy(dst_buf,out_buf,nbytes); | |
205 | } | |
206 | } | |
207 | ||
208 | /* The final UHASH result is XOR'd with the output of a pseudorandom | |
209 | * function. Here, we use AES to generate random output and | |
210 | * xor the appropriate bytes depending on the last bits of nonce. | |
211 | * This scheme is optimized for sequential, increasing big-endian nonces. | |
212 | */ | |
213 | ||
214 | typedef struct { | |
215 | UINT8 cache[AES_BLOCK_LEN]; /* Previous AES output is saved */ | |
216 | UINT8 nonce[AES_BLOCK_LEN]; /* The AES input making above cache */ | |
217 | aes_int_key prf_key; /* Expanded AES key for PDF */ | |
218 | } pdf_ctx; | |
219 | ||
220 | static void pdf_init(pdf_ctx *pc, aes_int_key prf_key) | |
221 | { | |
222 | UINT8 buf[UMAC_KEY_LEN]; | |
223 | ||
224 | kdf(buf, prf_key, 0, UMAC_KEY_LEN); | |
225 | aes_key_setup(buf, pc->prf_key); | |
226 | ||
227 | /* Initialize pdf and cache */ | |
228 | memset(pc->nonce, 0, sizeof(pc->nonce)); | |
229 | aes_encryption(pc->nonce, pc->cache, pc->prf_key); | |
230 | } | |
231 | ||
232 | static void pdf_gen_xor(pdf_ctx *pc, UINT8 nonce[8], UINT8 buf[8]) | |
233 | { | |
234 | /* 'ndx' indicates that we'll be using the 0th or 1st eight bytes | |
235 | * of the AES output. If last time around we returned the ndx-1st | |
236 | * element, then we may have the result in the cache already. | |
237 | */ | |
238 | ||
239 | #if (UMAC_OUTPUT_LEN == 4) | |
240 | #define LOW_BIT_MASK 3 | |
241 | #elif (UMAC_OUTPUT_LEN == 8) | |
242 | #define LOW_BIT_MASK 1 | |
243 | #elif (UMAC_OUTPUT_LEN > 8) | |
244 | #define LOW_BIT_MASK 0 | |
245 | #endif | |
246 | ||
247 | UINT8 tmp_nonce_lo[4]; | |
248 | #if LOW_BIT_MASK != 0 | |
249 | int ndx = nonce[7] & LOW_BIT_MASK; | |
250 | #endif | |
251 | *(UINT32 *)tmp_nonce_lo = ((UINT32 *)nonce)[1]; | |
252 | tmp_nonce_lo[3] &= ~LOW_BIT_MASK; /* zero last bit */ | |
253 | ||
254 | if ( (((UINT32 *)tmp_nonce_lo)[0] != ((UINT32 *)pc->nonce)[1]) || | |
255 | (((UINT32 *)nonce)[0] != ((UINT32 *)pc->nonce)[0]) ) | |
256 | { | |
257 | ((UINT32 *)pc->nonce)[0] = ((UINT32 *)nonce)[0]; | |
258 | ((UINT32 *)pc->nonce)[1] = ((UINT32 *)tmp_nonce_lo)[0]; | |
259 | aes_encryption(pc->nonce, pc->cache, pc->prf_key); | |
260 | } | |
261 | ||
262 | #if (UMAC_OUTPUT_LEN == 4) | |
263 | *((UINT32 *)buf) ^= ((UINT32 *)pc->cache)[ndx]; | |
264 | #elif (UMAC_OUTPUT_LEN == 8) | |
265 | *((UINT64 *)buf) ^= ((UINT64 *)pc->cache)[ndx]; | |
266 | #elif (UMAC_OUTPUT_LEN == 12) | |
267 | ((UINT64 *)buf)[0] ^= ((UINT64 *)pc->cache)[0]; | |
268 | ((UINT32 *)buf)[2] ^= ((UINT32 *)pc->cache)[2]; | |
269 | #elif (UMAC_OUTPUT_LEN == 16) | |
270 | ((UINT64 *)buf)[0] ^= ((UINT64 *)pc->cache)[0]; | |
271 | ((UINT64 *)buf)[1] ^= ((UINT64 *)pc->cache)[1]; | |
272 | #endif | |
273 | } | |
274 | ||
275 | /* ---------------------------------------------------------------------- */ | |
276 | /* ---------------------------------------------------------------------- */ | |
277 | /* ----- Begin NH Hash Section ------------------------------------------ */ | |
278 | /* ---------------------------------------------------------------------- */ | |
279 | /* ---------------------------------------------------------------------- */ | |
280 | ||
281 | /* The NH-based hash functions used in UMAC are described in the UMAC paper | |
282 | * and specification, both of which can be found at the UMAC website. | |
283 | * The interface to this implementation has two | |
284 | * versions, one expects the entire message being hashed to be passed | |
285 | * in a single buffer and returns the hash result immediately. The second | |
286 | * allows the message to be passed in a sequence of buffers. In the | |
287 | * muliple-buffer interface, the client calls the routine nh_update() as | |
288 | * many times as necessary. When there is no more data to be fed to the | |
289 | * hash, the client calls nh_final() which calculates the hash output. | |
290 | * Before beginning another hash calculation the nh_reset() routine | |
291 | * must be called. The single-buffer routine, nh(), is equivalent to | |
292 | * the sequence of calls nh_update() and nh_final(); however it is | |
293 | * optimized and should be prefered whenever the multiple-buffer interface | |
294 | * is not necessary. When using either interface, it is the client's | |
295 | * responsability to pass no more than L1_KEY_LEN bytes per hash result. | |
296 | * | |
297 | * The routine nh_init() initializes the nh_ctx data structure and | |
298 | * must be called once, before any other PDF routine. | |
299 | */ | |
300 | ||
301 | /* The "nh_aux" routines do the actual NH hashing work. They | |
302 | * expect buffers to be multiples of L1_PAD_BOUNDARY. These routines | |
303 | * produce output for all STREAMS NH iterations in one call, | |
304 | * allowing the parallel implementation of the streams. | |
305 | */ | |
306 | ||
307 | #define STREAMS (UMAC_OUTPUT_LEN / 4) /* Number of times hash is applied */ | |
308 | #define L1_KEY_LEN 1024 /* Internal key bytes */ | |
309 | #define L1_KEY_SHIFT 16 /* Toeplitz key shift between streams */ | |
310 | #define L1_PAD_BOUNDARY 32 /* pad message to boundary multiple */ | |
311 | #define ALLOC_BOUNDARY 16 /* Keep buffers aligned to this */ | |
312 | #define HASH_BUF_BYTES 64 /* nh_aux_hb buffer multiple */ | |
313 | ||
314 | typedef struct { | |
315 | UINT8 nh_key [L1_KEY_LEN + L1_KEY_SHIFT * (STREAMS - 1)]; /* NH Key */ | |
316 | UINT8 data [HASH_BUF_BYTES]; /* Incomming data buffer */ | |
317 | int next_data_empty; /* Bookeeping variable for data buffer. */ | |
318 | int bytes_hashed; /* Bytes (out of L1_KEY_LEN) incorperated. */ | |
319 | UINT64 state[STREAMS]; /* on-line state */ | |
320 | } nh_ctx; | |
321 | ||
322 | ||
323 | #if (UMAC_OUTPUT_LEN == 4) | |
324 | ||
325 | static void nh_aux(void *kp, void *dp, void *hp, UINT32 dlen) | |
326 | /* NH hashing primitive. Previous (partial) hash result is loaded and | |
327 | * then stored via hp pointer. The length of the data pointed at by "dp", | |
328 | * "dlen", is guaranteed to be divisible by L1_PAD_BOUNDARY (32). Key | |
329 | * is expected to be endian compensated in memory at key setup. | |
330 | */ | |
331 | { | |
332 | UINT64 h; | |
333 | UWORD c = dlen / 32; | |
334 | UINT32 *k = (UINT32 *)kp; | |
335 | UINT32 *d = (UINT32 *)dp; | |
336 | UINT32 d0,d1,d2,d3,d4,d5,d6,d7; | |
337 | UINT32 k0,k1,k2,k3,k4,k5,k6,k7; | |
338 | ||
339 | h = *((UINT64 *)hp); | |
340 | do { | |
341 | d0 = LOAD_UINT32_LITTLE(d+0); d1 = LOAD_UINT32_LITTLE(d+1); | |
342 | d2 = LOAD_UINT32_LITTLE(d+2); d3 = LOAD_UINT32_LITTLE(d+3); | |
343 | d4 = LOAD_UINT32_LITTLE(d+4); d5 = LOAD_UINT32_LITTLE(d+5); | |
344 | d6 = LOAD_UINT32_LITTLE(d+6); d7 = LOAD_UINT32_LITTLE(d+7); | |
345 | k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3); | |
346 | k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7); | |
347 | h += MUL64((k0 + d0), (k4 + d4)); | |
348 | h += MUL64((k1 + d1), (k5 + d5)); | |
349 | h += MUL64((k2 + d2), (k6 + d6)); | |
350 | h += MUL64((k3 + d3), (k7 + d7)); | |
351 | ||
352 | d += 8; | |
353 | k += 8; | |
354 | } while (--c); | |
355 | *((UINT64 *)hp) = h; | |
356 | } | |
357 | ||
358 | #elif (UMAC_OUTPUT_LEN == 8) | |
359 | ||
360 | static void nh_aux(void *kp, void *dp, void *hp, UINT32 dlen) | |
361 | /* Same as previous nh_aux, but two streams are handled in one pass, | |
362 | * reading and writing 16 bytes of hash-state per call. | |
363 | */ | |
364 | { | |
365 | UINT64 h1,h2; | |
366 | UWORD c = dlen / 32; | |
367 | UINT32 *k = (UINT32 *)kp; | |
368 | UINT32 *d = (UINT32 *)dp; | |
369 | UINT32 d0,d1,d2,d3,d4,d5,d6,d7; | |
370 | UINT32 k0,k1,k2,k3,k4,k5,k6,k7, | |
371 | k8,k9,k10,k11; | |
372 | ||
373 | h1 = *((UINT64 *)hp); | |
374 | h2 = *((UINT64 *)hp + 1); | |
375 | k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3); | |
376 | do { | |
377 | d0 = LOAD_UINT32_LITTLE(d+0); d1 = LOAD_UINT32_LITTLE(d+1); | |
378 | d2 = LOAD_UINT32_LITTLE(d+2); d3 = LOAD_UINT32_LITTLE(d+3); | |
379 | d4 = LOAD_UINT32_LITTLE(d+4); d5 = LOAD_UINT32_LITTLE(d+5); | |
380 | d6 = LOAD_UINT32_LITTLE(d+6); d7 = LOAD_UINT32_LITTLE(d+7); | |
381 | k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7); | |
382 | k8 = *(k+8); k9 = *(k+9); k10 = *(k+10); k11 = *(k+11); | |
383 | ||
384 | h1 += MUL64((k0 + d0), (k4 + d4)); | |
385 | h2 += MUL64((k4 + d0), (k8 + d4)); | |
386 | ||
387 | h1 += MUL64((k1 + d1), (k5 + d5)); | |
388 | h2 += MUL64((k5 + d1), (k9 + d5)); | |
389 | ||
390 | h1 += MUL64((k2 + d2), (k6 + d6)); | |
391 | h2 += MUL64((k6 + d2), (k10 + d6)); | |
392 | ||
393 | h1 += MUL64((k3 + d3), (k7 + d7)); | |
394 | h2 += MUL64((k7 + d3), (k11 + d7)); | |
395 | ||
396 | k0 = k8; k1 = k9; k2 = k10; k3 = k11; | |
397 | ||
398 | d += 8; | |
399 | k += 8; | |
400 | } while (--c); | |
401 | ((UINT64 *)hp)[0] = h1; | |
402 | ((UINT64 *)hp)[1] = h2; | |
403 | } | |
404 | ||
405 | #elif (UMAC_OUTPUT_LEN == 12) | |
406 | ||
407 | static void nh_aux(void *kp, void *dp, void *hp, UINT32 dlen) | |
408 | /* Same as previous nh_aux, but two streams are handled in one pass, | |
409 | * reading and writing 24 bytes of hash-state per call. | |
410 | */ | |
411 | { | |
412 | UINT64 h1,h2,h3; | |
413 | UWORD c = dlen / 32; | |
414 | UINT32 *k = (UINT32 *)kp; | |
415 | UINT32 *d = (UINT32 *)dp; | |
416 | UINT32 d0,d1,d2,d3,d4,d5,d6,d7; | |
417 | UINT32 k0,k1,k2,k3,k4,k5,k6,k7, | |
418 | k8,k9,k10,k11,k12,k13,k14,k15; | |
419 | ||
420 | h1 = *((UINT64 *)hp); | |
421 | h2 = *((UINT64 *)hp + 1); | |
422 | h3 = *((UINT64 *)hp + 2); | |
423 | k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3); | |
424 | k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7); | |
425 | do { | |
426 | d0 = LOAD_UINT32_LITTLE(d+0); d1 = LOAD_UINT32_LITTLE(d+1); | |
427 | d2 = LOAD_UINT32_LITTLE(d+2); d3 = LOAD_UINT32_LITTLE(d+3); | |
428 | d4 = LOAD_UINT32_LITTLE(d+4); d5 = LOAD_UINT32_LITTLE(d+5); | |
429 | d6 = LOAD_UINT32_LITTLE(d+6); d7 = LOAD_UINT32_LITTLE(d+7); | |
430 | k8 = *(k+8); k9 = *(k+9); k10 = *(k+10); k11 = *(k+11); | |
431 | k12 = *(k+12); k13 = *(k+13); k14 = *(k+14); k15 = *(k+15); | |
432 | ||
433 | h1 += MUL64((k0 + d0), (k4 + d4)); | |
434 | h2 += MUL64((k4 + d0), (k8 + d4)); | |
435 | h3 += MUL64((k8 + d0), (k12 + d4)); | |
436 | ||
437 | h1 += MUL64((k1 + d1), (k5 + d5)); | |
438 | h2 += MUL64((k5 + d1), (k9 + d5)); | |
439 | h3 += MUL64((k9 + d1), (k13 + d5)); | |
440 | ||
441 | h1 += MUL64((k2 + d2), (k6 + d6)); | |
442 | h2 += MUL64((k6 + d2), (k10 + d6)); | |
443 | h3 += MUL64((k10 + d2), (k14 + d6)); | |
444 | ||
445 | h1 += MUL64((k3 + d3), (k7 + d7)); | |
446 | h2 += MUL64((k7 + d3), (k11 + d7)); | |
447 | h3 += MUL64((k11 + d3), (k15 + d7)); | |
448 | ||
449 | k0 = k8; k1 = k9; k2 = k10; k3 = k11; | |
450 | k4 = k12; k5 = k13; k6 = k14; k7 = k15; | |
451 | ||
452 | d += 8; | |
453 | k += 8; | |
454 | } while (--c); | |
455 | ((UINT64 *)hp)[0] = h1; | |
456 | ((UINT64 *)hp)[1] = h2; | |
457 | ((UINT64 *)hp)[2] = h3; | |
458 | } | |
459 | ||
460 | #elif (UMAC_OUTPUT_LEN == 16) | |
461 | ||
462 | static void nh_aux(void *kp, void *dp, void *hp, UINT32 dlen) | |
463 | /* Same as previous nh_aux, but two streams are handled in one pass, | |
464 | * reading and writing 24 bytes of hash-state per call. | |
465 | */ | |
466 | { | |
467 | UINT64 h1,h2,h3,h4; | |
468 | UWORD c = dlen / 32; | |
469 | UINT32 *k = (UINT32 *)kp; | |
470 | UINT32 *d = (UINT32 *)dp; | |
471 | UINT32 d0,d1,d2,d3,d4,d5,d6,d7; | |
472 | UINT32 k0,k1,k2,k3,k4,k5,k6,k7, | |
473 | k8,k9,k10,k11,k12,k13,k14,k15, | |
474 | k16,k17,k18,k19; | |
475 | ||
476 | h1 = *((UINT64 *)hp); | |
477 | h2 = *((UINT64 *)hp + 1); | |
478 | h3 = *((UINT64 *)hp + 2); | |
479 | h4 = *((UINT64 *)hp + 3); | |
480 | k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3); | |
481 | k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7); | |
482 | do { | |
483 | d0 = LOAD_UINT32_LITTLE(d+0); d1 = LOAD_UINT32_LITTLE(d+1); | |
484 | d2 = LOAD_UINT32_LITTLE(d+2); d3 = LOAD_UINT32_LITTLE(d+3); | |
485 | d4 = LOAD_UINT32_LITTLE(d+4); d5 = LOAD_UINT32_LITTLE(d+5); | |
486 | d6 = LOAD_UINT32_LITTLE(d+6); d7 = LOAD_UINT32_LITTLE(d+7); | |
487 | k8 = *(k+8); k9 = *(k+9); k10 = *(k+10); k11 = *(k+11); | |
488 | k12 = *(k+12); k13 = *(k+13); k14 = *(k+14); k15 = *(k+15); | |
489 | k16 = *(k+16); k17 = *(k+17); k18 = *(k+18); k19 = *(k+19); | |
490 | ||
491 | h1 += MUL64((k0 + d0), (k4 + d4)); | |
492 | h2 += MUL64((k4 + d0), (k8 + d4)); | |
493 | h3 += MUL64((k8 + d0), (k12 + d4)); | |
494 | h4 += MUL64((k12 + d0), (k16 + d4)); | |
495 | ||
496 | h1 += MUL64((k1 + d1), (k5 + d5)); | |
497 | h2 += MUL64((k5 + d1), (k9 + d5)); | |
498 | h3 += MUL64((k9 + d1), (k13 + d5)); | |
499 | h4 += MUL64((k13 + d1), (k17 + d5)); | |
500 | ||
501 | h1 += MUL64((k2 + d2), (k6 + d6)); | |
502 | h2 += MUL64((k6 + d2), (k10 + d6)); | |
503 | h3 += MUL64((k10 + d2), (k14 + d6)); | |
504 | h4 += MUL64((k14 + d2), (k18 + d6)); | |
505 | ||
506 | h1 += MUL64((k3 + d3), (k7 + d7)); | |
507 | h2 += MUL64((k7 + d3), (k11 + d7)); | |
508 | h3 += MUL64((k11 + d3), (k15 + d7)); | |
509 | h4 += MUL64((k15 + d3), (k19 + d7)); | |
510 | ||
511 | k0 = k8; k1 = k9; k2 = k10; k3 = k11; | |
512 | k4 = k12; k5 = k13; k6 = k14; k7 = k15; | |
513 | k8 = k16; k9 = k17; k10 = k18; k11 = k19; | |
514 | ||
515 | d += 8; | |
516 | k += 8; | |
517 | } while (--c); | |
518 | ((UINT64 *)hp)[0] = h1; | |
519 | ((UINT64 *)hp)[1] = h2; | |
520 | ((UINT64 *)hp)[2] = h3; | |
521 | ((UINT64 *)hp)[3] = h4; | |
522 | } | |
523 | ||
524 | /* ---------------------------------------------------------------------- */ | |
525 | #endif /* UMAC_OUTPUT_LENGTH */ | |
526 | /* ---------------------------------------------------------------------- */ | |
527 | ||
528 | ||
529 | /* ---------------------------------------------------------------------- */ | |
530 | ||
531 | static void nh_transform(nh_ctx *hc, UINT8 *buf, UINT32 nbytes) | |
532 | /* This function is a wrapper for the primitive NH hash functions. It takes | |
533 | * as argument "hc" the current hash context and a buffer which must be a | |
534 | * multiple of L1_PAD_BOUNDARY. The key passed to nh_aux is offset | |
535 | * appropriately according to how much message has been hashed already. | |
536 | */ | |
537 | { | |
538 | UINT8 *key; | |
539 | ||
540 | key = hc->nh_key + hc->bytes_hashed; | |
541 | nh_aux(key, buf, hc->state, nbytes); | |
542 | } | |
543 | ||
544 | /* ---------------------------------------------------------------------- */ | |
545 | ||
546 | static void endian_convert(void *buf, UWORD bpw, UINT32 num_bytes) | |
547 | /* We endian convert the keys on little-endian computers to */ | |
548 | /* compensate for the lack of big-endian memory reads during hashing. */ | |
549 | { | |
550 | UWORD iters = num_bytes / bpw; | |
551 | if (bpw == 4) { | |
552 | UINT32 *p = (UINT32 *)buf; | |
553 | do { | |
554 | *p = LOAD_UINT32_REVERSED(p); | |
555 | p++; | |
556 | } while (--iters); | |
557 | } else if (bpw == 8) { | |
558 | UINT32 *p = (UINT32 *)buf; | |
559 | UINT32 t; | |
560 | do { | |
561 | t = LOAD_UINT32_REVERSED(p+1); | |
562 | p[1] = LOAD_UINT32_REVERSED(p); | |
563 | p[0] = t; | |
564 | p += 2; | |
565 | } while (--iters); | |
566 | } | |
567 | } | |
568 | #if (__LITTLE_ENDIAN__) | |
569 | #define endian_convert_if_le(x,y,z) endian_convert((x),(y),(z)) | |
570 | #else | |
571 | #define endian_convert_if_le(x,y,z) do{}while(0) /* Do nothing */ | |
572 | #endif | |
573 | ||
574 | /* ---------------------------------------------------------------------- */ | |
575 | ||
576 | static void nh_reset(nh_ctx *hc) | |
577 | /* Reset nh_ctx to ready for hashing of new data */ | |
578 | { | |
579 | hc->bytes_hashed = 0; | |
580 | hc->next_data_empty = 0; | |
581 | hc->state[0] = 0; | |
582 | #if (UMAC_OUTPUT_LEN >= 8) | |
583 | hc->state[1] = 0; | |
584 | #endif | |
585 | #if (UMAC_OUTPUT_LEN >= 12) | |
586 | hc->state[2] = 0; | |
587 | #endif | |
588 | #if (UMAC_OUTPUT_LEN == 16) | |
589 | hc->state[3] = 0; | |
590 | #endif | |
591 | ||
592 | } | |
593 | ||
594 | /* ---------------------------------------------------------------------- */ | |
595 | ||
596 | static void nh_init(nh_ctx *hc, aes_int_key prf_key) | |
597 | /* Generate nh_key, endian convert and reset to be ready for hashing. */ | |
598 | { | |
599 | kdf(hc->nh_key, prf_key, 1, sizeof(hc->nh_key)); | |
600 | endian_convert_if_le(hc->nh_key, 4, sizeof(hc->nh_key)); | |
601 | nh_reset(hc); | |
602 | } | |
603 | ||
604 | /* ---------------------------------------------------------------------- */ | |
605 | ||
606 | static void nh_update(nh_ctx *hc, UINT8 *buf, UINT32 nbytes) | |
607 | /* Incorporate nbytes of data into a nh_ctx, buffer whatever is not an */ | |
608 | /* even multiple of HASH_BUF_BYTES. */ | |
609 | { | |
610 | UINT32 i,j; | |
611 | ||
612 | j = hc->next_data_empty; | |
613 | if ((j + nbytes) >= HASH_BUF_BYTES) { | |
614 | if (j) { | |
615 | i = HASH_BUF_BYTES - j; | |
616 | memcpy(hc->data+j, buf, i); | |
617 | nh_transform(hc,hc->data,HASH_BUF_BYTES); | |
618 | nbytes -= i; | |
619 | buf += i; | |
620 | hc->bytes_hashed += HASH_BUF_BYTES; | |
621 | } | |
622 | if (nbytes >= HASH_BUF_BYTES) { | |
623 | i = nbytes & ~(HASH_BUF_BYTES - 1); | |
624 | nh_transform(hc, buf, i); | |
625 | nbytes -= i; | |
626 | buf += i; | |
627 | hc->bytes_hashed += i; | |
628 | } | |
629 | j = 0; | |
630 | } | |
631 | memcpy(hc->data + j, buf, nbytes); | |
632 | hc->next_data_empty = j + nbytes; | |
633 | } | |
634 | ||
635 | /* ---------------------------------------------------------------------- */ | |
636 | ||
637 | static void zero_pad(UINT8 *p, int nbytes) | |
638 | { | |
639 | /* Write "nbytes" of zeroes, beginning at "p" */ | |
640 | if (nbytes >= (int)sizeof(UWORD)) { | |
641 | while ((ptrdiff_t)p % sizeof(UWORD)) { | |
642 | *p = 0; | |
643 | nbytes--; | |
644 | p++; | |
645 | } | |
646 | while (nbytes >= (int)sizeof(UWORD)) { | |
647 | *(UWORD *)p = 0; | |
648 | nbytes -= sizeof(UWORD); | |
649 | p += sizeof(UWORD); | |
650 | } | |
651 | } | |
652 | while (nbytes) { | |
653 | *p = 0; | |
654 | nbytes--; | |
655 | p++; | |
656 | } | |
657 | } | |
658 | ||
659 | /* ---------------------------------------------------------------------- */ | |
660 | ||
661 | static void nh_final(nh_ctx *hc, UINT8 *result) | |
662 | /* After passing some number of data buffers to nh_update() for integration | |
663 | * into an NH context, nh_final is called to produce a hash result. If any | |
664 | * bytes are in the buffer hc->data, incorporate them into the | |
665 | * NH context. Finally, add into the NH accumulation "state" the total number | |
666 | * of bits hashed. The resulting numbers are written to the buffer "result". | |
667 | * If nh_update was never called, L1_PAD_BOUNDARY zeroes are incorporated. | |
668 | */ | |
669 | { | |
670 | int nh_len, nbits; | |
671 | ||
672 | if (hc->next_data_empty != 0) { | |
673 | nh_len = ((hc->next_data_empty + (L1_PAD_BOUNDARY - 1)) & | |
674 | ~(L1_PAD_BOUNDARY - 1)); | |
675 | zero_pad(hc->data + hc->next_data_empty, | |
676 | nh_len - hc->next_data_empty); | |
677 | nh_transform(hc, hc->data, nh_len); | |
678 | hc->bytes_hashed += hc->next_data_empty; | |
679 | } else if (hc->bytes_hashed == 0) { | |
680 | nh_len = L1_PAD_BOUNDARY; | |
681 | zero_pad(hc->data, L1_PAD_BOUNDARY); | |
682 | nh_transform(hc, hc->data, nh_len); | |
683 | } | |
684 | ||
685 | nbits = (hc->bytes_hashed << 3); | |
686 | ((UINT64 *)result)[0] = ((UINT64 *)hc->state)[0] + nbits; | |
687 | #if (UMAC_OUTPUT_LEN >= 8) | |
688 | ((UINT64 *)result)[1] = ((UINT64 *)hc->state)[1] + nbits; | |
689 | #endif | |
690 | #if (UMAC_OUTPUT_LEN >= 12) | |
691 | ((UINT64 *)result)[2] = ((UINT64 *)hc->state)[2] + nbits; | |
692 | #endif | |
693 | #if (UMAC_OUTPUT_LEN == 16) | |
694 | ((UINT64 *)result)[3] = ((UINT64 *)hc->state)[3] + nbits; | |
695 | #endif | |
696 | nh_reset(hc); | |
697 | } | |
698 | ||
699 | /* ---------------------------------------------------------------------- */ | |
700 | ||
701 | static void nh(nh_ctx *hc, UINT8 *buf, UINT32 padded_len, | |
702 | UINT32 unpadded_len, UINT8 *result) | |
703 | /* All-in-one nh_update() and nh_final() equivalent. | |
704 | * Assumes that padded_len is divisible by L1_PAD_BOUNDARY and result is | |
705 | * well aligned | |
706 | */ | |
707 | { | |
708 | UINT32 nbits; | |
709 | ||
710 | /* Initialize the hash state */ | |
711 | nbits = (unpadded_len << 3); | |
712 | ||
713 | ((UINT64 *)result)[0] = nbits; | |
714 | #if (UMAC_OUTPUT_LEN >= 8) | |
715 | ((UINT64 *)result)[1] = nbits; | |
716 | #endif | |
717 | #if (UMAC_OUTPUT_LEN >= 12) | |
718 | ((UINT64 *)result)[2] = nbits; | |
719 | #endif | |
720 | #if (UMAC_OUTPUT_LEN == 16) | |
721 | ((UINT64 *)result)[3] = nbits; | |
722 | #endif | |
723 | ||
724 | nh_aux(hc->nh_key, buf, result, padded_len); | |
725 | } | |
726 | ||
727 | /* ---------------------------------------------------------------------- */ | |
728 | /* ---------------------------------------------------------------------- */ | |
729 | /* ----- Begin UHASH Section -------------------------------------------- */ | |
730 | /* ---------------------------------------------------------------------- */ | |
731 | /* ---------------------------------------------------------------------- */ | |
732 | ||
733 | /* UHASH is a multi-layered algorithm. Data presented to UHASH is first | |
734 | * hashed by NH. The NH output is then hashed by a polynomial-hash layer | |
735 | * unless the initial data to be hashed is short. After the polynomial- | |
736 | * layer, an inner-product hash is used to produce the final UHASH output. | |
737 | * | |
738 | * UHASH provides two interfaces, one all-at-once and another where data | |
739 | * buffers are presented sequentially. In the sequential interface, the | |
740 | * UHASH client calls the routine uhash_update() as many times as necessary. | |
741 | * When there is no more data to be fed to UHASH, the client calls | |
742 | * uhash_final() which | |
743 | * calculates the UHASH output. Before beginning another UHASH calculation | |
744 | * the uhash_reset() routine must be called. The all-at-once UHASH routine, | |
745 | * uhash(), is equivalent to the sequence of calls uhash_update() and | |
746 | * uhash_final(); however it is optimized and should be | |
747 | * used whenever the sequential interface is not necessary. | |
748 | * | |
749 | * The routine uhash_init() initializes the uhash_ctx data structure and | |
750 | * must be called once, before any other UHASH routine. | |
751 | */ | |
752 | ||
753 | /* ---------------------------------------------------------------------- */ | |
754 | /* ----- Constants and uhash_ctx ---------------------------------------- */ | |
755 | /* ---------------------------------------------------------------------- */ | |
756 | ||
757 | /* ---------------------------------------------------------------------- */ | |
758 | /* ----- Poly hash and Inner-Product hash Constants --------------------- */ | |
759 | /* ---------------------------------------------------------------------- */ | |
760 | ||
761 | /* Primes and masks */ | |
762 | #define p36 ((UINT64)0x0000000FFFFFFFFBull) /* 2^36 - 5 */ | |
763 | #define p64 ((UINT64)0xFFFFFFFFFFFFFFC5ull) /* 2^64 - 59 */ | |
764 | #define m36 ((UINT64)0x0000000FFFFFFFFFull) /* The low 36 of 64 bits */ | |
765 | ||
766 | ||
767 | /* ---------------------------------------------------------------------- */ | |
768 | ||
769 | typedef struct uhash_ctx { | |
770 | nh_ctx hash; /* Hash context for L1 NH hash */ | |
771 | UINT64 poly_key_8[STREAMS]; /* p64 poly keys */ | |
772 | UINT64 poly_accum[STREAMS]; /* poly hash result */ | |
773 | UINT64 ip_keys[STREAMS*4]; /* Inner-product keys */ | |
774 | UINT32 ip_trans[STREAMS]; /* Inner-product translation */ | |
775 | UINT32 msg_len; /* Total length of data passed */ | |
776 | /* to uhash */ | |
777 | } uhash_ctx; | |
778 | typedef struct uhash_ctx *uhash_ctx_t; | |
779 | ||
780 | /* ---------------------------------------------------------------------- */ | |
781 | ||
782 | ||
783 | /* The polynomial hashes use Horner's rule to evaluate a polynomial one | |
784 | * word at a time. As described in the specification, poly32 and poly64 | |
785 | * require keys from special domains. The following implementations exploit | |
786 | * the special domains to avoid overflow. The results are not guaranteed to | |
787 | * be within Z_p32 and Z_p64, but the Inner-Product hash implementation | |
788 | * patches any errant values. | |
789 | */ | |
790 | ||
791 | static UINT64 poly64(UINT64 cur, UINT64 key, UINT64 data) | |
792 | { | |
793 | UINT32 key_hi = (UINT32)(key >> 32), | |
794 | key_lo = (UINT32)key, | |
795 | cur_hi = (UINT32)(cur >> 32), | |
796 | cur_lo = (UINT32)cur, | |
797 | x_lo, | |
798 | x_hi; | |
799 | UINT64 X,T,res; | |
800 | ||
801 | X = MUL64(key_hi, cur_lo) + MUL64(cur_hi, key_lo); | |
802 | x_lo = (UINT32)X; | |
803 | x_hi = (UINT32)(X >> 32); | |
804 | ||
805 | res = (MUL64(key_hi, cur_hi) + x_hi) * 59 + MUL64(key_lo, cur_lo); | |
806 | ||
807 | T = ((UINT64)x_lo << 32); | |
808 | res += T; | |
809 | if (res < T) | |
810 | res += 59; | |
811 | ||
812 | res += data; | |
813 | if (res < data) | |
814 | res += 59; | |
815 | ||
816 | return res; | |
817 | } | |
818 | ||
819 | ||
820 | /* Although UMAC is specified to use a ramped polynomial hash scheme, this | |
821 | * implementation does not handle all ramp levels. Because we don't handle | |
822 | * the ramp up to p128 modulus in this implementation, we are limited to | |
823 | * 2^14 poly_hash() invocations per stream (for a total capacity of 2^24 | |
824 | * bytes input to UMAC per tag, ie. 16MB). | |
825 | */ | |
826 | static void poly_hash(uhash_ctx_t hc, UINT32 data_in[]) | |
827 | { | |
828 | int i; | |
829 | UINT64 *data=(UINT64*)data_in; | |
830 | ||
831 | for (i = 0; i < STREAMS; i++) { | |
832 | if ((UINT32)(data[i] >> 32) == 0xfffffffful) { | |
833 | hc->poly_accum[i] = poly64(hc->poly_accum[i], | |
834 | hc->poly_key_8[i], p64 - 1); | |
835 | hc->poly_accum[i] = poly64(hc->poly_accum[i], | |
836 | hc->poly_key_8[i], (data[i] - 59)); | |
837 | } else { | |
838 | hc->poly_accum[i] = poly64(hc->poly_accum[i], | |
839 | hc->poly_key_8[i], data[i]); | |
840 | } | |
841 | } | |
842 | } | |
843 | ||
844 | ||
845 | /* ---------------------------------------------------------------------- */ | |
846 | ||
847 | ||
848 | /* The final step in UHASH is an inner-product hash. The poly hash | |
849 | * produces a result not neccesarily WORD_LEN bytes long. The inner- | |
850 | * product hash breaks the polyhash output into 16-bit chunks and | |
851 | * multiplies each with a 36 bit key. | |
852 | */ | |
853 | ||
854 | static UINT64 ip_aux(UINT64 t, UINT64 *ipkp, UINT64 data) | |
855 | { | |
856 | t = t + ipkp[0] * (UINT64)(UINT16)(data >> 48); | |
857 | t = t + ipkp[1] * (UINT64)(UINT16)(data >> 32); | |
858 | t = t + ipkp[2] * (UINT64)(UINT16)(data >> 16); | |
859 | t = t + ipkp[3] * (UINT64)(UINT16)(data); | |
860 | ||
861 | return t; | |
862 | } | |
863 | ||
864 | static UINT32 ip_reduce_p36(UINT64 t) | |
865 | { | |
866 | /* Divisionless modular reduction */ | |
867 | UINT64 ret; | |
868 | ||
869 | ret = (t & m36) + 5 * (t >> 36); | |
870 | if (ret >= p36) | |
871 | ret -= p36; | |
872 | ||
873 | /* return least significant 32 bits */ | |
874 | return (UINT32)(ret); | |
875 | } | |
876 | ||
877 | ||
878 | /* If the data being hashed by UHASH is no longer than L1_KEY_LEN, then | |
879 | * the polyhash stage is skipped and ip_short is applied directly to the | |
880 | * NH output. | |
881 | */ | |
882 | static void ip_short(uhash_ctx_t ahc, UINT8 *nh_res, u_char *res) | |
883 | { | |
884 | UINT64 t; | |
885 | UINT64 *nhp = (UINT64 *)nh_res; | |
886 | ||
887 | t = ip_aux(0,ahc->ip_keys, nhp[0]); | |
888 | STORE_UINT32_BIG((UINT32 *)res+0, ip_reduce_p36(t) ^ ahc->ip_trans[0]); | |
889 | #if (UMAC_OUTPUT_LEN >= 8) | |
890 | t = ip_aux(0,ahc->ip_keys+4, nhp[1]); | |
891 | STORE_UINT32_BIG((UINT32 *)res+1, ip_reduce_p36(t) ^ ahc->ip_trans[1]); | |
892 | #endif | |
893 | #if (UMAC_OUTPUT_LEN >= 12) | |
894 | t = ip_aux(0,ahc->ip_keys+8, nhp[2]); | |
895 | STORE_UINT32_BIG((UINT32 *)res+2, ip_reduce_p36(t) ^ ahc->ip_trans[2]); | |
896 | #endif | |
897 | #if (UMAC_OUTPUT_LEN == 16) | |
898 | t = ip_aux(0,ahc->ip_keys+12, nhp[3]); | |
899 | STORE_UINT32_BIG((UINT32 *)res+3, ip_reduce_p36(t) ^ ahc->ip_trans[3]); | |
900 | #endif | |
901 | } | |
902 | ||
903 | /* If the data being hashed by UHASH is longer than L1_KEY_LEN, then | |
904 | * the polyhash stage is not skipped and ip_long is applied to the | |
905 | * polyhash output. | |
906 | */ | |
907 | static void ip_long(uhash_ctx_t ahc, u_char *res) | |
908 | { | |
909 | int i; | |
910 | UINT64 t; | |
911 | ||
912 | for (i = 0; i < STREAMS; i++) { | |
913 | /* fix polyhash output not in Z_p64 */ | |
914 | if (ahc->poly_accum[i] >= p64) | |
915 | ahc->poly_accum[i] -= p64; | |
916 | t = ip_aux(0,ahc->ip_keys+(i*4), ahc->poly_accum[i]); | |
917 | STORE_UINT32_BIG((UINT32 *)res+i, | |
918 | ip_reduce_p36(t) ^ ahc->ip_trans[i]); | |
919 | } | |
920 | } | |
921 | ||
922 | ||
923 | /* ---------------------------------------------------------------------- */ | |
924 | ||
925 | /* ---------------------------------------------------------------------- */ | |
926 | ||
927 | /* Reset uhash context for next hash session */ | |
928 | static int uhash_reset(uhash_ctx_t pc) | |
929 | { | |
930 | nh_reset(&pc->hash); | |
931 | pc->msg_len = 0; | |
932 | pc->poly_accum[0] = 1; | |
933 | #if (UMAC_OUTPUT_LEN >= 8) | |
934 | pc->poly_accum[1] = 1; | |
935 | #endif | |
936 | #if (UMAC_OUTPUT_LEN >= 12) | |
937 | pc->poly_accum[2] = 1; | |
938 | #endif | |
939 | #if (UMAC_OUTPUT_LEN == 16) | |
940 | pc->poly_accum[3] = 1; | |
941 | #endif | |
942 | return 1; | |
943 | } | |
944 | ||
945 | /* ---------------------------------------------------------------------- */ | |
946 | ||
947 | /* Given a pointer to the internal key needed by kdf() and a uhash context, | |
948 | * initialize the NH context and generate keys needed for poly and inner- | |
949 | * product hashing. All keys are endian adjusted in memory so that native | |
950 | * loads cause correct keys to be in registers during calculation. | |
951 | */ | |
952 | static void uhash_init(uhash_ctx_t ahc, aes_int_key prf_key) | |
953 | { | |
954 | int i; | |
955 | UINT8 buf[(8*STREAMS+4)*sizeof(UINT64)]; | |
956 | ||
957 | /* Zero the entire uhash context */ | |
958 | memset(ahc, 0, sizeof(uhash_ctx)); | |
959 | ||
960 | /* Initialize the L1 hash */ | |
961 | nh_init(&ahc->hash, prf_key); | |
962 | ||
963 | /* Setup L2 hash variables */ | |
964 | kdf(buf, prf_key, 2, sizeof(buf)); /* Fill buffer with index 1 key */ | |
965 | for (i = 0; i < STREAMS; i++) { | |
966 | /* Fill keys from the buffer, skipping bytes in the buffer not | |
967 | * used by this implementation. Endian reverse the keys if on a | |
968 | * little-endian computer. | |
969 | */ | |
970 | memcpy(ahc->poly_key_8+i, buf+24*i, 8); | |
971 | endian_convert_if_le(ahc->poly_key_8+i, 8, 8); | |
972 | /* Mask the 64-bit keys to their special domain */ | |
973 | ahc->poly_key_8[i] &= ((UINT64)0x01ffffffu << 32) + 0x01ffffffu; | |
974 | ahc->poly_accum[i] = 1; /* Our polyhash prepends a non-zero word */ | |
975 | } | |
976 | ||
977 | /* Setup L3-1 hash variables */ | |
978 | kdf(buf, prf_key, 3, sizeof(buf)); /* Fill buffer with index 2 key */ | |
979 | for (i = 0; i < STREAMS; i++) | |
980 | memcpy(ahc->ip_keys+4*i, buf+(8*i+4)*sizeof(UINT64), | |
981 | 4*sizeof(UINT64)); | |
982 | endian_convert_if_le(ahc->ip_keys, sizeof(UINT64), | |
983 | sizeof(ahc->ip_keys)); | |
984 | for (i = 0; i < STREAMS*4; i++) | |
985 | ahc->ip_keys[i] %= p36; /* Bring into Z_p36 */ | |
986 | ||
987 | /* Setup L3-2 hash variables */ | |
988 | /* Fill buffer with index 4 key */ | |
989 | kdf(ahc->ip_trans, prf_key, 4, STREAMS * sizeof(UINT32)); | |
990 | endian_convert_if_le(ahc->ip_trans, sizeof(UINT32), | |
991 | STREAMS * sizeof(UINT32)); | |
992 | } | |
993 | ||
994 | /* ---------------------------------------------------------------------- */ | |
995 | ||
996 | #if 0 | |
997 | static uhash_ctx_t uhash_alloc(u_char key[]) | |
998 | { | |
999 | /* Allocate memory and force to a 16-byte boundary. */ | |
1000 | uhash_ctx_t ctx; | |
1001 | u_char bytes_to_add; | |
1002 | aes_int_key prf_key; | |
1003 | ||
1004 | ctx = (uhash_ctx_t)malloc(sizeof(uhash_ctx)+ALLOC_BOUNDARY); | |
1005 | if (ctx) { | |
1006 | if (ALLOC_BOUNDARY) { | |
1007 | bytes_to_add = ALLOC_BOUNDARY - | |
1008 | ((ptrdiff_t)ctx & (ALLOC_BOUNDARY -1)); | |
1009 | ctx = (uhash_ctx_t)((u_char *)ctx + bytes_to_add); | |
1010 | *((u_char *)ctx - 1) = bytes_to_add; | |
1011 | } | |
1012 | aes_key_setup(key,prf_key); | |
1013 | uhash_init(ctx, prf_key); | |
1014 | } | |
1015 | return (ctx); | |
1016 | } | |
1017 | #endif | |
1018 | ||
1019 | /* ---------------------------------------------------------------------- */ | |
1020 | ||
1021 | #if 0 | |
1022 | static int uhash_free(uhash_ctx_t ctx) | |
1023 | { | |
1024 | /* Free memory allocated by uhash_alloc */ | |
1025 | u_char bytes_to_sub; | |
1026 | ||
1027 | if (ctx) { | |
1028 | if (ALLOC_BOUNDARY) { | |
1029 | bytes_to_sub = *((u_char *)ctx - 1); | |
1030 | ctx = (uhash_ctx_t)((u_char *)ctx - bytes_to_sub); | |
1031 | } | |
1032 | free(ctx); | |
1033 | } | |
1034 | return (1); | |
1035 | } | |
1036 | #endif | |
1037 | /* ---------------------------------------------------------------------- */ | |
1038 | ||
1039 | static int uhash_update(uhash_ctx_t ctx, u_char *input, long len) | |
1040 | /* Given len bytes of data, we parse it into L1_KEY_LEN chunks and | |
1041 | * hash each one with NH, calling the polyhash on each NH output. | |
1042 | */ | |
1043 | { | |
1044 | UWORD bytes_hashed, bytes_remaining; | |
1045 | UINT8 nh_result[STREAMS*sizeof(UINT64)]; | |
1046 | ||
1047 | if (ctx->msg_len + len <= L1_KEY_LEN) { | |
1048 | nh_update(&ctx->hash, (UINT8 *)input, len); | |
1049 | ctx->msg_len += len; | |
1050 | } else { | |
1051 | ||
1052 | bytes_hashed = ctx->msg_len % L1_KEY_LEN; | |
1053 | if (ctx->msg_len == L1_KEY_LEN) | |
1054 | bytes_hashed = L1_KEY_LEN; | |
1055 | ||
1056 | if (bytes_hashed + len >= L1_KEY_LEN) { | |
1057 | ||
1058 | /* If some bytes have been passed to the hash function */ | |
1059 | /* then we want to pass at most (L1_KEY_LEN - bytes_hashed) */ | |
1060 | /* bytes to complete the current nh_block. */ | |
1061 | if (bytes_hashed) { | |
1062 | bytes_remaining = (L1_KEY_LEN - bytes_hashed); | |
1063 | nh_update(&ctx->hash, (UINT8 *)input, bytes_remaining); | |
1064 | nh_final(&ctx->hash, nh_result); | |
1065 | ctx->msg_len += bytes_remaining; | |
1066 | poly_hash(ctx,(UINT32 *)nh_result); | |
1067 | len -= bytes_remaining; | |
1068 | input += bytes_remaining; | |
1069 | } | |
1070 | ||
1071 | /* Hash directly from input stream if enough bytes */ | |
1072 | while (len >= L1_KEY_LEN) { | |
1073 | nh(&ctx->hash, (UINT8 *)input, L1_KEY_LEN, | |
1074 | L1_KEY_LEN, nh_result); | |
1075 | ctx->msg_len += L1_KEY_LEN; | |
1076 | len -= L1_KEY_LEN; | |
1077 | input += L1_KEY_LEN; | |
1078 | poly_hash(ctx,(UINT32 *)nh_result); | |
1079 | } | |
1080 | } | |
1081 | ||
1082 | /* pass remaining < L1_KEY_LEN bytes of input data to NH */ | |
1083 | if (len) { | |
1084 | nh_update(&ctx->hash, (UINT8 *)input, len); | |
1085 | ctx->msg_len += len; | |
1086 | } | |
1087 | } | |
1088 | ||
1089 | return (1); | |
1090 | } | |
1091 | ||
1092 | /* ---------------------------------------------------------------------- */ | |
1093 | ||
1094 | static int uhash_final(uhash_ctx_t ctx, u_char *res) | |
1095 | /* Incorporate any pending data, pad, and generate tag */ | |
1096 | { | |
1097 | UINT8 nh_result[STREAMS*sizeof(UINT64)]; | |
1098 | ||
1099 | if (ctx->msg_len > L1_KEY_LEN) { | |
1100 | if (ctx->msg_len % L1_KEY_LEN) { | |
1101 | nh_final(&ctx->hash, nh_result); | |
1102 | poly_hash(ctx,(UINT32 *)nh_result); | |
1103 | } | |
1104 | ip_long(ctx, res); | |
1105 | } else { | |
1106 | nh_final(&ctx->hash, nh_result); | |
1107 | ip_short(ctx,nh_result, res); | |
1108 | } | |
1109 | uhash_reset(ctx); | |
1110 | return (1); | |
1111 | } | |
1112 | ||
1113 | /* ---------------------------------------------------------------------- */ | |
1114 | ||
1115 | #if 0 | |
1116 | static int uhash(uhash_ctx_t ahc, u_char *msg, long len, u_char *res) | |
1117 | /* assumes that msg is in a writable buffer of length divisible by */ | |
1118 | /* L1_PAD_BOUNDARY. Bytes beyond msg[len] may be zeroed. */ | |
1119 | { | |
1120 | UINT8 nh_result[STREAMS*sizeof(UINT64)]; | |
1121 | UINT32 nh_len; | |
1122 | int extra_zeroes_needed; | |
1123 | ||
1124 | /* If the message to be hashed is no longer than L1_HASH_LEN, we skip | |
1125 | * the polyhash. | |
1126 | */ | |
1127 | if (len <= L1_KEY_LEN) { | |
1128 | if (len == 0) /* If zero length messages will not */ | |
1129 | nh_len = L1_PAD_BOUNDARY; /* be seen, comment out this case */ | |
1130 | else | |
1131 | nh_len = ((len + (L1_PAD_BOUNDARY - 1)) & ~(L1_PAD_BOUNDARY - 1)); | |
1132 | extra_zeroes_needed = nh_len - len; | |
1133 | zero_pad((UINT8 *)msg + len, extra_zeroes_needed); | |
1134 | nh(&ahc->hash, (UINT8 *)msg, nh_len, len, nh_result); | |
1135 | ip_short(ahc,nh_result, res); | |
1136 | } else { | |
1137 | /* Otherwise, we hash each L1_KEY_LEN chunk with NH, passing the NH | |
1138 | * output to poly_hash(). | |
1139 | */ | |
1140 | do { | |
1141 | nh(&ahc->hash, (UINT8 *)msg, L1_KEY_LEN, L1_KEY_LEN, nh_result); | |
1142 | poly_hash(ahc,(UINT32 *)nh_result); | |
1143 | len -= L1_KEY_LEN; | |
1144 | msg += L1_KEY_LEN; | |
1145 | } while (len >= L1_KEY_LEN); | |
1146 | if (len) { | |
1147 | nh_len = ((len + (L1_PAD_BOUNDARY - 1)) & ~(L1_PAD_BOUNDARY - 1)); | |
1148 | extra_zeroes_needed = nh_len - len; | |
1149 | zero_pad((UINT8 *)msg + len, extra_zeroes_needed); | |
1150 | nh(&ahc->hash, (UINT8 *)msg, nh_len, len, nh_result); | |
1151 | poly_hash(ahc,(UINT32 *)nh_result); | |
1152 | } | |
1153 | ||
1154 | ip_long(ahc, res); | |
1155 | } | |
1156 | ||
1157 | uhash_reset(ahc); | |
1158 | return 1; | |
1159 | } | |
1160 | #endif | |
1161 | ||
1162 | /* ---------------------------------------------------------------------- */ | |
1163 | /* ---------------------------------------------------------------------- */ | |
1164 | /* ----- Begin UMAC Section --------------------------------------------- */ | |
1165 | /* ---------------------------------------------------------------------- */ | |
1166 | /* ---------------------------------------------------------------------- */ | |
1167 | ||
1168 | /* The UMAC interface has two interfaces, an all-at-once interface where | |
1169 | * the entire message to be authenticated is passed to UMAC in one buffer, | |
1170 | * and a sequential interface where the message is presented a little at a | |
1171 | * time. The all-at-once is more optimaized than the sequential version and | |
1172 | * should be preferred when the sequential interface is not required. | |
1173 | */ | |
1174 | struct umac_ctx { | |
1175 | uhash_ctx hash; /* Hash function for message compression */ | |
1176 | pdf_ctx pdf; /* PDF for hashed output */ | |
1177 | void *free_ptr; /* Address to free this struct via */ | |
1178 | } umac_ctx; | |
1179 | ||
1180 | /* ---------------------------------------------------------------------- */ | |
1181 | ||
1182 | #if 0 | |
1183 | int umac_reset(struct umac_ctx *ctx) | |
1184 | /* Reset the hash function to begin a new authentication. */ | |
1185 | { | |
1186 | uhash_reset(&ctx->hash); | |
1187 | return (1); | |
1188 | } | |
1189 | #endif | |
1190 | ||
1191 | /* ---------------------------------------------------------------------- */ | |
1192 | ||
1193 | int umac_delete(struct umac_ctx *ctx) | |
1194 | /* Deallocate the ctx structure */ | |
1195 | { | |
1196 | if (ctx) { | |
1197 | if (ALLOC_BOUNDARY) | |
1198 | ctx = (struct umac_ctx *)ctx->free_ptr; | |
1199 | free(ctx); | |
1200 | } | |
1201 | return (1); | |
1202 | } | |
1203 | ||
1204 | /* ---------------------------------------------------------------------- */ | |
1205 | ||
1206 | struct umac_ctx *umac_new(u_char key[]) | |
1207 | /* Dynamically allocate a umac_ctx struct, initialize variables, | |
1208 | * generate subkeys from key. Align to 16-byte boundary. | |
1209 | */ | |
1210 | { | |
1211 | struct umac_ctx *ctx, *octx; | |
1212 | size_t bytes_to_add; | |
1213 | aes_int_key prf_key; | |
1214 | ||
1215 | octx = ctx = malloc(sizeof(*ctx) + ALLOC_BOUNDARY); | |
1216 | if (ctx) { | |
1217 | if (ALLOC_BOUNDARY) { | |
1218 | bytes_to_add = ALLOC_BOUNDARY - | |
1219 | ((ptrdiff_t)ctx & (ALLOC_BOUNDARY - 1)); | |
1220 | ctx = (struct umac_ctx *)((u_char *)ctx + bytes_to_add); | |
1221 | } | |
1222 | ctx->free_ptr = octx; | |
1223 | aes_key_setup(key,prf_key); | |
1224 | pdf_init(&ctx->pdf, prf_key); | |
1225 | uhash_init(&ctx->hash, prf_key); | |
1226 | } | |
1227 | ||
1228 | return (ctx); | |
1229 | } | |
1230 | ||
1231 | /* ---------------------------------------------------------------------- */ | |
1232 | ||
1233 | int umac_final(struct umac_ctx *ctx, u_char tag[], u_char nonce[8]) | |
1234 | /* Incorporate any pending data, pad, and generate tag */ | |
1235 | { | |
1236 | uhash_final(&ctx->hash, (u_char *)tag); | |
1237 | pdf_gen_xor(&ctx->pdf, (UINT8 *)nonce, (UINT8 *)tag); | |
1238 | ||
1239 | return (1); | |
1240 | } | |
1241 | ||
1242 | /* ---------------------------------------------------------------------- */ | |
1243 | ||
1244 | int umac_update(struct umac_ctx *ctx, u_char *input, long len) | |
1245 | /* Given len bytes of data, we parse it into L1_KEY_LEN chunks and */ | |
1246 | /* hash each one, calling the PDF on the hashed output whenever the hash- */ | |
1247 | /* output buffer is full. */ | |
1248 | { | |
1249 | uhash_update(&ctx->hash, input, len); | |
1250 | return (1); | |
1251 | } | |
1252 | ||
1253 | /* ---------------------------------------------------------------------- */ | |
1254 | ||
1255 | #if 0 | |
1256 | int umac(struct umac_ctx *ctx, u_char *input, | |
1257 | long len, u_char tag[], | |
1258 | u_char nonce[8]) | |
1259 | /* All-in-one version simply calls umac_update() and umac_final(). */ | |
1260 | { | |
1261 | uhash(&ctx->hash, input, len, (u_char *)tag); | |
1262 | pdf_gen_xor(&ctx->pdf, (UINT8 *)nonce, (UINT8 *)tag); | |
1263 | ||
1264 | return (1); | |
1265 | } | |
1266 | #endif | |
1267 | ||
1268 | /* ---------------------------------------------------------------------- */ | |
1269 | /* ---------------------------------------------------------------------- */ | |
1270 | /* ----- End UMAC Section ----------------------------------------------- */ | |
1271 | /* ---------------------------------------------------------------------- */ | |
1272 | /* ---------------------------------------------------------------------- */ |