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