]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/siphash24.c
siphash24: introduce state struct
[thirdparty/systemd.git] / src / basic / siphash24.c
CommitLineData
9bf3b535
LP
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
23typedef uint64_t u64;
24typedef uint32_t u32;
25typedef 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
708684ef 47#define SIPROUND(state) \
9bf3b535 48 do { \
708684ef
TG
49 (state)->v0 += (state)->v1; (state)->v1=ROTL((state)->v1,13); (state)->v1 ^= (state)->v0; (state)->v0=ROTL((state)->v0,32); \
50 (state)->v2 += (state)->v3; (state)->v3=ROTL((state)->v3,16); (state)->v3 ^= (state)->v2; \
51 (state)->v0 += (state)->v3; (state)->v3=ROTL((state)->v3,21); (state)->v3 ^= (state)->v0; \
52 (state)->v2 += (state)->v1; (state)->v1=ROTL((state)->v1,17); (state)->v1 ^= (state)->v2; (state)->v2=ROTL((state)->v2,32); \
9bf3b535
LP
53 } while(0)
54
708684ef
TG
55struct siphash {
56 u64 v0;
57 u64 v1;
58 u64 v2;
59 u64 v3;
60};
61
62static void siphash_init(struct siphash *state, const uint8_t k[16]) {
63 u64 k0, k1;
64
65 k0 = U8TO64_LE( k );
66 k1 = U8TO64_LE( k + 8 );
67
68 /* "somepseudorandomlygeneratedbytes" */
69 state->v0 = 0x736f6d6570736575ULL ^ k0;
70 state->v1 = 0x646f72616e646f6dULL ^ k1;
71 state->v2 = 0x6c7967656e657261ULL ^ k0;
72 state->v3 = 0x7465646279746573ULL ^ k1;
73}
74
9bf3b535
LP
75/* SipHash-2-4 */
76void siphash24(uint8_t out[8], const void *_in, size_t inlen, const uint8_t k[16])
77{
708684ef 78 struct siphash state;
9bf3b535 79 u64 b;
9bf3b535
LP
80 u64 m;
81 const u8 *in = _in;
82 const u8 *end = in + inlen - ( inlen % sizeof( u64 ) );
83 const int left = inlen & 7;
84 b = ( ( u64 )inlen ) << 56;
708684ef
TG
85
86 siphash_init(&state, k);
9bf3b535
LP
87
88 for ( ; in != end; in += 8 )
89 {
90 m = U8TO64_LE( in );
91#ifdef DEBUG
708684ef
TG
92 printf( "(%3d) v0 %08x %08x\n", ( int )inlen, ( u32 )( state.v0 >> 32 ), ( u32 )state.v0 );
93 printf( "(%3d) v1 %08x %08x\n", ( int )inlen, ( u32 )( state.v1 >> 32 ), ( u32 )state.v1 );
94 printf( "(%3d) v2 %08x %08x\n", ( int )inlen, ( u32 )( state.v2 >> 32 ), ( u32 )state.v2 );
95 printf( "(%3d) v3 %08x %08x\n", ( int )inlen, ( u32 )( state.v3 >> 32 ), ( u32 )state.v3 );
9bf3b535
LP
96 printf( "(%3d) compress %08x %08x\n", ( int )inlen, ( u32 )( m >> 32 ), ( u32 )m );
97#endif
708684ef
TG
98 state.v3 ^= m;
99 SIPROUND(&state);
100 SIPROUND(&state);
101 state.v0 ^= m;
9bf3b535
LP
102 }
103
104 switch( left )
105 {
106 case 7: b |= ( ( u64 )in[ 6] ) << 48;
107
108 case 6: b |= ( ( u64 )in[ 5] ) << 40;
109
110 case 5: b |= ( ( u64 )in[ 4] ) << 32;
111
112 case 4: b |= ( ( u64 )in[ 3] ) << 24;
113
114 case 3: b |= ( ( u64 )in[ 2] ) << 16;
115
116 case 2: b |= ( ( u64 )in[ 1] ) << 8;
117
118 case 1: b |= ( ( u64 )in[ 0] ); break;
119
120 case 0: break;
121 }
122
123#ifdef DEBUG
708684ef
TG
124 printf( "(%3d) v0 %08x %08x\n", ( int )inlen, ( u32 )( state.v0 >> 32 ), ( u32 )state.v0 );
125 printf( "(%3d) v1 %08x %08x\n", ( int )inlen, ( u32 )( state.v1 >> 32 ), ( u32 )state.v1 );
126 printf( "(%3d) v2 %08x %08x\n", ( int )inlen, ( u32 )( state.v2 >> 32 ), ( u32 )state.v2 );
127 printf( "(%3d) v3 %08x %08x\n", ( int )inlen, ( u32 )( state.v3 >> 32 ), ( u32 )state.v3 );
9bf3b535
LP
128 printf( "(%3d) padding %08x %08x\n", ( int )inlen, ( u32 )( b >> 32 ), ( u32 )b );
129#endif
708684ef
TG
130 state.v3 ^= b;
131 SIPROUND(&state);
132 SIPROUND(&state);
133 state.v0 ^= b;
9bf3b535 134#ifdef DEBUG
708684ef
TG
135 printf( "(%3d) v0 %08x %08x\n", ( int )inlen, ( u32 )( state.v0 >> 32 ), ( u32 )state.v0 );
136 printf( "(%3d) v1 %08x %08x\n", ( int )inlen, ( u32 )( state.v1 >> 32 ), ( u32 )state.v1 );
137 printf( "(%3d) v2 %08x %08x\n", ( int )inlen, ( u32 )( state.v2 >> 32 ), ( u32 )state.v2 );
138 printf( "(%3d) v3 %08x %08x\n", ( int )inlen, ( u32 )( state.v3 >> 32 ), ( u32 )state.v3 );
9bf3b535 139#endif
708684ef
TG
140 state.v2 ^= 0xff;
141 SIPROUND(&state);
142 SIPROUND(&state);
143 SIPROUND(&state);
144 SIPROUND(&state);
145 b = state.v0 ^ state.v1 ^ state.v2 ^ state.v3;
9bf3b535
LP
146 U64TO8_LE( out, b );
147}