OpenVPN
siphash.h
Go to the documentation of this file.
1/*
2 * SipHash reference C implementation
3 *
4 * Copyright (c) 2012-2021 Jean-Philippe Aumasson
5 * <jeanphilippe.aumasson@gmail.com>
6 * Copyright (c) 2012-2014 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
13 * with
14 * this software. If not, see
15 * <http://creativecommons.org/publicdomain/zero/1.0/>.
16 */
17
18#ifndef SIPHASH_H
19#define SIPHASH_H
20
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
24
25#include <stdint.h>
26#include <stdio.h>
27#include "crypto.h"
28
29/* We need to include this to check for the OPENSSL_IS_AWSLC macro */
30#ifdef ENABLE_CRYPTO_OPENSSL
31#include <openssl/opensslv.h>
32#endif
33
34/* siphash always uses 128-bit keys */
35#define SIPHASH_KEY_SIZE 16
36
40void
41siphash_reference(const void *in, size_t inlen, const void *k,
42 uint8_t *out, size_t outlen);
43
44
45#if defined(OPENSSL_IS_AWSLC)
46#define USE_CRYPOTOLIB_SIPHASH
47#include <openssl/siphash.h>
48#include <string.h>
49#include "error.h"
58static inline void
59siphash_cryptolib(const void *in, const size_t inlen,
60 const void *k, uint8_t *out, const size_t outlen)
61{
62 ASSERT(outlen == sizeof(uint64_t));
63 uint64_t sipout = SIPHASH_24(k, in, inlen);
64
65 memcpy(out, &sipout, sizeof(uint64_t));
66}
67#endif
68
69static inline void
70siphash(const void *in, size_t inlen, const void *k,
71 uint8_t *out, size_t outlen)
72{
73#if defined(USE_CRYPOTOLIB_SIPHASH)
74 siphash_cryptolib(in, inlen, k, out, outlen);
75#else
76 siphash_reference(in, inlen, k, out, outlen);
77#endif
78}
79
84static inline void
89
99uint64_t
100siphash_hash_func(const uint8_t *k, uint32_t length, const uint8_t hash_key[SIPHASH_KEY_SIZE]);
101#endif /* ifndef SIPHASH_H */
void prng_bytes(uint8_t *output, int len)
Definition crypto.c:1729
Data Channel Cryptography Module.
#define ASSERT(x)
Definition error.h:219
#define SIPHASH_KEY_SIZE
Definition siphash.h:35
void siphash_reference(const void *in, size_t inlen, const void *k, uint8_t *out, size_t outlen)
Calculates SIPHASH using the reference implementation.
static void siphash(const void *in, size_t inlen, const void *k, uint8_t *out, size_t outlen)
Definition siphash.h:70
uint64_t siphash_hash_func(const uint8_t *k, uint32_t length, const uint8_t hash_key[SIPHASH_KEY_SIZE])
Wrapper of the siphash function to be able to use it in the hash map.
Definition siphash.c:37
static void siphash_key_init(uint8_t *key)
Initialises a SIPHASH key with a random value.
Definition siphash.h:85
Container for unidirectional cipher and HMAC key material.
Definition crypto.h:152