GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/ssh/lib/../umac.c Lines: 0 249 0.0 %
Date: 2017-11-13 Branches: 0 74 0.0 %

Line Branch Exec Source
1
/* $OpenBSD: umac.c,v 1.13 2017/10/27 01:01:17 djm 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     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 <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
    explicit_bzero(in_buf, sizeof(in_buf));
201
    explicit_bzero(out_buf, sizeof(out_buf));
202
}
203
204
/* The final UHASH result is XOR'd with the output of a pseudorandom
205
 * function. Here, we use AES to generate random output and
206
 * xor the appropriate bytes depending on the last bits of nonce.
207
 * This scheme is optimized for sequential, increasing big-endian nonces.
208
 */
209
210
typedef struct {
211
    UINT8 cache[AES_BLOCK_LEN];  /* Previous AES output is saved      */
212
    UINT8 nonce[AES_BLOCK_LEN];  /* The AES input making above cache  */
213
    aes_int_key prf_key;         /* Expanded AES key for PDF          */
214
} pdf_ctx;
215
216
static void pdf_init(pdf_ctx *pc, aes_int_key prf_key)
217
{
218
    UINT8 buf[UMAC_KEY_LEN];
219
220
    kdf(buf, prf_key, 0, UMAC_KEY_LEN);
221
    aes_key_setup(buf, pc->prf_key);
222
223
    /* Initialize pdf and cache */
224
    memset(pc->nonce, 0, sizeof(pc->nonce));
225
    aes_encryption(pc->nonce, pc->cache, pc->prf_key);
226
    explicit_bzero(buf, sizeof(buf));
227
}
228
229
static void pdf_gen_xor(pdf_ctx *pc, const UINT8 nonce[8], UINT8 buf[8])
230
{
231
    /* 'ndx' indicates that we'll be using the 0th or 1st eight bytes
232
     * of the AES output. If last time around we returned the ndx-1st
233
     * element, then we may have the result in the cache already.
234
     */
235
236
#if (UMAC_OUTPUT_LEN == 4)
237
#define LOW_BIT_MASK 3
238
#elif (UMAC_OUTPUT_LEN == 8)
239
#define LOW_BIT_MASK 1
240
#elif (UMAC_OUTPUT_LEN > 8)
241
#define LOW_BIT_MASK 0
242
#endif
243
    union {
244
        UINT8 tmp_nonce_lo[4];
245
        UINT32 align;
246
    } t;
247
#if LOW_BIT_MASK != 0
248
    int ndx = nonce[7] & LOW_BIT_MASK;
249
#endif
250
    *(UINT32 *)t.tmp_nonce_lo = ((const UINT32 *)nonce)[1];
251
    t.tmp_nonce_lo[3] &= ~LOW_BIT_MASK; /* zero last bit */
252
253
    if ( (((UINT32 *)t.tmp_nonce_lo)[0] != ((UINT32 *)pc->nonce)[1]) ||
254
         (((const UINT32 *)nonce)[0] != ((UINT32 *)pc->nonce)[0]) )
255
    {
256
        ((UINT32 *)pc->nonce)[0] = ((const UINT32 *)nonce)[0];
257
        ((UINT32 *)pc->nonce)[1] = ((UINT32 *)t.tmp_nonce_lo)[0];
258
        aes_encryption(pc->nonce, pc->cache, pc->prf_key);
259
    }
260
261
#if (UMAC_OUTPUT_LEN == 4)
262
    *((UINT32 *)buf) ^= ((UINT32 *)pc->cache)[ndx];
263
#elif (UMAC_OUTPUT_LEN == 8)
264
    *((UINT64 *)buf) ^= ((UINT64 *)pc->cache)[ndx];
265
#elif (UMAC_OUTPUT_LEN == 12)
266
    ((UINT64 *)buf)[0] ^= ((UINT64 *)pc->cache)[0];
267
    ((UINT32 *)buf)[2] ^= ((UINT32 *)pc->cache)[2];
268
#elif (UMAC_OUTPUT_LEN == 16)
269
    ((UINT64 *)buf)[0] ^= ((UINT64 *)pc->cache)[0];
270
    ((UINT64 *)buf)[1] ^= ((UINT64 *)pc->cache)[1];
271
#endif
272
}
273
274
/* ---------------------------------------------------------------------- */
275
/* ---------------------------------------------------------------------- */
276
/* ----- Begin NH Hash Section ------------------------------------------ */
277
/* ---------------------------------------------------------------------- */
278
/* ---------------------------------------------------------------------- */
279
280
/* The NH-based hash functions used in UMAC are described in the UMAC paper
281
 * and specification, both of which can be found at the UMAC website.
282
 * The interface to this implementation has two
283
 * versions, one expects the entire message being hashed to be passed
284
 * in a single buffer and returns the hash result immediately. The second
285
 * allows the message to be passed in a sequence of buffers. In the
286
 * muliple-buffer interface, the client calls the routine nh_update() as
287
 * many times as necessary. When there is no more data to be fed to the
288
 * hash, the client calls nh_final() which calculates the hash output.
289
 * Before beginning another hash calculation the nh_reset() routine
290
 * must be called. The single-buffer routine, nh(), is equivalent to
291
 * the sequence of calls nh_update() and nh_final(); however it is
292
 * optimized and should be prefered whenever the multiple-buffer interface
293
 * is not necessary. When using either interface, it is the client's
294
 * responsability to pass no more than L1_KEY_LEN bytes per hash result.
295
 *
296
 * The routine nh_init() initializes the nh_ctx data structure and
297
 * must be called once, before any other PDF routine.
298
 */
299
300
 /* The "nh_aux" routines do the actual NH hashing work. They
301
  * expect buffers to be multiples of L1_PAD_BOUNDARY. These routines
302
  * produce output for all STREAMS NH iterations in one call,
303
  * allowing the parallel implementation of the streams.
304
  */
