Botan 3.9.0
Crypto and TLS for C&
siphash.cpp
Go to the documentation of this file.
1/*
2* SipHash
3* (C) 2014,2015 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/internal/siphash.h>
9
10#include <botan/internal/fmt.h>
11#include <botan/internal/loadstor.h>
12#include <botan/internal/rotate.h>
13#include <botan/internal/stl_util.h>
14
15namespace Botan {
16
17namespace {
18
19void SipRounds(uint64_t M, secure_vector<uint64_t>& V, size_t r) {
20 uint64_t V0 = V[0];
21 uint64_t V1 = V[1];
22 uint64_t V2 = V[2];
23 uint64_t V3 = V[3];
24
25 V3 ^= M;
26 for(size_t i = 0; i != r; ++i) {
27 V0 += V1;
28 V2 += V3;
29 V1 = rotl<13>(V1);
30 V3 = rotl<16>(V3);
31 V1 ^= V0;
32 V3 ^= V2;
33 V0 = rotl<32>(V0);
34
35 V2 += V1;
36 V0 += V3;
37 V1 = rotl<17>(V1);
38 V3 = rotl<21>(V3);
39 V1 ^= V2;
40 V3 ^= V0;
41 V2 = rotl<32>(V2);
42 }
43 V0 ^= M;
44
45 V[0] = V0;
46 V[1] = V1;
47 V[2] = V2;
48 V[3] = V3;
49}
50
51} // namespace
52
53void SipHash::add_data(std::span<const uint8_t> input) {
55
56 // SipHash counts the message length mod 256
57 m_words += static_cast<uint8_t>(input.size());
58
59 BufferSlicer in(input);
60
61 if(m_mbuf_pos > 0) {
62 while(!in.empty() && m_mbuf_pos != 8) {
63 m_mbuf = (m_mbuf >> 8) | (static_cast<uint64_t>(in.take_byte()) << 56);
64 ++m_mbuf_pos;
65 }
66
67 if(m_mbuf_pos == 8) {
68 SipRounds(m_mbuf, m_V, m_C);
69 m_mbuf_pos = 0;
70 m_mbuf = 0;
71 }
72 }
73
74 while(in.remaining() >= 8) {
75 SipRounds(load_le<uint64_t>(in.take(8).data(), 0), m_V, m_C);
76 }
77
78 while(!in.empty()) {
79 m_mbuf = (m_mbuf >> 8) | (static_cast<uint64_t>(in.take_byte()) << 56);
80 m_mbuf_pos++;
81 }
82}
83
84void SipHash::final_result(std::span<uint8_t> mac) {
86
87 if(m_mbuf_pos == 0) {
88 m_mbuf = (static_cast<uint64_t>(m_words) << 56);
89 } else if(m_mbuf_pos < 8) {
90 m_mbuf = (m_mbuf >> (64 - m_mbuf_pos * 8)) | (static_cast<uint64_t>(m_words) << 56);
91 }
92
93 SipRounds(m_mbuf, m_V, m_C);
94
95 m_V[2] ^= 0xFF;
96 SipRounds(0, m_V, m_D);
97
98 const uint64_t X = m_V[0] ^ m_V[1] ^ m_V[2] ^ m_V[3];
99
100 store_le(X, mac.data());
101
102 m_V[0] = m_K[0] ^ 0x736F6D6570736575;
103 m_V[1] = m_K[1] ^ 0x646F72616E646F6D;
104 m_V[2] = m_K[0] ^ 0x6C7967656E657261;
105 m_V[3] = m_K[1] ^ 0x7465646279746573;
106 m_mbuf = 0;
107 m_mbuf_pos = 0;
108 m_words = 0;
109}
110
112 return !m_V.empty();
113}
114
115void SipHash::key_schedule(std::span<const uint8_t> key) {
116 const uint64_t K0 = load_le<uint64_t>(key.data(), 0);
117 const uint64_t K1 = load_le<uint64_t>(key.data(), 1);
118
119 m_K.resize(2);
120 m_K[0] = K0;
121 m_K[1] = K1;
122
123 m_V.resize(4);
124 m_V[0] = m_K[0] ^ 0x736F6D6570736575;
125 m_V[1] = m_K[1] ^ 0x646F72616E646F6D;
126 m_V[2] = m_K[0] ^ 0x6C7967656E657261;
127 m_V[3] = m_K[1] ^ 0x7465646279746573;
128}
129
131 zap(m_K);
132 zap(m_V);
133 m_mbuf = 0;
134 m_mbuf_pos = 0;
135 m_words = 0;
136}
137
138std::string SipHash::name() const {
139 return fmt("SipHash({},{})", m_C, m_D);
140}
141
142std::unique_ptr<MessageAuthenticationCode> SipHash::new_object() const {
143 return std::make_unique<SipHash>(m_C, m_D);
144}
145
146} // namespace Botan
void clear() override
Definition siphash.cpp:130
std::string name() const override
Definition siphash.cpp:138
bool has_keying_material() const override
Definition siphash.cpp:111
std::unique_ptr< MessageAuthenticationCode > new_object() const override
Definition siphash.cpp:142
void assert_key_material_set() const
Definition sym_algo.h:146
void zap(std::vector< T, Alloc > &vec)
Definition secmem.h:134
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
constexpr auto store_le(ParamTs &&... params)
Definition loadstor.h:736
BOTAN_FORCE_INLINE constexpr T rotl(T input)
Definition rotate.h:23
constexpr auto load_le(ParamTs &&... params)
Definition loadstor.h:495
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:69