Debian Codegraph @

contrib/mod_sftp/umac.c

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