1 /* crypto/srp/srp_lib.c */
3 * Written by Christophe Renou (christophe.renou@edelweb.fr) with the
4 * precious help of Peter Sylvester (peter.sylvester@edelweb.fr) for the
5 * EdelKey project and contributed to the OpenSSL project 2004.
7 /* ====================================================================
8 * Copyright (c) 2004 The OpenSSL Project. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
22 * 3. All advertising materials mentioning features or use of this
23 * software must display the following acknowledgment:
24 * "This product includes software developed by the OpenSSL Project
25 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
27 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
28 * endorse or promote products derived from this software without
29 * prior written permission. For written permission, please contact
30 * licensing@OpenSSL.org.
32 * 5. Products derived from this software may not be called "OpenSSL"
33 * nor may "OpenSSL" appear in their names without prior written
34 * permission of the OpenSSL Project.
36 * 6. Redistributions of any form whatsoever must retain the following
38 * "This product includes software developed by the OpenSSL Project
39 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
41 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
42 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52 * OF THE POSSIBILITY OF SUCH DAMAGE.
53 * ====================================================================
55 * This product includes cryptographic software written by Eric Young
56 * (eay@cryptsoft.com). This product includes software written by Tim
57 * Hudson (tjh@cryptsoft.com).
60 #ifndef OPENSSL_NO_SRP
61 # include "cryptlib.h"
63 # include <openssl/srp.h>
64 # include <openssl/evp.h>
67 # if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__)
68 # define bn_pack4(a1,a2,a3,a4) ((a1##UI64<<48)|(a2##UI64<<32)|(a3##UI64<<16)|a4##UI64)
69 # elif defined(__arch64__)
70 # define bn_pack4(a1,a2,a3,a4) ((a1##UL<<48)|(a2##UL<<32)|(a3##UL<<16)|a4##UL)
72 # define bn_pack4(a1,a2,a3,a4) ((a1##ULL<<48)|(a2##ULL<<32)|(a3##ULL<<16)|a4##ULL)
74 # elif (BN_BYTES == 4)
75 # define bn_pack4(a1,a2,a3,a4) ((a3##UL<<16)|a4##UL), ((a1##UL<<16)|a2##UL)
77 # error "unsupported BN_BYTES"
80 # include "srp_grps.h"
82 static BIGNUM *srp_Calc_k(BIGNUM *N, BIGNUM *g)
84 /* k = SHA1(N | PAD(g)) -- tls-srp draft 8 */
86 unsigned char digest[SHA_DIGEST_LENGTH];
90 int longN = BN_num_bytes(N);
92 if (BN_ucmp(g, N) >= 0)
95 if ((tmp = OPENSSL_malloc(longN)) == NULL)
99 EVP_MD_CTX_init(&ctxt);
100 EVP_DigestInit_ex(&ctxt, EVP_sha1(), NULL);
101 EVP_DigestUpdate(&ctxt, tmp, longN);
103 memset(tmp, 0, longN);
104 longg = BN_bn2bin(g, tmp);
105 /* use the zeros behind to pad on left */
106 EVP_DigestUpdate(&ctxt, tmp + longg, longN - longg);
107 EVP_DigestUpdate(&ctxt, tmp, longg);
110 EVP_DigestFinal_ex(&ctxt, digest, NULL);
111 EVP_MD_CTX_cleanup(&ctxt);
112 return BN_bin2bn(digest, sizeof(digest), NULL);
115 BIGNUM *SRP_Calc_u(BIGNUM *A, BIGNUM *B, BIGNUM *N)
117 /* k = SHA1(PAD(A) || PAD(B) ) -- tls-srp draft 8 */
120 unsigned char cu[SHA_DIGEST_LENGTH];
124 if ((A == NULL) || (B == NULL) || (N == NULL))
127 if (BN_ucmp(A, N) >= 0 || BN_ucmp(B, N) >= 0)
130 longN = BN_num_bytes(N);
132 if ((cAB = OPENSSL_malloc(2 * longN)) == NULL)
135 memset(cAB, 0, longN);
137 EVP_MD_CTX_init(&ctxt);
138 EVP_DigestInit_ex(&ctxt, EVP_sha1(), NULL);
139 EVP_DigestUpdate(&ctxt, cAB + BN_bn2bin(A, cAB + longN), longN);
140 EVP_DigestUpdate(&ctxt, cAB + BN_bn2bin(B, cAB + longN), longN);
142 EVP_DigestFinal_ex(&ctxt, cu, NULL);
143 EVP_MD_CTX_cleanup(&ctxt);
145 if (!(u = BN_bin2bn(cu, sizeof(cu), NULL)))
153 BIGNUM *SRP_Calc_server_key(BIGNUM *A, BIGNUM *v, BIGNUM *u, BIGNUM *b,
156 BIGNUM *tmp = NULL, *S = NULL;
159 if (u == NULL || A == NULL || v == NULL || b == NULL || N == NULL)
162 if ((bn_ctx = BN_CTX_new()) == NULL || (tmp = BN_new()) == NULL)
165 /* S = (A*v**u) ** b */
167 if (!BN_mod_exp(tmp, v, u, N, bn_ctx))
169 if (!BN_mod_mul(tmp, A, tmp, N, bn_ctx))
173 if (S != NULL && !BN_mod_exp(S, tmp, b, N, bn_ctx)) {
183 BIGNUM *SRP_Calc_B(BIGNUM *b, BIGNUM *N, BIGNUM *g, BIGNUM *v)
185 BIGNUM *kv = NULL, *gb = NULL;
186 BIGNUM *B = NULL, *k = NULL;
189 if (b == NULL || N == NULL || g == NULL || v == NULL ||
190 (bn_ctx = BN_CTX_new()) == NULL)
193 if ((kv = BN_new()) == NULL ||
194 (gb = BN_new()) == NULL || (B = BN_new()) == NULL)
199 if (!BN_mod_exp(gb, g, b, N, bn_ctx) ||
200 !(k = srp_Calc_k(N, g)) ||
201 !BN_mod_mul(kv, v, k, N, bn_ctx) ||
202 !BN_mod_add(B, gb, kv, N, bn_ctx)) {
214 BIGNUM *SRP_Calc_x(BIGNUM *s, const char *user, const char *pass)
216 unsigned char dig[SHA_DIGEST_LENGTH];
220 if ((s == NULL) || (user == NULL) || (pass == NULL))
223 if ((cs = OPENSSL_malloc(BN_num_bytes(s))) == NULL)
226 EVP_MD_CTX_init(&ctxt);
227 EVP_DigestInit_ex(&ctxt, EVP_sha1(), NULL);
228 EVP_DigestUpdate(&ctxt, user, strlen(user));
229 EVP_DigestUpdate(&ctxt, ":", 1);
230 EVP_DigestUpdate(&ctxt, pass, strlen(pass));
231 EVP_DigestFinal_ex(&ctxt, dig, NULL);
233 EVP_DigestInit_ex(&ctxt, EVP_sha1(), NULL);
235 EVP_DigestUpdate(&ctxt, cs, BN_num_bytes(s));
237 EVP_DigestUpdate(&ctxt, dig, sizeof(dig));
238 EVP_DigestFinal_ex(&ctxt, dig, NULL);
239 EVP_MD_CTX_cleanup(&ctxt);
241 return BN_bin2bn(dig, sizeof(dig), NULL);
244 BIGNUM *SRP_Calc_A(BIGNUM *a, BIGNUM *N, BIGNUM *g)
249 if (a == NULL || N == NULL || g == NULL ||
250 (bn_ctx = BN_CTX_new()) == NULL)
253 if ((A = BN_new()) != NULL && !BN_mod_exp(A, g, a, N, bn_ctx)) {
261 BIGNUM *SRP_Calc_client_key(BIGNUM *N, BIGNUM *B, BIGNUM *g, BIGNUM *x,
262 BIGNUM *a, BIGNUM *u)
264 BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;
267 if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL
268 || a == NULL || (bn_ctx = BN_CTX_new()) == NULL)
271 if ((tmp = BN_new()) == NULL ||
272 (tmp2 = BN_new()) == NULL ||
273 (tmp3 = BN_new()) == NULL)
276 if (!BN_mod_exp(tmp, g, x, N, bn_ctx))
278 if (!(k = srp_Calc_k(N, g)))
280 if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))
282 if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))
285 if (!BN_mul(tmp3, u, x, bn_ctx))
287 if (!BN_add(tmp2, a, tmp3))
290 if (K != NULL && !BN_mod_exp(K, tmp, tmp2, N, bn_ctx)) {
304 int SRP_Verify_B_mod_N(BIGNUM *B, BIGNUM *N)
310 if (B == NULL || N == NULL || (bn_ctx = BN_CTX_new()) == NULL)
313 if ((r = BN_new()) == NULL)
315 /* Checks if B % N == 0 */
316 if (!BN_nnmod(r, B, N, bn_ctx))
318 ret = !BN_is_zero(r);
325 int SRP_Verify_A_mod_N(BIGNUM *A, BIGNUM *N)
327 /* Checks if A % N == 0 */
328 return SRP_Verify_B_mod_N(A, N);
332 * Check if G and N are kwown parameters. The values have been generated
333 * from the ietf-tls-srp draft version 8
335 char *SRP_check_known_gN_param(BIGNUM *g, BIGNUM *N)
338 if ((g == NULL) || (N == NULL))
344 for (i = 0; i < KNOWN_GN_NUMBER; i++) {
345 if (BN_cmp(knowngN[i].g, g) == 0 && BN_cmp(knowngN[i].N, N) == 0)
346 return knowngN[i].id;
351 SRP_gN *SRP_get_default_gN(const char *id)
357 for (i = 0; i < KNOWN_GN_NUMBER; i++) {
358 if (strcmp(knowngN[i].id, id) == 0)