305
306
#define STREAMS (UMAC_OUTPUT_LEN / 4) /* Number of times hash is applied  */
307
#define L1_KEY_LEN         1024     /* Internal key bytes                 */
308
#define L1_KEY_SHIFT         16     /* Toeplitz key shift between streams */
309
#define L1_PAD_BOUNDARY      32     /* pad message to boundary multiple   */
310
#define ALLOC_BOUNDARY       16     /* Keep buffers aligned to this       */
311
#define HASH_BUF_BYTES       64     /* nh_aux_hb buffer multiple          */
312
313
typedef struct {
314
    UINT8  nh_key [L1_KEY_LEN + L1_KEY_SHIFT * (STREAMS - 1)]; /* NH Key */
315
    UINT8  data   [HASH_BUF_BYTES];    /* Incoming data buffer           */
316
    int next_data_empty;    /* Bookeeping variable for data buffer.       */
317
    int bytes_hashed;        /* Bytes (out of L1_KEY_LEN) incorperated.   */
318
    UINT64 state[STREAMS];               /* on-line state     */
319
} nh_ctx;
320
321
322
#if (UMAC_OUTPUT_LEN == 4)
323
324
static void nh_aux(void *kp, const void *dp, void *hp, UINT32 dlen)
325
/* NH hashing primitive. Previous (partial) hash result is loaded and
326
* then stored via hp pointer. The length of the data pointed at by "dp",
327
* "dlen", is guaranteed to be divisible by L1_PAD_BOUNDARY (32).  Key
328
* is expected to be endian compensated in memory at key setup.
329
*/
330
{
331
    UINT64 h;
332
    UWORD c = dlen / 32;
333
    UINT32 *k = (UINT32 *)kp;
334
    const UINT32 *d = (const UINT32 *)dp;
335
    UINT32 d0,d1,d2,d3,d4,d5,d6,d7;
336
    UINT32 k0,k1,k2,k3,k4,k5,k6,k7;
337
338
    h = *((UINT64 *)hp);
339
    do {
340
        d0 = LOAD_UINT32_LITTLE(d+0); d1 = LOAD_UINT32_LITTLE(d+1);
341
        d2 = LOAD_UINT32_LITTLE(d+2); d3 = LOAD_UINT32_LITTLE(d+3);
342
        d4 = LOAD_UINT32_LITTLE(d+4); d5 = LOAD_UINT32_LITTLE(d+5);
343
        d6 = LOAD_UINT32_LITTLE(d+6); d7 = LOAD_UINT32_LITTLE(d+7);
344
        k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3);
345
        k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7);
346
        h += MUL64((k0 + d0), (k4 + d4));
347
        h += MUL64((k1 + d1), (k5 + d5));
348
        h += MUL64((k2 + d2), (k6 + d6));
349
        h += MUL64((k3 + d3), (k7 + d7));
350
351
        d += 8;
352
        k += 8;
353
    } while (--c);
354
  *((UINT64 *)hp) = h;
355
}
356
357
#elif (UMAC_OUTPUT_LEN == 8)
358
359
static void nh_aux(void *kp, const void *dp, void *hp, UINT32 dlen)
360
/* Same as previous nh_aux, but two streams are handled in one pass,
361
 * reading and writing 16 bytes of hash-state per call.
362
 */
363
{
364
  UINT64 h1,h2;
365
  UWORD c = dlen / 32;
366
  UINT32 *k = (UINT32 *)kp;
367
  const UINT32 *d = (const UINT32 *)dp;
368
  UINT32 d0,d1,d2,d3,d4,d5,d6,d7;
369
  UINT32 k0,k1,k2,k3,k4,k5,k6,k7,
370
        k8,k9,k10,k11;
371
372
  h1 = *((UINT64 *)hp);
373
  h2 = *((UINT64 *)hp + 1);
374
  k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3);
375
  do {
376
    d0 = LOAD_UINT32_LITTLE(d+0); d1 = LOAD_UINT32_LITTLE(d+1);
377
    d2 = LOAD_UINT32_LITTLE(d+2); d3 = LOAD_UINT32_LITTLE(d+3);
378
    d4 = LOAD_UINT32_LITTLE(d+4); d5 = LOAD_UINT32_LITTLE(d+5);
379
    d6 = LOAD_UINT32_LITTLE(d+6); d7 = LOAD_UINT32_LITTLE(d+7);
380
    k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7);
381
    k8 = *(k+8); k9 = *(k+9); k10 = *(k+10); k11 = *(k+11);
382
383
    h1 += MUL64((k0 + d0), (k4 + d4));
384
    h2 += MUL64((k4 + d0), (k8 + d4));
385
386
    h1 += MUL64((k1 + d1), (k5 + d5));
387
    h2 += MUL64((k5 + d1), (k9 + d5));
388
389
    h1 += MUL64((k2 + d2), (k6 + d6));
390
    h2 += MUL64((k6 + d2), (k10 + d6));
391
392
    h1 += MUL64((k3 + d3), (k7 + d7));
393
    h2 += MUL64((k7 + d3), (k11 + d7));
394
395
    k0 = k8; k1 = k9; k2 = k10; k3 = k11;
396
397
    d += 8;
398
    k += 8;
399
  } while (--c);
400
  ((UINT64 *)hp)[0] = h1;
401
  ((UINT64 *)hp)[1] = h2;
402
}
403
404
#elif (UMAC_OUTPUT_LEN == 12)
405
406
static void nh_aux(void *kp, const void *dp, void *hp, UINT32 dlen)
407
/* Same as previous nh_aux, but two streams are handled in one pass,
408
 * reading and writing 24 bytes of hash-state per call.
409
*/
410
{
411
    UINT64 h1,h2,h3;
412
    UWORD c = dlen / 32;
413
    UINT32 *k = (UINT32 *)kp;
414
    const UINT32 *d = (const UINT32 *)dp;
415
    UINT32 d0,d1,d2,d3,d4,d5,d6,d7;
416
    UINT32 k0,k1,k2,k3,k4,k5,k6,k7,
417
        k8,k9,k10,k11,k12,k13,k14,k15;
418
419
    h1 = *((UINT64 *)hp);
420
    h2 = *((UINT64 *)hp + 1);
421
    h3 = *((UINT64 *)hp + 2);
422
    k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3);
423
    k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7);
