2
0

srptest.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright 2011-2017 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. # include "testutil.h"
  11. #ifdef OPENSSL_NO_SRP
  12. # include <stdio.h>
  13. #else
  14. # include <openssl/srp.h>
  15. # include <openssl/rand.h>
  16. # include <openssl/err.h>
  17. # define RANDOM_SIZE 32 /* use 256 bits on each side */
  18. static int run_srp(const char *username, const char *client_pass,
  19. const char *server_pass)
  20. {
  21. int ret = 0;
  22. BIGNUM *s = NULL;
  23. BIGNUM *v = NULL;
  24. BIGNUM *a = NULL;
  25. BIGNUM *b = NULL;
  26. BIGNUM *u = NULL;
  27. BIGNUM *x = NULL;
  28. BIGNUM *Apub = NULL;
  29. BIGNUM *Bpub = NULL;
  30. BIGNUM *Kclient = NULL;
  31. BIGNUM *Kserver = NULL;
  32. unsigned char rand_tmp[RANDOM_SIZE];
  33. /* use builtin 1024-bit params */
  34. const SRP_gN *GN;
  35. if (!TEST_ptr(GN = SRP_get_default_gN("1024")))
  36. return 0;
  37. /* Set up server's password entry */
  38. if (!TEST_true(SRP_create_verifier_BN(username, server_pass,
  39. &s, &v, GN->N, GN->g)))
  40. goto end;
  41. test_output_bignum("N", GN->N);
  42. test_output_bignum("g", GN->g);
  43. test_output_bignum("Salt", s);
  44. test_output_bignum("Verifier", v);
  45. /* Server random */
  46. RAND_bytes(rand_tmp, sizeof(rand_tmp));
  47. b = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
  48. if (!TEST_BN_ne_zero(b))
  49. goto end;
  50. test_output_bignum("b", b);
  51. /* Server's first message */
  52. Bpub = SRP_Calc_B(b, GN->N, GN->g, v);
  53. test_output_bignum("B", Bpub);
  54. if (!TEST_true(SRP_Verify_B_mod_N(Bpub, GN->N)))
  55. goto end;
  56. /* Client random */
  57. RAND_bytes(rand_tmp, sizeof(rand_tmp));
  58. a = BN_bin2bn(rand_tmp, sizeof(rand_tmp), NULL);
  59. if (!TEST_BN_ne_zero(a))
  60. goto end;
  61. test_output_bignum("a", a);
  62. /* Client's response */
  63. Apub = SRP_Calc_A(a, GN->N, GN->g);
  64. test_output_bignum("A", Apub);
  65. if (!TEST_true(SRP_Verify_A_mod_N(Apub, GN->N)))
  66. goto end;
  67. /* Both sides calculate u */
  68. u = SRP_Calc_u(Apub, Bpub, GN->N);
  69. /* Client's key */
  70. x = SRP_Calc_x(s, username, client_pass);
  71. Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u);
  72. test_output_bignum("Client's key", Kclient);
  73. /* Server's key */
  74. Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N);
  75. test_output_bignum("Server's key", Kserver);
  76. if (!TEST_BN_eq(Kclient, Kserver))
  77. goto end;
  78. ret = 1;
  79. end:
  80. BN_clear_free(Kclient);
  81. BN_clear_free(Kserver);
  82. BN_clear_free(x);
  83. BN_free(u);
  84. BN_free(Apub);
  85. BN_clear_free(a);
  86. BN_free(Bpub);
  87. BN_clear_free(b);
  88. BN_free(s);
  89. BN_clear_free(v);
  90. return ret;
  91. }
  92. static int check_bn(const char *name, const BIGNUM *bn, const char *hexbn)
  93. {
  94. BIGNUM *tmp = NULL;
  95. int r;
  96. if (!TEST_true(BN_hex2bn(&tmp, hexbn)))
  97. return 0;
  98. if (BN_cmp(bn, tmp) != 0)
  99. TEST_error("unexpected %s value", name);
  100. r = TEST_BN_eq(bn, tmp);
  101. BN_free(tmp);
  102. return r;
  103. }
  104. /* SRP test vectors from RFC5054 */
  105. static int run_srp_kat(void)
  106. {
  107. int ret = 0;
  108. BIGNUM *s = NULL;
  109. BIGNUM *v = NULL;
  110. BIGNUM *a = NULL;
  111. BIGNUM *b = NULL;
  112. BIGNUM *u = NULL;
  113. BIGNUM *x = NULL;
  114. BIGNUM *Apub = NULL;
  115. BIGNUM *Bpub = NULL;
  116. BIGNUM *Kclient = NULL;
  117. BIGNUM *Kserver = NULL;
  118. /* use builtin 1024-bit params */
  119. const SRP_gN *GN;
  120. if (!TEST_ptr(GN = SRP_get_default_gN("1024")))
  121. goto err;
  122. BN_hex2bn(&s, "BEB25379D1A8581EB5A727673A2441EE");
  123. /* Set up server's password entry */
  124. if (!TEST_true(SRP_create_verifier_BN("alice", "password123", &s, &v, GN->N,
  125. GN->g)))
  126. goto err;
  127. TEST_info("checking v");
  128. if (!TEST_true(check_bn("v", v,
  129. "7E273DE8696FFC4F4E337D05B4B375BEB0DDE1569E8FA00A9886D812"
  130. "9BADA1F1822223CA1A605B530E379BA4729FDC59F105B4787E5186F5"
  131. "C671085A1447B52A48CF1970B4FB6F8400BBF4CEBFBB168152E08AB5"
  132. "EA53D15C1AFF87B2B9DA6E04E058AD51CC72BFC9033B564E26480D78"
  133. "E955A5E29E7AB245DB2BE315E2099AFB")))
  134. goto err;
  135. TEST_note(" okay");
  136. /* Server random */
  137. BN_hex2bn(&b, "E487CB59D31AC550471E81F00F6928E01DDA08E974A004F49E61F5D1"
  138. "05284D20");
  139. /* Server's first message */
  140. Bpub = SRP_Calc_B(b, GN->N, GN->g, v);
  141. if (!TEST_true(SRP_Verify_B_mod_N(Bpub, GN->N)))
  142. goto err;
  143. TEST_info("checking B");
  144. if (!TEST_true(check_bn("B", Bpub,
  145. "BD0C61512C692C0CB6D041FA01BB152D4916A1E77AF46AE105393011"
  146. "BAF38964DC46A0670DD125B95A981652236F99D9B681CBF87837EC99"
  147. "6C6DA04453728610D0C6DDB58B318885D7D82C7F8DEB75CE7BD4FBAA"
  148. "37089E6F9C6059F388838E7A00030B331EB76840910440B1B27AAEAE"
  149. "EB4012B7D7665238A8E3FB004B117B58")))
  150. goto err;
  151. TEST_note(" okay");
  152. /* Client random */
  153. BN_hex2bn(&a, "60975527035CF2AD1989806F0407210BC81EDC04E2762A56AFD529DD"
  154. "DA2D4393");
  155. /* Client's response */
  156. Apub = SRP_Calc_A(a, GN->N, GN->g);
  157. if (!TEST_true(SRP_Verify_A_mod_N(Apub, GN->N)))
  158. goto err;
  159. TEST_info("checking A");
  160. if (!TEST_true(check_bn("A", Apub,
  161. "61D5E490F6F1B79547B0704C436F523DD0E560F0C64115BB72557EC4"
  162. "4352E8903211C04692272D8B2D1A5358A2CF1B6E0BFCF99F921530EC"
  163. "8E39356179EAE45E42BA92AEACED825171E1E8B9AF6D9C03E1327F44"
  164. "BE087EF06530E69F66615261EEF54073CA11CF5858F0EDFDFE15EFEA"
  165. "B349EF5D76988A3672FAC47B0769447B")))
  166. goto err;
  167. TEST_note(" okay");
  168. /* Both sides calculate u */
  169. u = SRP_Calc_u(Apub, Bpub, GN->N);
  170. if (!TEST_true(check_bn("u", u,
  171. "CE38B9593487DA98554ED47D70A7AE5F462EF019")))
  172. goto err;
  173. /* Client's key */
  174. x = SRP_Calc_x(s, "alice", "password123");
  175. Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u);
  176. TEST_info("checking client's key");
  177. if (!TEST_true(check_bn("Client's key", Kclient,
  178. "B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D"
  179. "233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C"
  180. "41BB59B6D5979B5C00A172B4A2A5903A0BDCAF8A709585EB2AFAFA8F"
  181. "3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D"
  182. "C346D7E474B29EDE8A469FFECA686E5A")))
  183. goto err;
  184. TEST_note(" okay");
  185. /* Server's key */
  186. Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N);
  187. TEST_info("checking server's key");
  188. if (!TEST_true(check_bn("Server's key", Kserver,
  189. "B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D"
  190. "233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C"
  191. "41BB59B6D5979B5C00A172B4A2A5903A0BDCAF8A709585EB2AFAFA8F"
  192. "3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D"
  193. "C346D7E474B29EDE8A469FFECA686E5A")))
  194. goto err;
  195. TEST_note(" okay");
  196. ret = 1;
  197. err:
  198. BN_clear_free(Kclient);
  199. BN_clear_free(Kserver);
  200. BN_clear_free(x);
  201. BN_free(u);
  202. BN_free(Apub);
  203. BN_clear_free(a);
  204. BN_free(Bpub);
  205. BN_clear_free(b);
  206. BN_free(s);
  207. BN_clear_free(v);
  208. return ret;
  209. }
  210. static int run_srp_tests(void)
  211. {
  212. /* "Negative" test, expect a mismatch */
  213. TEST_info("run_srp: expecting a mismatch");
  214. if (!TEST_false(run_srp("alice", "password1", "password2")))
  215. return 0;
  216. /* "Positive" test, should pass */
  217. TEST_info("run_srp: expecting a match");
  218. if (!TEST_true(run_srp("alice", "password", "password")))
  219. return 0;
  220. return 1;
  221. }
  222. #endif
  223. int setup_tests(void)
  224. {
  225. #ifdef OPENSSL_NO_SRP
  226. printf("No SRP support\n");
  227. #else
  228. ADD_TEST(run_srp_tests);
  229. ADD_TEST(run_srp_kat);
  230. #endif
  231. return 1;
  232. }