2 * Secure Remote Password 6a implementation
3 * https://github.com/est31/csrp-gmp
5 * The MIT License (MIT)
7 * Copyright (c) 2010, 2013 Tom Cocagne, 2015 est31 <MTest31@outlook.com>
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:
16 * The above copyright notice and this permission notice shall be included in all
17 * copies or substantial portions of the Software.
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
42 #if USE_SYSTEM_GMP || defined (__ANDROID__) || defined (ANDROID)
45 #include <gmp/mini-gmp.h>
48 #include <util/sha2.h>
51 //#define CSRP_USE_SHA1
52 #define CSRP_USE_SHA256
54 #define srp_dbg_data(data, datalen, prevtext) ;
55 /*void srp_dbg_data(unsigned char * data, size_t datalen, char * prevtext)
59 for (i = 0; i < datalen; i++)
61 printf("%02X", data[i]);
66 static int g_initialized = 0;
68 #define RAND_BUFF_MAX 128
69 static unsigned int g_rand_idx;
70 static unsigned char g_rand_buff[RAND_BUFF_MAX];
84 /* All constants here were pulled from Appendix A of RFC 5054 */
85 static struct NGHex global_Ng_constants[] = {
87 "EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C9C256576D674DF7496"
88 "EA81D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8E"
89 "F4AD69B15D4982559B297BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA"
90 "9AFD5138FE8376435B9FC61D2FC0EB06E3",
94 "AC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4"
95 "A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF60"
96 "95179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF"
97 "747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B907"
98 "8717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB37861"
99 "60279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DB"
100 "FBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73",
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"
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",
165 {0,0} /* null sentinel */
169 static NGConstant *new_ng( SRP_NGType ng_type, const char *n_hex, const char *g_hex )
171 NGConstant *ng = (NGConstant *) malloc(sizeof(NGConstant));
178 if (ng_type != SRP_NG_CUSTOM) {
179 n_hex = global_Ng_constants[ ng_type ].n_hex;
180 g_hex = global_Ng_constants[ ng_type ].g_hex;
183 mpz_set_str(ng->N, n_hex, 16);
184 mpz_set_str(ng->g, g_hex, 16);
189 static void delete_ng( NGConstant *ng )
210 SRP_HashAlgorithm hash_alg;
214 unsigned char *bytes_B;
217 unsigned char M[SHA512_DIGEST_LENGTH];
218 unsigned char H_AMK[SHA512_DIGEST_LENGTH];
219 unsigned char session_key[SHA512_DIGEST_LENGTH];
225 SRP_HashAlgorithm hash_alg;
232 unsigned char *bytes_A;
236 char *username_verifier;
237 unsigned char *password;
240 unsigned char M[SHA512_DIGEST_LENGTH];
241 unsigned char H_AMK[SHA512_DIGEST_LENGTH];
242 unsigned char session_key[SHA512_DIGEST_LENGTH];
246 static int hash_init(SRP_HashAlgorithm alg, HashCTX *c)
250 case SRP_SHA1: return SHA1_Init(&c->sha);
252 /*case SRP_SHA224: return SHA224_Init(&c->sha256);*/
253 #ifdef CSRP_USE_SHA256
254 case SRP_SHA256: return SHA256_Init(&c->sha256);
256 /*case SRP_SHA384: return SHA384_Init(&c->sha512);
257 case SRP_SHA512: return SHA512_Init(&c->sha512);*/
261 static int hash_update( SRP_HashAlgorithm alg, HashCTX *c, const void *data, size_t len )
265 case SRP_SHA1: return SHA1_Update(&c->sha, data, len);
267 /*case SRP_SHA224: return SHA224_Update(&c->sha256, data, len);*/
268 #ifdef CSRP_USE_SHA256
269 case SRP_SHA256: return SHA256_Update(&c->sha256, data, len);
271 /*case SRP_SHA384: return SHA384_Update( &c->sha512, data, len );
272 case SRP_SHA512: return SHA512_Update( &c->sha512, data, len );*/
276 static int hash_final( SRP_HashAlgorithm alg, HashCTX *c, unsigned char *md )
280 case SRP_SHA1: return SHA1_Final(md, &c->sha);
282 /*case SRP_SHA224: return SHA224_Final(md, &c->sha256);*/
283 #ifdef CSRP_USE_SHA256
284 case SRP_SHA256: return SHA256_Final(md, &c->sha256);
286 /*case SRP_SHA384: return SHA384_Final(md, &c->sha512);
287 case SRP_SHA512: return SHA512_Final(md, &c->sha512);*/
291 static unsigned char *hash(SRP_HashAlgorithm alg, const unsigned char *d, size_t n, unsigned char *md)
295 case SRP_SHA1: return SHA1(d, n, md);
297 /*case SRP_SHA224: return SHA224( d, n, md );*/
298 #ifdef CSRP_USE_SHA256
299 case SRP_SHA256: return SHA256(d, n, md);
301 /*case SRP_SHA384: return SHA384( d, n, md );
302 case SRP_SHA512: return SHA512( d, n, md );*/
306 static size_t hash_length(SRP_HashAlgorithm alg)
310 case SRP_SHA1: return SHA_DIGEST_LENGTH;
312 /*case SRP_SHA224: return SHA224_DIGEST_LENGTH;*/
313 #ifdef CSRP_USE_SHA256
314 case SRP_SHA256: return SHA256_DIGEST_LENGTH;
316 /*case SRP_SHA384: return SHA384_DIGEST_LENGTH;
317 case SRP_SHA512: return SHA512_DIGEST_LENGTH;*/
322 inline static int mpz_num_bytes(const mpz_t op)
324 return (mpz_sizeinbase (op, 2) + 7) / 8;
327 inline static void mpz_to_bin(const mpz_t op, unsigned char *to)
329 mpz_export(to, NULL, 1, 1, 1, 0, op);
332 inline static void mpz_from_bin(const unsigned char *s, size_t len, mpz_t ret)
334 mpz_import(ret, len, 1, 1, 1, 0, s);
337 // set op to (op1 * op2) mod d, using tmp for the calculation
338 inline static void mpz_mulm(mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
340 mpz_mul(tmp, op1, op2);
344 // set op to (op1 + op2) mod d, using tmp for the calculation
345 inline static void mpz_addm( mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp )
347 mpz_add(tmp, op1, op2);
351 // set op to (op1 - op2) mod d, using tmp for the calculation
352 inline static void mpz_subm(mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
354 mpz_sub(tmp, op1, op2);
358 static int H_nn(mpz_t result, SRP_HashAlgorithm alg, const mpz_t N, const mpz_t n1, const mpz_t n2)
360 unsigned char buff[SHA512_DIGEST_LENGTH];
361 size_t len_N = mpz_num_bytes(N);
362 size_t len_n1 = mpz_num_bytes(n1);
363 size_t len_n2 = mpz_num_bytes(n2);
364 size_t nbytes = len_N + len_N;
365 unsigned char *bin = (unsigned char *) malloc(nbytes);
368 if (len_n1 > len_N || len_n2 > len_N) {
372 memset(bin, 0, nbytes);
373 mpz_to_bin(n1, bin + (len_N - len_n1));
374 mpz_to_bin(n2, bin + (len_N + len_N - len_n2));
375 hash( alg, bin, nbytes, buff );
377 mpz_from_bin(buff, hash_length(alg), result);
381 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)
383 unsigned char buff[SHA512_DIGEST_LENGTH];
384 size_t nbytes = len_n + len_bytes;
385 unsigned char *bin = (unsigned char *) malloc(nbytes);
388 memcpy(bin, n, len_n);
389 memcpy(bin + len_n, bytes, len_bytes);
390 hash(alg, bin, nbytes, buff);
392 mpz_from_bin(buff, hash_length(alg), result);
396 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)
398 unsigned char ucp_hash[SHA512_DIGEST_LENGTH];
400 hash_init(alg, &ctx);
402 srp_dbg_data((char*) username, strlen(username), "Username for x: ");
403 srp_dbg_data((char*) password, password_len, "Password for x: ");
404 hash_update(alg, &ctx, username, strlen(username));
405 hash_update(alg, &ctx, ":", 1);
406 hash_update(alg, &ctx, password, password_len);
408 hash_final(alg, &ctx, ucp_hash);
410 return H_ns(result, alg, salt, salt_len, ucp_hash, hash_length(alg));
413 static void update_hash_n(SRP_HashAlgorithm alg, HashCTX *ctx, const mpz_t n)
415 size_t len = mpz_num_bytes(n);
416 unsigned char* n_bytes = (unsigned char *) malloc(len);
419 mpz_to_bin(n, n_bytes);
420 hash_update(alg, ctx, n_bytes, len);
424 static void hash_num( SRP_HashAlgorithm alg, const mpz_t n, unsigned char *dest )
426 int nbytes = mpz_num_bytes(n);
427 unsigned char *bin = (unsigned char *) malloc(nbytes);
431 hash(alg, bin, nbytes, dest);
435 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)
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];
445 size_t hash_len = hash_length(alg);
447 hash_num(alg, ng->N, H_N);
448 hash_num(alg, ng->g, H_g);
450 hash(alg, (const unsigned char *)I, strlen(I), H_I);
453 for (i = 0; i < hash_len; i++ )
454 H_xor[i] = H_N[i] ^ H_g[i];
456 hash_init(alg, &ctx);
458 hash_update(alg, &ctx, H_xor, hash_len);
459 hash_update(alg, &ctx, H_I, hash_len);
460 hash_update(alg, &ctx, s_bytes, s_len);
461 update_hash_n(alg, &ctx, A);
462 update_hash_n(alg, &ctx, B);
463 hash_update(alg, &ctx, K, hash_len);
465 hash_final(alg, &ctx, dest);
468 static void calculate_H_AMK(SRP_HashAlgorithm alg, unsigned char *dest, const mpz_t A, const unsigned char *M, const unsigned char *K)
472 hash_init(alg, &ctx);
474 update_hash_n(alg, &ctx, A);
475 hash_update(alg, &ctx, M, hash_length(alg));
476 hash_update(alg, &ctx, K, hash_length(alg));
478 hash_final(alg, &ctx, dest);
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;
487 static unsigned long int srp_pcgrandom_next(srp_pcgrandom *r)
489 unsigned long long int oldstate = r->m_state;
490 r->m_state = oldstate * 6364136223846793005ULL + r->m_inc;
492 unsigned long int xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u;
493 unsigned long int rot = oldstate >> 59u;
494 return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
497 static void srp_pcgrandom_seed(srp_pcgrandom *r, unsigned long long int state,
498 unsigned long long int seq)
501 r->m_inc = (seq << 1u) | 1u;
502 srp_pcgrandom_next(r);
504 srp_pcgrandom_next(r);
508 static int fill_buff()
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);
527 fp = fopen("/dev/urandom", "r");
530 fread(g_rand_buff, sizeof(g_rand_buff), 1, fp);
533 srp_pcgrandom *r = (srp_pcgrandom *) malloc(sizeof(srp_pcgrandom));
534 srp_pcgrandom_seed(r, time(NULL) ^ clock(), 0xda3e39cb94b95bdbULL);
536 for (i = 0; i < RAND_BUFF_MAX; i++) {
537 g_rand_buff[i] = srp_pcgrandom_next(r);
544 static void mpz_fill_random(mpz_t num)
546 // was call: BN_rand(num, 256, -1, 0);
547 if (RAND_BUFF_MAX - g_rand_idx < 32)
549 mpz_from_bin((const unsigned char *) (&g_rand_buff[g_rand_idx]), 32, num);
553 static void init_random()
557 g_initialized = fill_buff();
560 #define srp_dbg_num(num, text) ;
561 /*void srp_dbg_num(mpz_t num, char * prevtext)
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);
571 /***********************************************************************************************************
575 ***********************************************************************************************************/
577 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 )
584 mpz_t v; mpz_init(v);
585 mpz_t x; mpz_init(x);
586 NGConstant *ng = new_ng(ng_type, n_hex, g_hex);
589 goto cleanup_and_exit;
591 if (*bytes_s == NULL) {
593 if (RAND_BUFF_MAX - g_rand_idx < 16)
595 *bytes_s = (unsigned char*)malloc(sizeof(char) * 16);
596 memcpy(*bytes_s, &g_rand_buff + g_rand_idx, sizeof(char) * 16);
601 if (!calculate_x(x, alg, *bytes_s, *len_s, username_for_verifier,
602 password, len_password))
603 goto cleanup_and_exit;
605 srp_dbg_num(x, "Server calculated x: ");
607 mpz_powm(v, ng->g, x, ng->N);
609 *len_v = mpz_num_bytes(v);
611 *bytes_v = (unsigned char*)malloc(*len_v);
614 goto cleanup_and_exit;
616 mpz_to_bin(v, *bytes_v);
626 /* Out: bytes_B, len_B.
628 * On failure, bytes_B will be set to NULL and len_B will be set to 0
630 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 )
639 mpz_t v; mpz_init(v); mpz_from_bin(bytes_v, len_v, v);
640 mpz_t A; mpz_init(A); mpz_from_bin(bytes_A, len_A, A);
641 mpz_t u; mpz_init(u);
642 mpz_t B; mpz_init(B);
643 mpz_t S; mpz_init(S);
644 mpz_t b; mpz_init(b);
645 mpz_t k; mpz_init(k);
646 mpz_t tmp1; mpz_init(tmp1);
647 mpz_t tmp2; mpz_init(tmp2);
648 mpz_t tmp3; mpz_init(tmp3);
649 size_t ulen = strlen(username) + 1;
650 NGConstant *ng = new_ng(ng_type, n_hex, g_hex);
651 struct SRPVerifier *ver = 0;
657 goto cleanup_and_exit;
659 ver = (struct SRPVerifier *) malloc( sizeof(struct SRPVerifier) );
662 goto cleanup_and_exit;
664 init_random(); /* Only happens once */
666 ver->username = (char *) malloc(ulen);
670 if (!ver->username) {
673 goto cleanup_and_exit;
676 memcpy((char*)ver->username, username, ulen);
678 ver->authenticated = 0;
680 /* SRP-6a safety check */
681 mpz_mod(tmp1, A, ng->N);
682 if (mpz_sgn(tmp1) != 0) {
684 mpz_from_bin(bytes_b, len_b, b);
689 if (!H_nn(k, alg, ng->N, ng->N, ng->g)) {
692 goto cleanup_and_exit;
696 mpz_mulm(tmp1, k, v, ng->N, tmp3);
697 mpz_powm(tmp2, ng->g, b, ng->N);
698 mpz_addm(B, tmp1, tmp2, ng->N, tmp3);
700 if (!H_nn(u, alg, ng->N, A, B)) {
703 goto cleanup_and_exit;
706 srp_dbg_num(u, "Server calculated u: ");
708 /* S = (A *(v^u)) ^ b */
709 mpz_powm(tmp1, v, u, ng->N);
710 mpz_mulm(tmp2, A, tmp1, ng->N, tmp3);
711 mpz_powm(S, tmp2, b, ng->N);
713 hash_num(alg, S, ver->session_key);
715 calculate_M(alg, ng, ver->M, username, bytes_s, len_s, A, B, ver->session_key);
716 calculate_H_AMK(alg, ver->H_AMK, A, ver->M, ver->session_key);
718 *len_B = mpz_num_bytes(B);
719 *bytes_B = (unsigned char*)malloc(*len_B);
726 goto cleanup_and_exit;
729 mpz_to_bin(B, *bytes_B);
731 ver->bytes_B = *bytes_B;
754 void srp_verifier_delete(struct SRPVerifier *ver)
760 memset(ver, 0, sizeof(*ver));
767 int srp_verifier_is_authenticated(struct SRPVerifier *ver)
769 return ver->authenticated;
773 const char *srp_verifier_get_username(struct SRPVerifier *ver)
775 return ver->username;
779 const unsigned char *srp_verifier_get_session_key(struct SRPVerifier *ver, size_t *key_length)
782 *key_length = hash_length(ver->hash_alg);
783 return ver->session_key;
787 size_t srp_verifier_get_session_key_length(struct SRPVerifier *ver)
789 return hash_length(ver->hash_alg);
793 /* user_M must be exactly SHA512_DIGEST_LENGTH bytes in size */
794 void srp_verifier_verify_session(struct SRPVerifier *ver, const unsigned char *user_M, unsigned char **bytes_HAMK)
796 if (memcmp(ver->M, user_M, hash_length(ver->hash_alg)) == 0) {
797 ver->authenticated = 1;
798 *bytes_HAMK = ver->H_AMK;
803 /*******************************************************************************/
805 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)
810 struct SRPUser *usr = (struct SRPUser *) malloc(sizeof(struct SRPUser));
811 size_t ulen = strlen(username) + 1;
812 size_t uvlen = strlen(username_for_verifier) + 1;
817 init_random(); /* Only happens once */
820 usr->ng = new_ng(ng_type, n_hex, g_hex);
829 usr->username = (char*)malloc(ulen);
830 usr->username_verifier = (char*)malloc(uvlen);
831 usr->password = (unsigned char*)malloc(len_password);
832 usr->password_len = len_password;
834 if (!usr->username || !usr->password)
837 memcpy(usr->username, username, ulen);
838 memcpy(usr->username_verifier, username_for_verifier, uvlen);
839 memcpy(usr->password, bytes_password, len_password);
841 usr->authenticated = 0;
854 if (usr->username_verifier)
855 free(usr->username_verifier);
857 memset(usr->password, 0, usr->password_len);
868 void srp_user_delete(struct SRPUser *usr)
877 memset(usr->password, 0, usr->password_len);
880 free(usr->username_verifier);
886 memset(usr, 0, sizeof(*usr));
893 int srp_user_is_authenticated(struct SRPUser *usr)
895 return usr->authenticated;
899 const char *srp_user_get_username(struct SRPUser *usr)
901 return usr->username;
905 const unsigned char* srp_user_get_session_key(struct SRPUser* usr, size_t* key_length)
908 *key_length = hash_length(usr->hash_alg);
909 return usr->session_key;
913 size_t srp_user_get_session_key_length(struct SRPUser *usr)
915 return hash_length(usr->hash_alg);
919 /* Output: username, bytes_A, len_A */
920 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)
925 mpz_from_bin(bytes_a, len_a, usr->a);
927 mpz_fill_random(usr->a);
930 mpz_powm(usr->A, usr->ng->g, usr->a, usr->ng->N);
932 *len_A = mpz_num_bytes(usr->A);
933 *bytes_A = (unsigned char*)malloc(*len_A);
942 mpz_to_bin(usr->A, *bytes_A);
944 usr->bytes_A = *bytes_A;
946 *username = usr->username;
950 /* Output: bytes_M. Buffer length is SHA512_DIGEST_LENGTH */
951 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)
956 mpz_t B; mpz_init(B); mpz_from_bin(bytes_B, len_B, B);
957 mpz_t u; mpz_init(u);
958 mpz_t x; mpz_init(x);
959 mpz_t k; mpz_init(k);
960 mpz_t v; mpz_init(v);
961 mpz_t tmp1; mpz_init(tmp1);
962 mpz_t tmp2; mpz_init(tmp2);
963 mpz_t tmp3; mpz_init(tmp3);
964 mpz_t tmp4; mpz_init(tmp4);
969 if (!H_nn(u, usr->hash_alg, usr->ng->N, usr->A, B))
970 goto cleanup_and_exit;
972 srp_dbg_num(u, "Client calculated u: ");
974 if (!calculate_x(x, usr->hash_alg, bytes_s, len_s,
975 usr->username_verifier, usr->password, usr->password_len))
976 goto cleanup_and_exit;
978 srp_dbg_num(x, "Client calculated x: ");
980 if (!H_nn(k, usr->hash_alg, usr->ng->N, usr->ng->N, usr->ng->g))
981 goto cleanup_and_exit;
983 /* SRP-6a safety check */
984 if ( mpz_sgn(B) != 0 && mpz_sgn(u) != 0 ) {
985 mpz_powm(v, usr->ng->g, x, usr->ng->N);
987 srp_dbg_num(v, "Client calculated v: ");
989 /* S = (B - k*(g^x)) ^ (a + ux) */
991 mpz_add(tmp2, usr->a, tmp1); /* tmp2 = (a + ux) */
992 mpz_powm(tmp1, usr->ng->g, x, usr->ng->N); /* tmp1 = g^x */
993 mpz_mulm(tmp3, k, tmp1, usr->ng->N, tmp4); /* tmp3 = k*(g^x) */
994 mpz_subm(tmp1, B, tmp3, usr->ng->N, tmp4); /* tmp1 = (B - K*(g^x)) */
995 mpz_powm(usr->S, tmp1, tmp2, usr->ng->N);
997 hash_num(usr->hash_alg, usr->S, usr->session_key);
999 calculate_M( usr->hash_alg, usr->ng, usr->M, usr->username, bytes_s, len_s, usr->A,B, usr->session_key );
1000 calculate_H_AMK( usr->hash_alg, usr->H_AMK, usr->A, usr->M, usr->session_key );
1004 *len_M = hash_length( usr->hash_alg );
1025 void srp_user_verify_session(struct SRPUser *usr, const unsigned char *bytes_HAMK)
1027 if (memcmp(usr->H_AMK, bytes_HAMK, hash_length(usr->hash_alg)) == 0)
1028 usr->authenticated = 1;