424
    do {
425
        d0 = LOAD_UINT32_LITTLE(d+0); d1 = LOAD_UINT32_LITTLE(d+1);
426
        d2 = LOAD_UINT32_LITTLE(d+2); d3 = LOAD_UINT32_LITTLE(d+3);
427
        d4 = LOAD_UINT32_LITTLE(d+4); d5 = LOAD_UINT32_LITTLE(d+5);
428
        d6 = LOAD_UINT32_LITTLE(d+6); d7 = LOAD_UINT32_LITTLE(d+7);
429
        k8 = *(k+8); k9 = *(k+9); k10 = *(k+10); k11 = *(k+11);
430
        k12 = *(k+12); k13 = *(k+13); k14 = *(k+14); k15 = *(k+15);
431
432
        h1 += MUL64((k0 + d0), (k4 + d4));
433
        h2 += MUL64((k4 + d0), (k8 + d4));
434
        h3 += MUL64((k8 + d0), (k12 + d4));
435
436
        h1 += MUL64((k1 + d1), (k5 + d5));
437
        h2 += MUL64((k5 + d1), (k9 + d5));
438
        h3 += MUL64((k9 + d1), (k13 + d5));
439
440
        h1 += MUL64((k2 + d2), (k6 + d6));
441
        h2 += MUL64((k6 + d2), (k10 + d6));
442
        h3 += MUL64((k10 + d2), (k14 + d6));
443
444
        h1 += MUL64((k3 + d3), (k7 + d7));
445
        h2 += MUL64((k7 + d3), (k11 + d7));
446
        h3 += MUL64((k11 + d3), (k15 + d7));
447
448
        k0 = k8; k1 = k9; k2 = k10; k3 = k11;
449
        k4 = k12; k5 = k13; k6 = k14; k7 = k15;
450
451
        d += 8;
452
        k += 8;
453
    } while (--c);
454
    ((UINT64 *)hp)[0] = h1;
455
    ((UINT64 *)hp)[1] = h2;
456
    ((UINT64 *)hp)[2] = h3;
457
}
458
459
#elif (UMAC_OUTPUT_LEN == 16)
460
461
static void nh_aux(void *kp, const void *dp, void *hp, UINT32 dlen)
462
/* Same as previous nh_aux, but two streams are handled in one pass,
463
 * reading and writing 24 bytes of hash-state per call.
464
*/
465
{
466
    UINT64 h1,h2,h3,h4;
467
    UWORD c = dlen / 32;
468
    UINT32 *k = (UINT32 *)kp;
469
    const UINT32 *d = (const UINT32 *)dp;
470
    UINT32 d0,d1,d2,d3,d4,d5,d6,d7;
471
    UINT32 k0,k1,k2,k3,k4,k5,k6,k7,
472
        k8,k9,k10,k11,k12,k13,k14,k15,
473
        k16,k17,k18,k19;
474
475
    h1 = *((UINT64 *)hp);
476
    h2 = *((UINT64 *)hp + 1);
477
    h3 = *((UINT64 *)hp + 2);
478
    h4 = *((UINT64 *)hp + 3);
479
    k0 = *(k+0); k1 = *(k+1); k2 = *(k+2); k3 = *(k+3);
480
    k4 = *(k+4); k5 = *(k+5); k6 = *(k+6); k7 = *(k+7);
481
    do {
482
        d0 = LOAD_UINT32_LITTLE(d+0); d1 = LOAD_UINT32_LITTLE(d+1);
483
        d2 = LOAD_UINT32_LITTLE(d+2); d3 = LOAD_UINT32_LITTLE(d+3);
484
        d4 = LOAD_UINT32_LITTLE(d+4); d5 = LOAD_UINT32_LITTLE(d+5);
485
        d6 = LOAD_UINT32_LITTLE(d+6); d7 = LOAD_UINT32_LITTLE(d+7);
486
        k8 = *(k+8); k9 = *(k+9); k10 = *(k+10); k11 = *(k+11);
487
        k12 = *(k+12); k13 = *(k+13); k14 = *(k+14); k15 = *(k+15);
488
        k16 = *(k+16); k17 = *(k+17); k18 = *(k+18); k19 = *(k+19);
489
490
        h1 += MUL64((k0 + d0), (k4 + d4));
491
        h2 += MUL64((k4 + d0), (k8 + d4));
492
        h3 += MUL64((k8 + d0), (k12 + d4));
493
        h4 += MUL64((k12 + d0), (k16 + d4));
494
495
        h1 += MUL64((k1 + d1), (k5 + d5));
496
        h2 += MUL64((k5 + d1), (k9 + d5));
497
        h3 += MUL64((k9 + d1), (k13 + d5));
498
        h4 += MUL64((k13 + d1), (k17 + d5));
499
500
        h1 += MUL64((k2 + d2), (k6 + d6));
501
        h2 += MUL64((k6 + d2), (k10 + d6));
502
        h3 += MUL64((k10 + d2), (k14 + d6));
503
        h4 += MUL64((k14 + d2), (k18 + d6));
504
505
        h1 += MUL64((k3 + d3), (k7 + d7));
506
        h2 += MUL64((k7 + d3), (k11 + d7));
507
        h3 += MUL64((k11 + d3), (k15 + d7));
508
        h4 += MUL64((k15 + d3), (k19 + d7));
509
510
        k0 = k8; k1 = k9; k2 = k10; k3 = k11;
511
        k4 = k12; k5 = k13; k6 = k14; k7 = k15;
512
        k8 = k16; k9 = k17; k10 = k18; k11 = k19;
513
514
        d += 8;
515
        k += 8;
516
    } while (--c);
517
    ((UINT64 *)hp)[0] = h1;
518
    ((UINT64 *)hp)[1] = h2;
519
    ((UINT64 *)hp)[2] = h3;
520
    ((UINT64 *)hp)[3] = h4;
521
}
522
523
/* ---------------------------------------------------------------------- */
524
#endif  /* UMAC_OUTPUT_LENGTH */
525
/* ---------------------------------------------------------------------- */
526
527
528
/* ---------------------------------------------------------------------- */
529
530
static void nh_transform(nh_ctx *hc, const UINT8 *buf, UINT32 nbytes)
531
/* This function is a wrapper for the primitive NH hash functions. It takes
532
 * as argument "hc" the current hash context and a buffer which must be a
533
 * multiple of L1_PAD_BOUNDARY. The key passed to nh_aux is offset
534
 * appropriately according to how much message has been hashed already.
535
 */
