1 /* $OpenBSD: umac.c,v 1.8 2013/11/08 00:39:15 djm Exp $ */
2 /* -----------------------------------------------------------------------
4 * umac.c -- C Implementation UMAC Message Authentication
6 * Version 0.93b of rfc4418.txt -- 2006 July 18
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.
12 * Copyright (c) 1999-2006 Ted Krovetz
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.
21 * Comments should be directed to Ted Krovetz (tdk@acm.org)
23 * ---------------------------------------------------------------------- */
25 /* ////////////////////// IMPORTANT NOTES /////////////////////////////////
27 * 1) This version does not work properly on messages larger than 16MB
29 * 2) If you set the switch to use SSE2, then all data must be 16-byte
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
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
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.
49 /////////////////////////////////////////////////////////////////////// */
51 /* ---------------------------------------------------------------------- */
52 /* --- User Switches ---------------------------------------------------- */
53 /* ---------------------------------------------------------------------- */
55 #ifndef UMAC_OUTPUT_LEN
56 #define UMAC_OUTPUT_LEN 8 /* Alowable: 4, 8, 12, 16 */
59 #if UMAC_OUTPUT_LEN != 4 && UMAC_OUTPUT_LEN != 8 && \
60 UMAC_OUTPUT_LEN != 12 && UMAC_OUTPUT_LEN != 16
61 # error UMAC_OUTPUT_LEN must be defined to 4, 8, 12 or 16
64 /* #define FORCE_C_ONLY 1 ANSI C and 64-bit integers req'd */
65 /* #define AES_IMPLEMENTAION 1 1 = OpenSSL, 2 = Barreto, 3 = Gladman */
66 /* #define SSE2 0 Is SSE2 is available? */
67 /* #define RUN_TESTS 0 Run basic correctness/speed tests */
68 /* #define UMAC_AE_SUPPORT 0 Enable auhthenticated encrytion */
70 /* ---------------------------------------------------------------------- */
71 /* -- Global Includes --------------------------------------------------- */
72 /* ---------------------------------------------------------------------- */
75 #include <sys/types.h>
83 /* ---------------------------------------------------------------------- */
84 /* --- Primitive Data Types --- */
85 /* ---------------------------------------------------------------------- */
87 /* The following assumptions may need change on your system */
88 typedef u_int8_t UINT8; /* 1 byte */
89 typedef u_int16_t UINT16; /* 2 byte */
90 typedef u_int32_t UINT32; /* 4 byte */
91 typedef u_int64_t UINT64; /* 8 bytes */
92 typedef unsigned int UWORD; /* Register */
94 /* ---------------------------------------------------------------------- */
95 /* --- Constants -------------------------------------------------------- */
96 /* ---------------------------------------------------------------------- */
98 #define UMAC_KEY_LEN 16 /* UMAC takes 16 bytes of external key */
100 /* Message "words" are read from memory in an endian-specific manner. */
101 /* For this implementation to behave correctly, __LITTLE_ENDIAN__ must */
102 /* be set true if the host computer is little-endian. */
104 #if BYTE_ORDER == LITTLE_ENDIAN
105 #define __LITTLE_ENDIAN__ 1
107 #define __LITTLE_ENDIAN__ 0
110 /* ---------------------------------------------------------------------- */
111 /* ---------------------------------------------------------------------- */
112 /* ----- Architecture Specific ------------------------------------------ */
113 /* ---------------------------------------------------------------------- */
114 /* ---------------------------------------------------------------------- */
117 /* ---------------------------------------------------------------------- */
118 /* ---------------------------------------------------------------------- */
119 /* ----- Primitive Routines --------------------------------------------- */
120 /* ---------------------------------------------------------------------- */
121 /* ---------------------------------------------------------------------- */
124 /* ---------------------------------------------------------------------- */
125 /* --- 32-bit by 32-bit to 64-bit Multiplication ------------------------ */
126 /* ---------------------------------------------------------------------- */
128 #define MUL64(a,b) ((UINT64)((UINT64)(UINT32)(a) * (UINT64)(UINT32)(b)))
130 /* ---------------------------------------------------------------------- */
131 /* --- Endian Conversion --- Forcing assembly on some platforms */
132 /* ---------------------------------------------------------------------- */
135 #define LOAD_UINT32_REVERSED(p) (swap32(*(const UINT32 *)(p)))
136 #define STORE_UINT32_REVERSED(p,v) (*(UINT32 *)(p) = swap32(v))
137 #else /* HAVE_SWAP32 */
139 static UINT32 LOAD_UINT32_REVERSED(const void *ptr)
141 UINT32 temp = *(const UINT32 *)ptr;
142 temp = (temp >> 24) | ((temp & 0x00FF0000) >> 8 )
143 | ((temp & 0x0000FF00) << 8 ) | (temp << 24);
147 # if (__LITTLE_ENDIAN__)
148 static void STORE_UINT32_REVERSED(void *ptr, UINT32 x)
150 UINT32 i = (UINT32)x;
151 *(UINT32 *)ptr = (i >> 24) | ((i & 0x00FF0000) >> 8 )
152 | ((i & 0x0000FF00) << 8 ) | (i << 24);
154 # endif /* __LITTLE_ENDIAN */
155 #endif /* HAVE_SWAP32 */
157 /* The following definitions use the above reversal-primitives to do the right
158 * thing on endian specific load and stores.
161 #if (__LITTLE_ENDIAN__)
162 #define LOAD_UINT32_LITTLE(ptr) (*(const UINT32 *)(ptr))
163 #define STORE_UINT32_BIG(ptr,x) STORE_UINT32_REVERSED(ptr,x)
165 #define LOAD_UINT32_LITTLE(ptr) LOAD_UINT32_REVERSED(ptr)
166 #define STORE_UINT32_BIG(ptr,x) (*(UINT32 *)(ptr) = (UINT32)(x))
169 /* ---------------------------------------------------------------------- */
170 /* ---------------------------------------------------------------------- */
171 /* ----- Begin KDF & PDF Section ---------------------------------------- */
172 /* ---------------------------------------------------------------------- */
173 /* ---------------------------------------------------------------------- */
175 /* UMAC uses AES with 16 byte block and key lengths */
176 #define AES_BLOCK_LEN 16
179 #include "openbsd-compat/openssl-compat.h"
180 #ifndef USE_BUILTIN_RIJNDAEL
181 # include <openssl/aes.h>
183 typedef AES_KEY aes_int_key[1];
184 #define aes_encryption(in,out,int_key) \
185 AES_encrypt((u_char *)(in),(u_char *)(out),(AES_KEY *)int_key)
186 #define aes_key_setup(key,int_key) \
187 AES_set_encrypt_key((const u_char *)(key),UMAC_KEY_LEN*8,int_key)
189 /* The user-supplied UMAC key is stretched using AES in a counter
190 * mode to supply all random bits needed by UMAC. The kdf function takes
191 * an AES internal key representation 'key' and writes a stream of
192 * 'nbytes' bytes to the memory pointed at by 'bufp'. Each distinct
193 * 'ndx' causes a distinct byte stream.
195 static void kdf(void *bufp, aes_int_key key, UINT8 ndx, int nbytes)
197 UINT8 in_buf[AES_BLOCK_LEN] = {0};
198 UINT8 out_buf[AES_BLOCK_LEN];
199 UINT8 *dst_buf = (UINT8 *)bufp;
202 /* Setup the initial value */
203 in_buf[AES_BLOCK_LEN-9] = ndx;
204 in_buf[AES_BLOCK_LEN-1] = i = 1;
206 while (nbytes >= AES_BLOCK_LEN) {
207 aes_encryption(in_buf, out_buf, key);
208 memcpy(dst_buf,out_buf,AES_BLOCK_LEN);
209 in_buf[AES_BLOCK_LEN-1] = ++i;
210 nbytes -= AES_BLOCK_LEN;
211 dst_buf += AES_BLOCK_LEN;
214 aes_encryption(in_buf, out_buf, key);
215 memcpy(dst_buf,out_buf,nbytes);
219 /* The final UHASH result is XOR'd with the output of a pseudorandom
220 * function. Here, we use AES to generate random output and
221 * xor the appropriate bytes depending on the last bits of nonce.
222 * This scheme is optimized for sequential, increasing big-endian nonces.
226 UINT8 cache[AES_BLOCK_LEN]; /* Previous AES output is saved */
227 UINT8 nonce[AES_BLOCK_LEN]; /* The AES input making above cache */
228 aes_int_key prf_key; /* Expanded AES key for PDF */
231 static void pdf_init(pdf_ctx *pc, aes_int_key prf_key)
233 UINT8 buf[UMAC_KEY_LEN];
235 kdf(buf, prf_key, 0, UMAC_KEY_LEN);
236 aes_key_setup(buf, pc->prf_key);
238 /* Initialize pdf and cache */
239 memset(pc->nonce, 0, sizeof(pc->nonce));
240 aes_encryption(pc->nonce, pc->cache, pc->prf_key);
243 static void pdf_gen_xor(pdf_ctx *pc, const UINT8 nonce[8], UINT8 buf[8])
245 /* 'ndx' indicates that we'll be using the 0th or 1st eight bytes
246 * of the AES output. If last time around we returned the ndx-1st
247 * element, then we may have the result in the cache already.
250 #if (UMAC_OUTPUT_LEN == 4)
251 #define LOW_BIT_MASK 3
252 #elif (UMAC_OUTPUT_LEN == 8)
253 #define LOW_BIT_MASK 1
254 #elif (UMAC_OUTPUT_LEN > 8)
255 #define LOW_BIT_MASK 0
258 UINT8 tmp_nonce_lo[4];
261 #if LOW_BIT_MASK != 0
262 int ndx = nonce[7] & LOW_BIT_MASK;
264 *(UINT32 *)t.tmp_nonce_lo = ((const UINT32 *)nonce)[1];
265 t.tmp_nonce_lo[3] &= ~LOW_BIT_MASK; /* zero last bit */
267 if ( (((UINT32 *)t.tmp_nonce_lo)[0] != ((UINT32 *)pc->nonce)[1]) ||
268 (((const UINT32 *)nonce)[0] != ((UINT32 *)pc->nonce)[0]) )
270 ((UINT32 *)pc->nonce)[0] = ((const UINT32 *)nonce)[0];
271 ((UINT32 *)pc->nonce)[1] = ((UINT32 *)t.tmp_nonce_lo)[0];
272 aes_encryption(pc->nonce, pc->cache, pc->prf_key);
275 #if (UMAC_OUTPUT_LEN == 4)
276 *((UINT32 *)buf) ^= ((UINT32 *)pc->cache)[ndx];
277 #elif (UMAC_OUTPUT_LEN == 8)
278 *((UINT64 *)buf) ^= ((UINT64 *)pc->cache)[ndx];
279 #elif (UMAC_OUTPUT_LEN == 12)
280 ((UINT64 *)buf)[0] ^= ((UINT64 *)pc->cache)[0];
281 ((UINT32 *)buf)[2] ^= ((UINT32 *)pc->cache)[2];
282 #elif (UMAC_OUTPUT_LEN == 16)
283 ((UINT64 *)buf)[0] ^= ((UINT64 *)pc->cache)[0];
284 ((UINT64 *)buf)[1] ^= ((UINT64 *)pc->cache)[1];
288 /* ---------------------------------------------------------------------- */
289 /* ---------------------------------------------------------------------- */
290 /* ----- Begin NH Hash Section ------------------------------------------ */
291 /* ---------------------------------------------------------------------- */
292 /* ---------------------------------------------------------------------- */
294 /* The NH-based hash functions used in UMAC are described in the UMAC paper
295 * and specification, both of which can be found at the UMAC website.
296 * The interface to this implementation has two
297 * versions, one expects the entire message being hashed to be passed
298 * in a single buffer and returns the hash result immediately. The second
299 * allows the message to be passed in a sequence of buffers. In the
300 * muliple-buffer interface, the client calls the routine nh_update() as
301 * many times as necessary. When there is no more data to be fed to the
302 * hash, the client calls nh_final() which calculates the hash output.
303 * Before beginning another hash calculation the nh_reset() routine
304 * must be called. The single-buffer routine, nh(), is equivalent to
305 * the sequence of calls nh_update() and nh_final(); however it is
306 * optimized and should be prefered whenever the multiple-buffer interface
307 * is not necessary. When using either interface, it is the client's
308 * responsability to pass no more than L1_KEY_LEN bytes per hash result.
310 * The routine nh_init() initializes the nh_ctx data structure and
311 * must be called once, before any other PDF routine.
314 /* The "nh_aux" routines do the actual NH hashing work. They
315 * expect buffers to be multiples of L1_PAD_BOUNDARY. These routines
316 * produce output for all STREAMS NH iterations in one call,
317 * allowing the parallel implementation of the streams.
320 #define STREAMS (UMAC_OUTPUT_LEN / 4) /* Number of times hash is applied */
321 #define L1_KEY_LEN 1024 /* Internal key bytes */
322 #define L1_KEY_SHIFT 16 /* Toeplitz key shift between streams */
323 #define L1_PAD_BOUNDARY 32 /* pad message to boundary multiple */
324 #define ALLOC_BOUNDARY 16 /* Keep buffers aligned to this */
325 #define HASH_BUF_BYTES 64 /* nh_aux_hb buffer multiple */
328 UINT8 nh_key [L1_KEY_LEN + L1_KEY_SHIFT * (STREAMS - 1)]; /* NH Key */
329 UINT8 data [HASH_BUF_BYTES]; /* Incoming data buffer */
330 int next_data_empty; /* Bookeeping variable for data buffer. */
331 int bytes_hashed; /* Bytes (out of L1_KEY_LEN) incorperated. */
332 UINT64 state[STREAMS]; /* on-line state */
336 #if (UMAC_OUTPUT_LEN == 4)
338 static void nh_aux(void *kp, const void *dp, void *hp, UINT32 dlen)
339 /* NH hashing primitive. Previous (partial) hash result is loaded and
340 * then stored via hp pointer. The length of the data pointed at by "dp",
341 * "dlen", is guaranteed to be divisible by L1_PAD_BOUNDARY (32). Key
342 * is expected to be endian compensated in memory at key setup.
347 UINT32 *k = (UINT32 *)kp;
348 const UINT32 *d = (const UINT32 *)dp;
349 UINT32 d0,d1,d2,d3,d4,d5,d6,d7;
350 UINT32 k0,k1,k2,k3,k4,k5,k6,k7;
354 d0 = LOAD_UINT32_LITTLE(d+0); d1 = LOAD_UINT32_LITTLE(d+1);
355 d2 = LOAD_UINT32_LITTLE(d+2); d3 = LOAD_UINT32_LITTLE(d+3);
356 d4 = LOAD_UINT32_LITTLE(d+4); d5 = LOAD_UINT32_LITTLE(d+5);
357 d6 = LOAD_UINT32_LITTLE(d+6); d7 = LOAD_UINT32_LITTLE(d+7);
358 k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3);
359 k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7);
360 h += MUL64((k0 + d0), (k4 + d4));
361 h += MUL64((k1 + d1), (k5 + d5));
362 h += MUL64((k2 + d2), (k6 + d6));
363 h += MUL64((k3 + d3), (k7 + d7));
371 #elif (UMAC_OUTPUT_LEN == 8)
373 static void nh_aux(void *kp, const void *dp, void *hp, UINT32 dlen)
374 /* Same as previous nh_aux, but two streams are handled in one pass,
375 * reading and writing 16 bytes of hash-state per call.
380 UINT32 *k = (UINT32 *)kp;
381 const UINT32 *d = (const UINT32 *)dp;
382 UINT32 d0,d1,d2,d3,d4,d5,d6,d7;
383 UINT32 k0,k1,k2,k3,k4,k5,k6,k7,
386 h1 = *((UINT64 *)hp);
387 h2 = *((UINT64 *)hp + 1);
388 k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3);
390 d0 = LOAD_UINT32_LITTLE(d+0); d1 = LOAD_UINT32_LITTLE(d+1);
391 d2 = LOAD_UINT32_LITTLE(d+2); d3 = LOAD_UINT32_LITTLE(d+3);
392 d4 = LOAD_UINT32_LITTLE(d+4); d5 = LOAD_UINT32_LITTLE(d+5);
393 d6 = LOAD_UINT32_LITTLE(d+6); d7 = LOAD_UINT32_LITTLE(d+7);
394 k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7);
395 k8 = *(k+8); k9 = *(k+9); k10 = *(k+10); k11 = *(k+11);
397 h1 += MUL64((k0 + d0), (k4 + d4));
398 h2 += MUL64((k4 + d0), (k8 + d4));
400 h1 += MUL64((k1 + d1), (k5 + d5));
401 h2 += MUL64((k5 + d1), (k9 + d5));
403 h1 += MUL64((k2 + d2), (k6 + d6));
404 h2 += MUL64((k6 + d2), (k10 + d6));
406 h1 += MUL64((k3 + d3), (k7 + d7));
407 h2 += MUL64((k7 + d3), (k11 + d7));
409 k0 = k8; k1 = k9; k2 = k10; k3 = k11;
414 ((UINT64 *)hp)[0] = h1;
415 ((UINT64 *)hp)[1] = h2;
418 #elif (UMAC_OUTPUT_LEN == 12)
420 static void nh_aux(void *kp, const void *dp, void *hp, UINT32 dlen)
421 /* Same as previous nh_aux, but two streams are handled in one pass,
422 * reading and writing 24 bytes of hash-state per call.
427 UINT32 *k = (UINT32 *)kp;
428 const UINT32 *d = (const UINT32 *)dp;
429 UINT32 d0,d1,d2,d3,d4,d5,d6,d7;
430 UINT32 k0,k1,k2,k3,k4,k5,k6,k7,
431 k8,k9,k10,k11,k12,k13,k14,k15;
433 h1 = *((UINT64 *)hp);
434 h2 = *((UINT64 *)hp + 1);
435 h3 = *((UINT64 *)hp + 2);
436 k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3);
437 k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7);
439 d0 = LOAD_UINT32_LITTLE(d+0); d1 = LOAD_UINT32_LITTLE(d+1);
440 d2 = LOAD_UINT32_LITTLE(d+2); d3 = LOAD_UINT32_LITTLE(d+3);
441 d4 = LOAD_UINT32_LITTLE(d+4); d5 = LOAD_UINT32_LITTLE(d+5);
442 d6 = LOAD_UINT32_LITTLE(d+6); d7 = LOAD_UINT32_LITTLE(d+7);
443 k8 = *(k+8); k9 = *(k+9); k10 = *(k+10); k11 = *(k+11);
444 k12 = *(k+12); k13 = *(k+13); k14 = *(k+14); k15 = *(k+15);
446 h1 += MUL64((k0 + d0), (k4 + d4));
447 h2 += MUL64((k4 + d0), (k8 + d4));
448 h3 += MUL64((k8 + d0), (k12 + d4));
450 h1 += MUL64((k1 + d1), (k5 + d5));
451 h2 += MUL64((k5 + d1), (k9 + d5));
452 h3 += MUL64((k9 + d1), (k13 + d5));
454 h1 += MUL64((k2 + d2), (k6 + d6));
455 h2 += MUL64((k6 + d2), (k10 + d6));
456 h3 += MUL64((k10 + d2), (k14 + d6));
458 h1 += MUL64((k3 + d3), (k7 + d7));
459 h2 += MUL64((k7 + d3), (k11 + d7));
460 h3 += MUL64((k11 + d3), (k15 + d7));
462 k0 = k8; k1 = k9; k2 = k10; k3 = k11;
463 k4 = k12; k5 = k13; k6 = k14; k7 = k15;
468 ((UINT64 *)hp)[0] = h1;
469 ((UINT64 *)hp)[1] = h2;
470 ((UINT64 *)hp)[2] = h3;
473 #elif (UMAC_OUTPUT_LEN == 16)
475 static void nh_aux(void *kp, const void *dp, void *hp, UINT32 dlen)
476 /* Same as previous nh_aux, but two streams are handled in one pass,
477 * reading and writing 24 bytes of hash-state per call.
482 UINT32 *k = (UINT32 *)kp;
483 const UINT32 *d = (const UINT32 *)dp;
484 UINT32 d0,d1,d2,d3,d4,d5,d6,d7;
485 UINT32 k0,k1,k2,k3,k4,k5,k6,k7,
486 k8,k9,k10,k11,k12,k13,k14,k15,
489 h1 = *((UINT64 *)hp);
490 h2 = *((UINT64 *)hp + 1);
491 h3 = *((UINT64 *)hp + 2);
492 h4 = *((UINT64 *)hp + 3);
493 k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3);
494 k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7);
496 d0 = LOAD_UINT32_LITTLE(d+0); d1 = LOAD_UINT32_LITTLE(d+1);
497 d2 = LOAD_UINT32_LITTLE(d+2); d3 = LOAD_UINT32_LITTLE(d+3);
498 d4 = LOAD_UINT32_LITTLE(d+4); d5 = LOAD_UINT32_LITTLE(d+5);
499 d6 = LOAD_UINT32_LITTLE(d+6); d7 = LOAD_UINT32_LITTLE(d+7);
500 k8 = *(k+8); k9 = *(k+9); k10 = *(k+10); k11 = *(k+11);
501 k12 = *(k+12); k13 = *(k+13); k14 = *(k+14); k15 = *(k+15);
502 k16 = *(k+16); k17 = *(k+17); k18 = *(k+18); k19 = *(k+19);
504 h1 += MUL64((k0 + d0), (k4 + d4));
505 h2 += MUL64((k4 + d0), (k8 + d4));
506 h3 += MUL64((k8 + d0), (k12 + d4));
507 h4 += MUL64((k12 + d0), (k16 + d4));
509 h1 += MUL64((k1 + d1), (k5 + d5));
510 h2 += MUL64((k5 + d1), (k9 + d5));
511 h3 += MUL64((k9 + d1), (k13 + d5));
512 h4 += MUL64((k13 + d1), (k17 + d5));
514 h1 += MUL64((k2 + d2), (k6 + d6));
515 h2 += MUL64((k6 + d2), (k10 + d6));
516 h3 += MUL64((k10 + d2), (k14 + d6));
517 h4 += MUL64((k14 + d2), (k18 + d6));
519 h1 += MUL64((k3 + d3), (k7 + d7));
520 h2 += MUL64((k7 + d3), (k11 + d7));
521 h3 += MUL64((k11 + d3), (k15 + d7));
522 h4 += MUL64((k15 + d3), (k19 + d7));
524 k0 = k8; k1 = k9; k2 = k10; k3 = k11;
525 k4 = k12; k5 = k13; k6 = k14; k7 = k15;
526 k8 = k16; k9 = k17; k10 = k18; k11 = k19;
531 ((UINT64 *)hp)[0] = h1;
532 ((UINT64 *)hp)[1] = h2;
533 ((UINT64 *)hp)[2] = h3;
534 ((UINT64 *)hp)[3] = h4;
537 /* ---------------------------------------------------------------------- */
538 #endif /* UMAC_OUTPUT_LENGTH */
539 /* ---------------------------------------------------------------------- */
542 /* ---------------------------------------------------------------------- */
544 static void nh_transform(nh_ctx *hc, const UINT8 *buf, UINT32 nbytes)
545 /* This function is a wrapper for the primitive NH hash functions. It takes
546 * as argument "hc" the current hash context and a buffer which must be a
547 * multiple of L1_PAD_BOUNDARY. The key passed to nh_aux is offset
548 * appropriately according to how much message has been hashed already.
553 key = hc->nh_key + hc->bytes_hashed;
554 nh_aux(key, buf, hc->state, nbytes);
557 /* ---------------------------------------------------------------------- */
559 #if (__LITTLE_ENDIAN__)
560 static void endian_convert(void *buf, UWORD bpw, UINT32 num_bytes)
561 /* We endian convert the keys on little-endian computers to */
562 /* compensate for the lack of big-endian memory reads during hashing. */
564 UWORD iters = num_bytes / bpw;
566 UINT32 *p = (UINT32 *)buf;
568 *p = LOAD_UINT32_REVERSED(p);
571 } else if (bpw == 8) {
572 UINT32 *p = (UINT32 *)buf;
575 t = LOAD_UINT32_REVERSED(p+1);
576 p[1] = LOAD_UINT32_REVERSED(p);
582 #define endian_convert_if_le(x,y,z) endian_convert((x),(y),(z))
584 #define endian_convert_if_le(x,y,z) do{}while(0) /* Do nothing */
587 /* ---------------------------------------------------------------------- */
589 static void nh_reset(nh_ctx *hc)
590 /* Reset nh_ctx to ready for hashing of new data */
592 hc->bytes_hashed = 0;
593 hc->next_data_empty = 0;
595 #if (UMAC_OUTPUT_LEN >= 8)
598 #if (UMAC_OUTPUT_LEN >= 12)
601 #if (UMAC_OUTPUT_LEN == 16)
607 /* ---------------------------------------------------------------------- */
609 static void nh_init(nh_ctx *hc, aes_int_key prf_key)
610 /* Generate nh_key, endian convert and reset to be ready for hashing. */
612 kdf(hc->nh_key, prf_key, 1, sizeof(hc->nh_key));
613 endian_convert_if_le(hc->nh_key, 4, sizeof(hc->nh_key));
617 /* ---------------------------------------------------------------------- */
619 static void nh_update(nh_ctx *hc, const UINT8 *buf, UINT32 nbytes)
620 /* Incorporate nbytes of data into a nh_ctx, buffer whatever is not an */
621 /* even multiple of HASH_BUF_BYTES. */
625 j = hc->next_data_empty;
626 if ((j + nbytes) >= HASH_BUF_BYTES) {
628 i = HASH_BUF_BYTES - j;
629 memcpy(hc->data+j, buf, i);
630 nh_transform(hc,hc->data,HASH_BUF_BYTES);
633 hc->bytes_hashed += HASH_BUF_BYTES;
635 if (nbytes >= HASH_BUF_BYTES) {
636 i = nbytes & ~(HASH_BUF_BYTES - 1);
637 nh_transform(hc, buf, i);
640 hc->bytes_hashed += i;
644 memcpy(hc->data + j, buf, nbytes);
645 hc->next_data_empty = j + nbytes;
648 /* ---------------------------------------------------------------------- */
650 static void zero_pad(UINT8 *p, int nbytes)
652 /* Write "nbytes" of zeroes, beginning at "p" */
653 if (nbytes >= (int)sizeof(UWORD)) {
654 while ((ptrdiff_t)p % sizeof(UWORD)) {
659 while (nbytes >= (int)sizeof(UWORD)) {
661 nbytes -= sizeof(UWORD);
672 /* ---------------------------------------------------------------------- */
674 static void nh_final(nh_ctx *hc, UINT8 *result)
675 /* After passing some number of data buffers to nh_update() for integration
676 * into an NH context, nh_final is called to produce a hash result. If any
677 * bytes are in the buffer hc->data, incorporate them into the
678 * NH context. Finally, add into the NH accumulation "state" the total number
679 * of bits hashed. The resulting numbers are written to the buffer "result".
680 * If nh_update was never called, L1_PAD_BOUNDARY zeroes are incorporated.
685 if (hc->next_data_empty != 0) {
686 nh_len = ((hc->next_data_empty + (L1_PAD_BOUNDARY - 1)) &
687 ~(L1_PAD_BOUNDARY - 1));
688 zero_pad(hc->data + hc->next_data_empty,
689 nh_len - hc->next_data_empty);
690 nh_transform(hc, hc->data, nh_len);
691 hc->bytes_hashed += hc->next_data_empty;
692 } else if (hc->bytes_hashed == 0) {
693 nh_len = L1_PAD_BOUNDARY;
694 zero_pad(hc->data, L1_PAD_BOUNDARY);
695 nh_transform(hc, hc->data, nh_len);
698 nbits = (hc->bytes_hashed << 3);
699 ((UINT64 *)result)[0] = ((UINT64 *)hc->state)[0] + nbits;
700 #if (UMAC_OUTPUT_LEN >= 8)
701 ((UINT64 *)result)[1] = ((UINT64 *)hc->state)[1] + nbits;
703 #if (UMAC_OUTPUT_LEN >= 12)
704 ((UINT64 *)result)[2] = ((UINT64 *)hc->state)[2] + nbits;
706 #if (UMAC_OUTPUT_LEN == 16)
707 ((UINT64 *)result)[3] = ((UINT64 *)hc->state)[3] + nbits;
712 /* ---------------------------------------------------------------------- */
714 static void nh(nh_ctx *hc, const UINT8 *buf, UINT32 padded_len,
715 UINT32 unpadded_len, UINT8 *result)
716 /* All-in-one nh_update() and nh_final() equivalent.
717 * Assumes that padded_len is divisible by L1_PAD_BOUNDARY and result is
723 /* Initialize the hash state */
724 nbits = (unpadded_len << 3);
726 ((UINT64 *)result)[0] = nbits;
727 #if (UMAC_OUTPUT_LEN >= 8)
728 ((UINT64 *)result)[1] = nbits;
730 #if (UMAC_OUTPUT_LEN >= 12)
731 ((UINT64 *)result)[2] = nbits;
733 #if (UMAC_OUTPUT_LEN == 16)
734 ((UINT64 *)result)[3] = nbits;
737 nh_aux(hc->nh_key, buf, result, padded_len);
740 /* ---------------------------------------------------------------------- */
741 /* ---------------------------------------------------------------------- */
742 /* ----- Begin UHASH Section -------------------------------------------- */
743 /* ---------------------------------------------------------------------- */
744 /* ---------------------------------------------------------------------- */
746 /* UHASH is a multi-layered algorithm. Data presented to UHASH is first
747 * hashed by NH. The NH output is then hashed by a polynomial-hash layer
748 * unless the initial data to be hashed is short. After the polynomial-
749 * layer, an inner-product hash is used to produce the final UHASH output.
751 * UHASH provides two interfaces, one all-at-once and another where data
752 * buffers are presented sequentially. In the sequential interface, the
753 * UHASH client calls the routine uhash_update() as many times as necessary.
754 * When there is no more data to be fed to UHASH, the client calls
755 * uhash_final() which
756 * calculates the UHASH output. Before beginning another UHASH calculation
757 * the uhash_reset() routine must be called. The all-at-once UHASH routine,
758 * uhash(), is equivalent to the sequence of calls uhash_update() and
759 * uhash_final(); however it is optimized and should be
760 * used whenever the sequential interface is not necessary.
762 * The routine uhash_init() initializes the uhash_ctx data structure and
763 * must be called once, before any other UHASH routine.
766 /* ---------------------------------------------------------------------- */
767 /* ----- Constants and uhash_ctx ---------------------------------------- */
768 /* ---------------------------------------------------------------------- */
770 /* ---------------------------------------------------------------------- */
771 /* ----- Poly hash and Inner-Product hash Constants --------------------- */
772 /* ---------------------------------------------------------------------- */
774 /* Primes and masks */
775 #define p36 ((UINT64)0x0000000FFFFFFFFBull) /* 2^36 - 5 */
776 #define p64 ((UINT64)0xFFFFFFFFFFFFFFC5ull) /* 2^64 - 59 */
777 #define m36 ((UINT64)0x0000000FFFFFFFFFull) /* The low 36 of 64 bits */
780 /* ---------------------------------------------------------------------- */
782 typedef struct uhash_ctx {
783 nh_ctx hash; /* Hash context for L1 NH hash */
784 UINT64 poly_key_8[STREAMS]; /* p64 poly keys */
785 UINT64 poly_accum[STREAMS]; /* poly hash result */
786 UINT64 ip_keys[STREAMS*4]; /* Inner-product keys */
787 UINT32 ip_trans[STREAMS]; /* Inner-product translation */
788 UINT32 msg_len; /* Total length of data passed */
791 typedef struct uhash_ctx *uhash_ctx_t;
793 /* ---------------------------------------------------------------------- */
796 /* The polynomial hashes use Horner's rule to evaluate a polynomial one
797 * word at a time. As described in the specification, poly32 and poly64
798 * require keys from special domains. The following implementations exploit
799 * the special domains to avoid overflow. The results are not guaranteed to
800 * be within Z_p32 and Z_p64, but the Inner-Product hash implementation
801 * patches any errant values.
804 static UINT64 poly64(UINT64 cur, UINT64 key, UINT64 data)
806 UINT32 key_hi = (UINT32)(key >> 32),
807 key_lo = (UINT32)key,
808 cur_hi = (UINT32)(cur >> 32),
809 cur_lo = (UINT32)cur,
814 X = MUL64(key_hi, cur_lo) + MUL64(cur_hi, key_lo);
816 x_hi = (UINT32)(X >> 32);
818 res = (MUL64(key_hi, cur_hi) + x_hi) * 59 + MUL64(key_lo, cur_lo);
820 T = ((UINT64)x_lo << 32);
833 /* Although UMAC is specified to use a ramped polynomial hash scheme, this
834 * implementation does not handle all ramp levels. Because we don't handle
835 * the ramp up to p128 modulus in this implementation, we are limited to
836 * 2^14 poly_hash() invocations per stream (for a total capacity of 2^24
837 * bytes input to UMAC per tag, ie. 16MB).
839 static void poly_hash(uhash_ctx_t hc, UINT32 data_in[])
842 UINT64 *data=(UINT64*)data_in;
844 for (i = 0; i < STREAMS; i++) {
845 if ((UINT32)(data[i] >> 32) == 0xfffffffful) {
846 hc->poly_accum[i] = poly64(hc->poly_accum[i],
847 hc->poly_key_8[i], p64 - 1);
848 hc->poly_accum[i] = poly64(hc->poly_accum[i],
849 hc->poly_key_8[i], (data[i] - 59));
851 hc->poly_accum[i] = poly64(hc->poly_accum[i],
852 hc->poly_key_8[i], data[i]);
858 /* ---------------------------------------------------------------------- */
861 /* The final step in UHASH is an inner-product hash. The poly hash
862 * produces a result not neccesarily WORD_LEN bytes long. The inner-
863 * product hash breaks the polyhash output into 16-bit chunks and
864 * multiplies each with a 36 bit key.
867 static UINT64 ip_aux(UINT64 t, UINT64 *ipkp, UINT64 data)
869 t = t + ipkp[0] * (UINT64)(UINT16)(data >> 48);
870 t = t + ipkp[1] * (UINT64)(UINT16)(data >> 32);
871 t = t + ipkp[2] * (UINT64)(UINT16)(data >> 16);
872 t = t + ipkp[3] * (UINT64)(UINT16)(data);
877 static UINT32 ip_reduce_p36(UINT64 t)
879 /* Divisionless modular reduction */
882 ret = (t & m36) + 5 * (t >> 36);
886 /* return least significant 32 bits */
887 return (UINT32)(ret);
891 /* If the data being hashed by UHASH is no longer than L1_KEY_LEN, then
892 * the polyhash stage is skipped and ip_short is applied directly to the
895 static void ip_short(uhash_ctx_t ahc, UINT8 *nh_res, u_char *res)
898 UINT64 *nhp = (UINT64 *)nh_res;
900 t = ip_aux(0,ahc->ip_keys, nhp[0]);
901 STORE_UINT32_BIG((UINT32 *)res+0, ip_reduce_p36(t) ^ ahc->ip_trans[0]);
902 #if (UMAC_OUTPUT_LEN >= 8)
903 t = ip_aux(0,ahc->ip_keys+4, nhp[1]);
904 STORE_UINT32_BIG((UINT32 *)res+1, ip_reduce_p36(t) ^ ahc->ip_trans[1]);
906 #if (UMAC_OUTPUT_LEN >= 12)
907 t = ip_aux(0,ahc->ip_keys+8, nhp[2]);
908 STORE_UINT32_BIG((UINT32 *)res+2, ip_reduce_p36(t) ^ ahc->ip_trans[2]);
910 #if (UMAC_OUTPUT_LEN == 16)
911 t = ip_aux(0,ahc->ip_keys+12, nhp[3]);
912 STORE_UINT32_BIG((UINT32 *)res+3, ip_reduce_p36(t) ^ ahc->ip_trans[3]);
916 /* If the data being hashed by UHASH is longer than L1_KEY_LEN, then
917 * the polyhash stage is not skipped and ip_long is applied to the
920 static void ip_long(uhash_ctx_t ahc, u_char *res)
925 for (i = 0; i < STREAMS; i++) {
926 /* fix polyhash output not in Z_p64 */
927 if (ahc->poly_accum[i] >= p64)
928 ahc->poly_accum[i] -= p64;
929 t = ip_aux(0,ahc->ip_keys+(i*4), ahc->poly_accum[i]);
930 STORE_UINT32_BIG((UINT32 *)res+i,
931 ip_reduce_p36(t) ^ ahc->ip_trans[i]);
936 /* ---------------------------------------------------------------------- */
938 /* ---------------------------------------------------------------------- */
940 /* Reset uhash context for next hash session */
941 static int uhash_reset(uhash_ctx_t pc)
945 pc->poly_accum[0] = 1;
946 #if (UMAC_OUTPUT_LEN >= 8)
947 pc->poly_accum[1] = 1;
949 #if (UMAC_OUTPUT_LEN >= 12)
950 pc->poly_accum[2] = 1;
952 #if (UMAC_OUTPUT_LEN == 16)
953 pc->poly_accum[3] = 1;
958 /* ---------------------------------------------------------------------- */
960 /* Given a pointer to the internal key needed by kdf() and a uhash context,
961 * initialize the NH context and generate keys needed for poly and inner-
962 * product hashing. All keys are endian adjusted in memory so that native
963 * loads cause correct keys to be in registers during calculation.
965 static void uhash_init(uhash_ctx_t ahc, aes_int_key prf_key)
968 UINT8 buf[(8*STREAMS+4)*sizeof(UINT64)];
970 /* Zero the entire uhash context */
971 memset(ahc, 0, sizeof(uhash_ctx));
973 /* Initialize the L1 hash */
974 nh_init(&ahc->hash, prf_key);
976 /* Setup L2 hash variables */
977 kdf(buf, prf_key, 2, sizeof(buf)); /* Fill buffer with index 1 key */
978 for (i = 0; i < STREAMS; i++) {
979 /* Fill keys from the buffer, skipping bytes in the buffer not
980 * used by this implementation. Endian reverse the keys if on a
981 * little-endian computer.
983 memcpy(ahc->poly_key_8+i, buf+24*i, 8);
984 endian_convert_if_le(ahc->poly_key_8+i, 8, 8);
985 /* Mask the 64-bit keys to their special domain */
986 ahc->poly_key_8[i] &= ((UINT64)0x01ffffffu << 32) + 0x01ffffffu;
987 ahc->poly_accum[i] = 1; /* Our polyhash prepends a non-zero word */
990 /* Setup L3-1 hash variables */
991 kdf(buf, prf_key, 3, sizeof(buf)); /* Fill buffer with index 2 key */
992 for (i = 0; i < STREAMS; i++)
993 memcpy(ahc->ip_keys+4*i, buf+(8*i+4)*sizeof(UINT64),
995 endian_convert_if_le(ahc->ip_keys, sizeof(UINT64),
996 sizeof(ahc->ip_keys));
997 for (i = 0; i < STREAMS*4; i++)
998 ahc->ip_keys[i] %= p36; /* Bring into Z_p36 */
1000 /* Setup L3-2 hash variables */
1001 /* Fill buffer with index 4 key */
1002 kdf(ahc->ip_trans, prf_key, 4, STREAMS * sizeof(UINT32));
1003 endian_convert_if_le(ahc->ip_trans, sizeof(UINT32),
1004 STREAMS * sizeof(UINT32));
1007 /* ---------------------------------------------------------------------- */
1010 static uhash_ctx_t uhash_alloc(u_char key[])
1012 /* Allocate memory and force to a 16-byte boundary. */
1014 u_char bytes_to_add;
1015 aes_int_key prf_key;
1017 ctx = (uhash_ctx_t)malloc(sizeof(uhash_ctx)+ALLOC_BOUNDARY);
1019 if (ALLOC_BOUNDARY) {
1020 bytes_to_add = ALLOC_BOUNDARY -
1021 ((ptrdiff_t)ctx & (ALLOC_BOUNDARY -1));
1022 ctx = (uhash_ctx_t)((u_char *)ctx + bytes_to_add);
1023 *((u_char *)ctx - 1) = bytes_to_add;
1025 aes_key_setup(key,prf_key);
1026 uhash_init(ctx, prf_key);
1032 /* ---------------------------------------------------------------------- */
1035 static int uhash_free(uhash_ctx_t ctx)
1037 /* Free memory allocated by uhash_alloc */
1038 u_char bytes_to_sub;
1041 if (ALLOC_BOUNDARY) {
1042 bytes_to_sub = *((u_char *)ctx - 1);
1043 ctx = (uhash_ctx_t)((u_char *)ctx - bytes_to_sub);
1050 /* ---------------------------------------------------------------------- */
1052 static int uhash_update(uhash_ctx_t ctx, const u_char *input, long len)
1053 /* Given len bytes of data, we parse it into L1_KEY_LEN chunks and
1054 * hash each one with NH, calling the polyhash on each NH output.
1057 UWORD bytes_hashed, bytes_remaining;
1058 UINT64 result_buf[STREAMS];
1059 UINT8 *nh_result = (UINT8 *)&result_buf;
1061 if (ctx->msg_len + len <= L1_KEY_LEN) {
1062 nh_update(&ctx->hash, (const UINT8 *)input, len);
1063 ctx->msg_len += len;
1066 bytes_hashed = ctx->msg_len % L1_KEY_LEN;
1067 if (ctx->msg_len == L1_KEY_LEN)
1068 bytes_hashed = L1_KEY_LEN;
1070 if (bytes_hashed + len >= L1_KEY_LEN) {
1072 /* If some bytes have been passed to the hash function */
1073 /* then we want to pass at most (L1_KEY_LEN - bytes_hashed) */
1074 /* bytes to complete the current nh_block. */
1076 bytes_remaining = (L1_KEY_LEN - bytes_hashed);
1077 nh_update(&ctx->hash, (const UINT8 *)input, bytes_remaining);
1078 nh_final(&ctx->hash, nh_result);
1079 ctx->msg_len += bytes_remaining;
1080 poly_hash(ctx,(UINT32 *)nh_result);
1081 len -= bytes_remaining;
1082 input += bytes_remaining;
1085 /* Hash directly from input stream if enough bytes */
1086 while (len >= L1_KEY_LEN) {
1087 nh(&ctx->hash, (const UINT8 *)input, L1_KEY_LEN,
1088 L1_KEY_LEN, nh_result);
1089 ctx->msg_len += L1_KEY_LEN;
1091 input += L1_KEY_LEN;
1092 poly_hash(ctx,(UINT32 *)nh_result);
1096 /* pass remaining < L1_KEY_LEN bytes of input data to NH */
1098 nh_update(&ctx->hash, (const UINT8 *)input, len);
1099 ctx->msg_len += len;
1106 /* ---------------------------------------------------------------------- */
1108 static int uhash_final(uhash_ctx_t ctx, u_char *res)
1109 /* Incorporate any pending data, pad, and generate tag */
1111 UINT64 result_buf[STREAMS];
1112 UINT8 *nh_result = (UINT8 *)&result_buf;
1114 if (ctx->msg_len > L1_KEY_LEN) {
1115 if (ctx->msg_len % L1_KEY_LEN) {
1116 nh_final(&ctx->hash, nh_result);
1117 poly_hash(ctx,(UINT32 *)nh_result);
1121 nh_final(&ctx->hash, nh_result);
1122 ip_short(ctx,nh_result, res);
1128 /* ---------------------------------------------------------------------- */
1131 static int uhash(uhash_ctx_t ahc, u_char *msg, long len, u_char *res)
1132 /* assumes that msg is in a writable buffer of length divisible by */
1133 /* L1_PAD_BOUNDARY. Bytes beyond msg[len] may be zeroed. */
1135 UINT8 nh_result[STREAMS*sizeof(UINT64)];
1137 int extra_zeroes_needed;
1139 /* If the message to be hashed is no longer than L1_HASH_LEN, we skip
1142 if (len <= L1_KEY_LEN) {
1143 if (len == 0) /* If zero length messages will not */
1144 nh_len = L1_PAD_BOUNDARY; /* be seen, comment out this case */
1146 nh_len = ((len + (L1_PAD_BOUNDARY - 1)) & ~(L1_PAD_BOUNDARY - 1));
1147 extra_zeroes_needed = nh_len - len;
1148 zero_pad((UINT8 *)msg + len, extra_zeroes_needed);
1149 nh(&ahc->hash, (UINT8 *)msg, nh_len, len, nh_result);
1150 ip_short(ahc,nh_result, res);
1152 /* Otherwise, we hash each L1_KEY_LEN chunk with NH, passing the NH
1153 * output to poly_hash().
1156 nh(&ahc->hash, (UINT8 *)msg, L1_KEY_LEN, L1_KEY_LEN, nh_result);
1157 poly_hash(ahc,(UINT32 *)nh_result);
1160 } while (len >= L1_KEY_LEN);
1162 nh_len = ((len + (L1_PAD_BOUNDARY - 1)) & ~(L1_PAD_BOUNDARY - 1));
1163 extra_zeroes_needed = nh_len - len;
1164 zero_pad((UINT8 *)msg + len, extra_zeroes_needed);
1165 nh(&ahc->hash, (UINT8 *)msg, nh_len, len, nh_result);
1166 poly_hash(ahc,(UINT32 *)nh_result);
1177 /* ---------------------------------------------------------------------- */
1178 /* ---------------------------------------------------------------------- */
1179 /* ----- Begin UMAC Section --------------------------------------------- */
1180 /* ---------------------------------------------------------------------- */
1181 /* ---------------------------------------------------------------------- */
1183 /* The UMAC interface has two interfaces, an all-at-once interface where
1184 * the entire message to be authenticated is passed to UMAC in one buffer,
1185 * and a sequential interface where the message is presented a little at a
1186 * time. The all-at-once is more optimaized than the sequential version and
1187 * should be preferred when the sequential interface is not required.
1190 uhash_ctx hash; /* Hash function for message compression */
1191 pdf_ctx pdf; /* PDF for hashed output */
1192 void *free_ptr; /* Address to free this struct via */
1195 /* ---------------------------------------------------------------------- */
1198 int umac_reset(struct umac_ctx *ctx)
1199 /* Reset the hash function to begin a new authentication. */
1201 uhash_reset(&ctx->hash);
1206 /* ---------------------------------------------------------------------- */
1208 int umac_delete(struct umac_ctx *ctx)
1209 /* Deallocate the ctx structure */
1213 ctx = (struct umac_ctx *)ctx->free_ptr;
1219 /* ---------------------------------------------------------------------- */
1221 struct umac_ctx *umac_new(const u_char key[])
1222 /* Dynamically allocate a umac_ctx struct, initialize variables,
1223 * generate subkeys from key. Align to 16-byte boundary.
1226 struct umac_ctx *ctx, *octx;
1227 size_t bytes_to_add;
1228 aes_int_key prf_key;
1230 octx = ctx = xcalloc(1, sizeof(*ctx) + ALLOC_BOUNDARY);
1232 if (ALLOC_BOUNDARY) {
1233 bytes_to_add = ALLOC_BOUNDARY -
1234 ((ptrdiff_t)ctx & (ALLOC_BOUNDARY - 1));
1235 ctx = (struct umac_ctx *)((u_char *)ctx + bytes_to_add);
1237 ctx->free_ptr = octx;
1238 aes_key_setup(key, prf_key);
1239 pdf_init(&ctx->pdf, prf_key);
1240 uhash_init(&ctx->hash, prf_key);
1246 /* ---------------------------------------------------------------------- */
1248 int umac_final(struct umac_ctx *ctx, u_char tag[], const u_char nonce[8])
1249 /* Incorporate any pending data, pad, and generate tag */
1251 uhash_final(&ctx->hash, (u_char *)tag);
1252 pdf_gen_xor(&ctx->pdf, (const UINT8 *)nonce, (UINT8 *)tag);
1257 /* ---------------------------------------------------------------------- */
1259 int umac_update(struct umac_ctx *ctx, const u_char *input, long len)
1260 /* Given len bytes of data, we parse it into L1_KEY_LEN chunks and */
1261 /* hash each one, calling the PDF on the hashed output whenever the hash- */
1262 /* output buffer is full. */
1264 uhash_update(&ctx->hash, input, len);
1268 /* ---------------------------------------------------------------------- */
1271 int umac(struct umac_ctx *ctx, u_char *input,
1272 long len, u_char tag[],
1274 /* All-in-one version simply calls umac_update() and umac_final(). */
1276 uhash(&ctx->hash, input, len, (u_char *)tag);
1277 pdf_gen_xor(&ctx->pdf, (UINT8 *)nonce, (UINT8 *)tag);
1283 /* ---------------------------------------------------------------------- */
1284 /* ---------------------------------------------------------------------- */
1285 /* ----- End UMAC Section ----------------------------------------------- */
1286 /* ---------------------------------------------------------------------- */
1287 /* ---------------------------------------------------------------------- */