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