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