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 void delete_ng(NGConstant *ng)
178 static NGConstant *new_ng( SRP_NGType ng_type, const char *n_hex, const char *g_hex )
180 NGConstant *ng = (NGConstant *) malloc(sizeof(NGConstant));
187 if (ng_type != SRP_NG_CUSTOM) {
188 n_hex = global_Ng_constants[ ng_type ].n_hex;
189 g_hex = global_Ng_constants[ ng_type ].g_hex;
193 rv = mpz_set_str(ng->N, n_hex, 16);
194 rv = rv | mpz_set_str(ng->g, g_hex, 16);
215 SRP_HashAlgorithm hash_alg;
219 unsigned char *bytes_B;
222 unsigned char M[SHA512_DIGEST_LENGTH];
223 unsigned char H_AMK[SHA512_DIGEST_LENGTH];
224 unsigned char session_key[SHA512_DIGEST_LENGTH];
230 SRP_HashAlgorithm hash_alg;
237 unsigned char *bytes_A;
241 char *username_verifier;
242 unsigned char *password;
245 unsigned char M[SHA512_DIGEST_LENGTH];
246 unsigned char H_AMK[SHA512_DIGEST_LENGTH];
247 unsigned char session_key[SHA512_DIGEST_LENGTH];
251 static int hash_init(SRP_HashAlgorithm alg, HashCTX *c)
255 case SRP_SHA1: return SHA1_Init(&c->sha);
257 /*case SRP_SHA224: return SHA224_Init(&c->sha256);*/
258 #ifdef CSRP_USE_SHA256
259 case SRP_SHA256: return SHA256_Init(&c->sha256);
261 /*case SRP_SHA384: return SHA384_Init(&c->sha512);
262 case SRP_SHA512: return SHA512_Init(&c->sha512);*/
266 static int hash_update( SRP_HashAlgorithm alg, HashCTX *c, const void *data, size_t len )
270 case SRP_SHA1: return SHA1_Update(&c->sha, data, len);
272 /*case SRP_SHA224: return SHA224_Update(&c->sha256, data, len);*/
273 #ifdef CSRP_USE_SHA256
274 case SRP_SHA256: return SHA256_Update(&c->sha256, data, len);
276 /*case SRP_SHA384: return SHA384_Update( &c->sha512, data, len );
277 case SRP_SHA512: return SHA512_Update( &c->sha512, data, len );*/
281 static int hash_final( SRP_HashAlgorithm alg, HashCTX *c, unsigned char *md )
285 case SRP_SHA1: return SHA1_Final(md, &c->sha);
287 /*case SRP_SHA224: return SHA224_Final(md, &c->sha256);*/
288 #ifdef CSRP_USE_SHA256
289 case SRP_SHA256: return SHA256_Final(md, &c->sha256);
291 /*case SRP_SHA384: return SHA384_Final(md, &c->sha512);
292 case SRP_SHA512: return SHA512_Final(md, &c->sha512);*/
296 static unsigned char *hash(SRP_HashAlgorithm alg, const unsigned char *d, size_t n, unsigned char *md)
300 case SRP_SHA1: return SHA1(d, n, md);
302 /*case SRP_SHA224: return SHA224( d, n, md );*/
303 #ifdef CSRP_USE_SHA256
304 case SRP_SHA256: return SHA256(d, n, md);
306 /*case SRP_SHA384: return SHA384( d, n, md );
307 case SRP_SHA512: return SHA512( d, n, md );*/
311 static size_t hash_length(SRP_HashAlgorithm alg)
315 case SRP_SHA1: return SHA_DIGEST_LENGTH;
317 /*case SRP_SHA224: return SHA224_DIGEST_LENGTH;*/
318 #ifdef CSRP_USE_SHA256
319 case SRP_SHA256: return SHA256_DIGEST_LENGTH;
321 /*case SRP_SHA384: return SHA384_DIGEST_LENGTH;
322 case SRP_SHA512: return SHA512_DIGEST_LENGTH;*/
327 inline static int mpz_num_bytes(const mpz_t op)
329 return (mpz_sizeinbase (op, 2) + 7) / 8;
332 inline static void mpz_to_bin(const mpz_t op, unsigned char *to)
334 mpz_export(to, NULL, 1, 1, 1, 0, op);
337 inline static void mpz_from_bin(const unsigned char *s, size_t len, mpz_t ret)
339 mpz_import(ret, len, 1, 1, 1, 0, s);
342 // set op to (op1 * op2) mod d, using tmp for the calculation
343 inline static void mpz_mulm(mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
345 mpz_mul(tmp, op1, op2);
349 // set op to (op1 + op2) mod d, using tmp for the calculation
350 inline static void mpz_addm( mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp )
352 mpz_add(tmp, op1, op2);
356 // set op to (op1 - op2) mod d, using tmp for the calculation
357 inline static void mpz_subm(mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
359 mpz_sub(tmp, op1, op2);
363 static int H_nn(mpz_t result, SRP_HashAlgorithm alg, const mpz_t N, const mpz_t n1, const mpz_t n2)
365 unsigned char buff[SHA512_DIGEST_LENGTH];
366 size_t len_N = mpz_num_bytes(N);
367 size_t len_n1 = mpz_num_bytes(n1);
368 size_t len_n2 = mpz_num_bytes(n2);
369 size_t nbytes = len_N + len_N;
370 unsigned char *bin = (unsigned char *) malloc(nbytes);
373 if (len_n1 > len_N || len_n2 > len_N) {
377 memset(bin, 0, nbytes);
378 mpz_to_bin(n1, bin + (len_N - len_n1));
379 mpz_to_bin(n2, bin + (len_N + len_N - len_n2));
380 hash( alg, bin, nbytes, buff );
382 mpz_from_bin(buff, hash_length(alg), result);
386 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)
388 unsigned char buff[SHA512_DIGEST_LENGTH];
389 size_t nbytes = len_n + len_bytes;
390 unsigned char *bin = (unsigned char *) malloc(nbytes);
393 memcpy(bin, n, len_n);
394 memcpy(bin + len_n, bytes, len_bytes);
395 hash(alg, bin, nbytes, buff);
397 mpz_from_bin(buff, hash_length(alg), result);
401 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)
403 unsigned char ucp_hash[SHA512_DIGEST_LENGTH];
405 hash_init(alg, &ctx);
407 srp_dbg_data((char*) username, strlen(username), "Username for x: ");
408 srp_dbg_data((char*) password, password_len, "Password for x: ");
409 hash_update(alg, &ctx, username, strlen(username));
410 hash_update(alg, &ctx, ":", 1);
411 hash_update(alg, &ctx, password, password_len);
413 hash_final(alg, &ctx, ucp_hash);
415 return H_ns(result, alg, salt, salt_len, ucp_hash, hash_length(alg));
418 static void update_hash_n(SRP_HashAlgorithm alg, HashCTX *ctx, const mpz_t n)
420 size_t len = mpz_num_bytes(n);
421 unsigned char* n_bytes = (unsigned char *) malloc(len);
424 mpz_to_bin(n, n_bytes);
425 hash_update(alg, ctx, n_bytes, len);
429 static void hash_num( SRP_HashAlgorithm alg, const mpz_t n, unsigned char *dest )
431 int nbytes = mpz_num_bytes(n);
432 unsigned char *bin = (unsigned char *) malloc(nbytes);
436 hash(alg, bin, nbytes, dest);
440 static void calculate_M(SRP_HashAlgorithm alg, NGConstant *ng, unsigned char *dest,
441 const char *I, const unsigned char *s_bytes, size_t s_len,
442 const mpz_t A, const mpz_t B, const unsigned char *K)
444 unsigned char H_N[SHA512_DIGEST_LENGTH];
445 unsigned char H_g[SHA512_DIGEST_LENGTH];
446 unsigned char H_I[SHA512_DIGEST_LENGTH];
447 unsigned char H_xor[SHA512_DIGEST_LENGTH];
450 size_t hash_len = hash_length(alg);
452 hash_num(alg, ng->N, H_N);
453 hash_num(alg, ng->g, H_g);
455 hash(alg, (const unsigned char *)I, strlen(I), H_I);
458 for (i = 0; i < hash_len; i++ )
459 H_xor[i] = H_N[i] ^ H_g[i];
461 hash_init(alg, &ctx);
463 hash_update(alg, &ctx, H_xor, hash_len);
464 hash_update(alg, &ctx, H_I, hash_len);
465 hash_update(alg, &ctx, s_bytes, s_len);
466 update_hash_n(alg, &ctx, A);
467 update_hash_n(alg, &ctx, B);
468 hash_update(alg, &ctx, K, hash_len);
470 hash_final(alg, &ctx, dest);
473 static void calculate_H_AMK(SRP_HashAlgorithm alg, unsigned char *dest, const mpz_t A, const unsigned char *M, const unsigned char *K)
477 hash_init(alg, &ctx);
479 update_hash_n(alg, &ctx, A);
480 hash_update(alg, &ctx, M, hash_length(alg));
481 hash_update(alg, &ctx, K, hash_length(alg));
483 hash_final(alg, &ctx, dest);
487 struct srp_pcgrandom {
488 unsigned long long int m_state;
489 unsigned long long int m_inc;
490 }; typedef struct srp_pcgrandom srp_pcgrandom;
492 static unsigned long int srp_pcgrandom_next(srp_pcgrandom *r)
494 unsigned long long int oldstate = r->m_state;
495 r->m_state = oldstate * 6364136223846793005ULL + r->m_inc;
497 unsigned long int xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u;
498 unsigned long int rot = oldstate >> 59u;
499 return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
502 static void srp_pcgrandom_seed(srp_pcgrandom *r, unsigned long long int state,
503 unsigned long long int seq)
506 r->m_inc = (seq << 1u) | 1u;
507 srp_pcgrandom_next(r);
509 srp_pcgrandom_next(r);
513 static int fill_buff()
525 CryptAcquireContext(&wctx, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
526 CryptGenRandom(wctx, sizeof(g_rand_buff), (BYTE*) g_rand_buff);
527 CryptReleaseContext(wctx, 0);
532 fp = fopen("/dev/urandom", "r");
535 fread(g_rand_buff, sizeof(g_rand_buff), 1, fp);
538 srp_pcgrandom *r = (srp_pcgrandom *) malloc(sizeof(srp_pcgrandom));
539 srp_pcgrandom_seed(r, time(NULL) ^ clock(), 0xda3e39cb94b95bdbULL);
541 for (i = 0; i < RAND_BUFF_MAX; i++) {
542 g_rand_buff[i] = srp_pcgrandom_next(r);
549 static void mpz_fill_random(mpz_t num)
551 // was call: BN_rand(num, 256, -1, 0);
552 if (RAND_BUFF_MAX - g_rand_idx < 32)
554 mpz_from_bin((const unsigned char *) (&g_rand_buff[g_rand_idx]), 32, num);
558 static void init_random()
562 g_initialized = fill_buff();
565 #define srp_dbg_num(num, text) ;
566 /*void srp_dbg_num(mpz_t num, char * prevtext)
568 int len_num = mpz_num_bytes(num);
569 char *bytes_num = (char*) malloc(len_num);
570 mpz_to_bin(num, (unsigned char *) bytes_num);
571 srp_dbg_data(bytes_num, len_num, prevtext);
576 /***********************************************************************************************************
580 ***********************************************************************************************************/
582 void srp_create_salted_verification_key( SRP_HashAlgorithm alg,
583 SRP_NGType ng_type, const char *username_for_verifier,
584 const unsigned char *password, size_t len_password,
585 unsigned char **bytes_s, size_t *len_s,
586 unsigned char **bytes_v, size_t *len_v,
587 const char *n_hex, const char *g_hex )
589 mpz_t v; mpz_init(v);
590 mpz_t x; mpz_init(x);
591 NGConstant *ng = new_ng(ng_type, n_hex, g_hex);
594 goto cleanup_and_exit;
596 init_random(); /* Only happens once */
598 if (*bytes_s == NULL) {
600 if (RAND_BUFF_MAX - g_rand_idx < 16)
602 *bytes_s = (unsigned char*)malloc(sizeof(char) * 16);
603 memcpy(*bytes_s, &g_rand_buff + g_rand_idx, sizeof(char) * 16);
608 if (!calculate_x(x, alg, *bytes_s, *len_s, username_for_verifier,
609 password, len_password))
610 goto cleanup_and_exit;
612 srp_dbg_num(x, "Server calculated x: ");
614 mpz_powm(v, ng->g, x, ng->N);
616 *len_v = mpz_num_bytes(v);
618 *bytes_v = (unsigned char*)malloc(*len_v);
621 goto cleanup_and_exit;
623 mpz_to_bin(v, *bytes_v);
633 /* Out: bytes_B, len_B.
635 * On failure, bytes_B will be set to NULL and len_B will be set to 0
637 struct SRPVerifier *srp_verifier_new(SRP_HashAlgorithm alg,
638 SRP_NGType ng_type, const char *username,
639 const unsigned char *bytes_s, size_t len_s,
640 const unsigned char *bytes_v, size_t len_v,
641 const unsigned char *bytes_A, size_t len_A,
642 const unsigned char *bytes_b, size_t len_b,
643 unsigned char **bytes_B, size_t *len_B,
644 const char *n_hex, const char *g_hex )
646 mpz_t v; mpz_init(v); mpz_from_bin(bytes_v, len_v, v);
647 mpz_t A; mpz_init(A); mpz_from_bin(bytes_A, len_A, A);
648 mpz_t u; mpz_init(u);
649 mpz_t B; mpz_init(B);
650 mpz_t S; mpz_init(S);
651 mpz_t b; mpz_init(b);
652 mpz_t k; mpz_init(k);
653 mpz_t tmp1; mpz_init(tmp1);
654 mpz_t tmp2; mpz_init(tmp2);
655 mpz_t tmp3; mpz_init(tmp3);
656 size_t ulen = strlen(username) + 1;
657 NGConstant *ng = new_ng(ng_type, n_hex, g_hex);
658 struct SRPVerifier *ver = 0;
664 goto cleanup_and_exit;
666 ver = (struct SRPVerifier *) malloc( sizeof(struct SRPVerifier) );
669 goto cleanup_and_exit;
671 init_random(); /* Only happens once */
673 ver->username = (char *) malloc(ulen);
677 if (!ver->username) {
680 goto cleanup_and_exit;
683 memcpy((char*)ver->username, username, ulen);
685 ver->authenticated = 0;
687 /* SRP-6a safety check */
688 mpz_mod(tmp1, A, ng->N);
689 if (mpz_sgn(tmp1) != 0) {
691 mpz_from_bin(bytes_b, len_b, b);
696 if (!H_nn(k, alg, ng->N, ng->N, ng->g)) {
699 goto cleanup_and_exit;
703 mpz_mulm(tmp1, k, v, ng->N, tmp3);
704 mpz_powm(tmp2, ng->g, b, ng->N);
705 mpz_addm(B, tmp1, tmp2, ng->N, tmp3);
707 if (!H_nn(u, alg, ng->N, A, B)) {
710 goto cleanup_and_exit;
713 srp_dbg_num(u, "Server calculated u: ");
715 /* S = (A *(v^u)) ^ b */
716 mpz_powm(tmp1, v, u, ng->N);
717 mpz_mulm(tmp2, A, tmp1, ng->N, tmp3);
718 mpz_powm(S, tmp2, b, ng->N);
720 hash_num(alg, S, ver->session_key);
722 calculate_M(alg, ng, ver->M, username, bytes_s, len_s, A, B, ver->session_key);
723 calculate_H_AMK(alg, ver->H_AMK, A, ver->M, ver->session_key);
725 *len_B = mpz_num_bytes(B);
726 *bytes_B = (unsigned char*)malloc(*len_B);
733 goto cleanup_and_exit;
736 mpz_to_bin(B, *bytes_B);
738 ver->bytes_B = *bytes_B;
761 void srp_verifier_delete(struct SRPVerifier *ver)
767 memset(ver, 0, sizeof(*ver));
774 int srp_verifier_is_authenticated(struct SRPVerifier *ver)
776 return ver->authenticated;
780 const char *srp_verifier_get_username(struct SRPVerifier *ver)
782 return ver->username;
786 const unsigned char *srp_verifier_get_session_key(struct SRPVerifier *ver, size_t *key_length)
789 *key_length = hash_length(ver->hash_alg);
790 return ver->session_key;
794 size_t srp_verifier_get_session_key_length(struct SRPVerifier *ver)
796 return hash_length(ver->hash_alg);
800 /* user_M must be exactly SHA512_DIGEST_LENGTH bytes in size */
801 void srp_verifier_verify_session(struct SRPVerifier *ver, const unsigned char *user_M, unsigned char **bytes_HAMK)
803 if (memcmp(ver->M, user_M, hash_length(ver->hash_alg)) == 0) {
804 ver->authenticated = 1;
805 *bytes_HAMK = ver->H_AMK;
810 /*******************************************************************************/
812 struct SRPUser *srp_user_new(SRP_HashAlgorithm alg, SRP_NGType ng_type,
813 const char *username, const char *username_for_verifier,
814 const unsigned char *bytes_password, size_t len_password,
815 const char *n_hex, const char *g_hex)
817 struct SRPUser *usr = (struct SRPUser *) malloc(sizeof(struct SRPUser));
818 size_t ulen = strlen(username) + 1;
819 size_t uvlen = strlen(username_for_verifier) + 1;
824 init_random(); /* Only happens once */
827 usr->ng = new_ng(ng_type, n_hex, g_hex);
836 usr->username = (char*)malloc(ulen);
837 usr->username_verifier = (char*)malloc(uvlen);
838 usr->password = (unsigned char*)malloc(len_password);
839 usr->password_len = len_password;
841 if (!usr->username || !usr->password)
844 memcpy(usr->username, username, ulen);
845 memcpy(usr->username_verifier, username_for_verifier, uvlen);
846 memcpy(usr->password, bytes_password, len_password);
848 usr->authenticated = 0;
863 if (usr->username_verifier)
864 free(usr->username_verifier);
866 memset(usr->password, 0, usr->password_len);
877 void srp_user_delete(struct SRPUser *usr)
886 memset(usr->password, 0, usr->password_len);
889 free(usr->username_verifier);
895 memset(usr, 0, sizeof(*usr));
902 int srp_user_is_authenticated(struct SRPUser *usr)
904 return usr->authenticated;
908 const char *srp_user_get_username(struct SRPUser *usr)
910 return usr->username;
914 const unsigned char* srp_user_get_session_key(struct SRPUser* usr, size_t* key_length)
917 *key_length = hash_length(usr->hash_alg);
918 return usr->session_key;
922 size_t srp_user_get_session_key_length(struct SRPUser *usr)
924 return hash_length(usr->hash_alg);
928 /* Output: username, bytes_A, len_A */
929 void srp_user_start_authentication(struct SRPUser *usr, char **username,
930 const unsigned char *bytes_a, size_t len_a,
931 unsigned char **bytes_A, size_t *len_A)
934 mpz_from_bin(bytes_a, len_a, usr->a);
936 mpz_fill_random(usr->a);
939 mpz_powm(usr->A, usr->ng->g, usr->a, usr->ng->N);
941 *len_A = mpz_num_bytes(usr->A);
942 *bytes_A = (unsigned char*)malloc(*len_A);
951 mpz_to_bin(usr->A, *bytes_A);
953 usr->bytes_A = *bytes_A;
955 *username = usr->username;
959 /* Output: bytes_M. Buffer length is SHA512_DIGEST_LENGTH */
960 void srp_user_process_challenge(struct SRPUser *usr,
961 const unsigned char *bytes_s, size_t len_s,
962 const unsigned char *bytes_B, size_t len_B,
963 unsigned char **bytes_M, size_t *len_M)
965 mpz_t B; mpz_init(B); mpz_from_bin(bytes_B, len_B, B);
966 mpz_t u; mpz_init(u);
967 mpz_t x; mpz_init(x);
968 mpz_t k; mpz_init(k);
969 mpz_t v; mpz_init(v);
970 mpz_t tmp1; mpz_init(tmp1);
971 mpz_t tmp2; mpz_init(tmp2);
972 mpz_t tmp3; mpz_init(tmp3);
973 mpz_t tmp4; mpz_init(tmp4);
978 if (!H_nn(u, usr->hash_alg, usr->ng->N, usr->A, B))
979 goto cleanup_and_exit;
981 srp_dbg_num(u, "Client calculated u: ");
983 if (!calculate_x(x, usr->hash_alg, bytes_s, len_s,
984 usr->username_verifier, usr->password, usr->password_len))
985 goto cleanup_and_exit;
987 srp_dbg_num(x, "Client calculated x: ");
989 if (!H_nn(k, usr->hash_alg, usr->ng->N, usr->ng->N, usr->ng->g))
990 goto cleanup_and_exit;
992 /* SRP-6a safety check */
993 if ( mpz_sgn(B) != 0 && mpz_sgn(u) != 0 ) {
994 mpz_powm(v, usr->ng->g, x, usr->ng->N);
996 srp_dbg_num(v, "Client calculated v: ");
998 /* S = (B - k*(g^x)) ^ (a + ux) */
1000 mpz_add(tmp2, usr->a, tmp1); /* tmp2 = (a + ux) */
1001 mpz_powm(tmp1, usr->ng->g, x, usr->ng->N); /* tmp1 = g^x */
1002 mpz_mulm(tmp3, k, tmp1, usr->ng->N, tmp4); /* tmp3 = k*(g^x) */
1003 mpz_subm(tmp1, B, tmp3, usr->ng->N, tmp4); /* tmp1 = (B - K*(g^x)) */
1004 mpz_powm(usr->S, tmp1, tmp2, usr->ng->N);
1006 hash_num(usr->hash_alg, usr->S, usr->session_key);
1008 calculate_M( usr->hash_alg, usr->ng, usr->M, usr->username, bytes_s, len_s, usr->A,B, usr->session_key );
1009 calculate_H_AMK( usr->hash_alg, usr->H_AMK, usr->A, usr->M, usr->session_key );
1013 *len_M = hash_length( usr->hash_alg );
1034 void srp_user_verify_session(struct SRPUser *usr, const unsigned char *bytes_HAMK)
1036 if (memcmp(usr->H_AMK, bytes_HAMK, hash_length(usr->hash_alg)) == 0)
1037 usr->authenticated = 1;