]>
git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/srp/srptest.c
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 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_pseudo_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_pseudo_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
);
129 CRYPTO_malloc_debug_init();
130 CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL
);
131 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON
);
133 ERR_load_crypto_strings();
135 /* "Negative" test, expect a mismatch */
136 if (run_srp("alice", "password1", "password2") == 0) {
137 fprintf(stderr
, "Mismatched SRP run failed\n");
141 /* "Positive" test, should pass */
142 if (run_srp("alice", "password", "password") != 0) {
143 fprintf(stderr
, "Plain SRP run failed\n");
147 CRYPTO_cleanup_all_ex_data();
148 ERR_remove_thread_state(NULL
);
150 CRYPTO_mem_leaks(bio_err
);