Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
SRP6.h
Go to the documentation of this file.
1#ifndef _SRP6_H
2#define _SRP6_H
3
4#include "AuthDefines.h"
5#include "BigNumber.h"
6#include "Define.h"
7#include "Common.h"
8#include "CryptoHash.h"
9#include <array>
10#include <optional>
11
12namespace SkyFire::Crypto
13{
14 class SRP6
15 {
16 public:
17 static constexpr size_t SALT_LENGTH = 32;
18 using Salt = std::array<uint8, SALT_LENGTH>;
19 static constexpr size_t VERIFIER_LENGTH = 32;
20 using Verifier = std::array<uint8, VERIFIER_LENGTH>;
21 static constexpr size_t EPHEMERAL_KEY_LENGTH = 32;
22 using EphemeralKey = std::array<uint8, EPHEMERAL_KEY_LENGTH>;
23
24 static std::array<uint8, 1> const g;
25 static std::array<uint8, 32> const N;
26
27 // username + password must be passed through Utf8ToUpperOnlyLatin FIRST!
28 static std::pair<Salt, Verifier> MakeRegistrationData(std::string const& username, std::string const& password);
29 // username + password must be passed through Utf8ToUpperOnlyLatin FIRST!
30 static bool CheckLogin(std::string const& username, std::string const& password, Salt const& salt, Verifier const& verifier)
31 {
32 return (verifier == CalculateVerifier(username, password, salt));
33 }
34
35 static SHA1::Digest GetSessionVerifier(EphemeralKey const& A, SHA1::Digest const& clientM, SessionKey const& K)
36 {
37 return SHA1::GetDigestOf(A, clientM, K);
38 }
39
40 SRP6(std::string const& username, Salt const& salt, Verifier const& verifier);
41 std::optional<SessionKey> VerifyChallengeResponse(EphemeralKey const& A, SHA1::Digest const& clientM);
42
43 private:
44 bool _used = false; // a single instance can only be used to verify once
45
46 static Verifier CalculateVerifier(std::string const& username, std::string const& password, Salt const& salt);
47 static SessionKey SHA1Interleave(EphemeralKey const& S);
48
49 /* global algorithm parameters */
50 static BigNumber const _g; // a [g]enerator for the ring of integers mod N, algorithm parameter
51 static BigNumber const _N; // the modulus, an algorithm parameter; all operations are mod this
52
53 static EphemeralKey _B(BigNumber const& b, BigNumber const& v) { return ((_g.ModExp(b,_N) + (v * 3)) % N).ToByteArray<EPHEMERAL_KEY_LENGTH>(); }
54
55 /* per-instantiation parameters, set on construction */
56 SHA1::Digest const _I; // H(I) - the username, all uppercase
57 BigNumber const _b; // b - randomly chosen by the server, 19 bytes, never given out
58 BigNumber const _v; // v - the user's password verifier, derived from s + H(USERNAME || ":" || PASSWORD)
59
60 public:
61 Salt const s; // s - the user's password salt, random, used to calculate v on registration
62 EphemeralKey const B; // B = 3v + g^b
63 };
64}
65
66#endif
std::array< uint8, SESSION_KEY_LENGTH > SessionKey
Definition AuthDefines.h:8
SkyFire::Crypto::SRP6 SRP6
Definition SRP6.cpp:8
BigNumber const _b
Definition SRP6.h:57
std::array< uint8, SALT_LENGTH > Salt
Definition SRP6.h:18
static Verifier CalculateVerifier(std::string const &username, std::string const &password, Salt const &salt)
Definition SRP6.cpp:23
std::optional< SessionKey > VerifyChallengeResponse(EphemeralKey const &A, SHA1::Digest const &clientM)
Definition SRP6.cpp:67
static bool CheckLogin(std::string const &username, std::string const &password, Salt const &salt, Verifier const &verifier)
Definition SRP6.h:30
SHA1::Digest const _I
Definition SRP6.h:56
static SessionKey SHA1Interleave(EphemeralKey const &S)
Definition SRP6.cpp:34
static std::array< uint8, 32 > const N
Definition SRP6.h:25
static BigNumber const _N
Definition SRP6.h:51
std::array< uint8, EPHEMERAL_KEY_LENGTH > EphemeralKey
Definition SRP6.h:22
SRP6(std::string const &username, Salt const &salt, Verifier const &verifier)
Definition SRP6.cpp:64
static std::pair< Salt, Verifier > MakeRegistrationData(std::string const &username, std::string const &password)
Definition SRP6.cpp:15
std::array< uint8, VERIFIER_LENGTH > Verifier
Definition SRP6.h:20
BigNumber const _v
Definition SRP6.h:58
static std::array< uint8, 1 > const g
Definition SRP6.h:24
static constexpr size_t VERIFIER_LENGTH
Definition SRP6.h:19
static EphemeralKey _B(BigNumber const &b, BigNumber const &v)
Definition SRP6.h:53
static BigNumber const _g
Definition SRP6.h:50
Salt const s
Definition SRP6.h:61
static SHA1::Digest GetSessionVerifier(EphemeralKey const &A, SHA1::Digest const &clientM, SessionKey const &K)
Definition SRP6.h:35
EphemeralKey const B
Definition SRP6.h:62
static constexpr size_t EPHEMERAL_KEY_LENGTH
Definition SRP6.h:21
static constexpr size_t SALT_LENGTH
Definition SRP6.h:17