Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Toggle main menu visibility
Loading...
Searching...
No Matches
SRP6.cpp
Go to the documentation of this file.
1
#include "
SRP6.h
"
2
#include "
CryptoRandom.h
"
3
#include "
Util.h
"
4
#include <algorithm>
5
#include <functional>
6
7
using
SHA1
=
SkyFire::Crypto::SHA1
;
8
using
SRP6
=
SkyFire::Crypto::SRP6
;
9
10
/*static*/
std::array<uint8, 1>
const
SRP6::g
= { 7 };
11
/*static*/
std::array<uint8, 32>
const
SRP6::N
=
HexStrToByteArray<32>
(
"894B645E89E1535BBDAD5B8B290650530801B18EBFBF5E8FAB3C82872A3E9BB7"
,
true
);
12
/*static*/
BigNumber
const
SRP6::_g
(
SRP6::g
);
13
/*static*/
BigNumber
const
SRP6::_N
(N);
14
15
/*static*/
std::pair<SRP6::Salt, SRP6::Verifier>
SRP6::MakeRegistrationData
(std::string
const
& username, std::string
const
& password)
16
{
17
std::pair<SRP6::Salt, SRP6::Verifier> res;
18
Crypto::GetRandomBytes
(res.first);
// random salt
19
res.second =
CalculateVerifier
(username, password, res.first);
20
return
res;
21
}
22
23
/*static*/
SRP6::Verifier
SRP6::CalculateVerifier
(std::string
const
& username, std::string
const
& password,
SRP6::Salt
const
& salt)
24
{
25
// v = g ^ H(s || H(u || ':' || p)) mod N
26
return
_g
.ModExp(
27
SHA1::GetDigestOf
(
28
salt,
29
SHA1::GetDigestOf
(username,
":"
, password)
30
)
31
,
_N
).ToByteArray<32>();
32
}
33
34
/*static*/
SessionKey
SRP6::SHA1Interleave
(
SRP6::EphemeralKey
const
& S)
35
{
36
// split S into two buffers
37
std::array<
uint8
,
EPHEMERAL_KEY_LENGTH
/2> buf0, buf1;
38
for
(
size_t
i = 0; i <
EPHEMERAL_KEY_LENGTH
/2; ++i)
39
{
40
buf0[i] = S[2 * i + 0];
41
buf1[i] = S[2 * i + 1];
42
}
43
44
// find position of first nonzero byte
45
size_t
p = 0;
46
while
(p <
EPHEMERAL_KEY_LENGTH
&& !S[p]) ++p;
47
if
(p & 1) ++p;
// skip one extra byte if p is odd
48
p /= 2;
// offset into buffers
49
50
// hash each of the halves, starting at the first nonzero byte
51
SHA1::Digest
const
hash0 =
SHA1::GetDigestOf
(buf0.data() + p,
EPHEMERAL_KEY_LENGTH
/2 - p);
52
SHA1::Digest
const
hash1 =
SHA1::GetDigestOf
(buf1.data() + p,
EPHEMERAL_KEY_LENGTH
/2 - p);
53
54
// stick the two hashes back together
55
SessionKey
K;
56
for
(
size_t
i = 0; i <
SHA1::DIGEST_LENGTH
; ++i)
57
{
58
K[2 * i + 0] = hash0[i];
59
K[2 * i + 1] = hash1[i];
60
}
61
return
K;
62
}
63
64
SRP6::SRP6
(std::string
const
& username,
Salt
const
& salt,
Verifier
const
& verifier)
65
:
_I
(
SHA1
::GetDigestOf(username)),
_b
(
Crypto
::
GetRandomBytes
<32>()),
_v
(verifier),
s
(salt),
B
(
_B
(
_b
,
_v
)) {}
66
67
std::optional<SessionKey>
SRP6::VerifyChallengeResponse
(
EphemeralKey
const
& A,
SHA1::Digest
const
& clientM)
68
{
69
ASSERT
(!
_used
);
70
_used
=
true
;
71
72
BigNumber
const
_A(A);
73
if
((_A %
_N
).isZero())
74
return
std::nullopt;
75
76
BigNumber
const
u(
SHA1::GetDigestOf
(A,
B
));
77
EphemeralKey
const
S = (_A * (
_v
.ModExp(u,
_N
))).ModExp(
_b
,
N
).ToByteArray<32>();
78
79
SessionKey
K =
SHA1Interleave
(S);
80
81
// NgHash = H(N) xor H(g)
82
SHA1::Digest
const
NHash =
SHA1::GetDigestOf
(
N
);
83
SHA1::Digest
const
gHash =
SHA1::GetDigestOf
(
g
);
84
SHA1::Digest
NgHash;
85
std::transform(NHash.begin(), NHash.end(), gHash.begin(), NgHash.begin(), std::bit_xor<>());
86
87
SHA1::Digest
const
ourM =
SHA1::GetDigestOf
(NgHash,
_I
,
s
, A,
B
, K);
88
if
(ourM == clientM)
89
return
K;
90
else
91
return
std::nullopt;
92
}
SessionKey
std::array< uint8, SESSION_KEY_LENGTH > SessionKey
Definition
AuthDefines.h:8
CryptoRandom.h
uint8
std::uint8_t uint8
Definition
Define.h:79
ASSERT
#define ASSERT
Definition
Errors.h:29
SHA1
SkyFire::Crypto::SHA1 SHA1
Definition
SRP6.cpp:7
SRP6
SkyFire::Crypto::SRP6 SRP6
Definition
SRP6.cpp:8
SRP6.h
Util.h
HexStrToByteArray
void HexStrToByteArray(std::string const &str, std::array< uint8, Size > &buf, bool reverse=false)
Definition
Util.h:350
BigNumber
Definition
BigNumber.h:19
SkyFire::Crypto::SRP6
Definition
SRP6.h:15
SkyFire::Crypto::SRP6::_b
BigNumber const _b
Definition
SRP6.h:57
SkyFire::Crypto::SRP6::Salt
std::array< uint8, SALT_LENGTH > Salt
Definition
SRP6.h:18
SkyFire::Crypto::SRP6::_used
bool _used
Definition
SRP6.h:44
SkyFire::Crypto::SRP6::CalculateVerifier
static Verifier CalculateVerifier(std::string const &username, std::string const &password, Salt const &salt)
Definition
SRP6.cpp:23
SkyFire::Crypto::SRP6::VerifyChallengeResponse
std::optional< SessionKey > VerifyChallengeResponse(EphemeralKey const &A, SHA1::Digest const &clientM)
Definition
SRP6.cpp:67
SkyFire::Crypto::SRP6::_I
SHA1::Digest const _I
Definition
SRP6.h:56
SkyFire::Crypto::SRP6::SHA1Interleave
static SessionKey SHA1Interleave(EphemeralKey const &S)
Definition
SRP6.cpp:34
SkyFire::Crypto::SRP6::N
static std::array< uint8, 32 > const N
Definition
SRP6.h:25
SkyFire::Crypto::SRP6::_N
static BigNumber const _N
Definition
SRP6.h:51
SkyFire::Crypto::SRP6::EphemeralKey
std::array< uint8, EPHEMERAL_KEY_LENGTH > EphemeralKey
Definition
SRP6.h:22
SkyFire::Crypto::SRP6::SRP6
SRP6(std::string const &username, Salt const &salt, Verifier const &verifier)
Definition
SRP6.cpp:64
SkyFire::Crypto::SRP6::MakeRegistrationData
static std::pair< Salt, Verifier > MakeRegistrationData(std::string const &username, std::string const &password)
Definition
SRP6.cpp:15
SkyFire::Crypto::SRP6::Verifier
std::array< uint8, VERIFIER_LENGTH > Verifier
Definition
SRP6.h:20
SkyFire::Crypto::SRP6::_v
BigNumber const _v
Definition
SRP6.h:58
SkyFire::Crypto::SRP6::g
static std::array< uint8, 1 > const g
Definition
SRP6.h:24
SkyFire::Crypto::SRP6::_B
static EphemeralKey _B(BigNumber const &b, BigNumber const &v)
Definition
SRP6.h:53
SkyFire::Crypto::SRP6::_g
static BigNumber const _g
Definition
SRP6.h:50
SkyFire::Crypto::SRP6::s
Salt const s
Definition
SRP6.h:61
SkyFire::Crypto::SRP6::B
EphemeralKey const B
Definition
SRP6.h:62
SkyFire::Crypto::SRP6::EPHEMERAL_KEY_LENGTH
static constexpr size_t EPHEMERAL_KEY_LENGTH
Definition
SRP6.h:21
SkyFire::Impl::GenericHash< EVP_sha1, Constants::SHA1_DIGEST_LENGTH_BYTES >::DIGEST_LENGTH
static constexpr size_t DIGEST_LENGTH
Definition
CryptoHash.h:38
SkyFire::Impl::GenericHash< EVP_sha1, Constants::SHA1_DIGEST_LENGTH_BYTES >::Digest
std::array< uint8, DIGEST_LENGTH > Digest
Definition
CryptoHash.h:39
SkyFire::Impl::GenericHash< EVP_sha1, Constants::SHA1_DIGEST_LENGTH_BYTES >::GetDigestOf
static Digest GetDigestOf(uint8 const *data, size_t len)
Definition
CryptoHash.h:41
SkyFire::Crypto
Definition
ARC4.h:15
SkyFire::Crypto::GetRandomBytes
void GetRandomBytes(uint8 *buf, size_t len)
Definition
CryptoRandom.cpp:10
SkyFire::Crypto::SHA1
SkyFire::Impl::GenericHash< EVP_sha1, Constants::SHA1_DIGEST_LENGTH_BYTES > SHA1
Definition
CryptoHash.h:103
SkyFire::Crypto::GetRandomBytes
std::array< uint8, S > GetRandomBytes()
Definition
CryptoRandom.h:23
src
server
shared
Cryptography
Authentication
SRP6.cpp
Generated on
for Project SkyFire Core by
1.17.0