536
{
537
    UINT8 *key;
538
539
    key = hc->nh_key + hc->bytes_hashed;
540
    nh_aux(key, buf, hc->state, nbytes);
541
}
542
543
/* ---------------------------------------------------------------------- */
544
545
#if (__LITTLE_ENDIAN__)
546
static void endian_convert(void *buf, UWORD bpw, UINT32 num_bytes)
547
/* We endian convert the keys on little-endian computers to               */
548
/* compensate for the lack of big-endian memory reads during hashing.     */
549
{
550
    UWORD iters = num_bytes / bpw;
551
    if (bpw == 4) {
552
        UINT32 *p = (UINT32 *)buf;
553
        do {
554
            *p = LOAD_UINT32_REVERSED(p);
555
            p++;
556
        } while (--iters);
557
    } else if (bpw == 8) {
558
        UINT32 *p = (UINT32 *)buf;
559
        UINT32 t;
560
        do {
561
            t = LOAD_UINT32_REVERSED(p+1);
562
            p[1] = LOAD_UINT32_REVERSED(p);
563
            p[0] = t;
564
            p += 2;
565
        } while (--iters);
566
    }
567
}
568
#define endian_convert_if_le(x,y,z) endian_convert((x),(y),(z))
569
#else
570
#define endian_convert_if_le(x,y,z) do{}while(0)  /* Do nothing */
571
#endif
572
573
/* ---------------------------------------------------------------------- */
574
575
static void nh_reset(nh_ctx *hc)
576
/* Reset nh_ctx to ready for hashing of new data */
577
{
578
    hc->bytes_hashed = 0;
579
    hc->next_data_empty = 0;
580
    hc->state[0] = 0;
581
#if (UMAC_OUTPUT_LEN >= 8)
582
    hc->state[1] = 0;
583
#endif
584
#if (UMAC_OUTPUT_LEN >= 12)
585
    hc->state[2] = 0;
586
#endif
587
#if (UMAC_OUTPUT_LEN == 16)
588
    hc->state[3] = 0;
589
#endif
590
591
}
592
593
/* ---------------------------------------------------------------------- */
594
595
static void nh_init(nh_ctx *hc, aes_int_key prf_key)
596
/* Generate nh_key, endian convert and reset to be ready for hashing.   */
597
{
598
    kdf(hc->nh_key, prf_key, 1, sizeof(hc->nh_key));
599
    endian_convert_if_le(hc->nh_key, 4, sizeof(hc->nh_key));
600
    nh_reset(hc);
601
}
602
603
/* ---------------------------------------------------------------------- */
604
605
static void nh_update(nh_ctx *hc, const UINT8 *buf, UINT32 nbytes)
606
/* Incorporate nbytes of data into a nh_ctx, buffer whatever is not an    */
607
/* even multiple of HASH_BUF_BYTES.                                       */
608
{
609
    UINT32 i,j;
610
611
    j = hc->next_data_empty;
612
    if ((j + nbytes) >= HASH_BUF_BYTES) {
613
        if (j) {
614
            i = HASH_BUF_BYTES - j;
615
            memcpy(hc->data+j, buf, i);
616
            nh_transform(hc,hc->data,HASH_BUF_BYTES);
617
            nbytes -= i;
618
            buf += i;
619
            hc->bytes_hashed += HASH_BUF_BYTES;
620
        }
621
        if (nbytes >= HASH_BUF_BYTES) {
622
            i = nbytes & ~(HASH_BUF_BYTES - 1);
623
            nh_transform(hc, buf, i);
624
            nbytes -= i;
625
            buf += i;
626
            hc->bytes_hashed += i;
627
        }
628
        j = 0;
629
    }
630
    memcpy(hc->data + j, buf, nbytes);
631
    hc->next_data_empty = j + nbytes;
632
}
633
634
/* ---------------------------------------------------------------------- */
635
636
static void zero_pad(UINT8 *p, int nbytes)
637
{
638
/* Write "nbytes" of zeroes, beginning at "p" */
639
    if (nbytes >= (int)sizeof(UWORD)) {
640
        while ((ptrdiff_t)p % sizeof(UWORD)) {
641
            *p = 0;
642
            nbytes--;
643
            p++;
644
        }
645
        while (nbytes >= (int)sizeof(UWORD)) {
646
            *(UWORD *)p = 0;
647
            nbytes -= sizeof(UWORD);
648
            p += sizeof(UWORD);
649
        }
650
    }
651
    while (nbytes) {
652
        *p = 0;
653
        nbytes--;
654
        p++;
655
    }
656
}
657
658
/* ---------------------------------------------------------------------- */
659
660
static void nh_final(nh_ctx *hc, UINT8 *result)
661
/* After passing some number of data buffers to nh_update() for integration
662
 * into an NH context, nh_final is called to produce a hash result. If any
663
 * bytes are in the buffer hc->data, incorporate them into the
664
 * NH context. Finally, add into the NH accumulation "state" the total number
665
 * of bits hashed. The resulting numbers are written to the buffer "result".
666
 * If nh_update was never called, L1_PAD_BOUNDARY zeroes are incorporated.
667
 */
668
{
669
    int nh_len, nbits;
670
671
    if (hc->next_data_empty != 0) {
672
        nh_len = ((hc->next_data_empty + (L1_PAD_BOUNDARY - 1)) &
673
                                                ~(L1_PAD_BOUNDARY - 1));
674
        zero_pad(hc->data + hc->next_data_empty,
675
                                          nh_len - hc->next_data_empty);
676
        nh_transform(hc, hc->data, nh_len);
677
        hc->bytes_hashed += hc->next_data_empty;
678
    } else if (hc->bytes_hashed == 0) {
679
    	nh_len = L1_PAD_BOUNDARY;
680
        zero_pad(hc->data, L1_PAD_BOUNDARY);
681
        nh_transform(hc, hc->data, nh_len);
682
    }
683
684
    nbits = (hc->bytes_hashed << 3);
685
    ((UINT64 *)result)[0] = ((UINT64 *)hc->state)[0] + nbits;
686
#if (UMAC_OUTPUT_LEN >= 8)
687
    ((UINT64 *)result)[1] = ((UINT64 *)hc->state)[1] + nbits;
688
#endif
689
#if (UMAC_OUTPUT_LEN >= 12)
690
    ((UINT64 *)result)[2] = ((UINT64 *)hc->state)[2] + nbits;
691
#endif
692
#if (UMAC_OUTPUT_LEN == 16)
693
    ((UINT64 *)result)[3] = ((UINT64 *)hc->state)[3] + nbits;
694
#endif
695
    nh_reset(hc);
696
}
697
698
/* ---------------------------------------------------------------------- */
699
700
static void nh(nh_ctx *hc, const UINT8 *buf, UINT32 padded_len,
701
               UINT32 unpadded_len, UINT8 *result)
