GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/ssh/lib/umac128.c Lines: 0 265 0.0 %
Date: 2016-12-06 Branches: 0 76 0.0 %

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