Coverage Report

Created: 2026-07-12 07:11

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