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 static BIGNUM *srp_Calc_k(const BIGNUM *N, const BIGNUM *g)
19 /* k = SHA1(N | PAD(g)) -- tls-srp draft 8 */
21 unsigned char digest[SHA_DIGEST_LENGTH];
22 unsigned char *tmp = NULL;
23 EVP_MD_CTX *ctxt = NULL;
25 int longN = BN_num_bytes(N);
28 if (BN_ucmp(g, N) >= 0)
31 ctxt = EVP_MD_CTX_new();
34 if ((tmp = OPENSSL_malloc(longN)) == NULL)
38 if (!EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
39 || !EVP_DigestUpdate(ctxt, tmp, longN))
42 memset(tmp, 0, longN);
43 longg = BN_bn2bin(g, tmp);
44 /* use the zeros behind to pad on left */
45 if (!EVP_DigestUpdate(ctxt, tmp + longg, longN - longg)
46 || !EVP_DigestUpdate(ctxt, tmp, longg))
49 if (!EVP_DigestFinal_ex(ctxt, digest, NULL))
51 res = BN_bin2bn(digest, sizeof(digest), NULL);
54 EVP_MD_CTX_free(ctxt);
58 BIGNUM *SRP_Calc_u(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N)
60 /* k = SHA1(PAD(A) || PAD(B) ) -- tls-srp draft 8 */
63 unsigned char cu[SHA_DIGEST_LENGTH];
64 unsigned char *cAB = NULL;
65 EVP_MD_CTX *ctxt = NULL;
67 if ((A == NULL) || (B == NULL) || (N == NULL))
70 if (BN_ucmp(A, N) >= 0 || BN_ucmp(B, N) >= 0)
73 longN = BN_num_bytes(N);
75 ctxt = EVP_MD_CTX_new();
78 if ((cAB = OPENSSL_malloc(2 * longN)) == NULL)
81 memset(cAB, 0, longN);
83 if (!EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
84 || !EVP_DigestUpdate(ctxt, cAB + BN_bn2bin(A, cAB + longN), longN)
85 || !EVP_DigestUpdate(ctxt, cAB + BN_bn2bin(B, cAB + longN), longN))
88 if (!EVP_DigestFinal_ex(ctxt, cu, NULL))
91 if ((u = BN_bin2bn(cu, sizeof(cu), NULL)) == NULL)
100 EVP_MD_CTX_free(ctxt);
105 BIGNUM *SRP_Calc_server_key(const BIGNUM *A, const BIGNUM *v, const BIGNUM *u,
106 const BIGNUM *b, const BIGNUM *N)
108 BIGNUM *tmp = NULL, *S = NULL;
111 if (u == NULL || A == NULL || v == NULL || b == NULL || N == NULL)
114 if ((bn_ctx = BN_CTX_new()) == NULL || (tmp = BN_new()) == NULL)
117 /* S = (A*v**u) ** b */
119 if (!BN_mod_exp(tmp, v, u, N, bn_ctx))
121 if (!BN_mod_mul(tmp, A, tmp, N, bn_ctx))
125 if (S != NULL && !BN_mod_exp(S, tmp, b, N, bn_ctx)) {
135 BIGNUM *SRP_Calc_B(const BIGNUM *b, const BIGNUM *N, const BIGNUM *g,
138 BIGNUM *kv = NULL, *gb = NULL;
139 BIGNUM *B = NULL, *k = NULL;
142 if (b == NULL || N == NULL || g == NULL || v == NULL ||
143 (bn_ctx = BN_CTX_new()) == NULL)
146 if ((kv = BN_new()) == NULL ||
147 (gb = BN_new()) == NULL || (B = BN_new()) == NULL)
152 if (!BN_mod_exp(gb, g, b, N, bn_ctx)
153 || (k = srp_Calc_k(N, g)) == NULL
154 || !BN_mod_mul(kv, v, k, N, bn_ctx)
155 || !BN_mod_add(B, gb, kv, N, bn_ctx)) {
167 BIGNUM *SRP_Calc_x(const BIGNUM *s, const char *user, const char *pass)
169 unsigned char dig[SHA_DIGEST_LENGTH];
174 if ((s == NULL) || (user == NULL) || (pass == NULL))
177 ctxt = EVP_MD_CTX_new();
180 if ((cs = OPENSSL_malloc(BN_num_bytes(s))) == NULL)
183 if (!EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
184 || !EVP_DigestUpdate(ctxt, user, strlen(user))
185 || !EVP_DigestUpdate(ctxt, ":", 1)
186 || !EVP_DigestUpdate(ctxt, pass, strlen(pass))
187 || !EVP_DigestFinal_ex(ctxt, dig, NULL)
188 || !EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL))
191 if (!EVP_DigestUpdate(ctxt, cs, BN_num_bytes(s)))
194 if (!EVP_DigestUpdate(ctxt, dig, sizeof(dig))
195 || !EVP_DigestFinal_ex(ctxt, dig, NULL))
198 res = BN_bin2bn(dig, sizeof(dig), NULL);
200 EVP_MD_CTX_free(ctxt);
204 BIGNUM *SRP_Calc_A(const BIGNUM *a, const BIGNUM *N, const BIGNUM *g)
209 if (a == NULL || N == NULL || g == NULL || (bn_ctx = BN_CTX_new()) == NULL)
212 if ((A = BN_new()) != NULL && !BN_mod_exp(A, g, a, N, bn_ctx)) {
220 BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,
221 const BIGNUM *x, const BIGNUM *a, const BIGNUM *u)
223 BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;
226 if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL
227 || a == NULL || (bn_ctx = BN_CTX_new()) == NULL)
230 if ((tmp = BN_new()) == NULL ||
231 (tmp2 = BN_new()) == NULL ||
232 (tmp3 = BN_new()) == NULL)
235 if (!BN_mod_exp(tmp, g, x, N, bn_ctx))
237 if ((k = srp_Calc_k(N, g)) == NULL)
239 if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))
241 if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))
243 if (!BN_mul(tmp3, u, x, bn_ctx))
245 if (!BN_add(tmp2, a, tmp3))
248 if (K != NULL && !BN_mod_exp(K, tmp, tmp2, N, bn_ctx)) {
262 int SRP_Verify_B_mod_N(const BIGNUM *B, const BIGNUM *N)
268 if (B == NULL || N == NULL || (bn_ctx = BN_CTX_new()) == NULL)
271 if ((r = BN_new()) == NULL)
273 /* Checks if B % N == 0 */
274 if (!BN_nnmod(r, B, N, bn_ctx))
276 ret = !BN_is_zero(r);
283 int SRP_Verify_A_mod_N(const BIGNUM *A, const BIGNUM *N)
285 /* Checks if A % N == 0 */
286 return SRP_Verify_B_mod_N(A, N);
289 static SRP_gN knowngN[] = {
290 {"8192", &bn_generator_19, &bn_group_8192},
291 {"6144", &bn_generator_5, &bn_group_6144},
292 {"4096", &bn_generator_5, &bn_group_4096},
293 {"3072", &bn_generator_5, &bn_group_3072},
294 {"2048", &bn_generator_2, &bn_group_2048},
295 {"1536", &bn_generator_2, &bn_group_1536},
296 {"1024", &bn_generator_2, &bn_group_1024},
299 # define KNOWN_GN_NUMBER sizeof(knowngN) / sizeof(SRP_gN)
302 * Check if G and N are known parameters. The values have been generated
303 * from the ietf-tls-srp draft version 8
305 char *SRP_check_known_gN_param(const BIGNUM *g, const BIGNUM *N)
308 if ((g == NULL) || (N == NULL))
311 for (i = 0; i < KNOWN_GN_NUMBER; i++) {
312 if (BN_cmp(knowngN[i].g, g) == 0 && BN_cmp(knowngN[i].N, N) == 0)
313 return knowngN[i].id;
318 SRP_gN *SRP_get_default_gN(const char *id)
324 for (i = 0; i < KNOWN_GN_NUMBER; i++) {
325 if (strcmp(knowngN[i].id, id) == 0)