702
/* All-in-one nh_update() and nh_final() equivalent.
703
 * Assumes that padded_len is divisible by L1_PAD_BOUNDARY and result is
704
 * well aligned
705
 */
706
{
707
    UINT32 nbits;
708
709
    /* Initialize the hash state */
710
    nbits = (unpadded_len << 3);
711
712
    ((UINT64 *)result)[0] = nbits;
713
#if (UMAC_OUTPUT_LEN >= 8)
714
    ((UINT64 *)result)[1] = nbits;
715
#endif
716
#if (UMAC_OUTPUT_LEN >= 12)
717
    ((UINT64 *)result)[2] = nbits;
718
#endif
719
#if (UMAC_OUTPUT_LEN == 16)
720
    ((UINT64 *)result)[3] = nbits;
721
#endif
722
723
    nh_aux(hc->nh_key, buf, result, padded_len);
724
}
725
726
/* ---------------------------------------------------------------------- */
727
/* ---------------------------------------------------------------------- */
728
/* ----- Begin UHASH Section -------------------------------------------- */
729
/* ---------------------------------------------------------------------- */
730
/* ---------------------------------------------------------------------- */
731
732
/* UHASH is a multi-layered algorithm. Data presented to UHASH is first
733
 * hashed by NH. The NH output is then hashed by a polynomial-hash layer
734
 * unless the initial data to be hashed is short. After the polynomial-
735
 * layer, an inner-product hash is used to produce the final UHASH output.
736
 *
737
 * UHASH provides two interfaces, one all-at-once and another where data
738
 * buffers are presented sequentially. In the sequential interface, the
739
 * UHASH client calls the routine uhash_update() as many times as necessary.
740
 * When there is no more data to be fed to UHASH, the client calls
741
 * uhash_final() which
742
 * calculates the UHASH output. Before beginning another UHASH calculation
743
 * the uhash_reset() routine must be called. The all-at-once UHASH routine,
744
 * uhash(), is equivalent to the sequence of calls uhash_update() and
745
 * uhash_final(); however it is optimized and should be
746
 * used whenever the sequential interface is not necessary.
747
 *
748
 * The routine uhash_init() initializes the uhash_ctx data structure and
749
 * must be called once, before any other UHASH routine.
750
 */
751
752
/* ---------------------------------------------------------------------- */
753
/* ----- Constants and uhash_ctx ---------------------------------------- */
754
/* ---------------------------------------------------------------------- */
755
756
/* ---------------------------------------------------------------------- */
757
/* ----- Poly hash and Inner-Product hash Constants --------------------- */
758
/* ---------------------------------------------------------------------- */
759
760
/* Primes and masks */
761
#define p36    ((UINT64)0x0000000FFFFFFFFBull)              /* 2^36 -  5 */
762
#define p64    ((UINT64)0xFFFFFFFFFFFFFFC5ull)              /* 2^64 - 59 */
763
#define m36    ((UINT64)0x0000000FFFFFFFFFull)  /* The low 36 of 64 bits */
764
765
766
/* ---------------------------------------------------------------------- */
767
768
typedef struct uhash_ctx {
769
    nh_ctx hash;                          /* Hash context for L1 NH hash  */
770
    UINT64 poly_key_8[STREAMS];           /* p64 poly keys                */
771
    UINT64 poly_accum[STREAMS];           /* poly hash result             */
772
    UINT64 ip_keys[STREAMS*4];            /* Inner-product keys           */
773
    UINT32 ip_trans[STREAMS];             /* Inner-product translation    */
774
    UINT32 msg_len;                       /* Total length of data passed  */
775
                                          /* to uhash */
776
} uhash_ctx;
777
typedef struct uhash_ctx *uhash_ctx_t;
778
779
/* ---------------------------------------------------------------------- */
780
781
782
/* The polynomial hashes use Horner's rule to evaluate a polynomial one
783
 * word at a time. As described in the specification, poly32 and poly64
784
 * require keys from special domains. The following implementations exploit
785
 * the special domains to avoid overflow. The results are not guaranteed to
786
 * be within Z_p32 and Z_p64, but the Inner-Product hash implementation
787
 * patches any errant values.
788
 */
789
790
static UINT64 poly64(UINT64 cur, UINT64 key, UINT64 data)
791
{
792
    UINT32 key_hi = (UINT32)(key >> 32),
793
           key_lo = (UINT32)key,
794
           cur_hi = (UINT32)(cur >> 32),
795
           cur_lo = (UINT32)cur,
796
           x_lo,
797
           x_hi;
798
    UINT64 X,T,res;
799
800
    X =  MUL64(key_hi, cur_lo) + MUL64(cur_hi, key_lo);
801
    x_lo = (UINT32)X;
802
    x_hi = (UINT32)(X >> 32);
803
804
    res = (MUL64(key_hi, cur_hi) + x_hi) * 59 + MUL64(key_lo, cur_lo);
805
806
    T = ((UINT64)x_lo << 32);
807
    res += T;
808
    if (res < T)
809
        res += 59;
810
811
    res += data;
812
    if (res < data)
813
        res += 59;
814
815
    return res;
816
}
817
818
819
/* Although UMAC is specified to use a ramped polynomial hash scheme, this
820
 * implementation does not handle all ramp levels. Because we don't handle
821
 * the ramp up to p128 modulus in this implementation, we are limited to
822
 * 2^14 poly_hash() invocations per stream (for a total capacity of 2^24
823
 * bytes input to UMAC per tag, ie. 16MB).
824
 */
