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,
29 const char *server_pass)
40 BIGNUM *Kclient = NULL;
41 BIGNUM *Kserver = NULL;
42 unsigned char rand_tmp[RANDOM_SIZE];
43 /* use builtin 1024-bit params */
44 const 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)) {
52 fprintf(stderr, "Failed to create SRP verifier\n");
59 showbn("Verifier", v);
62 RAND_bytes(rand_tmp, sizeof(rand_tmp));
63 b = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
64 /* TODO - check b != 0 */
67 /* Server's first message */
68 Bpub = SRP_Calc_B(b, GN->N, GN->g, v);
71 if (!SRP_Verify_B_mod_N(Bpub, GN->N)) {
72 fprintf(stderr, "Invalid B\n");
77 RAND_bytes(rand_tmp, sizeof(rand_tmp));
78 a = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
79 /* TODO - check a != 0 */
82 /* Client's response */
83 Apub = SRP_Calc_A(a, GN->N, GN->g);
86 if (!SRP_Verify_A_mod_N(Apub, GN->N)) {
87 fprintf(stderr, "Invalid A\n");
91 /* Both sides calculate u */
92 u = SRP_Calc_u(Apub, Bpub, GN->N);
95 x = SRP_Calc_x(s, username, client_pass);
96 Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u);
97 showbn("Client's key", Kclient);
100 Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N);
101 showbn("Server's key", Kserver);
103 if (BN_cmp(Kclient, Kserver) == 0) {
106 fprintf(stderr, "Keys mismatch\n");
110 BN_clear_free(Kclient);
111 BN_clear_free(Kserver);
124 int main(int argc, char **argv)
127 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
129 CRYPTO_set_mem_debug(1);
130 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
133 /* "Negative" test, expect a mismatch */
134 if (run_srp("alice", "password1", "password2") == 0) {
135 fprintf(stderr, "Mismatched SRP run failed\n");
139 /* "Positive" test, should pass */
140 if (run_srp("alice", "password", "password") != 0) {
141 fprintf(stderr, "Plain SRP run failed\n");
145 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
146 if (CRYPTO_mem_leaks(bio_err) <= 0)