Botan 3.14.1
Crypto and TLS for C&
srp6.cpp
Go to the documentation of this file.
1/*
2* SRP-6a (RFC 5054 compatible)
3* (C) 2011,2012,2019,2020 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#include <botan/srp6.h>
9
10#include <botan/assert.h>
11#include <botan/dl_group.h>
12#include <botan/exceptn.h>
13#include <botan/hash.h>
14#include <botan/internal/ct_utils.h>
15#include <botan/internal/fmt.h>
16#include <botan/internal/mp_core.h>
17
18namespace Botan {
19
20namespace {
21
22BigInt hash_seq(HashFunction& hash_fn, size_t p_bytes, const BigInt& in1, const BigInt& in2) {
23 hash_fn.update(in1.serialize(p_bytes));
24 hash_fn.update(in2.serialize(p_bytes));
25
26 return BigInt::from_bytes(hash_fn.final());
27}
28
29BigInt compute_x(HashFunction& hash_fn,
30 std::string_view identifier,
31 std::string_view password,
32 const std::vector<uint8_t>& salt) {
33 hash_fn.update(identifier);
34 hash_fn.update(":");
35 hash_fn.update(password);
36
37 secure_vector<uint8_t> inner_h = hash_fn.final();
38
39 hash_fn.update(salt);
40 hash_fn.update(inner_h);
41
42 secure_vector<uint8_t> outer_h = hash_fn.final();
43
44 return BigInt::from_bytes(outer_h);
45}
46
47} // namespace
48
49std::string srp6_group_identifier(const BigInt& N, const BigInt& g) {
50 /*
51 This function assumes that only one 'standard' SRP parameter set has
52 been defined for a particular bitsize. As of this writing that is the case.
53 */
54 try {
55 const std::string group_name = "modp/srp/" + std::to_string(N.bits());
56
57 const auto group = DL_Group::from_name(group_name);
58
59 if(group.get_p() == N && group.get_g() == g) {
60 return group_name;
61 }
62 } catch(...) {}
63
64 // If we didn't return, the group was unknown or did not match
65 throw Invalid_Argument("Invalid or unknown SRP group parameters");
66}
67
68std::pair<BigInt, SymmetricKey> srp6_client_agree(std::string_view identifier,
69 std::string_view password,
70 std::string_view group_id,
71 std::string_view hash_id,
72 const std::vector<uint8_t>& salt,
73 const BigInt& B,
75 const auto group = DL_Group::from_name(group_id);
76 const size_t a_bits = group.exponent_bits();
77
78 return srp6_client_agree(identifier, password, group, hash_id, salt, B, a_bits, rng);
79}
80
81std::pair<BigInt, SymmetricKey> srp6_client_agree(std::string_view identifier,
82 std::string_view password,
83 const DL_Group& group,
84 std::string_view hash_id,
85 const std::vector<uint8_t>& salt,
86 const BigInt& B,
87 const size_t a_bits,
89 BOTAN_ARG_CHECK(a_bits <= group.p_bits(), "Invalid a_bits");
90
91 const BigInt& g = group.get_g();
92 const BigInt& p = group.get_p();
93
94 const size_t p_bytes = group.p_bytes();
95
96 if(B <= 0 || B >= p) {
97 throw Decoding_Error("Invalid SRP parameter from server");
98 }
99
100 auto hash_fn = HashFunction::create_or_throw(hash_id);
101 if(8 * hash_fn->output_length() >= group.p_bits()) {
102 throw Invalid_Argument(fmt("Hash function {} too large for SRP6 with this group", hash_fn->name()));
103 }
104
105 const BigInt k = hash_seq(*hash_fn, p_bytes, p, g);
106
107 const BigInt a(rng, a_bits);
108 CT::poison(a);
109
110 const BigInt A = group.power_g_p(a, a_bits);
111 CT::unpoison(A);
112
113 const BigInt u = hash_seq(*hash_fn, p_bytes, A, B);
115
116 const BigInt x = compute_x(*hash_fn, identifier, password, salt);
117 CT::poison(x);
118
119 const BigInt g_x_p = group.power_g_p(x, hash_fn->output_length() * 8);
120
121 const BigInt B_k_g_x_p = group.mod_p(B + group.mod_p(p - group.multiply_mod_p(k, g_x_p)));
122
123 // a + u*x, with lengths that depend only on the sizes of the inputs, not on their values
124 secure_vector<word> a_ux_words(std::max(a.size(), u.size() + x.size()) + 1);
126 a_ux_words.data(), a_ux_words.size(), u._data(), u.size(), u.size(), x._data(), x.size(), x.size(), nullptr, 0);
127 const word carry = bigint_add2(a_ux_words.data(), a_ux_words.size(), a._data(), a.size());
129 const BigInt a_ux = BigInt::_from_words(a_ux_words);
130
131 // a < 2^a_bits and u*x < 2^(2H) where H is the hash output bits, so
132 // a + u*x < 2^(max(a_bits, 2H) + 1).
133 const size_t max_aux_bits = std::max<size_t>(a_bits, 2 * 8 * hash_fn->output_length()) + 1;
134 BOTAN_ASSERT_NOMSG(max_aux_bits >= a_ux.bits());
135
136 const BigInt S = group.power_b_p(B_k_g_x_p, a_ux, max_aux_bits);
137
138 // Any further secret handling of the output is up to the caller
139 CT::unpoison_all(a, x, S);
140
141 const SymmetricKey Sk(S.serialize<secure_vector<uint8_t>>(p_bytes));
142
143 return std::make_pair(A, Sk);
144}
145
146BigInt srp6_generate_verifier(std::string_view identifier,
147 std::string_view password,
148 const std::vector<uint8_t>& salt,
149 std::string_view group_id,
150 std::string_view hash_id) {
151 const auto group = DL_Group::from_name(group_id);
152 return srp6_generate_verifier(identifier, password, salt, group, hash_id);
153}
154
155BigInt srp6_generate_verifier(std::string_view identifier,
156 std::string_view password,
157 const std::vector<uint8_t>& salt,
158 const DL_Group& group,
159 std::string_view hash_id) {
160 auto hash_fn = HashFunction::create_or_throw(hash_id);
161 if(8 * hash_fn->output_length() >= group.p_bits()) {
162 throw Invalid_Argument(fmt("Hash function {} too large for SRP6 with this group", hash_fn->name()));
163 }
164
165 const BigInt x = compute_x(*hash_fn, identifier, password, salt);
166 const auto scope = CT::scoped_poison(x);
167 return CT::driveby_unpoison(group.power_g_p(x, hash_fn->output_length() * 8));
168}
169
171 std::string_view group_id,
172 std::string_view hash_id,
174 const auto group = DL_Group::from_name(group_id);
175 const size_t b_bits = group.exponent_bits();
176 return this->step1(v, group, hash_id, b_bits, rng);
177}
178
180 const BigInt& v, const DL_Group& group, std::string_view hash_id, size_t b_bits, RandomNumberGenerator& rng) {
181 if(v.signum() <= 0 || v >= group.get_p()) {
182 throw Invalid_Argument("SRP6_Server_Session: invalid verifier");
183 }
184
185 BOTAN_ARG_CHECK(b_bits >= 160 && b_bits <= group.p_bits(), "Invalid b_bits");
186
187 BOTAN_STATE_CHECK(!m_group);
188 m_group = std::make_unique<DL_Group>(group);
189 m_b_bits = b_bits;
190
191 const BigInt& g = m_group->get_g();
192 const BigInt& p = m_group->get_p();
193
194 m_v = v;
195 m_b = BigInt(rng, m_b_bits);
196 m_hash_id = hash_id;
197
198 auto hash_fn = HashFunction::create_or_throw(hash_id);
199 if(8 * hash_fn->output_length() >= m_group->p_bits()) {
200 throw Invalid_Argument(fmt("Hash function {} too large for SRP6 with this group", hash_fn->name()));
201 }
202
203 const BigInt k = hash_seq(*hash_fn, m_group->p_bytes(), p, g);
204
205 const auto scope = CT::scoped_poison(m_v, m_b);
206 m_B = m_group->mod_p(m_v * k + m_group->power_g_p(m_b, b_bits));
207 CT::unpoison(m_B);
208
209 return m_B;
210}
211
213 BOTAN_STATE_CHECK(m_group);
214
215 if(A <= 0 || A >= m_group->get_p()) {
216 throw Decoding_Error("Invalid SRP parameter from client");
217 }
218
219 auto hash_fn = HashFunction::create_or_throw(m_hash_id);
220 if(8 * hash_fn->output_length() >= m_group->p_bits()) {
221 throw Invalid_Argument(fmt("Hash function {} too large for SRP6 with this group", hash_fn->name()));
222 }
223
224 const BigInt u = hash_seq(*hash_fn, m_group->p_bytes(), A, m_B);
226
227 const auto scope = CT::scoped_poison(m_v, m_b);
228
229 // The maximum length of u is fixed based on the chosen hash function
230 const size_t u_bits = hash_fn->output_length() * 8;
231 const BigInt vup = m_group->power_b_p(m_v, u, u_bits);
232
233 // The size of b is chosen by either a formula or the application; either way it is public
234 const BigInt S = m_group->power_b_p(m_group->multiply_mod_p(A, vup), m_b, m_b_bits);
235
236 // Any further secret handling of the output is up to the caller
237 CT::unpoison(S);
238
239 return SymmetricKey(S.serialize<secure_vector<uint8_t>>(m_group->p_bytes()));
240}
241
242} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:49
#define BOTAN_ARG_CHECK(expr, msg)
Definition assert.h:33
size_t size() const
Definition bigint.h:681
static BigInt _from_words(secure_vector< word > &words)
Definition bigint.h:1052
int signum() const
Definition bigint.h:493
static BigInt from_bytes(std::span< const uint8_t > bytes)
Definition bigint.cpp:84
size_t bits() const
Definition bigint.cpp:308
const word * _data() const
Definition bigint.h:1033
bool is_zero() const
Definition bigint.h:510
T serialize(size_t len) const
Definition bigint.h:790
BigInt power_g_p(const BigInt &x) const
Definition dl_group.h:266
BigInt mod_p(const BigInt &x) const
Definition dl_group.cpp:580
BigInt multiply_mod_p(const BigInt &x, const BigInt &y) const
Definition dl_group.cpp:584
size_t p_bits() const
Definition dl_group.cpp:549
const BigInt & get_p() const
Definition dl_group.cpp:523
static DL_Group from_name(std::string_view name)
Definition dl_group.cpp:266
size_t p_bytes() const
Definition dl_group.cpp:553
BigInt power_b_p(const BigInt &b, const BigInt &x, size_t max_x_bits) const
Definition dl_group.cpp:632
const BigInt & get_g() const
Definition dl_group.cpp:530
static std::unique_ptr< HashFunction > create_or_throw(std::string_view algo_spec, std::string_view provider="")
Definition hash.cpp:311
BigInt step1(const BigInt &v, std::string_view group_id, std::string_view hash_id, RandomNumberGenerator &rng)
Definition srp6.cpp:170
SymmetricKey step2(const BigInt &A)
Definition srp6.cpp:212
constexpr decltype(auto) driveby_unpoison(T &&v)
Definition ct_utils.h:243
constexpr auto scoped_poison(const Ts &... xs)
Definition ct_utils.h:222
constexpr void unpoison_all(const Ts &... ts)
Definition ct_utils.h:207
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:67
constexpr void poison(const T *p, size_t n)
Definition ct_utils.h:56
constexpr auto bigint_add2(W x[], size_t x_size, const W y[], size_t y_size) -> W
Definition mp_core.h:98
OctetString SymmetricKey
Definition symkey.h:153
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
void bigint_mul(word z[], size_t z_size, const word x[], size_t x_size, size_t x_sw, const word y[], size_t y_size, size_t y_sw, word workspace[], size_t ws_size)
Definition mp_karat.cpp:283
std::pair< BigInt, SymmetricKey > srp6_client_agree(std::string_view identifier, std::string_view password, std::string_view group_id, std::string_view hash_id, const std::vector< uint8_t > &salt, const BigInt &B, RandomNumberGenerator &rng)
Definition srp6.cpp:68
BigInt srp6_generate_verifier(std::string_view identifier, std::string_view password, const std::vector< uint8_t > &salt, std::string_view group_id, std::string_view hash_id)
Definition srp6.cpp:146
std::string srp6_group_identifier(const BigInt &N, const BigInt &g)
Definition srp6.cpp:49
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
std::conditional_t< HasNative64BitRegisters, std::uint64_t, uint32_t > word
The native machine word, used as the limb type for multiprecision integers.
Definition types.h:131