825
static void poly_hash(uhash_ctx_t hc, UINT32 data_in[])
826
{
827
    int i;
828
    UINT64 *data=(UINT64*)data_in;
829
830
    for (i = 0; i < STREAMS; i++) {
831
        if ((UINT32)(data[i] >> 32) == 0xfffffffful) {
832
            hc->poly_accum[i] = poly64(hc->poly_accum[i],
833
                                       hc->poly_key_8[i], p64 - 1);
834
            hc->poly_accum[i] = poly64(hc->poly_accum[i],
835
                                       hc->poly_key_8[i], (data[i] - 59));
836
        } else {
837
            hc->poly_accum[i] = poly64(hc->poly_accum[i],
838
                                       hc->poly_key_8[i], data[i]);
839
        }
840
    }
841
}
842
843
844
/* ---------------------------------------------------------------------- */
845
846
847
/* The final step in UHASH is an inner-product hash. The poly hash
848
 * produces a result not neccesarily WORD_LEN bytes long. The inner-
849
 * product hash breaks the polyhash output into 16-bit chunks and
850
 * multiplies each with a 36 bit key.
851
 */
852
853
static UINT64 ip_aux(UINT64 t, UINT64 *ipkp, UINT64 data)
854
{
855
    t = t + ipkp[0] * (UINT64)(UINT16)(data >> 48);
856
    t = t + ipkp[1] * (UINT64)(UINT16)(data >> 32);
857
    t = t + ipkp[2] * (UINT64)(UINT16)(data >> 16);
858
    t = t + ipkp[3] * (UINT64)(UINT16)(data);
859
860
    return t;
861
}
862
863
static UINT32 ip_reduce_p36(UINT64 t)
864
{
865
/* Divisionless modular reduction */
866
    UINT64 ret;
867
868
    ret = (t & m36) + 5 * (t >> 36);
869
    if (ret >= p36)
870
        ret -= p36;
871
872
    /* return least significant 32 bits */
873
    return (UINT32)(ret);
874
}
875
876
877
/* If the data being hashed by UHASH is no longer than L1_KEY_LEN, then
878
 * the polyhash stage is skipped and ip_short is applied directly to the
879
 * NH output.
880
 */
881
static void ip_short(uhash_ctx_t ahc, UINT8 *nh_res, u_char *res)
882
{
883
    UINT64 t;
884
    UINT64 *nhp = (UINT64 *)nh_res;
885
886
    t  = ip_aux(0,ahc->ip_keys, nhp[0]);
887
    STORE_UINT32_BIG((UINT32 *)res+0, ip_reduce_p36(t) ^ ahc->ip_trans[0]);
888
#if (UMAC_OUTPUT_LEN >= 8)
889
    t  = ip_aux(0,ahc->ip_keys+4, nhp[1]);
890
    STORE_UINT32_BIG((UINT32 *)res+1, ip_reduce_p36(t) ^ ahc->ip_trans[1]);
891
#endif
892
#if (UMAC_OUTPUT_LEN >= 12)
893
    t  = ip_aux(0,ahc->ip_keys+8, nhp[2]);
894
    STORE_UINT32_BIG((UINT32 *)res+2, ip_reduce_p36(t) ^ ahc->ip_trans[2]);
895
#endif
896
#if (UMAC_OUTPUT_LEN == 16)
897
    t  = ip_aux(0,ahc->ip_keys+12, nhp[3]);
898
    STORE_UINT32_BIG((UINT32 *)res+3, ip_reduce_p36(t) ^ ahc->ip_trans[3]);
899
#endif
900
}
901
902
/* If the data being hashed by UHASH is longer than L1_KEY_LEN, then
903
 * the polyhash stage is not skipped and ip_long is applied to the
904
 * polyhash output.
905
 */
906
static void ip_long(uhash_ctx_t ahc, u_char *res)
907
{
908
    int i;
909
    UINT64 t;
910
911
    for (i = 0; i < STREAMS; i++) {
912
        /* fix polyhash output not in Z_p64 */
913
        if (ahc->poly_accum[i] >= p64)
914
            ahc->poly_accum[i] -= p64;
915
        t  = ip_aux(0,ahc->ip_keys+(i*4), ahc->poly_accum[i]);
916
        STORE_UINT32_BIG((UINT32 *)res+i,
917
                         ip_reduce_p36(t) ^ ahc->ip_trans[i]);
918
    }
919
}
920
921
922
/* ---------------------------------------------------------------------- */
923
924
/* ---------------------------------------------------------------------- */
925
926
/* Reset uhash context for next hash session */
927
static int uhash_reset(uhash_ctx_t pc)
928
{
929
    nh_reset(&pc->hash);
930
    pc->msg_len = 0;
931
    pc->poly_accum[0] = 1;
932
#if (UMAC_OUTPUT_LEN >= 8)
933
    pc->poly_accum[1] = 1;
934
#endif
935
#if (UMAC_OUTPUT_LEN >= 12)
936
    pc->poly_accum[2] = 1;
937
#endif
938
#if (UMAC_OUTPUT_LEN == 16)
939
    pc->poly_accum[3] = 1;
940
#endif
941
    return 1;
942
}
943
944
/* ---------------------------------------------------------------------- */
945
946
/* Given a pointer to the internal key needed by kdf() and a uhash context,
947
 * initialize the NH context and generate keys needed for poly and inner-
948
 * product hashing. All keys are endian adjusted in memory so that native
949
 * loads cause correct keys to be in registers during calculation.
950
 */
