Update CSRP-GMP to commit deaa11a7c29a73008

Backports 10 commits, with 8 commits
actually affecting source code:

695822e45d...deaa11a7c2
This commit is contained in:
est31 2016-04-10 04:08:22 +02:00
parent c566a8f2de
commit 9a04dfd0f5
2 changed files with 314 additions and 361 deletions

@ -26,12 +26,14 @@
* *
*/ */
// clang-format off
#ifdef WIN32 #ifdef WIN32
#include <windows.h> #include <windows.h>
#include <wincrypt.h> #include <wincrypt.h>
#else #else
#include <time.h> #include <time.h>
#endif #endif
// clang-format on
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -73,23 +75,24 @@ void *(*srp_alloc) (size_t) = &malloc;
void *(*srp_realloc)(void *, size_t) = &realloc; void *(*srp_realloc)(void *, size_t) = &realloc;
void (*srp_free)(void *) = &free; void (*srp_free)(void *) = &free;
// clang-format off
void srp_set_memory_functions( void srp_set_memory_functions(
void *(*new_srp_alloc)(size_t), void *(*new_srp_alloc)(size_t),
void *(*new_srp_realloc)(void *, size_t), void *(*new_srp_realloc)(void *, size_t),
void (*new_srp_free) (void *)) { void (*new_srp_free)(void *))
{
srp_alloc = new_srp_alloc; srp_alloc = new_srp_alloc;
srp_realloc = new_srp_realloc; srp_realloc = new_srp_realloc;
srp_free = new_srp_free; srp_free = new_srp_free;
} }
// clang-format on
typedef struct typedef struct {
{
mpz_t N; mpz_t N;
mpz_t g; mpz_t g;
} NGConstant; } NGConstant;
struct NGHex struct NGHex {
{
const char *n_hex; const char *n_hex;
const char *g_hex; const char *g_hex;
}; };
@ -97,22 +100,24 @@ struct NGHex
/* All constants here were pulled from Appendix A of RFC 5054 */ /* All constants here were pulled from Appendix A of RFC 5054 */
static struct NGHex global_Ng_constants[] = { static struct NGHex global_Ng_constants[] = {
{/* 1024 */ {/* 1024 */
"EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C9C256576D674DF7496" "EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C"
"EA81D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8E" "9C256576D674DF7496EA81D3383B4813D692C6E0E0D5D8E250B98BE4"
"F4AD69B15D4982559B297BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA" "8E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B29"
"9AFD5138FE8376435B9FC61D2FC0EB06E3", "7BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9A"
"2" "FD5138FE8376435B9FC61D2FC0EB06E3",
}, "2"},
{/* 2048 */ {/* 2048 */
"AC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC3192943DB56050A37329CBB4" "AC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC319294"
"A099ED8193E0757767A13DD52312AB4B03310DCD7F48A9DA04FD50E8083969EDB767B0CF60" "3DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310D"
"95179A163AB3661A05FBD5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF" "CD7F48A9DA04FD50E8083969EDB767B0CF6095179A163AB3661A05FB"
"747359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A436C6481F1D2B907" "D5FAAAE82918A9962F0B93B855F97993EC975EEAA80D740ADBF4FF74"
"8717461A5B9D32E688F87748544523B524B0D57D5EA77A2775D2ECFA032CFBDBF52FB37861" "7359D041D5C33EA71D281E446B14773BCA97B43A23FB801676BD207A"
"60279004E57AE6AF874E7303CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DB" "436C6481F1D2B9078717461A5B9D32E688F87748544523B524B0D57D"
"FBB694B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F9E4AFF73", "5EA77A2775D2ECFA032CFBDBF52FB3786160279004E57AE6AF874E73"
"2" "03CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB6"
}, "94B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F"
"9E4AFF73",
"2"},
{/* 4096 */ {/* 4096 */
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08" "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
"8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B" "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
@ -133,8 +138,7 @@ static struct NGHex global_Ng_constants[] = {
"233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127" "233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127"
"D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199" "D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199"
"FFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFF",
"5" "5"},
},
{/* 8192 */ {/* 8192 */
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08" "FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
"8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B" "8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
@ -173,12 +177,10 @@ static struct NGHex global_Ng_constants[] = {
"359046F4EB879F924009438B481C6CD7889A002ED5EE382BC9190DA6" "359046F4EB879F924009438B481C6CD7889A002ED5EE382BC9190DA6"
"FC026E479558E4475677E9AA9E3050E2765694DFC81F56E880B96E71" "FC026E479558E4475677E9AA9E3050E2765694DFC81F56E880B96E71"
"60C980DD98EDD3DFFFFFFFFFFFFFFFFF", "60C980DD98EDD3DFFFFFFFFFFFFFFFFF",
"13" "13"},
},
{0, 0} /* null sentinel */ {0, 0} /* null sentinel */
}; };
static void delete_ng(NGConstant *ng) static void delete_ng(NGConstant *ng)
{ {
if (ng) { if (ng) {
@ -191,12 +193,12 @@ static void delete_ng(NGConstant *ng)
static NGConstant *new_ng(SRP_NGType ng_type, const char *n_hex, const char *g_hex) static NGConstant *new_ng(SRP_NGType ng_type, const char *n_hex, const char *g_hex)
{ {
NGConstant *ng = (NGConstant *)srp_alloc(sizeof(NGConstant)); NGConstant *ng = (NGConstant *)srp_alloc(sizeof(NGConstant));
if (!ng) return 0;
mpz_init(ng->N); mpz_init(ng->N);
mpz_init(ng->g); mpz_init(ng->g);
if (!ng)
return 0;
if (ng_type != SRP_NG_CUSTOM) { if (ng_type != SRP_NG_CUSTOM) {
n_hex = global_Ng_constants[ng_type].n_hex; n_hex = global_Ng_constants[ng_type].n_hex;
g_hex = global_Ng_constants[ng_type].g_hex; g_hex = global_Ng_constants[ng_type].g_hex;
@ -214,17 +216,13 @@ static NGConstant *new_ng( SRP_NGType ng_type, const char *n_hex, const char *g_
return ng; return ng;
} }
typedef union {
typedef union
{
SHA_CTX sha; SHA_CTX sha;
SHA256_CTX sha256; SHA256_CTX sha256;
// SHA512_CTX sha512; // SHA512_CTX sha512;
} HashCTX; } HashCTX;
struct SRPVerifier {
struct SRPVerifier
{
SRP_HashAlgorithm hash_alg; SRP_HashAlgorithm hash_alg;
NGConstant *ng; NGConstant *ng;
@ -237,9 +235,7 @@ struct SRPVerifier
unsigned char session_key[SHA512_DIGEST_LENGTH]; unsigned char session_key[SHA512_DIGEST_LENGTH];
}; };
struct SRPUser {
struct SRPUser
{
SRP_HashAlgorithm hash_alg; SRP_HashAlgorithm hash_alg;
NGConstant *ng; NGConstant *ng;
@ -260,19 +256,23 @@ struct SRPUser
unsigned char session_key[SHA512_DIGEST_LENGTH]; unsigned char session_key[SHA512_DIGEST_LENGTH];
}; };
// clang-format off
static int hash_init(SRP_HashAlgorithm alg, HashCTX *c) static int hash_init(SRP_HashAlgorithm alg, HashCTX *c)
{ {
switch (alg) { switch (alg) {
#ifdef CSRP_USE_SHA1 #ifdef CSRP_USE_SHA1
case SRP_SHA1: return SHA1_Init(&c->sha); case SRP_SHA1: return SHA1_Init(&c->sha);
#endif #endif
/*case SRP_SHA224: return SHA224_Init(&c->sha256);*/ /*
case SRP_SHA224: return SHA224_Init(&c->sha256);
*/
#ifdef CSRP_USE_SHA256 #ifdef CSRP_USE_SHA256
case SRP_SHA256: return SHA256_Init(&c->sha256); case SRP_SHA256: return SHA256_Init(&c->sha256);
#endif #endif
/*case SRP_SHA384: return SHA384_Init(&c->sha512); /*
case SRP_SHA512: return SHA512_Init(&c->sha512);*/ case SRP_SHA384: return SHA384_Init(&c->sha512);
case SRP_SHA512: return SHA512_Init(&c->sha512);
*/
default: return -1; default: return -1;
}; };
} }
@ -282,12 +282,16 @@ static int hash_update( SRP_HashAlgorithm alg, HashCTX *c, const void *data, siz
#ifdef CSRP_USE_SHA1 #ifdef CSRP_USE_SHA1
case SRP_SHA1: return SHA1_Update(&c->sha, data, len); case SRP_SHA1: return SHA1_Update(&c->sha, data, len);
#endif #endif
/*case SRP_SHA224: return SHA224_Update(&c->sha256, data, len);*/ /*
case SRP_SHA224: return SHA224_Update(&c->sha256, data, len);
*/
#ifdef CSRP_USE_SHA256 #ifdef CSRP_USE_SHA256
case SRP_SHA256: return SHA256_Update(&c->sha256, data, len); case SRP_SHA256: return SHA256_Update(&c->sha256, data, len);
#endif #endif
/*case SRP_SHA384: return SHA384_Update( &c->sha512, data, len ); /*
case SRP_SHA512: return SHA512_Update( &c->sha512, data, len );*/ case SRP_SHA384: return SHA384_Update(&c->sha512, data, len);
case SRP_SHA512: return SHA512_Update(&c->sha512, data, len);
*/
default: return -1; default: return -1;
}; };
} }
@ -297,12 +301,16 @@ static int hash_final( SRP_HashAlgorithm alg, HashCTX *c, unsigned char *md )
#ifdef CSRP_USE_SHA1 #ifdef CSRP_USE_SHA1
case SRP_SHA1: return SHA1_Final(md, &c->sha); case SRP_SHA1: return SHA1_Final(md, &c->sha);
#endif #endif
/*case SRP_SHA224: return SHA224_Final(md, &c->sha256);*/ /*
case SRP_SHA224: return SHA224_Final(md, &c->sha256);
*/
#ifdef CSRP_USE_SHA256 #ifdef CSRP_USE_SHA256
case SRP_SHA256: return SHA256_Final(md, &c->sha256); case SRP_SHA256: return SHA256_Final(md, &c->sha256);
#endif #endif
/*case SRP_SHA384: return SHA384_Final(md, &c->sha512); /*
case SRP_SHA512: return SHA512_Final(md, &c->sha512);*/ case SRP_SHA384: return SHA384_Final(md, &c->sha512);
case SRP_SHA512: return SHA512_Final(md, &c->sha512);
*/
default: return -1; default: return -1;
}; };
} }
@ -312,12 +320,16 @@ static unsigned char *hash(SRP_HashAlgorithm alg, const unsigned char *d, size_t
#ifdef CSRP_USE_SHA1 #ifdef CSRP_USE_SHA1
case SRP_SHA1: return SHA1(d, n, md); case SRP_SHA1: return SHA1(d, n, md);
#endif #endif
/*case SRP_SHA224: return SHA224( d, n, md );*/ /*
case SRP_SHA224: return SHA224( d, n, md );
*/
#ifdef CSRP_USE_SHA256 #ifdef CSRP_USE_SHA256
case SRP_SHA256: return SHA256(d, n, md); case SRP_SHA256: return SHA256(d, n, md);
#endif #endif
/*case SRP_SHA384: return SHA384( d, n, md ); /*
case SRP_SHA512: return SHA512( d, n, md );*/ case SRP_SHA384: return SHA384( d, n, md );
case SRP_SHA512: return SHA512( d, n, md );
*/
default: return 0; default: return 0;
}; };
} }
@ -327,15 +339,20 @@ static size_t hash_length(SRP_HashAlgorithm alg)
#ifdef CSRP_USE_SHA1 #ifdef CSRP_USE_SHA1
case SRP_SHA1: return SHA_DIGEST_LENGTH; case SRP_SHA1: return SHA_DIGEST_LENGTH;
#endif #endif
/*case SRP_SHA224: return SHA224_DIGEST_LENGTH;*/ /*
case SRP_SHA224: return SHA224_DIGEST_LENGTH;
*/
#ifdef CSRP_USE_SHA256 #ifdef CSRP_USE_SHA256
case SRP_SHA256: return SHA256_DIGEST_LENGTH; case SRP_SHA256: return SHA256_DIGEST_LENGTH;
#endif #endif
/*case SRP_SHA384: return SHA384_DIGEST_LENGTH; /*
case SRP_SHA512: return SHA512_DIGEST_LENGTH;*/ case SRP_SHA384: return SHA384_DIGEST_LENGTH;
case SRP_SHA512: return SHA512_DIGEST_LENGTH;
*/
default: return -1; default: return -1;
}; };
} }
// clang-format on
inline static int mpz_num_bytes(const mpz_t op) inline static int mpz_num_bytes(const mpz_t op)
{ {
@ -353,27 +370,31 @@ inline static void mpz_from_bin(const unsigned char *s, size_t len, mpz_t ret)
} }
// set op to (op1 * op2) mod d, using tmp for the calculation // set op to (op1 * op2) mod d, using tmp for the calculation
inline static void mpz_mulm(mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp) inline static void mpz_mulm(
mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
{ {
mpz_mul(tmp, op1, op2); mpz_mul(tmp, op1, op2);
mpz_mod(op, tmp, d); mpz_mod(op, tmp, d);
} }
// set op to (op1 + op2) mod d, using tmp for the calculation // set op to (op1 + op2) mod d, using tmp for the calculation
inline static void mpz_addm( mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp ) inline static void mpz_addm(
mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
{ {
mpz_add(tmp, op1, op2); mpz_add(tmp, op1, op2);
mpz_mod(op, tmp, d); mpz_mod(op, tmp, d);
} }
// set op to (op1 - op2) mod d, using tmp for the calculation // set op to (op1 - op2) mod d, using tmp for the calculation
inline static void mpz_subm(mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp) inline static void mpz_subm(
mpz_t op, const mpz_t op1, const mpz_t op2, const mpz_t d, mpz_t tmp)
{ {
mpz_sub(tmp, op1, op2); mpz_sub(tmp, op1, op2);
mpz_mod(op, tmp, d); mpz_mod(op, tmp, d);
} }
static int H_nn(mpz_t result, SRP_HashAlgorithm alg, const mpz_t N, const mpz_t n1, const mpz_t n2) static SRP_Result H_nn(
mpz_t result, SRP_HashAlgorithm alg, const mpz_t N, const mpz_t n1, const mpz_t n2)
{ {
unsigned char buff[SHA512_DIGEST_LENGTH]; unsigned char buff[SHA512_DIGEST_LENGTH];
size_t len_N = mpz_num_bytes(N); size_t len_N = mpz_num_bytes(N);
@ -381,11 +402,10 @@ static int H_nn(mpz_t result, SRP_HashAlgorithm alg, const mpz_t N, const mpz_t
size_t len_n2 = mpz_num_bytes(n2); size_t len_n2 = mpz_num_bytes(n2);
size_t nbytes = len_N + len_N; size_t nbytes = len_N + len_N;
unsigned char *bin = (unsigned char *)srp_alloc(nbytes); unsigned char *bin = (unsigned char *)srp_alloc(nbytes);
if (!bin) if (!bin) return SRP_ERR;
return 0;
if (len_n1 > len_N || len_n2 > len_N) { if (len_n1 > len_N || len_n2 > len_N) {
srp_free(bin); srp_free(bin);
return 0; return SRP_ERR;
} }
memset(bin, 0, nbytes); memset(bin, 0, nbytes);
mpz_to_bin(n1, bin + (len_N - len_n1)); mpz_to_bin(n1, bin + (len_N - len_n1));
@ -393,25 +413,27 @@ static int H_nn(mpz_t result, SRP_HashAlgorithm alg, const mpz_t N, const mpz_t
hash(alg, bin, nbytes, buff); hash(alg, bin, nbytes, buff);
srp_free(bin); srp_free(bin);
mpz_from_bin(buff, hash_length(alg), result); mpz_from_bin(buff, hash_length(alg), result);
return 1; return SRP_OK;
} }
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) static SRP_Result H_ns(mpz_t result, SRP_HashAlgorithm alg, const unsigned char *n,
size_t len_n, const unsigned char *bytes, size_t len_bytes)
{ {
unsigned char buff[SHA512_DIGEST_LENGTH]; unsigned char buff[SHA512_DIGEST_LENGTH];
size_t nbytes = len_n + len_bytes; size_t nbytes = len_n + len_bytes;
unsigned char *bin = (unsigned char *)srp_alloc(nbytes); unsigned char *bin = (unsigned char *)srp_alloc(nbytes);
if (!bin) if (!bin) return SRP_ERR;
return 0;
memcpy(bin, n, len_n); memcpy(bin, n, len_n);
memcpy(bin + len_n, bytes, len_bytes); memcpy(bin + len_n, bytes, len_bytes);
hash(alg, bin, nbytes, buff); hash(alg, bin, nbytes, buff);
srp_free(bin); srp_free(bin);
mpz_from_bin(buff, hash_length(alg), result); mpz_from_bin(buff, hash_length(alg), result);
return 1; return SRP_OK;
} }
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) 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)
{ {
unsigned char ucp_hash[SHA512_DIGEST_LENGTH]; unsigned char ucp_hash[SHA512_DIGEST_LENGTH];
HashCTX ctx; HashCTX ctx;
@ -428,31 +450,31 @@ static int calculate_x(mpz_t result, SRP_HashAlgorithm alg, const unsigned char
return H_ns(result, alg, salt, salt_len, ucp_hash, hash_length(alg)); return H_ns(result, alg, salt, salt_len, ucp_hash, hash_length(alg));
} }
static void update_hash_n(SRP_HashAlgorithm alg, HashCTX *ctx, const mpz_t n) static SRP_Result update_hash_n(SRP_HashAlgorithm alg, HashCTX *ctx, const mpz_t n)
{ {
size_t len = mpz_num_bytes(n); size_t len = mpz_num_bytes(n);
unsigned char *n_bytes = (unsigned char *)srp_alloc(len); unsigned char *n_bytes = (unsigned char *)srp_alloc(len);
if (!n_bytes) if (!n_bytes) return SRP_ERR;
return;
mpz_to_bin(n, n_bytes); mpz_to_bin(n, n_bytes);
hash_update(alg, ctx, n_bytes, len); hash_update(alg, ctx, n_bytes, len);
srp_free(n_bytes); srp_free(n_bytes);
return SRP_OK;
} }
static void hash_num( SRP_HashAlgorithm alg, const mpz_t n, unsigned char *dest ) static SRP_Result hash_num(SRP_HashAlgorithm alg, const mpz_t n, unsigned char *dest)
{ {
int nbytes = mpz_num_bytes(n); int nbytes = mpz_num_bytes(n);
unsigned char *bin = (unsigned char *)srp_alloc(nbytes); unsigned char *bin = (unsigned char *)srp_alloc(nbytes);
if(!bin) if (!bin) return SRP_ERR;
return;
mpz_to_bin(n, bin); mpz_to_bin(n, bin);
hash(alg, bin, nbytes, dest); hash(alg, bin, nbytes, dest);
srp_free(bin); srp_free(bin);
return SRP_OK;
} }
static void calculate_M(SRP_HashAlgorithm alg, NGConstant *ng, unsigned char *dest, static SRP_Result calculate_M(SRP_HashAlgorithm alg, NGConstant *ng, unsigned char *dest,
const char *I, const unsigned char *s_bytes, size_t s_len, const char *I, const unsigned char *s_bytes, size_t s_len, const mpz_t A,
const mpz_t A, const mpz_t B, const unsigned char *K) const mpz_t B, const unsigned char *K)
{ {
unsigned char H_N[SHA512_DIGEST_LENGTH]; unsigned char H_N[SHA512_DIGEST_LENGTH];
unsigned char H_g[SHA512_DIGEST_LENGTH]; unsigned char H_g[SHA512_DIGEST_LENGTH];
@ -462,12 +484,11 @@ static void calculate_M(SRP_HashAlgorithm alg, NGConstant *ng, unsigned char *de
size_t i = 0; size_t i = 0;
size_t hash_len = hash_length(alg); size_t hash_len = hash_length(alg);
hash_num(alg, ng->N, H_N); if (!hash_num(alg, ng->N, H_N)) return SRP_ERR;
hash_num(alg, ng->g, H_g); if (!hash_num(alg, ng->g, H_g)) return SRP_ERR;
hash(alg, (const unsigned char *)I, strlen(I), H_I); hash(alg, (const unsigned char *)I, strlen(I), H_I);
for (i = 0; i < hash_len; i++) for (i = 0; i < hash_len; i++)
H_xor[i] = H_N[i] ^ H_g[i]; H_xor[i] = H_N[i] ^ H_g[i];
@ -476,54 +497,29 @@ static void calculate_M(SRP_HashAlgorithm alg, NGConstant *ng, unsigned char *de
hash_update(alg, &ctx, H_xor, hash_len); hash_update(alg, &ctx, H_xor, hash_len);
hash_update(alg, &ctx, H_I, hash_len); hash_update(alg, &ctx, H_I, hash_len);
hash_update(alg, &ctx, s_bytes, s_len); hash_update(alg, &ctx, s_bytes, s_len);
update_hash_n(alg, &ctx, A); if (!update_hash_n(alg, &ctx, A)) return SRP_ERR;
update_hash_n(alg, &ctx, B); if (!update_hash_n(alg, &ctx, B)) return SRP_ERR;
hash_update(alg, &ctx, K, hash_len); hash_update(alg, &ctx, K, hash_len);
hash_final(alg, &ctx, dest); hash_final(alg, &ctx, dest);
return SRP_OK;
} }
static void calculate_H_AMK(SRP_HashAlgorithm alg, unsigned char *dest, const mpz_t A, const unsigned char *M, const unsigned char *K) static SRP_Result calculate_H_AMK(SRP_HashAlgorithm alg, unsigned char *dest,
const mpz_t A, const unsigned char *M, const unsigned char *K)
{ {
HashCTX ctx; HashCTX ctx;
hash_init(alg, &ctx); hash_init(alg, &ctx);
update_hash_n(alg, &ctx, A); if (!update_hash_n(alg, &ctx, A)) return SRP_ERR;
hash_update(alg, &ctx, M, hash_length(alg)); hash_update(alg, &ctx, M, hash_length(alg));
hash_update(alg, &ctx, K, hash_length(alg)); hash_update(alg, &ctx, K, hash_length(alg));
hash_final(alg, &ctx, dest); hash_final(alg, &ctx, dest);
return SRP_OK;
} }
#ifndef WIN32
struct srp_pcgrandom {
unsigned long long int m_state;
unsigned long long int m_inc;
}; typedef struct srp_pcgrandom srp_pcgrandom;
static unsigned long int srp_pcgrandom_next(srp_pcgrandom *r)
{
unsigned long long int oldstate = r->m_state;
r->m_state = oldstate * 6364136223846793005ULL + r->m_inc;
unsigned long int xorshifted = ((oldstate >> 18u) ^ oldstate) >> 27u;
unsigned long int rot = oldstate >> 59u;
return (xorshifted >> rot) | (xorshifted << ((-rot) & 31));
}
static void srp_pcgrandom_seed(srp_pcgrandom *r, unsigned long long int state,
unsigned long long int seq)
{
r->m_state = 0U;
r->m_inc = (seq << 1u) | 1u;
srp_pcgrandom_next(r);
r->m_state += state;
srp_pcgrandom_next(r);
}
#endif
static SRP_Result fill_buff() static SRP_Result fill_buff()
{ {
g_rand_idx = 0; g_rand_idx = 0;
@ -536,29 +532,18 @@ static SRP_Result fill_buff()
#ifdef WIN32 #ifdef WIN32
CryptAcquireContext(&wctx, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT); if (!CryptAcquireContext(&wctx, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
CryptGenRandom(wctx, sizeof(g_rand_buff), (BYTE*) g_rand_buff); return SRP_ERR;
CryptReleaseContext(wctx, 0); if (!CryptGenRandom(wctx, sizeof(g_rand_buff), (BYTE *)g_rand_buff)) return SRP_ERR;
if (!CryptReleaseContext(wctx, 0)) return SRP_ERR;
return SRP_OK;
#else #else
fp = fopen("/dev/urandom", "r"); fp = fopen("/dev/urandom", "r");
if (fp) { if (!fp) return SRP_ERR;
fread(g_rand_buff, sizeof(g_rand_buff), 1, fp);
fclose(fp); if (fread(g_rand_buff, sizeof(g_rand_buff), 1, fp) != 1) return SRP_ERR;
} else { if (fclose(fp)) return SRP_ERR;
srp_pcgrandom *r = (srp_pcgrandom *) srp_alloc(sizeof(srp_pcgrandom));
if (!r)
return SRP_ERR;
srp_pcgrandom_seed(r, time(NULL) ^ clock(), 0xda3e39cb94b95bdbULL);
size_t i = 0;
for (i = 0; i < RAND_BUFF_MAX; i++) {
g_rand_buff[i] = srp_pcgrandom_next(r);
}
srp_free(r);
}
#endif #endif
return SRP_OK; return SRP_OK;
} }
@ -567,8 +552,7 @@ static SRP_Result mpz_fill_random(mpz_t num)
{ {
// was call: BN_rand(num, 256, -1, 0); // was call: BN_rand(num, 256, -1, 0);
if (RAND_BUFF_MAX - g_rand_idx < 32) if (RAND_BUFF_MAX - g_rand_idx < 32)
if (fill_buff() != SRP_OK) if (fill_buff() != SRP_OK) return SRP_ERR;
return SRP_ERR;
mpz_from_bin((const unsigned char *)(&g_rand_buff[g_rand_idx]), 32, num); mpz_from_bin((const unsigned char *)(&g_rand_buff[g_rand_idx]), 32, num);
g_rand_idx += 32; g_rand_idx += 32;
return SRP_OK; return SRP_OK;
@ -576,8 +560,7 @@ static SRP_Result mpz_fill_random(mpz_t num)
static SRP_Result init_random() static SRP_Result init_random()
{ {
if (g_initialized) if (g_initialized) return SRP_OK;
return SRP_OK;
SRP_Result ret = fill_buff(); SRP_Result ret = fill_buff();
g_initialized = (ret == SRP_OK); g_initialized = (ret == SRP_OK);
return ret; return ret;
@ -600,6 +583,7 @@ static SRP_Result init_random()
* *
***********************************************************************************************************/ ***********************************************************************************************************/
// clang-format off
SRP_Result srp_create_salted_verification_key( SRP_HashAlgorithm alg, SRP_Result srp_create_salted_verification_key( SRP_HashAlgorithm alg,
SRP_NGType ng_type, const char *username_for_verifier, SRP_NGType ng_type, const char *username_for_verifier,
const unsigned char *password, size_t len_password, const unsigned char *password, size_t len_password,
@ -611,10 +595,11 @@ SRP_Result srp_create_salted_verification_key( SRP_HashAlgorithm alg,
mpz_t v; mpz_init(v); mpz_t v; mpz_init(v);
mpz_t x; mpz_init(x); mpz_t x; mpz_init(x);
// clang-format on
NGConstant *ng = new_ng(ng_type, n_hex, g_hex); NGConstant *ng = new_ng(ng_type, n_hex, g_hex);
if (!ng) if (!ng) goto error_and_exit;
goto error_and_exit;
if (init_random() != SRP_OK) /* Only happens once */ if (init_random() != SRP_OK) /* Only happens once */
goto error_and_exit; goto error_and_exit;
@ -623,18 +608,15 @@ SRP_Result srp_create_salted_verification_key( SRP_HashAlgorithm alg,
size_t size_to_fill = 16; size_t size_to_fill = 16;
*len_s = size_to_fill; *len_s = size_to_fill;
if (RAND_BUFF_MAX - g_rand_idx < size_to_fill) if (RAND_BUFF_MAX - g_rand_idx < size_to_fill)
if (fill_buff() != SRP_OK) if (fill_buff() != SRP_OK) goto error_and_exit;
goto error_and_exit;
*bytes_s = (unsigned char *)srp_alloc(size_to_fill); *bytes_s = (unsigned char *)srp_alloc(size_to_fill);
if (!*bytes_s) if (!*bytes_s) goto error_and_exit;
goto error_and_exit;
memcpy(*bytes_s, &g_rand_buff + g_rand_idx, size_to_fill); memcpy(*bytes_s, &g_rand_buff + g_rand_idx, size_to_fill);
g_rand_idx += size_to_fill; g_rand_idx += size_to_fill;
} }
if (!calculate_x(
if (!calculate_x(x, alg, *bytes_s, *len_s, username_for_verifier, x, alg, *bytes_s, *len_s, username_for_verifier, password, len_password))
password, len_password))
goto error_and_exit; goto error_and_exit;
srp_dbg_num(x, "Server calculated x: "); srp_dbg_num(x, "Server calculated x: ");
@ -645,8 +627,7 @@ SRP_Result srp_create_salted_verification_key( SRP_HashAlgorithm alg,
*bytes_v = (unsigned char *)srp_alloc(*len_v); *bytes_v = (unsigned char *)srp_alloc(*len_v);
if (!bytes_v) if (!*bytes_v) goto error_and_exit;
goto error_and_exit;
mpz_to_bin(v, *bytes_v); mpz_to_bin(v, *bytes_v);
@ -660,7 +641,7 @@ error_and_exit:
goto cleanup_and_exit; goto cleanup_and_exit;
} }
// clang-format off
/* Out: bytes_B, len_B. /* Out: bytes_B, len_B.
* *
@ -685,6 +666,7 @@ struct SRPVerifier *srp_verifier_new(SRP_HashAlgorithm alg,
mpz_t tmp1; mpz_init(tmp1); mpz_t tmp1; mpz_init(tmp1);
mpz_t tmp2; mpz_init(tmp2); mpz_t tmp2; mpz_init(tmp2);
mpz_t tmp3; mpz_init(tmp3); mpz_t tmp3; mpz_init(tmp3);
// clang-format on
size_t ulen = strlen(username) + 1; size_t ulen = strlen(username) + 1;
NGConstant *ng = new_ng(ng_type, n_hex, g_hex); NGConstant *ng = new_ng(ng_type, n_hex, g_hex);
struct SRPVerifier *ver = 0; struct SRPVerifier *ver = 0;
@ -692,13 +674,11 @@ struct SRPVerifier *srp_verifier_new(SRP_HashAlgorithm alg,
*len_B = 0; *len_B = 0;
*bytes_B = 0; *bytes_B = 0;
if (!ng) if (!ng) goto cleanup_and_exit;
goto cleanup_and_exit;
ver = (struct SRPVerifier *)srp_alloc(sizeof(struct SRPVerifier)); ver = (struct SRPVerifier *)srp_alloc(sizeof(struct SRPVerifier));
if (!ver) if (!ver) goto cleanup_and_exit;
goto cleanup_and_exit;
if (init_random() != SRP_OK) { /* Only happens once */ if (init_random() != SRP_OK) { /* Only happens once */
srp_free(ver); srp_free(ver);
@ -726,29 +706,17 @@ struct SRPVerifier *srp_verifier_new(SRP_HashAlgorithm alg,
if (bytes_b) { if (bytes_b) {
mpz_from_bin(bytes_b, len_b, b); mpz_from_bin(bytes_b, len_b, b);
} else { } else {
if (mpz_fill_random(b) != SRP_OK) { if (!mpz_fill_random(b)) goto ver_cleanup_and_exit;
srp_free(ver);
ver = 0;
goto cleanup_and_exit;
}
} }
if (!H_nn(k, alg, ng->N, ng->N, ng->g)) { if (!H_nn(k, alg, ng->N, ng->N, ng->g)) goto ver_cleanup_and_exit;
srp_free(ver);
ver = 0;
goto cleanup_and_exit;
}
/* B = kv + g^b */ /* B = kv + g^b */
mpz_mulm(tmp1, k, v, ng->N, tmp3); mpz_mulm(tmp1, k, v, ng->N, tmp3);
mpz_powm(tmp2, ng->g, b, ng->N); mpz_powm(tmp2, ng->g, b, ng->N);
mpz_addm(B, tmp1, tmp2, ng->N, tmp3); mpz_addm(B, tmp1, tmp2, ng->N, tmp3);
if (!H_nn(u, alg, ng->N, A, B)) { if (!H_nn(u, alg, ng->N, A, B)) goto ver_cleanup_and_exit;
srp_free(ver);
ver = 0;
goto cleanup_and_exit;
}
srp_dbg_num(u, "Server calculated u: "); srp_dbg_num(u, "Server calculated u: ");
@ -757,20 +725,22 @@ struct SRPVerifier *srp_verifier_new(SRP_HashAlgorithm alg,
mpz_mulm(tmp2, A, tmp1, ng->N, tmp3); mpz_mulm(tmp2, A, tmp1, ng->N, tmp3);
mpz_powm(S, tmp2, b, ng->N); mpz_powm(S, tmp2, b, ng->N);
hash_num(alg, S, ver->session_key); if (!hash_num(alg, S, ver->session_key)) goto ver_cleanup_and_exit;
calculate_M(alg, ng, ver->M, username, bytes_s, len_s, A, B, ver->session_key); if (!calculate_M(
calculate_H_AMK(alg, ver->H_AMK, A, ver->M, ver->session_key); alg, ng, ver->M, username, bytes_s, len_s, A, B, ver->session_key)) {
goto ver_cleanup_and_exit;
}
if (!calculate_H_AMK(alg, ver->H_AMK, A, ver->M, ver->session_key)) {
goto ver_cleanup_and_exit;
}
*len_B = mpz_num_bytes(B); *len_B = mpz_num_bytes(B);
*bytes_B = (unsigned char *)srp_alloc(*len_B); *bytes_B = (unsigned char *)srp_alloc(*len_B);
if (!*bytes_B) { if (!*bytes_B) {
srp_free(ver->username);
srp_free(ver);
ver = 0;
*len_B = 0; *len_B = 0;
goto cleanup_and_exit; goto ver_cleanup_and_exit;
} }
mpz_to_bin(B, *bytes_B); mpz_to_bin(B, *bytes_B);
@ -793,11 +763,13 @@ cleanup_and_exit:
mpz_clear(tmp2); mpz_clear(tmp2);
mpz_clear(tmp3); mpz_clear(tmp3);
return ver; return ver;
ver_cleanup_and_exit:
srp_free(ver->username);
srp_free(ver);
ver = 0;
goto cleanup_and_exit;
} }
void srp_verifier_delete(struct SRPVerifier *ver) void srp_verifier_delete(struct SRPVerifier *ver)
{ {
if (ver) { if (ver) {
@ -809,36 +781,31 @@ void srp_verifier_delete(struct SRPVerifier *ver)
} }
} }
int srp_verifier_is_authenticated(struct SRPVerifier *ver) int srp_verifier_is_authenticated(struct SRPVerifier *ver)
{ {
return ver->authenticated; return ver->authenticated;
} }
const char *srp_verifier_get_username(struct SRPVerifier *ver) const char *srp_verifier_get_username(struct SRPVerifier *ver)
{ {
return ver->username; return ver->username;
} }
const unsigned char *srp_verifier_get_session_key(
const unsigned char *srp_verifier_get_session_key(struct SRPVerifier *ver, size_t *key_length) struct SRPVerifier *ver, size_t *key_length)
{ {
if (key_length) if (key_length) *key_length = hash_length(ver->hash_alg);
*key_length = hash_length(ver->hash_alg);
return ver->session_key; return ver->session_key;
} }
size_t srp_verifier_get_session_key_length(struct SRPVerifier *ver) size_t srp_verifier_get_session_key_length(struct SRPVerifier *ver)
{ {
return hash_length(ver->hash_alg); return hash_length(ver->hash_alg);
} }
/* user_M must be exactly SHA512_DIGEST_LENGTH bytes in size */ /* user_M must be exactly SHA512_DIGEST_LENGTH bytes in size */
void srp_verifier_verify_session(struct SRPVerifier *ver, const unsigned char *user_M, unsigned char **bytes_HAMK) void srp_verifier_verify_session(
struct SRPVerifier *ver, const unsigned char *user_M, unsigned char **bytes_HAMK)
{ {
if (memcmp(ver->M, user_M, hash_length(ver->hash_alg)) == 0) { if (memcmp(ver->M, user_M, hash_length(ver->hash_alg)) == 0) {
ver->authenticated = 1; ver->authenticated = 1;
@ -851,15 +818,14 @@ void srp_verifier_verify_session(struct SRPVerifier *ver, const unsigned char *u
struct SRPUser *srp_user_new(SRP_HashAlgorithm alg, SRP_NGType ng_type, struct SRPUser *srp_user_new(SRP_HashAlgorithm alg, SRP_NGType ng_type,
const char *username, const char *username_for_verifier, const char *username, const char *username_for_verifier,
const unsigned char *bytes_password, size_t len_password, const unsigned char *bytes_password, size_t len_password, const char *n_hex,
const char *n_hex, const char *g_hex) const char *g_hex)
{ {
struct SRPUser *usr = (struct SRPUser *)srp_alloc(sizeof(struct SRPUser)); struct SRPUser *usr = (struct SRPUser *)srp_alloc(sizeof(struct SRPUser));
size_t ulen = strlen(username) + 1; size_t ulen = strlen(username) + 1;
size_t uvlen = strlen(username_for_verifier) + 1; size_t uvlen = strlen(username_for_verifier) + 1;
if (!usr) if (!usr) goto err_exit;
goto err_exit;
if (init_random() != SRP_OK) /* Only happens once */ if (init_random() != SRP_OK) /* Only happens once */
goto err_exit; goto err_exit;
@ -871,16 +837,14 @@ struct SRPUser *srp_user_new(SRP_HashAlgorithm alg, SRP_NGType ng_type,
mpz_init(usr->A); mpz_init(usr->A);
mpz_init(usr->S); mpz_init(usr->S);
if (!usr->ng) if (!usr->ng) goto err_exit;
goto err_exit;
usr->username = (char *)srp_alloc(ulen); usr->username = (char *)srp_alloc(ulen);
usr->username_verifier = (char *)srp_alloc(uvlen); usr->username_verifier = (char *)srp_alloc(uvlen);
usr->password = (unsigned char *)srp_alloc(len_password); usr->password = (unsigned char *)srp_alloc(len_password);
usr->password_len = len_password; usr->password_len = len_password;
if (!usr->username || !usr->password || !usr->username_verifier) if (!usr->username || !usr->password || !usr->username_verifier) goto err_exit;
goto err_exit;
memcpy(usr->username, username, ulen); memcpy(usr->username, username, ulen);
memcpy(usr->username_verifier, username_for_verifier, uvlen); memcpy(usr->username_verifier, username_for_verifier, uvlen);
@ -897,8 +861,7 @@ err_exit:
mpz_clear(usr->a); mpz_clear(usr->a);
mpz_clear(usr->A); mpz_clear(usr->A);
mpz_clear(usr->S); mpz_clear(usr->S);
if (usr->ng) if (usr->ng) delete_ng(usr->ng);
delete_ng(usr->ng);
srp_free(usr->username); srp_free(usr->username);
srp_free(usr->username_verifier); srp_free(usr->username_verifier);
if (usr->password) { if (usr->password) {
@ -911,8 +874,6 @@ err_exit:
return 0; return 0;
} }
void srp_user_delete(struct SRPUser *usr) void srp_user_delete(struct SRPUser *usr)
{ {
if (usr) { if (usr) {
@ -928,52 +889,45 @@ void srp_user_delete(struct SRPUser *usr)
srp_free(usr->username_verifier); srp_free(usr->username_verifier);
srp_free(usr->password); srp_free(usr->password);
if (usr->bytes_A) if (usr->bytes_A) srp_free(usr->bytes_A);
srp_free(usr->bytes_A);
memset(usr, 0, sizeof(*usr)); memset(usr, 0, sizeof(*usr));
srp_free(usr); srp_free(usr);
} }
} }
int srp_user_is_authenticated(struct SRPUser *usr) int srp_user_is_authenticated(struct SRPUser *usr)
{ {
return usr->authenticated; return usr->authenticated;
} }
const char *srp_user_get_username(struct SRPUser *usr) const char *srp_user_get_username(struct SRPUser *usr)
{ {
return usr->username; return usr->username;
} }
const unsigned char *srp_user_get_session_key(struct SRPUser *usr, size_t *key_length) const unsigned char *srp_user_get_session_key(struct SRPUser *usr, size_t *key_length)
{ {
if (key_length) if (key_length) *key_length = hash_length(usr->hash_alg);
*key_length = hash_length(usr->hash_alg);
return usr->session_key; return usr->session_key;
} }
size_t srp_user_get_session_key_length(struct SRPUser *usr) size_t srp_user_get_session_key_length(struct SRPUser *usr)
{ {
return hash_length(usr->hash_alg); return hash_length(usr->hash_alg);
} }
// clang-format off
/* Output: username, bytes_A, len_A */ /* Output: username, bytes_A, len_A */
SRP_Result srp_user_start_authentication(struct SRPUser *usr, char **username, SRP_Result srp_user_start_authentication(struct SRPUser *usr, char **username,
const unsigned char *bytes_a, size_t len_a, const unsigned char *bytes_a, size_t len_a,
unsigned char **bytes_A, size_t *len_A) unsigned char **bytes_A, size_t *len_A)
{ {
// clang-format on
if (bytes_a) { if (bytes_a) {
mpz_from_bin(bytes_a, len_a, usr->a); mpz_from_bin(bytes_a, len_a, usr->a);
} else { } else {
if (mpz_fill_random(usr->a) != SRP_OK) if (!mpz_fill_random(usr->a)) goto error_and_exit;
goto error_and_exit;
} }
mpz_powm(usr->A, usr->ng->g, usr->a, usr->ng->N); mpz_powm(usr->A, usr->ng->g, usr->a, usr->ng->N);
@ -981,14 +935,12 @@ SRP_Result srp_user_start_authentication(struct SRPUser *usr, char **username,
*len_A = mpz_num_bytes(usr->A); *len_A = mpz_num_bytes(usr->A);
*bytes_A = (unsigned char *)srp_alloc(*len_A); *bytes_A = (unsigned char *)srp_alloc(*len_A);
if (!*bytes_A) if (!*bytes_A) goto error_and_exit;
goto error_and_exit;
mpz_to_bin(usr->A, *bytes_A); mpz_to_bin(usr->A, *bytes_A);
usr->bytes_A = *bytes_A; usr->bytes_A = *bytes_A;
if (username) if (username) *username = usr->username;
*username = usr->username;
return SRP_OK; return SRP_OK;
@ -999,7 +951,7 @@ error_and_exit:
return SRP_ERR; return SRP_ERR;
} }
// clang-format off
/* Output: bytes_M. Buffer length is SHA512_DIGEST_LENGTH */ /* Output: bytes_M. Buffer length is SHA512_DIGEST_LENGTH */
void srp_user_process_challenge(struct SRPUser *usr, void srp_user_process_challenge(struct SRPUser *usr,
const unsigned char *bytes_s, size_t len_s, const unsigned char *bytes_s, size_t len_s,
@ -1015,17 +967,17 @@ void srp_user_process_challenge(struct SRPUser *usr,
mpz_t tmp2; mpz_init(tmp2); mpz_t tmp2; mpz_init(tmp2);
mpz_t tmp3; mpz_init(tmp3); mpz_t tmp3; mpz_init(tmp3);
mpz_t tmp4; mpz_init(tmp4); mpz_t tmp4; mpz_init(tmp4);
// clang-format on
*len_M = 0; *len_M = 0;
*bytes_M = 0; *bytes_M = 0;
if (!H_nn(u, usr->hash_alg, usr->ng->N, usr->A, B)) if (!H_nn(u, usr->hash_alg, usr->ng->N, usr->A, B)) goto cleanup_and_exit;
goto cleanup_and_exit;
srp_dbg_num(u, "Client calculated u: "); srp_dbg_num(u, "Client calculated u: ");
if (!calculate_x(x, usr->hash_alg, bytes_s, len_s, if (!calculate_x(x, usr->hash_alg, bytes_s, len_s, usr->username_verifier,
usr->username_verifier, usr->password, usr->password_len)) usr->password, usr->password_len))
goto cleanup_and_exit; goto cleanup_and_exit;
srp_dbg_num(x, "Client calculated x: "); srp_dbg_num(x, "Client calculated x: ");
@ -1039,6 +991,7 @@ void srp_user_process_challenge(struct SRPUser *usr,
srp_dbg_num(v, "Client calculated v: "); srp_dbg_num(v, "Client calculated v: ");
// clang-format off
/* S = (B - k*(g^x)) ^ (a + ux) */ /* S = (B - k*(g^x)) ^ (a + ux) */
mpz_mul(tmp1, u, x); mpz_mul(tmp1, u, x);
mpz_add(tmp2, usr->a, tmp1); /* tmp2 = (a + ux) */ mpz_add(tmp2, usr->a, tmp1); /* tmp2 = (a + ux) */
@ -1046,23 +999,24 @@ void srp_user_process_challenge(struct SRPUser *usr,
mpz_mulm(tmp3, k, tmp1, usr->ng->N, tmp4); /* tmp3 = k*(g^x) */ mpz_mulm(tmp3, k, tmp1, usr->ng->N, tmp4); /* tmp3 = k*(g^x) */
mpz_subm(tmp1, B, tmp3, usr->ng->N, tmp4); /* tmp1 = (B - K*(g^x)) */ mpz_subm(tmp1, B, tmp3, usr->ng->N, tmp4); /* tmp1 = (B - K*(g^x)) */
mpz_powm(usr->S, tmp1, tmp2, usr->ng->N); mpz_powm(usr->S, tmp1, tmp2, usr->ng->N);
// clang-format on
hash_num(usr->hash_alg, usr->S, usr->session_key); if (!hash_num(usr->hash_alg, usr->S, usr->session_key)) goto cleanup_and_exit;
calculate_M( usr->hash_alg, usr->ng, usr->M, usr->username, bytes_s, len_s, usr->A,B, usr->session_key ); if (!calculate_M(usr->hash_alg, usr->ng, usr->M, usr->username, bytes_s, len_s,
calculate_H_AMK( usr->hash_alg, usr->H_AMK, usr->A, usr->M, usr->session_key ); usr->A, B, usr->session_key))
goto cleanup_and_exit;
if (!calculate_H_AMK(usr->hash_alg, usr->H_AMK, usr->A, usr->M, usr->session_key))
goto cleanup_and_exit;
*bytes_M = usr->M; *bytes_M = usr->M;
if (len_M) if (len_M) *len_M = hash_length(usr->hash_alg);
*len_M = hash_length( usr->hash_alg );
} else { } else {
*bytes_M = NULL; *bytes_M = NULL;
if (len_M) if (len_M) *len_M = 0;
*len_M = 0;
} }
cleanup_and_exit: cleanup_and_exit:
mpz_clear(B); mpz_clear(B);
mpz_clear(u); mpz_clear(u);
mpz_clear(x); mpz_clear(x);
@ -1074,7 +1028,6 @@ cleanup_and_exit:
mpz_clear(tmp4); mpz_clear(tmp4);
} }
void srp_user_verify_session(struct SRPUser *usr, const unsigned char *bytes_HAMK) void srp_user_verify_session(struct SRPUser *usr, const unsigned char *bytes_HAMK)
{ {
if (memcmp(usr->H_AMK, bytes_HAMK, hash_length(usr->hash_alg)) == 0) if (memcmp(usr->H_AMK, bytes_HAMK, hash_length(usr->hash_alg)) == 0)

@ -56,12 +56,10 @@
#ifndef SRP_H #ifndef SRP_H
#define SRP_H #define SRP_H
struct SRPVerifier; struct SRPVerifier;
struct SRPUser; struct SRPUser;
typedef enum typedef enum {
{
SRP_NG_1024, SRP_NG_1024,
SRP_NG_2048, SRP_NG_2048,
SRP_NG_4096, SRP_NG_4096,
@ -69,8 +67,7 @@ typedef enum
SRP_NG_CUSTOM SRP_NG_CUSTOM
} SRP_NGType; } SRP_NGType;
typedef enum typedef enum {
{
/*SRP_SHA1,*/ /*SRP_SHA1,*/
/*SRP_SHA224,*/ /*SRP_SHA224,*/
SRP_SHA256, SRP_SHA256,
@ -78,12 +75,13 @@ typedef enum
SRP_SHA512*/ SRP_SHA512*/
} SRP_HashAlgorithm; } SRP_HashAlgorithm;
typedef enum typedef enum {
{
SRP_OK,
SRP_ERR, SRP_ERR,
SRP_OK,
} SRP_Result; } SRP_Result;
// clang-format off
/* Sets the memory functions used by srp. /* Sets the memory functions used by srp.
* Note: this doesn't set the memory functions used by gmp, * Note: this doesn't set the memory functions used by gmp,
* but it is supported to have different functions for srp and gmp. * but it is supported to have different functions for srp and gmp.
@ -101,7 +99,8 @@ void srp_set_memory_functions(
* The n_hex and g_hex parameters should be 0 unless SRP_NG_CUSTOM is used for ng_type. * The n_hex and g_hex parameters should be 0 unless SRP_NG_CUSTOM is used for ng_type.
* If provided, they must contain ASCII text of the hexidecimal notation. * If provided, they must contain ASCII text of the hexidecimal notation.
* *
* If bytes_s == NULL, it is filled with random data. The caller is responsible for freeing. * If bytes_s == NULL, it is filled with random data.
* The caller is responsible for freeing.
* *
* Returns SRP_OK on success, and SRP_ERR on error. * Returns SRP_OK on success, and SRP_ERR on error.
* bytes_s might be in this case invalid, don't free it. * bytes_s might be in this case invalid, don't free it.
@ -132,42 +131,39 @@ struct SRPVerifier* srp_verifier_new(SRP_HashAlgorithm alg, SRP_NGType ng_type,
unsigned char** bytes_B, size_t *len_B, unsigned char** bytes_B, size_t *len_B,
const char* n_hex, const char* g_hex); const char* n_hex, const char* g_hex);
// clang-format on
void srp_verifier_delete(struct SRPVerifier *ver); void srp_verifier_delete(struct SRPVerifier *ver);
// srp_verifier_verify_session must have been called before // srp_verifier_verify_session must have been called before
int srp_verifier_is_authenticated(struct SRPVerifier *ver); int srp_verifier_is_authenticated(struct SRPVerifier *ver);
const char *srp_verifier_get_username(struct SRPVerifier *ver); const char *srp_verifier_get_username(struct SRPVerifier *ver);
/* key_length may be null */ /* key_length may be null */
const unsigned char* srp_verifier_get_session_key( struct SRPVerifier* ver, const unsigned char *srp_verifier_get_session_key(
size_t *key_length ); struct SRPVerifier *ver, size_t *key_length);
size_t srp_verifier_get_session_key_length(struct SRPVerifier *ver); size_t srp_verifier_get_session_key_length(struct SRPVerifier *ver);
/* Verifies session, on success, it writes bytes_HAMK. /* Verifies session, on success, it writes bytes_HAMK.
* user_M must be exactly srp_verifier_get_session_key_length() bytes in size * user_M must be exactly srp_verifier_get_session_key_length() bytes in size
*/ */
void srp_verifier_verify_session( struct SRPVerifier* ver, void srp_verifier_verify_session(
const unsigned char* user_M, unsigned char** bytes_HAMK ); struct SRPVerifier *ver, const unsigned char *user_M, unsigned char **bytes_HAMK);
/*******************************************************************************/ /*******************************************************************************/
/* The n_hex and g_hex parameters should be 0 unless SRP_NG_CUSTOM is used for ng_type */ /* The n_hex and g_hex parameters should be 0 unless SRP_NG_CUSTOM is used for ng_type */
struct SRPUser *srp_user_new(SRP_HashAlgorithm alg, SRP_NGType ng_type, struct SRPUser *srp_user_new(SRP_HashAlgorithm alg, SRP_NGType ng_type,
const char *username, const char *username_for_verifier, const char *username, const char *username_for_verifier,
const unsigned char *bytes_password, size_t len_password, const unsigned char *bytes_password, size_t len_password, const char *n_hex,
const char *n_hex, const char *g_hex); const char *g_hex);
void srp_user_delete(struct SRPUser *usr); void srp_user_delete(struct SRPUser *usr);
int srp_user_is_authenticated(struct SRPUser *usr); int srp_user_is_authenticated(struct SRPUser *usr);
const char *srp_user_get_username(struct SRPUser *usr); const char *srp_user_get_username(struct SRPUser *usr);
/* key_length may be null */ /* key_length may be null */
@ -175,7 +171,10 @@ const unsigned char* srp_user_get_session_key(struct SRPUser* usr, size_t* key_l
size_t srp_user_get_session_key_length(struct SRPUser *usr); size_t srp_user_get_session_key_length(struct SRPUser *usr);
/* Output: username, bytes_A, len_A. If you don't want it get written, set username to NULL. // clang-format off
/* Output: username, bytes_A, len_A.
* If you don't want it get written, set username to NULL.
* If bytes_a == NULL, random data is used for a. */ * If bytes_a == NULL, random data is used for a. */
SRP_Result srp_user_start_authentication(struct SRPUser* usr, char **username, SRP_Result srp_user_start_authentication(struct SRPUser* usr, char **username,
const unsigned char *bytes_a, size_t len_a, const unsigned char *bytes_a, size_t len_a,
@ -187,6 +186,7 @@ void srp_user_process_challenge(struct SRPUser *usr,
const unsigned char *bytes_s, size_t len_s, const unsigned char *bytes_s, size_t len_s,
const unsigned char *bytes_B, size_t len_B, const unsigned char *bytes_B, size_t len_B,
unsigned char **bytes_M, size_t *len_M); unsigned char **bytes_M, size_t *len_M);
// clang-format on
/* bytes_HAMK must be exactly srp_user_get_session_key_length() bytes in size */ /* bytes_HAMK must be exactly srp_user_get_session_key_length() bytes in size */
void srp_user_verify_session(struct SRPUser *usr, const unsigned char *bytes_HAMK); void srp_user_verify_session(struct SRPUser *usr, const unsigned char *bytes_HAMK);