LCOV - code coverage report
Current view: top level - basic - siphash24.c (source / functions) Hit Total Coverage
Test: systemd test coverage Lines: 42 42 100.0 %
Date: 2015-07-29 18:47:03 Functions: 1 1 100.0 %

          Line data    Source code
       1             : /*
       2             :    SipHash reference C implementation
       3             : 
       4             :    Written in 2012 by
       5             :    Jean-Philippe Aumasson <jeanphilippe.aumasson@gmail.com>
       6             :    Daniel J. Bernstein <djb@cr.yp.to>
       7             : 
       8             :    To the extent possible under law, the author(s) have dedicated all copyright
       9             :    and related and neighboring rights to this software to the public domain
      10             :    worldwide. This software is distributed without any warranty.
      11             : 
      12             :    You should have received a copy of the CC0 Public Domain Dedication along with
      13             :    this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
      14             : 
      15             :    (Minimal changes made by Lennart Poettering, to make clean for inclusion in systemd)
      16             : */
      17             : #include <stdint.h>
      18             : #include <stdio.h>
      19             : #include <string.h>
      20             : 
      21             : #include "siphash24.h"
      22             : 
      23             : typedef uint64_t u64;
      24             : typedef uint32_t u32;
      25             : typedef uint8_t u8;
      26             : 
      27             : #define ROTL(x,b) (u64)( ((x) << (b)) | ( (x) >> (64 - (b))) )
      28             : 
      29             : #define U32TO8_LE(p, v)         \
      30             :     (p)[0] = (u8)((v)      ); (p)[1] = (u8)((v) >>  8); \
      31             :     (p)[2] = (u8)((v) >> 16); (p)[3] = (u8)((v) >> 24);
      32             : 
      33             : #define U64TO8_LE(p, v)         \
      34             :   U32TO8_LE((p),     (u32)((v)      ));   \
      35             :   U32TO8_LE((p) + 4, (u32)((v) >> 32));
      36             : 
      37             : #define U8TO64_LE(p) \
      38             :   (((u64)((p)[0])      ) | \
      39             :    ((u64)((p)[1]) <<  8) | \
      40             :    ((u64)((p)[2]) << 16) | \
      41             :    ((u64)((p)[3]) << 24) | \
      42             :    ((u64)((p)[4]) << 32) | \
      43             :    ((u64)((p)[5]) << 40) | \
      44             :    ((u64)((p)[6]) << 48) | \
      45             :    ((u64)((p)[7]) << 56))
      46             : 
      47             : #define SIPROUND            \
      48             :   do {              \
      49             :     v0 += v1; v1=ROTL(v1,13); v1 ^= v0; v0=ROTL(v0,32); \
      50             :     v2 += v3; v3=ROTL(v3,16); v3 ^= v2;     \
      51             :     v0 += v3; v3=ROTL(v3,21); v3 ^= v0;     \
      52             :     v2 += v1; v1=ROTL(v1,17); v1 ^= v2; v2=ROTL(v2,32); \
      53             :   } while(0)
      54             : 
      55             : /* SipHash-2-4 */
      56    33860433 : void siphash24(uint8_t out[8], const void *_in, size_t inlen, const uint8_t k[16])
      57             : {
      58             :   /* "somepseudorandomlygeneratedbytes" */
      59    33860433 :   u64 v0 = 0x736f6d6570736575ULL;
      60    33860433 :   u64 v1 = 0x646f72616e646f6dULL;
      61    33860433 :   u64 v2 = 0x6c7967656e657261ULL;
      62    33860433 :   u64 v3 = 0x7465646279746573ULL;
      63             :   u64 b;
      64    33860433 :   u64 k0 = U8TO64_LE( k );
      65    33860433 :   u64 k1 = U8TO64_LE( k + 8 );
      66             :   u64 m;
      67    33860433 :   const u8 *in = _in;
      68    33860433 :   const u8 *end = in + inlen - ( inlen % sizeof( u64 ) );
      69    33860433 :   const int left = inlen & 7;
      70    33860433 :   b = ( ( u64 )inlen ) << 56;
      71    33860433 :   v3 ^= k1;
      72    33860433 :   v2 ^= k0;
      73    33860433 :   v1 ^= k1;
      74    33860433 :   v0 ^= k0;
      75             : 
      76    67842958 :   for ( ; in != end; in += 8 )
      77             :   {
      78    33982525 :     m = U8TO64_LE( in );
      79             : #ifdef DEBUG
      80             :     printf( "(%3d) v0 %08x %08x\n", ( int )inlen, ( u32 )( v0 >> 32 ), ( u32 )v0 );
      81             :     printf( "(%3d) v1 %08x %08x\n", ( int )inlen, ( u32 )( v1 >> 32 ), ( u32 )v1 );
      82             :     printf( "(%3d) v2 %08x %08x\n", ( int )inlen, ( u32 )( v2 >> 32 ), ( u32 )v2 );
      83             :     printf( "(%3d) v3 %08x %08x\n", ( int )inlen, ( u32 )( v3 >> 32 ), ( u32 )v3 );
      84             :     printf( "(%3d) compress %08x %08x\n", ( int )inlen, ( u32 )( m >> 32 ), ( u32 )m );
      85             : #endif
      86    33982525 :     v3 ^= m;
      87    33982525 :     SIPROUND;
      88    33982525 :     SIPROUND;
      89    33982525 :     v0 ^= m;
      90             :   }
      91             : 
      92    33860433 :   switch( left )
      93             :   {
      94        6999 :   case 7: b |= ( ( u64 )in[ 6] )  << 48;
      95             : 
      96       12992 :   case 6: b |= ( ( u64 )in[ 5] )  << 40;
      97             : 
      98       17709 :   case 5: b |= ( ( u64 )in[ 4] )  << 32;
      99             : 
     100       25658 :   case 4: b |= ( ( u64 )in[ 3] )  << 24;
     101             : 
     102       32147 :   case 3: b |= ( ( u64 )in[ 2] )  << 16;
     103             : 
     104       38004 :   case 2: b |= ( ( u64 )in[ 1] )  <<  8;
     105             : 
     106       42421 :   case 1: b |= ( ( u64 )in[ 0] ); break;
     107             : 
     108    33818012 :   case 0: break;
     109             :   }
     110             : 
     111             : #ifdef DEBUG
     112             :   printf( "(%3d) v0 %08x %08x\n", ( int )inlen, ( u32 )( v0 >> 32 ), ( u32 )v0 );
     113             :   printf( "(%3d) v1 %08x %08x\n", ( int )inlen, ( u32 )( v1 >> 32 ), ( u32 )v1 );
     114             :   printf( "(%3d) v2 %08x %08x\n", ( int )inlen, ( u32 )( v2 >> 32 ), ( u32 )v2 );
     115             :   printf( "(%3d) v3 %08x %08x\n", ( int )inlen, ( u32 )( v3 >> 32 ), ( u32 )v3 );
     116             :   printf( "(%3d) padding   %08x %08x\n", ( int )inlen, ( u32 )( b >> 32 ), ( u32 )b );
     117             : #endif
     118    33860433 :   v3 ^= b;
     119    33860433 :   SIPROUND;
     120    33860433 :   SIPROUND;
     121    33860433 :   v0 ^= b;
     122             : #ifdef DEBUG
     123             :   printf( "(%3d) v0 %08x %08x\n", ( int )inlen, ( u32 )( v0 >> 32 ), ( u32 )v0 );
     124             :   printf( "(%3d) v1 %08x %08x\n", ( int )inlen, ( u32 )( v1 >> 32 ), ( u32 )v1 );
     125             :   printf( "(%3d) v2 %08x %08x\n", ( int )inlen, ( u32 )( v2 >> 32 ), ( u32 )v2 );
     126             :   printf( "(%3d) v3 %08x %08x\n", ( int )inlen, ( u32 )( v3 >> 32 ), ( u32 )v3 );
     127             : #endif
     128    33860433 :   v2 ^= 0xff;
     129    33860433 :   SIPROUND;
     130    33860433 :   SIPROUND;
     131    33860433 :   SIPROUND;
     132    33860433 :   SIPROUND;
     133    33860433 :   b = v0 ^ v1 ^ v2  ^ v3;
     134    33860433 :   U64TO8_LE( out, b );
     135    33860433 : }

Generated by: LCOV version 1.11