/* "UH Universal Hashing Library" Copyright ((c)) 2002, Rice University All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of Rice University (RICE) nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. This software is provided by RICE and the contributors on an "as is" basis, without any representations or warranties of any kind, express or implied including, but not limited to, representations or warranties of non-infringement, merchantability or fitness for a particular purpose. In no event shall RICE or contributors be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage. */ /* Example code for universal-hashing short inputs, up to 16 bytes in length via inlined C&W functions. This example generates 32 bits of output. */ #ifndef UH_CW12_H #define UH_CW12_H #include "uh_internals.h" #include "uh_aes.h" /* 64 bit internal state Process 16 bits at a time Max length 16 bytes, hard coded in _alloc (but changable) 32 bits output. Not much faster then the generalized functions. */ typedef struct uh_cw12_ctx *uh_cw12_ctx_t; uh_cw12_ctx_t uh_cw12_alloc(const unsigned char key[UH_AES_KEY_LEN]); void uh_cw12_delete(uh_cw12_ctx_t ctx); void uh_cw12_getkey(uh_cw12_ctx_t ctx, char *key); struct uh_cw12_ctx { char key[UH_AES_KEY_LEN]; UINT32 *rndvector; }; /* Prime number for modulo operations */ static UINT32 p32 = ((UINT32)0xFFFFFFFBu); #define HIGH(u) ((UINT64)(((u)>>16) & 0xFFFF)) #define LOW(u) ((UINT64)((u) & 0xFFFF)) /* BUGGO: TODO: FIXME: What is C's evaluation order, can I do ++ twice in an expression safetly? */ #define LONG(accum,ctx,offset,u) accum+= HIGH(u)*(ctx)->rndvector[offset++]; accum+=LOW(u)*(ctx)->rndvector[offset++] #define SHORT(accum,ctx,offset,s) accum += (((UINT64)(s))*(ctx)->rndvector[offset++]) static inline unsigned int uh_cw12_ex1(const uh_cw12_ctx_t ctx, UINT32 ip) { int offset=0; int accum=0; LONG(accum,ctx,offset,ip); return accum%p32; } static inline unsigned int uh_cw12_ex2(const uh_cw12_ctx_t ctx, UINT32 ip, UINT16 port) { int offset=0; int accum=0; LONG(accum,ctx,offset,ip); SHORT(accum,ctx,offset,port); return accum%p32; } static inline unsigned int uh_cw12_ex3(const uh_cw12_ctx_t ctx, UINT32 srcip, UINT32 dstip, UINT16 srcport, UINT16 dstport) { int offset=0; int accum=0; LONG(accum,ctx,offset,srcip); SHORT(accum,ctx,offset,srcport); LONG(accum,ctx,offset,dstip); SHORT(accum,ctx,offset,dstport); return accum%p32; } #undef HIGH #undef LOW #undef SHORT #undef LONG #endif