diff options
| -rw-r--r-- | lib/lib5.c | 138 | ||||
| -rw-r--r-- | lib/lib5.h | 22 | ||||
| -rw-r--r-- | set5/task36.c | 31 |
3 files changed, 150 insertions, 41 deletions
@@ -176,86 +176,142 @@ void dh_mitm(struct dh_param *dh) dh_get_session_key(dh); } -void srp_compute_x(int salt, unsigned char *password, char *sha1_hash) +void srp_context_init(struct srp_context *s) +{ + s->salt = BN_new(); + s->v = BN_new(); + s->g = BN_new(); + s->N = BN_new(); + s->a = BN_new(); + s->u = BN_new(); + s->k = BN_new(); + s->b = BN_new(); + s->A = BN_new(); + s->B = BN_new(); + + char *N_str = "ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024" + "e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd" + "3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec" + "6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f" + "24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361" + "c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552" + "bb9ed529077096966d670c354e4abc9804f1746c08ca237327fff" + "fffffffffffff"; + unsigned char *res = malloc(strlen(N_str)); + int len_dec = decode_hex_string(N_str, res); + + BN_bin2bn(res, len_dec, s->N); + BN_set_word(s->g, 2); + BN_set_word(s->k, 3); +} + +void srp_compute_x(BIGNUM *salt, unsigned char *password, char *sha1_hash) { unsigned char *to_hash; SHA1Context sha1; + to_hash = malloc(strlen(password) + BN_num_bytes(salt)); - to_hash = malloc(strlen(password) + sizeof(int)); - - memcpy(to_hash, &salt, sizeof(int)); - memcpy(&to_hash[sizeof(int)], password, strlen(password)); + BN_bn2bin(salt, to_hash); + memcpy(&to_hash[BN_num_bytes(salt)-1], password, strlen(password)); SHA1Reset(&sha1); SHA1Input(&sha1, to_hash, strlen(to_hash)); SHA1Result(&sha1); - memcpy(sha1_hash, &(sha1.Message_Digest), 20); } -void srp_server_init(char *email, char *password, char *g, char *N) +void srp_server_init(char *email, char *password, struct srp_context *srpc) { - int salt; char sha1_hash[20]; - mpz_t sha1_as_number; - mpz_t v; + BIGNUM *x = BN_new(); - generate_random_bytes((char *)&salt, sizeof(int)); + BN_pseudo_rand(srpc->salt, 256, -1, -1); - srp_compute_x(salt, password, sha1_hash); - modexp_bignums(g, sha1_hash, N, 16, &v); + srp_compute_x(srpc->salt, password, sha1_hash); + BN_bin2bn(sha1_hash, 20, x); + BN_mod_exp(srpc->v, srpc->g, x, srpc->N, ctx); } -void srp_client_send1(char *g) +void srp_client_send1(char *email, struct srp_context *srpc) { - // send email - - // compute public key A - //char *a + BN_pseudo_rand(srpc->a, 1024, -1, -1); + BN_mod_exp(srpc->A, srpc->g, srpc->a, srpc->N, ctx); } -void srp_server_send1() +void srp_server_send1(struct srp_context *srpc) { - // send salt - - // compute public key B + BIGNUM *t = BN_new(); + BIGNUM *t2 = BN_new(); + BN_pseudo_rand(srpc->b, 1024, -1, -1); + BN_mod_exp(t, srpc->g, srpc->b, srpc->N, ctx); + BN_mod_mul(t2, srpc->k, srpc->v, srpc->N, ctx); + BN_mod_add(srpc->B, t, t2, srpc->N, ctx); } -void srp_compute_uH(unsigned char *A, unsigned char *B) +void srp_compute_uH(struct srp_context *srpc) { SHA1Context sha1; unsigned char uH[20]; - mpz_t u; - unsigned char *res = malloc(strlen(A) + strlen(B)); - - memcpy(res, A, strlen(A)); - memcpy(&res[strlen(A)], B, strlen(B)); + unsigned char *res = malloc(BN_num_bytes(srpc->A) + BN_num_bytes(srpc->B)); + BN_bn2bin(srpc->A, res); + BN_bn2bin(srpc->B, &res[BN_num_bytes(srpc->A)-1]); SHA1Reset(&sha1); - SHA1Input(&sha1, res, (strlen(A) + strlen(B))); + SHA1Input(&sha1, res, BN_num_bytes(srpc->A) + BN_num_bytes(srpc->B)); SHA1Result(&sha1); memcpy(uH, &(sha1.Message_Digest), 20); - mpz_init_set_str(u, uH, 16); + BN_bin2bn(uH, 20, srpc->u); } -/* -void srp_client(unsigned char *salt, unsigned char *password, unsigned char *g, - unsigned char *N, unsigned char *B, unsigned char *k) + +void srp_client_prepare_k(struct srp_context *srpc, char *password) { + SHA1Context sha1; + BIGNUM *x = BN_new(); + char K[20]; char sha1_hash[20]; - mpz_t g_mp, N_mp, B_mp, k_mp, tmp_mp; - srp_compute_x(salt, password, sha1_hash); + srp_compute_x(srpc->salt, password, sha1_hash); + BN_bin2bn(sha1_hash, 20, x); + + BIGNUM *S = BN_new(); + BIGNUM *tmp = BN_new(); + BIGNUM *tmp1 = BN_new(); + BIGNUM *left= BN_new(); + BN_mod_exp(tmp1, srpc->g, x, srpc->N, ctx); + BN_mod_mul(tmp, srpc->k, tmp1, srpc->N, ctx); + BN_mod_sub(left, srpc->B, tmp, srpc->N, ctx); - mpz_init_set_str(g_mp, g, 16); - mpz_init_set_str(N_mp, N, 16); - mpz_init_set_str(B_mp, B, 16); - mpz_init_set_str(k_mp, k, 16); + BN_mod_mul(tmp, srpc->u, x, srpc->N, ctx); + BN_mod_add(tmp, tmp, srpc->a, srpc->N, ctx); + BN_mod_exp(S, left, tmp, srpc->N, ctx); - mpz_pow_ + char *s_str = malloc(BN_num_bytes(S)); + BN_bn2bin(S, s_str); + SHA1Reset(&sha1); + SHA1Input(&sha1, s_str, BN_num_bytes(S)); + SHA1Result(&sha1); + memcpy(srpc->client_K, &(sha1.Message_Digest), 20); +} + +void srp_server_prepare_k(struct srp_context *srpc) +{ + BIGNUM *S = BN_new(); + BIGNUM *tmp = BN_new(); + char K[20]; + SHA1Context sha1; + + BN_mod_exp(tmp, srpc->v, srpc->u, srpc->N, ctx); + BN_mod_mul(tmp, tmp, srpc->A, srpc->N, ctx); + BN_mod_exp(S, tmp, srpc->b, srpc->N, ctx); + char *s_str = malloc(BN_num_bytes(S)); + BN_bn2bin(S, s_str); + SHA1Reset(&sha1); + SHA1Input(&sha1, s_str, BN_num_bytes(S)); + SHA1Result(&sha1); + memcpy(srpc->server_K, &(sha1.Message_Digest), 20); } -*/ /** * in C the % operator is more the remainder than the modulo @@ -30,6 +30,21 @@ struct dh_param_bignum { BIGNUM *s2; }; +struct srp_context { + BIGNUM *salt; + BIGNUM *v; + BIGNUM *g; + BIGNUM *N; + BIGNUM *a; + BIGNUM *u; + BIGNUM *k; + BIGNUM *b; + BIGNUM *B; + BIGNUM *A; + char client_K[20]; + char server_K[20]; +}; + // global openssl context for auxaliry results BN_CTX *ctx; BIO *out; @@ -94,4 +109,11 @@ int check_co_prime(int a, int b); int __chinese_remainder_theorem(int *a, int *n, int len); int nth_root_bignum(BIGNUM *res, BIGNUM *number, BIGNUM *n); double nth_root_wr(double x, int n); +void srp_server_init(char *email, char *password, struct srp_context *srpc); +void srp_client_send1(char *email, struct srp_context *srpc); +void srp_server_send1(struct srp_context *srpc); +void srp_compute_uH(struct srp_context *srpc); +void srp_client_prepare_k(struct srp_context *srpc, char *password); +void srp_server_prepare_k(struct srp_context *srpc); +void srp_context_init(struct srp_context *s); #endif diff --git a/set5/task36.c b/set5/task36.c new file mode 100644 index 0000000..5fc05af --- /dev/null +++ b/set5/task36.c @@ -0,0 +1,31 @@ +#include "../lib/lib.h" +#include "../lib/lib2.h" +#include "../lib/lib3.h" +#include "../lib/lib4.h" +#include "../lib/lib5.h" +#include <time.h> + +int main() +{ + struct srp_context srpc; + char *email = "test@example.com"; + char *password = "123456"; + char sK[41]; + char cK[41]; + out = BIO_new(BIO_s_file()); + BIO_set_fp(out, stdout, BIO_NOCLOSE); + + ctx = BN_CTX_new(); + + srp_context_init(&srpc); + srp_server_init(email, password, &srpc); + srp_client_send1(email, &srpc); + srp_server_send1(&srpc); + srp_compute_uH(&srpc); + srp_client_prepare_k(&srpc, password); + srp_server_prepare_k(&srpc); + hex_binary_to_string(srpc.client_K, cK, 20); + hex_binary_to_string(srpc.server_K, sK, 20); + printf("cK: %s\n", cK); + printf("sK: %s\n", sK); +} |
