2 * Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
10 #ifndef OPENSSL_NO_SRP
11 # include "internal/cryptlib.h"
12 # include <openssl/sha.h>
13 # include <openssl/srp.h>
14 # include <openssl/evp.h>
15 # include "internal/bn_srp.h"
17 /* calculate = SHA1(PAD(x) || PAD(y)) */
19 static BIGNUM *srp_Calc_xy(const BIGNUM *x, const BIGNUM *y, const BIGNUM *N)
21 unsigned char digest[SHA_DIGEST_LENGTH];
22 unsigned char *tmp = NULL;
23 int numN = BN_num_bytes(N);
25 if (x != N && BN_ucmp(x, N) >= 0)
27 if (y != N && BN_ucmp(y, N) >= 0)
29 if ((tmp = OPENSSL_malloc(numN * 2)) == NULL)
31 if (BN_bn2binpad(x, tmp, numN) < 0
32 || BN_bn2binpad(y, tmp + numN, numN) < 0
33 || !EVP_Digest(tmp, numN * 2, digest, NULL, EVP_sha1(), NULL))
35 res = BN_bin2bn(digest, sizeof(digest), NULL);
41 static BIGNUM *srp_Calc_k(const BIGNUM *N, const BIGNUM *g)
43 /* k = SHA1(N | PAD(g)) -- tls-srp draft 8 */
44 return srp_Calc_xy(N, g, N);
47 BIGNUM *SRP_Calc_u(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N)
49 /* k = SHA1(PAD(A) || PAD(B) ) -- tls-srp draft 8 */
50 return srp_Calc_xy(A, B, N);
53 BIGNUM *SRP_Calc_server_key(const BIGNUM *A, const BIGNUM *v, const BIGNUM *u,
54 const BIGNUM *b, const BIGNUM *N)
56 BIGNUM *tmp = NULL, *S = NULL;
59 if (u == NULL || A == NULL || v == NULL || b == NULL || N == NULL)
62 if ((bn_ctx = BN_CTX_new()) == NULL || (tmp = BN_new()) == NULL)
65 /* S = (A*v**u) ** b */
67 if (!BN_mod_exp(tmp, v, u, N, bn_ctx))
69 if (!BN_mod_mul(tmp, A, tmp, N, bn_ctx))
73 if (S != NULL && !BN_mod_exp(S, tmp, b, N, bn_ctx)) {
83 BIGNUM *SRP_Calc_B(const BIGNUM *b, const BIGNUM *N, const BIGNUM *g,
86 BIGNUM *kv = NULL, *gb = NULL;
87 BIGNUM *B = NULL, *k = NULL;
90 if (b == NULL || N == NULL || g == NULL || v == NULL ||
91 (bn_ctx = BN_CTX_new()) == NULL)
94 if ((kv = BN_new()) == NULL ||
95 (gb = BN_new()) == NULL || (B = BN_new()) == NULL)
100 if (!BN_mod_exp(gb, g, b, N, bn_ctx)
101 || (k = srp_Calc_k(N, g)) == NULL
102 || !BN_mod_mul(kv, v, k, N, bn_ctx)
103 || !BN_mod_add(B, gb, kv, N, bn_ctx)) {
115 BIGNUM *SRP_Calc_x(const BIGNUM *s, const char *user, const char *pass)
117 unsigned char dig[SHA_DIGEST_LENGTH];
119 unsigned char *cs = NULL;
122 if ((s == NULL) || (user == NULL) || (pass == NULL))
125 ctxt = EVP_MD_CTX_new();
128 if ((cs = OPENSSL_malloc(BN_num_bytes(s))) == NULL)
131 if (!EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
132 || !EVP_DigestUpdate(ctxt, user, strlen(user))
133 || !EVP_DigestUpdate(ctxt, ":", 1)
134 || !EVP_DigestUpdate(ctxt, pass, strlen(pass))
135 || !EVP_DigestFinal_ex(ctxt, dig, NULL)
136 || !EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL))
139 if (!EVP_DigestUpdate(ctxt, cs, BN_num_bytes(s)))
142 if (!EVP_DigestUpdate(ctxt, dig, sizeof(dig))
143 || !EVP_DigestFinal_ex(ctxt, dig, NULL))
146 res = BN_bin2bn(dig, sizeof(dig), NULL);
150 EVP_MD_CTX_free(ctxt);
154 BIGNUM *SRP_Calc_A(const BIGNUM *a, const BIGNUM *N, const BIGNUM *g)
159 if (a == NULL || N == NULL || g == NULL || (bn_ctx = BN_CTX_new()) == NULL)
162 if ((A = BN_new()) != NULL && !BN_mod_exp(A, g, a, N, bn_ctx)) {
170 BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,
171 const BIGNUM *x, const BIGNUM *a, const BIGNUM *u)
173 BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;
176 if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL
177 || a == NULL || (bn_ctx = BN_CTX_new()) == NULL)
180 if ((tmp = BN_new()) == NULL ||
181 (tmp2 = BN_new()) == NULL ||
182 (tmp3 = BN_new()) == NULL)
185 if (!BN_mod_exp(tmp, g, x, N, bn_ctx))
187 if ((k = srp_Calc_k(N, g)) == NULL)
189 if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))
191 if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))
193 if (!BN_mul(tmp3, u, x, bn_ctx))
195 if (!BN_add(tmp2, a, tmp3))
198 if (K != NULL && !BN_mod_exp(K, tmp, tmp2, N, bn_ctx)) {
212 int SRP_Verify_B_mod_N(const BIGNUM *B, const BIGNUM *N)
218 if (B == NULL || N == NULL || (bn_ctx = BN_CTX_new()) == NULL)
221 if ((r = BN_new()) == NULL)
223 /* Checks if B % N == 0 */
224 if (!BN_nnmod(r, B, N, bn_ctx))
226 ret = !BN_is_zero(r);
233 int SRP_Verify_A_mod_N(const BIGNUM *A, const BIGNUM *N)
235 /* Checks if A % N == 0 */
236 return SRP_Verify_B_mod_N(A, N);
239 static SRP_gN knowngN[] = {
240 {"8192", &bn_generator_19, &bn_group_8192},
241 {"6144", &bn_generator_5, &bn_group_6144},
242 {"4096", &bn_generator_5, &bn_group_4096},
243 {"3072", &bn_generator_5, &bn_group_3072},
244 {"2048", &bn_generator_2, &bn_group_2048},
245 {"1536", &bn_generator_2, &bn_group_1536},
246 {"1024", &bn_generator_2, &bn_group_1024},
249 # define KNOWN_GN_NUMBER sizeof(knowngN) / sizeof(SRP_gN)
252 * Check if G and N are known parameters. The values have been generated
253 * from the ietf-tls-srp draft version 8
255 char *SRP_check_known_gN_param(const BIGNUM *g, const BIGNUM *N)
258 if ((g == NULL) || (N == NULL))
261 for (i = 0; i < KNOWN_GN_NUMBER; i++) {
262 if (BN_cmp(knowngN[i].g, g) == 0 && BN_cmp(knowngN[i].N, N) == 0)
263 return knowngN[i].id;
268 SRP_gN *SRP_get_default_gN(const char *id)
274 for (i = 0; i < KNOWN_GN_NUMBER; i++) {
275 if (strcmp(knowngN[i].id, id) == 0)