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