Whitespace style changes

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

105
srp.c
View File

@ -166,8 +166,7 @@ static NGConstant * new_ng( SRP_NGType ng_type, const char * n_hex, const char *
if (!ng || !ng->N || !ng->g)
return 0;
if ( ng_type != SRP_NG_CUSTOM )
{
if (ng_type != SRP_NG_CUSTOM) {
n_hex = global_Ng_constants[ ng_type ].n_hex;
g_hex = global_Ng_constants[ ng_type ].g_hex;
}
@ -180,8 +179,7 @@ static NGConstant * new_ng( SRP_NGType ng_type, const char * n_hex, const char *
static void delete_ng( NGConstant *ng )
{
if (ng)
{
if (ng) {
mpz_clear(ng->N);
mpz_clear(ng->g);
free(ng);
@ -238,67 +236,57 @@ struct SRPUser
static int hash_init(SRP_HashAlgorithm alg, HashCTX *c)
{
switch (alg)
{
switch (alg) {
case SRP_SHA1: return SHA1_Init(&c->sha);
/*case SRP_SHA224: return SHA224_Init(&c->sha256);*/
case SRP_SHA256: return SHA256_Init(&c->sha256);
/*case SRP_SHA384: return SHA384_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 )
{
switch (alg)
{
switch (alg) {
case SRP_SHA1: return SHA1_Update(&c->sha, data, len);
/*case SRP_SHA224: return SHA224_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_SHA512: return SHA512_Update( &c->sha512, data, len );*/
default:
return -1;
default: return -1;
};
}
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_SHA224: return SHA224_Final(md, &c->sha256);*/
case SRP_SHA256: return SHA256_Final(md, &c->sha256);
/*case SRP_SHA384: return SHA384_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)
{
switch (alg)
{
switch (alg) {
case SRP_SHA1: return SHA1(d, n, md);
/*case SRP_SHA224: return SHA224( d, n, md );*/
case SRP_SHA256: return SHA256(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;
};
}
static int hash_length(SRP_HashAlgorithm alg)
{
switch (alg)
{
switch (alg) {
case SRP_SHA1: return SHA_DIGEST_LENGTH;
/*case SRP_SHA224: return SHA224_DIGEST_LENGTH;*/
case SRP_SHA256: return SHA256_DIGEST_LENGTH;
/*case SRP_SHA384: return SHA384_DIGEST_LENGTH;
case SRP_SHA512: return SHA512_DIGEST_LENGTH;*/
default:
return -1;
default: return -1;
};
}
@ -348,8 +336,7 @@ static int H_nn( mpz_t result, SRP_HashAlgorithm alg, const mpz_t N, const mpz_t
unsigned char *bin = (unsigned char *) malloc(nbytes);
if (!bin)
return 0;
if (len_n1 > len_N || len_n2 > len_N)
{
if (len_n1 > len_N || len_n2 > len_N) {
free(bin);
return 0;
}
@ -381,12 +368,10 @@ static int calculate_x( mpz_t result, SRP_HashAlgorithm alg, const unsigned char
{
unsigned char ucp_hash[SHA512_DIGEST_LENGTH];
HashCTX ctx;
hash_init(alg, &ctx);
srp_dbg_data((char*) username, strlen(username), "Username 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);
@ -418,7 +403,8 @@ static void hash_num( SRP_HashAlgorithm alg, const mpz_t n, unsigned char * dest
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 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];
@ -583,7 +569,8 @@ void srp_create_salted_verification_key( SRP_HashAlgorithm alg,
}
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;
srp_dbg_num(x, "Server calculated x: ");
@ -611,7 +598,8 @@ 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
*/
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 char *username,
const unsigned char *bytes_s, int len_s,
const unsigned char *bytes_v, int len_v,
const unsigned char *bytes_A, int len_A,
@ -650,8 +638,7 @@ struct SRPVerifier * srp_verifier_new( SRP_HashAlgorithm alg, SRP_NGType ng_typ
ver->hash_alg = alg;
ver->ng = ng;
if (!ver->username)
{
if (!ver->username) {
free(ver);
ver = 0;
goto cleanup_and_exit;
@ -663,17 +650,14 @@ struct SRPVerifier * srp_verifier_new( SRP_HashAlgorithm alg, SRP_NGType ng_typ
/* SRP-6a safety check */
mpz_mod(tmp1, A, ng->N);
if ( mpz_sgn(tmp1) != 0 )
{
if (bytes_b)
{
if (mpz_sgn(tmp1) != 0) {
if (bytes_b) {
mpz_from_bin(bytes_b, len_b, b);
} else {
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);
ver = 0;
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_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);
ver = 0;
goto cleanup_and_exit;
@ -706,8 +689,7 @@ struct SRPVerifier * srp_verifier_new( SRP_HashAlgorithm alg, SRP_NGType ng_typ
*len_B = mpz_num_bytes(B);
*bytes_B = (const unsigned char *) malloc(*len_B);
if( !*bytes_B )
{
if (!*bytes_B) {
free((void*) ver->username);
free(ver);
ver = 0;
@ -734,7 +716,6 @@ struct SRPVerifier * srp_verifier_new( SRP_HashAlgorithm alg, SRP_NGType ng_typ
mpz_clear(tmp1);
mpz_clear(tmp2);
mpz_clear(tmp3);
return ver;
}
@ -743,8 +724,7 @@ struct SRPVerifier * srp_verifier_new( SRP_HashAlgorithm alg, SRP_NGType ng_typ
void srp_verifier_delete(struct SRPVerifier *ver)
{
if (ver)
{
if (ver) {
delete_ng(ver->ng);
free((char *) ver->username);
free((unsigned char *) ver->bytes_B);
@ -784,12 +764,10 @@ int srp_verifier_get_session_key_length( struct SRPVerifier *
/* 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)
{
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;
*bytes_HAMK = ver->H_AMK;
}
else
} else
*bytes_HAMK = NULL;
}
@ -838,8 +816,7 @@ struct SRPUser * srp_user_new( SRP_HashAlgorithm alg, SRP_NGType ng_type,
return usr;
err_exit:
if (usr)
{
if (usr) {
mpz_clear(usr->a);
mpz_clear(usr->A);
mpz_clear(usr->S);
@ -847,8 +824,7 @@ struct SRPUser * srp_user_new( SRP_HashAlgorithm alg, SRP_NGType ng_type,
free((void*)usr->username);
if (usr->username_verifier)
free((void*)usr->username_verifier);
if (usr->password)
{
if (usr->password) {
memset((void*)usr->password, 0, usr->password_len);
free((void*)usr->password);
}
@ -862,8 +838,7 @@ struct SRPUser * srp_user_new( SRP_HashAlgorithm alg, SRP_NGType ng_type,
void srp_user_delete(struct SRPUser *usr)
{
if( usr )
{
if(usr) {
mpz_clear(usr->a);
mpz_clear(usr->A);
mpz_clear(usr->S);
@ -898,7 +873,6 @@ const char * srp_user_get_username( struct SRPUser * usr )
}
const unsigned char *srp_user_get_session_key(struct SRPUser *usr, int *key_length)
{
if (key_length)
@ -913,14 +887,12 @@ int srp_user_get_session_key_length( struct SRPUser * usr )
}
/* Output: username, bytes_A, len_A */
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)
{
if (bytes_a)
{
if (bytes_a) {
mpz_from_bin(bytes_a, len_a, usr->a);
} else {
mpz_fill_random(usr->a);
@ -931,8 +903,7 @@ void srp_user_start_authentication( struct SRPUser * usr, const char ** usernam
*len_A = mpz_num_bytes(usr->A);
*bytes_A = (const unsigned char *) malloc(*len_A);
if (!*bytes_A)
{
if (!*bytes_A) {
*len_A = 0;
*bytes_A = 0;
*username = 0;
@ -971,7 +942,8 @@ void srp_user_process_challenge( struct SRPUser * usr,
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;
srp_dbg_num(x, "Client calculated x: ");
@ -980,8 +952,7 @@ void srp_user_process_challenge( struct SRPUser * usr,
goto cleanup_and_exit;
/* 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);
srp_dbg_num(v, "Client calculated v: ");
@ -1002,9 +973,7 @@ void srp_user_process_challenge( struct SRPUser * usr,
*bytes_M = usr->M;
if (len_M)
*len_M = hash_length( usr->hash_alg );
}
else
{
} else {
*bytes_M = NULL;
if (len_M)
*len_M = 0;

9
srp.h
View File

@ -102,7 +102,8 @@ void srp_create_salted_verification_key( SRP_HashAlgorithm alg,
*
* 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 char * username,
const unsigned char* bytes_s, int len_s,
const unsigned char* bytes_v, int len_v,
const unsigned char* bytes_A, int len_A,
@ -120,7 +121,8 @@ int srp_verifier_is_authenticated( struct SRPVerifier * ver );
const char * srp_verifier_get_username( struct SRPVerifier* ver );
/* 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 );
@ -128,8 +130,7 @@ int srp_verifier_get_session_key_length( struct SRPVerifier *
/* user_M must be exactly srp_verifier_get_session_key_length() bytes in size */
void srp_verifier_verify_session( struct SRPVerifier* ver,
const unsigned char * user_M,
const unsigned char ** bytes_HAMK );
const unsigned char* user_M, const unsigned char** bytes_HAMK );
/*******************************************************************************/

View File

@ -145,8 +145,7 @@ int test_rfc_5054_compat()
(const unsigned char *)password,
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");
return 1;
}
@ -157,8 +156,7 @@ int test_rfc_5054_compat()
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");
return 1;
}
@ -168,14 +166,12 @@ int test_rfc_5054_compat()
len_s, bytes_v, len_v, bytes_A, len_A, srp_5054_b, 32, &bytes_B,
&len_B, NULL, NULL);
if ( !bytes_B )
{
if (!bytes_B) {
printf(" SRP-6a safety check violated for B!\n");
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");
return 1;
}
@ -184,8 +180,7 @@ int test_rfc_5054_compat()
/* 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);
if ( !bytes_M )
{
if (!bytes_M) {
printf(" SRP-6a safety check violated for M!\n");
goto cleanup;
}
@ -193,8 +188,7 @@ int test_rfc_5054_compat()
/* User -> Host: (bytes_M) */
srp_verifier_verify_session(ver, bytes_M, &bytes_HAMK);
if ( !bytes_HAMK )
{
if (!bytes_HAMK) {
printf(" user authentication failed!\n");
goto cleanup;
}
@ -202,15 +196,13 @@ int test_rfc_5054_compat()
/* Host -> User: (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");
}
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");
return 1;
}
@ -226,7 +218,8 @@ int test_rfc_5054_compat()
return 0;
}
const char * test_n_hex = "EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C9C256576D674DF7496"
const char * test_n_hex =
"EEAF0AB9ADB38DD69C33F80AFA8FC5E86072618775FF3C0B9EA2314C9C256576D674DF7496"
"EA81D3383B4813D692C6E0E0D5D8E250B98BE48E495C1D6089DAD15DC7D7B46154D6B6CE8E"
"F4AD69B15D4982559B297BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA"
"9AFD5138FE8376435B9FC61D2FC0EB06E3";
@ -268,24 +261,18 @@ int main( int argc, char * argv[] )
SRP_HashAlgorithm alg = TEST_HASH;
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;
g_hex = test_g_hex;
}
srp_create_salted_verification_key(alg, ng_type, ver_unam,
(const unsigned char *)password,
strlen(password),
(const unsigned char *)password, strlen(password),
&bytes_s, &len_s, &bytes_v, &len_v, n_hex, g_hex);
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,
(const unsigned char *)password,
strlen(password), n_hex, g_hex);
@ -296,8 +283,7 @@ int main( int argc, char * argv[] )
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);
if ( !bytes_B )
{
if (!bytes_B) {
printf("Verifier SRP-6a safety check violated!\n");
goto cleanup;
}
@ -305,8 +291,7 @@ int main( int argc, char * argv[] )
/* Host -> User: (bytes_s, bytes_B) */
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");
goto cleanup;
}
@ -314,8 +299,7 @@ int main( int argc, char * argv[] )
/* User -> Host: (bytes_M) */
srp_verifier_verify_session(ver, bytes_M, &bytes_HAMK);
if ( !bytes_HAMK )
{
if (!bytes_HAMK) {
printf("User authentication failed!\n");
goto cleanup;
}
@ -323,8 +307,7 @@ int main( int argc, char * argv[] )
/* Host -> User: (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");
}