srptest.c 3.6 KB

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