]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/siphash24.c
siphash24: move last compression iteration from compression step to finalization...
[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;
f2936011
TG
60 u64 padding;
61 size_t inlen;
708684ef
TG
62};
63
64static void siphash_init(struct siphash *state, const uint8_t k[16]) {
65 u64 k0, k1;
66
67 k0 = U8TO64_LE( k );
68 k1 = U8TO64_LE( k + 8 );
69
70 /* "somepseudorandomlygeneratedbytes" */
71 state->v0 = 0x736f6d6570736575ULL ^ k0;
72 state->v1 = 0x646f72616e646f6dULL ^ k1;
73 state->v2 = 0x6c7967656e657261ULL ^ k0;
74 state->v3 = 0x7465646279746573ULL ^ k1;
f2936011
TG
75 state->padding = 0;
76 state->inlen = 0;
708684ef
TG
77}
78
c7b68d84 79static void siphash24_compress(const void *_in, size_t inlen, struct siphash *state) {
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;
708684ef 84
f2936011 85 state->inlen = inlen;
9bf3b535
LP
86
87 for ( ; in != end; in += 8 )
88 {
89 m = U8TO64_LE( in );
90#ifdef DEBUG
f2936011
TG
91 printf( "(%3d) v0 %08x %08x\n", ( int )state->inlen, ( u32 )( state->v0 >> 32 ), ( u32 )state->v0 );
92 printf( "(%3d) v1 %08x %08x\n", ( int )state->inlen, ( u32 )( state->v1 >> 32 ), ( u32 )state->v1 );
93 printf( "(%3d) v2 %08x %08x\n", ( int )state->inlen, ( u32 )( state->v2 >> 32 ), ( u32 )state->v2 );
94 printf( "(%3d) v3 %08x %08x\n", ( int )state->inlen, ( u32 )( state->v3 >> 32 ), ( u32 )state->v3 );
95 printf( "(%3d) compress %08x %08x\n", ( int )state->inlen, ( u32 )( m >> 32 ), ( u32 )m );
9bf3b535 96#endif
c7b68d84
TG
97 state->v3 ^= m;
98 SIPROUND(state);
99 SIPROUND(state);
100 state->v0 ^= m;
9bf3b535
LP
101 }
102
103 switch( left )
104 {
f2936011 105 case 7: state->padding |= ( ( u64 )in[ 6] ) << 48;
9bf3b535 106
f2936011 107 case 6: state->padding |= ( ( u64 )in[ 5] ) << 40;
9bf3b535 108
f2936011 109 case 5: state->padding |= ( ( u64 )in[ 4] ) << 32;
9bf3b535 110
f2936011 111 case 4: state->padding |= ( ( u64 )in[ 3] ) << 24;
9bf3b535 112
f2936011 113 case 3: state->padding |= ( ( u64 )in[ 2] ) << 16;
9bf3b535 114
f2936011 115 case 2: state->padding |= ( ( u64 )in[ 1] ) << 8;
9bf3b535 116
f2936011 117 case 1: state->padding |= ( ( u64 )in[ 0] ); break;
9bf3b535
LP
118
119 case 0: break;
120 }
f2936011
TG
121}
122
123static u64 siphash24_finalize(struct siphash *state) {
124 u64 b;
9bf3b535 125
f2936011 126 b = state->padding | (( ( u64 )state->inlen ) << 56);
9bf3b535 127#ifdef DEBUG
f2936011
TG
128 printf( "(%3d) v0 %08x %08x\n", ( int )state->inlen, ( u32 )( state->v0 >> 32 ), ( u32 )state->v0 );
129 printf( "(%3d) v1 %08x %08x\n", ( int )state->inlen, ( u32 )( state->v1 >> 32 ), ( u32 )state->v1 );
130 printf( "(%3d) v2 %08x %08x\n", ( int )state->inlen, ( u32 )( state->v2 >> 32 ), ( u32 )state->v2 );
131 printf( "(%3d) v3 %08x %08x\n", ( int )state->inlen, ( u32 )( state->v3 >> 32 ), ( u32 )state->v3 );
132 printf( "(%3d) padding %08x %08x\n", ( int )state->inlen, ( u32 )( state->padding >> 32 ), ( u32 )state->padding );
9bf3b535 133#endif
c7b68d84
TG
134 state->v3 ^= b;
135 SIPROUND(state);
136 SIPROUND(state);
137 state->v0 ^= b;
f2936011 138
9bf3b535 139#ifdef DEBUG
f2936011
TG
140 printf( "(%3d) v0 %08x %08x\n", ( int )state->inlen, ( u32 )( state->v0 >> 32 ), ( u32 )state->v0 );
141 printf( "(%3d) v1 %08x %08x\n", ( int )state->inlen, ( u32 )( state->v1 >> 32 ), ( u32 )state->v1 );
142 printf( "(%3d) v2 %08x %08x\n", ( int )state->inlen, ( u32 )( state->v2 >> 32 ), ( u32 )state->v2 );
143 printf( "(%3d) v3 %08x %08x\n", ( int )state->inlen, ( u32 )( state->v3 >> 32 ), ( u32 )state->v3 );
9bf3b535 144#endif
c7b68d84
TG
145 state->v2 ^= 0xff;
146 SIPROUND(state);
147 SIPROUND(state);
148 SIPROUND(state);
149 SIPROUND(state);
150
151 return state->v0 ^ state->v1 ^ state->v2 ^ state->v3;
152}
153
154/* SipHash-2-4 */
155void siphash24(uint8_t out[8], const void *_in, size_t inlen, const uint8_t k[16])
156{
157 struct siphash state;
158 u64 b;
159
160 siphash_init(&state, k);
161
162 siphash24_compress(_in, inlen, &state);
9e77e048
TG
163
164 b = siphash24_finalize(&state);
165
9bf3b535
LP
166 U64TO8_LE( out, b );
167}