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