Coverage Report

Created: 2026-02-26 06:46

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