srp_lib.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /* crypto/srp/srp_lib.c */
  2. /*
  3. * Written by Christophe Renou (christophe.renou@edelweb.fr) with the
  4. * precious help of Peter Sylvester (peter.sylvester@edelweb.fr) for the
  5. * EdelKey project and contributed to the OpenSSL project 2004.
  6. */
  7. /* ====================================================================
  8. * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. *
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. *
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. *
  22. * 3. All advertising materials mentioning features or use of this
  23. * software must display the following acknowledgment:
  24. * "This product includes software developed by the OpenSSL Project
  25. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  26. *
  27. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  28. * endorse or promote products derived from this software without
  29. * prior written permission. For written permission, please contact
  30. * licensing@OpenSSL.org.
  31. *
  32. * 5. Products derived from this software may not be called "OpenSSL"
  33. * nor may "OpenSSL" appear in their names without prior written
  34. * permission of the OpenSSL Project.
  35. *
  36. * 6. Redistributions of any form whatsoever must retain the following
  37. * acknowledgment:
  38. * "This product includes software developed by the OpenSSL Project
  39. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  42. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  44. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  45. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  46. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  47. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  48. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  49. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  50. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  51. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  52. * OF THE POSSIBILITY OF SUCH DAMAGE.
  53. * ====================================================================
  54. *
  55. * This product includes cryptographic software written by Eric Young
  56. * (eay@cryptsoft.com). This product includes software written by Tim
  57. * Hudson (tjh@cryptsoft.com).
  58. *
  59. */
  60. #ifndef OPENSSL_NO_SRP
  61. # include "cryptlib.h"
  62. # include "srp_lcl.h"
  63. # include <openssl/srp.h>
  64. # include <openssl/evp.h>
  65. # if (BN_BYTES == 8)
  66. # if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__)
  67. # define bn_pack4(a1,a2,a3,a4) ((a1##UI64<<48)|(a2##UI64<<32)|(a3##UI64<<16)|a4##UI64)
  68. # elif defined(__arch64__)
  69. # define bn_pack4(a1,a2,a3,a4) ((a1##UL<<48)|(a2##UL<<32)|(a3##UL<<16)|a4##UL)
  70. # else
  71. # define bn_pack4(a1,a2,a3,a4) ((a1##ULL<<48)|(a2##ULL<<32)|(a3##ULL<<16)|a4##ULL)
  72. # endif
  73. # elif (BN_BYTES == 4)
  74. # define bn_pack4(a1,a2,a3,a4) ((a3##UL<<16)|a4##UL), ((a1##UL<<16)|a2##UL)
  75. # else
  76. # error "unsupported BN_BYTES"
  77. # endif
  78. # include "srp_grps.h"
  79. static BIGNUM *srp_Calc_k(BIGNUM *N, BIGNUM *g)
  80. {
  81. /* k = SHA1(N | PAD(g)) -- tls-srp draft 8 */
  82. unsigned char digest[SHA_DIGEST_LENGTH];
  83. unsigned char *tmp;
  84. EVP_MD_CTX ctxt;
  85. int longg;
  86. int longN = BN_num_bytes(N);
  87. if (BN_ucmp(g, N) >= 0)
  88. return NULL;
  89. if ((tmp = OPENSSL_malloc(longN)) == NULL)
  90. return NULL;
  91. BN_bn2bin(N, tmp);
  92. EVP_MD_CTX_init(&ctxt);
  93. EVP_DigestInit_ex(&ctxt, EVP_sha1(), NULL);
  94. EVP_DigestUpdate(&ctxt, tmp, longN);
  95. memset(tmp, 0, longN);
  96. longg = BN_bn2bin(g, tmp);
  97. /* use the zeros behind to pad on left */
  98. EVP_DigestUpdate(&ctxt, tmp + longg, longN - longg);
  99. EVP_DigestUpdate(&ctxt, tmp, longg);
  100. OPENSSL_free(tmp);
  101. EVP_DigestFinal_ex(&ctxt, digest, NULL);
  102. EVP_MD_CTX_cleanup(&ctxt);
  103. return BN_bin2bn(digest, sizeof(digest), NULL);
  104. }
  105. BIGNUM *SRP_Calc_u(BIGNUM *A, BIGNUM *B, BIGNUM *N)
  106. {
  107. /* k = SHA1(PAD(A) || PAD(B) ) -- tls-srp draft 8 */
  108. BIGNUM *u;
  109. unsigned char cu[SHA_DIGEST_LENGTH];
  110. unsigned char *cAB;
  111. EVP_MD_CTX ctxt;
  112. int longN;
  113. if ((A == NULL) || (B == NULL) || (N == NULL))
  114. return NULL;
  115. if (BN_ucmp(A, N) >= 0 || BN_ucmp(B, N) >= 0)
  116. return NULL;
  117. longN = BN_num_bytes(N);
  118. if ((cAB = OPENSSL_malloc(2 * longN)) == NULL)
  119. return NULL;
  120. memset(cAB, 0, longN);
  121. EVP_MD_CTX_init(&ctxt);
  122. EVP_DigestInit_ex(&ctxt, EVP_sha1(), NULL);
  123. EVP_DigestUpdate(&ctxt, cAB + BN_bn2bin(A, cAB + longN), longN);
  124. EVP_DigestUpdate(&ctxt, cAB + BN_bn2bin(B, cAB + longN), longN);
  125. OPENSSL_free(cAB);
  126. EVP_DigestFinal_ex(&ctxt, cu, NULL);
  127. EVP_MD_CTX_cleanup(&ctxt);
  128. if (!(u = BN_bin2bn(cu, sizeof(cu), NULL)))
  129. return NULL;
  130. if (!BN_is_zero(u))
  131. return u;
  132. BN_free(u);
  133. return NULL;
  134. }
  135. BIGNUM *SRP_Calc_server_key(BIGNUM *A, BIGNUM *v, BIGNUM *u, BIGNUM *b,
  136. BIGNUM *N)
  137. {
  138. BIGNUM *tmp = NULL, *S = NULL;
  139. BN_CTX *bn_ctx;
  140. if (u == NULL || A == NULL || v == NULL || b == NULL || N == NULL)
  141. return NULL;
  142. if ((bn_ctx = BN_CTX_new()) == NULL || (tmp = BN_new()) == NULL)
  143. goto err;
  144. /* S = (A*v**u) ** b */
  145. if (!BN_mod_exp(tmp, v, u, N, bn_ctx))
  146. goto err;
  147. if (!BN_mod_mul(tmp, A, tmp, N, bn_ctx))
  148. goto err;
  149. S = BN_new();
  150. if (S != NULL && !BN_mod_exp(S, tmp, b, N, bn_ctx)) {
  151. BN_free(S);
  152. S = NULL;
  153. }
  154. err:
  155. BN_CTX_free(bn_ctx);
  156. BN_clear_free(tmp);
  157. return S;
  158. }
  159. BIGNUM *SRP_Calc_B(BIGNUM *b, BIGNUM *N, BIGNUM *g, BIGNUM *v)
  160. {
  161. BIGNUM *kv = NULL, *gb = NULL;
  162. BIGNUM *B = NULL, *k = NULL;
  163. BN_CTX *bn_ctx;
  164. if (b == NULL || N == NULL || g == NULL || v == NULL ||
  165. (bn_ctx = BN_CTX_new()) == NULL)
  166. return NULL;
  167. if ((kv = BN_new()) == NULL ||
  168. (gb = BN_new()) == NULL || (B = BN_new()) == NULL)
  169. goto err;
  170. /* B = g**b + k*v */
  171. if (!BN_mod_exp(gb, g, b, N, bn_ctx) ||
  172. !(k = srp_Calc_k(N, g)) ||
  173. !BN_mod_mul(kv, v, k, N, bn_ctx) ||
  174. !BN_mod_add(B, gb, kv, N, bn_ctx)) {
  175. BN_free(B);
  176. B = NULL;
  177. }
  178. err:
  179. BN_CTX_free(bn_ctx);
  180. BN_clear_free(kv);
  181. BN_clear_free(gb);
  182. BN_free(k);
  183. return B;
  184. }
  185. BIGNUM *SRP_Calc_x(BIGNUM *s, const char *user, const char *pass)
  186. {
  187. unsigned char dig[SHA_DIGEST_LENGTH];
  188. EVP_MD_CTX ctxt;
  189. unsigned char *cs;
  190. if ((s == NULL) || (user == NULL) || (pass == NULL))
  191. return NULL;
  192. if ((cs = OPENSSL_malloc(BN_num_bytes(s))) == NULL)
  193. return NULL;
  194. EVP_MD_CTX_init(&ctxt);
  195. EVP_DigestInit_ex(&ctxt, EVP_sha1(), NULL);
  196. EVP_DigestUpdate(&ctxt, user, strlen(user));
  197. EVP_DigestUpdate(&ctxt, ":", 1);
  198. EVP_DigestUpdate(&ctxt, pass, strlen(pass));
  199. EVP_DigestFinal_ex(&ctxt, dig, NULL);
  200. EVP_DigestInit_ex(&ctxt, EVP_sha1(), NULL);
  201. BN_bn2bin(s, cs);
  202. EVP_DigestUpdate(&ctxt, cs, BN_num_bytes(s));
  203. OPENSSL_free(cs);
  204. EVP_DigestUpdate(&ctxt, dig, sizeof(dig));
  205. EVP_DigestFinal_ex(&ctxt, dig, NULL);
  206. EVP_MD_CTX_cleanup(&ctxt);
  207. return BN_bin2bn(dig, sizeof(dig), NULL);
  208. }
  209. BIGNUM *SRP_Calc_A(BIGNUM *a, BIGNUM *N, BIGNUM *g)
  210. {
  211. BN_CTX *bn_ctx;
  212. BIGNUM *A = NULL;
  213. if (a == NULL || N == NULL || g == NULL ||
  214. (bn_ctx = BN_CTX_new()) == NULL)
  215. return NULL;
  216. if ((A = BN_new()) != NULL && !BN_mod_exp(A, g, a, N, bn_ctx)) {
  217. BN_free(A);
  218. A = NULL;
  219. }
  220. BN_CTX_free(bn_ctx);
  221. return A;
  222. }
  223. BIGNUM *SRP_Calc_client_key(BIGNUM *N, BIGNUM *B, BIGNUM *g, BIGNUM *x,
  224. BIGNUM *a, BIGNUM *u)
  225. {
  226. BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;
  227. BN_CTX *bn_ctx;
  228. if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL
  229. || a == NULL || (bn_ctx = BN_CTX_new()) == NULL)
  230. return NULL;
  231. if ((tmp = BN_new()) == NULL ||
  232. (tmp2 = BN_new()) == NULL ||
  233. (tmp3 = BN_new()) == NULL)
  234. goto err;
  235. if (!BN_mod_exp(tmp, g, x, N, bn_ctx))
  236. goto err;
  237. if (!(k = srp_Calc_k(N, g)))
  238. goto err;
  239. if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))
  240. goto err;
  241. if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))
  242. goto err;
  243. if (!BN_mul(tmp3, u, x, bn_ctx))
  244. goto err;
  245. if (!BN_add(tmp2, a, tmp3))
  246. goto err;
  247. K = BN_new();
  248. if (K != NULL && !BN_mod_exp(K, tmp, tmp2, N, bn_ctx)) {
  249. BN_free(K);
  250. K = NULL;
  251. }
  252. err:
  253. BN_CTX_free(bn_ctx);
  254. BN_clear_free(tmp);
  255. BN_clear_free(tmp2);
  256. BN_clear_free(tmp3);
  257. BN_free(k);
  258. return K;
  259. }
  260. int SRP_Verify_B_mod_N(BIGNUM *B, BIGNUM *N)
  261. {
  262. BIGNUM *r;
  263. BN_CTX *bn_ctx;
  264. int ret = 0;
  265. if (B == NULL || N == NULL || (bn_ctx = BN_CTX_new()) == NULL)
  266. return 0;
  267. if ((r = BN_new()) == NULL)
  268. goto err;
  269. /* Checks if B % N == 0 */
  270. if (!BN_nnmod(r, B, N, bn_ctx))
  271. goto err;
  272. ret = !BN_is_zero(r);
  273. err:
  274. BN_CTX_free(bn_ctx);
  275. BN_free(r);
  276. return ret;
  277. }
  278. int SRP_Verify_A_mod_N(BIGNUM *A, BIGNUM *N)
  279. {
  280. /* Checks if A % N == 0 */
  281. return SRP_Verify_B_mod_N(A, N);
  282. }
  283. /*
  284. * Check if G and N are kwown parameters. The values have been generated
  285. * from the ietf-tls-srp draft version 8
  286. */
  287. char *SRP_check_known_gN_param(BIGNUM *g, BIGNUM *N)
  288. {
  289. size_t i;
  290. if ((g == NULL) || (N == NULL))
  291. return 0;
  292. srp_bn_print(g);
  293. srp_bn_print(N);
  294. for (i = 0; i < KNOWN_GN_NUMBER; i++) {
  295. if (BN_cmp(knowngN[i].g, g) == 0 && BN_cmp(knowngN[i].N, N) == 0)
  296. return knowngN[i].id;
  297. }
  298. return NULL;
  299. }
  300. SRP_gN *SRP_get_default_gN(const char *id)
  301. {
  302. size_t i;
  303. if (id == NULL)
  304. return knowngN;
  305. for (i = 0; i < KNOWN_GN_NUMBER; i++) {
  306. if (strcmp(knowngN[i].id, id) == 0)
  307. return knowngN + i;
  308. }
  309. return NULL;
  310. }
  311. #endif