951
static void uhash_init(uhash_ctx_t ahc, aes_int_key prf_key)
952
{
953
    int i;
954
    UINT8 buf[(8*STREAMS+4)*sizeof(UINT64)];
955
956
    /* Zero the entire uhash context */
957
    memset(ahc, 0, sizeof(uhash_ctx));
958
959
    /* Initialize the L1 hash */
960
    nh_init(&ahc->hash, prf_key);
961
962
    /* Setup L2 hash variables */
963
    kdf(buf, prf_key, 2, sizeof(buf));    /* Fill buffer with index 1 key */
964
    for (i = 0; i < STREAMS; i++) {
965
        /* Fill keys from the buffer, skipping bytes in the buffer not
966
         * used by this implementation. Endian reverse the keys if on a
967
         * little-endian computer.
968
         */
969
        memcpy(ahc->poly_key_8+i, buf+24*i, 8);
970
        endian_convert_if_le(ahc->poly_key_8+i, 8, 8);
971
        /* Mask the 64-bit keys to their special domain */
972
        ahc->poly_key_8[i] &= ((UINT64)0x01ffffffu << 32) + 0x01ffffffu;
973
        ahc->poly_accum[i] = 1;  /* Our polyhash prepends a non-zero word */
974
    }
975
976
    /* Setup L3-1 hash variables */
977
    kdf(buf, prf_key, 3, sizeof(buf)); /* Fill buffer with index 2 key */
978
    for (i = 0; i < STREAMS; i++)
979
          memcpy(ahc->ip_keys+4*i, buf+(8*i+4)*sizeof(UINT64),
980
                                                 4*sizeof(UINT64));
981
    endian_convert_if_le(ahc->ip_keys, sizeof(UINT64),
982
                                                  sizeof(ahc->ip_keys));
983
    for (i = 0; i < STREAMS*4; i++)
984
        ahc->ip_keys[i] %= p36;  /* Bring into Z_p36 */
985
986
    /* Setup L3-2 hash variables    */
987
    /* Fill buffer with index 4 key */
988
    kdf(ahc->ip_trans, prf_key, 4, STREAMS * sizeof(UINT32));
989
    endian_convert_if_le(ahc->ip_trans, sizeof(UINT32),
990
                         STREAMS * sizeof(UINT32));
991
    explicit_bzero(buf, sizeof(buf));
992
}
993
994
/* ---------------------------------------------------------------------- */
995
996
#if 0
997
static uhash_ctx_t uhash_alloc(u_char key[])
998
{
999
/* Allocate memory and force to a 16-byte boundary. */
1000
    uhash_ctx_t ctx;
1001
    u_char bytes_to_add;
1002
    aes_int_key prf_key;
1003
1004
    ctx = (uhash_ctx_t)malloc(sizeof(uhash_ctx)+ALLOC_BOUNDARY);
1005
    if (ctx) {
1006
        if (ALLOC_BOUNDARY) {
1007
            bytes_to_add = ALLOC_BOUNDARY -
1008
                              ((ptrdiff_t)ctx & (ALLOC_BOUNDARY -1));
1009
            ctx = (uhash_ctx_t)((u_char *)ctx + bytes_to_add);
1010
            *((u_char *)ctx - 1) = bytes_to_add;
1011
        }
1012
        aes_key_setup(key,prf_key);
1013
        uhash_init(ctx, prf_key);
1014
    }
1015
    return (ctx);
1016
}
1017
#endif
1018
1019
/* ---------------------------------------------------------------------- */
1020
1021
#if 0
1022
static int uhash_free(uhash_ctx_t ctx)
1023
{
1024
/* Free memory allocated by uhash_alloc */
1025
    u_char bytes_to_sub;
1026
1027
    if (ctx) {
1028
        if (ALLOC_BOUNDARY) {
1029
            bytes_to_sub = *((u_char *)ctx - 1);
1030
            ctx = (uhash_ctx_t)((u_char *)ctx - bytes_to_sub);
1031
        }
1032
        free(ctx);
1033
    }
1034
    return (1);
1035
}
1036
#endif
1037
/* ---------------------------------------------------------------------- */
1038
1039
static int uhash_update(uhash_ctx_t ctx, const u_char *input, long len)
1040
/* Given len bytes of data, we parse it into L1_KEY_LEN chunks and
1041
 * hash each one with NH, calling the polyhash on each NH output.
1042
 */
