srptest.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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, const char *server_pass)
  22. {
  23. int ret=-1;
  24. BIGNUM *s = NULL;
  25. BIGNUM *v = NULL;
  26. BIGNUM *a = NULL;
  27. BIGNUM *b = NULL;
  28. BIGNUM *u = NULL;
  29. BIGNUM *x = NULL;
  30. BIGNUM *Apub = NULL;
  31. BIGNUM *Bpub = NULL;
  32. BIGNUM *Kclient = NULL;
  33. BIGNUM *Kserver = NULL;
  34. unsigned char rand_tmp[RANDOM_SIZE];
  35. /* use builtin 1024-bit params */
  36. SRP_gN *GN = SRP_get_default_gN("1024");
  37. if(GN == NULL)
  38. {
  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. {
  45. fprintf(stderr, "Failed to create SRP verifier\n");
  46. return -1;
  47. }
  48. showbn("N", GN->N);
  49. showbn("g", GN->g);
  50. showbn("Salt", s);
  51. showbn("Verifier", v);
  52. /* Server random */
  53. RAND_pseudo_bytes(rand_tmp, sizeof(rand_tmp));
  54. b = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
  55. /* TODO - check b != 0 */
  56. showbn("b", b);
  57. /* Server's first message */
  58. Bpub = SRP_Calc_B(b, GN->N, GN->g, v);
  59. showbn("B", Bpub);
  60. if(!SRP_Verify_B_mod_N(Bpub, GN->N))
  61. {
  62. fprintf(stderr, "Invalid B\n");
  63. return -1;
  64. }
  65. /* Client random */
  66. RAND_pseudo_bytes(rand_tmp, sizeof(rand_tmp));
  67. a = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
  68. /* TODO - check a != 0 */
  69. showbn("a", a);
  70. /* Client's response */
  71. Apub = SRP_Calc_A(a, GN->N, GN->g);
  72. showbn("A", Apub);
  73. if(!SRP_Verify_A_mod_N(Apub, GN->N))
  74. {
  75. fprintf(stderr, "Invalid A\n");
  76. return -1;
  77. }
  78. /* Both sides calculate u */
  79. u = SRP_Calc_u(Apub, Bpub, GN->N);
  80. /* Client's key */
  81. x = SRP_Calc_x(s, username, client_pass);
  82. Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u);
  83. showbn("Client's key", Kclient);
  84. /* Server's key */
  85. Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N);
  86. showbn("Server's key", Kserver);
  87. if(BN_cmp(Kclient, Kserver) == 0)
  88. {
  89. ret = 0;
  90. }
  91. else
  92. {
  93. fprintf(stderr, "Keys mismatch\n");
  94. ret = 1;
  95. }
  96. BN_clear_free(Kclient);
  97. BN_clear_free(Kserver);
  98. BN_clear_free(x);
  99. BN_free(u);
  100. BN_free(Apub);
  101. BN_clear_free(a);
  102. BN_free(Bpub);
  103. BN_clear_free(b);
  104. BN_free(s);
  105. BN_clear_free(v);
  106. return ret;
  107. }
  108. int main(int argc, char **argv)
  109. {
  110. BIO *bio_err;
  111. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE);
  112. CRYPTO_malloc_debug_init();
  113. CRYPTO_dbg_set_options(V_CRYPTO_MDEBUG_ALL);
  114. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  115. ERR_load_crypto_strings();
  116. /* "Negative" test, expect a mismatch */
  117. if(run_srp("alice", "password1", "password2") == 0)
  118. {
  119. fprintf(stderr, "Mismatched SRP run failed\n");
  120. return 1;
  121. }
  122. /* "Positive" test, should pass */
  123. if(run_srp("alice", "password", "password") != 0)
  124. {
  125. fprintf(stderr, "Plain SRP run failed\n");
  126. return 1;
  127. }
  128. CRYPTO_cleanup_all_ex_data();
  129. ERR_remove_thread_state(NULL);
  130. ERR_free_strings();
  131. CRYPTO_mem_leaks(bio_err);
  132. return 0;
  133. }
  134. #endif