srp_lib.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright 2004-2019 The OpenSSL Project Authors. All Rights Reserved.
  3. * Copyright (c) 2004, EdelKey Project. All Rights Reserved.
  4. *
  5. * Licensed under the OpenSSL license (the "License"). You may not use
  6. * this file except in compliance with the License. You can obtain a copy
  7. * in the file LICENSE in the source distribution or at
  8. * https://www.openssl.org/source/license.html
  9. *
  10. * Originally written by Christophe Renou and Peter Sylvester,
  11. * for the EdelKey project.
  12. */
  13. #ifndef OPENSSL_NO_SRP
  14. # include "internal/cryptlib.h"
  15. # include <openssl/sha.h>
  16. # include <openssl/srp.h>
  17. # include <openssl/evp.h>
  18. # include "crypto/bn_srp.h"
  19. # include "../crypto/bn/bn_local.h"
  20. /* calculate = SHA1(PAD(x) || PAD(y)) */
  21. static BIGNUM *srp_Calc_xy(const BIGNUM *x, const BIGNUM *y, const BIGNUM *N)
  22. {
  23. unsigned char digest[SHA_DIGEST_LENGTH];
  24. unsigned char *tmp = NULL;
  25. int numN = BN_num_bytes(N);
  26. BIGNUM *res = NULL;
  27. if (x != N && BN_ucmp(x, N) >= 0)
  28. return NULL;
  29. if (y != N && BN_ucmp(y, N) >= 0)
  30. return NULL;
  31. if ((tmp = OPENSSL_malloc(numN * 2)) == NULL)
  32. goto err;
  33. if (BN_bn2binpad(x, tmp, numN) < 0
  34. || BN_bn2binpad(y, tmp + numN, numN) < 0
  35. || !EVP_Digest(tmp, numN * 2, digest, NULL, EVP_sha1(), NULL))
  36. goto err;
  37. res = BN_bin2bn(digest, sizeof(digest), NULL);
  38. err:
  39. OPENSSL_free(tmp);
  40. return res;
  41. }
  42. static BIGNUM *srp_Calc_k(const BIGNUM *N, const BIGNUM *g)
  43. {
  44. /* k = SHA1(N | PAD(g)) -- tls-srp draft 8 */
  45. return srp_Calc_xy(N, g, N);
  46. }
  47. BIGNUM *SRP_Calc_u(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N)
  48. {
  49. /* k = SHA1(PAD(A) || PAD(B) ) -- tls-srp draft 8 */
  50. return srp_Calc_xy(A, B, N);
  51. }
  52. BIGNUM *SRP_Calc_server_key(const BIGNUM *A, const BIGNUM *v, const BIGNUM *u,
  53. const BIGNUM *b, const BIGNUM *N)
  54. {
  55. BIGNUM *tmp = NULL, *S = NULL;
  56. BN_CTX *bn_ctx;
  57. if (u == NULL || A == NULL || v == NULL || b == NULL || N == NULL)
  58. return NULL;
  59. if ((bn_ctx = BN_CTX_new()) == NULL || (tmp = BN_new()) == NULL)
  60. goto err;
  61. /* S = (A*v**u) ** b */
  62. if (!BN_mod_exp(tmp, v, u, N, bn_ctx))
  63. goto err;
  64. if (!BN_mod_mul(tmp, A, tmp, N, bn_ctx))
  65. goto err;
  66. S = BN_new();
  67. if (S != NULL && !BN_mod_exp(S, tmp, b, N, bn_ctx)) {
  68. BN_free(S);
  69. S = NULL;
  70. }
  71. err:
  72. BN_CTX_free(bn_ctx);
  73. BN_clear_free(tmp);
  74. return S;
  75. }
  76. BIGNUM *SRP_Calc_B(const BIGNUM *b, const BIGNUM *N, const BIGNUM *g,
  77. const BIGNUM *v)
  78. {
  79. BIGNUM *kv = NULL, *gb = NULL;
  80. BIGNUM *B = NULL, *k = NULL;
  81. BN_CTX *bn_ctx;
  82. if (b == NULL || N == NULL || g == NULL || v == NULL ||
  83. (bn_ctx = BN_CTX_new()) == NULL)
  84. return NULL;
  85. if ((kv = BN_new()) == NULL ||
  86. (gb = BN_new()) == NULL || (B = BN_new()) == NULL)
  87. goto err;
  88. /* B = g**b + k*v */
  89. if (!BN_mod_exp(gb, g, b, N, bn_ctx)
  90. || (k = srp_Calc_k(N, g)) == NULL
  91. || !BN_mod_mul(kv, v, k, N, bn_ctx)
  92. || !BN_mod_add(B, gb, kv, N, bn_ctx)) {
  93. BN_free(B);
  94. B = NULL;
  95. }
  96. err:
  97. BN_CTX_free(bn_ctx);
  98. BN_clear_free(kv);
  99. BN_clear_free(gb);
  100. BN_free(k);
  101. return B;
  102. }
  103. BIGNUM *SRP_Calc_x(const BIGNUM *s, const char *user, const char *pass)
  104. {
  105. unsigned char dig[SHA_DIGEST_LENGTH];
  106. EVP_MD_CTX *ctxt;
  107. unsigned char *cs = NULL;
  108. BIGNUM *res = NULL;
  109. if ((s == NULL) || (user == NULL) || (pass == NULL))
  110. return NULL;
  111. ctxt = EVP_MD_CTX_new();
  112. if (ctxt == NULL)
  113. return NULL;
  114. if ((cs = OPENSSL_malloc(BN_num_bytes(s))) == NULL)
  115. goto err;
  116. if (!EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
  117. || !EVP_DigestUpdate(ctxt, user, strlen(user))
  118. || !EVP_DigestUpdate(ctxt, ":", 1)
  119. || !EVP_DigestUpdate(ctxt, pass, strlen(pass))
  120. || !EVP_DigestFinal_ex(ctxt, dig, NULL)
  121. || !EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL))
  122. goto err;
  123. if (BN_bn2bin(s, cs) < 0)
  124. goto err;
  125. if (!EVP_DigestUpdate(ctxt, cs, BN_num_bytes(s)))
  126. goto err;
  127. if (!EVP_DigestUpdate(ctxt, dig, sizeof(dig))
  128. || !EVP_DigestFinal_ex(ctxt, dig, NULL))
  129. goto err;
  130. res = BN_bin2bn(dig, sizeof(dig), NULL);
  131. err:
  132. OPENSSL_free(cs);
  133. EVP_MD_CTX_free(ctxt);
  134. return res;
  135. }
  136. BIGNUM *SRP_Calc_A(const BIGNUM *a, const BIGNUM *N, const BIGNUM *g)
  137. {
  138. BN_CTX *bn_ctx;
  139. BIGNUM *A = NULL;
  140. if (a == NULL || N == NULL || g == NULL || (bn_ctx = BN_CTX_new()) == NULL)
  141. return NULL;
  142. if ((A = BN_new()) != NULL && !BN_mod_exp(A, g, a, N, bn_ctx)) {
  143. BN_free(A);
  144. A = NULL;
  145. }
  146. BN_CTX_free(bn_ctx);
  147. return A;
  148. }
  149. BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,
  150. const BIGNUM *x, const BIGNUM *a, const BIGNUM *u)
  151. {
  152. BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;
  153. BN_CTX *bn_ctx;
  154. if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL
  155. || a == NULL || (bn_ctx = BN_CTX_new()) == NULL)
  156. return NULL;
  157. if ((tmp = BN_new()) == NULL ||
  158. (tmp2 = BN_new()) == NULL ||
  159. (tmp3 = BN_new()) == NULL)
  160. goto err;
  161. if (!BN_mod_exp(tmp, g, x, N, bn_ctx))
  162. goto err;
  163. if ((k = srp_Calc_k(N, g)) == NULL)
  164. goto err;
  165. if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))
  166. goto err;
  167. if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))
  168. goto err;
  169. if (!BN_mul(tmp3, u, x, bn_ctx))
  170. goto err;
  171. if (!BN_add(tmp2, a, tmp3))
  172. goto err;
  173. K = BN_new();
  174. if (K != NULL && !BN_mod_exp(K, tmp, tmp2, N, bn_ctx)) {
  175. BN_free(K);
  176. K = NULL;
  177. }
  178. err:
  179. BN_CTX_free(bn_ctx);
  180. BN_clear_free(tmp);
  181. BN_clear_free(tmp2);
  182. BN_clear_free(tmp3);
  183. BN_free(k);
  184. return K;
  185. }
  186. int SRP_Verify_B_mod_N(const BIGNUM *B, const BIGNUM *N)
  187. {
  188. BIGNUM *r;
  189. BN_CTX *bn_ctx;
  190. int ret = 0;
  191. if (B == NULL || N == NULL || (bn_ctx = BN_CTX_new()) == NULL)
  192. return 0;
  193. if ((r = BN_new()) == NULL)
  194. goto err;
  195. /* Checks if B % N == 0 */
  196. if (!BN_nnmod(r, B, N, bn_ctx))
  197. goto err;
  198. ret = !BN_is_zero(r);
  199. err:
  200. BN_CTX_free(bn_ctx);
  201. BN_free(r);
  202. return ret;
  203. }
  204. int SRP_Verify_A_mod_N(const BIGNUM *A, const BIGNUM *N)
  205. {
  206. /* Checks if A % N == 0 */
  207. return SRP_Verify_B_mod_N(A, N);
  208. }
  209. static SRP_gN knowngN[] = {
  210. {"8192", &bn_generator_19, &bn_group_8192},
  211. {"6144", &bn_generator_5, &bn_group_6144},
  212. {"4096", &bn_generator_5, &bn_group_4096},
  213. {"3072", &bn_generator_5, &bn_group_3072},
  214. {"2048", &bn_generator_2, &bn_group_2048},
  215. {"1536", &bn_generator_2, &bn_group_1536},
  216. {"1024", &bn_generator_2, &bn_group_1024},
  217. };
  218. # define KNOWN_GN_NUMBER sizeof(knowngN) / sizeof(SRP_gN)
  219. /*
  220. * Check if G and N are known parameters. The values have been generated
  221. * from the ietf-tls-srp draft version 8
  222. */
  223. char *SRP_check_known_gN_param(const BIGNUM *g, const BIGNUM *N)
  224. {
  225. size_t i;
  226. if ((g == NULL) || (N == NULL))
  227. return 0;
  228. for (i = 0; i < KNOWN_GN_NUMBER; i++) {
  229. if (BN_cmp(knowngN[i].g, g) == 0 && BN_cmp(knowngN[i].N, N) == 0)
  230. return knowngN[i].id;
  231. }
  232. return NULL;
  233. }
  234. SRP_gN *SRP_get_default_gN(const char *id)
  235. {
  236. size_t i;
  237. if (id == NULL)
  238. return knowngN;
  239. for (i = 0; i < KNOWN_GN_NUMBER; i++) {
  240. if (strcmp(knowngN[i].id, id) == 0)
  241. return knowngN + i;
  242. }
  243. return NULL;
  244. }
  245. #endif