vx32

Local 9vx git repository for patches.
git clone git://r-36.net/vx32
Log | Files | Refs

whirlpool.h (3884B)


      1 
      2 #ifndef PORTABLE_C__
      3 #define PORTABLE_C__
      4 
      5 #include <limits.h>
      6 
      7 /* Definition of minimum-width integer types
      8  * 
      9  * u8   -> unsigned integer type, at least 8 bits, equivalent to unsigned char
     10  * u16  -> unsigned integer type, at least 16 bits
     11  * u32  -> unsigned integer type, at least 32 bits
     12  *
     13  * s8, s16, s32  -> signed counterparts of u8, u16, u32
     14  *
     15  * Always use macro's T8(), T16() or T32() to obtain exact-width results,
     16  * i.e., to specify the size of the result of each expression.
     17  */
     18 
     19 typedef signed char s8;
     20 typedef unsigned char u8;
     21 
     22 #if UINT_MAX >= 4294967295UL
     23 
     24 typedef signed short s16;
     25 typedef signed int s32;
     26 typedef unsigned short u16;
     27 typedef unsigned int u32;
     28 
     29 #define ONE32   0xffffffffU
     30 
     31 #else
     32 
     33 typedef signed int s16;
     34 typedef signed long s32;
     35 typedef unsigned int u16;
     36 typedef unsigned long u32;
     37 
     38 #define ONE32   0xffffffffUL
     39 
     40 #endif
     41 
     42 #define ONE8    0xffU
     43 #define ONE16   0xffffU
     44 
     45 #define T8(x)   ((x) & ONE8)
     46 #define T16(x)  ((x) & ONE16)
     47 #define T32(x)  ((x) & ONE32)
     48 
     49 #ifdef _MSC_VER
     50 typedef unsigned __int64 u64;
     51 typedef signed __int64 s64;
     52 #define LL(v)   (v##i64)
     53 #define ONE64   LL(0xffffffffffffffff)
     54 #else  /* !_MSC_VER */
     55 typedef unsigned long long u64;
     56 typedef signed long long s64;
     57 #define LL(v)   (v##ULL)
     58 #define ONE64   LL(0xffffffffffffffff)
     59 #endif /* ?_MSC_VER */
     60 #define T64(x)  ((x) & ONE64)
     61 #define ROTR64(v, n)   (((v) >> (n)) | T64((v) << (64 - (n))))
     62 /*
     63  * Note: the test is used to detect native 64-bit architectures;
     64  * if the unsigned long is strictly greater than 32-bit, it is
     65  * assumed to be at least 64-bit. This will not work correctly
     66  * on (old) 36-bit architectures (PDP-11 for instance).
     67  *
     68  * On non-64-bit architectures, "long long" is used.
     69  */
     70 
     71 /*
     72  * U8TO32_BIG(c) returns the 32-bit value stored in big-endian convention
     73  * in the unsigned char array pointed to by c.
     74  */
     75 #define U8TO32_BIG(c)  (((u32)T8(*(c)) << 24) | ((u32)T8(*((c) + 1)) << 16) | ((u32)T8(*((c) + 2)) << 8) | ((u32)T8(*((c) + 3))))
     76 
     77 /*
     78  * U8TO32_LITTLE(c) returns the 32-bit value stored in little-endian convention
     79  * in the unsigned char array pointed to by c.
     80  */
     81 #define U8TO32_LITTLE(c)  (((u32)T8(*(c))) | ((u32)T8(*((c) + 1)) << 8) | (u32)T8(*((c) + 2)) << 16) | ((u32)T8(*((c) + 3)) << 24))
     82 
     83 /*
     84  * U8TO32_BIG(c, v) stores the 32-bit-value v in big-endian convention
     85  * into the unsigned char array pointed to by c.
     86  */
     87 #define U32TO8_BIG(c, v)    do { u32 x = (v); u8 *d = (c); d[0] = T8(x >> 24); d[1] = T8(x >> 16); d[2] = T8(x >> 8); d[3] = T8(x); } while (0)
     88 
     89 /*
     90  * U8TO32_LITTLE(c, v) stores the 32-bit-value v in little-endian convention
     91  * into the unsigned char array pointed to by c.
     92  */
     93 #define U32TO8_LITTLE(c, v)    do { u32 x = (v); u8 *d = (c); d[0] = T8(x); d[1] = T8(x >> 8); d[2] = T8(x >> 16); d[3] = T8(x >> 24); } while (0)
     94 
     95 /*
     96  * ROTL32(v, n) returns the value of the 32-bit unsigned value v after
     97  * a rotation of n bits to the left. It might be replaced by the appropriate
     98  * architecture-specific macro.
     99  *
    100  * It evaluates v and n twice.
    101  *
    102  * The compiler might emit a warning if n is the constant 0. The result
    103  * is undefined if n is greater than 31.
    104  */
    105 #define ROTL32(v, n)   (T32((v) << (n)) | ((v) >> (32 - (n))))
    106 
    107 /*
    108  * Whirlpool-specific definitions.
    109  */
    110 
    111 #define DIGESTBYTES 64
    112 #define DIGESTBITS  (8*DIGESTBYTES) /* 512 */
    113 
    114 #define WBLOCKBYTES 64
    115 #define WBLOCKBITS  (8*WBLOCKBYTES) /* 512 */
    116 
    117 #define LENGTHBYTES 32
    118 #define LENGTHBITS  (8*LENGTHBYTES) /* 256 */
    119 
    120 typedef struct NESSIEstruct {
    121 	u8  bitLength[LENGTHBYTES]; /* global number of hashed bits (256-bit counter) */
    122 	u8  buffer[WBLOCKBYTES];	/* buffer of data to hash */
    123 	int bufferBits;		        /* current number of bits on the buffer */
    124 	int bufferPos;		        /* current (possibly incomplete) byte slot on the buffer */
    125 	u64 hash[DIGESTBYTES/8];    /* the hashing state */
    126 } NESSIEstruct;
    127 
    128 #endif   /* PORTABLE_C__ */
    129