Convert more tests
[openssl.git] / test / srptest.c
1 /*
2  * Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the OpenSSL license (the "License").  You may not use
5  * this file except in compliance with the License.  You can obtain a copy
6  * in the file LICENSE in the source distribution or at
7  * https://www.openssl.org/source/license.html
8  */
9
10 #include <openssl/opensslconf.h>
11 #ifdef OPENSSL_NO_SRP
12
13 # include <stdio.h>
14
15 int main(int argc, char *argv[])
16 {
17     printf("No SRP support\n");
18     return (0);
19 }
20
21 #else
22
23 # include <openssl/srp.h>
24 # include <openssl/rand.h>
25 # include <openssl/err.h>
26 # include "testutil.h"
27 # include "test_main.h"
28
29 static void showbn(const char *name, const BIGNUM *bn)
30 {
31     BIO *b;
32     const char *text;
33     
34     if (!TEST_ptr(b = BIO_new(BIO_s_mem())))
35         return;
36     BIO_write(b, name, strlen(name));
37     BIO_write(b, " = ", 3);
38     BN_print(b, bn);
39     BIO_write(b, "\0", 1);
40     BIO_get_mem_data(b, &text);
41     TEST_info("%s", text);
42     BIO_free(b);
43 }
44
45 # define RANDOM_SIZE 32         /* use 256 bits on each side */
46
47 static int run_srp(const char *username, const char *client_pass,
48                    const char *server_pass)
49 {
50     int ret = 0;
51     BIGNUM *s = NULL;
52     BIGNUM *v = NULL;
53     BIGNUM *a = NULL;
54     BIGNUM *b = NULL;
55     BIGNUM *u = NULL;
56     BIGNUM *x = NULL;
57     BIGNUM *Apub = NULL;
58     BIGNUM *Bpub = NULL;
59     BIGNUM *Kclient = NULL;
60     BIGNUM *Kserver = NULL;
61     unsigned char rand_tmp[RANDOM_SIZE];
62     /* use builtin 1024-bit params */
63     const SRP_gN *GN;
64     
65     if (!TEST_ptr(GN = SRP_get_default_gN("1024")))
66         return 0;
67
68     /* Set up server's password entry */
69     if (!TEST_true(SRP_create_verifier_BN(username, server_pass,
70                                           &s, &v, GN->N, GN->g)))
71         goto end;
72
73     showbn("N", GN->N);
74     showbn("g", GN->g);
75     showbn("Salt", s);
76     showbn("Verifier", v);
77
78     /* Server random */
79     RAND_bytes(rand_tmp, sizeof(rand_tmp));
80     b = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
81     if (!TEST_false(BN_is_zero(b)))
82         goto end;
83     showbn("b", b);
84
85     /* Server's first message */
86     Bpub = SRP_Calc_B(b, GN->N, GN->g, v);
87     showbn("B", Bpub);
88
89     if (!TEST_true(SRP_Verify_B_mod_N(Bpub, GN->N)))
90         goto end;
91
92     /* Client random */
93     RAND_bytes(rand_tmp, sizeof(rand_tmp));
94     a = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
95     if (!TEST_false(BN_is_zero(a)))
96         goto end;
97     showbn("a", a);
98
99     /* Client's response */
100     Apub = SRP_Calc_A(a, GN->N, GN->g);
101     showbn("A", Apub);
102
103     if (!TEST_true(SRP_Verify_A_mod_N(Apub, GN->N)))
104         goto end;
105
106     /* Both sides calculate u */
107     u = SRP_Calc_u(Apub, Bpub, GN->N);
108
109     /* Client's key */
110     x = SRP_Calc_x(s, username, client_pass);
111     Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u);
112     showbn("Client's key", Kclient);
113
114     /* Server's key */
115     Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N);
116     showbn("Server's key", Kserver);
117
118     if (!TEST_int_eq(BN_cmp(Kclient, Kserver), 0))
119         goto end;
120
121     ret = 1;
122
123 end:
124     BN_clear_free(Kclient);
125     BN_clear_free(Kserver);
126     BN_clear_free(x);
127     BN_free(u);
128     BN_free(Apub);
129     BN_clear_free(a);
130     BN_free(Bpub);
131     BN_clear_free(b);
132     BN_free(s);
133     BN_clear_free(v);
134
135     return ret;
136 }
137
138 static int check_bn(const char *name, const BIGNUM *bn, const char *hexbn)
139 {
140     BIGNUM *tmp = NULL;
141
142     if (!TEST_true(BN_hex2bn(&tmp, hexbn)))
143         return 0;
144
145     if (!TEST_int_eq(BN_cmp(bn, tmp), 0)) {
146         TEST_info("Unexpected %s value", name);
147         showbn("expecting", tmp);
148         showbn("received", bn);
149         BN_free(tmp);
150         return 0;
151     }
152     BN_free(tmp);
153     return 1;
154 }
155
156 /* SRP test vectors from RFC5054 */
157 static int run_srp_kat(void)
158 {
159     int ret = 0;
160     BIGNUM *s = NULL;
161     BIGNUM *v = NULL;
162     BIGNUM *a = NULL;
163     BIGNUM *b = NULL;
164     BIGNUM *u = NULL;
165     BIGNUM *x = NULL;
166     BIGNUM *Apub = NULL;
167     BIGNUM *Bpub = NULL;
168     BIGNUM *Kclient = NULL;
169     BIGNUM *Kserver = NULL;
170     /* use builtin 1024-bit params */
171     const SRP_gN *GN;
172         
173     if (!TEST_ptr(GN = SRP_get_default_gN("1024")))
174         goto err;
175     BN_hex2bn(&s, "BEB25379D1A8581EB5A727673A2441EE");
176     /* Set up server's password entry */
177     if (!TEST_true(SRP_create_verifier_BN("alice", "password123", &s, &v, GN->N,
178                                 GN->g)))
179         goto err;
180
181     if (!TEST_true(check_bn("v", v,
182                  "7E273DE8696FFC4F4E337D05B4B375BEB0DDE1569E8FA00A9886D812"
183                  "9BADA1F1822223CA1A605B530E379BA4729FDC59F105B4787E5186F5"
184                  "C671085A1447B52A48CF1970B4FB6F8400BBF4CEBFBB168152E08AB5"
185                  "EA53D15C1AFF87B2B9DA6E04E058AD51CC72BFC9033B564E26480D78"
186                  "E955A5E29E7AB245DB2BE315E2099AFB")))
187         goto err;
188
189     /* Server random */
190     BN_hex2bn(&b, "E487CB59D31AC550471E81F00F6928E01DDA08E974A004F49E61F5D1"
191                   "05284D20");
192
193     /* Server's first message */
194     Bpub = SRP_Calc_B(b, GN->N, GN->g, v);
195     if (!TEST_true(SRP_Verify_B_mod_N(Bpub, GN->N)))
196         goto err;
197
198     if (!TEST_true(check_bn("B", Bpub,
199                   "BD0C61512C692C0CB6D041FA01BB152D4916A1E77AF46AE105393011"
200                   "BAF38964DC46A0670DD125B95A981652236F99D9B681CBF87837EC99"
201                   "6C6DA04453728610D0C6DDB58B318885D7D82C7F8DEB75CE7BD4FBAA"
202                   "37089E6F9C6059F388838E7A00030B331EB76840910440B1B27AAEAE"
203                   "EB4012B7D7665238A8E3FB004B117B58")))
204         goto err;
205
206     /* Client random */
207     BN_hex2bn(&a, "60975527035CF2AD1989806F0407210BC81EDC04E2762A56AFD529DD"
208                   "DA2D4393");
209
210     /* Client's response */
211     Apub = SRP_Calc_A(a, GN->N, GN->g);
212     if (!TEST_true(SRP_Verify_A_mod_N(Apub, GN->N)))
213         goto err;
214
215     if (!TEST_true(check_bn("A", Apub,
216                   "61D5E490F6F1B79547B0704C436F523DD0E560F0C64115BB72557EC4"
217                   "4352E8903211C04692272D8B2D1A5358A2CF1B6E0BFCF99F921530EC"
218                   "8E39356179EAE45E42BA92AEACED825171E1E8B9AF6D9C03E1327F44"
219                   "BE087EF06530E69F66615261EEF54073CA11CF5858F0EDFDFE15EFEA"
220                   "B349EF5D76988A3672FAC47B0769447B")))
221         goto err;
222
223     /* Both sides calculate u */
224     u = SRP_Calc_u(Apub, Bpub, GN->N);
225
226     if (!TEST_true(check_bn("u", u,
227                     "CE38B9593487DA98554ED47D70A7AE5F462EF019")))
228         goto err;
229
230     /* Client's key */
231     x = SRP_Calc_x(s, "alice", "password123");
232     Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u);
233     if (!TEST_true(check_bn("Client's key", Kclient,
234                   "B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D"
235                   "233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C"
236                   "41BB59B6D5979B5C00A172B4A2A5903A0BDCAF8A709585EB2AFAFA8F"
237                   "3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D"
238                   "C346D7E474B29EDE8A469FFECA686E5A")))
239         goto err;
240
241     /* Server's key */
242     Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N);
243     if (!TEST_true(check_bn("Server's key", Kserver,
244                   "B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D"
245                   "233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C"
246                   "41BB59B6D5979B5C00A172B4A2A5903A0BDCAF8A709585EB2AFAFA8F"
247                   "3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D"
248                   "C346D7E474B29EDE8A469FFECA686E5A")))
249         goto err;
250
251     ret = 1;
252
253 err:
254     BN_clear_free(Kclient);
255     BN_clear_free(Kserver);
256     BN_clear_free(x);
257     BN_free(u);
258     BN_free(Apub);
259     BN_clear_free(a);
260     BN_free(Bpub);
261     BN_clear_free(b);
262     BN_free(s);
263     BN_clear_free(v);
264
265     return ret;
266 }
267
268 static int run_srp_tests(void)
269 {
270     /* "Negative" test, expect a mismatch */
271     if (!TEST_false(run_srp("alice", "password1", "password2")))
272         return 0;
273
274     /* "Positive" test, should pass */
275     if (!TEST_true(run_srp("alice", "password", "password")))
276         return 0;
277
278     return 1;
279 }
280
281 void register_tests(void)
282 {
283     ADD_TEST(run_srp_tests);
284     ADD_TEST(run_srp_kat);
285 }
286 #endif