fork/slippc
git clone https://git.julia.blue/fork/slippc
85fcfbd
0.8.1
1/* 2* The code is placed under public domain by Kazuho Oku <kazuhooku@gmail.com>. 3* 4* The MD5 implementation is based on a public domain implementation written by 5* Solar Designer <solar@openwall.com> in 2001, which is used by Dovecot. 6* 7* The SHA1 implementation is based on a public domain implementation written 8* by Wei Dai and other contributors for libcrypt, used also in liboauth. 9* 10* The SHA224/SHA256 implementation is based on a public domain implementation 11* by Sam Hocevar <sam@hocevar.net> for LibTomCrypt. 12*/ 13#ifndef _picohash_h_ 14#define _picohash_h_ 15 16#include <assert.h> 17#include <inttypes.h> 18#include <string.h> 19 20#ifdef __BIG_ENDIAN__ 21#define _PICOHASH_BIG_ENDIAN 22#elif defined__LITTLE_ENDIAN__ 23/* override */ 24#elif defined__BYTE_ORDER 25#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 26#define _PICOHASH_BIG_ENDIAN 27#endif 28#else // ! defined __LITTLE_ENDIAN__ 29#include <endian.h> // machine/endian.h 30#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ 31#define _PICOHASH_BIG_ENDIAN 32#endif 33#endif 34 35#define PICOHASH_MD5_BLOCK_LENGTH 64 36#define PICOHASH_MD5_DIGEST_LENGTH 16 37 38typedef struct { 39uint_fast32_t lo ,hi ; 40uint_fast32_t a ,b ,c ,d ; 41unsigned char buffer [64 ]; 42uint_fast32_t block [PICOHASH_MD5_DIGEST_LENGTH ]; 43}_picohash_md5_ctx_t ; 44 45static void _picohash_md5_init (_picohash_md5_ctx_t * ctx ); 46static void _picohash_md5_update (_picohash_md5_ctx_t * ctx ,const void * data ,size_t size ); 47static void _picohash_md5_final (_picohash_md5_ctx_t * ctx ,void * digest ); 48 49#define PICOHASH_SHA1_BLOCK_LENGTH 64 50#define PICOHASH_SHA1_DIGEST_LENGTH 20 51 52typedef struct { 53uint32_t buffer [PICOHASH_SHA1_BLOCK_LENGTH /4 ]; 54uint32_t state [PICOHASH_SHA1_DIGEST_LENGTH /4 ]; 55uint64_t byteCount ; 56uint8_t bufferOffset ; 57}_picohash_sha1_ctx_t ; 58 59static void _picohash_sha1_init (_picohash_sha1_ctx_t * ctx ); 60static void _picohash_sha1_update (_picohash_sha1_ctx_t * ctx ,const void * input ,size_t len ); 61static void _picohash_sha1_final (_picohash_sha1_ctx_t * ctx ,void * digest ); 62 63#define PICOHASH_SHA256_BLOCK_LENGTH 64 64#define PICOHASH_SHA256_DIGEST_LENGTH 32 65#define PICOHASH_SHA224_BLOCK_LENGTH PICOHASH_SHA256_BLOCK_LENGTH 66#define PICOHASH_SHA224_DIGEST_LENGTH 28 67 68typedef struct { 69uint64_t length ; 70uint32_t state [PICOHASH_SHA256_DIGEST_LENGTH /4 ]; 71uint32_t curlen ; 72unsigned char buf [PICOHASH_SHA256_BLOCK_LENGTH ]; 73}_picohash_sha256_ctx_t ; 74 75static void _picohash_sha256_init (_picohash_sha256_ctx_t * ctx ); 76static void _picohash_sha256_update (_picohash_sha256_ctx_t * ctx ,const void * data ,size_t len ); 77static void _picohash_sha256_final (_picohash_sha256_ctx_t * ctx ,void * digest ); 78static void _picohash_sha224_init (_picohash_sha256_ctx_t * ctx ); 79static void _picohash_sha224_final (_picohash_sha256_ctx_t * ctx ,void * digest ); 80 81#define PICOHASH_MAX_BLOCK_LENGTH 64 82#define PICOHASH_MAX_DIGEST_LENGTH 32 83 84typedef struct { 85union { 86_picohash_md5_ctx_t _md5 ; 87_picohash_sha1_ctx_t _sha1 ; 88_picohash_sha256_ctx_t _sha256 ; 89 }; 90size_t block_length ; 91size_t digest_length ; 92void (* _reset )(void * ctx ); 93void (* _update )(void * ctx ,const void * input ,size_t len ); 94void (* _final )(void * ctx ,void * digest ); 95struct { 96unsigned char key [PICOHASH_MAX_BLOCK_LENGTH ]; 97void (* hash_reset )(void * ctx ); 98void (* hash_final )(void * ctx ,void * digest ); 99 }_hmac ; 100}picohash_ctx_t ; 101 102static void picohash_init_md5 (picohash_ctx_t * ctx ); 103static void picohash_init_sha1 (picohash_ctx_t * ctx ); 104static void picohash_init_sha224 (picohash_ctx_t * ctx ); 105static void picohash_init_sha256 (picohash_ctx_t * ctx ); 106static void picohash_update (picohash_ctx_t * ctx ,const void * input ,size_t len ); 107static void picohash_final (picohash_ctx_t * ctx ,void * digest ); 108static void picohash_reset (picohash_ctx_t * ctx ); 109 110static void picohash_init_hmac (picohash_ctx_t * ctx ,void (* initf )(picohash_ctx_t * ),const void * key ,size_t key_len ); 111 112/* following are private definitions */ 113 114/* 115* The basic MD5 functions. 116* 117* F is optimized compared to its RFC 1321 definition just like in Colin 118* Plumb's implementation. 119*/ 120#define _PICOHASH_MD5_F (x ,y ,z ) ((z) ^ ((x) & ((y) ^ (z)))) 121#define _PICOHASH_MD5_G (x ,y ,z ) ((y) ^ ((z) & ((x) ^ (y)))) 122#define _PICOHASH_MD5_H (x ,y ,z ) ((x) ^ (y) ^ (z)) 123#define _PICOHASH_MD5_I (x ,y ,z ) ((y) ^ ((x) | ~(z))) 124 125/* 126* The MD5 transformation for all four rounds. 127*/ 128#define _PICOHASH_MD5_STEP (f ,a ,b ,c ,d ,x ,t ,s ) \ 129 (a) += f((b), (c), (d)) + (x) + (t); \ 130 (a) = (((a) << (s)) | (((a)&0xffffffff) >> (32 - (s)))); \ 131 (a) += (b); 132 133/* 134* SET reads 4 input bytes in little-endian byte order and stores them 135* in a properly aligned word in host byte order. 136* 137* The check for little-endian architectures which tolerate unaligned 138* memory accesses is just an optimization. Nothing will break if it 139* doesn't work. 140*/ 141#if defined(__i386__ )|| defined(__x86_64__ )|| defined(__vax__ ) 142#define _PICOHASH_MD5_SET (n ) (*(const uint32_t *)&ptr[(n)*4]) 143#define _PICOHASH_MD5_GET (n ) _PICOHASH_MD5_SET(n) 144#else 145#define _PICOHASH_MD5_SET (n ) \ 146 (ctx->block[(n)] = (uint_fast32_t)ptr[(n)*4] | ((uint_fast32_t)ptr[(n)*4 + 1] << 8) | ((uint_fast32_t)ptr[(n)*4 + 2] << 16) | \ 147 ((uint_fast32_t)ptr[(n)*4 + 3] << 24)) 148#define _PICOHASH_MD5_GET (n ) (ctx->block[(n)]) 149#endif 150 151/* 152* This processes one or more 64-byte data blocks, but does NOT update 153* the bit counters. There're no alignment requirements. 154*/ 155static const void * _picohash_md5_body (_picohash_md5_ctx_t * ctx ,const void * data ,size_t size ) 156{ 157const unsigned char * ptr ; 158uint_fast32_t a ,b ,c ,d ; 159uint_fast32_t saved_a ,saved_b ,saved_c ,saved_d ; 160 161ptr = (const unsigned char * )data ; 162 163a = ctx -> a ; 164b = ctx -> b ; 165c = ctx -> c ; 166d = ctx -> d ; 167 168do { 169saved_a = a ; 170saved_b = b ; 171saved_c = c ; 172saved_d = d ; 173 174/* Round 1 */ 175_PICOHASH_MD5_STEP (_PICOHASH_MD5_F ,a ,b ,c ,d ,_PICOHASH_MD5_SET (0 ),0xd76aa478 ,7 ) 176_PICOHASH_MD5_STEP (_PICOHASH_MD5_F ,d ,a ,b ,c ,_PICOHASH_MD5_SET (1 ),0xe8c7b756 ,12 ) 177_PICOHASH_MD5_STEP (_PICOHASH_MD5_F ,c ,d ,a ,b ,_PICOHASH_MD5_SET (2 ),0x242070db ,17 ) 178_PICOHASH_MD5_STEP (_PICOHASH_MD5_F ,b ,c ,d ,a ,_PICOHASH_MD5_SET (3 ),0xc1bdceee ,22 ) 179_PICOHASH_MD5_STEP (_PICOHASH_MD5_F ,a ,b ,c ,d ,_PICOHASH_MD5_SET (4 ),0xf57c0faf ,7 ) 180_PICOHASH_MD5_STEP (_PICOHASH_MD5_F ,d ,a ,b ,c ,_PICOHASH_MD5_SET (5 ),0x4787c62a ,12 ) 181_PICOHASH_MD5_STEP (_PICOHASH_MD5_F ,c ,d ,a ,b ,_PICOHASH_MD5_SET (6 ),0xa8304613 ,17 ) 182_PICOHASH_MD5_STEP (_PICOHASH_MD5_F ,b ,c ,d ,a ,_PICOHASH_MD5_SET (7 ),0xfd469501 ,22 ) 183_PICOHASH_MD5_STEP (_PICOHASH_MD5_F ,a ,b ,c ,d ,_PICOHASH_MD5_SET (8 ),0x698098d8 ,7 ) 184_PICOHASH_MD5_STEP (_PICOHASH_MD5_F ,d ,a ,b ,c ,_PICOHASH_MD5_SET (9 ),0x8b44f7af ,12 ) 185_PICOHASH_MD5_STEP (_PICOHASH_MD5_F ,c ,d ,a ,b ,_PICOHASH_MD5_SET (10 ),0xffff5bb1 ,17 ) 186_PICOHASH_MD5_STEP (_PICOHASH_MD5_F ,b ,c ,d ,a ,_PICOHASH_MD5_SET (11 ),0x895cd7be ,22 ) 187_PICOHASH_MD5_STEP (_PICOHASH_MD5_F ,a ,b ,c ,d ,_PICOHASH_MD5_SET (12 ),0x6b901122 ,7 ) 188_PICOHASH_MD5_STEP (_PICOHASH_MD5_F ,d ,a ,b ,c ,_PICOHASH_MD5_SET (13 ),0xfd987193 ,12 ) 189_PICOHASH_MD5_STEP (_PICOHASH_MD5_F ,c ,d ,a ,b ,_PICOHASH_MD5_SET (14 ),0xa679438e ,17 ) 190_PICOHASH_MD5_STEP (_PICOHASH_MD5_F ,b ,c ,d ,a ,_PICOHASH_MD5_SET (15 ),0x49b40821 ,22 ) 191 192/* Round 2 */ 193_PICOHASH_MD5_STEP (_PICOHASH_MD5_G ,a ,b ,c ,d ,_PICOHASH_MD5_GET (1 ),0xf61e2562 ,5 ) 194_PICOHASH_MD5_STEP (_PICOHASH_MD5_G ,d ,a ,b ,c ,_PICOHASH_MD5_GET (6 ),0xc040b340 ,9 ) 195_PICOHASH_MD5_STEP (_PICOHASH_MD5_G ,c ,d ,a ,b ,_PICOHASH_MD5_GET (11 ),0x265e5a51 ,14 ) 196_PICOHASH_MD5_STEP (_PICOHASH_MD5_G ,b ,c ,d ,a ,_PICOHASH_MD5_GET (0 ),0xe9b6c7aa ,20 ) 197_PICOHASH_MD5_STEP (_PICOHASH_MD5_G ,a ,b ,c ,d ,_PICOHASH_MD5_GET (5 ),0xd62f105d ,5 ) 198_PICOHASH_MD5_STEP (_PICOHASH_MD5_G ,d ,a ,b ,c ,_PICOHASH_MD5_GET (10 ),0x02441453 ,9 ) 199_PICOHASH_MD5_STEP (_PICOHASH_MD5_G ,c ,d ,a ,b ,_PICOHASH_MD5_GET (15 ),0xd8a1e681 ,14 ) 200_PICOHASH_MD5_STEP (_PICOHASH_MD5_G ,b ,c ,d ,a ,_PICOHASH_MD5_GET (4 ),0xe7d3fbc8 ,20 ) 201_PICOHASH_MD5_STEP (_PICOHASH_MD5_G ,a ,b ,c ,d ,_PICOHASH_MD5_GET (9 ),0x21e1cde6 ,5 ) 202_PICOHASH_MD5_STEP (_PICOHASH_MD5_G ,d ,a ,b ,c ,_PICOHASH_MD5_GET (14 ),0xc33707d6 ,9 ) 203_PICOHASH_MD5_STEP (_PICOHASH_MD5_G ,c ,d ,a ,b ,_PICOHASH_MD5_GET (3 ),0xf4d50d87 ,14 ) 204_PICOHASH_MD5_STEP (_PICOHASH_MD5_G ,b ,c ,d ,a ,_PICOHASH_MD5_GET (8 ),0x455a14ed ,20 ) 205_PICOHASH_MD5_STEP (_PICOHASH_MD5_G ,a ,b ,c ,d ,_PICOHASH_MD5_GET (13 ),0xa9e3e905 ,5 ) 206_PICOHASH_MD5_STEP (_PICOHASH_MD5_G ,d ,a ,b ,c ,_PICOHASH_MD5_GET (2 ),0xfcefa3f8 ,9 ) 207_PICOHASH_MD5_STEP (_PICOHASH_MD5_G ,c ,d ,a ,b ,_PICOHASH_MD5_GET (7 ),0x676f02d9 ,14 ) 208_PICOHASH_MD5_STEP (_PICOHASH_MD5_G ,b ,c ,d ,a ,_PICOHASH_MD5_GET (12 ),0x8d2a4c8a ,20 ) 209 210/* Round 3 */ 211_PICOHASH_MD5_STEP (_PICOHASH_MD5_H ,a ,b ,c ,d ,_PICOHASH_MD5_GET (5 ),0xfffa3942 ,4 ) 212_PICOHASH_MD5_STEP (_PICOHASH_MD5_H ,d ,a ,b ,c ,_PICOHASH_MD5_GET (8 ),0x8771f681 ,11 ) 213_PICOHASH_MD5_STEP (_PICOHASH_MD5_H ,c ,d ,a ,b ,_PICOHASH_MD5_GET (11 ),0x6d9d6122 ,16 ) 214_PICOHASH_MD5_STEP (_PICOHASH_MD5_H ,b ,c ,d ,a ,_PICOHASH_MD5_GET (14 ),0xfde5380c ,23 ) 215_PICOHASH_MD5_STEP (_PICOHASH_MD5_H ,a ,b ,c ,d ,_PICOHASH_MD5_GET (1 ),0xa4beea44 ,4 ) 216_PICOHASH_MD5_STEP (_PICOHASH_MD5_H ,d ,a ,b ,c ,_PICOHASH_MD5_GET (4 ),0x4bdecfa9 ,11 ) 217_PICOHASH_MD5_STEP (_PICOHASH_MD5_H ,c ,d ,a ,b ,_PICOHASH_MD5_GET (7 ),0xf6bb4b60 ,16 ) 218_PICOHASH_MD5_STEP (_PICOHASH_MD5_H ,b ,c ,d ,a ,_PICOHASH_MD5_GET (10 ),0xbebfbc70 ,23 ) 219_PICOHASH_MD5_STEP (_PICOHASH_MD5_H ,a ,b ,c ,d ,_PICOHASH_MD5_GET (13 ),0x289b7ec6 ,4 ) 220_PICOHASH_MD5_STEP (_PICOHASH_MD5_H ,d ,a ,b ,c ,_PICOHASH_MD5_GET (0 ),0xeaa127fa ,11 ) 221_PICOHASH_MD5_STEP (_PICOHASH_MD5_H ,c ,d ,a ,b ,_PICOHASH_MD5_GET (3 ),0xd4ef3085 ,16 ) 222_PICOHASH_MD5_STEP (_PICOHASH_MD5_H ,b ,c ,d ,a ,_PICOHASH_MD5_GET (6 ),0x04881d05 ,23 ) 223_PICOHASH_MD5_STEP (_PICOHASH_MD5_H ,a ,b ,c ,d ,_PICOHASH_MD5_GET (9 ),0xd9d4d039 ,4 ) 224_PICOHASH_MD5_STEP (_PICOHASH_MD5_H ,d ,a ,b ,c ,_PICOHASH_MD5_GET (12 ),0xe6db99e5 ,11 ) 225_PICOHASH_MD5_STEP (_PICOHASH_MD5_H ,c ,d ,a ,b ,_PICOHASH_MD5_GET (15 ),0x1fa27cf8 ,16 ) 226_PICOHASH_MD5_STEP (_PICOHASH_MD5_H ,b ,c ,d ,a ,_PICOHASH_MD5_GET (2 ),0xc4ac5665 ,23 ) 227 228/* Round 4 */ 229_PICOHASH_MD5_STEP (_PICOHASH_MD5_I ,a ,b ,c ,d ,_PICOHASH_MD5_GET (0 ),0xf4292244 ,6 ) 230_PICOHASH_MD5_STEP (_PICOHASH_MD5_I ,d ,a ,b ,c ,_PICOHASH_MD5_GET (7 ),0x432aff97 ,10 ) 231_PICOHASH_MD5_STEP (_PICOHASH_MD5_I ,c ,d ,a ,b ,_PICOHASH_MD5_GET (14 ),0xab9423a7 ,15 ) 232_PICOHASH_MD5_STEP (_PICOHASH_MD5_I ,b ,c ,d ,a ,_PICOHASH_MD5_GET (5 ),0xfc93a039 ,21 ) 233_PICOHASH_MD5_STEP (_PICOHASH_MD5_I ,a ,b ,c ,d ,_PICOHASH_MD5_GET (12 ),0x655b59c3 ,6 ) 234_PICOHASH_MD5_STEP (_PICOHASH_MD5_I ,d ,a ,b ,c ,_PICOHASH_MD5_GET (3 ),0x8f0ccc92 ,10 ) 235_PICOHASH_MD5_STEP (_PICOHASH_MD5_I ,c ,d ,a ,b ,_PICOHASH_MD5_GET (10 ),0xffeff47d ,15 ) 236_PICOHASH_MD5_STEP (_PICOHASH_MD5_I ,b ,c ,d ,a ,_PICOHASH_MD5_GET (1 ),0x85845dd1 ,21 ) 237_PICOHASH_MD5_STEP (_PICOHASH_MD5_I ,a ,b ,c ,d ,_PICOHASH_MD5_GET (8 ),0x6fa87e4f ,6 ) 238_PICOHASH_MD5_STEP (_PICOHASH_MD5_I ,d ,a ,b ,c ,_PICOHASH_MD5_GET (15 ),0xfe2ce6e0 ,10 ) 239_PICOHASH_MD5_STEP (_PICOHASH_MD5_I ,c ,d ,a ,b ,_PICOHASH_MD5_GET (6 ),0xa3014314 ,15 ) 240_PICOHASH_MD5_STEP (_PICOHASH_MD5_I ,b ,c ,d ,a ,_PICOHASH_MD5_GET (13 ),0x4e0811a1 ,21 ) 241_PICOHASH_MD5_STEP (_PICOHASH_MD5_I ,a ,b ,c ,d ,_PICOHASH_MD5_GET (4 ),0xf7537e82 ,6 ) 242_PICOHASH_MD5_STEP (_PICOHASH_MD5_I ,d ,a ,b ,c ,_PICOHASH_MD5_GET (11 ),0xbd3af235 ,10 ) 243_PICOHASH_MD5_STEP (_PICOHASH_MD5_I ,c ,d ,a ,b ,_PICOHASH_MD5_GET (2 ),0x2ad7d2bb ,15 ) 244_PICOHASH_MD5_STEP (_PICOHASH_MD5_I ,b ,c ,d ,a ,_PICOHASH_MD5_GET (9 ),0xeb86d391 ,21 ) 245 246a += saved_a ; 247b += saved_b ; 248c += saved_c ; 249d += saved_d ; 250 251ptr += 64 ; 252 }while (size -= 64 ); 253 254ctx -> a = a ; 255ctx -> b = b ; 256ctx -> c = c ; 257ctx -> d = d ; 258 259return ptr ; 260} 261 262inline void _picohash_md5_init (_picohash_md5_ctx_t * ctx ) 263{ 264ctx -> a = 0x67452301 ; 265ctx -> b = 0xefcdab89 ; 266ctx -> c = 0x98badcfe ; 267ctx -> d = 0x10325476 ; 268 269ctx -> lo = 0 ; 270ctx -> hi = 0 ; 271} 272 273inline void _picohash_md5_update (_picohash_md5_ctx_t * ctx ,const void * data ,size_t size ) 274{ 275uint_fast32_t saved_lo ; 276unsigned long used ,free ; 277 278saved_lo = ctx -> lo ; 279if ((ctx -> lo = (saved_lo + size )& 0x1fffffff )< saved_lo ) 280ctx -> hi ++ ; 281ctx -> hi += size >>29 ; 282 283used = saved_lo & 0x3f ; 284 285if (used ) { 286free = 64 - used ; 287 288if (size < free ) { 289memcpy (& ctx -> buffer [used ],data ,size ); 290return ; 291 } 292 293memcpy (& ctx -> buffer [used ],data ,free ); 294data = (const unsigned char * )data + free ; 295size -= free ; 296_picohash_md5_body (ctx ,ctx -> buffer ,64 ); 297 } 298 299if (size >=64 ) { 300data = _picohash_md5_body (ctx ,data ,size & ~(unsigned long )0x3f ); 301size &=0x3f ; 302 } 303 304memcpy (ctx -> buffer ,data ,size ); 305} 306 307inline void _picohash_md5_final (_picohash_md5_ctx_t * ctx ,void * _digest ) 308{ 309unsigned char * digest = (unsigned char * )_digest ; 310unsigned long used ,free ; 311 312used = ctx -> lo & 0x3f ; 313 314ctx -> buffer [used ++ ]= 0x80 ; 315 316free = 64 - used ; 317 318if (free < 8 ) { 319memset (& ctx -> buffer [used ],0 ,free ); 320_picohash_md5_body (ctx ,ctx -> buffer ,64 ); 321used = 0 ; 322free = 64 ; 323 } 324 325memset (& ctx -> buffer [used ],0 ,free - 8 ); 326 327ctx -> lo <<=3 ; 328ctx -> buffer [56 ]= ctx -> lo ; 329ctx -> buffer [57 ]= ctx -> lo >>8 ; 330ctx -> buffer [58 ]= ctx -> lo >>16 ; 331ctx -> buffer [59 ]= ctx -> lo >>24 ; 332ctx -> buffer [60 ]= ctx -> hi ; 333ctx -> buffer [61 ]= ctx -> hi >>8 ; 334ctx -> buffer [62 ]= ctx -> hi >>16 ; 335ctx -> buffer [63 ]= ctx -> hi >>24 ; 336 337_picohash_md5_body (ctx ,ctx -> buffer ,64 ); 338 339digest [0 ]= ctx -> a ; 340digest [1 ]= ctx -> a >>8 ; 341digest [2 ]= ctx -> a >>16 ; 342digest [3 ]= ctx -> a >>24 ; 343digest [4 ]= ctx -> b ; 344digest [5 ]= ctx -> b >>8 ; 345digest [6 ]= ctx -> b >>16 ; 346digest [7 ]= ctx -> b >>24 ; 347digest [8 ]= ctx -> c ; 348digest [9 ]= ctx -> c >>8 ; 349digest [10 ]= ctx -> c >>16 ; 350digest [11 ]= ctx -> c >>24 ; 351digest [12 ]= ctx -> d ; 352digest [13 ]= ctx -> d >>8 ; 353digest [14 ]= ctx -> d >>16 ; 354digest [15 ]= ctx -> d >>24 ; 355 356memset (ctx ,0 ,sizeof (* ctx )); 357} 358 359#define _PICOHASH_SHA1_K0 0x5a827999 360#define _PICOHASH_SHA1_K20 0x6ed9eba1 361#define _PICOHASH_SHA1_K40 0x8f1bbcdc 362#define _PICOHASH_SHA1_K60 0xca62c1d6 363 364static inline uint32_t _picohash_sha1_rol32 (uint32_t number ,uint8_t bits ) 365{ 366return ((number <<bits ) | (number >> (32 - bits ))); 367} 368 369static inline void _picohash_sha1_hash_block (_picohash_sha1_ctx_t * s ) 370{ 371uint8_t i ; 372uint32_t a ,b ,c ,d ,e ,t ; 373 374a = s -> state [0 ]; 375b = s -> state [1 ]; 376c = s -> state [2 ]; 377d = s -> state [3 ]; 378e = s -> state [4 ]; 379for (i = 0 ;i < 80 ;i ++ ) { 380if (i >=16 ) { 381t = s -> buffer [(i + 13 )& 15 ] ^s -> buffer [(i + 8 )& 15 ] ^s -> buffer [(i + 2 )& 15 ] ^s -> buffer [i & 15 ]; 382s -> buffer [i & 15 ]= _picohash_sha1_rol32 (t ,1 ); 383 } 384if (i < 20 ) { 385t = (d ^ (b & (c ^d )))+ _PICOHASH_SHA1_K0 ; 386 }else if (i < 40 ) { 387t = (b ^c ^d )+ _PICOHASH_SHA1_K20 ; 388 }else if (i < 60 ) { 389t = ((b & c ) | (d & (b |c )))+ _PICOHASH_SHA1_K40 ; 390 }else { 391t = (b ^c ^d )+ _PICOHASH_SHA1_K60 ; 392 } 393t += _picohash_sha1_rol32 (a ,5 )+ e + s -> buffer [i & 15 ]; 394e = d ; 395d = c ; 396c = _picohash_sha1_rol32 (b ,30 ); 397b = a ; 398a = t ; 399 } 400s -> state [0 ]+= a ; 401s -> state [1 ]+= b ; 402s -> state [2 ]+= c ; 403s -> state [3 ]+= d ; 404s -> state [4 ]+= e ; 405} 406 407static inline void _picohash_sha1_add_uncounted (_picohash_sha1_ctx_t * s ,uint8_t data ) 408{ 409uint8_t * const b = (uint8_t * )s -> buffer ; 410#ifdef _PICOHASH_BIG_ENDIAN 411b [s -> bufferOffset ]= data ; 412#else 413b [s -> bufferOffset ^3 ]= data ; 414#endif 415s -> bufferOffset ++ ; 416if (s -> bufferOffset == PICOHASH_SHA1_BLOCK_LENGTH ) { 417_picohash_sha1_hash_block (s ); 418s -> bufferOffset = 0 ; 419 } 420} 421 422inline void _picohash_sha1_init (_picohash_sha1_ctx_t * s ) 423{ 424s -> state [0 ]= 0x67452301 ; 425s -> state [1 ]= 0xefcdab89 ; 426s -> state [2 ]= 0x98badcfe ; 427s -> state [3 ]= 0x10325476 ; 428s -> state [4 ]= 0xc3d2e1f0 ; 429s -> byteCount = 0 ; 430s -> bufferOffset = 0 ; 431} 432 433inline void _picohash_sha1_update (_picohash_sha1_ctx_t * s ,const void * _data ,size_t len ) 434{ 435const uint8_t * data = (const uint8_t * )_data ; 436for (;len != 0 ;-- len ) { 437++ s -> byteCount ; 438_picohash_sha1_add_uncounted (s ,* data ++ ); 439 } 440} 441 442inline void _picohash_sha1_final (_picohash_sha1_ctx_t * s ,void * digest ) 443{ 444// Pad with 0x80 followed by 0x00 until the end of the block 445_picohash_sha1_add_uncounted (s ,0x80 ); 446while (s -> bufferOffset != 56 ) 447_picohash_sha1_add_uncounted (s ,0x00 ); 448 449// Append length in the last 8 bytes 450_picohash_sha1_add_uncounted (s ,s -> byteCount >>53 );// Shifting to multiply by 8 451_picohash_sha1_add_uncounted (s ,s -> byteCount >>45 );// as SHA-1 supports bitstreams as well as 452_picohash_sha1_add_uncounted (s ,s -> byteCount >>37 );// byte. 453_picohash_sha1_add_uncounted (s ,s -> byteCount >>29 ); 454_picohash_sha1_add_uncounted (s ,s -> byteCount >>21 ); 455_picohash_sha1_add_uncounted (s ,s -> byteCount >>13 ); 456_picohash_sha1_add_uncounted (s ,s -> byteCount >>5 ); 457_picohash_sha1_add_uncounted (s ,s -> byteCount <<3 ); 458 459#ifndef SHA_BIG_ENDIAN 460 {// Swap byte order back 461int i ; 462for (i = 0 ;i < 5 ;i ++ ) { 463s -> state [i ]= (((s -> state [i ]) <<24 )& 0xff000000 ) | (((s -> state [i ]) <<8 )& 0x00ff0000 ) | 464 (((s -> state [i ]) >>8 )& 0x0000ff00 ) | (((s -> state [i ]) >>24 )& 0x000000ff ); 465 } 466 } 467#endif 468 469memcpy (digest ,s -> state ,sizeof (s -> state )); 470} 471 472#define _picohash_sha256_ch (x ,y ,z ) (z ^ (x & (y ^ z))) 473#define _picohash_sha256_maj (x ,y ,z ) (((x | y) & z) | (x & y)) 474#define _picohash_sha256_s (x ,y ) \ 475 (((((uint32_t)(x)&0xFFFFFFFFUL) >> (uint32_t)((y)&31)) | ((uint32_t)(x) << (uint32_t)(32 - ((y)&31)))) & 0xFFFFFFFFUL) 476#define _picohash_sha256_r (x ,n ) (((x)&0xFFFFFFFFUL) >> (n)) 477#define _picohash_sha256_sigma0 (x ) (_picohash_sha256_s(x, 2) ^ _picohash_sha256_s(x, 13) ^ _picohash_sha256_s(x, 22)) 478#define _picohash_sha256_sigma1 (x ) (_picohash_sha256_s(x, 6) ^ _picohash_sha256_s(x, 11) ^ _picohash_sha256_s(x, 25)) 479#define _picohash_sha256_gamma0 (x ) (_picohash_sha256_s(x, 7) ^ _picohash_sha256_s(x, 18) ^ _picohash_sha256_r(x, 3)) 480#define _picohash_sha256_gamma1 (x ) (_picohash_sha256_s(x, 17) ^ _picohash_sha256_s(x, 19) ^ _picohash_sha256_r(x, 10)) 481#define _picohash_sha256_rnd (a ,b ,c ,d ,e ,f ,g ,h ,i ) \ 482 t0 = h + _picohash_sha256_sigma1(e) + _picohash_sha256_ch(e, f, g) + K[i] + W[i]; \ 483 t1 = _picohash_sha256_sigma0(a) + _picohash_sha256_maj(a, b, c); \ 484 d += t0; \ 485 h = t0 + t1; 486 487static inline void _picohash_sha256_compress (_picohash_sha256_ctx_t * ctx ,unsigned char * buf ) 488{ 489static const uint32_t K [64 ]= { 4900x428a2f98UL ,0x71374491UL ,0xb5c0fbcfUL ,0xe9b5dba5UL ,0x3956c25bUL ,0x59f111f1UL ,0x923f82a4UL ,0xab1c5ed5UL , 4910xd807aa98UL ,0x12835b01UL ,0x243185beUL ,0x550c7dc3UL ,0x72be5d74UL ,0x80deb1feUL ,0x9bdc06a7UL ,0xc19bf174UL , 4920xe49b69c1UL ,0xefbe4786UL ,0x0fc19dc6UL ,0x240ca1ccUL ,0x2de92c6fUL ,0x4a7484aaUL ,0x5cb0a9dcUL ,0x76f988daUL , 4930x983e5152UL ,0xa831c66dUL ,0xb00327c8UL ,0xbf597fc7UL ,0xc6e00bf3UL ,0xd5a79147UL ,0x06ca6351UL ,0x14292967UL , 4940x27b70a85UL ,0x2e1b2138UL ,0x4d2c6dfcUL ,0x53380d13UL ,0x650a7354UL ,0x766a0abbUL ,0x81c2c92eUL ,0x92722c85UL , 4950xa2bfe8a1UL ,0xa81a664bUL ,0xc24b8b70UL ,0xc76c51a3UL ,0xd192e819UL ,0xd6990624UL ,0xf40e3585UL ,0x106aa070UL , 4960x19a4c116UL ,0x1e376c08UL ,0x2748774cUL ,0x34b0bcb5UL ,0x391c0cb3UL ,0x4ed8aa4aUL ,0x5b9cca4fUL ,0x682e6ff3UL , 4970x748f82eeUL ,0x78a5636fUL ,0x84c87814UL ,0x8cc70208UL ,0x90befffaUL ,0xa4506cebUL ,0xbef9a3f7UL ,0xc67178f2UL }; 498uint32_t S [8 ],W [64 ],t ,t0 ,t1 ; 499int i ; 500 501/* copy state into S */ 502for (i = 0 ;i < 8 ;i ++ ) 503S [i ]= ctx -> state [i ]; 504 505/* copy the state into 512-bits into W[0..15] */ 506for (i = 0 ;i < 16 ;i ++ ) 507W [i ]= 508 (uint32_t )buf [4 * i ] <<24 | (uint32_t )buf [4 * i + 1 ] <<16 | (uint32_t )buf [4 * i + 2 ] <<8 | (uint32_t )buf [4 * i + 3 ]; 509 510/* fill W[16..63] */ 511for (i = 16 ;i < 64 ;i ++ ) 512W [i ]= _picohash_sha256_gamma1 (W [i - 2 ])+ W [i - 7 ]+ _picohash_sha256_gamma0 (W [i - 15 ])+ W [i - 16 ]; 513 514/* Compress */ 515for (i = 0 ;i < 64 ;++ i ) { 516_picohash_sha256_rnd (S [0 ],S [1 ],S [2 ],S [3 ],S [4 ],S [5 ],S [6 ],S [7 ],i ); 517t = S [7 ]; 518S [7 ]= S [6 ]; 519S [6 ]= S [5 ]; 520S [5 ]= S [4 ]; 521S [4 ]= S [3 ]; 522S [3 ]= S [2 ]; 523S [2 ]= S [1 ]; 524S [1 ]= S [0 ]; 525S [0 ]= t ; 526 } 527 528/* feedback */ 529for (i = 0 ;i < 8 ;i ++ ) 530ctx -> state [i ]= ctx -> state [i ]+ S [i ]; 531} 532 533static inline void _picohash_sha256_do_final (_picohash_sha256_ctx_t * ctx ,void * digest ,size_t len ) 534{ 535unsigned char * out = (unsigned char * )digest ; 536size_t i ; 537 538/* increase the length of the message */ 539ctx -> length += ctx -> curlen * 8 ; 540 541/* append the '1' bit */ 542ctx -> buf [ctx -> curlen ++ ]= (unsigned char )0x80 ; 543 544/* if the length is currently above 56 bytes we append zeros 545* then compress. Then we can fall back to padding zeros and length 546* encoding like normal. 547*/ 548if (ctx -> curlen > 56 ) { 549while (ctx -> curlen < 64 ) { 550ctx -> buf [ctx -> curlen ++ ]= (unsigned char )0 ; 551 } 552_picohash_sha256_compress (ctx ,ctx -> buf ); 553ctx -> curlen = 0 ; 554 } 555 556/* pad upto 56 bytes of zeroes */ 557while (ctx -> curlen < 56 ) { 558ctx -> buf [ctx -> curlen ++ ]= (unsigned char )0 ; 559 } 560 561/* store length */ 562for (i = 0 ;i != 8 ;++ i ) 563ctx -> buf [56 + i ]= ctx -> length >> (56 - 8 * i ); 564_picohash_sha256_compress (ctx ,ctx -> buf ); 565 566/* copy output */ 567for (i = 0 ;i != len /4 ;++ i ) { 568out [i * 4 ]= ctx -> state [i ] >>24 ; 569out [i * 4 + 1 ]= ctx -> state [i ] >>16 ; 570out [i * 4 + 2 ]= ctx -> state [i ] >>8 ; 571out [i * 4 + 3 ]= ctx -> state [i ]; 572 } 573} 574 575inline void _picohash_sha256_init (_picohash_sha256_ctx_t * ctx ) 576{ 577ctx -> curlen = 0 ; 578ctx -> length = 0 ; 579ctx -> state [0 ]= 0x6A09E667UL ; 580ctx -> state [1 ]= 0xBB67AE85UL ; 581ctx -> state [2 ]= 0x3C6EF372UL ; 582ctx -> state [3 ]= 0xA54FF53AUL ; 583ctx -> state [4 ]= 0x510E527FUL ; 584ctx -> state [5 ]= 0x9B05688CUL ; 585ctx -> state [6 ]= 0x1F83D9ABUL ; 586ctx -> state [7 ]= 0x5BE0CD19UL ; 587} 588 589inline void _picohash_sha256_update (_picohash_sha256_ctx_t * ctx ,const void * data ,size_t len ) 590{ 591const unsigned char * in = (const unsigned char * )data ; 592size_t n ; 593 594while (len > 0 ) { 595if (ctx -> curlen == 0 && len >=PICOHASH_SHA256_BLOCK_LENGTH ) { 596_picohash_sha256_compress (ctx , (unsigned char * )in ); 597ctx -> length += PICOHASH_SHA256_BLOCK_LENGTH * 8 ; 598in += PICOHASH_SHA256_BLOCK_LENGTH ; 599len -= PICOHASH_SHA256_BLOCK_LENGTH ; 600 }else { 601n = PICOHASH_SHA256_BLOCK_LENGTH - ctx -> curlen ; 602if (n > len ) 603n = len ; 604memcpy (ctx -> buf + ctx -> curlen ,in , (size_t )n ); 605ctx -> curlen += n ; 606in += n ; 607len -= n ; 608if (ctx -> curlen == 64 ) { 609_picohash_sha256_compress (ctx ,ctx -> buf ); 610ctx -> length += 8 * PICOHASH_SHA256_BLOCK_LENGTH ; 611ctx -> curlen = 0 ; 612 } 613 } 614 } 615} 616 617inline void _picohash_sha256_final (_picohash_sha256_ctx_t * ctx ,void * digest ) 618{ 619_picohash_sha256_do_final (ctx ,digest ,PICOHASH_SHA256_DIGEST_LENGTH ); 620} 621 622inline void _picohash_sha224_init (_picohash_sha256_ctx_t * ctx ) 623{ 624ctx -> curlen = 0 ; 625ctx -> length = 0 ; 626ctx -> state [0 ]= 0xc1059ed8UL ; 627ctx -> state [1 ]= 0x367cd507UL ; 628ctx -> state [2 ]= 0x3070dd17UL ; 629ctx -> state [3 ]= 0xf70e5939UL ; 630ctx -> state [4 ]= 0xffc00b31UL ; 631ctx -> state [5 ]= 0x68581511UL ; 632ctx -> state [6 ]= 0x64f98fa7UL ; 633ctx -> state [7 ]= 0xbefa4fa4UL ; 634} 635 636inline void _picohash_sha224_final (_picohash_sha256_ctx_t * ctx ,void * digest ) 637{ 638_picohash_sha256_do_final (ctx ,digest ,PICOHASH_SHA224_DIGEST_LENGTH ); 639} 640 641inline void picohash_init_md5 (picohash_ctx_t * ctx ) 642{ 643ctx -> block_length = PICOHASH_MD5_BLOCK_LENGTH ; 644ctx -> digest_length = PICOHASH_MD5_DIGEST_LENGTH ; 645ctx -> _reset = (void (* )(void * ))_picohash_md5_init ; 646ctx -> _update = (void (* )(void * ,const void * ,size_t ))_picohash_md5_update ; 647ctx -> _final = (void (* )(void * ,void * ))_picohash_md5_final ; 648 649_picohash_md5_init (& ctx -> _md5 ); 650} 651 652inline void picohash_init_sha1 (picohash_ctx_t * ctx ) 653{ 654ctx -> block_length = PICOHASH_SHA1_BLOCK_LENGTH ; 655ctx -> digest_length = PICOHASH_SHA1_DIGEST_LENGTH ; 656ctx -> _reset = (void (* )(void * ))_picohash_sha1_init ; 657ctx -> _update = (void (* )(void * ,const void * ,size_t ))_picohash_sha1_update ; 658ctx -> _final = (void (* )(void * ,void * ))_picohash_sha1_final ; 659_picohash_sha1_init (& ctx -> _sha1 ); 660} 661 662inline void picohash_init_sha224 (picohash_ctx_t * ctx ) 663{ 664ctx -> block_length = PICOHASH_SHA224_BLOCK_LENGTH ; 665ctx -> digest_length = PICOHASH_SHA224_DIGEST_LENGTH ; 666ctx -> _reset = (void (* )(void * ))_picohash_sha224_init ; 667ctx -> _update = (void (* )(void * ,const void * ,size_t ))_picohash_sha256_update ; 668ctx -> _final = (void (* )(void * ,void * ))_picohash_sha224_final ; 669_picohash_sha224_init (& ctx -> _sha256 ); 670} 671 672inline void picohash_init_sha256 (picohash_ctx_t * ctx ) 673{ 674ctx -> block_length = PICOHASH_SHA256_BLOCK_LENGTH ; 675ctx -> digest_length = PICOHASH_SHA256_DIGEST_LENGTH ; 676ctx -> _reset = (void (* )(void * ))_picohash_sha256_init ; 677ctx -> _update = (void (* )(void * ,const void * ,size_t ))_picohash_sha256_update ; 678ctx -> _final = (void (* )(void * ,void * ))_picohash_sha256_final ; 679_picohash_sha256_init (& ctx -> _sha256 ); 680} 681 682inline void picohash_update (picohash_ctx_t * ctx ,const void * input ,size_t len ) 683{ 684ctx -> _update (ctx ,input ,len ); 685} 686 687inline void picohash_final (picohash_ctx_t * ctx ,void * digest ) 688{ 689ctx -> _final (ctx ,digest ); 690} 691 692inline void picohash_reset (picohash_ctx_t * ctx ) 693{ 694ctx -> _reset (ctx ); 695} 696 697static inline void _picohash_hmac_apply_key (picohash_ctx_t * ctx ,unsigned char delta ) 698{ 699size_t i ; 700for (i = 0 ;i != ctx -> block_length ;++ i ) 701ctx -> _hmac .key [i ] ^=delta ; 702picohash_update (ctx ,ctx -> _hmac .key ,ctx -> block_length ); 703for (i = 0 ;i != ctx -> block_length ;++ i ) 704ctx -> _hmac .key [i ] ^=delta ; 705} 706 707static void _picohash_hmac_final (picohash_ctx_t * ctx ,void * digest ) 708{ 709unsigned char inner_digest [PICOHASH_MAX_DIGEST_LENGTH ]; 710 711ctx -> _hmac .hash_final (ctx ,inner_digest ); 712 713ctx -> _hmac .hash_reset (ctx ); 714_picohash_hmac_apply_key (ctx ,0x5c ); 715picohash_update (ctx ,inner_digest ,ctx -> digest_length ); 716memset (inner_digest ,0 ,ctx -> digest_length ); 717 718ctx -> _hmac .hash_final (ctx ,digest ); 719} 720 721static inline void _picohash_hmac_reset (picohash_ctx_t * ctx ) 722{ 723ctx -> _hmac .hash_reset (ctx ); 724_picohash_hmac_apply_key (ctx ,0x36 ); 725} 726 727inline void picohash_init_hmac (picohash_ctx_t * ctx ,void (* initf )(picohash_ctx_t * ),const void * key ,size_t key_len ) 728{ 729initf (ctx ); 730 731memset (ctx -> _hmac .key ,0 ,ctx -> block_length ); 732if (key_len > ctx -> block_length ) { 733/* hash the key if it is too long */ 734picohash_update (ctx ,key ,key_len ); 735picohash_final (ctx ,ctx -> _hmac .key ); 736ctx -> _hmac .hash_reset (ctx ); 737 }else { 738memcpy (ctx -> _hmac .key ,key ,key_len ); 739 } 740 741/* replace reset and final function */ 742ctx -> _hmac .hash_reset = ctx -> _reset ; 743ctx -> _hmac .hash_final = ctx -> _final ; 744ctx -> _reset = (void (* )(void * ))_picohash_hmac_reset ; 745ctx -> _final = (void (* )(void * ,void * ))_picohash_hmac_final ; 746 747/* start calculating the inner hash */ 748_picohash_hmac_apply_key (ctx ,0x36 ); 749} 750 751#endif