srptest.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #include <openssl/opensslconf.h>
  10. #ifdef OPENSSL_NO_SRP
  11. # include <stdio.h>
  12. int main(int argc, char *argv[])
  13. {
  14. printf("No SRP support\n");
  15. return (0);
  16. }
  17. #else
  18. # include <openssl/srp.h>
  19. # include <openssl/rand.h>
  20. # include <openssl/err.h>
  21. static void showbn(const char *name, const BIGNUM *bn)
  22. {
  23. fputs(name, stdout);
  24. fputs(" = ", stdout);
  25. BN_print_fp(stdout, bn);
  26. putc('\n', stdout);
  27. }
  28. # define RANDOM_SIZE 32 /* use 256 bits on each side */
  29. static int run_srp(const char *username, const char *client_pass,
  30. const char *server_pass)
  31. {
  32. int ret = -1;
  33. BIGNUM *s = NULL;
  34. BIGNUM *v = NULL;
  35. BIGNUM *a = NULL;
  36. BIGNUM *b = NULL;
  37. BIGNUM *u = NULL;
  38. BIGNUM *x = NULL;
  39. BIGNUM *Apub = NULL;
  40. BIGNUM *Bpub = NULL;
  41. BIGNUM *Kclient = NULL;
  42. BIGNUM *Kserver = NULL;
  43. unsigned char rand_tmp[RANDOM_SIZE];
  44. /* use builtin 1024-bit params */
  45. const SRP_gN *GN = SRP_get_default_gN("1024");
  46. if (GN == NULL) {
  47. fprintf(stderr, "Failed to get SRP parameters\n");
  48. return -1;
  49. }
  50. /* Set up server's password entry */
  51. if (!SRP_create_verifier_BN(username, server_pass, &s, &v, GN->N, GN->g)) {
  52. fprintf(stderr, "Failed to create SRP verifier\n");
  53. return -1;
  54. }
  55. showbn("N", GN->N);
  56. showbn("g", GN->g);
  57. showbn("Salt", s);
  58. showbn("Verifier", v);
  59. /* Server random */
  60. RAND_bytes(rand_tmp, sizeof(rand_tmp));
  61. b = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
  62. /* TODO - check b != 0 */
  63. showbn("b", b);
  64. /* Server's first message */
  65. Bpub = SRP_Calc_B(b, GN->N, GN->g, v);
  66. showbn("B", Bpub);
  67. if (!SRP_Verify_B_mod_N(Bpub, GN->N)) {
  68. fprintf(stderr, "Invalid B\n");
  69. return -1;
  70. }
  71. /* Client random */
  72. RAND_bytes(rand_tmp, sizeof(rand_tmp));
  73. a = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
  74. /* TODO - check a != 0 */
  75. showbn("a", a);
  76. /* Client's response */
  77. Apub = SRP_Calc_A(a, GN->N, GN->g);
  78. showbn("A", Apub);
  79. if (!SRP_Verify_A_mod_N(Apub, GN->N)) {
  80. fprintf(stderr, "Invalid A\n");
  81. return -1;
  82. }
  83. /* Both sides calculate u */
  84. u = SRP_Calc_u(Apub, Bpub, GN->N);
  85. /* Client's key */
  86. x = SRP_Calc_x(s, username, client_pass);
  87. Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u);
  88. showbn("Client's key", Kclient);
  89. /* Server's key */
  90. Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N);
  91. showbn("Server's key", Kserver);
  92. if (BN_cmp(Kclient, Kserver) == 0) {
  93. ret = 0;
  94. } else {
  95. fprintf(stderr, "Keys mismatch\n");
  96. ret = 1;
  97. }
  98. BN_clear_free(Kclient);
  99. BN_clear_free(Kserver);
  100. BN_clear_free(x);
  101. BN_free(u);
  102. BN_free(Apub);
  103. BN_clear_free(a);
  104. BN_free(Bpub);
  105. BN_clear_free(b);
  106. BN_free(s);
  107. BN_clear_free(v);
  108. return ret;
  109. }
  110. int main(int argc, char **argv)
  111. {
  112. BIO *bio_err;
  113. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  114. CRYPTO_set_mem_debug(1);
  115. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  116. /* "Negative" test, expect a mismatch */
  117. if (run_srp("alice", "password1", "password2") == 0) {
  118. fprintf(stderr, "Mismatched SRP run failed\n");
  119. return 1;
  120. }
  121. /* "Positive" test, should pass */
  122. if (run_srp("alice", "password", "password") != 0) {
  123. fprintf(stderr, "Plain SRP run failed\n");
  124. return 1;
  125. }
  126. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  127. if (CRYPTO_mem_leaks(bio_err) <= 0)
  128. return 1;
  129. #endif
  130. BIO_free(bio_err);
  131. return 0;
  132. }
  133. #endif