1 /* $OpenBSD: umac.c,v 1.3 2008/05/12 20:52:20 pvalchev 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 #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 */
62 /* ---------------------------------------------------------------------- */
63 /* -- Global Includes --------------------------------------------------- */
64 /* ---------------------------------------------------------------------- */
67 #include <sys/types.h>
75 /* ---------------------------------------------------------------------- */
76 /* --- Primitive Data Types --- */
77 /* ---------------------------------------------------------------------- */
79 /* The following assumptions may need change on your system */
80 typedef u_int8_t UINT8
; /* 1 byte */
81 typedef u_int16_t UINT16
; /* 2 byte */
82 typedef u_int32_t UINT32
; /* 4 byte */
83 typedef u_int64_t UINT64
; /* 8 bytes */
84 typedef unsigned int UWORD
; /* Register */
86 /* ---------------------------------------------------------------------- */
87 /* --- Constants -------------------------------------------------------- */
88 /* ---------------------------------------------------------------------- */
90 #define UMAC_KEY_LEN 16 /* UMAC takes 16 bytes of external key */
92 /* Message "words" are read from memory in an endian-specific manner. */
93 /* For this implementation to behave correctly, __LITTLE_ENDIAN__ must */
94 /* be set true if the host computer is little-endian. */
96 #if BYTE_ORDER == LITTLE_ENDIAN
97 #define __LITTLE_ENDIAN__ 1
99 #define __LITTLE_ENDIAN__ 0
102 /* ---------------------------------------------------------------------- */
103 /* ---------------------------------------------------------------------- */
104 /* ----- Architecture Specific ------------------------------------------ */
105 /* ---------------------------------------------------------------------- */
106 /* ---------------------------------------------------------------------- */
109 /* ---------------------------------------------------------------------- */
110 /* ---------------------------------------------------------------------- */
111 /* ----- Primitive Routines --------------------------------------------- */
112 /* ---------------------------------------------------------------------- */
113 /* ---------------------------------------------------------------------- */
116 /* ---------------------------------------------------------------------- */
117 /* --- 32-bit by 32-bit to 64-bit Multiplication ------------------------ */
118 /* ---------------------------------------------------------------------- */
120 #define MUL64(a,b) ((UINT64)((UINT64)(UINT32)(a) * (UINT64)(UINT32)(b)))
122 /* ---------------------------------------------------------------------- */
123 /* --- Endian Conversion --- Forcing assembly on some platforms */
124 /* ---------------------------------------------------------------------- */
127 #define LOAD_UINT32_REVERSED(p) (swap32(*(UINT32 *)(p)))
128 #define STORE_UINT32_REVERSED(p,v) (*(UINT32 *)(p) = swap32(v))
129 #else /* HAVE_SWAP32 */
131 static UINT32
LOAD_UINT32_REVERSED(void *ptr
)
133 UINT32 temp
= *(UINT32
*)ptr
;
134 temp
= (temp
>> 24) | ((temp
& 0x00FF0000) >> 8 )
135 | ((temp
& 0x0000FF00) << 8 ) | (temp
<< 24);
139 # if (__LITTLE_ENDIAN__)
140 static void STORE_UINT32_REVERSED(void *ptr
, UINT32 x
)
142 UINT32 i
= (UINT32
)x
;
143 *(UINT32
*)ptr
= (i
>> 24) | ((i
& 0x00FF0000) >> 8 )
144 | ((i
& 0x0000FF00) << 8 ) | (i
<< 24);
146 # endif /* __LITTLE_ENDIAN */
147 #endif /* HAVE_SWAP32 */
149 /* The following definitions use the above reversal-primitives to do the right
150 * thing on endian specific load and stores.
153 #if (__LITTLE_ENDIAN__)
154 #define LOAD_UINT32_LITTLE(ptr) (*(UINT32 *)(ptr))
155 #define STORE_UINT32_BIG(ptr,x) STORE_UINT32_REVERSED(ptr,x)
157 #define LOAD_UINT32_LITTLE(ptr) LOAD_UINT32_REVERSED(ptr)
158 #define STORE_UINT32_BIG(ptr,x) (*(UINT32 *)(ptr) = (UINT32)(x))
161 /* ---------------------------------------------------------------------- */
162 /* ---------------------------------------------------------------------- */
163 /* ----- Begin KDF & PDF Section ---------------------------------------- */
164 /* ---------------------------------------------------------------------- */
165 /* ---------------------------------------------------------------------- */
167 /* UMAC uses AES with 16 byte block and key lengths */
168 #define AES_BLOCK_LEN 16
171 #include "openbsd-compat/openssl-compat.h"
172 #ifndef USE_BUILTIN_RIJNDAEL
173 # include <openssl/aes.h>
175 typedef AES_KEY aes_int_key
[1];
176 #define aes_encryption(in,out,int_key) \
177 AES_encrypt((u_char *)(in),(u_char *)(out),(AES_KEY *)int_key)
178 #define aes_key_setup(key,int_key) \
179 AES_set_encrypt_key((u_char *)(key),UMAC_KEY_LEN*8,int_key)
181 /* The user-supplied UMAC key is stretched using AES in a counter
182 * mode to supply all random bits needed by UMAC. The kdf function takes
183 * an AES internal key representation 'key' and writes a stream of
184 * 'nbytes' bytes to the memory pointed at by 'bufp'. Each distinct
185 * 'ndx' causes a distinct byte stream.
187 static void kdf(void *bufp
, aes_int_key key
, UINT8 ndx
, int nbytes
)
189 UINT8 in_buf
[AES_BLOCK_LEN
] = {0};
190 UINT8 out_buf
[AES_BLOCK_LEN
];
191 UINT8
*dst_buf
= (UINT8
*)bufp
;
194 /* Setup the initial value */
195 in_buf
[AES_BLOCK_LEN
-9] = ndx
;
196 in_buf
[AES_BLOCK_LEN
-1] = i
= 1;
198 while (nbytes
>= AES_BLOCK_LEN
) {
199 aes_encryption(in_buf
, out_buf
, key
);
200 memcpy(dst_buf
,out_buf
,AES_BLOCK_LEN
);
201 in_buf
[AES_BLOCK_LEN
-1] = ++i
;
202 nbytes
-= AES_BLOCK_LEN
;
203 dst_buf
+= AES_BLOCK_LEN
;
206 aes_encryption(in_buf
, out_buf
, key
);
207 memcpy(dst_buf
,out_buf
,nbytes
);
211 /* The final UHASH result is XOR'd with the output of a pseudorandom
212 * function. Here, we use AES to generate random output and
213 * xor the appropriate bytes depending on the last bits of nonce.
214 * This scheme is optimized for sequential, increasing big-endian nonces.
218 UINT8 cache
[AES_BLOCK_LEN
]; /* Previous AES output is saved */
219 UINT8 nonce
[AES_BLOCK_LEN
]; /* The AES input making above cache */
220 aes_int_key prf_key
; /* Expanded AES key for PDF */
223 static void pdf_init(pdf_ctx
*pc
, aes_int_key prf_key
)
225 UINT8 buf
[UMAC_KEY_LEN
];
227 kdf(buf
, prf_key
, 0, UMAC_KEY_LEN
);
228 aes_key_setup(buf
, pc
->prf_key
);
230 /* Initialize pdf and cache */
231 memset(pc
->nonce
, 0, sizeof(pc
->nonce
));
232 aes_encryption(pc
->nonce
, pc
->cache
, pc
->prf_key
);
235 static void pdf_gen_xor(pdf_ctx
*pc
, UINT8 nonce
[8], UINT8 buf
[8])
237 /* 'ndx' indicates that we'll be using the 0th or 1st eight bytes
238 * of the AES output. If last time around we returned the ndx-1st
239 * element, then we may have the result in the cache already.
242 #if (UMAC_OUTPUT_LEN == 4)
243 #define LOW_BIT_MASK 3
244 #elif (UMAC_OUTPUT_LEN == 8)
245 #define LOW_BIT_MASK 1
246 #elif (UMAC_OUTPUT_LEN > 8)
247 #define LOW_BIT_MASK 0
250 UINT8 tmp_nonce_lo
[4];
251 #if LOW_BIT_MASK != 0
252 int ndx
= nonce
[7] & LOW_BIT_MASK
;
254 *(UINT32
*)tmp_nonce_lo
= ((UINT32
*)nonce
)[1];
255 tmp_nonce_lo
[3] &= ~LOW_BIT_MASK
; /* zero last bit */
257 if ( (((UINT32
*)tmp_nonce_lo
)[0] != ((UINT32
*)pc
->nonce
)[1]) ||
258 (((UINT32
*)nonce
)[0] != ((UINT32
*)pc
->nonce
)[0]) )
260 ((UINT32
*)pc
->nonce
)[0] = ((UINT32
*)nonce
)[0];
261 ((UINT32
*)pc
->nonce
)[1] = ((UINT32
*)tmp_nonce_lo
)[0];
262 aes_encryption(pc
->nonce
, pc
->cache
, pc
->prf_key
);
265 #if (UMAC_OUTPUT_LEN == 4)
266 *((UINT32
*)buf
) ^= ((UINT32
*)pc
->cache
)[ndx
];
267 #elif (UMAC_OUTPUT_LEN == 8)
268 *((UINT64
*)buf
) ^= ((UINT64
*)pc
->cache
)[ndx
];
269 #elif (UMAC_OUTPUT_LEN == 12)
270 ((UINT64
*)buf
)[0] ^= ((UINT64
*)pc
->cache
)[0];
271 ((UINT32
*)buf
)[2] ^= ((UINT32
*)pc
->cache
)[2];
272 #elif (UMAC_OUTPUT_LEN == 16)
273 ((UINT64
*)buf
)[0] ^= ((UINT64
*)pc
->cache
)[0];
274 ((UINT64
*)buf
)[1] ^= ((UINT64
*)pc
->cache
)[1];
278 /* ---------------------------------------------------------------------- */
279 /* ---------------------------------------------------------------------- */
280 /* ----- Begin NH Hash Section ------------------------------------------ */
281 /* ---------------------------------------------------------------------- */
282 /* ---------------------------------------------------------------------- */
284 /* The NH-based hash functions used in UMAC are described in the UMAC paper
285 * and specification, both of which can be found at the UMAC website.
286 * The interface to this implementation has two
287 * versions, one expects the entire message being hashed to be passed
288 * in a single buffer and returns the hash result immediately. The second
289 * allows the message to be passed in a sequence of buffers. In the
290 * muliple-buffer interface, the client calls the routine nh_update() as
291 * many times as necessary. When there is no more data to be fed to the
292 * hash, the client calls nh_final() which calculates the hash output.
293 * Before beginning another hash calculation the nh_reset() routine
294 * must be called. The single-buffer routine, nh(), is equivalent to
295 * the sequence of calls nh_update() and nh_final(); however it is
296 * optimized and should be prefered whenever the multiple-buffer interface
297 * is not necessary. When using either interface, it is the client's
298 * responsability to pass no more than L1_KEY_LEN bytes per hash result.
300 * The routine nh_init() initializes the nh_ctx data structure and
301 * must be called once, before any other PDF routine.
304 /* The "nh_aux" routines do the actual NH hashing work. They
305 * expect buffers to be multiples of L1_PAD_BOUNDARY. These routines
306 * produce output for all STREAMS NH iterations in one call,
307 * allowing the parallel implementation of the streams.
310 #define STREAMS (UMAC_OUTPUT_LEN / 4) /* Number of times hash is applied */
311 #define L1_KEY_LEN 1024 /* Internal key bytes */
312 #define L1_KEY_SHIFT 16 /* Toeplitz key shift between streams */
313 #define L1_PAD_BOUNDARY 32 /* pad message to boundary multiple */
314 #define ALLOC_BOUNDARY 16 /* Keep buffers aligned to this */
315 #define HASH_BUF_BYTES 64 /* nh_aux_hb buffer multiple */
318 UINT8 nh_key
[L1_KEY_LEN
+ L1_KEY_SHIFT
* (STREAMS
- 1)]; /* NH Key */
319 UINT8 data
[HASH_BUF_BYTES
]; /* Incomming data buffer */
320 int next_data_empty
; /* Bookeeping variable for data buffer. */
321 int bytes_hashed
; /* Bytes (out of L1_KEY_LEN) incorperated. */
322 UINT64 state
[STREAMS
]; /* on-line state */
326 #if (UMAC_OUTPUT_LEN == 4)
328 static void nh_aux(void *kp
, void *dp
, void *hp
, UINT32 dlen
)
329 /* NH hashing primitive. Previous (partial) hash result is loaded and
330 * then stored via hp pointer. The length of the data pointed at by "dp",
331 * "dlen", is guaranteed to be divisible by L1_PAD_BOUNDARY (32). Key
332 * is expected to be endian compensated in memory at key setup.
337 UINT32
*k
= (UINT32
*)kp
;
338 UINT32
*d
= (UINT32
*)dp
;
339 UINT32 d0
,d1
,d2
,d3
,d4
,d5
,d6
,d7
;
340 UINT32 k0
,k1
,k2
,k3
,k4
,k5
,k6
,k7
;
344 d0
= LOAD_UINT32_LITTLE(d
+0); d1
= LOAD_UINT32_LITTLE(d
+1);
345 d2
= LOAD_UINT32_LITTLE(d
+2); d3
= LOAD_UINT32_LITTLE(d
+3);
346 d4
= LOAD_UINT32_LITTLE(d
+4); d5
= LOAD_UINT32_LITTLE(d
+5);
347 d6
= LOAD_UINT32_LITTLE(d
+6); d7
= LOAD_UINT32_LITTLE(d
+7);
348 k0
= *(k
+0); k1
= *(k
+1); k2
= *(k
+2); k3
= *(k
+3);
349 k4
= *(k
+4); k5
= *(k
+5); k6
= *(k
+6); k7
= *(k
+7);
350 h
+= MUL64((k0
+ d0
), (k4
+ d4
));
351 h
+= MUL64((k1
+ d1
), (k5
+ d5
));
352 h
+= MUL64((k2
+ d2
), (k6
+ d6
));
353 h
+= MUL64((k3
+ d3
), (k7
+ d7
));
361 #elif (UMAC_OUTPUT_LEN == 8)
363 static void nh_aux(void *kp
, void *dp
, void *hp
, UINT32 dlen
)
364 /* Same as previous nh_aux, but two streams are handled in one pass,
365 * reading and writing 16 bytes of hash-state per call.
370 UINT32
*k
= (UINT32
*)kp
;
371 UINT32
*d
= (UINT32
*)dp
;
372 UINT32 d0
,d1
,d2
,d3
,d4
,d5
,d6
,d7
;
373 UINT32 k0
,k1
,k2
,k3
,k4
,k5
,k6
,k7
,
376 h1
= *((UINT64
*)hp
);
377 h2
= *((UINT64
*)hp
+ 1);
378 k0
= *(k
+0); k1
= *(k
+1); k2
= *(k
+2); k3
= *(k
+3);
380 d0
= LOAD_UINT32_LITTLE(d
+0); d1
= LOAD_UINT32_LITTLE(d
+1);
381 d2
= LOAD_UINT32_LITTLE(d
+2); d3
= LOAD_UINT32_LITTLE(d
+3);
382 d4
= LOAD_UINT32_LITTLE(d
+4); d5
= LOAD_UINT32_LITTLE(d
+5);
383 d6
= LOAD_UINT32_LITTLE(d
+6); d7
= LOAD_UINT32_LITTLE(d
+7);
384 k4
= *(k
+4); k5
= *(k
+5); k6
= *(k
+6); k7
= *(k
+7);
385 k8
= *(k
+8); k9
= *(k
+9); k10
= *(k
+10); k11
= *(k
+11);
387 h1
+= MUL64((k0
+ d0
), (k4
+ d4
));
388 h2
+= MUL64((k4
+ d0
), (k8
+ d4
));
390 h1
+= MUL64((k1
+ d1
), (k5
+ d5
));
391 h2
+= MUL64((k5
+ d1
), (k9
+ d5
));
393 h1
+= MUL64((k2
+ d2
), (k6
+ d6
));
394 h2
+= MUL64((k6
+ d2
), (k10
+ d6
));
396 h1
+= MUL64((k3
+ d3
), (k7
+ d7
));
397 h2
+= MUL64((k7
+ d3
), (k11
+ d7
));
399 k0
= k8
; k1
= k9
; k2
= k10
; k3
= k11
;
404 ((UINT64
*)hp
)[0] = h1
;
405 ((UINT64
*)hp
)[1] = h2
;
408 #elif (UMAC_OUTPUT_LEN == 12)
410 static void nh_aux(void *kp
, void *dp
, void *hp
, UINT32 dlen
)
411 /* Same as previous nh_aux, but two streams are handled in one pass,
412 * reading and writing 24 bytes of hash-state per call.
417 UINT32
*k
= (UINT32
*)kp
;
418 UINT32
*d
= (UINT32
*)dp
;
419 UINT32 d0
,d1
,d2
,d3
,d4
,d5
,d6
,d7
;
420 UINT32 k0
,k1
,k2
,k3
,k4
,k5
,k6
,k7
,
421 k8
,k9
,k10
,k11
,k12
,k13
,k14
,k15
;
423 h1
= *((UINT64
*)hp
);
424 h2
= *((UINT64
*)hp
+ 1);
425 h3
= *((UINT64
*)hp
+ 2);
426 k0
= *(k
+0); k1
= *(k
+1); k2
= *(k
+2); k3
= *(k
+3);
427 k4
= *(k
+4); k5
= *(k
+5); k6
= *(k
+6); k7
= *(k
+7);
429 d0
= LOAD_UINT32_LITTLE(d
+0); d1
= LOAD_UINT32_LITTLE(d
+1);
430 d2
= LOAD_UINT32_LITTLE(d
+2); d3
= LOAD_UINT32_LITTLE(d
+3);
431 d4
= LOAD_UINT32_LITTLE(d
+4); d5
= LOAD_UINT32_LITTLE(d
+5);
432 d6
= LOAD_UINT32_LITTLE(d
+6); d7
= LOAD_UINT32_LITTLE(d
+7);
433 k8
= *(k
+8); k9
= *(k
+9); k10
= *(k
+10); k11
= *(k
+11);
434 k12
= *(k
+12); k13
= *(k
+13); k14
= *(k
+14); k15
= *(k
+15);
436 h1
+= MUL64((k0
+ d0
), (k4
+ d4
));
437 h2
+= MUL64((k4
+ d0
), (k8
+ d4
));
438 h3
+= MUL64((k8
+ d0
), (k12
+ d4
));
440 h1
+= MUL64((k1
+ d1
), (k5
+ d5
));
441 h2
+= MUL64((k5
+ d1
), (k9
+ d5
));
442 h3
+= MUL64((k9
+ d1
), (k13
+ d5
));
444 h1
+= MUL64((k2
+ d2
), (k6
+ d6
));
445 h2
+= MUL64((k6
+ d2
), (k10
+ d6
));
446 h3
+= MUL64((k10
+ d2
), (k14
+ d6
));
448 h1
+= MUL64((k3
+ d3
), (k7
+ d7
));
449 h2
+= MUL64((k7
+ d3
), (k11
+ d7
));
450 h3
+= MUL64((k11
+ d3
), (k15
+ d7
));
452 k0
= k8
; k1
= k9
; k2
= k10
; k3
= k11
;
453 k4
= k12
; k5
= k13
; k6
= k14
; k7
= k15
;
458 ((UINT64
*)hp
)[0] = h1
;
459 ((UINT64
*)hp
)[1] = h2
;
460 ((UINT64
*)hp
)[2] = h3
;
463 #elif (UMAC_OUTPUT_LEN == 16)
465 static void nh_aux(void *kp
, void *dp
, void *hp
, UINT32 dlen
)
466 /* Same as previous nh_aux, but two streams are handled in one pass,
467 * reading and writing 24 bytes of hash-state per call.
472 UINT32
*k
= (UINT32
*)kp
;
473 UINT32
*d
= (UINT32
*)dp
;
474 UINT32 d0
,d1
,d2
,d3
,d4
,d5
,d6
,d7
;
475 UINT32 k0
,k1
,k2
,k3
,k4
,k5
,k6
,k7
,
476 k8
,k9
,k10
,k11
,k12
,k13
,k14
,k15
,
479 h1
= *((UINT64
*)hp
);
480 h2
= *((UINT64
*)hp
+ 1);
481 h3
= *((UINT64
*)hp
+ 2);
482 h4
= *((UINT64
*)hp
+ 3);
483 k0
= *(k
+0); k1
= *(k
+1); k2
= *(k
+2); k3
= *(k
+3);
484 k4
= *(k
+4); k5
= *(k
+5); k6
= *(k
+6); k7
= *(k
+7);
486 d0
= LOAD_UINT32_LITTLE(d
+0); d1
= LOAD_UINT32_LITTLE(d
+1);
487 d2
= LOAD_UINT32_LITTLE(d
+2); d3
= LOAD_UINT32_LITTLE(d
+3);
488 d4
= LOAD_UINT32_LITTLE(d
+4); d5
= LOAD_UINT32_LITTLE(d
+5);
489 d6
= LOAD_UINT32_LITTLE(d
+6); d7
= LOAD_UINT32_LITTLE(d
+7);
490 k8
= *(k
+8); k9
= *(k
+9); k10
= *(k
+10); k11
= *(k
+11);
491 k12
= *(k
+12); k13
= *(k
+13); k14
= *(k
+14); k15
= *(k
+15);
492 k16
= *(k
+16); k17
= *(k
+17); k18
= *(k
+18); k19
= *(k
+19);
494 h1
+= MUL64((k0
+ d0
), (k4
+ d4
));
495 h2
+= MUL64((k4
+ d0
), (k8
+ d4
));
496 h3
+= MUL64((k8
+ d0
), (k12
+ d4
));
497 h4
+= MUL64((k12
+ d0
), (k16
+ d4
));
499 h1
+= MUL64((k1
+ d1
), (k5
+ d5
));
500 h2
+= MUL64((k5
+ d1
), (k9
+ d5
));
501 h3
+= MUL64((k9
+ d1
), (k13
+ d5
));
502 h4
+= MUL64((k13
+ d1
), (k17
+ d5
));
504 h1
+= MUL64((k2
+ d2
), (k6
+ d6
));
505 h2
+= MUL64((k6
+ d2
), (k10
+ d6
));
506 h3
+= MUL64((k10
+ d2
), (k14
+ d6
));
507 h4
+= MUL64((k14
+ d2
), (k18
+ d6
));
509 h1
+= MUL64((k3
+ d3
), (k7
+ d7
));
510 h2
+= MUL64((k7
+ d3
), (k11
+ d7
));
511 h3
+= MUL64((k11
+ d3
), (k15
+ d7
));
512 h4
+= MUL64((k15
+ d3
), (k19
+ d7
));
514 k0
= k8
; k1
= k9
; k2
= k10
; k3
= k11
;
515 k4
= k12
; k5
= k13
; k6
= k14
; k7
= k15
;
516 k8
= k16
; k9
= k17
; k10
= k18
; k11
= k19
;
521 ((UINT64
*)hp
)[0] = h1
;
522 ((UINT64
*)hp
)[1] = h2
;
523 ((UINT64
*)hp
)[2] = h3
;
524 ((UINT64
*)hp
)[3] = h4
;
527 /* ---------------------------------------------------------------------- */
528 #endif /* UMAC_OUTPUT_LENGTH */
529 /* ---------------------------------------------------------------------- */
532 /* ---------------------------------------------------------------------- */
534 static void nh_transform(nh_ctx
*hc
, UINT8
*buf
, UINT32 nbytes
)
535 /* This function is a wrapper for the primitive NH hash functions. It takes
536 * as argument "hc" the current hash context and a buffer which must be a
537 * multiple of L1_PAD_BOUNDARY. The key passed to nh_aux is offset
538 * appropriately according to how much message has been hashed already.
543 key
= hc
->nh_key
+ hc
->bytes_hashed
;
544 nh_aux(key
, buf
, hc
->state
, nbytes
);
547 /* ---------------------------------------------------------------------- */
549 #if (__LITTLE_ENDIAN__)
550 static void endian_convert(void *buf
, UWORD bpw
, UINT32 num_bytes
)
551 /* We endian convert the keys on little-endian computers to */
552 /* compensate for the lack of big-endian memory reads during hashing. */
554 UWORD iters
= num_bytes
/ bpw
;
556 UINT32
*p
= (UINT32
*)buf
;
558 *p
= LOAD_UINT32_REVERSED(p
);
561 } else if (bpw
== 8) {
562 UINT32
*p
= (UINT32
*)buf
;
565 t
= LOAD_UINT32_REVERSED(p
+1);
566 p
[1] = LOAD_UINT32_REVERSED(p
);
572 #define endian_convert_if_le(x,y,z) endian_convert((x),(y),(z))
574 #define endian_convert_if_le(x,y,z) do{}while(0) /* Do nothing */
577 /* ---------------------------------------------------------------------- */
579 static void nh_reset(nh_ctx
*hc
)
580 /* Reset nh_ctx to ready for hashing of new data */
582 hc
->bytes_hashed
= 0;
583 hc
->next_data_empty
= 0;
585 #if (UMAC_OUTPUT_LEN >= 8)
588 #if (UMAC_OUTPUT_LEN >= 12)
591 #if (UMAC_OUTPUT_LEN == 16)
597 /* ---------------------------------------------------------------------- */
599 static void nh_init(nh_ctx
*hc
, aes_int_key prf_key
)
600 /* Generate nh_key, endian convert and reset to be ready for hashing. */
602 kdf(hc
->nh_key
, prf_key
, 1, sizeof(hc
->nh_key
));
603 endian_convert_if_le(hc
->nh_key
, 4, sizeof(hc
->nh_key
));
607 /* ---------------------------------------------------------------------- */
609 static void nh_update(nh_ctx
*hc
, UINT8
*buf
, UINT32 nbytes
)
610 /* Incorporate nbytes of data into a nh_ctx, buffer whatever is not an */
611 /* even multiple of HASH_BUF_BYTES. */
615 j
= hc
->next_data_empty
;
616 if ((j
+ nbytes
) >= HASH_BUF_BYTES
) {
618 i
= HASH_BUF_BYTES
- j
;
619 memcpy(hc
->data
+j
, buf
, i
);
620 nh_transform(hc
,hc
->data
,HASH_BUF_BYTES
);
623 hc
->bytes_hashed
+= HASH_BUF_BYTES
;
625 if (nbytes
>= HASH_BUF_BYTES
) {
626 i
= nbytes
& ~(HASH_BUF_BYTES
- 1);
627 nh_transform(hc
, buf
, i
);
630 hc
->bytes_hashed
+= i
;
634 memcpy(hc
->data
+ j
, buf
, nbytes
);
635 hc
->next_data_empty
= j
+ nbytes
;
638 /* ---------------------------------------------------------------------- */
640 static void zero_pad(UINT8
*p
, int nbytes
)
642 /* Write "nbytes" of zeroes, beginning at "p" */
643 if (nbytes
>= (int)sizeof(UWORD
)) {
644 while ((ptrdiff_t)p
% sizeof(UWORD
)) {
649 while (nbytes
>= (int)sizeof(UWORD
)) {
651 nbytes
-= sizeof(UWORD
);
662 /* ---------------------------------------------------------------------- */
664 static void nh_final(nh_ctx
*hc
, UINT8
*result
)
665 /* After passing some number of data buffers to nh_update() for integration
666 * into an NH context, nh_final is called to produce a hash result. If any
667 * bytes are in the buffer hc->data, incorporate them into the
668 * NH context. Finally, add into the NH accumulation "state" the total number
669 * of bits hashed. The resulting numbers are written to the buffer "result".
670 * If nh_update was never called, L1_PAD_BOUNDARY zeroes are incorporated.
675 if (hc
->next_data_empty
!= 0) {
676 nh_len
= ((hc
->next_data_empty
+ (L1_PAD_BOUNDARY
- 1)) &
677 ~(L1_PAD_BOUNDARY
- 1));
678 zero_pad(hc
->data
+ hc
->next_data_empty
,
679 nh_len
- hc
->next_data_empty
);
680 nh_transform(hc
, hc
->data
, nh_len
);
681 hc
->bytes_hashed
+= hc
->next_data_empty
;
682 } else if (hc
->bytes_hashed
== 0) {
683 nh_len
= L1_PAD_BOUNDARY
;
684 zero_pad(hc
->data
, L1_PAD_BOUNDARY
);
685 nh_transform(hc
, hc
->data
, nh_len
);
688 nbits
= (hc
->bytes_hashed
<< 3);
689 ((UINT64
*)result
)[0] = ((UINT64
*)hc
->state
)[0] + nbits
;
690 #if (UMAC_OUTPUT_LEN >= 8)
691 ((UINT64
*)result
)[1] = ((UINT64
*)hc
->state
)[1] + nbits
;
693 #if (UMAC_OUTPUT_LEN >= 12)
694 ((UINT64
*)result
)[2] = ((UINT64
*)hc
->state
)[2] + nbits
;
696 #if (UMAC_OUTPUT_LEN == 16)
697 ((UINT64
*)result
)[3] = ((UINT64
*)hc
->state
)[3] + nbits
;
702 /* ---------------------------------------------------------------------- */
704 static void nh(nh_ctx
*hc
, UINT8
*buf
, UINT32 padded_len
,
705 UINT32 unpadded_len
, UINT8
*result
)
706 /* All-in-one nh_update() and nh_final() equivalent.
707 * Assumes that padded_len is divisible by L1_PAD_BOUNDARY and result is
713 /* Initialize the hash state */
714 nbits
= (unpadded_len
<< 3);
716 ((UINT64
*)result
)[0] = nbits
;
717 #if (UMAC_OUTPUT_LEN >= 8)
718 ((UINT64
*)result
)[1] = nbits
;
720 #if (UMAC_OUTPUT_LEN >= 12)
721 ((UINT64
*)result
)[2] = nbits
;
723 #if (UMAC_OUTPUT_LEN == 16)
724 ((UINT64
*)result
)[3] = nbits
;
727 nh_aux(hc
->nh_key
, buf
, result
, padded_len
);
730 /* ---------------------------------------------------------------------- */
731 /* ---------------------------------------------------------------------- */
732 /* ----- Begin UHASH Section -------------------------------------------- */
733 /* ---------------------------------------------------------------------- */
734 /* ---------------------------------------------------------------------- */
736 /* UHASH is a multi-layered algorithm. Data presented to UHASH is first
737 * hashed by NH. The NH output is then hashed by a polynomial-hash layer
738 * unless the initial data to be hashed is short. After the polynomial-
739 * layer, an inner-product hash is used to produce the final UHASH output.
741 * UHASH provides two interfaces, one all-at-once and another where data
742 * buffers are presented sequentially. In the sequential interface, the
743 * UHASH client calls the routine uhash_update() as many times as necessary.
744 * When there is no more data to be fed to UHASH, the client calls
745 * uhash_final() which
746 * calculates the UHASH output. Before beginning another UHASH calculation
747 * the uhash_reset() routine must be called. The all-at-once UHASH routine,
748 * uhash(), is equivalent to the sequence of calls uhash_update() and
749 * uhash_final(); however it is optimized and should be
750 * used whenever the sequential interface is not necessary.
752 * The routine uhash_init() initializes the uhash_ctx data structure and
753 * must be called once, before any other UHASH routine.
756 /* ---------------------------------------------------------------------- */
757 /* ----- Constants and uhash_ctx ---------------------------------------- */
758 /* ---------------------------------------------------------------------- */
760 /* ---------------------------------------------------------------------- */
761 /* ----- Poly hash and Inner-Product hash Constants --------------------- */
762 /* ---------------------------------------------------------------------- */
764 /* Primes and masks */
765 #define p36 ((UINT64)0x0000000FFFFFFFFBull) /* 2^36 - 5 */
766 #define p64 ((UINT64)0xFFFFFFFFFFFFFFC5ull) /* 2^64 - 59 */
767 #define m36 ((UINT64)0x0000000FFFFFFFFFull) /* The low 36 of 64 bits */
770 /* ---------------------------------------------------------------------- */
772 typedef struct uhash_ctx
{
773 nh_ctx hash
; /* Hash context for L1 NH hash */
774 UINT64 poly_key_8
[STREAMS
]; /* p64 poly keys */
775 UINT64 poly_accum
[STREAMS
]; /* poly hash result */
776 UINT64 ip_keys
[STREAMS
*4]; /* Inner-product keys */
777 UINT32 ip_trans
[STREAMS
]; /* Inner-product translation */
778 UINT32 msg_len
; /* Total length of data passed */
781 typedef struct uhash_ctx
*uhash_ctx_t
;
783 /* ---------------------------------------------------------------------- */
786 /* The polynomial hashes use Horner's rule to evaluate a polynomial one
787 * word at a time. As described in the specification, poly32 and poly64
788 * require keys from special domains. The following implementations exploit
789 * the special domains to avoid overflow. The results are not guaranteed to
790 * be within Z_p32 and Z_p64, but the Inner-Product hash implementation
791 * patches any errant values.
794 static UINT64
poly64(UINT64 cur
, UINT64 key
, UINT64 data
)
796 UINT32 key_hi
= (UINT32
)(key
>> 32),
797 key_lo
= (UINT32
)key
,
798 cur_hi
= (UINT32
)(cur
>> 32),
799 cur_lo
= (UINT32
)cur
,
804 X
= MUL64(key_hi
, cur_lo
) + MUL64(cur_hi
, key_lo
);
806 x_hi
= (UINT32
)(X
>> 32);
808 res
= (MUL64(key_hi
, cur_hi
) + x_hi
) * 59 + MUL64(key_lo
, cur_lo
);
810 T
= ((UINT64
)x_lo
<< 32);
823 /* Although UMAC is specified to use a ramped polynomial hash scheme, this
824 * implementation does not handle all ramp levels. Because we don't handle
825 * the ramp up to p128 modulus in this implementation, we are limited to
826 * 2^14 poly_hash() invocations per stream (for a total capacity of 2^24
827 * bytes input to UMAC per tag, ie. 16MB).
829 static void poly_hash(uhash_ctx_t hc
, UINT32 data_in
[])
832 UINT64
*data
=(UINT64
*)data_in
;
834 for (i
= 0; i
< STREAMS
; i
++) {
835 if ((UINT32
)(data
[i
] >> 32) == 0xfffffffful
) {
836 hc
->poly_accum
[i
] = poly64(hc
->poly_accum
[i
],
837 hc
->poly_key_8
[i
], p64
- 1);
838 hc
->poly_accum
[i
] = poly64(hc
->poly_accum
[i
],
839 hc
->poly_key_8
[i
], (data
[i
] - 59));
841 hc
->poly_accum
[i
] = poly64(hc
->poly_accum
[i
],
842 hc
->poly_key_8
[i
], data
[i
]);
848 /* ---------------------------------------------------------------------- */
851 /* The final step in UHASH is an inner-product hash. The poly hash
852 * produces a result not neccesarily WORD_LEN bytes long. The inner-
853 * product hash breaks the polyhash output into 16-bit chunks and
854 * multiplies each with a 36 bit key.
857 static UINT64
ip_aux(UINT64 t
, UINT64
*ipkp
, UINT64 data
)
859 t
= t
+ ipkp
[0] * (UINT64
)(UINT16
)(data
>> 48);
860 t
= t
+ ipkp
[1] * (UINT64
)(UINT16
)(data
>> 32);
861 t
= t
+ ipkp
[2] * (UINT64
)(UINT16
)(data
>> 16);
862 t
= t
+ ipkp
[3] * (UINT64
)(UINT16
)(data
);
867 static UINT32
ip_reduce_p36(UINT64 t
)
869 /* Divisionless modular reduction */
872 ret
= (t
& m36
) + 5 * (t
>> 36);
876 /* return least significant 32 bits */
877 return (UINT32
)(ret
);
881 /* If the data being hashed by UHASH is no longer than L1_KEY_LEN, then
882 * the polyhash stage is skipped and ip_short is applied directly to the
885 static void ip_short(uhash_ctx_t ahc
, UINT8
*nh_res
, u_char
*res
)
888 UINT64
*nhp
= (UINT64
*)nh_res
;
890 t
= ip_aux(0,ahc
->ip_keys
, nhp
[0]);
891 STORE_UINT32_BIG((UINT32
*)res
+0, ip_reduce_p36(t
) ^ ahc
->ip_trans
[0]);
892 #if (UMAC_OUTPUT_LEN >= 8)
893 t
= ip_aux(0,ahc
->ip_keys
+4, nhp
[1]);
894 STORE_UINT32_BIG((UINT32
*)res
+1, ip_reduce_p36(t
) ^ ahc
->ip_trans
[1]);
896 #if (UMAC_OUTPUT_LEN >= 12)
897 t
= ip_aux(0,ahc
->ip_keys
+8, nhp
[2]);
898 STORE_UINT32_BIG((UINT32
*)res
+2, ip_reduce_p36(t
) ^ ahc
->ip_trans
[2]);
900 #if (UMAC_OUTPUT_LEN == 16)
901 t
= ip_aux(0,ahc
->ip_keys
+12, nhp
[3]);
902 STORE_UINT32_BIG((UINT32
*)res
+3, ip_reduce_p36(t
) ^ ahc
->ip_trans
[3]);
906 /* If the data being hashed by UHASH is longer than L1_KEY_LEN, then
907 * the polyhash stage is not skipped and ip_long is applied to the
910 static void ip_long(uhash_ctx_t ahc
, u_char
*res
)
915 for (i
= 0; i
< STREAMS
; i
++) {
916 /* fix polyhash output not in Z_p64 */
917 if (ahc
->poly_accum
[i
] >= p64
)
918 ahc
->poly_accum
[i
] -= p64
;
919 t
= ip_aux(0,ahc
->ip_keys
+(i
*4), ahc
->poly_accum
[i
]);
920 STORE_UINT32_BIG((UINT32
*)res
+i
,
921 ip_reduce_p36(t
) ^ ahc
->ip_trans
[i
]);
926 /* ---------------------------------------------------------------------- */
928 /* ---------------------------------------------------------------------- */
930 /* Reset uhash context for next hash session */
931 static int uhash_reset(uhash_ctx_t pc
)
935 pc
->poly_accum
[0] = 1;
936 #if (UMAC_OUTPUT_LEN >= 8)
937 pc
->poly_accum
[1] = 1;
939 #if (UMAC_OUTPUT_LEN >= 12)
940 pc
->poly_accum
[2] = 1;
942 #if (UMAC_OUTPUT_LEN == 16)
943 pc
->poly_accum
[3] = 1;
948 /* ---------------------------------------------------------------------- */
950 /* Given a pointer to the internal key needed by kdf() and a uhash context,
951 * initialize the NH context and generate keys needed for poly and inner-
952 * product hashing. All keys are endian adjusted in memory so that native
953 * loads cause correct keys to be in registers during calculation.
955 static void uhash_init(uhash_ctx_t ahc
, aes_int_key prf_key
)
958 UINT8 buf
[(8*STREAMS
+4)*sizeof(UINT64
)];
960 /* Zero the entire uhash context */
961 memset(ahc
, 0, sizeof(uhash_ctx
));
963 /* Initialize the L1 hash */
964 nh_init(&ahc
->hash
, prf_key
);
966 /* Setup L2 hash variables */
967 kdf(buf
, prf_key
, 2, sizeof(buf
)); /* Fill buffer with index 1 key */
968 for (i
= 0; i
< STREAMS
; i
++) {
969 /* Fill keys from the buffer, skipping bytes in the buffer not
970 * used by this implementation. Endian reverse the keys if on a
971 * little-endian computer.
973 memcpy(ahc
->poly_key_8
+i
, buf
+24*i
, 8);
974 endian_convert_if_le(ahc
->poly_key_8
+i
, 8, 8);
975 /* Mask the 64-bit keys to their special domain */
976 ahc
->poly_key_8
[i
] &= ((UINT64
)0x01ffffffu
<< 32) + 0x01ffffffu
;
977 ahc
->poly_accum
[i
] = 1; /* Our polyhash prepends a non-zero word */
980 /* Setup L3-1 hash variables */
981 kdf(buf
, prf_key
, 3, sizeof(buf
)); /* Fill buffer with index 2 key */
982 for (i
= 0; i
< STREAMS
; i
++)
983 memcpy(ahc
->ip_keys
+4*i
, buf
+(8*i
+4)*sizeof(UINT64
),
985 endian_convert_if_le(ahc
->ip_keys
, sizeof(UINT64
),
986 sizeof(ahc
->ip_keys
));
987 for (i
= 0; i
< STREAMS
*4; i
++)
988 ahc
->ip_keys
[i
] %= p36
; /* Bring into Z_p36 */
990 /* Setup L3-2 hash variables */
991 /* Fill buffer with index 4 key */
992 kdf(ahc
->ip_trans
, prf_key
, 4, STREAMS
* sizeof(UINT32
));
993 endian_convert_if_le(ahc
->ip_trans
, sizeof(UINT32
),
994 STREAMS
* sizeof(UINT32
));
997 /* ---------------------------------------------------------------------- */
1000 static uhash_ctx_t
uhash_alloc(u_char key
[])
1002 /* Allocate memory and force to a 16-byte boundary. */
1004 u_char bytes_to_add
;
1005 aes_int_key prf_key
;
1007 ctx
= (uhash_ctx_t
)malloc(sizeof(uhash_ctx
)+ALLOC_BOUNDARY
);
1009 if (ALLOC_BOUNDARY
) {
1010 bytes_to_add
= ALLOC_BOUNDARY
-
1011 ((ptrdiff_t)ctx
& (ALLOC_BOUNDARY
-1));
1012 ctx
= (uhash_ctx_t
)((u_char
*)ctx
+ bytes_to_add
);
1013 *((u_char
*)ctx
- 1) = bytes_to_add
;
1015 aes_key_setup(key
,prf_key
);
1016 uhash_init(ctx
, prf_key
);
1022 /* ---------------------------------------------------------------------- */
1025 static int uhash_free(uhash_ctx_t ctx
)
1027 /* Free memory allocated by uhash_alloc */
1028 u_char bytes_to_sub
;
1031 if (ALLOC_BOUNDARY
) {
1032 bytes_to_sub
= *((u_char
*)ctx
- 1);
1033 ctx
= (uhash_ctx_t
)((u_char
*)ctx
- bytes_to_sub
);
1040 /* ---------------------------------------------------------------------- */
1042 static int uhash_update(uhash_ctx_t ctx
, u_char
*input
, long len
)
1043 /* Given len bytes of data, we parse it into L1_KEY_LEN chunks and
1044 * hash each one with NH, calling the polyhash on each NH output.
1047 UWORD bytes_hashed
, bytes_remaining
;
1048 UINT64 result_buf
[STREAMS
];
1049 UINT8
*nh_result
= (UINT8
*)&result_buf
;
1051 if (ctx
->msg_len
+ len
<= L1_KEY_LEN
) {
1052 nh_update(&ctx
->hash
, (UINT8
*)input
, len
);
1053 ctx
->msg_len
+= len
;
1056 bytes_hashed
= ctx
->msg_len
% L1_KEY_LEN
;
1057 if (ctx
->msg_len
== L1_KEY_LEN
)
1058 bytes_hashed
= L1_KEY_LEN
;
1060 if (bytes_hashed
+ len
>= L1_KEY_LEN
) {
1062 /* If some bytes have been passed to the hash function */
1063 /* then we want to pass at most (L1_KEY_LEN - bytes_hashed) */
1064 /* bytes to complete the current nh_block. */
1066 bytes_remaining
= (L1_KEY_LEN
- bytes_hashed
);
1067 nh_update(&ctx
->hash
, (UINT8
*)input
, bytes_remaining
);
1068 nh_final(&ctx
->hash
, nh_result
);
1069 ctx
->msg_len
+= bytes_remaining
;
1070 poly_hash(ctx
,(UINT32
*)nh_result
);
1071 len
-= bytes_remaining
;
1072 input
+= bytes_remaining
;
1075 /* Hash directly from input stream if enough bytes */
1076 while (len
>= L1_KEY_LEN
) {
1077 nh(&ctx
->hash
, (UINT8
*)input
, L1_KEY_LEN
,
1078 L1_KEY_LEN
, nh_result
);
1079 ctx
->msg_len
+= L1_KEY_LEN
;
1081 input
+= L1_KEY_LEN
;
1082 poly_hash(ctx
,(UINT32
*)nh_result
);
1086 /* pass remaining < L1_KEY_LEN bytes of input data to NH */
1088 nh_update(&ctx
->hash
, (UINT8
*)input
, len
);
1089 ctx
->msg_len
+= len
;
1096 /* ---------------------------------------------------------------------- */
1098 static int uhash_final(uhash_ctx_t ctx
, u_char
*res
)
1099 /* Incorporate any pending data, pad, and generate tag */
1101 UINT64 result_buf
[STREAMS
];
1102 UINT8
*nh_result
= (UINT8
*)&result_buf
;
1104 if (ctx
->msg_len
> L1_KEY_LEN
) {
1105 if (ctx
->msg_len
% L1_KEY_LEN
) {
1106 nh_final(&ctx
->hash
, nh_result
);
1107 poly_hash(ctx
,(UINT32
*)nh_result
);
1111 nh_final(&ctx
->hash
, nh_result
);
1112 ip_short(ctx
,nh_result
, res
);
1118 /* ---------------------------------------------------------------------- */
1121 static int uhash(uhash_ctx_t ahc
, u_char
*msg
, long len
, u_char
*res
)
1122 /* assumes that msg is in a writable buffer of length divisible by */
1123 /* L1_PAD_BOUNDARY. Bytes beyond msg[len] may be zeroed. */
1125 UINT8 nh_result
[STREAMS
*sizeof(UINT64
)];
1127 int extra_zeroes_needed
;
1129 /* If the message to be hashed is no longer than L1_HASH_LEN, we skip
1132 if (len
<= L1_KEY_LEN
) {
1133 if (len
== 0) /* If zero length messages will not */
1134 nh_len
= L1_PAD_BOUNDARY
; /* be seen, comment out this case */
1136 nh_len
= ((len
+ (L1_PAD_BOUNDARY
- 1)) & ~(L1_PAD_BOUNDARY
- 1));
1137 extra_zeroes_needed
= nh_len
- len
;
1138 zero_pad((UINT8
*)msg
+ len
, extra_zeroes_needed
);
1139 nh(&ahc
->hash
, (UINT8
*)msg
, nh_len
, len
, nh_result
);
1140 ip_short(ahc
,nh_result
, res
);
1142 /* Otherwise, we hash each L1_KEY_LEN chunk with NH, passing the NH
1143 * output to poly_hash().
1146 nh(&ahc
->hash
, (UINT8
*)msg
, L1_KEY_LEN
, L1_KEY_LEN
, nh_result
);
1147 poly_hash(ahc
,(UINT32
*)nh_result
);
1150 } while (len
>= L1_KEY_LEN
);
1152 nh_len
= ((len
+ (L1_PAD_BOUNDARY
- 1)) & ~(L1_PAD_BOUNDARY
- 1));
1153 extra_zeroes_needed
= nh_len
- len
;
1154 zero_pad((UINT8
*)msg
+ len
, extra_zeroes_needed
);
1155 nh(&ahc
->hash
, (UINT8
*)msg
, nh_len
, len
, nh_result
);
1156 poly_hash(ahc
,(UINT32
*)nh_result
);
1167 /* ---------------------------------------------------------------------- */
1168 /* ---------------------------------------------------------------------- */
1169 /* ----- Begin UMAC Section --------------------------------------------- */
1170 /* ---------------------------------------------------------------------- */
1171 /* ---------------------------------------------------------------------- */
1173 /* The UMAC interface has two interfaces, an all-at-once interface where
1174 * the entire message to be authenticated is passed to UMAC in one buffer,
1175 * and a sequential interface where the message is presented a little at a
1176 * time. The all-at-once is more optimaized than the sequential version and
1177 * should be preferred when the sequential interface is not required.
1180 uhash_ctx hash
; /* Hash function for message compression */
1181 pdf_ctx pdf
; /* PDF for hashed output */
1182 void *free_ptr
; /* Address to free this struct via */
1185 /* ---------------------------------------------------------------------- */
1188 int umac_reset(struct umac_ctx
*ctx
)
1189 /* Reset the hash function to begin a new authentication. */
1191 uhash_reset(&ctx
->hash
);
1196 /* ---------------------------------------------------------------------- */
1198 int umac_delete(struct umac_ctx
*ctx
)
1199 /* Deallocate the ctx structure */
1203 ctx
= (struct umac_ctx
*)ctx
->free_ptr
;
1209 /* ---------------------------------------------------------------------- */
1211 struct umac_ctx
*umac_new(u_char key
[])
1212 /* Dynamically allocate a umac_ctx struct, initialize variables,
1213 * generate subkeys from key. Align to 16-byte boundary.
1216 struct umac_ctx
*ctx
, *octx
;
1217 size_t bytes_to_add
;
1218 aes_int_key prf_key
;
1220 octx
= ctx
= xmalloc(sizeof(*ctx
) + ALLOC_BOUNDARY
);
1222 if (ALLOC_BOUNDARY
) {
1223 bytes_to_add
= ALLOC_BOUNDARY
-
1224 ((ptrdiff_t)ctx
& (ALLOC_BOUNDARY
- 1));
1225 ctx
= (struct umac_ctx
*)((u_char
*)ctx
+ bytes_to_add
);
1227 ctx
->free_ptr
= octx
;
1228 aes_key_setup(key
,prf_key
);
1229 pdf_init(&ctx
->pdf
, prf_key
);
1230 uhash_init(&ctx
->hash
, prf_key
);
1236 /* ---------------------------------------------------------------------- */
1238 int umac_final(struct umac_ctx
*ctx
, u_char tag
[], u_char nonce
[8])
1239 /* Incorporate any pending data, pad, and generate tag */
1241 uhash_final(&ctx
->hash
, (u_char
*)tag
);
1242 pdf_gen_xor(&ctx
->pdf
, (UINT8
*)nonce
, (UINT8
*)tag
);
1247 /* ---------------------------------------------------------------------- */
1249 int umac_update(struct umac_ctx
*ctx
, u_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. */
1254 uhash_update(&ctx
->hash
, input
, len
);
1258 /* ---------------------------------------------------------------------- */
1261 int umac(struct umac_ctx
*ctx
, u_char
*input
,
1262 long len
, u_char tag
[],
1264 /* All-in-one version simply calls umac_update() and umac_final(). */
1266 uhash(&ctx
->hash
, input
, len
, (u_char
*)tag
);
1267 pdf_gen_xor(&ctx
->pdf
, (UINT8
*)nonce
, (UINT8
*)tag
);
1273 /* ---------------------------------------------------------------------- */
1274 /* ---------------------------------------------------------------------- */
1275 /* ----- End UMAC Section ----------------------------------------------- */
1276 /* ---------------------------------------------------------------------- */
1277 /* ---------------------------------------------------------------------- */