LCOV - code coverage report
Current view: top level - external/include - siphash.h (source / functions) Hit Total Coverage
Test: cov.info Lines: 57 57 100.0 %
Date: 2015-08-15 Functions: 4 4 100.0 %

          Line data    Source code
       1             : /* ==========================================================================
       2             :  * siphash.h - SipHash-2-4 in a single header file
       3             :  * --------------------------------------------------------------------------
       4             :  * Derived by William Ahern from the reference implementation[1] published[2]
       5             :  * by Jean-Philippe Aumasson and Daniel J. Berstein. Licensed in kind.
       6             :  *
       7             :  * 1. https://www.131002.net/siphash/siphash24.c
       8             :  * 2. https://www.131002.net/siphash/
       9             :  * --------------------------------------------------------------------------
      10             :  * HISTORY:
      11             :  *
      12             :  * 2012-11-04 - Born.
      13             :  * --------------------------------------------------------------------------
      14             :  * USAGE:
      15             :  *
      16             :  * SipHash-2-4 takes as input two 64-bit words as the key, some number of
      17             :  * message bytes, and outputs a 64-bit word as the message digest. This
      18             :  * implementation employs two data structures: a struct sipkey for
      19             :  * representing the key, and a struct siphash for representing the hash
      20             :  * state.
      21             :  *
      22             :  * For converting a 16-byte unsigned char array to a key, use either the
      23             :  * macro sip_keyof or the routine sip_tokey. The former instantiates a
      24             :  * compound literal key, while the latter requires a key object as a
      25             :  * parameter.
      26             :  *
      27             :  *      unsigned char secret[16];
      28             :  *      arc4random_buf(secret, sizeof secret);
      29             :  *      struct sipkey *key = sip_keyof(secret);
      30             :  *
      31             :  * For hashing a message, use either the convenience macro siphash24 or the
      32             :  * routines sip24_init, sip24_update, and sip24_final.
      33             :  *
      34             :  *      struct siphash state;
      35             :  *      void *msg;
      36             :  *      size_t len;
      37             :  *      uint64_t hash;
      38             :  *
      39             :  *      sip24_init(&state, key);
      40             :  *      sip24_update(&state, msg, len);
      41             :  *      hash = sip24_final(&state);
      42             :  *
      43             :  * or
      44             :  *
      45             :  *      hash = siphash24(msg, len, key);
      46             :  *
      47             :  * To convert the 64-bit hash value to a canonical 8-byte little-endian
      48             :  * binary representation, use either the macro sip_binof or the routine
      49             :  * sip_tobin. The former instantiates and returns a compound literal array,
      50             :  * while the latter requires an array object as a parameter.
      51             :  * --------------------------------------------------------------------------
      52             :  * NOTES:
      53             :  *
      54             :  * o Neither sip_keyof, sip_binof, nor siphash24 will work with compilers
      55             :  *   lacking compound literal support. Instead, you must use the lower-level
      56             :  *   interfaces which take as parameters the temporary state objects.
      57             :  *
      58             :  * o Uppercase macros may evaluate parameters more than once. Lowercase
      59             :  *   macros should not exhibit any such side effects.
      60             :  * ==========================================================================
      61             :  */
      62             : #ifndef SIPHASH_H
      63             : #define SIPHASH_H
      64             : 
      65             : #include <stddef.h> /* size_t */
      66             : #include <stdint.h> /* uint64_t uint32_t uint8_t */
      67             : 
      68             : 
      69             : #define SIP_ROTL(x, b) (uint64_t)(((x) << (b)) | ( (x) >> (64 - (b))))
      70             : 
      71             : #define SIP_U32TO8_LE(p, v) \
      72             :         (p)[0] = (uint8_t)((v) >>  0); (p)[1] = (uint8_t)((v) >>  8); \
      73             :         (p)[2] = (uint8_t)((v) >> 16); (p)[3] = (uint8_t)((v) >> 24);
      74             : 
      75             : #define SIP_U64TO8_LE(p, v) \
      76             :         SIP_U32TO8_LE((p) + 0, (uint32_t)((v) >>  0)); \
      77             :         SIP_U32TO8_LE((p) + 4, (uint32_t)((v) >> 32));
      78             : 
      79             : #define SIP_U8TO64_LE(p) \
      80             :         (((uint64_t)((p)[0]) <<  0) | \
      81             :          ((uint64_t)((p)[1]) <<  8) | \
      82             :          ((uint64_t)((p)[2]) << 16) | \
      83             :          ((uint64_t)((p)[3]) << 24) | \
      84             :          ((uint64_t)((p)[4]) << 32) | \
      85             :          ((uint64_t)((p)[5]) << 40) | \
      86             :          ((uint64_t)((p)[6]) << 48) | \
      87             :          ((uint64_t)((p)[7]) << 56))
      88             : 
      89             : 
      90             : #define SIPHASH_INITIALIZER { 0, 0, 0, 0, { 0 }, 0, 0 }
      91             : 
      92             : struct siphash {
      93             :         uint64_t v0, v1, v2, v3;
      94             : 
      95             :         unsigned char buf[8], *p;
      96             :         uint64_t c;
      97             : }; /* struct siphash */
      98             : 
      99             : 
     100             : #define SIP_KEYLEN 16
     101             : 
     102             : struct sipkey {
     103             :         uint64_t k[2];
     104             : }; /* struct sipkey */
     105             : 
     106             : #define sip_keyof(k) sip_tokey(&(struct sipkey){ { 0 } }, (k))
     107             : 
     108             : static inline struct sipkey *sip_tokey(struct sipkey *key, const void *src) {
     109             :         key->k[0] = SIP_U8TO64_LE((const unsigned char *)src);
     110             :         key->k[1] = SIP_U8TO64_LE((const unsigned char *)src + 8);
     111             :         return key;
     112             : } /* sip_tokey() */
     113             : 
     114             : 
     115             : #define sip_binof(v) sip_tobin((unsigned char[8]){ 0 }, (v))
     116             : 
     117             : static inline void *sip_tobin(void *dst, uint64_t u64) {
     118             :         SIP_U64TO8_LE((unsigned char *)dst, u64);
     119             :         return dst;
     120             : } /* sip_tobin() */
     121             : 
     122             : 
     123         203 : static inline void sip_round(struct siphash *H, const int rounds) {
     124             :         int i;
     125             : 
     126         671 :         for (i = 0; i < rounds; i++) {
     127         468 :                 H->v0 += H->v1;
     128         468 :                 H->v1 = SIP_ROTL(H->v1, 13);
     129         468 :                 H->v1 ^= H->v0;
     130         468 :                 H->v0 = SIP_ROTL(H->v0, 32);
     131             : 
     132         468 :                 H->v2 += H->v3;
     133         468 :                 H->v3 = SIP_ROTL(H->v3, 16);
     134         468 :                 H->v3 ^= H->v2;
     135             : 
     136         468 :                 H->v0 += H->v3;
     137         468 :                 H->v3 = SIP_ROTL(H->v3, 21);
     138         468 :                 H->v3 ^= H->v0;
     139             : 
     140         468 :                 H->v2 += H->v1;
     141         468 :                 H->v1 = SIP_ROTL(H->v1, 17);
     142         468 :                 H->v1 ^= H->v2;
     143         468 :                 H->v2 = SIP_ROTL(H->v2, 32);
     144             :         }
     145         203 : } /* sip_round() */
     146             : 
     147             : 
     148          31 : static inline struct siphash *sip24_init(struct siphash *H, const struct sipkey *key) {
     149          31 :         H->v0 = 0x736f6d6570736575ULL ^ key->k[0];
     150          31 :         H->v1 = 0x646f72616e646f6dULL ^ key->k[1];
     151          31 :         H->v2 = 0x6c7967656e657261ULL ^ key->k[0];
     152          31 :         H->v3 = 0x7465646279746573ULL ^ key->k[1];
     153             : 
     154          31 :         H->p = H->buf;
     155          31 :         H->c = 0;
     156             : 
     157          31 :         return H;
     158             : } /* sip24_init() */
     159             : 
     160             : 
     161             : #define sip_endof(a) (&(a)[sizeof (a) / sizeof *(a)])
     162             : 
     163          31 : static inline struct siphash *sip24_update(struct siphash *H, const void *src, size_t len) {
     164          31 :         const unsigned char *p = src, *pe = p + len;
     165             :         uint64_t m;
     166             : 
     167             :         do {
     168        1585 :                 while (p < pe && H->p < sip_endof(H->buf))
     169        1247 :                         *H->p++ = *p++;
     170             : 
     171         169 :                 if (H->p < sip_endof(H->buf))
     172          28 :                         break;
     173             : 
     174         141 :                 m = SIP_U8TO64_LE(H->buf);
     175         141 :                 H->v3 ^= m;
     176         141 :                 sip_round(H, 2);
     177         141 :                 H->v0 ^= m;
     178             : 
     179         141 :                 H->p = H->buf;
     180         141 :                 H->c += 8;
     181         141 :         } while (p < pe);
     182             : 
     183          31 :         return H;
     184             : } /* sip24_update() */
     185             : 
     186             : 
     187          31 : static inline uint64_t sip24_final(struct siphash *H) {
     188          31 :         char left = H->p - H->buf;
     189          31 :         uint64_t b = (H->c + left) << 56;
     190             : 
     191          31 :         switch (left) {
     192           6 :         case 7: b |= (uint64_t)H->buf[6] << 48;
     193          10 :         case 6: b |= (uint64_t)H->buf[5] << 40;
     194          18 :         case 5: b |= (uint64_t)H->buf[4] << 32;
     195          18 :         case 4: b |= (uint64_t)H->buf[3] << 24;
     196          18 :         case 3: b |= (uint64_t)H->buf[2] << 16;
     197          21 :         case 2: b |= (uint64_t)H->buf[1] << 8;
     198          28 :         case 1: b |= (uint64_t)H->buf[0] << 0;
     199          31 :         case 0: break;
     200             :         }
     201             : 
     202          31 :         H->v3 ^= b;
     203          31 :         sip_round(H, 2);
     204          31 :         H->v0 ^= b;
     205          31 :         H->v2 ^= 0xff;
     206          31 :         sip_round(H, 4);
     207             : 
     208          31 :         return H->v0 ^ H->v1 ^ H->v2  ^ H->v3;
     209             : } /* sip24_final() */
     210             : 
     211             : 
     212             : #define siphash24(src, len, key) \
     213             :         sip24_final(sip24_update(sip24_init(&(struct siphash)SIPHASH_INITIALIZER, (key)), (src), (len)))
     214             : 
     215             : 
     216             : #if SIPHASH_MAIN
     217             : 
     218             : /*
     219             :  * SipHash-2-4 output with
     220             :  * k = 00 01 02 ...
     221             :  * and
     222             :  * in = (empty string)
     223             :  * in = 00 (1 byte)
     224             :  * in = 00 01 (2 bytes)
     225             :  * in = 00 01 02 (3 bytes)
     226             :  * ...
     227             :  * in = 00 01 02 ... 3e (63 bytes)
     228             :  */
     229             : static inline _Bool sip24_valid(void) {
     230             :         static const unsigned char vectors[64][8] = {
     231             :                 { 0x31, 0x0e, 0x0e, 0xdd, 0x47, 0xdb, 0x6f, 0x72, },
     232             :                 { 0xfd, 0x67, 0xdc, 0x93, 0xc5, 0x39, 0xf8, 0x74, },
     233             :                 { 0x5a, 0x4f, 0xa9, 0xd9, 0x09, 0x80, 0x6c, 0x0d, },
     234             :                 { 0x2d, 0x7e, 0xfb, 0xd7, 0x96, 0x66, 0x67, 0x85, },
     235             :                 { 0xb7, 0x87, 0x71, 0x27, 0xe0, 0x94, 0x27, 0xcf, },
     236             :                 { 0x8d, 0xa6, 0x99, 0xcd, 0x64, 0x55, 0x76, 0x18, },
     237             :                 { 0xce, 0xe3, 0xfe, 0x58, 0x6e, 0x46, 0xc9, 0xcb, },
     238             :                 { 0x37, 0xd1, 0x01, 0x8b, 0xf5, 0x00, 0x02, 0xab, },
     239             :                 { 0x62, 0x24, 0x93, 0x9a, 0x79, 0xf5, 0xf5, 0x93, },
     240             :                 { 0xb0, 0xe4, 0xa9, 0x0b, 0xdf, 0x82, 0x00, 0x9e, },
     241             :                 { 0xf3, 0xb9, 0xdd, 0x94, 0xc5, 0xbb, 0x5d, 0x7a, },
     242             :                 { 0xa7, 0xad, 0x6b, 0x22, 0x46, 0x2f, 0xb3, 0xf4, },
     243             :                 { 0xfb, 0xe5, 0x0e, 0x86, 0xbc, 0x8f, 0x1e, 0x75, },
     244             :                 { 0x90, 0x3d, 0x84, 0xc0, 0x27, 0x56, 0xea, 0x14, },
     245             :                 { 0xee, 0xf2, 0x7a, 0x8e, 0x90, 0xca, 0x23, 0xf7, },
     246             :                 { 0xe5, 0x45, 0xbe, 0x49, 0x61, 0xca, 0x29, 0xa1, },
     247             :                 { 0xdb, 0x9b, 0xc2, 0x57, 0x7f, 0xcc, 0x2a, 0x3f, },
     248             :                 { 0x94, 0x47, 0xbe, 0x2c, 0xf5, 0xe9, 0x9a, 0x69, },
     249             :                 { 0x9c, 0xd3, 0x8d, 0x96, 0xf0, 0xb3, 0xc1, 0x4b, },
     250             :                 { 0xbd, 0x61, 0x79, 0xa7, 0x1d, 0xc9, 0x6d, 0xbb, },
     251             :                 { 0x98, 0xee, 0xa2, 0x1a, 0xf2, 0x5c, 0xd6, 0xbe, },
     252             :                 { 0xc7, 0x67, 0x3b, 0x2e, 0xb0, 0xcb, 0xf2, 0xd0, },
     253             :                 { 0x88, 0x3e, 0xa3, 0xe3, 0x95, 0x67, 0x53, 0x93, },
     254             :                 { 0xc8, 0xce, 0x5c, 0xcd, 0x8c, 0x03, 0x0c, 0xa8, },
     255             :                 { 0x94, 0xaf, 0x49, 0xf6, 0xc6, 0x50, 0xad, 0xb8, },
     256             :                 { 0xea, 0xb8, 0x85, 0x8a, 0xde, 0x92, 0xe1, 0xbc, },
     257             :                 { 0xf3, 0x15, 0xbb, 0x5b, 0xb8, 0x35, 0xd8, 0x17, },
     258             :                 { 0xad, 0xcf, 0x6b, 0x07, 0x63, 0x61, 0x2e, 0x2f, },
     259             :                 { 0xa5, 0xc9, 0x1d, 0xa7, 0xac, 0xaa, 0x4d, 0xde, },
     260             :                 { 0x71, 0x65, 0x95, 0x87, 0x66, 0x50, 0xa2, 0xa6, },
     261             :                 { 0x28, 0xef, 0x49, 0x5c, 0x53, 0xa3, 0x87, 0xad, },
     262             :                 { 0x42, 0xc3, 0x41, 0xd8, 0xfa, 0x92, 0xd8, 0x32, },
     263             :                 { 0xce, 0x7c, 0xf2, 0x72, 0x2f, 0x51, 0x27, 0x71, },
     264             :                 { 0xe3, 0x78, 0x59, 0xf9, 0x46, 0x23, 0xf3, 0xa7, },
     265             :                 { 0x38, 0x12, 0x05, 0xbb, 0x1a, 0xb0, 0xe0, 0x12, },
     266             :                 { 0xae, 0x97, 0xa1, 0x0f, 0xd4, 0x34, 0xe0, 0x15, },
     267             :                 { 0xb4, 0xa3, 0x15, 0x08, 0xbe, 0xff, 0x4d, 0x31, },
     268             :                 { 0x81, 0x39, 0x62, 0x29, 0xf0, 0x90, 0x79, 0x02, },
     269             :                 { 0x4d, 0x0c, 0xf4, 0x9e, 0xe5, 0xd4, 0xdc, 0xca, },
     270             :                 { 0x5c, 0x73, 0x33, 0x6a, 0x76, 0xd8, 0xbf, 0x9a, },
     271             :                 { 0xd0, 0xa7, 0x04, 0x53, 0x6b, 0xa9, 0x3e, 0x0e, },
     272             :                 { 0x92, 0x59, 0x58, 0xfc, 0xd6, 0x42, 0x0c, 0xad, },
     273             :                 { 0xa9, 0x15, 0xc2, 0x9b, 0xc8, 0x06, 0x73, 0x18, },
     274             :                 { 0x95, 0x2b, 0x79, 0xf3, 0xbc, 0x0a, 0xa6, 0xd4, },
     275             :                 { 0xf2, 0x1d, 0xf2, 0xe4, 0x1d, 0x45, 0x35, 0xf9, },
     276             :                 { 0x87, 0x57, 0x75, 0x19, 0x04, 0x8f, 0x53, 0xa9, },
     277             :                 { 0x10, 0xa5, 0x6c, 0xf5, 0xdf, 0xcd, 0x9a, 0xdb, },
     278             :                 { 0xeb, 0x75, 0x09, 0x5c, 0xcd, 0x98, 0x6c, 0xd0, },
     279             :                 { 0x51, 0xa9, 0xcb, 0x9e, 0xcb, 0xa3, 0x12, 0xe6, },
     280             :                 { 0x96, 0xaf, 0xad, 0xfc, 0x2c, 0xe6, 0x66, 0xc7, },
     281             :                 { 0x72, 0xfe, 0x52, 0x97, 0x5a, 0x43, 0x64, 0xee, },
     282             :                 { 0x5a, 0x16, 0x45, 0xb2, 0x76, 0xd5, 0x92, 0xa1, },
     283             :                 { 0xb2, 0x74, 0xcb, 0x8e, 0xbf, 0x87, 0x87, 0x0a, },
     284             :                 { 0x6f, 0x9b, 0xb4, 0x20, 0x3d, 0xe7, 0xb3, 0x81, },
     285             :                 { 0xea, 0xec, 0xb2, 0xa3, 0x0b, 0x22, 0xa8, 0x7f, },
     286             :                 { 0x99, 0x24, 0xa4, 0x3c, 0xc1, 0x31, 0x57, 0x24, },
     287             :                 { 0xbd, 0x83, 0x8d, 0x3a, 0xaf, 0xbf, 0x8d, 0xb7, },
     288             :                 { 0x0b, 0x1a, 0x2a, 0x32, 0x65, 0xd5, 0x1a, 0xea, },
     289             :                 { 0x13, 0x50, 0x79, 0xa3, 0x23, 0x1c, 0xe6, 0x60, },
     290             :                 { 0x93, 0x2b, 0x28, 0x46, 0xe4, 0xd7, 0x06, 0x66, },
     291             :                 { 0xe1, 0x91, 0x5f, 0x5c, 0xb1, 0xec, 0xa4, 0x6c, },
     292             :                 { 0xf3, 0x25, 0x96, 0x5c, 0xa1, 0x6d, 0x62, 0x9f, },
     293             :                 { 0x57, 0x5f, 0xf2, 0x8e, 0x60, 0x38, 0x1b, 0xe5, },
     294             :                 { 0x72, 0x45, 0x06, 0xeb, 0x4c, 0x32, 0x8a, 0x95, }
     295             :         };
     296             :         unsigned char in[64];
     297             :         struct sipkey k;
     298             :         size_t i;
     299             : 
     300             :         sip_tokey(&k, "\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017");
     301             : 
     302             :         for (i = 0; i < sizeof in; ++i) {
     303             :                 in[i] = i;
     304             : 
     305             :                 if (siphash24(in, i, &k) != SIP_U8TO64_LE(vectors[i]))
     306             :                         return 0;
     307             :         }
     308             : 
     309             :         return 1;
     310             : } /* sip24_valid() */
     311             : 
     312             : 
     313             : #include <stdio.h>
     314             : 
     315             : int main(void) {
     316             :         _Bool ok = sip24_valid();
     317             : 
     318             :         if (ok)
     319             :                 puts("OK");
     320             :         else
     321             :                 puts("FAIL");
     322             : 
     323             :         return !ok;
     324             : } /* main() */
     325             : 
     326             : #endif /* SIPHASH_MAIN */
     327             : 
     328             : 
     329             : #endif /* SIPHASH_H */

Generated by: LCOV version 1.10