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