srp_lib.c 9.0 KB

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