Debian Codegraph @

src/common/bnetsrp3.cpp

raw symbols SCIP
1 /*
2 * Class that implements the SRP-3 based authentication schema
3 * used by Blizzards WarCraft 3. Implementations is based upon
4 * public information available under
5 * http://www.javaop.com/@ron/documents/SRP.html
6 *
7 * Copyright (C) 2008 - Olaf Freyer
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Affero General Public License
11 * as published by the Free Software Foundation; either version 3
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
18 */
19
20 #include "common/setup_before.h"
21 #include "bnetsrp3.h"
22
23 #include <cassert>
24 #include <cctype>
25 #include <cstdint>
26 #include <cstdio>
27 #include <cstdlib>
28 #include <cstring>
29 #include <iostream>
30 #include <sstream>
31 #include <stdexcept>
32
33 #include "common/bigint.h"
34 #include "common/bnethash.h"
35 #include "common/eventlog.h"
36 #include "common/util.h"
37 #include "common/xalloc.h"
38 #include "common/xstring.h"
39
40 #include "common/setup_after.h"
41
42 namespace pvpgn
43 {
44
45 std::uint8_t bnetsrp3_g = 0x2F;
46
47 const unsigned char bnetsrp3_N[] = {
48 0xF8, 0xFF, 0x1A, 0x8B, 0x61, 0x99, 0x18, 0x03,
49 0x21, 0x86, 0xB6, 0x8C, 0xA0, 0x92, 0xB5, 0x55,
50 0x7E, 0x97, 0x6C, 0x78, 0xC7, 0x32, 0x12, 0xD9,
51 0x12, 0x16, 0xF6, 0x65, 0x85, 0x23, 0xC7, 0x87
52 };
53
54 const unsigned char bnetsrp3_I[] = {
55 0xF8, 0x01, 0x8C, 0xF0, 0xA4, 0x25, 0xBA, 0x8B,
56 0xEB, 0x89, 0x58, 0xB1, 0xAB, 0x6B, 0xF9, 0x0A,
57 0xED, 0x97, 0x0E, 0x6C
58 };
59
60 BigInt BnetSRP3::N = BigInt(bnetsrp3_N, 32);
61 BigInt BnetSRP3::g = BigInt(bnetsrp3_g);
62 BigInt BnetSRP3::I = BigInt(bnetsrp3_I, 32);
63
64 int
65 BnetSRP3::init(const char* username_, const char* password_, BigInt* salt_)
66 {
67 unsigned int i;
68 const char* source;
69 char* symbol;
70
71 if (!username_) {
72 eventlog(eventlog_level_error, __FUNCTION__, "got NULL username_");
73 return -1;
74 }
75
76 username_length = std::strlen(username_);
77 username = (char*)xmalloc(username_length + 1);
78 source = username_;
79 symbol = username;
80 for (i = 0; i < username_length; i++)
81 {
82 *(symbol++) = safe_toupper(*(source++));
83 }
84
85 if (!((password_ == NULL) ^ (salt_ == NULL))) {
86 eventlog(eventlog_level_error, __FUNCTION__, "need to init with EITHER password_ OR salt_");
87 return -1;
88 }
89
90 if (password_ != NULL) {
91 password_length = std::strlen(password_);
92 password = (char*)xmalloc(password_length + 1);
93 source = password_;
94 symbol = password;
95 for (i = 0; i < password_length; i++)
96 {
97 *(symbol++) = safe_toupper(*(source++));
98 }
99 a = BigInt::random(32) % N;
100 s = BigInt::random(32);
101 }
102 else {
103 password = NULL;
104 password_length = 0;
105 b = BigInt::random(32) % N;
106 s = *salt_;
107 }
108
109 B = NULL;
110
111 s.getData(raw_salt, 32);
112
113 return 0;
114 }
115
116
117 BnetSRP3::BnetSRP3(const char* username_, BigInt& salt)
118 {
119 init(username_, NULL, &salt);
120 }
121
122 BnetSRP3::BnetSRP3(const std::string& username_, BigInt& salt)
123 {
124 init(username_.c_str(), NULL, &salt);
125 }
126
127 BnetSRP3::BnetSRP3(const char* username_, const char* password_)
128 {
129 init(username_, password_, NULL);
130 }
131
132 BnetSRP3::BnetSRP3(const std::string& username_, const std::string& password_)
133 {
134 init(username_.c_str(), password_.c_str(), NULL);
135 }
136
137 BnetSRP3::~BnetSRP3()
138 {
139 if (username)
140 xfree(username);
141
142 if (password)
143 xfree(password);
144
145 delete B;
146 }
147
148 BigInt
149 BnetSRP3::getClientPrivateKey() const
150 {
151 char* userpass; // username,':',password
152 t_hash userpass_hash;
153 char private_value[32 + 20]; // s, H(userpass)
154 t_hash private_value_hash;
155
156 userpass = (char*)xmalloc(username_length + 1 + password_length + 1);
157 std::memcpy(userpass, username, username_length);
158 userpass[username_length] = ':';
159 std::memcpy(userpass + username_length + 1, password, password_length);
160 userpass[username_length + 1 + password_length] = '\0';
161 little_endian_sha1_hash(&userpass_hash, username_length + 1 + password_length, userpass);
162 xfree(userpass);
163
164 std::memcpy(&private_value[0], raw_salt, 32);
165 std::memcpy(&private_value[32], userpass_hash, 20);
166 little_endian_sha1_hash(&private_value_hash, 52, private_value);
167
168 return BigInt((unsigned char const*)private_value_hash, 20, 1, false);
169 }
170
171 BigInt
172 BnetSRP3::getScrambler(BigInt& B) const
173 {
174 unsigned char raw_B[32];
175 std::uint32_t scrambler;
176 t_hash hash;
177
178 B.getData(raw_B, 32, 4, false);
179 sha1_hash(&hash, 32, raw_B);
180 scrambler = *(std::uint32_t*)hash;
181
182 return BigInt(scrambler);
183 }
184
185 BigInt
186 BnetSRP3::getClientSecret(BigInt& B) const
187 {
188 BigInt x = getClientPrivateKey();
189 BigInt u = getScrambler(B);
190 return (N + B - g.powm(x, N)).powm((x*u) + a, N);
191 }
192
193 BigInt
194 BnetSRP3::getServerSecret(BigInt& A, BigInt& v)
195 {
196 BigInt B = getServerSessionPublicKey(v);
197 BigInt u = getScrambler(B);
198 return ((A * v.powm(u, N)) % N).powm(b, N);
199 }
200
201 BigInt
202 BnetSRP3::hashSecret(BigInt& secret) const
203 {
204 int i;
205 unsigned char* raw_secret;
206 unsigned char odd[16], even[16], hashedSecret[40];
207 unsigned char* secretPointer;
208 unsigned char* oddPointer;
209 unsigned char* evenPointer;
210 t_hash odd_hash, even_hash;
211
212 raw_secret = secret.getData(32, 4, false);
213 secretPointer = raw_secret;
214 oddPointer = odd;
215 evenPointer = even;
216
217 for (i = 0; i < 16; i++)
218 {
219 *(oddPointer++) = *(secretPointer++);
220 *(evenPointer++) = *(secretPointer++);
221 }
222
223 xfree(raw_secret);
224 little_endian_sha1_hash(&odd_hash, 16, odd);
225 little_endian_sha1_hash(&even_hash, 16, even);
226
227 secretPointer = hashedSecret;
228 oddPointer = (unsigned char*)odd_hash;
229 evenPointer = (unsigned char*)even_hash;
230
231 for (i = 0; i < 20; i++)
232 {
233 *(secretPointer++) = *(oddPointer++);
234 *(secretPointer++) = *(evenPointer++);
235 }
236
237 return BigInt(hashedSecret, 40, 1, false);
238 }
239
240 BigInt
241 BnetSRP3::getVerifier() const
242 {
243 return g.powm(getClientPrivateKey(), N);
244 }
245
246 BigInt
247 BnetSRP3::getSalt() const
248 {
249 return s;
250 }
251
252 void
253 BnetSRP3::setSalt(BigInt salt_)
254 {
255 s = salt_;
256 s.getData(raw_salt, 32);
257 }
258
259 BigInt
260 BnetSRP3::getClientSessionPublicKey() const
261 {
262 return g.powm(a, N);
263 }
264
265 BigInt
266 BnetSRP3::getServerSessionPublicKey(BigInt& v)
267 {
268 if (!B)
269 B = new BigInt((v + g.powm(b, N)) % N);
270
271 return *B;
272 }
273
274 BigInt
275 BnetSRP3::getHashedClientSecret(BigInt& B) const
276 {
277 BigInt clientSecret = getClientSecret(B);
278 return hashSecret(clientSecret);
279 }
280
281 BigInt
282 BnetSRP3::getHashedServerSecret(BigInt& A, BigInt& v)
283 {
284 BigInt serverSecret = getServerSecret(A, v);
285 return hashSecret(serverSecret);
286 }
287
288 BigInt
289 BnetSRP3::getClientPasswordProof(BigInt& A, BigInt& B, BigInt& K) const
290 {
291 unsigned char proofData[176];
292 t_hash usernameHash, proofHash;
293
294 little_endian_sha1_hash(&usernameHash, username_length, username);
295
296 I.getData(&proofData[0], 20, 4, false);
297 std::memcpy(&proofData[20], usernameHash, 20);
298 s.getData(&proofData[40], 32);
299 A.getData(&proofData[72], 32, 4, false);
300 B.getData(&proofData[104], 32, 4, false);
301 K.getData(&proofData[136], 40, 4, false);
302
303 little_endian_sha1_hash(&proofHash, 176, proofData);
304
305 return BigInt((unsigned char*)proofHash, 20, 1, false);
306 }
307
308 BigInt
309 BnetSRP3::getServerPasswordProof(BigInt& A, BigInt& M, BigInt& K) const
310 {
311 unsigned char proofData[92];
312 t_hash proofHash;
313
314 A.getData(&proofData[0], 32, 4, false);
315 M.getData(&proofData[32], 20, 4, false);
316 K.getData(&proofData[52], 40, 4, false);
317
318 little_endian_sha1_hash(&proofHash, 92, proofData);
319
320 return BigInt((unsigned char*)proofHash, 20, 1, false);
321 }
322
323 }