1 /* SPDX-License-Identifier: CC0-1.0 */
2
3 #pragma once
4
5 #include <inttypes.h>
6 #include <stddef.h>
7 #include <stdint.h>
8 #include <sys/types.h>
9
10 #include "string-util.h"
11 #include "time-util.h"
12
13 struct siphash {
14 uint64_t v0;
15 uint64_t v1;
16 uint64_t v2;
17 uint64_t v3;
18 uint64_t padding;
19 size_t inlen;
20 };
21
22 void siphash24_init(struct siphash *state, const uint8_t k[static 16]);
23 void siphash24_compress(const void *in, size_t inlen, struct siphash *state);
24 #define siphash24_compress_byte(byte, state) siphash24_compress((const uint8_t[]) { (byte) }, 1, (state))
25
siphash24_compress_boolean(bool in,struct siphash * state)26 static inline void siphash24_compress_boolean(bool in, struct siphash *state) {
27 uint8_t i = in;
28
29 siphash24_compress(&i, sizeof i, state);
30 }
31
siphash24_compress_usec_t(usec_t in,struct siphash * state)32 static inline void siphash24_compress_usec_t(usec_t in, struct siphash *state) {
33 siphash24_compress(&in, sizeof in, state);
34 }
35
siphash24_compress_safe(const void * in,size_t inlen,struct siphash * state)36 static inline void siphash24_compress_safe(const void *in, size_t inlen, struct siphash *state) {
37 if (inlen == 0)
38 return;
39
40 siphash24_compress(in, inlen, state);
41 }
42
siphash24_compress_string(const char * in,struct siphash * state)43 static inline void siphash24_compress_string(const char *in, struct siphash *state) {
44 siphash24_compress_safe(in, strlen_ptr(in), state);
45 }
46
47 uint64_t siphash24_finalize(struct siphash *state);
48
49 uint64_t siphash24(const void *in, size_t inlen, const uint8_t k[static 16]);
50
siphash24_string(const char * s,const uint8_t k[static16])51 static inline uint64_t siphash24_string(const char *s, const uint8_t k[static 16]) {
52 return siphash24(s, strlen(s) + 1, k);
53 }
54