Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/siphash/siphash.c

raw
1 // Copyright (c) 2019, Google Inc.
2 // SPDX-License-Identifier: ISC
3
4 #include <stdint.h>
5 #include <string.h>
6
7 #include <openssl/siphash.h>
8
9 #include "../internal.h"
10
11
12 static void siphash_round(uint64_t v[4]) {
13 v[0] += v[1];
14 v[2] += v[3];
15 v[1] = CRYPTO_rotl_u64(v[1], 13);
16 v[3] = CRYPTO_rotl_u64(v[3], 16);
17 v[1] ^= v[0];
18 v[3] ^= v[2];
19 v[0] = CRYPTO_rotl_u64(v[0], 32);
20 v[2] += v[1];
21 v[0] += v[3];
22 v[1] = CRYPTO_rotl_u64(v[1], 17);
23 v[3] = CRYPTO_rotl_u64(v[3], 21);
24 v[1] ^= v[2];
25 v[3] ^= v[0];
26 v[2] = CRYPTO_rotl_u64(v[2], 32);
27 }
28
29 uint64_t SIPHASH_24(const uint64_t key[2], const uint8_t *input,
30 size_t input_len) {
31 const size_t orig_input_len = input_len;
32
33 uint64_t v[4], k0, k1;
34 #ifdef OPENSSL_BIG_ENDIAN
35 k0 = CRYPTO_bswap8(key[0]);
36 k1 = CRYPTO_bswap8(key[1]);
37 #else
38 k0 = key[0];
39 k1 = key[1];
40 #endif
41 v[0] = k0 ^ UINT64_C(0x736f6d6570736575);
42 v[1] = k1 ^ UINT64_C(0x646f72616e646f6d);
43 v[2] = k0 ^ UINT64_C(0x6c7967656e657261);
44 v[3] = k1 ^ UINT64_C(0x7465646279746573);
45
46 while (input_len >= sizeof(uint64_t)) {
47 uint64_t m = CRYPTO_load_u64_le(input);
48 v[3] ^= m;
49 siphash_round(v);
50 siphash_round(v);
51 v[0] ^= m;
52
53 input += sizeof(uint64_t);
54 input_len -= sizeof(uint64_t);
55 }
56
57 uint8_t last_block[8];
58 OPENSSL_memset(last_block, 0, sizeof(last_block));
59 OPENSSL_memcpy(last_block, input, input_len);
60 last_block[7] = orig_input_len & 0xff;
61
62 uint64_t last_block_word = CRYPTO_load_u64_le(last_block);
63 v[3] ^= last_block_word;
64 siphash_round(v);
65 siphash_round(v);
66 v[0] ^= last_block_word;
67
68 v[2] ^= 0xff;
69 siphash_round(v);
70 siphash_round(v);
71 siphash_round(v);
72 siphash_round(v);
73
74 return v[0] ^ v[1] ^ v[2] ^ v[3];
75 }