2
0

srp_lib.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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 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. /* All the SRP APIs in this file are deprecated */
  14. #define OPENSSL_SUPPRESS_DEPRECATED
  15. #ifndef OPENSSL_NO_SRP
  16. # include "internal/cryptlib.h"
  17. # include <openssl/sha.h>
  18. # include <openssl/srp.h>
  19. # include <openssl/evp.h>
  20. # include "crypto/bn_srp.h"
  21. /* calculate = SHA1(PAD(x) || PAD(y)) */
  22. static BIGNUM *srp_Calc_xy(const BIGNUM *x, const BIGNUM *y, const BIGNUM *N,
  23. OSSL_LIB_CTX *libctx, const char *propq)
  24. {
  25. unsigned char digest[SHA_DIGEST_LENGTH];
  26. unsigned char *tmp = NULL;
  27. int numN = BN_num_bytes(N);
  28. BIGNUM *res = NULL;
  29. EVP_MD *sha1 = EVP_MD_fetch(libctx, "SHA1", propq);
  30. if (sha1 == NULL)
  31. return NULL;
  32. if (x != N && BN_ucmp(x, N) >= 0)
  33. goto err;
  34. if (y != N && BN_ucmp(y, N) >= 0)
  35. goto err;
  36. if ((tmp = OPENSSL_malloc(numN * 2)) == NULL)
  37. goto err;
  38. if (BN_bn2binpad(x, tmp, numN) < 0
  39. || BN_bn2binpad(y, tmp + numN, numN) < 0
  40. || !EVP_Digest(tmp, numN * 2, digest, NULL, sha1, NULL))
  41. goto err;
  42. res = BN_bin2bn(digest, sizeof(digest), NULL);
  43. err:
  44. EVP_MD_free(sha1);
  45. OPENSSL_free(tmp);
  46. return res;
  47. }
  48. static BIGNUM *srp_Calc_k(const BIGNUM *N, const BIGNUM *g,
  49. OSSL_LIB_CTX *libctx,
  50. const char *propq)
  51. {
  52. /* k = SHA1(N | PAD(g)) -- tls-srp RFC 5054 */
  53. return srp_Calc_xy(N, g, N, libctx, propq);
  54. }
  55. BIGNUM *SRP_Calc_u_ex(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N,
  56. OSSL_LIB_CTX *libctx, const char *propq)
  57. {
  58. /* u = SHA1(PAD(A) || PAD(B) ) -- tls-srp RFC 5054 */
  59. return srp_Calc_xy(A, B, N, libctx, propq);
  60. }
  61. BIGNUM *SRP_Calc_u(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N)
  62. {
  63. /* u = SHA1(PAD(A) || PAD(B) ) -- tls-srp RFC 5054 */
  64. return srp_Calc_xy(A, B, N, NULL, NULL);
  65. }
  66. BIGNUM *SRP_Calc_server_key(const BIGNUM *A, const BIGNUM *v, const BIGNUM *u,
  67. const BIGNUM *b, const BIGNUM *N)
  68. {
  69. BIGNUM *tmp = NULL, *S = NULL;
  70. BN_CTX *bn_ctx;
  71. if (u == NULL || A == NULL || v == NULL || b == NULL || N == NULL)
  72. return NULL;
  73. if ((bn_ctx = BN_CTX_new()) == NULL || (tmp = BN_new()) == NULL)
  74. goto err;
  75. /* S = (A*v**u) ** b */
  76. if (!BN_mod_exp(tmp, v, u, N, bn_ctx))
  77. goto err;
  78. if (!BN_mod_mul(tmp, A, tmp, N, bn_ctx))
  79. goto err;
  80. S = BN_new();
  81. if (S != NULL && !BN_mod_exp(S, tmp, b, N, bn_ctx)) {
  82. BN_free(S);
  83. S = NULL;
  84. }
  85. err:
  86. BN_CTX_free(bn_ctx);
  87. BN_clear_free(tmp);
  88. return S;
  89. }
  90. BIGNUM *SRP_Calc_B_ex(const BIGNUM *b, const BIGNUM *N, const BIGNUM *g,
  91. const BIGNUM *v, OSSL_LIB_CTX *libctx, const char *propq)
  92. {
  93. BIGNUM *kv = NULL, *gb = NULL;
  94. BIGNUM *B = NULL, *k = NULL;
  95. BN_CTX *bn_ctx;
  96. if (b == NULL || N == NULL || g == NULL || v == NULL ||
  97. (bn_ctx = BN_CTX_new_ex(libctx)) == NULL)
  98. return NULL;
  99. if ((kv = BN_new()) == NULL ||
  100. (gb = BN_new()) == NULL || (B = BN_new()) == NULL)
  101. goto err;
  102. /* B = g**b + k*v */
  103. if (!BN_mod_exp(gb, g, b, N, bn_ctx)
  104. || (k = srp_Calc_k(N, g, libctx, propq)) == NULL
  105. || !BN_mod_mul(kv, v, k, N, bn_ctx)
  106. || !BN_mod_add(B, gb, kv, N, bn_ctx)) {
  107. BN_free(B);
  108. B = NULL;
  109. }
  110. err:
  111. BN_CTX_free(bn_ctx);
  112. BN_clear_free(kv);
  113. BN_clear_free(gb);
  114. BN_free(k);
  115. return B;
  116. }
  117. BIGNUM *SRP_Calc_B(const BIGNUM *b, const BIGNUM *N, const BIGNUM *g,
  118. const BIGNUM *v)
  119. {
  120. return SRP_Calc_B_ex(b, N, g, v, NULL, NULL);
  121. }
  122. BIGNUM *SRP_Calc_x_ex(const BIGNUM *s, const char *user, const char *pass,
  123. OSSL_LIB_CTX *libctx, const char *propq)
  124. {
  125. unsigned char dig[SHA_DIGEST_LENGTH];
  126. EVP_MD_CTX *ctxt;
  127. unsigned char *cs = NULL;
  128. BIGNUM *res = NULL;
  129. EVP_MD *sha1 = NULL;
  130. if ((s == NULL) || (user == NULL) || (pass == NULL))
  131. return NULL;
  132. ctxt = EVP_MD_CTX_new();
  133. if (ctxt == NULL)
  134. return NULL;
  135. if ((cs = OPENSSL_malloc(BN_num_bytes(s))) == NULL)
  136. goto err;
  137. sha1 = EVP_MD_fetch(libctx, "SHA1", propq);
  138. if (sha1 == NULL)
  139. goto err;
  140. if (!EVP_DigestInit_ex(ctxt, sha1, NULL)
  141. || !EVP_DigestUpdate(ctxt, user, strlen(user))
  142. || !EVP_DigestUpdate(ctxt, ":", 1)
  143. || !EVP_DigestUpdate(ctxt, pass, strlen(pass))
  144. || !EVP_DigestFinal_ex(ctxt, dig, NULL)
  145. || !EVP_DigestInit_ex(ctxt, sha1, NULL))
  146. goto err;
  147. if (BN_bn2bin(s, cs) < 0)
  148. goto err;
  149. if (!EVP_DigestUpdate(ctxt, cs, BN_num_bytes(s)))
  150. goto err;
  151. if (!EVP_DigestUpdate(ctxt, dig, sizeof(dig))
  152. || !EVP_DigestFinal_ex(ctxt, dig, NULL))
  153. goto err;
  154. res = BN_bin2bn(dig, sizeof(dig), NULL);
  155. err:
  156. EVP_MD_free(sha1);
  157. OPENSSL_free(cs);
  158. EVP_MD_CTX_free(ctxt);
  159. return res;
  160. }
  161. BIGNUM *SRP_Calc_x(const BIGNUM *s, const char *user, const char *pass)
  162. {
  163. return SRP_Calc_x_ex(s, user, pass, NULL, NULL);
  164. }
  165. BIGNUM *SRP_Calc_A(const BIGNUM *a, const BIGNUM *N, const BIGNUM *g)
  166. {
  167. BN_CTX *bn_ctx;
  168. BIGNUM *A = NULL;
  169. if (a == NULL || N == NULL || g == NULL || (bn_ctx = BN_CTX_new()) == NULL)
  170. return NULL;
  171. if ((A = BN_new()) != NULL && !BN_mod_exp(A, g, a, N, bn_ctx)) {
  172. BN_free(A);
  173. A = NULL;
  174. }
  175. BN_CTX_free(bn_ctx);
  176. return A;
  177. }
  178. BIGNUM *SRP_Calc_client_key_ex(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,
  179. const BIGNUM *x, const BIGNUM *a, const BIGNUM *u,
  180. OSSL_LIB_CTX *libctx, const char *propq)
  181. {
  182. BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;
  183. BIGNUM *xtmp = NULL;
  184. BN_CTX *bn_ctx;
  185. if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL
  186. || a == NULL || (bn_ctx = BN_CTX_new_ex(libctx)) == NULL)
  187. return NULL;
  188. if ((tmp = BN_new()) == NULL ||
  189. (tmp2 = BN_new()) == NULL ||
  190. (tmp3 = BN_new()) == NULL ||
  191. (xtmp = BN_new()) == NULL)
  192. goto err;
  193. BN_with_flags(xtmp, x, BN_FLG_CONSTTIME);
  194. BN_set_flags(tmp, BN_FLG_CONSTTIME);
  195. if (!BN_mod_exp(tmp, g, xtmp, N, bn_ctx))
  196. goto err;
  197. if ((k = srp_Calc_k(N, g, libctx, propq)) == NULL)
  198. goto err;
  199. if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))
  200. goto err;
  201. if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))
  202. goto err;
  203. if (!BN_mul(tmp3, u, xtmp, bn_ctx))
  204. goto err;
  205. if (!BN_add(tmp2, a, tmp3))
  206. goto err;
  207. K = BN_new();
  208. if (K != NULL && !BN_mod_exp(K, tmp, tmp2, N, bn_ctx)) {
  209. BN_free(K);
  210. K = NULL;
  211. }
  212. err:
  213. BN_CTX_free(bn_ctx);
  214. BN_free(xtmp);
  215. BN_clear_free(tmp);
  216. BN_clear_free(tmp2);
  217. BN_clear_free(tmp3);
  218. BN_free(k);
  219. return K;
  220. }
  221. BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,
  222. const BIGNUM *x, const BIGNUM *a, const BIGNUM *u)
  223. {
  224. return SRP_Calc_client_key_ex(N, B, g, x, a, u, NULL, NULL);
  225. }
  226. int SRP_Verify_B_mod_N(const BIGNUM *B, const BIGNUM *N)
  227. {
  228. BIGNUM *r;
  229. BN_CTX *bn_ctx;
  230. int ret = 0;
  231. if (B == NULL || N == NULL || (bn_ctx = BN_CTX_new()) == NULL)
  232. return 0;
  233. if ((r = BN_new()) == NULL)
  234. goto err;
  235. /* Checks if B % N == 0 */
  236. if (!BN_nnmod(r, B, N, bn_ctx))
  237. goto err;
  238. ret = !BN_is_zero(r);
  239. err:
  240. BN_CTX_free(bn_ctx);
  241. BN_free(r);
  242. return ret;
  243. }
  244. int SRP_Verify_A_mod_N(const BIGNUM *A, const BIGNUM *N)
  245. {
  246. /* Checks if A % N == 0 */
  247. return SRP_Verify_B_mod_N(A, N);
  248. }
  249. static SRP_gN knowngN[] = {
  250. {"8192", &ossl_bn_generator_19, &ossl_bn_group_8192},
  251. {"6144", &ossl_bn_generator_5, &ossl_bn_group_6144},
  252. {"4096", &ossl_bn_generator_5, &ossl_bn_group_4096},
  253. {"3072", &ossl_bn_generator_5, &ossl_bn_group_3072},
  254. {"2048", &ossl_bn_generator_2, &ossl_bn_group_2048},
  255. {"1536", &ossl_bn_generator_2, &ossl_bn_group_1536},
  256. {"1024", &ossl_bn_generator_2, &ossl_bn_group_1024},
  257. };
  258. # define KNOWN_GN_NUMBER sizeof(knowngN) / sizeof(SRP_gN)
  259. /*
  260. * Check if G and N are known parameters. The values have been generated
  261. * from the IETF RFC 5054
  262. */
  263. char *SRP_check_known_gN_param(const BIGNUM *g, const BIGNUM *N)
  264. {
  265. size_t i;
  266. if ((g == NULL) || (N == NULL))
  267. return NULL;
  268. for (i = 0; i < KNOWN_GN_NUMBER; i++) {
  269. if (BN_cmp(knowngN[i].g, g) == 0 && BN_cmp(knowngN[i].N, N) == 0)
  270. return knowngN[i].id;
  271. }
  272. return NULL;
  273. }
  274. SRP_gN *SRP_get_default_gN(const char *id)
  275. {
  276. size_t i;
  277. if (id == NULL)
  278. return knowngN;
  279. for (i = 0; i < KNOWN_GN_NUMBER; i++) {
  280. if (strcmp(knowngN[i].id, id) == 0)
  281. return knowngN + i;
  282. }
  283. return NULL;
  284. }
  285. #endif