srptest.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. static int check_bn(const char *name, const BIGNUM *bn, const char *hexbn)
  111. {
  112. BIGNUM *tmp = NULL;
  113. int rv;
  114. if (BN_hex2bn(&tmp, hexbn) == 0)
  115. return 0;
  116. rv = BN_cmp(bn, tmp);
  117. if (rv == 0) {
  118. printf("%s = ", name);
  119. BN_print_fp(stdout, bn);
  120. printf("\n");
  121. BN_free(tmp);
  122. return 1;
  123. }
  124. printf("Unexpected %s value\n", name);
  125. printf("Expecting: ");
  126. BN_print_fp(stdout, tmp);
  127. printf("\nReceived: ");
  128. BN_print_fp(stdout, bn);
  129. printf("\n");
  130. BN_free(tmp);
  131. return 0;
  132. }
  133. /* SRP test vectors from RFC5054 */
  134. static int run_srp_kat(void)
  135. {
  136. int ret = 0;
  137. BIGNUM *s = NULL;
  138. BIGNUM *v = NULL;
  139. BIGNUM *a = NULL;
  140. BIGNUM *b = NULL;
  141. BIGNUM *u = NULL;
  142. BIGNUM *x = NULL;
  143. BIGNUM *Apub = NULL;
  144. BIGNUM *Bpub = NULL;
  145. BIGNUM *Kclient = NULL;
  146. BIGNUM *Kserver = NULL;
  147. /* use builtin 1024-bit params */
  148. const SRP_gN *GN = SRP_get_default_gN("1024");
  149. if (GN == NULL) {
  150. fprintf(stderr, "Failed to get SRP parameters\n");
  151. goto err;
  152. }
  153. BN_hex2bn(&s, "BEB25379D1A8581EB5A727673A2441EE");
  154. /* Set up server's password entry */
  155. if (!SRP_create_verifier_BN("alice", "password123", &s, &v, GN->N,
  156. GN->g)) {
  157. fprintf(stderr, "Failed to create SRP verifier\n");
  158. goto err;
  159. }
  160. if (!check_bn("v", v,
  161. "7E273DE8696FFC4F4E337D05B4B375BEB0DDE1569E8FA00A9886D812"
  162. "9BADA1F1822223CA1A605B530E379BA4729FDC59F105B4787E5186F5"
  163. "C671085A1447B52A48CF1970B4FB6F8400BBF4CEBFBB168152E08AB5"
  164. "EA53D15C1AFF87B2B9DA6E04E058AD51CC72BFC9033B564E26480D78"
  165. "E955A5E29E7AB245DB2BE315E2099AFB"))
  166. goto err;
  167. /* Server random */
  168. BN_hex2bn(&b, "E487CB59D31AC550471E81F00F6928E01DDA08E974A004F49E61F5D1"
  169. "05284D20");
  170. /* Server's first message */
  171. Bpub = SRP_Calc_B(b, GN->N, GN->g, v);
  172. if (!SRP_Verify_B_mod_N(Bpub, GN->N)) {
  173. fprintf(stderr, "Invalid B\n");
  174. goto err;
  175. }
  176. if (!check_bn("B", Bpub,
  177. "BD0C61512C692C0CB6D041FA01BB152D4916A1E77AF46AE105393011"
  178. "BAF38964DC46A0670DD125B95A981652236F99D9B681CBF87837EC99"
  179. "6C6DA04453728610D0C6DDB58B318885D7D82C7F8DEB75CE7BD4FBAA"
  180. "37089E6F9C6059F388838E7A00030B331EB76840910440B1B27AAEAE"
  181. "EB4012B7D7665238A8E3FB004B117B58"))
  182. goto err;
  183. /* Client random */
  184. BN_hex2bn(&a, "60975527035CF2AD1989806F0407210BC81EDC04E2762A56AFD529DD"
  185. "DA2D4393");
  186. /* Client's response */
  187. Apub = SRP_Calc_A(a, GN->N, GN->g);
  188. if (!SRP_Verify_A_mod_N(Apub, GN->N)) {
  189. fprintf(stderr, "Invalid A\n");
  190. return -1;
  191. }
  192. if (!check_bn("A", Apub,
  193. "61D5E490F6F1B79547B0704C436F523DD0E560F0C64115BB72557EC4"
  194. "4352E8903211C04692272D8B2D1A5358A2CF1B6E0BFCF99F921530EC"
  195. "8E39356179EAE45E42BA92AEACED825171E1E8B9AF6D9C03E1327F44"
  196. "BE087EF06530E69F66615261EEF54073CA11CF5858F0EDFDFE15EFEA"
  197. "B349EF5D76988A3672FAC47B0769447B"))
  198. goto err;
  199. /* Both sides calculate u */
  200. u = SRP_Calc_u(Apub, Bpub, GN->N);
  201. if (!check_bn("u", u, "CE38B9593487DA98554ED47D70A7AE5F462EF019"))
  202. goto err;
  203. /* Client's key */
  204. x = SRP_Calc_x(s, "alice", "password123");
  205. Kclient = SRP_Calc_client_key(GN->N, Bpub, GN->g, x, a, u);
  206. if (!check_bn("Client's key", Kclient,
  207. "B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D"
  208. "233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C"
  209. "41BB59B6D5979B5C00A172B4A2A5903A0BDCAF8A709585EB2AFAFA8F"
  210. "3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D"
  211. "C346D7E474B29EDE8A469FFECA686E5A"))
  212. goto err;
  213. /* Server's key */
  214. Kserver = SRP_Calc_server_key(Apub, v, u, b, GN->N);
  215. if (!check_bn("Server's key", Kserver,
  216. "B0DC82BABCF30674AE450C0287745E7990A3381F63B387AAF271A10D"
  217. "233861E359B48220F7C4693C9AE12B0A6F67809F0876E2D013800D6C"
  218. "41BB59B6D5979B5C00A172B4A2A5903A0BDCAF8A709585EB2AFAFA8F"
  219. "3499B200210DCC1F10EB33943CD67FC88A2F39A4BE5BEC4EC0A3212D"
  220. "C346D7E474B29EDE8A469FFECA686E5A"))
  221. goto err;
  222. ret = 1;
  223. err:
  224. BN_clear_free(Kclient);
  225. BN_clear_free(Kserver);
  226. BN_clear_free(x);
  227. BN_free(u);
  228. BN_free(Apub);
  229. BN_clear_free(a);
  230. BN_free(Bpub);
  231. BN_clear_free(b);
  232. BN_free(s);
  233. BN_clear_free(v);
  234. return ret;
  235. }
  236. int main(int argc, char **argv)
  237. {
  238. BIO *bio_err;
  239. bio_err = BIO_new_fp(stderr, BIO_NOCLOSE | BIO_FP_TEXT);
  240. CRYPTO_set_mem_debug(1);
  241. CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
  242. /* "Negative" test, expect a mismatch */
  243. if (run_srp("alice", "password1", "password2") == 0) {
  244. fprintf(stderr, "Mismatched SRP run failed\n");
  245. return 1;
  246. }
  247. /* "Positive" test, should pass */
  248. if (run_srp("alice", "password", "password") != 0) {
  249. fprintf(stderr, "Plain SRP run failed\n");
  250. return 1;
  251. }
  252. /* KAT from RFC5054: should pass */
  253. if (run_srp_kat() != 1) {
  254. fprintf(stderr, "SRP KAT failed\n");
  255. return 1;
  256. }
  257. #ifndef OPENSSL_NO_CRYPTO_MDEBUG
  258. if (CRYPTO_mem_leaks(bio_err) <= 0)
  259. return 1;
  260. #endif
  261. BIO_free(bio_err);
  262. return 0;
  263. }
  264. #endif