srptest.c 8.1 KB

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