Correct PPTP server firewall rules chain.
[tomato/davidwu.git] / release / src / router / openssl / crypto / srp / srptest.c
blob04b66b454416339d3768ee0fd097f5b2207e2a3e
1 #include <openssl/opensslconf.h>
2 #ifdef OPENSSL_NO_SRP
4 #include <stdio.h>
6 int main(int argc, char *argv[])
8 printf("No SRP support\n");
9 return(0);
12 #else
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)
20 fputs(name, stdout);
21 fputs(" = ", stdout);
22 BN_print_fp(stdout, bn);
23 putc('\n', stdout);
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)
30 int ret=-1;
31 BIGNUM *s = NULL;
32 BIGNUM *v = NULL;
33 BIGNUM *a = NULL;
34 BIGNUM *b = NULL;
35 BIGNUM *u = NULL;
36 BIGNUM *x = NULL;
37 BIGNUM *Apub = NULL;
38 BIGNUM *Bpub = NULL;
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");
45 if(GN == NULL)
47 fprintf(stderr, "Failed to get SRP parameters\n");
48 return -1;
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");
54 return -1;
57 showbn("N", GN->N);
58 showbn("g", GN->g);
59 showbn("Salt", s);
60 showbn("Verifier", v);
62 /* Server random */
63 RAND_pseudo_bytes(rand_tmp, sizeof(rand_tmp));
64 b = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
65 /* TODO - check b != 0 */
66 showbn("b", b);
68 /* Server's first message */
69 Bpub = SRP_Calc_B(b, GN->N, GN->g, v);
70 showbn("B", Bpub);
72 if(!SRP_Verify_B_mod_N(Bpub, GN->N))
74 fprintf(stderr, "Invalid B\n");
75 return -1;
78 /* Client random */
79 RAND_pseudo_bytes(rand_tmp, sizeof(rand_tmp));
80 a = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
81 /* TODO - check a != 0 */
82 showbn("a", a);
84 /* Client's response */
85 Apub = SRP_Calc_A(a, GN->N, GN->g);
86 showbn("A", Apub);
88 if(!SRP_Verify_A_mod_N(Apub, GN->N))
90 fprintf(stderr, "Invalid A\n");
91 return -1;
94 /* Both sides calculate u */
95 u = SRP_Calc_u(Apub, Bpub, GN->N);
97 /* Client's key */
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);
102 /* Server's key */
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)
108 ret = 0;
110 else
112 fprintf(stderr, "Keys mismatch\n");
113 ret = 1;
116 BN_clear_free(Kclient);
117 BN_clear_free(Kserver);
118 BN_clear_free(x);
119 BN_free(u);
120 BN_free(Apub);
121 BN_clear_free(a);
122 BN_free(Bpub);
123 BN_clear_free(b);
124 BN_free(s);
125 BN_clear_free(v);
127 return ret;
130 int main(int argc, char **argv)
132 BIO *bio_err;
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");
145 return 1;
148 /* "Positive" test, should pass */
149 if(run_srp("alice", "password", "password") != 0)
151 fprintf(stderr, "Plain SRP run failed\n");
152 return 1;
155 CRYPTO_cleanup_all_ex_data();
156 ERR_remove_thread_state(NULL);
157 ERR_free_strings();
158 CRYPTO_mem_leaks(bio_err);
160 return 0;
162 #endif