Coverage Report

Created: 2026-05-11 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/siphash/siphash.cc
Line
Count
Source
1
// Copyright 2019 The BoringSSL Authors
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//     https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <stdint.h>
16
#include <string.h>
17
18
#include <openssl/siphash.h>
19
20
#include "../internal.h"
21
22
23
using namespace bssl;
24
25
0
static void siphash_round(uint64_t v[4]) {
26
0
  v[0] += v[1];
27
0
  v[2] += v[3];
28
0
  v[1] = CRYPTO_rotl_u64(v[1], 13);
29
0
  v[3] = CRYPTO_rotl_u64(v[3], 16);
30
0
  v[1] ^= v[0];
31
0
  v[3] ^= v[2];
32
0
  v[0] = CRYPTO_rotl_u64(v[0], 32);
33
0
  v[2] += v[1];
34
0
  v[0] += v[3];
35
0
  v[1] = CRYPTO_rotl_u64(v[1], 17);
36
0
  v[3] = CRYPTO_rotl_u64(v[3], 21);
37
0
  v[1] ^= v[2];
38
0
  v[3] ^= v[0];
39
0
  v[2] = CRYPTO_rotl_u64(v[2], 32);
40
0
}
41
42
uint64_t SIPHASH_24(const uint64_t key[2], const uint8_t *input,
43
0
                    size_t input_len) {
44
0
  const size_t orig_input_len = input_len;
45
46
0
  uint64_t v[4];
47
0
  v[0] = key[0] ^ UINT64_C(0x736f6d6570736575);
48
0
  v[1] = key[1] ^ UINT64_C(0x646f72616e646f6d);
49
0
  v[2] = key[0] ^ UINT64_C(0x6c7967656e657261);
50
0
  v[3] = key[1] ^ UINT64_C(0x7465646279746573);
51
52
0
  while (input_len >= sizeof(uint64_t)) {
53
0
    uint64_t m = CRYPTO_load_u64_le(input);
54
0
    v[3] ^= m;
55
0
    siphash_round(v);
56
0
    siphash_round(v);
57
0
    v[0] ^= m;
58
59
0
    input += sizeof(uint64_t);
60
0
    input_len -= sizeof(uint64_t);
61
0
  }
62
63
0
  uint8_t last_block[8];
64
0
  OPENSSL_memset(last_block, 0, sizeof(last_block));
65
0
  OPENSSL_memcpy(last_block, input, input_len);
66
0
  last_block[7] = orig_input_len & 0xff;
67
68
0
  uint64_t last_block_word = CRYPTO_load_u64_le(last_block);
69
0
  v[3] ^= last_block_word;
70
0
  siphash_round(v);
71
0
  siphash_round(v);
72
0
  v[0] ^= last_block_word;
73
74
0
  v[2] ^= 0xff;
75
0
  siphash_round(v);
76
0
  siphash_round(v);
77
0
  siphash_round(v);
78
0
  siphash_round(v);
79
80
0
  return v[0] ^ v[1] ^ v[2] ^ v[3];
81
0
}