srp_lib.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 Apache License 2.0 (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 RFC 5054 */
  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. /* u = SHA1(PAD(A) || PAD(B) ) -- tls-srp RFC 5054 */
  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. BN_CTX *bn_ctx;
  153. if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL
  154. || a == NULL || (bn_ctx = BN_CTX_new()) == NULL)
  155. return NULL;
  156. if ((tmp = BN_new()) == NULL ||
  157. (tmp2 = BN_new()) == NULL ||
  158. (tmp3 = BN_new()) == NULL)
  159. goto err;
  160. if (!BN_mod_exp(tmp, g, x, N, bn_ctx))
  161. goto err;
  162. if ((k = srp_Calc_k(N, g)) == NULL)
  163. goto err;
  164. if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))
  165. goto err;
  166. if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))
  167. goto err;
  168. if (!BN_mul(tmp3, u, x, bn_ctx))
  169. goto err;
  170. if (!BN_add(tmp2, a, tmp3))
  171. goto err;
  172. K = BN_new();
  173. if (K != NULL && !BN_mod_exp(K, tmp, tmp2, N, bn_ctx)) {
  174. BN_free(K);
  175. K = NULL;
  176. }
  177. err:
  178. BN_CTX_free(bn_ctx);
  179. BN_clear_free(tmp);
  180. BN_clear_free(tmp2);
  181. BN_clear_free(tmp3);
  182. BN_free(k);
  183. return K;
  184. }
  185. int SRP_Verify_B_mod_N(const BIGNUM *B, const BIGNUM *N)
  186. {
  187. BIGNUM *r;
  188. BN_CTX *bn_ctx;
  189. int ret = 0;
  190. if (B == NULL || N == NULL || (bn_ctx = BN_CTX_new()) == NULL)
  191. return 0;
  192. if ((r = BN_new()) == NULL)
  193. goto err;
  194. /* Checks if B % N == 0 */
  195. if (!BN_nnmod(r, B, N, bn_ctx))
  196. goto err;
  197. ret = !BN_is_zero(r);
  198. err:
  199. BN_CTX_free(bn_ctx);
  200. BN_free(r);
  201. return ret;
  202. }
  203. int SRP_Verify_A_mod_N(const BIGNUM *A, const BIGNUM *N)
  204. {
  205. /* Checks if A % N == 0 */
  206. return SRP_Verify_B_mod_N(A, N);
  207. }
  208. static SRP_gN knowngN[] = {
  209. {"8192", &bn_generator_19, &bn_group_8192},
  210. {"6144", &bn_generator_5, &bn_group_6144},
  211. {"4096", &bn_generator_5, &bn_group_4096},
  212. {"3072", &bn_generator_5, &bn_group_3072},
  213. {"2048", &bn_generator_2, &bn_group_2048},
  214. {"1536", &bn_generator_2, &bn_group_1536},
  215. {"1024", &bn_generator_2, &bn_group_1024},
  216. };
  217. # define KNOWN_GN_NUMBER sizeof(knowngN) / sizeof(SRP_gN)
  218. /*
  219. * Check if G and N are known parameters. The values have been generated
  220. * from the IETF RFC 5054
  221. */
  222. char *SRP_check_known_gN_param(const BIGNUM *g, const BIGNUM *N)
  223. {
  224. size_t i;
  225. if ((g == NULL) || (N == NULL))
  226. return NULL;
  227. for (i = 0; i < KNOWN_GN_NUMBER; i++) {
  228. if (BN_cmp(knowngN[i].g, g) == 0 && BN_cmp(knowngN[i].N, N) == 0)
  229. return knowngN[i].id;
  230. }
  231. return NULL;
  232. }
  233. SRP_gN *SRP_get_default_gN(const char *id)
  234. {
  235. size_t i;
  236. if (id == NULL)
  237. return knowngN;
  238. for (i = 0; i < KNOWN_GN_NUMBER; i++) {
  239. if (strcmp(knowngN[i].id, id) == 0)
  240. return knowngN + i;
  241. }
  242. return NULL;
  243. }
  244. #endif