Format code with clang-format

Voice of world control said: let there be peace about code style.
And there was no more fighting.
Voice of world control saw that it was good.
master
est31 2016-04-10 03:20:40 +02:00
parent 2a90017b0f
commit 66323332eb
4 changed files with 300 additions and 380 deletions

12
.clang-format Normal file
View File

@ -0,0 +1,12 @@
---
BasedOnStyle: LLVM
IndentWidth: 8
UseTab: Always
IndentCaseLabels: false
AllowShortIfStatementsOnASingleLine: true
AlignAfterOpenBracket: false
ContinuationIndentWidth: 8
BreakBeforeBraces: Linux
ColumnLimit: 90
AllowShortFunctionsOnASingleLine: None
...

210
srp.c
View File

@ -80,14 +80,12 @@ void srp_set_memory_functions(
}
// clang-format on
typedef struct
{
typedef struct {
mpz_t N;
mpz_t g;
} NGConstant;
struct NGHex
{
struct NGHex {
const char *n_hex;
const char *g_hex;
};
@ -100,8 +98,7 @@ static struct NGHex global_Ng_constants[] = {
"8E495C1D6089DAD15DC7D7B46154D6B6CE8EF4AD69B15D4982559B29"
"7BCF1885C529F566660E57EC68EDBC3C05726CC02FD4CBF4976EAA9A"
"FD5138FE8376435B9FC61D2FC0EB06E3",
"2"
},
"2"},
{/* 2048 */
"AC6BDB41324A9A9BF166DE5E1389582FAF72B6651987EE07FC319294"
"3DB56050A37329CBB4A099ED8193E0757767A13DD52312AB4B03310D"
@ -113,8 +110,7 @@ static struct NGHex global_Ng_constants[] = {
"03CE53299CCC041C7BC308D82A5698F3A8D0C38271AE35F8E9DBFBB6"
"94B5C803D89F7AE435DE236D525F54759B65E372FCD68EF20FA7111F"
"9E4AFF73",
"2"
},
"2"},
{/* 4096 */
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
"8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
@ -135,8 +131,7 @@ static struct NGHex global_Ng_constants[] = {
"233BA186515BE7ED1F612970CEE2D7AFB81BDD762170481CD0069127"
"D5B05AA993B4EA988D8FDDC186FFB7DC90A6C08F4DF435C934063199"
"FFFFFFFFFFFFFFFF",
"5"
},
"5"},
{/* 8192 */
"FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E08"
"8A67CC74020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B"
@ -175,12 +170,10 @@ static struct NGHex global_Ng_constants[] = {
"359046F4EB879F924009438B481C6CD7889A002ED5EE382BC9190DA6"
"FC026E479558E4475677E9AA9E3050E2765694DFC81F56E880B96E71"
"60C980DD98EDD3DFFFFFFFFFFFFFFFFF",
"13"
},
"13"},
{0, 0} /* null sentinel */
};
static void delete_ng(NGConstant *ng)
{
if (ng) {
@ -194,8 +187,7 @@ static NGConstant *new_ng( SRP_NGType ng_type, const char *n_hex, const char *g_
{
NGConstant *ng = (NGConstant *)srp_alloc(sizeof(NGConstant));
if (!ng)
return 0;
if (!ng) return 0;
mpz_init(ng->N);
mpz_init(ng->g);
@ -217,17 +209,13 @@ static NGConstant *new_ng( SRP_NGType ng_type, const char *n_hex, const char *g_
return ng;
}
typedef union
{
typedef union {
SHA_CTX sha;
SHA256_CTX sha256;
// SHA512_CTX sha512;
} HashCTX;
struct SRPVerifier
{
struct SRPVerifier {
SRP_HashAlgorithm hash_alg;
NGConstant *ng;
@ -240,9 +228,7 @@ struct SRPVerifier
unsigned char session_key[SHA512_DIGEST_LENGTH];
};
struct SRPUser
{
struct SRPUser {
SRP_HashAlgorithm hash_alg;
NGConstant *ng;
@ -377,27 +363,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
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_mod(op, tmp, d);
}
// 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_mod(op, tmp, d);
}
// 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_mod(op, tmp, d);
}
static SRP_Result 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];
size_t len_N = mpz_num_bytes(N);
@ -405,8 +395,7 @@ static SRP_Result H_nn(mpz_t result, SRP_HashAlgorithm alg, const mpz_t N, const
size_t len_n2 = mpz_num_bytes(n2);
size_t nbytes = len_N + len_N;
unsigned char *bin = (unsigned char *)srp_alloc(nbytes);
if (!bin)
return SRP_ERR;
if (!bin) return SRP_ERR;
if (len_n1 > len_N || len_n2 > len_N) {
srp_free(bin);
return SRP_ERR;
@ -420,13 +409,13 @@ static SRP_Result H_nn(mpz_t result, SRP_HashAlgorithm alg, const mpz_t N, const
return SRP_OK;
}
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)
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];
size_t nbytes = len_n + len_bytes;
unsigned char *bin = (unsigned char *)srp_alloc(nbytes);
if (!bin)
return SRP_ERR;
if (!bin) return SRP_ERR;
memcpy(bin, n, len_n);
memcpy(bin + len_n, bytes, len_bytes);
hash(alg, bin, nbytes, buff);
@ -435,7 +424,9 @@ static SRP_Result H_ns(mpz_t result, SRP_HashAlgorithm alg, const unsigned char
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];
HashCTX ctx;
@ -456,8 +447,7 @@ static SRP_Result update_hash_n(SRP_HashAlgorithm alg, HashCTX *ctx, const mpz_t
{
size_t len = mpz_num_bytes(n);
unsigned char *n_bytes = (unsigned char *)srp_alloc(len);
if (!n_bytes)
return SRP_ERR;
if (!n_bytes) return SRP_ERR;
mpz_to_bin(n, n_bytes);
hash_update(alg, ctx, n_bytes, len);
srp_free(n_bytes);
@ -468,8 +458,7 @@ static SRP_Result hash_num( SRP_HashAlgorithm alg, const mpz_t n, unsigned char
{
int nbytes = mpz_num_bytes(n);
unsigned char *bin = (unsigned char *)srp_alloc(nbytes);
if (!bin)
return SRP_ERR;
if (!bin) return SRP_ERR;
mpz_to_bin(n, bin);
hash(alg, bin, nbytes, dest);
srp_free(bin);
@ -477,8 +466,8 @@ static SRP_Result hash_num( SRP_HashAlgorithm alg, const mpz_t n, unsigned char
}
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 mpz_t A, const mpz_t B, const unsigned char *K)
const char *I, const unsigned char *s_bytes, size_t s_len, const mpz_t A,
const mpz_t B, const unsigned char *K)
{
unsigned char H_N[SHA512_DIGEST_LENGTH];
unsigned char H_g[SHA512_DIGEST_LENGTH];
@ -493,7 +482,6 @@ static SRP_Result calculate_M(SRP_HashAlgorithm alg, NGConstant *ng, unsigned ch
hash(alg, (const unsigned char *)I, strlen(I), H_I);
for (i = 0; i < hash_len; i++)
H_xor[i] = H_N[i] ^ H_g[i];
@ -510,7 +498,8 @@ static SRP_Result calculate_M(SRP_HashAlgorithm alg, NGConstant *ng, unsigned ch
return SRP_OK;
}
static SRP_Result 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;
@ -539,19 +528,15 @@ static SRP_Result fill_buff()
return SRP_ERR;
if (!CryptGenRandom(wctx, sizeof(g_rand_buff), (BYTE *)g_rand_buff))
return SRP_ERR;
if (!CryptReleaseContext(wctx, 0))
return SRP_ERR;
if (!CryptReleaseContext(wctx, 0)) return SRP_ERR;
#else
fp = fopen("/dev/urandom", "r");
if (!fp)
return SRP_ERR;
if (!fp) return SRP_ERR;
if (fread(g_rand_buff, sizeof(g_rand_buff), 1, fp) != 1)
return SRP_ERR;
if (fclose(fp))
return SRP_ERR;
if (fread(g_rand_buff, sizeof(g_rand_buff), 1, fp) != 1) return SRP_ERR;
if (fclose(fp)) return SRP_ERR;
#endif
return SRP_OK;
}
@ -560,8 +545,7 @@ static SRP_Result mpz_fill_random(mpz_t num)
{
// was call: BN_rand(num, 256, -1, 0);
if (RAND_BUFF_MAX - g_rand_idx < 32)
if (fill_buff() != SRP_OK)
return SRP_ERR;
if (fill_buff() != SRP_OK) return SRP_ERR;
mpz_from_bin((const unsigned char *)(&g_rand_buff[g_rand_idx]), 32, num);
g_rand_idx += 32;
return SRP_OK;
@ -569,8 +553,7 @@ static SRP_Result mpz_fill_random(mpz_t num)
static SRP_Result init_random()
{
if (g_initialized)
return SRP_OK;
if (g_initialized) return SRP_OK;
SRP_Result ret = fill_buff();
g_initialized = (ret == SRP_OK);
return ret;
@ -609,8 +592,7 @@ SRP_Result srp_create_salted_verification_key( SRP_HashAlgorithm alg,
NGConstant *ng = new_ng(ng_type, n_hex, g_hex);
if (!ng)
goto error_and_exit;
if (!ng) goto error_and_exit;
if (init_random() != SRP_OK) /* Only happens once */
goto error_and_exit;
@ -619,18 +601,15 @@ SRP_Result srp_create_salted_verification_key( SRP_HashAlgorithm alg,
size_t size_to_fill = 16;
*len_s = size_to_fill;
if (RAND_BUFF_MAX - g_rand_idx < size_to_fill)
if (fill_buff() != SRP_OK)
goto error_and_exit;
if (fill_buff() != SRP_OK) goto error_and_exit;
*bytes_s = (unsigned char *)srp_alloc(size_to_fill);
if (!*bytes_s)
goto error_and_exit;
if (!*bytes_s) goto error_and_exit;
memcpy(*bytes_s, &g_rand_buff + g_rand_idx, size_to_fill);
g_rand_idx += size_to_fill;
}
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 error_and_exit;
srp_dbg_num(x, "Server calculated x: ");
@ -641,8 +620,7 @@ SRP_Result srp_create_salted_verification_key( SRP_HashAlgorithm alg,
*bytes_v = (unsigned char *)srp_alloc(*len_v);
if (!*bytes_v)
goto error_and_exit;
if (!*bytes_v) goto error_and_exit;
mpz_to_bin(v, *bytes_v);
@ -656,7 +634,6 @@ error_and_exit:
goto cleanup_and_exit;
}
// clang-format off
/* Out: bytes_B, len_B.
@ -690,13 +667,11 @@ struct SRPVerifier *srp_verifier_new(SRP_HashAlgorithm alg,
*len_B = 0;
*bytes_B = 0;
if (!ng)
goto cleanup_and_exit;
if (!ng) goto cleanup_and_exit;
ver = (struct SRPVerifier *)srp_alloc(sizeof(struct SRPVerifier));
if (!ver)
goto cleanup_and_exit;
if (!ver) goto cleanup_and_exit;
if (init_random() != SRP_OK) { /* Only happens once */
srp_free(ver);
@ -724,20 +699,17 @@ struct SRPVerifier *srp_verifier_new(SRP_HashAlgorithm alg,
if (bytes_b) {
mpz_from_bin(bytes_b, len_b, b);
} else {
if (!mpz_fill_random(b))
goto ver_cleanup_and_exit;
if (!mpz_fill_random(b)) goto ver_cleanup_and_exit;
}
if (!H_nn(k, alg, ng->N, ng->N, ng->g))
goto ver_cleanup_and_exit;
if (!H_nn(k, alg, ng->N, ng->N, ng->g)) goto ver_cleanup_and_exit;
/* B = kv + g^b */
mpz_mulm(tmp1, k, v, ng->N, tmp3);
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))
goto ver_cleanup_and_exit;
if (!H_nn(u, alg, ng->N, A, B)) goto ver_cleanup_and_exit;
srp_dbg_num(u, "Server calculated u: ");
@ -746,11 +718,10 @@ struct SRPVerifier *srp_verifier_new(SRP_HashAlgorithm alg,
mpz_mulm(tmp2, A, tmp1, ng->N, tmp3);
mpz_powm(S, tmp2, b, ng->N);
if (!hash_num(alg, S, ver->session_key))
goto ver_cleanup_and_exit;
if (!hash_num(alg, S, ver->session_key)) goto ver_cleanup_and_exit;
if (!calculate_M(alg, ng, ver->M, username, bytes_s, len_s,
A, B, ver->session_key)) {
if (!calculate_M(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)) {
@ -792,9 +763,6 @@ ver_cleanup_and_exit:
goto cleanup_and_exit;
}
void srp_verifier_delete(struct SRPVerifier *ver)
{
if (ver) {
@ -806,36 +774,31 @@ void srp_verifier_delete(struct SRPVerifier *ver)
}
}
int srp_verifier_is_authenticated(struct SRPVerifier *ver)
{
return ver->authenticated;
}
const char *srp_verifier_get_username(struct SRPVerifier *ver)
{
return ver->username;
}
const unsigned char *srp_verifier_get_session_key(struct SRPVerifier *ver, size_t *key_length)
const unsigned char *srp_verifier_get_session_key(
struct SRPVerifier *ver, size_t *key_length)
{
if (key_length)
*key_length = hash_length(ver->hash_alg);
if (key_length) *key_length = hash_length(ver->hash_alg);
return ver->session_key;
}
size_t srp_verifier_get_session_key_length(struct SRPVerifier *ver)
{
return hash_length(ver->hash_alg);
}
/* 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) {
ver->authenticated = 1;
@ -848,15 +811,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,
const char *username, const char *username_for_verifier,
const unsigned char *bytes_password, size_t len_password,
const char *n_hex, const char *g_hex)
const unsigned char *bytes_password, size_t len_password, const char *n_hex,
const char *g_hex)
{
struct SRPUser *usr = (struct SRPUser *)srp_alloc(sizeof(struct SRPUser));
size_t ulen = strlen(username) + 1;
size_t uvlen = strlen(username_for_verifier) + 1;
if (!usr)
goto err_exit;
if (!usr) goto err_exit;
if (init_random() != SRP_OK) /* Only happens once */
goto err_exit;
@ -868,16 +830,14 @@ struct SRPUser *srp_user_new(SRP_HashAlgorithm alg, SRP_NGType ng_type,
mpz_init(usr->A);
mpz_init(usr->S);
if (!usr->ng)
goto err_exit;
if (!usr->ng) goto err_exit;
usr->username = (char *)srp_alloc(ulen);
usr->username_verifier = (char *)srp_alloc(uvlen);
usr->password = (unsigned char *)srp_alloc(len_password);
usr->password_len = len_password;
if (!usr->username || !usr->password || !usr->username_verifier)
goto err_exit;
if (!usr->username || !usr->password || !usr->username_verifier) goto err_exit;
memcpy(usr->username, username, ulen);
memcpy(usr->username_verifier, username_for_verifier, uvlen);
@ -894,8 +854,7 @@ err_exit:
mpz_clear(usr->a);
mpz_clear(usr->A);
mpz_clear(usr->S);
if (usr->ng)
delete_ng(usr->ng);
if (usr->ng) delete_ng(usr->ng);
srp_free(usr->username);
srp_free(usr->username_verifier);
if (usr->password) {
@ -908,8 +867,6 @@ err_exit:
return 0;
}
void srp_user_delete(struct SRPUser *usr)
{
if (usr) {
@ -925,36 +882,29 @@ void srp_user_delete(struct SRPUser *usr)
srp_free(usr->username_verifier);
srp_free(usr->password);
if (usr->bytes_A)
srp_free(usr->bytes_A);
if (usr->bytes_A) srp_free(usr->bytes_A);
memset(usr, 0, sizeof(*usr));
srp_free(usr);
}
}
int srp_user_is_authenticated(struct SRPUser *usr)
{
return usr->authenticated;
}
const char *srp_user_get_username(struct SRPUser *usr)
{
return usr->username;
}
const unsigned char *srp_user_get_session_key(struct SRPUser *usr, size_t *key_length)
{
if (key_length)
*key_length = hash_length(usr->hash_alg);
if (key_length) *key_length = hash_length(usr->hash_alg);
return usr->session_key;
}
size_t srp_user_get_session_key_length(struct SRPUser *usr)
{
return hash_length(usr->hash_alg);
@ -970,8 +920,7 @@ SRP_Result srp_user_start_authentication(struct SRPUser *usr, char **username,
if (bytes_a) {
mpz_from_bin(bytes_a, len_a, usr->a);
} else {
if (!mpz_fill_random(usr->a))
goto error_and_exit;
if (!mpz_fill_random(usr->a)) goto error_and_exit;
}
mpz_powm(usr->A, usr->ng->g, usr->a, usr->ng->N);
@ -979,14 +928,12 @@ SRP_Result srp_user_start_authentication(struct SRPUser *usr, char **username,
*len_A = mpz_num_bytes(usr->A);
*bytes_A = (unsigned char *)srp_alloc(*len_A);
if (!*bytes_A)
goto error_and_exit;
if (!*bytes_A) goto error_and_exit;
mpz_to_bin(usr->A, *bytes_A);
usr->bytes_A = *bytes_A;
if (username)
*username = usr->username;
if (username) *username = usr->username;
return SRP_OK;
@ -997,7 +944,6 @@ error_and_exit:
return SRP_ERR;
}
// clang-format off
/* Output: bytes_M. Buffer length is SHA512_DIGEST_LENGTH */
void srp_user_process_challenge(struct SRPUser *usr,
@ -1019,13 +965,12 @@ void srp_user_process_challenge(struct SRPUser *usr,
*len_M = 0;
*bytes_M = 0;
if (!H_nn(u, usr->hash_alg, usr->ng->N, usr->A, B))
goto cleanup_and_exit;
if (!H_nn(u, usr->hash_alg, usr->ng->N, usr->A, B)) goto cleanup_and_exit;
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: ");
@ -1052,20 +997,18 @@ void srp_user_process_challenge(struct SRPUser *usr,
if (!hash_num(usr->hash_alg, usr->S, usr->session_key))
goto cleanup_and_exit;
if (!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, 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))
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;
if (len_M)
*len_M = hash_length( usr->hash_alg );
if (len_M) *len_M = hash_length(usr->hash_alg);
} else {
*bytes_M = NULL;
if (len_M)
*len_M = 0;
if (len_M) *len_M = 0;
}
cleanup_and_exit:
@ -1080,7 +1023,6 @@ cleanup_and_exit:
mpz_clear(tmp4);
}
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)

27
srp.h
View File

@ -56,12 +56,10 @@
#ifndef SRP_H
#define SRP_H
struct SRPVerifier;
struct SRPUser;
typedef enum
{
typedef enum {
SRP_NG_1024,
SRP_NG_2048,
SRP_NG_4096,
@ -69,8 +67,7 @@ typedef enum
SRP_NG_CUSTOM
} SRP_NGType;
typedef enum
{
typedef enum {
SRP_SHA1,
/*SRP_SHA224,*/
SRP_SHA256,
@ -78,8 +75,7 @@ typedef enum
SRP_SHA512*/
} SRP_HashAlgorithm;
typedef enum
{
typedef enum {
SRP_ERR,
SRP_OK,
} SRP_Result;
@ -137,42 +133,37 @@ struct SRPVerifier* srp_verifier_new(SRP_HashAlgorithm alg, SRP_NGType ng_type,
// clang-format on
void srp_verifier_delete(struct SRPVerifier *ver);
// srp_verifier_verify_session must have been called before
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,
size_t *key_length );
const unsigned char *srp_verifier_get_session_key(
struct SRPVerifier *ver, size_t *key_length);
size_t srp_verifier_get_session_key_length(struct SRPVerifier *ver);
/* Verifies session, on success, it writes bytes_HAMK.
* 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, unsigned char** bytes_HAMK );
void srp_verifier_verify_session(
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 */
struct SRPUser *srp_user_new(SRP_HashAlgorithm alg, SRP_NGType ng_type,
const char *username, const char *username_for_verifier,
const unsigned char *bytes_password, size_t len_password,
const char *n_hex, const char *g_hex);
const unsigned char *bytes_password, size_t len_password, const char *n_hex,
const char *g_hex);
void srp_user_delete(struct SRPUser *usr);
int srp_user_is_authenticated(struct SRPUser *usr);
const char *srp_user_get_username(struct SRPUser *usr);
/* key_length may be null */

View File

@ -3,10 +3,8 @@
#include <string.h>
#include <sys/time.h>
#include "srp.h"
#define NITER 100
#define TEST_HASH SRP_SHA256
#define TEST_NG SRP_NG_1024
@ -22,93 +20,71 @@ unsigned long long get_usec()
// https://tools.ietf.org/html/rfc5054#appendix-B
static const char srp_5054_salt[] = {
0xBE, 0xB2, 0x53, 0x79, 0xD1, 0xA8, 0x58, 0x1E,
0xB5, 0xA7, 0x27, 0x67, 0x3A, 0x24, 0x41, 0xEE,
0xBE, 0xB2, 0x53, 0x79, 0xD1, 0xA8, 0x58, 0x1E, 0xB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4, 0x72, 0x9F, 0xDC, 0x59, 0xF1, 0x05, 0xB4, 0x78,
0x7E, 0x51, 0x86, 0xF5, 0xC6, 0x71, 0x08, 0x5A, 0x14, 0x47, 0xB5, 0x2A, 0x48,
0xCF, 0x19, 0x70, 0xB4, 0xFB, 0x6F, 0x84, 0x00, 0xBB, 0xF4, 0xCE, 0xBF, 0xBB,
0x16, 0x81, 0x52, 0xE0, 0x8A, 0xB5, 0xEA, 0x53, 0xD1, 0x5C, 0x1A, 0xFF, 0x87,
0xB2, 0xB9, 0xDA, 0x6E, 0x04, 0xE0, 0x58, 0xAD, 0x51, 0xCC, 0x72, 0xBF, 0xC9,
0x03, 0x3B, 0x56, 0x4E, 0x26, 0x48, 0x0D, 0x78, 0xE9, 0x55, 0xA5, 0xE2, 0x9E,
0x7A, 0xB2, 0x45, 0xDB, 0x2B, 0xE3, 0x15, 0xE2, 0x09, 0x9A, 0xFB,
};
static const char srp_5054_a[] = {
0x60, 0x97, 0x55, 0x27, 0x03, 0x5C, 0xF2, 0xAD,
0x19, 0x89, 0x80, 0x6F, 0x04, 0x07, 0x21, 0x0B,
0xC8, 0x1E, 0xDC, 0x04, 0xE2, 0x76, 0x2A, 0x56,
0xAF, 0xD5, 0x29, 0xDD, 0xDA, 0x2D, 0x43, 0x93,
0x60, 0x97, 0x55, 0x27, 0x03, 0x5C, 0xF2, 0xAD, 0x19, 0x89, 0x80, 0x6F, 0x04,
0x07, 0x21, 0x0B, 0xC8, 0x1E, 0xDC, 0x04, 0xE2, 0x76, 0x2A, 0x56, 0xAF, 0xD5,
0x29, 0xDD, 0xDA, 0x2D, 0x43, 0x93,
};
static const char srp_5054_A[] = {
0x61, 0xD5, 0xE4, 0x90, 0xF6, 0xF1, 0xB7, 0x95,
0x47, 0xB0, 0x70, 0x4C, 0x43, 0x6F, 0x52, 0x3D,
0xD0, 0xE5, 0x60, 0xF0, 0xC6, 0x41, 0x15, 0xBB,
0x72, 0x55, 0x7E, 0xC4, 0x43, 0x52, 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 0xF0, 0x65, 0x30, 0xE6,
0x9F, 0x66, 0x61, 0x52, 0x61, 0xEE, 0xF5, 0x40, 0x73, 0xCA, 0x11, 0xCF, 0x58,
0x58, 0xF0, 0xED, 0xFD, 0xFE, 0x15, 0xEF, 0xEA, 0xB3, 0x49, 0xEF, 0x5D, 0x76,
0x98, 0x8A, 0x36, 0x72, 0xFA, 0xC4, 0x7B, 0x07, 0x69, 0x44, 0x7B,
};
static const char srp_5054_b[] = {
0xE4, 0x87, 0xCB, 0x59, 0xD3, 0x1A, 0xC5, 0x50,
0x47, 0x1E, 0x81, 0xF0, 0x0F, 0x69, 0x28, 0xE0,
0x1D, 0xDA, 0x08, 0xE9, 0x74, 0xA0, 0x04, 0xF4,
0x9E, 0x61, 0xF5, 0xD1, 0x05, 0x28, 0x4D, 0x20,
0xE4, 0x87, 0xCB, 0x59, 0xD3, 0x1A, 0xC5, 0x50, 0x47, 0x1E, 0x81, 0xF0, 0x0F,
0x69, 0x28, 0xE0, 0x1D, 0xDA, 0x08, 0xE9, 0x74, 0xA0, 0x04, 0xF4, 0x9E, 0x61,
0xF5, 0xD1, 0x05, 0x28, 0x4D, 0x20,
};
static const char srp_5054_B[] = {
0xBD, 0x0C, 0x61, 0x51, 0x2C, 0x69, 0x2C, 0x0C,
0xB6, 0xD0, 0x41, 0xFA, 0x01, 0xBB, 0x15, 0x2D,
0x49, 0x16, 0xA1, 0xE7, 0x7A, 0xF4, 0x6A, 0xE1,
0x05, 0x39, 0x30, 0x11, 0xBA, 0xF3, 0x89, 0x64,
0xDC, 0x46, 0xA0, 0x67, 0x0D, 0xD1, 0x25, 0xB9,
0x5A, 0x98, 0x16, 0x52, 0x23, 0x6F, 0x99, 0xD9,
0xB6, 0x81, 0xCB, 0xF8, 0x78, 0x37, 0xEC, 0x99,
0x6C, 0x6D, 0xA0, 0x44, 0x53, 0x72, 0x86, 0x10,
0xD0, 0xC6, 0xDD, 0xB5, 0x8B, 0x31, 0x88, 0x85,
0xD7, 0xD8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et)
static const char srp_5054_u[] = {
0xCE, 0x38, 0xB9, 0x59, 0x34, 0x87, 0xDA, 0x98,
0x55, 0x4E, 0xD4, 0x7D, 0x70, 0xA7, 0xAE, 0x5F,
0x46, 0x2E, 0xF0, 0x19,
0xCE, 0x38, 0xB9, 0x59, 0x34, 0x87, 0xDA, 0x98, 0x55, 0x4E, 0xD4, 0x7D, 0x70,
0xA7, 0xAE, 0x5F, 0x46, 0x2E, 0xF0, 0x19,
};
// This is SHA-1(<premaster secret>)
static const char srp_5054_S[] = {
0x01, 0x7e, 0xef, 0xa1, 0xce, 0xfc, 0x5c, 0x2e,
0x62, 0x6e, 0x21, 0x59, 0x89, 0x87, 0xf3, 0x1e,
0x0f, 0x1b, 0x11, 0xbb,
0x01, 0x7e, 0xef, 0xa1, 0xce, 0xfc, 0x5c, 0x2e, 0x62, 0x6e, 0x21, 0x59, 0x89,
0x87, 0xf3, 0x1e, 0x0f, 0x1b, 0x11, 0xbb,
};
int test_rfc_5054_compat()
@ -143,8 +119,8 @@ int test_rfc_5054_compat()
printf("Testing RFC 5054 test vectors...");
srp_create_salted_verification_key(alg, ng_type, username,
(const unsigned char *)password,
strlen(password), &bytes_s, &len_s, &bytes_v, &len_v, NULL, NULL );
(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) {
printf(" computed v doesn't match!\n");
@ -152,10 +128,10 @@ int test_rfc_5054_compat()
}
usr = srp_user_new(alg, ng_type, username, username,
(const unsigned char *)password,
strlen(password), NULL, NULL);
(const unsigned char *)password, strlen(password), NULL, NULL);
srp_user_start_authentication(usr, NULL, (unsigned char*)srp_5054_a, 32, &bytes_A, &len_A);
srp_user_start_authentication(
usr, NULL, (unsigned char *)srp_5054_a, 32, &bytes_A, &len_A);
if (memcmp(&srp_5054_A, bytes_A, len_A) != 0) {
printf(" computed A doesn't match!\n");
@ -164,8 +140,8 @@ int test_rfc_5054_compat()
/* User -> Host: (username, bytes_A) */
ver = srp_verifier_new(alg, ng_type, username, (unsigned char *)srp_5054_salt,
len_s, bytes_v, len_v, bytes_A, len_A, (unsigned char*)srp_5054_b, 32, &bytes_B,
&len_B, NULL, NULL);
len_s, bytes_v, len_v, bytes_A, len_A, (unsigned char *)srp_5054_b, 32,
&bytes_B, &len_B, NULL, NULL);
if (!bytes_B) {
printf(" SRP-6a safety check violated for B!\n");
@ -177,9 +153,9 @@ int test_rfc_5054_compat()
return 1;
}
/* Host -> User: (bytes_s, bytes_B) */
srp_user_process_challenge(usr, (unsigned char*)srp_5054_salt, len_s, bytes_B,len_B, &bytes_M, &len_M);
srp_user_process_challenge(usr, (unsigned char *)srp_5054_salt, len_s, bytes_B,
len_B, &bytes_M, &len_M);
if (!bytes_M) {
printf(" SRP-6a safety check violated for M!\n");
@ -269,26 +245,25 @@ int main(int argc, char * argv[])
}
if (srp_create_salted_verification_key(alg, ng_type, ver_unam,
(const unsigned char *)password, strlen(password),
&bytes_s, &len_s, &bytes_v, &len_v, n_hex, g_hex) != SRP_OK)
(const unsigned char *)password, strlen(password), &bytes_s, &len_s,
&bytes_v, &len_v, n_hex, g_hex) != SRP_OK)
return 1;
start = get_usec();
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);
(const unsigned char *)password, strlen(password), n_hex, g_hex);
if (srp_user_start_authentication(usr, NULL, NULL, 0, &bytes_A, &len_A) != SRP_OK) {
if (srp_user_start_authentication(usr, NULL, NULL, 0, &bytes_A, &len_A) !=
SRP_OK) {
printf("Error while starting SRP-6a authentication!\n");
goto cleanup;
}
/* User -> Host: (username, bytes_A) */
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);
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) {
printf("Verifier SRP-6a safety check violated!\n");
@ -296,7 +271,8 @@ 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);
srp_user_process_challenge(
usr, bytes_s, len_s, bytes_B, len_B, &bytes_M, &len_M);
if (!bytes_M) {
printf("User SRP-6a safety check violation!\n");
@ -327,7 +303,6 @@ cleanup:
printf("Usec per login sequence: %d\n", (int)(duration / NITER));
free((char *)bytes_s);
free((char *)bytes_v);