Whitespace style changes

Use tabs.
This commit is contained in:
est31 2015-05-07 20:19:30 +02:00
parent b15de7b2ad
commit 2a7ff334b3
3 changed files with 896 additions and 943 deletions

451
srp.c
View File

@ -68,8 +68,8 @@ typedef struct
struct NGHex struct NGHex
{ {
const char * n_hex; const char* n_hex;
const char * g_hex; const char* g_hex;
}; };
/* All constants here were pulled from Appendix A of RFC 5054 */ /* All constants here were pulled from Appendix A of RFC 5054 */
@ -157,33 +157,31 @@ static struct NGHex global_Ng_constants[] = {
}; };
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 *) malloc( sizeof(NGConstant) ); NGConstant *ng = (NGConstant *) malloc(sizeof(NGConstant));
mpz_init(ng->N); mpz_init(ng->N);
mpz_init(ng->g); mpz_init(ng->g);
if( !ng || !ng->N || !ng->g ) if (!ng || !ng->N || !ng->g)
return 0; 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;
} }
mpz_set_str( ng->N, n_hex, 16 ); mpz_set_str(ng->N, n_hex, 16);
mpz_set_str( ng->g, g_hex, 16 ); mpz_set_str(ng->g, g_hex, 16);
return ng; return ng;
} }
static void delete_ng( NGConstant * ng ) static void delete_ng( NGConstant *ng )
{ {
if (ng) if (ng) {
{ mpz_clear(ng->N);
mpz_clear( ng->N ); mpz_clear(ng->g);
mpz_clear( ng->g );
free(ng); free(ng);
} }
} }
@ -203,13 +201,13 @@ struct SRPVerifier
SRP_HashAlgorithm hash_alg; SRP_HashAlgorithm hash_alg;
NGConstant *ng; NGConstant *ng;
const char * username; const char *username;
const unsigned char * bytes_B; const unsigned char *bytes_B;
int authenticated; int authenticated;
unsigned char M [SHA512_DIGEST_LENGTH]; unsigned char M[SHA512_DIGEST_LENGTH];
unsigned char H_AMK [SHA512_DIGEST_LENGTH]; unsigned char H_AMK[SHA512_DIGEST_LENGTH];
unsigned char session_key [SHA512_DIGEST_LENGTH]; unsigned char session_key[SHA512_DIGEST_LENGTH];
}; };
@ -222,134 +220,123 @@ struct SRPUser
mpz_t A; mpz_t A;
mpz_t S; mpz_t S;
const unsigned char * bytes_A; const unsigned char *bytes_A;
int authenticated; int authenticated;
const char * username; const char *username;
const char * username_verifier; const char *username_verifier;
const unsigned char * password; const unsigned char *password;
int password_len; int password_len;
unsigned char M [SHA512_DIGEST_LENGTH]; unsigned char M[SHA512_DIGEST_LENGTH];
unsigned char H_AMK [SHA512_DIGEST_LENGTH]; unsigned char H_AMK[SHA512_DIGEST_LENGTH];
unsigned char session_key [SHA512_DIGEST_LENGTH]; unsigned char session_key[SHA512_DIGEST_LENGTH];
}; };
static int hash_init( SRP_HashAlgorithm alg, HashCTX *c ) static int hash_init(SRP_HashAlgorithm alg, HashCTX *c)
{ {
switch (alg) switch (alg) {
{ case SRP_SHA1: return SHA1_Init(&c->sha);
case SRP_SHA1 : return SHA1_Init( &c->sha ); /*case SRP_SHA224: return SHA224_Init(&c->sha256);*/
/*case SRP_SHA224: return SHA224_Init( &c->sha256 );*/ case SRP_SHA256: return SHA256_Init(&c->sha256);
case SRP_SHA256: return SHA256_Init( &c->sha256 ); /*case SRP_SHA384: return SHA384_Init(&c->sha512);
/*case SRP_SHA384: return SHA384_Init( &c->sha512 ); case SRP_SHA512: return SHA512_Init(&c->sha512);*/
case SRP_SHA512: return SHA512_Init( &c->sha512 );*/ default: return -1;
default:
return -1;
}; };
} }
static int hash_update( SRP_HashAlgorithm alg, HashCTX *c, const void *data, size_t len ) static int hash_update( SRP_HashAlgorithm alg, HashCTX *c, const void *data, size_t len )
{ {
switch (alg) switch (alg) {
{ case SRP_SHA1: return SHA1_Update(&c->sha, data, len);
case SRP_SHA1 : return SHA1_Update( &c->sha, data, len ); /*case SRP_SHA224: return SHA224_Update(&c->sha256, data, len);*/
/*case SRP_SHA224: return SHA224_Update( &c->sha256, data, len );*/ case SRP_SHA256: return SHA256_Update(&c->sha256, data, len);
case SRP_SHA256: return SHA256_Update( &c->sha256, data, len );
/*case SRP_SHA384: return SHA384_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 );*/ case SRP_SHA512: return SHA512_Update( &c->sha512, data, len );*/
default: default: return -1;
return -1;
}; };
} }
static int hash_final( SRP_HashAlgorithm alg, HashCTX *c, unsigned char *md ) static int hash_final( SRP_HashAlgorithm alg, HashCTX *c, unsigned char *md )
{ {
switch (alg) switch (alg) {
{ case SRP_SHA1: return SHA1_Final(md, &c->sha);
case SRP_SHA1 : return SHA1_Final( md, &c->sha ); /*case SRP_SHA224: return SHA224_Final(md, &c->sha256);*/
/*case SRP_SHA224: return SHA224_Final( md, &c->sha256 );*/ case SRP_SHA256: return SHA256_Final(md, &c->sha256);
case SRP_SHA256: return SHA256_Final( md, &c->sha256 ); /*case SRP_SHA384: return SHA384_Final(md, &c->sha512);
/*case SRP_SHA384: return SHA384_Final( md, &c->sha512 ); case SRP_SHA512: return SHA512_Final(md, &c->sha512);*/
case SRP_SHA512: return SHA512_Final( md, &c->sha512 );*/ default: return -1;
default:
return -1;
}; };
} }
static unsigned char * hash( SRP_HashAlgorithm alg, const unsigned char *d, size_t n, unsigned char *md ) static unsigned char *hash(SRP_HashAlgorithm alg, const unsigned char *d, size_t n, unsigned char *md)
{ {
switch (alg) switch (alg) {
{ case SRP_SHA1: return SHA1(d, n, md);
case SRP_SHA1 : return SHA1( d, n, md );
/*case SRP_SHA224: return SHA224( d, n, md );*/ /*case SRP_SHA224: return SHA224( d, n, md );*/
case SRP_SHA256: return SHA256( d, n, md ); case SRP_SHA256: return SHA256(d, n, md);
/*case SRP_SHA384: return SHA384( d, n, md ); /*case SRP_SHA384: return SHA384( d, n, md );
case SRP_SHA512: return SHA512( d, n, md );*/ case SRP_SHA512: return SHA512( d, n, md );*/
default: default: return 0;
return 0;
}; };
} }
static int hash_length( SRP_HashAlgorithm alg ) static int hash_length(SRP_HashAlgorithm alg)
{ {
switch (alg) switch (alg) {
{ case SRP_SHA1: return SHA_DIGEST_LENGTH;
case SRP_SHA1 : return SHA_DIGEST_LENGTH;
/*case SRP_SHA224: return SHA224_DIGEST_LENGTH;*/ /*case SRP_SHA224: return SHA224_DIGEST_LENGTH;*/
case SRP_SHA256: return SHA256_DIGEST_LENGTH; case SRP_SHA256: return SHA256_DIGEST_LENGTH;
/*case SRP_SHA384: return SHA384_DIGEST_LENGTH; /*case SRP_SHA384: return SHA384_DIGEST_LENGTH;
case SRP_SHA512: return SHA512_DIGEST_LENGTH;*/ case SRP_SHA512: return SHA512_DIGEST_LENGTH;*/
default: default: return -1;
return -1;
}; };
} }
inline static int mpz_num_bytes( const mpz_t op ) inline static int mpz_num_bytes(const mpz_t op)
{ {
return (mpz_sizeinbase (op, 2) + 7) / 8; return (mpz_sizeinbase (op, 2) + 7) / 8;
} }
inline static void mpz_to_bin( const mpz_t op, unsigned char * to ) inline static void mpz_to_bin(const mpz_t op, unsigned char *to)
{ {
mpz_export(to, NULL, 1, 1, 1, 0, op); mpz_export(to, NULL, 1, 1, 1, 0, op);
} }
inline static void mpz_from_bin( const unsigned char * s, int len, mpz_t ret ) inline static void mpz_from_bin(const unsigned char *s, int len, mpz_t ret)
{ {
mpz_import(ret, len, 1, 1, 1, 0, s); mpz_import(ret, len, 1, 1, 1, 0, s);
} }
// 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 int 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];
int len_N = mpz_num_bytes(N); int len_N = mpz_num_bytes(N);
int len_n1 = mpz_num_bytes(n1); int len_n1 = mpz_num_bytes(n1);
int len_n2 = mpz_num_bytes(n2); int len_n2 = mpz_num_bytes(n2);
int nbytes = len_N + len_N; int nbytes = len_N + len_N;
unsigned char * bin = (unsigned char *) malloc( nbytes ); unsigned char *bin = (unsigned char *) malloc(nbytes);
if (!bin) if (!bin)
return 0; return 0;
if (len_n1 > len_N || len_n2 > len_N) if (len_n1 > len_N || len_n2 > len_N) {
{
free(bin); free(bin);
return 0; return 0;
} }
@ -362,44 +349,42 @@ static int H_nn( mpz_t result, SRP_HashAlgorithm alg, const mpz_t N, const mpz_t
return 1; return 1;
} }
static int H_ns( mpz_t result, SRP_HashAlgorithm alg, const unsigned char * n, int len_n, const unsigned char * bytes, int len_bytes ) static int H_ns(mpz_t result, SRP_HashAlgorithm alg, const unsigned char *n, int len_n, const unsigned char *bytes, int len_bytes)
{ {
unsigned char buff[ SHA512_DIGEST_LENGTH ]; unsigned char buff[SHA512_DIGEST_LENGTH];
int nbytes = len_n + len_bytes; int nbytes = len_n + len_bytes;
unsigned char * bin = (unsigned char *) malloc( nbytes ); unsigned char *bin = (unsigned char *) malloc(nbytes);
if (!bin) if (!bin)
return 0; 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);
free(bin); free(bin);
mpz_from_bin(buff, hash_length(alg), result); mpz_from_bin(buff, hash_length(alg), result);
return 1; return 1;
} }
static int calculate_x( mpz_t result, SRP_HashAlgorithm alg, const unsigned char * salt, int salt_len, const char * username, const unsigned char * password, int password_len ) static int calculate_x(mpz_t result, SRP_HashAlgorithm alg, const unsigned char *salt, int salt_len, const char *username, const unsigned char *password, int password_len)
{ {
unsigned char ucp_hash[SHA512_DIGEST_LENGTH]; unsigned char ucp_hash[SHA512_DIGEST_LENGTH];
HashCTX ctx; HashCTX ctx;
hash_init(alg, &ctx);
hash_init( alg, &ctx );
srp_dbg_data((char*) username, strlen(username), "Username for x: "); srp_dbg_data((char*) username, strlen(username), "Username for x: ");
srp_dbg_data((char*) password, password_len, "Password for x: "); srp_dbg_data((char*) password, password_len, "Password for x: ");
hash_update(alg, &ctx, username, strlen(username));
hash_update(alg, &ctx, ":", 1);
hash_update(alg, &ctx, password, password_len);
hash_update( alg, &ctx, username, strlen(username) ); hash_final(alg, &ctx, ucp_hash);
hash_update( alg, &ctx, ":", 1 );
hash_update( alg, &ctx, password, password_len );
hash_final( alg, &ctx, ucp_hash ); 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 void update_hash_n(SRP_HashAlgorithm alg, HashCTX *ctx, const mpz_t n)
{ {
unsigned long len = mpz_num_bytes(n); unsigned long len = mpz_num_bytes(n);
unsigned char * n_bytes = (unsigned char *) malloc( len ); unsigned char* n_bytes = (unsigned char *) malloc(len);
if (!n_bytes) if (!n_bytes)
return; return;
mpz_to_bin(n, n_bytes); mpz_to_bin(n, n_bytes);
@ -407,60 +392,61 @@ static void update_hash_n( SRP_HashAlgorithm alg, HashCTX *ctx, const mpz_t n )
free(n_bytes); free(n_bytes);
} }
static void hash_num( SRP_HashAlgorithm alg, const mpz_t n, unsigned char * dest ) static void 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 *) malloc( nbytes ); unsigned char *bin = (unsigned char *) malloc(nbytes);
if(!bin) if(!bin)
return; return;
mpz_to_bin(n, bin); mpz_to_bin(n, bin);
hash( alg, bin, nbytes, dest ); hash(alg, bin, nbytes, dest);
free(bin); free(bin);
} }
static void calculate_M( SRP_HashAlgorithm alg, NGConstant *ng, unsigned char * dest, const char * I, const unsigned char * s_bytes, int s_len, static void calculate_M(SRP_HashAlgorithm alg, NGConstant *ng, unsigned char *dest,
const mpz_t A, const mpz_t B, const unsigned char * K ) const char *I, const unsigned char *s_bytes, int s_len,
const mpz_t A, 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];
unsigned char H_I[ SHA512_DIGEST_LENGTH ]; unsigned char H_I[SHA512_DIGEST_LENGTH];
unsigned char H_xor[ SHA512_DIGEST_LENGTH ]; unsigned char H_xor[SHA512_DIGEST_LENGTH];
HashCTX ctx; HashCTX ctx;
int i = 0; int i = 0;
int hash_len = hash_length(alg); int hash_len = hash_length(alg);
hash_num( alg, ng->N, H_N ); hash_num(alg, ng->N, H_N);
hash_num( alg, ng->g, H_g ); hash_num(alg, ng->g, H_g);
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];
hash_init( alg, &ctx ); hash_init(alg, &ctx);
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 ); update_hash_n(alg, &ctx, A);
update_hash_n( alg, &ctx, B ); update_hash_n(alg, &ctx, B);
hash_update( alg, &ctx, K, hash_len ); hash_update(alg, &ctx, K, hash_len);
hash_final( alg, &ctx, dest ); hash_final(alg, &ctx, dest);
} }
static void calculate_H_AMK( SRP_HashAlgorithm alg, unsigned char *dest, const mpz_t A, const unsigned char * M, const unsigned char * K ) static void 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 ); update_hash_n(alg, &ctx, A);
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);
} }
@ -526,12 +512,12 @@ static int fill_buff()
return 1; return 1;
} }
static void mpz_fill_random( mpz_t num ) static void 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)
fill_buff(); fill_buff();
mpz_from_bin( (const char *) (&g_rand_buff[g_rand_idx]), 32, num ); mpz_from_bin((const char *) (&g_rand_buff[g_rand_idx]), 32, num);
g_rand_idx += 32; g_rand_idx += 32;
} }
@ -560,30 +546,31 @@ static void init_random()
***********************************************************************************************************/ ***********************************************************************************************************/
void srp_create_salted_verification_key( SRP_HashAlgorithm alg, void 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, int len_password, const unsigned char *password, int len_password,
const unsigned char ** bytes_s, int * len_s, const unsigned char **bytes_s, int *len_s,
const unsigned char ** bytes_v, int * len_v, const unsigned char **bytes_v, int *len_v,
const char * n_hex, const char * g_hex ) const char *n_hex, const char *g_hex )
{ {
mpz_t v; mpz_init(v); mpz_t v; mpz_init(v);
mpz_t x; mpz_init(x); mpz_t x; mpz_init(x);
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 cleanup_and_exit; goto cleanup_and_exit;
if( bytes_s == NULL ) { if (bytes_s == NULL) {
*len_s = 16; *len_s = 16;
if ( RAND_BUFF_MAX - g_rand_idx < 16 ) if (RAND_BUFF_MAX - g_rand_idx < 16)
fill_buff(); fill_buff();
*bytes_s = (const unsigned char *) malloc( sizeof(char) * 16 ); *bytes_s = (const unsigned char *) malloc(sizeof(char) * 16);
memcpy( (unsigned char *) *bytes_s, &g_rand_buff + g_rand_idx, sizeof(char) * 16 ); memcpy((unsigned char *) *bytes_s, &g_rand_buff + g_rand_idx, sizeof(char) * 16);
g_rand_idx += 16; g_rand_idx += 16;
} }
if( !calculate_x( x, alg, *bytes_s, *len_s, username_for_verifier, password, len_password )) if (!calculate_x(x, alg, *bytes_s, *len_s, username_for_verifier,
password, len_password))
goto cleanup_and_exit; goto cleanup_and_exit;
srp_dbg_num(x, "Server calculated x: "); srp_dbg_num(x, "Server calculated x: ");
@ -592,14 +579,14 @@ void srp_create_salted_verification_key( SRP_HashAlgorithm alg,
*len_v = mpz_num_bytes(v); *len_v = mpz_num_bytes(v);
*bytes_v = (const unsigned char *) malloc( *len_v ); *bytes_v = (const unsigned char *) malloc(*len_v);
if (!bytes_v) if (!bytes_v)
goto cleanup_and_exit; goto cleanup_and_exit;
mpz_to_bin(v, (unsigned char *) *bytes_v); mpz_to_bin(v, (unsigned char *) *bytes_v);
cleanup_and_exit: cleanup_and_exit:
delete_ng( ng ); delete_ng( ng );
mpz_clear(v); mpz_clear(v);
mpz_clear(x); mpz_clear(x);
@ -611,13 +598,14 @@ void srp_create_salted_verification_key( SRP_HashAlgorithm alg,
* *
* On failure, bytes_B will be set to NULL and len_B will be set to 0 * On failure, bytes_B will be set to NULL and len_B will be set to 0
*/ */
struct SRPVerifier * srp_verifier_new( SRP_HashAlgorithm alg, SRP_NGType ng_type, const char * username, struct SRPVerifier *srp_verifier_new(SRP_HashAlgorithm alg,
const unsigned char * bytes_s, int len_s, SRP_NGType ng_type, const char *username,
const unsigned char * bytes_v, int len_v, const unsigned char *bytes_s, int len_s,
const unsigned char * bytes_A, int len_A, const unsigned char *bytes_v, int len_v,
const unsigned char * bytes_b, int len_b, const unsigned char *bytes_A, int len_A,
const unsigned char ** bytes_B, int * len_B, const unsigned char *bytes_b, int len_b,
const char * n_hex, const char * g_hex ) const unsigned char **bytes_B, int *len_B,
const char *n_hex, const char *g_hex )
{ {
mpz_t v; mpz_init(v); mpz_from_bin(bytes_v, len_v, v); mpz_t v; mpz_init(v); mpz_from_bin(bytes_v, len_v, v);
mpz_t A; mpz_init(A); mpz_from_bin(bytes_A, len_A, A); mpz_t A; mpz_init(A); mpz_from_bin(bytes_A, len_A, A);
@ -630,13 +618,13 @@ struct SRPVerifier * srp_verifier_new( SRP_HashAlgorithm alg, SRP_NGType ng_typ
mpz_t tmp2; mpz_init(tmp2); mpz_t tmp2; mpz_init(tmp2);
mpz_t tmp3; mpz_init(tmp3); mpz_t tmp3; mpz_init(tmp3);
int ulen = strlen(username) + 1; int 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;
*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 *) malloc( sizeof(struct SRPVerifier) ); ver = (struct SRPVerifier *) malloc( sizeof(struct SRPVerifier) );
@ -646,34 +634,30 @@ struct SRPVerifier * srp_verifier_new( SRP_HashAlgorithm alg, SRP_NGType ng_typ
init_random(); /* Only happens once */ init_random(); /* Only happens once */
ver->username = (char *) malloc( ulen ); ver->username = (char *) malloc(ulen);
ver->hash_alg = alg; ver->hash_alg = alg;
ver->ng = ng; ver->ng = ng;
if (!ver->username) if (!ver->username) {
{
free(ver); free(ver);
ver = 0; ver = 0;
goto cleanup_and_exit; goto cleanup_and_exit;
} }
memcpy( (char*)ver->username, username, ulen ); memcpy((char*)ver->username, username, ulen);
ver->authenticated = 0; ver->authenticated = 0;
/* SRP-6a safety check */ /* SRP-6a safety check */
mpz_mod(tmp1, A, ng->N); mpz_mod(tmp1, A, ng->N);
if ( mpz_sgn(tmp1) != 0 ) if (mpz_sgn(tmp1) != 0) {
{ if (bytes_b) {
if (bytes_b)
{
mpz_from_bin(bytes_b, len_b, b); mpz_from_bin(bytes_b, len_b, b);
} else { } else {
mpz_fill_random(b); mpz_fill_random(b);
} }
if (!H_nn(k, alg, ng->N, ng->N, ng->g)) if (!H_nn(k, alg, ng->N, ng->N, ng->g)) {
{
free(ver); free(ver);
ver = 0; ver = 0;
goto cleanup_and_exit; goto cleanup_and_exit;
@ -684,8 +668,7 @@ struct SRPVerifier * srp_verifier_new( SRP_HashAlgorithm alg, SRP_NGType ng_typ
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)) {
{
free(ver); free(ver);
ver = 0; ver = 0;
goto cleanup_and_exit; goto cleanup_and_exit;
@ -700,22 +683,21 @@ struct SRPVerifier * srp_verifier_new( SRP_HashAlgorithm alg, SRP_NGType ng_typ
hash_num(alg, S, ver->session_key); hash_num(alg, S, ver->session_key);
calculate_M( alg, ng, ver->M, username, bytes_s, len_s, A, B, ver->session_key ); calculate_M(alg, ng, ver->M, username, bytes_s, len_s, A, B, ver->session_key);
calculate_H_AMK( alg, ver->H_AMK, A, ver->M, ver->session_key ); calculate_H_AMK(alg, ver->H_AMK, A, ver->M, ver->session_key);
*len_B = mpz_num_bytes(B); *len_B = mpz_num_bytes(B);
*bytes_B = (const unsigned char *) malloc( *len_B ); *bytes_B = (const unsigned char *) malloc(*len_B);
if( !*bytes_B ) if (!*bytes_B) {
{ free((void*) ver->username);
free( (void*) ver->username ); free(ver);
free( ver );
ver = 0; ver = 0;
*len_B = 0; *len_B = 0;
goto cleanup_and_exit; goto cleanup_and_exit;
} }
mpz_to_bin( B, (unsigned char *) *bytes_B ); mpz_to_bin(B, (unsigned char *) *bytes_B);
ver->bytes_B = *bytes_B; ver->bytes_B = *bytes_B;
} else { } else {
@ -723,7 +705,7 @@ struct SRPVerifier * srp_verifier_new( SRP_HashAlgorithm alg, SRP_NGType ng_typ
ver = 0; ver = 0;
} }
cleanup_and_exit: cleanup_and_exit:
mpz_clear(v); mpz_clear(v);
mpz_clear(A); mpz_clear(A);
mpz_clear(u); mpz_clear(u);
@ -734,73 +716,69 @@ struct SRPVerifier * srp_verifier_new( SRP_HashAlgorithm alg, SRP_NGType ng_typ
mpz_clear(tmp1); mpz_clear(tmp1);
mpz_clear(tmp2); mpz_clear(tmp2);
mpz_clear(tmp3); mpz_clear(tmp3);
return ver; return ver;
} }
void srp_verifier_delete( struct SRPVerifier * ver ) void srp_verifier_delete(struct SRPVerifier *ver)
{ {
if (ver) if (ver) {
{ delete_ng(ver->ng);
delete_ng( ver->ng ); free((char *) ver->username);
free( (char *) ver->username ); free((unsigned char *) ver->bytes_B);
free( (unsigned char *) ver->bytes_B );
memset(ver, 0, sizeof(*ver)); memset(ver, 0, sizeof(*ver));
free( ver ); free(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( struct SRPVerifier * ver, int * key_length ) const unsigned char *srp_verifier_get_session_key(struct SRPVerifier *ver, int *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;
} }
int srp_verifier_get_session_key_length( struct SRPVerifier * ver ) int 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, const unsigned char ** bytes_HAMK ) void srp_verifier_verify_session(struct SRPVerifier *ver, const unsigned char *user_M, const 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;
*bytes_HAMK = ver->H_AMK; *bytes_HAMK = ver->H_AMK;
} } else
else
*bytes_HAMK = NULL; *bytes_HAMK = NULL;
} }
/*******************************************************************************/ /*******************************************************************************/
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, int len_password, const unsigned char *bytes_password, int len_password,
const char * n_hex, const char * g_hex ) const char *n_hex, const char *g_hex )
{ {
struct SRPUser *usr = (struct SRPUser *) malloc( sizeof(struct SRPUser) ); struct SRPUser *usr = (struct SRPUser *) malloc(sizeof(struct SRPUser));
int ulen = strlen(username) + 1; int ulen = strlen(username) + 1;
int uvlen = strlen(username_for_verifier) + 1; int uvlen = strlen(username_for_verifier) + 1;
@ -810,7 +788,7 @@ struct SRPUser * srp_user_new( SRP_HashAlgorithm alg, SRP_NGType ng_type,
init_random(); /* Only happens once */ init_random(); /* Only happens once */
usr->hash_alg = alg; usr->hash_alg = alg;
usr->ng = new_ng( ng_type, n_hex, g_hex ); usr->ng = new_ng(ng_type, n_hex, g_hex);
mpz_init(usr->a); mpz_init(usr->a);
mpz_init(usr->A); mpz_init(usr->A);
@ -837,9 +815,8 @@ struct SRPUser * srp_user_new( SRP_HashAlgorithm alg, SRP_NGType ng_type,
return usr; return usr;
err_exit: err_exit:
if (usr) if (usr) {
{
mpz_clear(usr->a); mpz_clear(usr->a);
mpz_clear(usr->A); mpz_clear(usr->A);
mpz_clear(usr->S); mpz_clear(usr->S);
@ -847,8 +824,7 @@ struct SRPUser * srp_user_new( SRP_HashAlgorithm alg, SRP_NGType ng_type,
free((void*)usr->username); free((void*)usr->username);
if (usr->username_verifier) if (usr->username_verifier)
free((void*)usr->username_verifier); free((void*)usr->username_verifier);
if (usr->password) if (usr->password) {
{
memset((void*)usr->password, 0, usr->password_len); memset((void*)usr->password, 0, usr->password_len);
free((void*)usr->password); free((void*)usr->password);
} }
@ -860,15 +836,14 @@ struct SRPUser * srp_user_new( SRP_HashAlgorithm alg, SRP_NGType ng_type,
void srp_user_delete( struct SRPUser * usr ) void srp_user_delete(struct SRPUser *usr)
{ {
if( usr ) if(usr) {
{ mpz_clear(usr->a);
mpz_clear( usr->a ); mpz_clear(usr->A);
mpz_clear( usr->A ); mpz_clear(usr->S);
mpz_clear( usr->S );
delete_ng( usr->ng ); delete_ng(usr->ng);
memset((void*)usr->password, 0, usr->password_len); memset((void*)usr->password, 0, usr->password_len);
@ -877,50 +852,47 @@ void srp_user_delete( struct SRPUser * usr )
free((char *)usr->password); free((char *)usr->password);
if (usr->bytes_A) if (usr->bytes_A)
free( (char *)usr->bytes_A ); free((char *)usr->bytes_A);
memset(usr, 0, sizeof(*usr)); memset(usr, 0, sizeof(*usr));
free( usr ); 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, int *key_length)
const unsigned char * srp_user_get_session_key( struct SRPUser * usr, int * 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;
} }
int srp_user_get_session_key_length( struct SRPUser * usr ) int srp_user_get_session_key_length(struct SRPUser *usr)
{ {
return hash_length( usr->hash_alg ); return hash_length(usr->hash_alg);
} }
/* Output: username, bytes_A, len_A */ /* Output: username, bytes_A, len_A */
void srp_user_start_authentication( struct SRPUser * usr, const char ** username, void srp_user_start_authentication(struct SRPUser *usr, const char **username,
const unsigned char * bytes_a, int len_a, const unsigned char *bytes_a, int len_a,
const unsigned char ** bytes_A, int * len_A ) const unsigned char **bytes_A, int *len_A)
{ {
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 {
mpz_fill_random(usr->a); mpz_fill_random(usr->a);
@ -929,29 +901,28 @@ void srp_user_start_authentication( struct SRPUser * usr, const char ** usernam
mpz_powm(usr->A, usr->ng->g, usr->a, usr->ng->N); mpz_powm(usr->A, usr->ng->g, usr->a, usr->ng->N);
*len_A = mpz_num_bytes(usr->A); *len_A = mpz_num_bytes(usr->A);
*bytes_A = (const unsigned char *) malloc( *len_A ); *bytes_A = (const unsigned char *) malloc(*len_A);
if (!*bytes_A) if (!*bytes_A) {
{
*len_A = 0; *len_A = 0;
*bytes_A = 0; *bytes_A = 0;
*username = 0; *username = 0;
return; return;
} }
mpz_to_bin( usr->A, (unsigned char *) *bytes_A ); mpz_to_bin(usr->A, (unsigned char *) *bytes_A);
usr->bytes_A = *bytes_A; usr->bytes_A = *bytes_A;
if ( username ) if (username)
*username = usr->username; *username = usr->username;
} }
/* 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, int len_s, const unsigned char *bytes_s, int len_s,
const unsigned char * bytes_B, int len_B, const unsigned char *bytes_B, int len_B,
const unsigned char ** bytes_M, int * len_M ) const unsigned char **bytes_M, int *len_M)
{ {
mpz_t B; mpz_init(B); mpz_from_bin(bytes_B, len_B, B); mpz_t B; mpz_init(B); mpz_from_bin(bytes_B, len_B, B);
mpz_t u; mpz_init(u); mpz_t u; mpz_init(u);
@ -971,7 +942,8 @@ void srp_user_process_challenge( struct SRPUser * usr,
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, usr->username_verifier, usr->password, usr->password_len )) if (!calculate_x(x, usr->hash_alg, bytes_s, len_s,
usr->username_verifier, 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: ");
@ -980,8 +952,7 @@ void srp_user_process_challenge( struct SRPUser * usr,
goto cleanup_and_exit; goto cleanup_and_exit;
/* SRP-6a safety check */ /* SRP-6a safety check */
if ( mpz_sgn(B) != 0 && mpz_sgn(u) != 0 ) if ( mpz_sgn(B) != 0 && mpz_sgn(u) != 0 ) {
{
mpz_powm(v, usr->ng->g, x, usr->ng->N); mpz_powm(v, usr->ng->g, x, usr->ng->N);
srp_dbg_num(v, "Client calculated v: "); srp_dbg_num(v, "Client calculated v: ");
@ -996,21 +967,19 @@ void srp_user_process_challenge( struct SRPUser * usr,
hash_num(usr->hash_alg, usr->S, usr->session_key); hash_num(usr->hash_alg, usr->S, usr->session_key);
calculate_M( usr->hash_alg, usr->ng, usr->M, usr->username, bytes_s, len_s, usr->A, B, usr->session_key ); calculate_M( usr->hash_alg, usr->ng, usr->M, usr->username, bytes_s, len_s, usr->A,B, usr->session_key );
calculate_H_AMK( usr->hash_alg, usr->H_AMK, usr->A, usr->M, usr->session_key ); calculate_H_AMK( usr->hash_alg, usr->H_AMK, usr->A, usr->M, usr->session_key );
*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);
@ -1024,8 +993,8 @@ void srp_user_process_challenge( struct SRPUser * usr,
} }
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)
usr->authenticated = 1; usr->authenticated = 1;
} }

61
srp.h
View File

@ -102,69 +102,70 @@ void srp_create_salted_verification_key( SRP_HashAlgorithm alg,
* *
* If bytes_b == NULL, random data is used for b. * If bytes_b == NULL, random data is used for b.
*/ */
struct SRPVerifier * srp_verifier_new( SRP_HashAlgorithm alg, SRP_NGType ng_type, const char * username, struct SRPVerifier* srp_verifier_new( SRP_HashAlgorithm alg, SRP_NGType ng_type,
const unsigned char * bytes_s, int len_s, const char * username,
const unsigned char * bytes_v, int len_v, const unsigned char* bytes_s, int len_s,
const unsigned char * bytes_A, int len_A, const unsigned char* bytes_v, int len_v,
const unsigned char * bytes_b, int len_b, const unsigned char* bytes_A, int len_A,
const unsigned char ** bytes_B, int * len_B, const unsigned char* bytes_b, int len_b,
const char * n_hex, const char * g_hex ); const unsigned char** bytes_B, int* len_B,
const char* n_hex, const char* g_hex );
void srp_verifier_delete( struct SRPVerifier * ver ); void srp_verifier_delete( struct SRPVerifier* ver );
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, int * key_length ); const unsigned char* srp_verifier_get_session_key( struct SRPVerifier* ver,
int* key_length );
int srp_verifier_get_session_key_length( struct SRPVerifier * ver ); int srp_verifier_get_session_key_length( struct SRPVerifier* ver );
/* 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( struct SRPVerifier* ver,
const unsigned char * user_M, const unsigned char* user_M, const unsigned char** bytes_HAMK );
const 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, int len_password, const unsigned char* bytes_password, int len_password,
const char * n_hex, const char * g_hex ); const char* n_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 */
const unsigned char * srp_user_get_session_key( struct SRPUser * usr, int * key_length ); const unsigned char* srp_user_get_session_key(struct SRPUser* usr, int* key_length);
int srp_user_get_session_key_length( struct SRPUser * usr ); int 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. /* 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. */
void srp_user_start_authentication( struct SRPUser * usr, const char ** username, void srp_user_start_authentication(struct SRPUser* usr, const char** username,
const unsigned char * bytes_a, int len_a, const unsigned char* bytes_a, int len_a,
const unsigned char ** bytes_A, int * len_A ); const unsigned char** bytes_A, int* len_A);
/* Output: bytes_M, len_M (len_M may be null and will always be /* Output: bytes_M, len_M (len_M may be null and will always be
* srp_user_get_session_key_length() bytes in size) */ * srp_user_get_session_key_length() bytes in size) */
void srp_user_process_challenge( struct SRPUser * usr, void srp_user_process_challenge(struct SRPUser * usr,
const unsigned char * bytes_s, int len_s, const unsigned char * bytes_s, int len_s,
const unsigned char * bytes_B, int len_B, const unsigned char * bytes_B, int len_B,
const unsigned char ** bytes_M, int * len_M ); const unsigned char ** bytes_M, int * len_M);
/* 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);
#endif /* Include Guard */ #endif /* Include Guard */

View File

@ -113,10 +113,10 @@ static const char srp_5054_S[] = {
int test_rfc_5054_compat() int test_rfc_5054_compat()
{ {
struct SRPVerifier * ver; struct SRPVerifier *ver;
struct SRPUser * usr; struct SRPUser *usr;
const unsigned char * bytes_s = (const unsigned char *) srp_5054_salt; const unsigned char *bytes_s = (const unsigned char *) srp_5054_salt;
const unsigned char * bytes_v = 0; const unsigned char * bytes_v = 0;
const unsigned char * bytes_A = 0; const unsigned char * bytes_A = 0;
const unsigned char * bytes_B = 0; const unsigned char * bytes_B = 0;
@ -133,120 +133,113 @@ int test_rfc_5054_compat()
int len_S = 0; int len_S = 0;
int i; int i;
const char * username = "alice"; const char *username = "alice";
const char * password = "password123"; const char *password = "password123";
SRP_HashAlgorithm alg = SRP_SHA1; SRP_HashAlgorithm alg = SRP_SHA1;
SRP_NGType ng_type = SRP_NG_1024; //TEST_NG; SRP_NGType ng_type = SRP_NG_1024; //TEST_NG;
printf("Testing RFC 5054 test vectors..."); printf("Testing RFC 5054 test vectors...");
srp_create_salted_verification_key( alg, ng_type, username, srp_create_salted_verification_key(alg, ng_type, username,
(const unsigned char *)password, (const unsigned char *)password,
strlen(password), &bytes_s, &len_s, &bytes_v, &len_v, NULL, NULL ); strlen(password), &bytes_s, &len_s, &bytes_v, &len_v, NULL, NULL );
if (len_v != 128 || memcmp(&srp_5054_v, bytes_v, len_v) != 0) if (len_v != 128 || memcmp(&srp_5054_v, bytes_v, len_v) != 0) {
{
printf(" computed v doesn't match!\n"); printf(" computed v doesn't match!\n");
return 1; return 1;
} }
usr = srp_user_new( alg, ng_type, username, username, usr = srp_user_new(alg, ng_type, username, username,
(const unsigned char *)password, (const unsigned char *)password,
strlen(password), NULL, NULL ); strlen(password), NULL, NULL);
srp_user_start_authentication( usr, NULL, srp_5054_a, 32, &bytes_A, &len_A ); srp_user_start_authentication(usr, NULL, srp_5054_a, 32, &bytes_A, &len_A);
if (memcmp(&srp_5054_A, bytes_A, len_A) != 0) if (memcmp(&srp_5054_A, bytes_A, len_A) != 0) {
{
printf(" computed A doesn't match!\n"); printf(" computed A doesn't match!\n");
return 1; return 1;
} }
/* User -> Host: (username, bytes_A) */ /* User -> Host: (username, bytes_A) */
ver = srp_verifier_new( alg, ng_type, username, (const unsigned char *) srp_5054_salt, ver = srp_verifier_new(alg, ng_type, username, (const unsigned char *) srp_5054_salt,
len_s, bytes_v, len_v, bytes_A, len_A, srp_5054_b, 32, &bytes_B, len_s, bytes_v, len_v, bytes_A, len_A, srp_5054_b, 32, &bytes_B,
&len_B, NULL, NULL ); &len_B, NULL, NULL);
if ( !bytes_B ) if (!bytes_B) {
{
printf(" SRP-6a safety check violated for B!\n"); printf(" SRP-6a safety check violated for B!\n");
return 1; return 1;
} }
if (memcmp(&srp_5054_B, bytes_B, len_B) != 0) if (memcmp(&srp_5054_B, bytes_B, len_B) != 0) {
{
printf(" computed B doesn't match!\n"); printf(" computed B doesn't match!\n");
return 1; return 1;
} }
/* Host -> User: (bytes_s, bytes_B) */ /* Host -> User: (bytes_s, bytes_B) */
srp_user_process_challenge( usr, (const unsigned char *) srp_5054_salt, len_s, bytes_B, len_B, &bytes_M, &len_M ); srp_user_process_challenge(usr, (const unsigned char *) srp_5054_salt, len_s, bytes_B,len_B, &bytes_M, &len_M);
if ( !bytes_M ) if (!bytes_M) {
{
printf(" SRP-6a safety check violated for M!\n"); printf(" SRP-6a safety check violated for M!\n");
goto cleanup; goto cleanup;
} }
/* User -> Host: (bytes_M) */ /* User -> Host: (bytes_M) */
srp_verifier_verify_session( ver, bytes_M, &bytes_HAMK ); srp_verifier_verify_session(ver, bytes_M, &bytes_HAMK);
if ( !bytes_HAMK ) if (!bytes_HAMK) {
{
printf(" user authentication failed!\n"); printf(" user authentication failed!\n");
goto cleanup; goto cleanup;
} }
/* Host -> User: (HAMK) */ /* Host -> User: (HAMK) */
srp_user_verify_session( usr, bytes_HAMK ); srp_user_verify_session(usr, bytes_HAMK);
if ( !srp_user_is_authenticated(usr) ) if (!srp_user_is_authenticated(usr)) {
{
printf(" server authentication failed!\n"); printf(" server authentication failed!\n");
} }
bytes_S = srp_verifier_get_session_key(ver, &len_S); bytes_S = srp_verifier_get_session_key(ver, &len_S);
if (memcmp(&srp_5054_S, bytes_S, len_S) != 0) if (memcmp(&srp_5054_S, bytes_S, len_S) != 0) {
{
printf(" computed session key doesn't match!\n"); printf(" computed session key doesn't match!\n");
return 1; return 1;
} }
printf(" success.\n"); printf(" success.\n");
cleanup: cleanup:
srp_verifier_delete( ver ); srp_verifier_delete(ver);
srp_user_delete( usr ); srp_user_delete(usr);
free( (char *)bytes_v ); free((char *)bytes_v);
return 0; return 0;
} }
const char * test_n_hex = "EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C9C256576D674DF7496" const char * test_n_hex =
"EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C9C256576D674DF7496"
"EA81D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8E" "EA81D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8E"
"F4AD69B15D4982559B297BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA" "F4AD69B15D4982559B297BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA"
"9AFD5138FE8376435B9FC61D2FC0EB06E3"; "9AFD5138FE8376435B9FC61D2FC0EB06E3";
const char * test_g_hex = "2"; const char * test_g_hex = "2";
int main( int argc, char * argv[] ) int main(int argc, char * argv[])
{ {
test_rfc_5054_compat(); test_rfc_5054_compat();
printf("Performing the speedtest.\n"); printf("Performing the speedtest.\n");
struct SRPVerifier * ver; struct SRPVerifier *ver;
struct SRPUser * usr; struct SRPUser *usr;
const unsigned char * bytes_s = 0; const unsigned char *bytes_s = 0;
const unsigned char * bytes_v = 0; const unsigned char *bytes_v = 0;
const unsigned char * bytes_A = 0; const unsigned char *bytes_A = 0;
const unsigned char * bytes_B = 0; const unsigned char *bytes_B = 0;
const unsigned char * bytes_M = 0; const unsigned char *bytes_M = 0;
const unsigned char * bytes_HAMK = 0; const unsigned char *bytes_HAMK = 0;
int len_s = 0; int len_s = 0;
int len_v = 0; int len_v = 0;
@ -258,79 +251,69 @@ int main( int argc, char * argv[] )
unsigned long long start; unsigned long long start;
unsigned long long duration; unsigned long long duration;
const char * username = "TestUser"; const char *username = "TestUser";
const char * ver_unam = "testuser"; const char *ver_unam = "testuser";
const char * password = "password"; const char *password = "password";
const char * n_hex = 0; const char *n_hex = 0;
const char * g_hex = 0; const char *g_hex = 0;
SRP_HashAlgorithm alg = TEST_HASH; SRP_HashAlgorithm alg = TEST_HASH;
SRP_NGType ng_type = SRP_NG_8192; //TEST_NG; SRP_NGType ng_type = SRP_NG_8192; //TEST_NG;
if (ng_type == SRP_NG_CUSTOM) if (ng_type == SRP_NG_CUSTOM) {
{
n_hex = test_n_hex; n_hex = test_n_hex;
g_hex = test_g_hex; g_hex = test_g_hex;
} }
srp_create_salted_verification_key(alg, ng_type, ver_unam,
srp_create_salted_verification_key( alg, ng_type, ver_unam, (const unsigned char *)password, strlen(password),
(const unsigned char *)password, &bytes_s, &len_s, &bytes_v, &len_v, n_hex, g_hex);
strlen(password),
&bytes_s, &len_s, &bytes_v, &len_v, n_hex, g_hex );
start = get_usec(); start = get_usec();
for( i = 0; i < NITER; i++ ) for (i = 0; i < NITER; i++) {
{ usr = srp_user_new(alg, ng_type, username, ver_unam,
usr = srp_user_new( alg, ng_type, username, ver_unam,
(const unsigned char *)password, (const unsigned char *)password,
strlen(password), n_hex, g_hex ); strlen(password), n_hex, g_hex);
srp_user_start_authentication( usr, NULL, NULL, 0, &bytes_A, &len_A ); srp_user_start_authentication(usr, NULL, NULL, 0, &bytes_A, &len_A);
/* User -> Host: (username, bytes_A) */ /* User -> Host: (username, bytes_A) */
ver = srp_verifier_new( alg, ng_type, username, bytes_s, len_s, bytes_v, len_v, ver = srp_verifier_new(alg, ng_type, username, bytes_s, len_s, bytes_v, len_v,
bytes_A, len_A, NULL, 0, & bytes_B, &len_B, n_hex, g_hex ); bytes_A, len_A, NULL, 0, & bytes_B, &len_B, n_hex, g_hex);
if ( !bytes_B ) if (!bytes_B) {
{
printf("Verifier SRP-6a safety check violated!\n"); printf("Verifier SRP-6a safety check violated!\n");
goto cleanup; goto cleanup;
} }
/* Host -> User: (bytes_s, bytes_B) */ /* Host -> User: (bytes_s, bytes_B) */
srp_user_process_challenge( usr, bytes_s, len_s, bytes_B, len_B, &bytes_M, &len_M ); srp_user_process_challenge(usr, bytes_s, len_s, bytes_B, len_B, &bytes_M, &len_M);
if ( !bytes_M ) if (!bytes_M) {
{
printf("User SRP-6a safety check violation!\n"); printf("User SRP-6a safety check violation!\n");
goto cleanup; goto cleanup;
} }
/* User -> Host: (bytes_M) */ /* User -> Host: (bytes_M) */
srp_verifier_verify_session( ver, bytes_M, &bytes_HAMK ); srp_verifier_verify_session(ver, bytes_M, &bytes_HAMK);
if ( !bytes_HAMK ) if (!bytes_HAMK) {
{
printf("User authentication failed!\n"); printf("User authentication failed!\n");
goto cleanup; goto cleanup;
} }
/* Host -> User: (HAMK) */ /* Host -> User: (HAMK) */
srp_user_verify_session( usr, bytes_HAMK ); srp_user_verify_session(usr, bytes_HAMK);
if ( !srp_user_is_authenticated(usr) ) if (!srp_user_is_authenticated(usr)) {
{
printf("Server authentication failed!\n"); printf("Server authentication failed!\n");
} }
cleanup: cleanup:
srp_verifier_delete( ver ); srp_verifier_delete(ver);
srp_user_delete( usr ); srp_user_delete(usr);
} }
duration = get_usec() - start; duration = get_usec() - start;
@ -338,8 +321,8 @@ cleanup:
printf("Usec per call: %d\n", (int)(duration / NITER)); printf("Usec per call: %d\n", (int)(duration / NITER));
free( (char *)bytes_s ); free((char *)bytes_s);
free( (char *)bytes_v ); free((char *)bytes_v);
return 0; return 0;
} }