Coverage Report

Created: 2024-07-27 06:19

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