2016-05-18 02:20:24 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the OpenSSL license (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
2011-03-13 01:01:19 +08:00
|
|
|
#include <openssl/opensslconf.h>
|
2017-04-26 23:21:29 +08:00
|
|
|
# include "testutil.h"
|
2011-03-13 01:01:19 +08:00
|
|
|
|
2017-04-26 23:21:29 +08:00
|
|
|
#ifdef OPENSSL_NO_SRP
|
2015-01-22 11:40:55 +08:00
|
|
|
# include <stdio.h>
|
2011-03-13 01:01:19 +08:00
|
|
|
#else
|
|
|
|
|
2015-01-22 11:40:55 +08:00
|
|
|
# include <openssl/srp.h>
|
|
|
|
# include <openssl/rand.h>
|
|
|
|
# include <openssl/err.h>
|
2011-03-13 01:01:19 +08:00
|
|
|
|
|
|
|
static void showbn(const char *name, const BIGNUM *bn)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
2017-04-19 02:34:43 +08:00
|
|
|
BIO *b;
|
|
|
|
const char *text;
|
|
|
|
|
|
|
|
if (!TEST_ptr(b = BIO_new(BIO_s_mem())))
|
|
|
|
return;
|
|
|
|
BIO_write(b, name, strlen(name));
|
|
|
|
BIO_write(b, " = ", 3);
|
|
|
|
BN_print(b, bn);
|
|
|
|
BIO_write(b, "\0", 1);
|
|
|
|
BIO_get_mem_data(b, &text);
|
|
|
|
TEST_info("%s", text);
|
|
|
|
BIO_free(b);
|
2015-01-22 11:40:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# define RANDOM_SIZE 32 /* use 256 bits on each side */
|
|
|
|
|
|
|
|
static int run_srp(const char *username, const char *client_pass,
|
|
|
|
const char *server_pass)
|
|
|
|
{
|
2017-04-19 02:34:43 +08:00
|
|
|
int ret = 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
BIGNUM *s = NULL;
|
|
|
|
BIGNUM *v = NULL;
|
|
|
|
BIGNUM *a = NULL;
|
|
|
|
BIGNUM *b = NULL;
|
|
|
|
BIGNUM *u = NULL;
|
|
|
|
BIGNUM *x = NULL;
|
|
|
|
BIGNUM *Apub = NULL;
|
|
|
|
BIGNUM *Bpub = NULL;
|
|
|
|
BIGNUM *Kclient = NULL;
|
|
|
|
BIGNUM *Kserver = NULL;
|
|
|
|
unsigned char rand_tmp[RANDOM_SIZE];
|
|
|
|
/* use builtin 1024-bit params */
|
2017-04-19 02:34:43 +08:00
|
|
|
const SRP_gN *GN;
|
|
|
|
|
|
|
|
if (!TEST_ptr(GN = SRP_get_default_gN("1024")))
|
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
/* Set up server's password entry */
|
2017-04-19 02:34:43 +08:00
|
|
|
if (!TEST_true(SRP_create_verifier_BN(username, server_pass,
|
|
|
|
&s, &v, GN->N, GN->g)))
|
|
|
|
goto end;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
showbn("N", GN->N);
|
|
|
|
showbn("g", GN->g);
|
|
|
|
showbn("Salt", s);
|
|
|
|
showbn("Verifier", v);
|
|
|
|
|
|
|
|
/* Server random */
|
2015-02-26 19:57:37 +08:00
|
|
|
RAND_bytes(rand_tmp, sizeof(rand_tmp));
|
2015-01-22 11:40:55 +08:00
|
|
|
b = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_ne_zero(b))
|
2017-04-19 02:34:43 +08:00
|
|
|
goto end;
|
2015-01-22 11:40:55 +08:00
|
|
|
showbn("b", b);
|
|
|
|
|
|
|
|
/* Server's first message */
|
|
|
|
Bpub = SRP_Calc_B(b, GN->N, GN->g, v);
|
|
|
|
showbn("B", Bpub);
|
|
|
|
|
2017-04-19 02:34:43 +08:00
|
|
|
if (!TEST_true(SRP_Verify_B_mod_N(Bpub, GN->N)))
|
|
|
|
goto end;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
/* Client random */
|
2015-02-26 19:57:37 +08:00
|
|
|
RAND_bytes(rand_tmp, sizeof(rand_tmp));
|
2015-01-22 11:40:55 +08:00
|
|
|
a = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_ne_zero(a))
|
2017-04-19 02:34:43 +08:00
|
|
|
goto end;
|
2015-01-22 11:40:55 +08:00
|
|
|
showbn("a", a);
|
|
|
|
|
|
|
|
/* Client's response */
|
|
|
|
Apub = SRP_Calc_A(a, GN->N, GN->g);
|
|
|
|
showbn("A", Apub);
|
|
|
|
|
2017-04-19 02:34:43 +08:00
|
|
|
if (!TEST_true(SRP_Verify_A_mod_N(Apub, GN->N)))
|
|
|
|
goto end;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
/* Both sides calculate u */
|
|
|
|
u = SRP_Calc_u(Apub, Bpub, GN->N);
|
|
|
|
|
|
|
|
/* Client's key */
|
|
|
|
x = SRP_Calc_x(s, username, client_pass);
|
|
|
|
Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u);
|
|
|
|
showbn("Client's key", Kclient);
|
|
|
|
|
|
|
|
/* Server's key */
|
|
|
|
Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N);
|
|
|
|
showbn("Server's key", Kserver);
|
|
|
|
|
2017-05-08 10:09:41 +08:00
|
|
|
if (!TEST_BN_eq(Kclient, Kserver))
|
2017-04-19 02:34:43 +08:00
|
|
|
goto end;
|
|
|
|
|
|
|
|
ret = 1;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-19 02:34:43 +08:00
|
|
|
end:
|
2015-01-22 11:40:55 +08:00
|
|
|
BN_clear_free(Kclient);
|
|
|
|
BN_clear_free(Kserver);
|
|
|
|
BN_clear_free(x);
|
|
|
|
BN_free(u);
|
|
|
|
BN_free(Apub);
|
|
|
|
BN_clear_free(a);
|
|
|
|
BN_free(Bpub);
|
|
|
|
BN_clear_free(b);
|
|
|
|
BN_free(s);
|
|
|
|
BN_clear_free(v);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
2011-03-13 01:01:19 +08:00
|
|
|
|
2016-09-30 06:22:46 +08:00
|
|
|
static int check_bn(const char *name, const BIGNUM *bn, const char *hexbn)
|
|
|
|
{
|
|
|
|
BIGNUM *tmp = NULL;
|
2017-05-08 10:09:41 +08:00
|
|
|
int r;
|
2017-04-19 02:34:43 +08:00
|
|
|
|
|
|
|
if (!TEST_true(BN_hex2bn(&tmp, hexbn)))
|
2016-09-30 06:22:46 +08:00
|
|
|
return 0;
|
2017-04-19 02:34:43 +08:00
|
|
|
|
2017-05-08 10:09:41 +08:00
|
|
|
if (BN_cmp(bn, tmp) != 0)
|
|
|
|
TEST_error("unexpected %s value", name);
|
|
|
|
r = TEST_BN_eq(bn, tmp);
|
2016-09-30 06:22:46 +08:00
|
|
|
BN_free(tmp);
|
2017-05-08 10:09:41 +08:00
|
|
|
return r;
|
2016-09-30 06:22:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* SRP test vectors from RFC5054 */
|
|
|
|
static int run_srp_kat(void)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
BIGNUM *s = NULL;
|
|
|
|
BIGNUM *v = NULL;
|
|
|
|
BIGNUM *a = NULL;
|
|
|
|
BIGNUM *b = NULL;
|
|
|
|
BIGNUM *u = NULL;
|
|
|
|
BIGNUM *x = NULL;
|
|
|
|
BIGNUM *Apub = NULL;
|
|
|
|
BIGNUM *Bpub = NULL;
|
|
|
|
BIGNUM *Kclient = NULL;
|
|
|
|
BIGNUM *Kserver = NULL;
|
|
|
|
/* use builtin 1024-bit params */
|
2017-04-19 02:34:43 +08:00
|
|
|
const SRP_gN *GN;
|
|
|
|
|
|
|
|
if (!TEST_ptr(GN = SRP_get_default_gN("1024")))
|
2016-09-30 06:22:46 +08:00
|
|
|
goto err;
|
|
|
|
BN_hex2bn(&s, "BEB25379D1A8581EB5A727673A2441EE");
|
|
|
|
/* Set up server's password entry */
|
2017-04-19 02:34:43 +08:00
|
|
|
if (!TEST_true(SRP_create_verifier_BN("alice", "password123", &s, &v, GN->N,
|
|
|
|
GN->g)))
|
2016-09-30 06:22:46 +08:00
|
|
|
goto err;
|
|
|
|
|
2017-04-19 02:34:43 +08:00
|
|
|
if (!TEST_true(check_bn("v", v,
|
2016-09-30 06:22:46 +08:00
|
|
|
"7E273DE8696FFC4F4E337D05B4B375BEB0DDE1569E8FA00A9886D812"
|
|
|
|
"9BADA1F1822223CA1A605B530E379BA4729FDC59F105B4787E5186F5"
|
|
|
|
"C671085A1447B52A48CF1970B4FB6F8400BBF4CEBFBB168152E08AB5"
|
|
|
|
"EA53D15C1AFF87B2B9DA6E04E058AD51CC72BFC9033B564E26480D78"
|
2017-04-19 02:34:43 +08:00
|
|
|
"E955A5E29E7AB245DB2BE315E2099AFB")))
|
2016-09-30 06:22:46 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* Server random */
|
|
|
|
BN_hex2bn(&b, "E487CB59D31AC550471E81F00F6928E01DDA08E974A004F49E61F5D1"
|
|
|
|
"05284D20");
|
|
|
|
|
|
|
|
/* Server's first message */
|
|
|
|
Bpub = SRP_Calc_B(b, GN->N, GN->g, v);
|
2017-04-19 02:34:43 +08:00
|
|
|
if (!TEST_true(SRP_Verify_B_mod_N(Bpub, GN->N)))
|
2016-09-30 06:22:46 +08:00
|
|
|
goto err;
|
|
|
|
|
2017-04-19 02:34:43 +08:00
|
|
|
if (!TEST_true(check_bn("B", Bpub,
|
2016-09-30 06:22:46 +08:00
|
|
|
"BD0C61512C692C0CB6D041FA01BB152D4916A1E77AF46AE105393011"
|
|
|
|
"BAF38964DC46A0670DD125B95A981652236F99D9B681CBF87837EC99"
|
|
|
|
"6C6DA04453728610D0C6DDB58B318885D7D82C7F8DEB75CE7BD4FBAA"
|
|
|
|
"37089E6F9C6059F388838E7A00030B331EB76840910440B1B27AAEAE"
|
2017-04-19 02:34:43 +08:00
|
|
|
"EB4012B7D7665238A8E3FB004B117B58")))
|
2016-09-30 06:22:46 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* Client random */
|
|
|
|
BN_hex2bn(&a, "60975527035CF2AD1989806F0407210BC81EDC04E2762A56AFD529DD"
|
|
|
|
"DA2D4393");
|
|
|
|
|
|
|
|
/* Client's response */
|
|
|
|
Apub = SRP_Calc_A(a, GN->N, GN->g);
|
2017-04-19 02:34:43 +08:00
|
|
|
if (!TEST_true(SRP_Verify_A_mod_N(Apub, GN->N)))
|
|
|
|
goto err;
|
2016-09-30 06:22:46 +08:00
|
|
|
|
2017-04-19 02:34:43 +08:00
|
|
|
if (!TEST_true(check_bn("A", Apub,
|
2016-09-30 06:22:46 +08:00
|
|
|
"61D5E490F6F1B79547B0704C436F523DD0E560F0C64115BB72557EC4"
|
|
|
|
"4352E8903211C04692272D8B2D1A5358A2CF1B6E0BFCF99F921530EC"
|
|
|
|
"8E39356179EAE45E42BA92AEACED825171E1E8B9AF6D9C03E1327F44"
|
|
|
|
"BE087EF06530E69F66615261EEF54073CA11CF5858F0EDFDFE15EFEA"
|
2017-04-19 02:34:43 +08:00
|
|
|
"B349EF5D76988A3672FAC47B0769447B")))
|
2016-09-30 06:22:46 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* Both sides calculate u */
|
|
|
|
u = SRP_Calc_u(Apub, Bpub, GN->N);
|
|
|
|
|
2017-04-19 02:34:43 +08:00
|
|
|
if (!TEST_true(check_bn("u", u,
|
|
|
|
"CE38B9593487DA98554ED47D70A7AE5F462EF019")))
|
2016-09-30 06:22:46 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
/* Client's key */
|
|
|
|
x = SRP_Calc_x(s, "alice", "password123");
|
|
|
|
Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u);
|
2017-04-19 02:34:43 +08:00
|
|
|
if (!TEST_true(check_bn("Client's key", Kclient,
|
2016-09-30 06:22:46 +08:00
|
|
|
"B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D"
|
|
|
|
"233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C"
|
|
|
|
"41BB59B6D5979B5C00A172B4A2A5903A0BDCAF8A709585EB2AFAFA8F"
|
|
|
|
"3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D"
|
2017-04-19 02:34:43 +08:00
|
|
|
"C346D7E474B29EDE8A469FFECA686E5A")))
|
2016-09-30 06:22:46 +08:00
|
|
|
goto err;
|
2017-04-19 02:34:43 +08:00
|
|
|
|
2016-09-30 06:22:46 +08:00
|
|
|
/* Server's key */
|
|
|
|
Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N);
|
2017-04-19 02:34:43 +08:00
|
|
|
if (!TEST_true(check_bn("Server's key", Kserver,
|
2016-09-30 06:22:46 +08:00
|
|
|
"B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D"
|
|
|
|
"233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C"
|
|
|
|
"41BB59B6D5979B5C00A172B4A2A5903A0BDCAF8A709585EB2AFAFA8F"
|
|
|
|
"3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D"
|
2017-04-19 02:34:43 +08:00
|
|
|
"C346D7E474B29EDE8A469FFECA686E5A")))
|
2016-09-30 06:22:46 +08:00
|
|
|
goto err;
|
|
|
|
|
|
|
|
ret = 1;
|
|
|
|
|
2017-04-19 02:34:43 +08:00
|
|
|
err:
|
2016-09-30 06:22:46 +08:00
|
|
|
BN_clear_free(Kclient);
|
|
|
|
BN_clear_free(Kserver);
|
|
|
|
BN_clear_free(x);
|
|
|
|
BN_free(u);
|
|
|
|
BN_free(Apub);
|
|
|
|
BN_clear_free(a);
|
|
|
|
BN_free(Bpub);
|
|
|
|
BN_clear_free(b);
|
|
|
|
BN_free(s);
|
|
|
|
BN_clear_free(v);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-04-19 02:34:43 +08:00
|
|
|
static int run_srp_tests(void)
|
2015-01-22 11:40:55 +08:00
|
|
|
{
|
|
|
|
/* "Negative" test, expect a mismatch */
|
2017-04-19 02:34:43 +08:00
|
|
|
if (!TEST_false(run_srp("alice", "password1", "password2")))
|
|
|
|
return 0;
|
2015-01-22 11:40:55 +08:00
|
|
|
|
|
|
|
/* "Positive" test, should pass */
|
2017-04-19 02:34:43 +08:00
|
|
|
if (!TEST_true(run_srp("alice", "password", "password")))
|
|
|
|
return 0;
|
2016-09-30 06:22:46 +08:00
|
|
|
|
2017-04-19 02:34:43 +08:00
|
|
|
return 1;
|
|
|
|
}
|
2017-04-26 23:21:29 +08:00
|
|
|
#endif
|
2015-01-22 11:40:55 +08:00
|
|
|
|
2017-04-19 02:34:43 +08:00
|
|
|
void register_tests(void)
|
|
|
|
{
|
2017-04-26 23:21:29 +08:00
|
|
|
#ifdef OPENSSL_NO_SRP
|
|
|
|
printf("No SRP support\n");
|
|
|
|
#else
|
2017-04-19 02:34:43 +08:00
|
|
|
ADD_TEST(run_srp_tests);
|
|
|
|
ADD_TEST(run_srp_kat);
|
2011-03-13 01:01:19 +08:00
|
|
|
#endif
|
2017-04-26 23:21:29 +08:00
|
|
|
}
|