srp_lib.c 7.5 KB

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