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