xref: /minix3/crypto/external/bsd/openssl/dist/crypto/srp/srp_lib.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1ebfedea0SLionel Sambuc /* crypto/srp/srp_lib.c */
2*0a6a1f1dSLionel Sambuc /*
3*0a6a1f1dSLionel Sambuc  * Written by Christophe Renou (christophe.renou@edelweb.fr) with the
4*0a6a1f1dSLionel Sambuc  * precious help of Peter Sylvester (peter.sylvester@edelweb.fr) for the
5*0a6a1f1dSLionel Sambuc  * EdelKey project and contributed to the OpenSSL project 2004.
6ebfedea0SLionel Sambuc  */
7ebfedea0SLionel Sambuc /* ====================================================================
8ebfedea0SLionel Sambuc  * Copyright (c) 2004 The OpenSSL Project.  All rights reserved.
9ebfedea0SLionel Sambuc  *
10ebfedea0SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
11ebfedea0SLionel Sambuc  * modification, are permitted provided that the following conditions
12ebfedea0SLionel Sambuc  * are met:
13ebfedea0SLionel Sambuc  *
14ebfedea0SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
15ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
16ebfedea0SLionel Sambuc  *
17ebfedea0SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
18ebfedea0SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in
19ebfedea0SLionel Sambuc  *    the documentation and/or other materials provided with the
20ebfedea0SLionel Sambuc  *    distribution.
21ebfedea0SLionel Sambuc  *
22ebfedea0SLionel Sambuc  * 3. All advertising materials mentioning features or use of this
23ebfedea0SLionel Sambuc  *    software must display the following acknowledgment:
24ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
25ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26ebfedea0SLionel Sambuc  *
27ebfedea0SLionel Sambuc  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
28ebfedea0SLionel Sambuc  *    endorse or promote products derived from this software without
29ebfedea0SLionel Sambuc  *    prior written permission. For written permission, please contact
30ebfedea0SLionel Sambuc  *    licensing@OpenSSL.org.
31ebfedea0SLionel Sambuc  *
32ebfedea0SLionel Sambuc  * 5. Products derived from this software may not be called "OpenSSL"
33ebfedea0SLionel Sambuc  *    nor may "OpenSSL" appear in their names without prior written
34ebfedea0SLionel Sambuc  *    permission of the OpenSSL Project.
35ebfedea0SLionel Sambuc  *
36ebfedea0SLionel Sambuc  * 6. Redistributions of any form whatsoever must retain the following
37ebfedea0SLionel Sambuc  *    acknowledgment:
38ebfedea0SLionel Sambuc  *    "This product includes software developed by the OpenSSL Project
39ebfedea0SLionel Sambuc  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40ebfedea0SLionel Sambuc  *
41ebfedea0SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
42ebfedea0SLionel Sambuc  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43ebfedea0SLionel Sambuc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44ebfedea0SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
45ebfedea0SLionel Sambuc  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46ebfedea0SLionel Sambuc  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47ebfedea0SLionel Sambuc  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48ebfedea0SLionel Sambuc  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49ebfedea0SLionel Sambuc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50ebfedea0SLionel Sambuc  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51ebfedea0SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52ebfedea0SLionel Sambuc  * OF THE POSSIBILITY OF SUCH DAMAGE.
53ebfedea0SLionel Sambuc  * ====================================================================
54ebfedea0SLionel Sambuc  *
55ebfedea0SLionel Sambuc  * This product includes cryptographic software written by Eric Young
56ebfedea0SLionel Sambuc  * (eay@cryptsoft.com).  This product includes software written by Tim
57ebfedea0SLionel Sambuc  * Hudson (tjh@cryptsoft.com).
58ebfedea0SLionel Sambuc  *
59ebfedea0SLionel Sambuc  */
60ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_SRP
61ebfedea0SLionel Sambuc # include "cryptlib.h"
62ebfedea0SLionel Sambuc # include "srp_lcl.h"
63ebfedea0SLionel Sambuc # include <openssl/srp.h>
64ebfedea0SLionel Sambuc # include <openssl/evp.h>
65ebfedea0SLionel Sambuc 
66ebfedea0SLionel Sambuc # if (BN_BYTES == 8)
67*0a6a1f1dSLionel Sambuc #  if (defined(_WIN32) || defined(_WIN64)) && !defined(__MINGW32__)
68*0a6a1f1dSLionel Sambuc #   define bn_pack4(a1,a2,a3,a4) ((a1##UI64<<48)|(a2##UI64<<32)|(a3##UI64<<16)|a4##UI64)
69*0a6a1f1dSLionel Sambuc #  elif defined(__arch64__)
70*0a6a1f1dSLionel Sambuc #   define bn_pack4(a1,a2,a3,a4) ((a1##UL<<48)|(a2##UL<<32)|(a3##UL<<16)|a4##UL)
71*0a6a1f1dSLionel Sambuc #  else
72*0a6a1f1dSLionel Sambuc #   define bn_pack4(a1,a2,a3,a4) ((a1##ULL<<48)|(a2##ULL<<32)|(a3##ULL<<16)|a4##ULL)
73ebfedea0SLionel Sambuc #  endif
74*0a6a1f1dSLionel Sambuc # elif (BN_BYTES == 4)
75*0a6a1f1dSLionel Sambuc #  define bn_pack4(a1,a2,a3,a4)  ((a3##UL<<16)|a4##UL), ((a1##UL<<16)|a2##UL)
76*0a6a1f1dSLionel Sambuc # else
77*0a6a1f1dSLionel Sambuc #  error "unsupported BN_BYTES"
78ebfedea0SLionel Sambuc # endif
79ebfedea0SLionel Sambuc 
80ebfedea0SLionel Sambuc # include "srp_grps.h"
81ebfedea0SLionel Sambuc 
srp_Calc_k(BIGNUM * N,BIGNUM * g)82ebfedea0SLionel Sambuc static BIGNUM *srp_Calc_k(BIGNUM *N, BIGNUM *g)
83ebfedea0SLionel Sambuc {
84ebfedea0SLionel Sambuc     /* k = SHA1(N | PAD(g)) -- tls-srp draft 8 */
85ebfedea0SLionel Sambuc 
86ebfedea0SLionel Sambuc     unsigned char digest[SHA_DIGEST_LENGTH];
87ebfedea0SLionel Sambuc     unsigned char *tmp;
88ebfedea0SLionel Sambuc     EVP_MD_CTX ctxt;
89ebfedea0SLionel Sambuc     int longg;
90ebfedea0SLionel Sambuc     int longN = BN_num_bytes(N);
91ebfedea0SLionel Sambuc 
92*0a6a1f1dSLionel Sambuc     if (BN_ucmp(g, N) >= 0)
93*0a6a1f1dSLionel Sambuc         return NULL;
94*0a6a1f1dSLionel Sambuc 
95ebfedea0SLionel Sambuc     if ((tmp = OPENSSL_malloc(longN)) == NULL)
96ebfedea0SLionel Sambuc         return NULL;
97ebfedea0SLionel Sambuc     BN_bn2bin(N, tmp);
98ebfedea0SLionel Sambuc 
99ebfedea0SLionel Sambuc     EVP_MD_CTX_init(&ctxt);
100ebfedea0SLionel Sambuc     EVP_DigestInit_ex(&ctxt, EVP_sha1(), NULL);
101ebfedea0SLionel Sambuc     EVP_DigestUpdate(&ctxt, tmp, longN);
102ebfedea0SLionel Sambuc 
103ebfedea0SLionel Sambuc     memset(tmp, 0, longN);
104ebfedea0SLionel Sambuc     longg = BN_bn2bin(g, tmp);
105ebfedea0SLionel Sambuc     /* use the zeros behind to pad on left */
106ebfedea0SLionel Sambuc     EVP_DigestUpdate(&ctxt, tmp + longg, longN - longg);
107ebfedea0SLionel Sambuc     EVP_DigestUpdate(&ctxt, tmp, longg);
108ebfedea0SLionel Sambuc     OPENSSL_free(tmp);
109ebfedea0SLionel Sambuc 
110ebfedea0SLionel Sambuc     EVP_DigestFinal_ex(&ctxt, digest, NULL);
111ebfedea0SLionel Sambuc     EVP_MD_CTX_cleanup(&ctxt);
112ebfedea0SLionel Sambuc     return BN_bin2bn(digest, sizeof(digest), NULL);
113ebfedea0SLionel Sambuc }
114ebfedea0SLionel Sambuc 
SRP_Calc_u(BIGNUM * A,BIGNUM * B,BIGNUM * N)115ebfedea0SLionel Sambuc BIGNUM *SRP_Calc_u(BIGNUM *A, BIGNUM *B, BIGNUM *N)
116ebfedea0SLionel Sambuc {
117ebfedea0SLionel Sambuc     /* k = SHA1(PAD(A) || PAD(B) ) -- tls-srp draft 8 */
118ebfedea0SLionel Sambuc 
119ebfedea0SLionel Sambuc     BIGNUM *u;
120ebfedea0SLionel Sambuc     unsigned char cu[SHA_DIGEST_LENGTH];
121ebfedea0SLionel Sambuc     unsigned char *cAB;
122ebfedea0SLionel Sambuc     EVP_MD_CTX ctxt;
123ebfedea0SLionel Sambuc     int longN;
124ebfedea0SLionel Sambuc     if ((A == NULL) || (B == NULL) || (N == NULL))
125ebfedea0SLionel Sambuc         return NULL;
126ebfedea0SLionel Sambuc 
127*0a6a1f1dSLionel Sambuc     if (BN_ucmp(A, N) >= 0 || BN_ucmp(B, N) >= 0)
128*0a6a1f1dSLionel Sambuc         return NULL;
129*0a6a1f1dSLionel Sambuc 
130ebfedea0SLionel Sambuc     longN = BN_num_bytes(N);
131ebfedea0SLionel Sambuc 
132ebfedea0SLionel Sambuc     if ((cAB = OPENSSL_malloc(2 * longN)) == NULL)
133ebfedea0SLionel Sambuc         return NULL;
134ebfedea0SLionel Sambuc 
135ebfedea0SLionel Sambuc     memset(cAB, 0, longN);
136ebfedea0SLionel Sambuc 
137ebfedea0SLionel Sambuc     EVP_MD_CTX_init(&ctxt);
138ebfedea0SLionel Sambuc     EVP_DigestInit_ex(&ctxt, EVP_sha1(), NULL);
139ebfedea0SLionel Sambuc     EVP_DigestUpdate(&ctxt, cAB + BN_bn2bin(A, cAB + longN), longN);
140ebfedea0SLionel Sambuc     EVP_DigestUpdate(&ctxt, cAB + BN_bn2bin(B, cAB + longN), longN);
141ebfedea0SLionel Sambuc     OPENSSL_free(cAB);
142ebfedea0SLionel Sambuc     EVP_DigestFinal_ex(&ctxt, cu, NULL);
143ebfedea0SLionel Sambuc     EVP_MD_CTX_cleanup(&ctxt);
144ebfedea0SLionel Sambuc 
145ebfedea0SLionel Sambuc     if (!(u = BN_bin2bn(cu, sizeof(cu), NULL)))
146ebfedea0SLionel Sambuc         return NULL;
147ebfedea0SLionel Sambuc     if (!BN_is_zero(u))
148ebfedea0SLionel Sambuc         return u;
149ebfedea0SLionel Sambuc     BN_free(u);
150ebfedea0SLionel Sambuc     return NULL;
151ebfedea0SLionel Sambuc }
152ebfedea0SLionel Sambuc 
SRP_Calc_server_key(BIGNUM * A,BIGNUM * v,BIGNUM * u,BIGNUM * b,BIGNUM * N)153*0a6a1f1dSLionel Sambuc BIGNUM *SRP_Calc_server_key(BIGNUM *A, BIGNUM *v, BIGNUM *u, BIGNUM *b,
154*0a6a1f1dSLionel Sambuc                             BIGNUM *N)
155ebfedea0SLionel Sambuc {
156ebfedea0SLionel Sambuc     BIGNUM *tmp = NULL, *S = NULL;
157ebfedea0SLionel Sambuc     BN_CTX *bn_ctx;
158ebfedea0SLionel Sambuc 
159ebfedea0SLionel Sambuc     if (u == NULL || A == NULL || v == NULL || b == NULL || N == NULL)
160ebfedea0SLionel Sambuc         return NULL;
161ebfedea0SLionel Sambuc 
162ebfedea0SLionel Sambuc     if ((bn_ctx = BN_CTX_new()) == NULL ||
163*0a6a1f1dSLionel Sambuc         (tmp = BN_new()) == NULL || (S = BN_new()) == NULL)
164ebfedea0SLionel Sambuc         goto err;
165ebfedea0SLionel Sambuc 
166ebfedea0SLionel Sambuc     /* S = (A*v**u) ** b */
167ebfedea0SLionel Sambuc 
168ebfedea0SLionel Sambuc     if (!BN_mod_exp(tmp, v, u, N, bn_ctx))
169ebfedea0SLionel Sambuc         goto err;
170ebfedea0SLionel Sambuc     if (!BN_mod_mul(tmp, A, tmp, N, bn_ctx))
171ebfedea0SLionel Sambuc         goto err;
172ebfedea0SLionel Sambuc     if (!BN_mod_exp(S, tmp, b, N, bn_ctx))
173ebfedea0SLionel Sambuc         goto err;
174ebfedea0SLionel Sambuc  err:
175ebfedea0SLionel Sambuc     BN_CTX_free(bn_ctx);
176ebfedea0SLionel Sambuc     BN_clear_free(tmp);
177ebfedea0SLionel Sambuc     return S;
178ebfedea0SLionel Sambuc }
179ebfedea0SLionel Sambuc 
SRP_Calc_B(BIGNUM * b,BIGNUM * N,BIGNUM * g,BIGNUM * v)180ebfedea0SLionel Sambuc BIGNUM *SRP_Calc_B(BIGNUM *b, BIGNUM *N, BIGNUM *g, BIGNUM *v)
181ebfedea0SLionel Sambuc {
182ebfedea0SLionel Sambuc     BIGNUM *kv = NULL, *gb = NULL;
183ebfedea0SLionel Sambuc     BIGNUM *B = NULL, *k = NULL;
184ebfedea0SLionel Sambuc     BN_CTX *bn_ctx;
185ebfedea0SLionel Sambuc 
186ebfedea0SLionel Sambuc     if (b == NULL || N == NULL || g == NULL || v == NULL ||
187ebfedea0SLionel Sambuc         (bn_ctx = BN_CTX_new()) == NULL)
188ebfedea0SLionel Sambuc         return NULL;
189ebfedea0SLionel Sambuc 
190ebfedea0SLionel Sambuc     if ((kv = BN_new()) == NULL ||
191*0a6a1f1dSLionel Sambuc         (gb = BN_new()) == NULL || (B = BN_new()) == NULL)
192ebfedea0SLionel Sambuc         goto err;
193ebfedea0SLionel Sambuc 
194ebfedea0SLionel Sambuc     /* B = g**b + k*v */
195ebfedea0SLionel Sambuc 
196ebfedea0SLionel Sambuc     if (!BN_mod_exp(gb, g, b, N, bn_ctx) ||
197ebfedea0SLionel Sambuc         !(k = srp_Calc_k(N, g)) ||
198ebfedea0SLionel Sambuc         !BN_mod_mul(kv, v, k, N, bn_ctx) ||
199*0a6a1f1dSLionel Sambuc         !BN_mod_add(B, gb, kv, N, bn_ctx)) {
200ebfedea0SLionel Sambuc         BN_free(B);
201ebfedea0SLionel Sambuc         B = NULL;
202ebfedea0SLionel Sambuc     }
203ebfedea0SLionel Sambuc  err:
204ebfedea0SLionel Sambuc     BN_CTX_free(bn_ctx);
205ebfedea0SLionel Sambuc     BN_clear_free(kv);
206ebfedea0SLionel Sambuc     BN_clear_free(gb);
207ebfedea0SLionel Sambuc     BN_free(k);
208ebfedea0SLionel Sambuc     return B;
209ebfedea0SLionel Sambuc }
210ebfedea0SLionel Sambuc 
SRP_Calc_x(BIGNUM * s,const char * user,const char * pass)211ebfedea0SLionel Sambuc BIGNUM *SRP_Calc_x(BIGNUM *s, const char *user, const char *pass)
212ebfedea0SLionel Sambuc {
213ebfedea0SLionel Sambuc     unsigned char dig[SHA_DIGEST_LENGTH];
214ebfedea0SLionel Sambuc     EVP_MD_CTX ctxt;
215ebfedea0SLionel Sambuc     unsigned char *cs;
216ebfedea0SLionel Sambuc 
217*0a6a1f1dSLionel Sambuc     if ((s == NULL) || (user == NULL) || (pass == NULL))
218ebfedea0SLionel Sambuc         return NULL;
219ebfedea0SLionel Sambuc 
220ebfedea0SLionel Sambuc     if ((cs = OPENSSL_malloc(BN_num_bytes(s))) == NULL)
221ebfedea0SLionel Sambuc         return NULL;
222ebfedea0SLionel Sambuc 
223ebfedea0SLionel Sambuc     EVP_MD_CTX_init(&ctxt);
224ebfedea0SLionel Sambuc     EVP_DigestInit_ex(&ctxt, EVP_sha1(), NULL);
225ebfedea0SLionel Sambuc     EVP_DigestUpdate(&ctxt, user, strlen(user));
226ebfedea0SLionel Sambuc     EVP_DigestUpdate(&ctxt, ":", 1);
227ebfedea0SLionel Sambuc     EVP_DigestUpdate(&ctxt, pass, strlen(pass));
228ebfedea0SLionel Sambuc     EVP_DigestFinal_ex(&ctxt, dig, NULL);
229ebfedea0SLionel Sambuc 
230ebfedea0SLionel Sambuc     EVP_DigestInit_ex(&ctxt, EVP_sha1(), NULL);
231ebfedea0SLionel Sambuc     BN_bn2bin(s, cs);
232ebfedea0SLionel Sambuc     EVP_DigestUpdate(&ctxt, cs, BN_num_bytes(s));
233ebfedea0SLionel Sambuc     OPENSSL_free(cs);
234ebfedea0SLionel Sambuc     EVP_DigestUpdate(&ctxt, dig, sizeof(dig));
235ebfedea0SLionel Sambuc     EVP_DigestFinal_ex(&ctxt, dig, NULL);
236ebfedea0SLionel Sambuc     EVP_MD_CTX_cleanup(&ctxt);
237ebfedea0SLionel Sambuc 
238ebfedea0SLionel Sambuc     return BN_bin2bn(dig, sizeof(dig), NULL);
239ebfedea0SLionel Sambuc }
240ebfedea0SLionel Sambuc 
SRP_Calc_A(BIGNUM * a,BIGNUM * N,BIGNUM * g)241ebfedea0SLionel Sambuc BIGNUM *SRP_Calc_A(BIGNUM *a, BIGNUM *N, BIGNUM *g)
242ebfedea0SLionel Sambuc {
243ebfedea0SLionel Sambuc     BN_CTX *bn_ctx;
244ebfedea0SLionel Sambuc     BIGNUM *A = NULL;
245ebfedea0SLionel Sambuc 
246ebfedea0SLionel Sambuc     if (a == NULL || N == NULL || g == NULL ||
247ebfedea0SLionel Sambuc         (bn_ctx = BN_CTX_new()) == NULL)
248ebfedea0SLionel Sambuc         return NULL;
249ebfedea0SLionel Sambuc 
250*0a6a1f1dSLionel Sambuc     if ((A = BN_new()) != NULL && !BN_mod_exp(A, g, a, N, bn_ctx)) {
251ebfedea0SLionel Sambuc         BN_free(A);
252ebfedea0SLionel Sambuc         A = NULL;
253ebfedea0SLionel Sambuc     }
254ebfedea0SLionel Sambuc     BN_CTX_free(bn_ctx);
255ebfedea0SLionel Sambuc     return A;
256ebfedea0SLionel Sambuc }
257ebfedea0SLionel Sambuc 
SRP_Calc_client_key(BIGNUM * N,BIGNUM * B,BIGNUM * g,BIGNUM * x,BIGNUM * a,BIGNUM * u)258*0a6a1f1dSLionel Sambuc BIGNUM *SRP_Calc_client_key(BIGNUM *N, BIGNUM *B, BIGNUM *g, BIGNUM *x,
259*0a6a1f1dSLionel Sambuc                             BIGNUM *a, BIGNUM *u)
260ebfedea0SLionel Sambuc {
261ebfedea0SLionel Sambuc     BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;
262ebfedea0SLionel Sambuc     BN_CTX *bn_ctx;
263ebfedea0SLionel Sambuc 
264*0a6a1f1dSLionel Sambuc     if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL
265*0a6a1f1dSLionel Sambuc         || a == NULL || (bn_ctx = BN_CTX_new()) == NULL)
266ebfedea0SLionel Sambuc         return NULL;
267ebfedea0SLionel Sambuc 
268ebfedea0SLionel Sambuc     if ((tmp = BN_new()) == NULL ||
269ebfedea0SLionel Sambuc         (tmp2 = BN_new()) == NULL ||
270*0a6a1f1dSLionel Sambuc         (tmp3 = BN_new()) == NULL || (K = BN_new()) == NULL)
271ebfedea0SLionel Sambuc         goto err;
272ebfedea0SLionel Sambuc 
273ebfedea0SLionel Sambuc     if (!BN_mod_exp(tmp, g, x, N, bn_ctx))
274ebfedea0SLionel Sambuc         goto err;
275ebfedea0SLionel Sambuc     if (!(k = srp_Calc_k(N, g)))
276ebfedea0SLionel Sambuc         goto err;
277ebfedea0SLionel Sambuc     if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))
278ebfedea0SLionel Sambuc         goto err;
279ebfedea0SLionel Sambuc     if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))
280ebfedea0SLionel Sambuc         goto err;
281ebfedea0SLionel Sambuc 
282ebfedea0SLionel Sambuc     if (!BN_mod_mul(tmp3, u, x, N, bn_ctx))
283ebfedea0SLionel Sambuc         goto err;
284ebfedea0SLionel Sambuc     if (!BN_mod_add(tmp2, a, tmp3, N, bn_ctx))
285ebfedea0SLionel Sambuc         goto err;
286ebfedea0SLionel Sambuc     if (!BN_mod_exp(K, tmp, tmp2, N, bn_ctx))
287ebfedea0SLionel Sambuc         goto err;
288ebfedea0SLionel Sambuc 
289ebfedea0SLionel Sambuc  err:
290ebfedea0SLionel Sambuc     BN_CTX_free(bn_ctx);
291ebfedea0SLionel Sambuc     BN_clear_free(tmp);
292ebfedea0SLionel Sambuc     BN_clear_free(tmp2);
293ebfedea0SLionel Sambuc     BN_clear_free(tmp3);
294ebfedea0SLionel Sambuc     BN_free(k);
295ebfedea0SLionel Sambuc     return K;
296ebfedea0SLionel Sambuc }
297ebfedea0SLionel Sambuc 
SRP_Verify_B_mod_N(BIGNUM * B,BIGNUM * N)298ebfedea0SLionel Sambuc int SRP_Verify_B_mod_N(BIGNUM *B, BIGNUM *N)
299ebfedea0SLionel Sambuc {
300ebfedea0SLionel Sambuc     BIGNUM *r;
301ebfedea0SLionel Sambuc     BN_CTX *bn_ctx;
302ebfedea0SLionel Sambuc     int ret = 0;
303ebfedea0SLionel Sambuc 
304*0a6a1f1dSLionel Sambuc     if (B == NULL || N == NULL || (bn_ctx = BN_CTX_new()) == NULL)
305ebfedea0SLionel Sambuc         return 0;
306ebfedea0SLionel Sambuc 
307ebfedea0SLionel Sambuc     if ((r = BN_new()) == NULL)
308ebfedea0SLionel Sambuc         goto err;
309ebfedea0SLionel Sambuc     /* Checks if B % N == 0 */
310ebfedea0SLionel Sambuc     if (!BN_nnmod(r, B, N, bn_ctx))
311ebfedea0SLionel Sambuc         goto err;
312ebfedea0SLionel Sambuc     ret = !BN_is_zero(r);
313ebfedea0SLionel Sambuc  err:
314ebfedea0SLionel Sambuc     BN_CTX_free(bn_ctx);
315ebfedea0SLionel Sambuc     BN_free(r);
316ebfedea0SLionel Sambuc     return ret;
317ebfedea0SLionel Sambuc }
318ebfedea0SLionel Sambuc 
SRP_Verify_A_mod_N(BIGNUM * A,BIGNUM * N)319ebfedea0SLionel Sambuc int SRP_Verify_A_mod_N(BIGNUM *A, BIGNUM *N)
320ebfedea0SLionel Sambuc {
321ebfedea0SLionel Sambuc     /* Checks if A % N == 0 */
322ebfedea0SLionel Sambuc     return SRP_Verify_B_mod_N(A, N);
323ebfedea0SLionel Sambuc }
324ebfedea0SLionel Sambuc 
325*0a6a1f1dSLionel Sambuc /*
326*0a6a1f1dSLionel Sambuc  * Check if G and N are kwown parameters. The values have been generated
327*0a6a1f1dSLionel Sambuc  * from the ietf-tls-srp draft version 8
328ebfedea0SLionel Sambuc  */
SRP_check_known_gN_param(BIGNUM * g,BIGNUM * N)329ebfedea0SLionel Sambuc char *SRP_check_known_gN_param(BIGNUM *g, BIGNUM *N)
330ebfedea0SLionel Sambuc {
331ebfedea0SLionel Sambuc     size_t i;
332ebfedea0SLionel Sambuc     if ((g == NULL) || (N == NULL))
333ebfedea0SLionel Sambuc         return 0;
334ebfedea0SLionel Sambuc 
335ebfedea0SLionel Sambuc     srp_bn_print(g);
336ebfedea0SLionel Sambuc     srp_bn_print(N);
337ebfedea0SLionel Sambuc 
338*0a6a1f1dSLionel Sambuc     for (i = 0; i < KNOWN_GN_NUMBER; i++) {
339ebfedea0SLionel Sambuc         if (BN_cmp(knowngN[i].g, g) == 0 && BN_cmp(knowngN[i].N, N) == 0)
340ebfedea0SLionel Sambuc             return knowngN[i].id;
341ebfedea0SLionel Sambuc     }
342ebfedea0SLionel Sambuc     return NULL;
343ebfedea0SLionel Sambuc }
344ebfedea0SLionel Sambuc 
SRP_get_default_gN(const char * id)345ebfedea0SLionel Sambuc SRP_gN *SRP_get_default_gN(const char *id)
346ebfedea0SLionel Sambuc {
347ebfedea0SLionel Sambuc     size_t i;
348ebfedea0SLionel Sambuc 
349ebfedea0SLionel Sambuc     if (id == NULL)
350ebfedea0SLionel Sambuc         return knowngN;
351*0a6a1f1dSLionel Sambuc     for (i = 0; i < KNOWN_GN_NUMBER; i++) {
352ebfedea0SLionel Sambuc         if (strcmp(knowngN[i].id, id) == 0)
353ebfedea0SLionel Sambuc             return knowngN + i;
354ebfedea0SLionel Sambuc     }
355ebfedea0SLionel Sambuc     return NULL;
356ebfedea0SLionel Sambuc }
357ebfedea0SLionel Sambuc #endif
358