srp_lib.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * Copyright 2004-2018 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 "internal/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. BN_bn2bin(s, cs);
  123. if (!EVP_DigestUpdate(ctxt, cs, BN_num_bytes(s)))
  124. goto err;
  125. if (!EVP_DigestUpdate(ctxt, dig, sizeof(dig))
  126. || !EVP_DigestFinal_ex(ctxt, dig, NULL))
  127. goto err;
  128. res = BN_bin2bn(dig, sizeof(dig), NULL);
  129. err:
  130. OPENSSL_free(cs);
  131. EVP_MD_CTX_free(ctxt);
  132. return res;
  133. }
  134. BIGNUM *SRP_Calc_A(const BIGNUM *a, const BIGNUM *N, const BIGNUM *g)
  135. {
  136. BN_CTX *bn_ctx;
  137. BIGNUM *A = NULL;
  138. if (a == NULL || N == NULL || g == NULL || (bn_ctx = BN_CTX_new()) == NULL)
  139. return NULL;
  140. if ((A = BN_new()) != NULL && !BN_mod_exp(A, g, a, N, bn_ctx)) {
  141. BN_free(A);
  142. A = NULL;
  143. }
  144. BN_CTX_free(bn_ctx);
  145. return A;
  146. }
  147. BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,
  148. const BIGNUM *x, const BIGNUM *a, const BIGNUM *u)
  149. {
  150. BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;
  151. BN_CTX *bn_ctx;
  152. if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL
  153. || a == NULL || (bn_ctx = BN_CTX_new()) == NULL)
  154. return NULL;
  155. if ((tmp = BN_new()) == NULL ||
  156. (tmp2 = BN_new()) == NULL ||
  157. (tmp3 = BN_new()) == NULL)
  158. goto err;
  159. if (!BN_mod_exp(tmp, g, x, N, bn_ctx))
  160. goto err;
  161. if ((k = srp_Calc_k(N, g)) == NULL)
  162. goto err;
  163. if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))
  164. goto err;
  165. if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))
  166. goto err;
  167. if (!BN_mul(tmp3, u, x, bn_ctx))
  168. goto err;
  169. if (!BN_add(tmp2, a, tmp3))
  170. goto err;
  171. K = BN_new();
  172. if (K != NULL && !BN_mod_exp(K, tmp, tmp2, N, bn_ctx)) {
  173. BN_free(K);
  174. K = NULL;
  175. }
  176. err:
  177. BN_CTX_free(bn_ctx);
  178. BN_clear_free(tmp);
  179. BN_clear_free(tmp2);
  180. BN_clear_free(tmp3);
  181. BN_free(k);
  182. return K;
  183. }
  184. int SRP_Verify_B_mod_N(const BIGNUM *B, const BIGNUM *N)
  185. {
  186. BIGNUM *r;
  187. BN_CTX *bn_ctx;
  188. int ret = 0;
  189. if (B == NULL || N == NULL || (bn_ctx = BN_CTX_new()) == NULL)
  190. return 0;
  191. if ((r = BN_new()) == NULL)
  192. goto err;
  193. /* Checks if B % N == 0 */
  194. if (!BN_nnmod(r, B, N, bn_ctx))
  195. goto err;
  196. ret = !BN_is_zero(r);
  197. err:
  198. BN_CTX_free(bn_ctx);
  199. BN_free(r);
  200. return ret;
  201. }
  202. int SRP_Verify_A_mod_N(const BIGNUM *A, const BIGNUM *N)
  203. {
  204. /* Checks if A % N == 0 */
  205. return SRP_Verify_B_mod_N(A, N);
  206. }
  207. static SRP_gN knowngN[] = {
  208. {"8192", &bn_generator_19, &bn_group_8192},
  209. {"6144", &bn_generator_5, &bn_group_6144},
  210. {"4096", &bn_generator_5, &bn_group_4096},
  211. {"3072", &bn_generator_5, &bn_group_3072},
  212. {"2048", &bn_generator_2, &bn_group_2048},
  213. {"1536", &bn_generator_2, &bn_group_1536},
  214. {"1024", &bn_generator_2, &bn_group_1024},
  215. };
  216. # define KNOWN_GN_NUMBER sizeof(knowngN) / sizeof(SRP_gN)
  217. /*
  218. * Check if G and N are known parameters. The values have been generated
  219. * from the ietf-tls-srp draft version 8
  220. */
  221. char *SRP_check_known_gN_param(const BIGNUM *g, const BIGNUM *N)
  222. {
  223. size_t i;
  224. if ((g == NULL) || (N == NULL))
  225. return 0;
  226. for (i = 0; i < KNOWN_GN_NUMBER; i++) {
  227. if (BN_cmp(knowngN[i].g, g) == 0 && BN_cmp(knowngN[i].N, N) == 0)
  228. return knowngN[i].id;
  229. }
  230. return NULL;
  231. }
  232. SRP_gN *SRP_get_default_gN(const char *id)
  233. {
  234. size_t i;
  235. if (id == NULL)
  236. return knowngN;
  237. for (i = 0; i < KNOWN_GN_NUMBER; i++) {
  238. if (strcmp(knowngN[i].id, id) == 0)
  239. return knowngN + i;
  240. }
  241. return NULL;
  242. }
  243. #endif