Coverage Report

Created: 2024-06-18 06:23

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