1 #include <openssl/opensslconf.h>
6 int main(int argc
, char *argv
[])
8 printf("No SRP support\n");
14 #include <openssl/srp.h>
15 #include <openssl/rand.h>
16 #include <openssl/err.h>
18 static void showbn(const char *name
, const BIGNUM
*bn
)
22 BN_print_fp(stdout
, bn
);
26 #define RANDOM_SIZE 32 /* use 256 bits on each side */
28 static int run_srp(const char *username
, const char *client_pass
, const char *server_pass
)
39 BIGNUM
*Kclient
= NULL
;
40 BIGNUM
*Kserver
= NULL
;
41 unsigned char rand_tmp
[RANDOM_SIZE
];
42 /* use builtin 1024-bit params */
43 SRP_gN
*GN
= SRP_get_default_gN("1024");
47 fprintf(stderr
, "Failed to get SRP parameters\n");
50 /* Set up server's password entry */
51 if(!SRP_create_verifier_BN(username
, server_pass
, &s
, &v
, GN
->N
, GN
->g
))
53 fprintf(stderr
, "Failed to create SRP verifier\n");
60 showbn("Verifier", v
);
63 RAND_pseudo_bytes(rand_tmp
, sizeof(rand_tmp
));
64 b
= BN_bin2bn(rand_tmp
, sizeof(rand_tmp
), NULL
);
65 /* TODO - check b != 0 */
68 /* Server's first message */
69 Bpub
= SRP_Calc_B(b
, GN
->N
, GN
->g
, v
);
72 if(!SRP_Verify_B_mod_N(Bpub
, GN
->N
))
74 fprintf(stderr
, "Invalid B\n");
79 RAND_pseudo_bytes(rand_tmp
, sizeof(rand_tmp
));
80 a
= BN_bin2bn(rand_tmp
, sizeof(rand_tmp
), NULL
);
81 /* TODO - check a != 0 */
84 /* Client's response */
85 Apub
= SRP_Calc_A(a
, GN
->N
, GN
->g
);
88 if(!SRP_Verify_A_mod_N(Apub
, GN
->N
))
90 fprintf(stderr
, "Invalid A\n");
94 /* Both sides calculate u */
95 u
= SRP_Calc_u(Apub
, Bpub
, GN
->N
);
98 x
= SRP_Calc_x(s
, username
, client_pass
);
99 Kclient
= SRP_Calc_client_key(GN
->N
, Bpub
, GN
->g
, x
, a
, u
);
100 showbn("Client's key", Kclient
);
103 Kserver
= SRP_Calc_server_key(Apub
, v
, u
, b
, GN
->N
);
104 showbn("Server's key", Kserver
);
106 if(BN_cmp(Kclient
, Kserver
) == 0)
112 fprintf(stderr
, "Keys mismatch\n");
116 BN_clear_free(Kclient
);
117 BN_clear_free(Kserver
);
130 int main(int argc
, char **argv
)
133 bio_err
= BIO_new_fp(stderr
, BIO_NOCLOSE
);
135 CRYPTO_malloc_debug_init();
136 CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL
);
137 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
139 ERR_load_crypto_strings();
141 /* "Negative" test, expect a mismatch */
142 if(run_srp("alice", "password1", "password2") == 0)
144 fprintf(stderr
, "Mismatched SRP run failed\n");
148 /* "Positive" test, should pass */
149 if(run_srp("alice", "password", "password") != 0)
151 fprintf(stderr
, "Plain SRP run failed\n");
155 CRYPTO_cleanup_all_ex_data();
156 ERR_remove_thread_state(NULL
);
158 CRYPTO_mem_leaks(bio_err
);