1043
{
1044
    UWORD bytes_hashed, bytes_remaining;
1045
    UINT64 result_buf[STREAMS];
1046
    UINT8 *nh_result = (UINT8 *)&result_buf;
1047
1048
    if (ctx->msg_len + len <= L1_KEY_LEN) {
1049
        nh_update(&ctx->hash, (const 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, (const 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, (const 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, (const UINT8 *)input, len);
1086
             ctx->msg_len += len;
1087
         }
1088
     }
1089
1090
    return (1);
1091
}
1092
1093
/* ---------------------------------------------------------------------- */
1094
1095
static int uhash_final(uhash_ctx_t ctx, u_char *res)
1096
/* Incorporate any pending data, pad, and generate tag */
1097
{
1098
    UINT64 result_buf[STREAMS];
1099
    UINT8 *nh_result = (UINT8 *)&result_buf;
1100
1101
    if (ctx->msg_len > L1_KEY_LEN) {
1102
        if (ctx->msg_len % L1_KEY_LEN) {
1103
            nh_final(&ctx->hash, nh_result);
1104
            poly_hash(ctx,(UINT32 *)nh_result);
1105
        }
1106
        ip_long(ctx, res);
1107
    } else {
1108
        nh_final(&ctx->hash, nh_result);
1109
        ip_short(ctx,nh_result, res);
1110
    }
1111
    uhash_reset(ctx);
1112
    return (1);
1113
}
1114
1115
/* ---------------------------------------------------------------------- */
1116
1117
#if 0
1118
static int uhash(uhash_ctx_t ahc, u_char *msg, long len, u_char *res)
1119
/* assumes that msg is in a writable buffer of length divisible by */
1120
/* L1_PAD_BOUNDARY. Bytes beyond msg[len] may be zeroed.           */
1121
{
1122
    UINT8 nh_result[STREAMS*sizeof(UINT64)];
1123
    UINT32 nh_len;
1124
    int extra_zeroes_needed;
1125
1126
    /* If the message to be hashed is no longer than L1_HASH_LEN, we skip
1127
     * the polyhash.
1128
     */
1129
    if (len <= L1_KEY_LEN) {
1130
    	if (len == 0)                  /* If zero length messages will not */
1131
    		nh_len = L1_PAD_BOUNDARY;  /* be seen, comment out this case   */
1132
    	else
1133
        	nh_len = ((len + (L1_PAD_BOUNDARY - 1)) & ~(L1_PAD_BOUNDARY - 1));
1134
        extra_zeroes_needed = nh_len - len;
1135
        zero_pad((UINT8 *)msg + len, extra_zeroes_needed);
1136
        nh(&ahc->hash, (UINT8 *)msg, nh_len, len, nh_result);
1137
        ip_short(ahc,nh_result, res);
1138
    } else {
1139
        /* Otherwise, we hash each L1_KEY_LEN chunk with NH, passing the NH
1140
         * output to poly_hash().
1141
         */
1142
        do {
1143
            nh(&ahc->hash, (UINT8 *)msg, L1_KEY_LEN, L1_KEY_LEN, nh_result);
1144
            poly_hash(ahc,(UINT32 *)nh_result);
1145
            len -= L1_KEY_LEN;
1146
            msg += L1_KEY_LEN;
1147
        } while (len >= L1_KEY_LEN);
1148
        if (len) {
1149
            nh_len = ((len + (L1_PAD_BOUNDARY - 1)) & ~(L1_PAD_BOUNDARY - 1));
1150
            extra_zeroes_needed = nh_len - len;
1151
            zero_pad((UINT8 *)msg + len, extra_zeroes_needed);
1152
            nh(&ahc->hash, (UINT8 *)msg, nh_len, len, nh_result);
1153
            poly_hash(ahc,(UINT32 *)nh_result);
1154
        }
1155
1156
        ip_long(ahc, res);
1157
    }
1158
1159
    uhash_reset(ahc);
1160
    return 1;
1161
}
1162
#endif
1163
1164
/* ---------------------------------------------------------------------- */
1165
/* ---------------------------------------------------------------------- */
1166
/* ----- Begin UMAC Section --------------------------------------------- */
1167
/* ---------------------------------------------------------------------- */
1168
/* ---------------------------------------------------------------------- */
1169
1170
/* The UMAC interface has two interfaces, an all-at-once interface where
1171
 * the entire message to be authenticated is passed to UMAC in one buffer,
1172
 * and a sequential interface where the message is presented a little at a
1173
 * time. The all-at-once is more optimaized than the sequential version and
1174
 * should be preferred when the sequential interface is not required.
1175
 */
1176
struct umac_ctx {
1177
    uhash_ctx hash;          /* Hash function for message compression    */
1178
    pdf_ctx pdf;             /* PDF for hashed output                    */
1179
    void *free_ptr;          /* Address to free this struct via          */
1180
} umac_ctx;
1181
1182
/* ---------------------------------------------------------------------- */
1183
1184
#if 0
1185
int umac_reset(struct umac_ctx *ctx)
1186
/* Reset the hash function to begin a new authentication.        */
1187
{
1188
    uhash_reset(&ctx->hash);
1189
    return (1);
1190
}
1191
#endif
1192
1193
/* ---------------------------------------------------------------------- */
1194
1195
int umac_delete(struct umac_ctx *ctx)
1196
/* Deallocate the ctx structure */
1197
{
1198
    if (ctx) {
1199
        if (ALLOC_BOUNDARY)
1200
            ctx = (struct umac_ctx *)ctx->free_ptr;
1201
        explicit_bzero(ctx, sizeof(*ctx) + ALLOC_BOUNDARY);
1202
        free(ctx);
1203
    }
1204
    return (1);
1205
}
1206
1207
/* ---------------------------------------------------------------------- */
1208
1209
struct umac_ctx *umac_new(const u_char key[])
1210
/* Dynamically allocate a umac_ctx struct, initialize variables,
1211
 * generate subkeys from key. Align to 16-byte boundary.
1212
 */
1213
{
1214
    struct umac_ctx *ctx, *octx;
1215
    size_t bytes_to_add;
1216
    aes_int_key prf_key;
1217
1218
    octx = ctx = xcalloc(1, sizeof(*ctx) + ALLOC_BOUNDARY);
1219
    if (ctx) {
1220
        if (ALLOC_BOUNDARY) {
1221
            bytes_to_add = ALLOC_BOUNDARY -
1222
                              ((ptrdiff_t)ctx & (ALLOC_BOUNDARY - 1));
1223
            ctx = (struct umac_ctx *)((u_char *)ctx + bytes_to_add);
1224
        }
1225
        ctx->free_ptr = octx;
1226
        aes_key_setup(key, prf_key);
1227
        pdf_init(&ctx->pdf, prf_key);
1228
        uhash_init(&ctx->hash, prf_key);
1229
        explicit_bzero(prf_key, sizeof(prf_key));
1230
    }
1231
1232
    return (ctx);
1233
}
1234
1235
/* ---------------------------------------------------------------------- */
1236
1237
int umac_final(struct umac_ctx *ctx, u_char tag[], const u_char nonce[8])
1238
/* Incorporate any pending data, pad, and generate tag */
1239
{
1240
    uhash_final(&ctx->hash, (u_char *)tag);
1241
    pdf_gen_xor(&ctx->pdf, (const UINT8 *)nonce, (UINT8 *)tag);
1242
1243
    return (1);
1244
}
1245
1246
/* ---------------------------------------------------------------------- */
1247
1248
int umac_update(struct umac_ctx *ctx, const u_char *input, long len)
1249
/* Given len bytes of data, we parse it into L1_KEY_LEN chunks and   */
1250
/* hash each one, calling the PDF on the hashed output whenever the hash- */
1251
/* output buffer is full.                                                 */
1252
{
1253
    uhash_update(&ctx->hash, input, len);
1254
    return (1);
1255
}
1256
1257
/* ---------------------------------------------------------------------- */
1258
1259
#if 0
1260
int umac(struct umac_ctx *ctx, u_char *input,
1261
         long len, u_char tag[],
1262
         u_char nonce[8])
1263
/* All-in-one version simply calls umac_update() and umac_final().        */
1264
{
1265
    uhash(&ctx->hash, input, len, (u_char *)tag);
1266
    pdf_gen_xor(&ctx->pdf, (UINT8 *)nonce, (UINT8 *)tag);
1267
1268
    return (1);
1269
}
1270
#endif
1271
1272
/* ---------------------------------------------------------------------- */
1273
/* ---------------------------------------------------------------------- */
1274
/* ----- End UMAC Section ----------------------------------------------- */
1275
/* ---------------------------------------------------------------------- */
1276
/* ---------------------------------------------------------------------- */