2 * Copyright 2004-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2004, EdelKey Project. All Rights Reserved.
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
10 * Originally written by Christophe Renou and Peter Sylvester,
11 * for the EdelKey project.
14 /* All the SRP APIs in this file are deprecated */
15 #define OPENSSL_SUPPRESS_DEPRECATED
17 #ifndef OPENSSL_NO_SRP
18 # include "internal/cryptlib.h"
19 # include <openssl/sha.h>
20 # include <openssl/srp.h>
21 # include <openssl/evp.h>
22 # include "crypto/bn_srp.h"
24 /* calculate = SHA1(PAD(x) || PAD(y)) */
26 static BIGNUM *srp_Calc_xy(const BIGNUM *x, const BIGNUM *y, const BIGNUM *N,
27 OSSL_LIB_CTX *libctx, const char *propq)
29 unsigned char digest[SHA_DIGEST_LENGTH];
30 unsigned char *tmp = NULL;
31 int numN = BN_num_bytes(N);
33 EVP_MD *sha1 = EVP_MD_fetch(libctx, "SHA1", propq);
38 if (x != N && BN_ucmp(x, N) >= 0)
40 if (y != N && BN_ucmp(y, N) >= 0)
42 if ((tmp = OPENSSL_malloc(numN * 2)) == NULL)
44 if (BN_bn2binpad(x, tmp, numN) < 0
45 || BN_bn2binpad(y, tmp + numN, numN) < 0
46 || !EVP_Digest(tmp, numN * 2, digest, NULL, sha1, NULL))
48 res = BN_bin2bn(digest, sizeof(digest), NULL);
55 static BIGNUM *srp_Calc_k(const BIGNUM *N, const BIGNUM *g,
59 /* k = SHA1(N | PAD(g)) -- tls-srp RFC 5054 */
60 return srp_Calc_xy(N, g, N, libctx, propq);
63 BIGNUM *SRP_Calc_u_ex(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N,
64 OSSL_LIB_CTX *libctx, const char *propq)
66 /* u = SHA1(PAD(A) || PAD(B) ) -- tls-srp RFC 5054 */
67 return srp_Calc_xy(A, B, N, libctx, propq);
70 BIGNUM *SRP_Calc_u(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N)
72 /* u = SHA1(PAD(A) || PAD(B) ) -- tls-srp RFC 5054 */
73 return srp_Calc_xy(A, B, N, NULL, NULL);
76 BIGNUM *SRP_Calc_server_key(const BIGNUM *A, const BIGNUM *v, const BIGNUM *u,
77 const BIGNUM *b, const BIGNUM *N)
79 BIGNUM *tmp = NULL, *S = NULL;
82 if (u == NULL || A == NULL || v == NULL || b == NULL || N == NULL)
85 if ((bn_ctx = BN_CTX_new()) == NULL || (tmp = BN_new()) == NULL)
88 /* S = (A*v**u) ** b */
90 if (!BN_mod_exp(tmp, v, u, N, bn_ctx))
92 if (!BN_mod_mul(tmp, A, tmp, N, bn_ctx))
96 if (S != NULL && !BN_mod_exp(S, tmp, b, N, bn_ctx)) {
106 BIGNUM *SRP_Calc_B_ex(const BIGNUM *b, const BIGNUM *N, const BIGNUM *g,
107 const BIGNUM *v, OSSL_LIB_CTX *libctx, const char *propq)
109 BIGNUM *kv = NULL, *gb = NULL;
110 BIGNUM *B = NULL, *k = NULL;
113 if (b == NULL || N == NULL || g == NULL || v == NULL ||
114 (bn_ctx = BN_CTX_new_ex(libctx)) == NULL)
117 if ((kv = BN_new()) == NULL ||
118 (gb = BN_new()) == NULL || (B = BN_new()) == NULL)
123 if (!BN_mod_exp(gb, g, b, N, bn_ctx)
124 || (k = srp_Calc_k(N, g, libctx, propq)) == NULL
125 || !BN_mod_mul(kv, v, k, N, bn_ctx)
126 || !BN_mod_add(B, gb, kv, N, bn_ctx)) {
138 BIGNUM *SRP_Calc_B(const BIGNUM *b, const BIGNUM *N, const BIGNUM *g,
141 return SRP_Calc_B_ex(b, N, g, v, NULL, NULL);
144 BIGNUM *SRP_Calc_x_ex(const BIGNUM *s, const char *user, const char *pass,
145 OSSL_LIB_CTX *libctx, const char *propq)
147 unsigned char dig[SHA_DIGEST_LENGTH];
149 unsigned char *cs = NULL;
153 if ((s == NULL) || (user == NULL) || (pass == NULL))
156 ctxt = EVP_MD_CTX_new();
159 if ((cs = OPENSSL_malloc(BN_num_bytes(s))) == NULL)
162 sha1 = EVP_MD_fetch(libctx, "SHA1", propq);
166 if (!EVP_DigestInit_ex(ctxt, sha1, NULL)
167 || !EVP_DigestUpdate(ctxt, user, strlen(user))
168 || !EVP_DigestUpdate(ctxt, ":", 1)
169 || !EVP_DigestUpdate(ctxt, pass, strlen(pass))
170 || !EVP_DigestFinal_ex(ctxt, dig, NULL)
171 || !EVP_DigestInit_ex(ctxt, sha1, NULL))
173 if (BN_bn2bin(s, cs) < 0)
175 if (!EVP_DigestUpdate(ctxt, cs, BN_num_bytes(s)))
178 if (!EVP_DigestUpdate(ctxt, dig, sizeof(dig))
179 || !EVP_DigestFinal_ex(ctxt, dig, NULL))
182 res = BN_bin2bn(dig, sizeof(dig), NULL);
187 EVP_MD_CTX_free(ctxt);
191 BIGNUM *SRP_Calc_x(const BIGNUM *s, const char *user, const char *pass)
193 return SRP_Calc_x_ex(s, user, pass, NULL, NULL);
196 BIGNUM *SRP_Calc_A(const BIGNUM *a, const BIGNUM *N, const BIGNUM *g)
201 if (a == NULL || N == NULL || g == NULL || (bn_ctx = BN_CTX_new()) == NULL)
204 if ((A = BN_new()) != NULL && !BN_mod_exp(A, g, a, N, bn_ctx)) {
212 BIGNUM *SRP_Calc_client_key_ex(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,
213 const BIGNUM *x, const BIGNUM *a, const BIGNUM *u,
214 OSSL_LIB_CTX *libctx, const char *propq)
216 BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;
220 if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL
221 || a == NULL || (bn_ctx = BN_CTX_new_ex(libctx)) == NULL)
224 if ((tmp = BN_new()) == NULL ||
225 (tmp2 = BN_new()) == NULL ||
226 (tmp3 = BN_new()) == NULL ||
227 (xtmp = BN_new()) == NULL)
230 BN_with_flags(xtmp, x, BN_FLG_CONSTTIME);
231 BN_set_flags(tmp, BN_FLG_CONSTTIME);
232 if (!BN_mod_exp(tmp, g, xtmp, N, bn_ctx))
234 if ((k = srp_Calc_k(N, g, libctx, propq)) == NULL)
236 if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))
238 if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))
240 if (!BN_mul(tmp3, u, xtmp, bn_ctx))
242 if (!BN_add(tmp2, a, tmp3))
245 if (K != NULL && !BN_mod_exp(K, tmp, tmp2, N, bn_ctx)) {
260 BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,
261 const BIGNUM *x, const BIGNUM *a, const BIGNUM *u)
263 return SRP_Calc_client_key_ex(N, B, g, x, a, u, NULL, NULL);
266 int SRP_Verify_B_mod_N(const BIGNUM *B, const BIGNUM *N)
272 if (B == NULL || N == NULL || (bn_ctx = BN_CTX_new()) == NULL)
275 if ((r = BN_new()) == NULL)
277 /* Checks if B % N == 0 */
278 if (!BN_nnmod(r, B, N, bn_ctx))
280 ret = !BN_is_zero(r);
287 int SRP_Verify_A_mod_N(const BIGNUM *A, const BIGNUM *N)
289 /* Checks if A % N == 0 */
290 return SRP_Verify_B_mod_N(A, N);
293 static SRP_gN knowngN[] = {
294 {"8192", &ossl_bn_generator_19, &ossl_bn_group_8192},
295 {"6144", &ossl_bn_generator_5, &ossl_bn_group_6144},
296 {"4096", &ossl_bn_generator_5, &ossl_bn_group_4096},
297 {"3072", &ossl_bn_generator_5, &ossl_bn_group_3072},
298 {"2048", &ossl_bn_generator_2, &ossl_bn_group_2048},
299 {"1536", &ossl_bn_generator_2, &ossl_bn_group_1536},
300 {"1024", &ossl_bn_generator_2, &ossl_bn_group_1024},
303 # define KNOWN_GN_NUMBER sizeof(knowngN) / sizeof(SRP_gN)
306 * Check if G and N are known parameters. The values have been generated
307 * from the IETF RFC 5054
309 char *SRP_check_known_gN_param(const BIGNUM *g, const BIGNUM *N)
312 if ((g == NULL) || (N == NULL))
315 for (i = 0; i < KNOWN_GN_NUMBER; i++) {
316 if (BN_cmp(knowngN[i].g, g) == 0 && BN_cmp(knowngN[i].N, N) == 0)
317 return knowngN[i].id;
322 SRP_gN *SRP_get_default_gN(const char *id)
328 for (i = 0; i < KNOWN_GN_NUMBER; i++) {
329 if (strcmp(knowngN[i].id, id) == 0)