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