]> git.lizzy.rs Git - dragonfireclient.git/blob - src/util/srp.cpp
Fix Wstringop-overflow warning from util/srp.cpp (#6855)
[dragonfireclient.git] / src / util / srp.cpp
1 /*
2  * Secure Remote Password 6a implementation
3  * https://github.com/est31/csrp-gmp
4  *
5  * The MIT License (MIT)
6  *
7  * Copyright (c) 2010, 2013 Tom Cocagne, 2015 est31 <MTest31@outlook.com>
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy of
10  * this software and associated documentation files (the "Software"), to deal in
11  * the Software without restriction, including without limitation the rights to
12  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
13  * of the Software, and to permit persons to whom the Software is furnished to do
14  * so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in all
17  * copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  *
27  */
28
29 // clang-format off
30 #ifdef WIN32
31         #include <windows.h>
32         #include <wincrypt.h>
33 #else
34         #include <ctime>
35
36 #endif
37 // clang-format on
38
39 #include <cstdlib>
40 #include <cstring>
41 #include <cstdio>
42 #include <cstdint>
43
44 #include <config.h>
45
46 #if USE_SYSTEM_GMP || defined (__ANDROID__) || defined (ANDROID)
47         #include <gmp.h>
48 #else
49         #include <mini-gmp.h>
50 #endif
51
52 #include <util/sha2.h>
53
54 #include "srp.h"
55 //#define CSRP_USE_SHA1
56 #define CSRP_USE_SHA256
57
58 #define srp_dbg_data(data, datalen, prevtext) ;
59 /*void srp_dbg_data(unsigned char * data, size_t datalen, char * prevtext)
60 {
61         printf(prevtext);
62         size_t i;
63         for (i = 0; i < datalen; i++)
64         {
65                 printf("%02X", data[i]);
66         }
67         printf("\n");
68 }*/
69
70 static int g_initialized = 0;
71
72 #define RAND_BUFF_MAX 128
73 static unsigned int g_rand_idx;
74 static unsigned char g_rand_buff[RAND_BUFF_MAX];
75
76 void *(*srp_alloc)(size_t) = &malloc;
77 void *(*srp_realloc)(void *, size_t) = &realloc;
78 void (*srp_free)(void *) = &free;
79
80 // clang-format off
81 void srp_set_memory_functions(
82                 void *(*new_srp_alloc)(size_t),
83                 void *(*new_srp_realloc)(void *, size_t),
84                 void (*new_srp_free)(void *))
85 {
86         srp_alloc = new_srp_alloc;
87         srp_realloc = new_srp_realloc;
88         srp_free = new_srp_free;
89 }
90 // clang-format on
91
92 typedef struct {
93         mpz_t N;
94         mpz_t g;
95 } NGConstant;
96
97 struct NGHex {
98         const char *n_hex;
99         const char *g_hex;
100 };
101
102 /* All constants here were pulled from Appendix A of RFC 5054 */
103 static struct NGHex global_Ng_constants[] = {
104         {/* 1024 */
105                 "EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C"
106                 "9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE4"
107                 "8E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B29"
108                 "7BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9A"
109                 "FD5138FE8376435B9FC61D2FC0EB06E3",
110                 "2"},
111         {/* 2048 */
112                 "AC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC319294"
113                 "3DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310D"
114                 "CD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FB"
115                 "D5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF74"
116                 "7359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A"
117                 "436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D"
118                 "5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E73"
119                 "03CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB6"
120                 "94B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F"
121                 "9E4AFF73",
122                 "2"},
123         {/* 4096 */
124                 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
125                 "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
126                 "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9"
127                 "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6"
128                 "49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8"
129                 "FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D"
130                 "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C"
131                 "180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718"
132                 "3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D"
133                 "04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D"
134                 "B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226"
135                 "1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
136                 "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC"
137                 "E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26"
138                 "99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB"
139                 "04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2"
140                 "233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127"
141                 "D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199"
142                 "FFFFFFFFFFFFFFFF",
143                 "5"},
144         {/* 8192 */
145                 "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
146                 "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
147                 "302B0A6DF25F14374FE1356D6D51C245E485B576625E7EC6F44C42E9"
148                 "A637ED6B0BFF5CB6F406B7EDEE386BFB5A899FA5AE9F24117C4B1FE6"
149                 "49286651ECE45B3DC2007CB8A163BF0598DA48361C55D39A69163FA8"
150                 "FD24CF5F83655D23DCA3AD961C62F356208552BB9ED529077096966D"
151                 "670C354E4ABC9804F1746C08CA18217C32905E462E36CE3BE39E772C"
152                 "180E86039B2783A2EC07A28FB5C55DF06F4C52C9DE2BCBF695581718"
153                 "3995497CEA956AE515D2261898FA051015728E5A8AAAC42DAD33170D"
154                 "04507A33A85521ABDF1CBA64ECFB850458DBEF0A8AEA71575D060C7D"
155                 "B3970F85A6E1E4C7ABF5AE8CDB0933D71E8C94E04A25619DCEE3D226"
156                 "1AD2EE6BF12FFA06D98A0864D87602733EC86A64521F2B18177B200C"
157                 "BBE117577A615D6C770988C0BAD946E208E24FA074E5AB3143DB5BFC"
158                 "E0FD108E4B82D120A92108011A723C12A787E6D788719A10BDBA5B26"
159                 "99C327186AF4E23C1A946834B6150BDA2583E9CA2AD44CE8DBBBC2DB"
160                 "04DE8EF92E8EFC141FBECAA6287C59474E6BC05D99B2964FA090C3A2"
161                 "233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127"
162                 "D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934028492"
163                 "36C3FAB4D27C7026C1D4DCB2602646DEC9751E763DBA37BDF8FF9406"
164                 "AD9E530EE5DB382F413001AEB06A53ED9027D831179727B0865A8918"
165                 "DA3EDBEBCF9B14ED44CE6CBACED4BB1BDB7F1447E6CC254B33205151"
166                 "2BD7AF426FB8F401378CD2BF5983CA01C64B92ECF032EA15D1721D03"
167                 "F482D7CE6E74FEF6D55E702F46980C82B5A84031900B1C9E59E7C97F"
168                 "BEC7E8F323A97A7E36CC88BE0F1D45B7FF585AC54BD407B22B4154AA"
169                 "CC8F6D7EBF48E1D814CC5ED20F8037E0A79715EEF29BE32806A1D58B"
170                 "B7C5DA76F550AA3D8A1FBFF0EB19CCB1A313D55CDA56C9EC2EF29632"
171                 "387FE8D76E3C0468043E8F663F4860EE12BF2D5B0B7474D6E694F91E"
172                 "6DBE115974A3926F12FEE5E438777CB6A932DF8CD8BEC4D073B931BA"
173                 "3BC832B68D9DD300741FA7BF8AFC47ED2576F6936BA424663AAB639C"
174                 "5AE4F5683423B4742BF1C978238F16CBE39D652DE3FDB8BEFC848AD9"
175                 "22222E04A4037C0713EB57A81A23F0C73473FC646CEA306B4BCBC886"
176                 "2F8385DDFA9D4B7FA2C087E879683303ED5BDD3A062B3CF5B3A278A6"
177                 "6D2A13F83F44F82DDF310EE074AB6A364597E899A0255DC164F31CC5"
178                 "0846851DF9AB48195DED7EA1B1D510BD7EE74D73FAF36BC31ECFA268"
179                 "359046F4EB879F924009438B481C6CD7889A002ED5EE382BC9190DA6"
180                 "FC026E479558E4475677E9AA9E3050E2765694DFC81F56E880B96E71"
181                 "60C980DD98EDD3DFFFFFFFFFFFFFFFFF",
182                 "13"},
183         {0, 0} /* null sentinel */
184 };
185
186 static void delete_ng(NGConstant *ng)
187 {
188         if (ng) {
189                 mpz_clear(ng->N);
190                 mpz_clear(ng->g);
191                 srp_free(ng);
192         }
193 }
194
195 static NGConstant *new_ng(SRP_NGType ng_type, const char *n_hex, const char *g_hex)
196 {
197         NGConstant *ng = (NGConstant *)srp_alloc(sizeof(NGConstant));
198
199         if (!ng) return 0;
200
201         mpz_init(ng->N);
202         mpz_init(ng->g);
203
204         if (ng_type != SRP_NG_CUSTOM) {
205                 n_hex = global_Ng_constants[ng_type].n_hex;
206                 g_hex = global_Ng_constants[ng_type].g_hex;
207         }
208
209         int rv = 0;
210         rv = mpz_set_str(ng->N, n_hex, 16);
211         rv = rv | mpz_set_str(ng->g, g_hex, 16);
212
213         if (rv) {
214                 delete_ng(ng);
215                 return 0;
216         }
217
218         return ng;
219 }
220
221 typedef union {
222         SHA_CTX sha;
223         SHA256_CTX sha256;
224         // SHA512_CTX sha512;
225 } HashCTX;
226
227 struct SRPVerifier {
228         SRP_HashAlgorithm hash_alg;
229         NGConstant *ng;
230
231         char *username;
232         unsigned char *bytes_B;
233         int authenticated;
234
235         unsigned char M[SHA512_DIGEST_LENGTH];
236         unsigned char H_AMK[SHA512_DIGEST_LENGTH];
237         unsigned char session_key[SHA512_DIGEST_LENGTH];
238 };
239
240 struct SRPUser {
241         SRP_HashAlgorithm hash_alg;
242         NGConstant *ng;
243
244         mpz_t a;
245         mpz_t A;
246         mpz_t S;
247
248         unsigned char *bytes_A;
249         int authenticated;
250
251         char *username;
252         char *username_verifier;
253         unsigned char *password;
254         size_t password_len;
255
256         unsigned char M[SHA512_DIGEST_LENGTH];
257         unsigned char H_AMK[SHA512_DIGEST_LENGTH];
258         unsigned char session_key[SHA512_DIGEST_LENGTH];
259 };
260
261 // clang-format off
262 static int hash_init(SRP_HashAlgorithm alg, HashCTX *c)
263 {
264         switch (alg) {
265 #ifdef CSRP_USE_SHA1
266                 case SRP_SHA1: return SHA1_Init(&c->sha);
267 #endif
268                 /*
269                 case SRP_SHA224: return SHA224_Init(&c->sha256);
270                 */
271 #ifdef CSRP_USE_SHA256
272                 case SRP_SHA256: return SHA256_Init(&c->sha256);
273 #endif
274                 /*
275                 case SRP_SHA384: return SHA384_Init(&c->sha512);
276                 case SRP_SHA512: return SHA512_Init(&c->sha512);
277                 */
278                 default: return -1;
279         };
280 }
281 static int hash_update( SRP_HashAlgorithm alg, HashCTX *c, const void *data, size_t len )
282 {
283         switch (alg) {
284 #ifdef CSRP_USE_SHA1
285                 case SRP_SHA1: return SHA1_Update(&c->sha, data, len);
286 #endif
287                 /*
288                 case SRP_SHA224: return SHA224_Update(&c->sha256, data, len);
289                 */
290 #ifdef CSRP_USE_SHA256
291                 case SRP_SHA256: return SHA256_Update(&c->sha256, data, len);
292 #endif
293                 /*
294                 case SRP_SHA384: return SHA384_Update(&c->sha512, data, len);
295                 case SRP_SHA512: return SHA512_Update(&c->sha512, data, len);
296                 */
297                 default: return -1;
298         };
299 }
300 static int hash_final( SRP_HashAlgorithm alg, HashCTX *c, unsigned char *md )
301 {
302         switch (alg) {
303 #ifdef CSRP_USE_SHA1
304                 case SRP_SHA1: return SHA1_Final(md, &c->sha);
305 #endif
306                 /*
307                 case SRP_SHA224: return SHA224_Final(md, &c->sha256);
308                 */
309 #ifdef CSRP_USE_SHA256
310                 case SRP_SHA256: return SHA256_Final(md, &c->sha256);
311 #endif
312                 /*
313                 case SRP_SHA384: return SHA384_Final(md, &c->sha512);
314                 case SRP_SHA512: return SHA512_Final(md, &c->sha512);
315                 */
316                 default: return -1;
317         };
318 }
319 static unsigned char *hash(SRP_HashAlgorithm alg, const unsigned char *d, size_t n, unsigned char *md)
320 {
321         switch (alg) {
322 #ifdef CSRP_USE_SHA1
323                 case SRP_SHA1: return SHA1(d, n, md);
324 #endif
325                 /*
326                 case SRP_SHA224: return SHA224( d, n, md );
327                 */
328 #ifdef CSRP_USE_SHA256
329                 case SRP_SHA256: return SHA256(d, n, md);
330 #endif
331                 /*
332                 case SRP_SHA384: return SHA384( d, n, md );
333                 case SRP_SHA512: return SHA512( d, n, md );
334                 */
335                 default: return 0;
336         };
337 }
338 static size_t hash_length(SRP_HashAlgorithm alg)
339 {
340         switch (alg) {
341 #ifdef CSRP_USE_SHA1
342                 case SRP_SHA1: return SHA_DIGEST_LENGTH;
343 #endif
344                 /*
345                 case SRP_SHA224: return SHA224_DIGEST_LENGTH;
346                 */
347 #ifdef CSRP_USE_SHA256
348                 case SRP_SHA256: return SHA256_DIGEST_LENGTH;
349 #endif
350                 /*
351                 case SRP_SHA384: return SHA384_DIGEST_LENGTH;
352                 case SRP_SHA512: return SHA512_DIGEST_LENGTH;
353                 */
354                 default: return -1;
355         };
356 }
357 // clang-format on
358
359 inline static int mpz_num_bytes(const mpz_t op)
360 {
361         return (mpz_sizeinbase(op, 2) + 7) / 8;
362 }
363
364 inline static void mpz_to_bin(const mpz_t op, unsigned char *to)
365 {
366         mpz_export(to, NULL, 1, 1, 1, 0, op);
367 }
368
369 inline static void mpz_from_bin(const unsigned char *s, size_t len, mpz_t ret)
370 {
371         mpz_import(ret, len, 1, 1, 1, 0, s);
372 }
373
374 // set op to (op1 * op2) mod d, using tmp for the calculation
375 inline static void mpz_mulm(
376         mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
377 {
378         mpz_mul(tmp, op1, op2);
379         mpz_mod(op, tmp, d);
380 }
381
382 // set op to (op1 + op2) mod d, using tmp for the calculation
383 inline static void mpz_addm(
384         mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
385 {
386         mpz_add(tmp, op1, op2);
387         mpz_mod(op, tmp, d);
388 }
389
390 // set op to (op1 - op2) mod d, using tmp for the calculation
391 inline static void mpz_subm(
392         mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
393 {
394         mpz_sub(tmp, op1, op2);
395         mpz_mod(op, tmp, d);
396 }
397
398 static SRP_Result H_nn(
399         mpz_t result, SRP_HashAlgorithm alg, const mpz_t N, const mpz_t n1, const mpz_t n2)
400 {
401         unsigned char buff[SHA512_DIGEST_LENGTH];
402         size_t len_N = mpz_num_bytes(N);
403         size_t len_n1 = mpz_num_bytes(n1);
404         size_t len_n2 = mpz_num_bytes(n2);
405         size_t nbytes = len_N + len_N;
406         unsigned char *bin = (unsigned char *)srp_alloc(nbytes);
407         if (!bin) return SRP_ERR;
408         if (len_n1 > len_N || len_n2 > len_N) {
409                 srp_free(bin);
410                 return SRP_ERR;
411         }
412         memset(bin, 0, nbytes);
413         mpz_to_bin(n1, bin + (len_N - len_n1));
414         mpz_to_bin(n2, bin + (len_N + len_N - len_n2));
415         hash(alg, bin, nbytes, buff);
416         srp_free(bin);
417         mpz_from_bin(buff, hash_length(alg), result);
418         return SRP_OK;
419 }
420
421 static SRP_Result H_ns(mpz_t result, SRP_HashAlgorithm alg, const unsigned char *n,
422         size_t len_n, const unsigned char *bytes, uint32_t len_bytes)
423 {
424         unsigned char buff[SHA512_DIGEST_LENGTH];
425         size_t nbytes = len_n + len_bytes;
426         unsigned char *bin = (unsigned char *)srp_alloc(nbytes);
427         if (!bin) return SRP_ERR;
428         memcpy(bin, n, len_n);
429         memcpy(bin + len_n, bytes, len_bytes);
430         hash(alg, bin, nbytes, buff);
431         srp_free(bin);
432         mpz_from_bin(buff, hash_length(alg), result);
433         return SRP_OK;
434 }
435
436 static int calculate_x(mpz_t result, SRP_HashAlgorithm alg, const unsigned char *salt,
437         size_t salt_len, const char *username, const unsigned char *password,
438         size_t password_len)
439 {
440         unsigned char ucp_hash[SHA512_DIGEST_LENGTH];
441         HashCTX ctx;
442         hash_init(alg, &ctx);
443
444         srp_dbg_data((char *)username, strlen(username), "Username for x: ");
445         srp_dbg_data((char *)password, password_len, "Password for x: ");
446         hash_update(alg, &ctx, username, strlen(username));
447         hash_update(alg, &ctx, ":", 1);
448         hash_update(alg, &ctx, password, password_len);
449
450         hash_final(alg, &ctx, ucp_hash);
451
452         return H_ns(result, alg, salt, salt_len, ucp_hash, hash_length(alg));
453 }
454
455 static SRP_Result update_hash_n(SRP_HashAlgorithm alg, HashCTX *ctx, const mpz_t n)
456 {
457         size_t len = mpz_num_bytes(n);
458         unsigned char *n_bytes = (unsigned char *)srp_alloc(len);
459         if (!n_bytes) return SRP_ERR;
460         mpz_to_bin(n, n_bytes);
461         hash_update(alg, ctx, n_bytes, len);
462         srp_free(n_bytes);
463         return SRP_OK;
464 }
465
466 static SRP_Result hash_num(SRP_HashAlgorithm alg, const mpz_t n, unsigned char *dest)
467 {
468         int nbytes = mpz_num_bytes(n);
469         unsigned char *bin = (unsigned char *)srp_alloc(nbytes);
470         if (!bin) return SRP_ERR;
471         mpz_to_bin(n, bin);
472         hash(alg, bin, nbytes, dest);
473         srp_free(bin);
474         return SRP_OK;
475 }
476
477 static SRP_Result calculate_M(SRP_HashAlgorithm alg, NGConstant *ng, unsigned char *dest,
478         const char *I, const unsigned char *s_bytes, size_t s_len, const mpz_t A,
479         const mpz_t B, const unsigned char *K)
480 {
481         unsigned char H_N[SHA512_DIGEST_LENGTH];
482         unsigned char H_g[SHA512_DIGEST_LENGTH];
483         unsigned char H_I[SHA512_DIGEST_LENGTH];
484         unsigned char H_xor[SHA512_DIGEST_LENGTH];
485         HashCTX ctx;
486         size_t i = 0;
487         size_t hash_len = hash_length(alg);
488
489         if (!hash_num(alg, ng->N, H_N)) return SRP_ERR;
490         if (!hash_num(alg, ng->g, H_g)) return SRP_ERR;
491
492         hash(alg, (const unsigned char *)I, strlen(I), H_I);
493
494         for (i = 0; i < hash_len; i++)
495                 H_xor[i] = H_N[i] ^ H_g[i];
496
497         hash_init(alg, &ctx);
498
499         hash_update(alg, &ctx, H_xor, hash_len);
500         hash_update(alg, &ctx, H_I, hash_len);
501         hash_update(alg, &ctx, s_bytes, s_len);
502         if (!update_hash_n(alg, &ctx, A)) return SRP_ERR;
503         if (!update_hash_n(alg, &ctx, B)) return SRP_ERR;
504         hash_update(alg, &ctx, K, hash_len);
505
506         hash_final(alg, &ctx, dest);
507         return SRP_OK;
508 }
509
510 static SRP_Result calculate_H_AMK(SRP_HashAlgorithm alg, unsigned char *dest,
511         const mpz_t A, const unsigned char *M, const unsigned char *K)
512 {
513         HashCTX ctx;
514
515         hash_init(alg, &ctx);
516
517         if (!update_hash_n(alg, &ctx, A)) return SRP_ERR;
518         hash_update(alg, &ctx, M, hash_length(alg));
519         hash_update(alg, &ctx, K, hash_length(alg));
520
521         hash_final(alg, &ctx, dest);
522         return SRP_OK;
523 }
524
525 static SRP_Result fill_buff()
526 {
527         g_rand_idx = 0;
528
529 #ifdef WIN32
530         HCRYPTPROV wctx;
531 #else
532         FILE *fp = 0;
533 #endif
534
535 #ifdef WIN32
536
537         if (!CryptAcquireContext(&wctx, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
538                 return SRP_ERR;
539         if (!CryptGenRandom(wctx, sizeof(g_rand_buff), (BYTE *)g_rand_buff)) return SRP_ERR;
540         if (!CryptReleaseContext(wctx, 0)) return SRP_ERR;
541
542 #else
543         fp = fopen("/dev/urandom", "r");
544
545         if (!fp) return SRP_ERR;
546
547         if (fread(g_rand_buff, sizeof(g_rand_buff), 1, fp) != 1) { fclose(fp); return SRP_ERR; }
548         if (fclose(fp)) return SRP_ERR;
549 #endif
550         return SRP_OK;
551 }
552
553 static SRP_Result mpz_fill_random(mpz_t num)
554 {
555         // was call: BN_rand(num, 256, -1, 0);
556         if (RAND_BUFF_MAX - g_rand_idx < 32)
557                 if (fill_buff() != SRP_OK) return SRP_ERR;
558         mpz_from_bin((const unsigned char *)(&g_rand_buff[g_rand_idx]), 32, num);
559         g_rand_idx += 32;
560         return SRP_OK;
561 }
562
563 static SRP_Result init_random()
564 {
565         if (g_initialized) return SRP_OK;
566         SRP_Result ret = fill_buff();
567         g_initialized = (ret == SRP_OK);
568         return ret;
569 }
570
571 #define srp_dbg_num(num, text) ;
572 /*void srp_dbg_num(mpz_t num, char * prevtext)
573 {
574         int len_num = mpz_num_bytes(num);
575         char *bytes_num = (char*) srp_alloc(len_num);
576         mpz_to_bin(num, (unsigned char *) bytes_num);
577         srp_dbg_data(bytes_num, len_num, prevtext);
578         srp_free(bytes_num);
579
580 }*/
581
582 /***********************************************************************************************************
583  *
584  *  Exported Functions
585  *
586  ***********************************************************************************************************/
587
588 // clang-format off
589 SRP_Result srp_create_salted_verification_key( SRP_HashAlgorithm alg,
590         SRP_NGType ng_type, const char *username_for_verifier,
591         const unsigned char *password, size_t len_password,
592         unsigned char **bytes_s,  size_t *len_s,
593         unsigned char **bytes_v, size_t *len_v,
594         const char *n_hex, const char *g_hex )
595 {
596         SRP_Result ret = SRP_OK;
597
598         mpz_t v; mpz_init(v);
599         mpz_t x; mpz_init(x);
600         // clang-format on
601
602         NGConstant *ng = new_ng(ng_type, n_hex, g_hex);
603
604         if (!ng) goto error_and_exit;
605
606         if (init_random() != SRP_OK) /* Only happens once */
607                 goto error_and_exit;
608
609         if (*bytes_s == NULL) {
610                 size_t size_to_fill = 16;
611                 *len_s = size_to_fill;
612                 if (RAND_BUFF_MAX - g_rand_idx < size_to_fill)
613                         if (fill_buff() != SRP_OK) goto error_and_exit;
614                 *bytes_s = (unsigned char *)srp_alloc(size_to_fill);
615                 if (!*bytes_s) goto error_and_exit;
616                 memcpy(*bytes_s, &g_rand_buff + g_rand_idx, size_to_fill);
617                 g_rand_idx += size_to_fill;
618         }
619
620         if (!calculate_x(
621                         x, alg, *bytes_s, *len_s, username_for_verifier, password, len_password))
622                 goto error_and_exit;
623
624         srp_dbg_num(x, "Server calculated x: ");
625
626         mpz_powm(v, ng->g, x, ng->N);
627
628         *len_v = mpz_num_bytes(v);
629
630         *bytes_v = (unsigned char *)srp_alloc(*len_v);
631
632         if (!*bytes_v) goto error_and_exit;
633
634         mpz_to_bin(v, *bytes_v);
635
636 cleanup_and_exit:
637         delete_ng(ng);
638         mpz_clear(v);
639         mpz_clear(x);
640         return ret;
641 error_and_exit:
642         ret = SRP_ERR;
643         goto cleanup_and_exit;
644 }
645
646 // clang-format off
647
648 /* Out: bytes_B, len_B.
649  *
650  * On failure, bytes_B will be set to NULL and len_B will be set to 0
651  */
652 struct SRPVerifier *srp_verifier_new(SRP_HashAlgorithm alg,
653         SRP_NGType ng_type, const char *username,
654         const unsigned char *bytes_s, size_t len_s,
655         const unsigned char *bytes_v, size_t len_v,
656         const unsigned char *bytes_A, size_t len_A,
657         const unsigned char *bytes_b, size_t len_b,
658         unsigned char **bytes_B, size_t *len_B,
659         const char *n_hex, const char *g_hex )
660 {
661         mpz_t v; mpz_init(v); mpz_from_bin(bytes_v, len_v, v);
662         mpz_t A; mpz_init(A); mpz_from_bin(bytes_A, len_A, A);
663         mpz_t u; mpz_init(u);
664         mpz_t B; mpz_init(B);
665         mpz_t S; mpz_init(S);
666         mpz_t b; mpz_init(b);
667         mpz_t k; mpz_init(k);
668         mpz_t tmp1; mpz_init(tmp1);
669         mpz_t tmp2; mpz_init(tmp2);
670         mpz_t tmp3; mpz_init(tmp3);
671         // clang-format on
672         size_t ulen = strlen(username) + 1;
673         NGConstant *ng = new_ng(ng_type, n_hex, g_hex);
674         struct SRPVerifier *ver = 0;
675
676         *len_B = 0;
677         *bytes_B = 0;
678
679         if (!ng) goto cleanup_and_exit;
680
681         ver = (struct SRPVerifier *)srp_alloc(sizeof(struct SRPVerifier));
682
683         if (!ver) goto cleanup_and_exit;
684
685         if (init_random() != SRP_OK) { /* Only happens once */
686                 srp_free(ver);
687                 ver = 0;
688                 goto cleanup_and_exit;
689         }
690
691         ver->username = (char *)srp_alloc(ulen);
692         ver->hash_alg = alg;
693         ver->ng = ng;
694
695         if (!ver->username) {
696                 srp_free(ver);
697                 ver = 0;
698                 goto cleanup_and_exit;
699         }
700
701         memcpy(ver->username, username, ulen);
702
703         ver->authenticated = 0;
704
705         /* SRP-6a safety check */
706         mpz_mod(tmp1, A, ng->N);
707         if (mpz_sgn(tmp1) != 0) {
708                 if (bytes_b) {
709                         mpz_from_bin(bytes_b, len_b, b);
710                 } else {
711                         if (!mpz_fill_random(b)) goto ver_cleanup_and_exit;
712                 }
713
714                 if (!H_nn(k, alg, ng->N, ng->N, ng->g)) goto ver_cleanup_and_exit;
715
716                 /* B = kv + g^b */
717                 mpz_mulm(tmp1, k, v, ng->N, tmp3);
718                 mpz_powm(tmp2, ng->g, b, ng->N);
719                 mpz_addm(B, tmp1, tmp2, ng->N, tmp3);
720
721                 if (!H_nn(u, alg, ng->N, A, B)) goto ver_cleanup_and_exit;
722
723                 srp_dbg_num(u, "Server calculated u: ");
724
725                 /* S = (A *(v^u)) ^ b */
726                 mpz_powm(tmp1, v, u, ng->N);
727                 mpz_mulm(tmp2, A, tmp1, ng->N, tmp3);
728                 mpz_powm(S, tmp2, b, ng->N);
729
730                 if (!hash_num(alg, S, ver->session_key)) goto ver_cleanup_and_exit;
731
732                 if (!calculate_M(
733                                 alg, ng, ver->M, username, bytes_s, len_s, A, B, ver->session_key)) {
734                         goto ver_cleanup_and_exit;
735                 }
736                 if (!calculate_H_AMK(alg, ver->H_AMK, A, ver->M, ver->session_key)) {
737                         goto ver_cleanup_and_exit;
738                 }
739
740                 *len_B = mpz_num_bytes(B);
741                 *bytes_B = (unsigned char *)srp_alloc(*len_B);
742
743                 if (!*bytes_B) {
744                         *len_B = 0;
745                         goto ver_cleanup_and_exit;
746                 }
747
748                 mpz_to_bin(B, *bytes_B);
749
750                 ver->bytes_B = *bytes_B;
751         } else {
752                 srp_free(ver);
753                 ver = 0;
754         }
755
756 cleanup_and_exit:
757         mpz_clear(v);
758         mpz_clear(A);
759         mpz_clear(u);
760         mpz_clear(k);
761         mpz_clear(B);
762         mpz_clear(S);
763         mpz_clear(b);
764         mpz_clear(tmp1);
765         mpz_clear(tmp2);
766         mpz_clear(tmp3);
767         return ver;
768 ver_cleanup_and_exit:
769         srp_free(ver->username);
770         srp_free(ver);
771         ver = 0;
772         goto cleanup_and_exit;
773 }
774
775 void srp_verifier_delete(struct SRPVerifier *ver)
776 {
777         if (ver) {
778                 delete_ng(ver->ng);
779                 srp_free(ver->username);
780                 srp_free(ver->bytes_B);
781                 memset(ver, 0, sizeof(*ver));
782                 srp_free(ver);
783         }
784 }
785
786 int srp_verifier_is_authenticated(struct SRPVerifier *ver)
787 {
788         return ver->authenticated;
789 }
790
791 const char *srp_verifier_get_username(struct SRPVerifier *ver)
792 {
793         return ver->username;
794 }
795
796 const unsigned char *srp_verifier_get_session_key(
797         struct SRPVerifier *ver, size_t *key_length)
798 {
799         if (key_length) *key_length = hash_length(ver->hash_alg);
800         return ver->session_key;
801 }
802
803 size_t srp_verifier_get_session_key_length(struct SRPVerifier *ver)
804 {
805         return hash_length(ver->hash_alg);
806 }
807
808 /* user_M must be exactly SHA512_DIGEST_LENGTH bytes in size */
809 void srp_verifier_verify_session(
810         struct SRPVerifier *ver, const unsigned char *user_M, unsigned char **bytes_HAMK)
811 {
812         if (memcmp(ver->M, user_M, hash_length(ver->hash_alg)) == 0) {
813                 ver->authenticated = 1;
814                 *bytes_HAMK = ver->H_AMK;
815         } else
816                 *bytes_HAMK = NULL;
817 }
818
819 /*******************************************************************************/
820
821 struct SRPUser *srp_user_new(SRP_HashAlgorithm alg, SRP_NGType ng_type,
822         const char *username, const char *username_for_verifier,
823         const unsigned char *bytes_password, size_t len_password, const char *n_hex,
824         const char *g_hex)
825 {
826         struct SRPUser *usr = (struct SRPUser *)srp_alloc(sizeof(struct SRPUser));
827         size_t ulen = strlen(username) + 1;
828         size_t uvlen = strlen(username_for_verifier) + 1;
829
830         if (!usr) goto err_exit;
831
832         if (init_random() != SRP_OK) /* Only happens once */
833                 goto err_exit;
834
835         usr->hash_alg = alg;
836         usr->ng = new_ng(ng_type, n_hex, g_hex);
837
838         mpz_init(usr->a);
839         mpz_init(usr->A);
840         mpz_init(usr->S);
841
842         if (!usr->ng) goto err_exit;
843
844         usr->username = (char *)srp_alloc(ulen);
845         usr->username_verifier = (char *)srp_alloc(uvlen);
846         usr->password = (unsigned char *)srp_alloc(len_password);
847         usr->password_len = len_password;
848
849         if (!usr->username || !usr->password || !usr->username_verifier) goto err_exit;
850
851         memcpy(usr->username, username, ulen);
852         memcpy(usr->username_verifier, username_for_verifier, uvlen);
853         memcpy(usr->password, bytes_password, len_password);
854
855         usr->authenticated = 0;
856
857         usr->bytes_A = 0;
858
859         return usr;
860
861 err_exit:
862         if (usr) {
863                 mpz_clear(usr->a);
864                 mpz_clear(usr->A);
865                 mpz_clear(usr->S);
866                 delete_ng(usr->ng);
867                 srp_free(usr->username);
868                 srp_free(usr->username_verifier);
869                 if (usr->password) {
870                         memset(usr->password, 0, usr->password_len);
871                         srp_free(usr->password);
872                 }
873                 srp_free(usr);
874         }
875
876         return 0;
877 }
878
879 void srp_user_delete(struct SRPUser *usr)
880 {
881         if (usr) {
882                 mpz_clear(usr->a);
883                 mpz_clear(usr->A);
884                 mpz_clear(usr->S);
885
886                 delete_ng(usr->ng);
887
888                 memset(usr->password, 0, usr->password_len);
889
890                 srp_free(usr->username);
891                 srp_free(usr->username_verifier);
892                 srp_free(usr->password);
893
894                 if (usr->bytes_A) srp_free(usr->bytes_A);
895
896                 memset(usr, 0, sizeof(*usr));
897                 srp_free(usr);
898         }
899 }
900
901 int srp_user_is_authenticated(struct SRPUser *usr)
902 {
903         return usr->authenticated;
904 }
905
906 const char *srp_user_get_username(struct SRPUser *usr)
907 {
908         return usr->username;
909 }
910
911 const unsigned char *srp_user_get_session_key(struct SRPUser *usr, size_t *key_length)
912 {
913         if (key_length) *key_length = hash_length(usr->hash_alg);
914         return usr->session_key;
915 }
916
917 size_t srp_user_get_session_key_length(struct SRPUser *usr)
918 {
919         return hash_length(usr->hash_alg);
920 }
921
922 // clang-format off
923 /* Output: username, bytes_A, len_A */
924 SRP_Result srp_user_start_authentication(struct SRPUser *usr, char **username,
925         const unsigned char *bytes_a, size_t len_a,
926         unsigned char **bytes_A, size_t *len_A)
927 {
928         // clang-format on
929         if (bytes_a) {
930                 mpz_from_bin(bytes_a, len_a, usr->a);
931         } else {
932                 if (!mpz_fill_random(usr->a)) goto error_and_exit;
933         }
934
935         mpz_powm(usr->A, usr->ng->g, usr->a, usr->ng->N);
936
937         *len_A = mpz_num_bytes(usr->A);
938         *bytes_A = (unsigned char *)srp_alloc(*len_A);
939
940         if (!*bytes_A) goto error_and_exit;
941
942         mpz_to_bin(usr->A, *bytes_A);
943
944         usr->bytes_A = *bytes_A;
945         if (username) *username = usr->username;
946
947         return SRP_OK;
948
949 error_and_exit:
950         *len_A = 0;
951         *bytes_A = 0;
952         *username = 0;
953         return SRP_ERR;
954 }
955
956 // clang-format off
957 /* Output: bytes_M. Buffer length is SHA512_DIGEST_LENGTH */
958 void  srp_user_process_challenge(struct SRPUser *usr,
959         const unsigned char *bytes_s, size_t len_s,
960         const unsigned char *bytes_B, size_t len_B,
961         unsigned char **bytes_M, size_t *len_M)
962 {
963         mpz_t B; mpz_init(B); mpz_from_bin(bytes_B, len_B, B);
964         mpz_t u; mpz_init(u);
965         mpz_t x; mpz_init(x);
966         mpz_t k; mpz_init(k);
967         mpz_t v; mpz_init(v);
968         mpz_t tmp1; mpz_init(tmp1);
969         mpz_t tmp2; mpz_init(tmp2);
970         mpz_t tmp3; mpz_init(tmp3);
971         mpz_t tmp4; mpz_init(tmp4);
972         // clang-format on
973
974         *len_M = 0;
975         *bytes_M = 0;
976
977         if (!H_nn(u, usr->hash_alg, usr->ng->N, usr->A, B)) goto cleanup_and_exit;
978
979         srp_dbg_num(u, "Client calculated u: ");
980
981         if (!calculate_x(x, usr->hash_alg, bytes_s, len_s, usr->username_verifier,
982                         usr->password, usr->password_len))
983                 goto cleanup_and_exit;
984
985         srp_dbg_num(x, "Client calculated x: ");
986
987         if (!H_nn(k, usr->hash_alg, usr->ng->N, usr->ng->N, usr->ng->g))
988                 goto cleanup_and_exit;
989
990         /* SRP-6a safety check */
991         if (mpz_sgn(B) != 0 && mpz_sgn(u) != 0) {
992                 mpz_powm(v, usr->ng->g, x, usr->ng->N);
993
994                 srp_dbg_num(v, "Client calculated v: ");
995
996                 // clang-format off
997                 /* S = (B - k*(g^x)) ^ (a + ux) */
998                 mpz_mul(tmp1, u, x);
999                 mpz_add(tmp2, usr->a, tmp1);               /* tmp2 = (a + ux)      */
1000                 mpz_powm(tmp1, usr->ng->g, x, usr->ng->N); /* tmp1 = g^x           */
1001                 mpz_mulm(tmp3, k, tmp1, usr->ng->N, tmp4); /* tmp3 = k*(g^x)       */
1002                 mpz_subm(tmp1, B, tmp3, usr->ng->N, tmp4); /* tmp1 = (B - K*(g^x)) */
1003                 mpz_powm(usr->S, tmp1, tmp2, usr->ng->N);
1004                 // clang-format on
1005
1006                 if (!hash_num(usr->hash_alg, usr->S, usr->session_key)) goto cleanup_and_exit;
1007
1008                 if (!calculate_M(usr->hash_alg, usr->ng, usr->M, usr->username, bytes_s, len_s,
1009                                 usr->A, B, usr->session_key))
1010                         goto cleanup_and_exit;
1011                 if (!calculate_H_AMK(usr->hash_alg, usr->H_AMK, usr->A, usr->M, usr->session_key))
1012                         goto cleanup_and_exit;
1013
1014                 *bytes_M = usr->M;
1015                 if (len_M) *len_M = hash_length(usr->hash_alg);
1016         } else {
1017                 *bytes_M = NULL;
1018                 if (len_M) *len_M = 0;
1019         }
1020
1021 cleanup_and_exit:
1022         mpz_clear(B);
1023         mpz_clear(u);
1024         mpz_clear(x);
1025         mpz_clear(k);
1026         mpz_clear(v);
1027         mpz_clear(tmp1);
1028         mpz_clear(tmp2);
1029         mpz_clear(tmp3);
1030         mpz_clear(tmp4);
1031 }
1032
1033 void srp_user_verify_session(struct SRPUser *usr, const unsigned char *bytes_HAMK)
1034 {
1035         if (memcmp(usr->H_AMK, bytes_HAMK, hash_length(usr->hash_alg)) == 0)
1036                 usr->authenticated = 1;
1037 }