2 * Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/opensslconf.h>
15 int main(int argc, char *argv[])
17 printf("No SRP support\n");
23 # include <openssl/srp.h>
24 # include <openssl/rand.h>
25 # include <openssl/err.h>
27 static void showbn(const char *name, const BIGNUM *bn)
31 BN_print_fp(stdout, bn);
35 # define RANDOM_SIZE 32 /* use 256 bits on each side */
37 static int run_srp(const char *username, const char *client_pass,
38 const char *server_pass)
49 BIGNUM *Kclient = NULL;
50 BIGNUM *Kserver = NULL;
51 unsigned char rand_tmp[RANDOM_SIZE];
52 /* use builtin 1024-bit params */
53 const SRP_gN *GN = SRP_get_default_gN("1024");
56 fprintf(stderr, "Failed to get SRP parameters\n");
59 /* Set up server's password entry */
60 if (!SRP_create_verifier_BN(username, server_pass, &s, &v, GN->N, GN->g)) {
61 fprintf(stderr, "Failed to create SRP verifier\n");
68 showbn("Verifier", v);
71 RAND_bytes(rand_tmp, sizeof(rand_tmp));
72 b = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
73 /* TODO - check b != 0 */
76 /* Server's first message */
77 Bpub = SRP_Calc_B(b, GN->N, GN->g, v);
80 if (!SRP_Verify_B_mod_N(Bpub, GN->N)) {
81 fprintf(stderr, "Invalid B\n");
86 RAND_bytes(rand_tmp, sizeof(rand_tmp));
87 a = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
88 /* TODO - check a != 0 */
91 /* Client's response */
92 Apub = SRP_Calc_A(a, GN->N, GN->g);
95 if (!SRP_Verify_A_mod_N(Apub, GN->N)) {
96 fprintf(stderr, "Invalid A\n");
100 /* Both sides calculate u */
101 u = SRP_Calc_u(Apub, Bpub, GN->N);
104 x = SRP_Calc_x(s, username, client_pass);
105 Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u);
106 showbn("Client's key", Kclient);
109 Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N);
110 showbn("Server's key", Kserver);
112 if (BN_cmp(Kclient, Kserver) == 0) {
115 fprintf(stderr, "Keys mismatch\n");
119 BN_clear_free(Kclient);
120 BN_clear_free(Kserver);
133 int main(int argc, char **argv)
136 bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
138 CRYPTO_set_mem_debug(1);
139 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
142 /* "Negative" test, expect a mismatch */
143 if (run_srp("alice", "password1", "password2") == 0) {
144 fprintf(stderr, "Mismatched SRP run failed\n");
148 /* "Positive" test, should pass */
149 if (run_srp("alice", "password", "password") != 0) {
150 fprintf(stderr, "Plain SRP run failed\n");
154 #ifndef OPENSSL_NO_CRYPTO_MDEBUG
155 if (CRYPTO_mem_leaks(bio_err) <= 0)