2 * Copyright 2004-2018 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2004, EdelKey Project. All Rights Reserved.
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
10 * Originally written by Christophe Renou and Peter Sylvester,
11 * for the EdelKey project.
14 #ifndef OPENSSL_NO_SRP
15 # include "internal/cryptlib.h"
16 # include <openssl/sha.h>
17 # include <openssl/srp.h>
18 # include <openssl/evp.h>
19 # include "internal/bn_srp.h"
21 /* calculate = SHA1(PAD(x) || PAD(y)) */
23 static BIGNUM *srp_Calc_xy(const BIGNUM *x, const BIGNUM *y, const BIGNUM *N)
25 unsigned char digest[SHA_DIGEST_LENGTH];
26 unsigned char *tmp = NULL;
27 int numN = BN_num_bytes(N);
30 if (x != N && BN_ucmp(x, N) >= 0)
32 if (y != N && BN_ucmp(y, N) >= 0)
34 if ((tmp = OPENSSL_malloc(numN * 2)) == NULL)
36 if (BN_bn2binpad(x, tmp, numN) < 0
37 || BN_bn2binpad(y, tmp + numN, numN) < 0
38 || !EVP_Digest(tmp, numN * 2, digest, NULL, EVP_sha1(), NULL))
40 res = BN_bin2bn(digest, sizeof(digest), NULL);
46 static BIGNUM *srp_Calc_k(const BIGNUM *N, const BIGNUM *g)
48 /* k = SHA1(N | PAD(g)) -- tls-srp draft 8 */
49 return srp_Calc_xy(N, g, N);
52 BIGNUM *SRP_Calc_u(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N)
54 /* k = SHA1(PAD(A) || PAD(B) ) -- tls-srp draft 8 */
55 return srp_Calc_xy(A, B, N);
58 BIGNUM *SRP_Calc_server_key(const BIGNUM *A, const BIGNUM *v, const BIGNUM *u,
59 const BIGNUM *b, const BIGNUM *N)
61 BIGNUM *tmp = NULL, *S = NULL;
64 if (u == NULL || A == NULL || v == NULL || b == NULL || N == NULL)
67 if ((bn_ctx = BN_CTX_new()) == NULL || (tmp = BN_new()) == NULL)
70 /* S = (A*v**u) ** b */
72 if (!BN_mod_exp(tmp, v, u, N, bn_ctx))
74 if (!BN_mod_mul(tmp, A, tmp, N, bn_ctx))
78 if (S != NULL && !BN_mod_exp(S, tmp, b, N, bn_ctx)) {
88 BIGNUM *SRP_Calc_B(const BIGNUM *b, const BIGNUM *N, const BIGNUM *g,
91 BIGNUM *kv = NULL, *gb = NULL;
92 BIGNUM *B = NULL, *k = NULL;
95 if (b == NULL || N == NULL || g == NULL || v == NULL ||
96 (bn_ctx = BN_CTX_new()) == NULL)
99 if ((kv = BN_new()) == NULL ||
100 (gb = BN_new()) == NULL || (B = BN_new()) == NULL)
105 if (!BN_mod_exp(gb, g, b, N, bn_ctx)
106 || (k = srp_Calc_k(N, g)) == NULL
107 || !BN_mod_mul(kv, v, k, N, bn_ctx)
108 || !BN_mod_add(B, gb, kv, N, bn_ctx)) {
120 BIGNUM *SRP_Calc_x(const BIGNUM *s, const char *user, const char *pass)
122 unsigned char dig[SHA_DIGEST_LENGTH];
124 unsigned char *cs = NULL;
127 if ((s == NULL) || (user == NULL) || (pass == NULL))
130 ctxt = EVP_MD_CTX_new();
133 if ((cs = OPENSSL_malloc(BN_num_bytes(s))) == NULL)
136 if (!EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
137 || !EVP_DigestUpdate(ctxt, user, strlen(user))
138 || !EVP_DigestUpdate(ctxt, ":", 1)
139 || !EVP_DigestUpdate(ctxt, pass, strlen(pass))
140 || !EVP_DigestFinal_ex(ctxt, dig, NULL)
141 || !EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL))
143 if (BN_bn2bin(s, cs) < 0)
145 if (!EVP_DigestUpdate(ctxt, cs, BN_num_bytes(s)))
148 if (!EVP_DigestUpdate(ctxt, dig, sizeof(dig))
149 || !EVP_DigestFinal_ex(ctxt, dig, NULL))
152 res = BN_bin2bn(dig, sizeof(dig), NULL);
156 EVP_MD_CTX_free(ctxt);
160 BIGNUM *SRP_Calc_A(const BIGNUM *a, const BIGNUM *N, const BIGNUM *g)
165 if (a == NULL || N == NULL || g == NULL || (bn_ctx = BN_CTX_new()) == NULL)
168 if ((A = BN_new()) != NULL && !BN_mod_exp(A, g, a, N, bn_ctx)) {
176 BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,
177 const BIGNUM *x, const BIGNUM *a, const BIGNUM *u)
179 BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;
182 if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL
183 || a == NULL || (bn_ctx = BN_CTX_new()) == NULL)
186 if ((tmp = BN_new()) == NULL ||
187 (tmp2 = BN_new()) == NULL ||
188 (tmp3 = BN_new()) == NULL)
191 if (!BN_mod_exp(tmp, g, x, N, bn_ctx))
193 if ((k = srp_Calc_k(N, g)) == NULL)
195 if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))
197 if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))
199 if (!BN_mul(tmp3, u, x, bn_ctx))
201 if (!BN_add(tmp2, a, tmp3))
204 if (K != NULL && !BN_mod_exp(K, tmp, tmp2, N, bn_ctx)) {
218 int SRP_Verify_B_mod_N(const BIGNUM *B, const BIGNUM *N)
224 if (B == NULL || N == NULL || (bn_ctx = BN_CTX_new()) == NULL)
227 if ((r = BN_new()) == NULL)
229 /* Checks if B % N == 0 */
230 if (!BN_nnmod(r, B, N, bn_ctx))
232 ret = !BN_is_zero(r);
239 int SRP_Verify_A_mod_N(const BIGNUM *A, const BIGNUM *N)
241 /* Checks if A % N == 0 */
242 return SRP_Verify_B_mod_N(A, N);
245 static SRP_gN knowngN[] = {
246 {"8192", &bn_generator_19, &bn_group_8192},
247 {"6144", &bn_generator_5, &bn_group_6144},
248 {"4096", &bn_generator_5, &bn_group_4096},
249 {"3072", &bn_generator_5, &bn_group_3072},
250 {"2048", &bn_generator_2, &bn_group_2048},
251 {"1536", &bn_generator_2, &bn_group_1536},
252 {"1024", &bn_generator_2, &bn_group_1024},
255 # define KNOWN_GN_NUMBER sizeof(knowngN) / sizeof(SRP_gN)
258 * Check if G and N are known parameters. The values have been generated
259 * from the ietf-tls-srp draft version 8
261 char *SRP_check_known_gN_param(const BIGNUM *g, const BIGNUM *N)
264 if ((g == NULL) || (N == NULL))
267 for (i = 0; i < KNOWN_GN_NUMBER; i++) {
268 if (BN_cmp(knowngN[i].g, g) == 0 && BN_cmp(knowngN[i].N, N) == 0)
269 return knowngN[i].id;
274 SRP_gN *SRP_get_default_gN(const char *id)
280 for (i = 0; i < KNOWN_GN_NUMBER; i++) {
281 if (strcmp(knowngN[i].id, id) == 0)