Cleanup: fix all sources that used EVP_MD_CTX_(create|init|destroy)
[openssl.git] / crypto / srp / srp_lib.c
1 /* crypto/srp/srp_lib.c */
2 /*
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.
6  */
7 /* ====================================================================
8  * Copyright (c) 2004 The OpenSSL Project.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  *
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
20  *    distribution.
21  *
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/)"
26  *
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.
31  *
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.
35  *
36  * 6. Redistributions of any form whatsoever must retain the following
37  *    acknowledgment:
38  *    "This product includes software developed by the OpenSSL Project
39  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40  *
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  * ====================================================================
54  *
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).
58  *
59  */
60 #ifndef OPENSSL_NO_SRP
61 # include "internal/cryptlib.h"
62 # include <openssl/sha.h>
63 # include <openssl/srp.h>
64 # include <openssl/evp.h>
65 # include "internal/bn_srp.h"
66
67 static BIGNUM *srp_Calc_k(BIGNUM *N, BIGNUM *g)
68 {
69     /* k = SHA1(N | PAD(g)) -- tls-srp draft 8 */
70
71     unsigned char digest[SHA_DIGEST_LENGTH];
72     unsigned char *tmp;
73     EVP_MD_CTX *ctxt = NULL;
74     int longg;
75     int longN = BN_num_bytes(N);
76     BIGNUM *res = NULL;
77
78     if (BN_ucmp(g, N) >= 0)
79         return NULL;
80
81     ctxt = EVP_MD_CTX_new();
82     if (ctxt == NULL)
83         return NULL;
84     if ((tmp = OPENSSL_malloc(longN)) == NULL)
85         goto err;
86     BN_bn2bin(N, tmp);
87
88     EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL);
89     EVP_DigestUpdate(ctxt, tmp, longN);
90
91     memset(tmp, 0, longN);
92     longg = BN_bn2bin(g, tmp);
93     /* use the zeros behind to pad on left */
94     EVP_DigestUpdate(ctxt, tmp + longg, longN - longg);
95     EVP_DigestUpdate(ctxt, tmp, longg);
96     OPENSSL_free(tmp);
97
98     EVP_DigestFinal_ex(ctxt, digest, NULL);
99     res = BN_bin2bn(digest, sizeof(digest), NULL);
100  err:
101     EVP_MD_CTX_free(ctxt);
102     return res;
103 }
104
105 BIGNUM *SRP_Calc_u(BIGNUM *A, BIGNUM *B, BIGNUM *N)
106 {
107     /* k = SHA1(PAD(A) || PAD(B) ) -- tls-srp draft 8 */
108
109     BIGNUM *u;
110     unsigned char cu[SHA_DIGEST_LENGTH];
111     unsigned char *cAB;
112     EVP_MD_CTX *ctxt = NULL;
113     int longN;
114     if ((A == NULL) || (B == NULL) || (N == NULL))
115         return NULL;
116
117     if (BN_ucmp(A, N) >= 0 || BN_ucmp(B, N) >= 0)
118         return NULL;
119
120     longN = BN_num_bytes(N);
121
122     ctxt = EVP_MD_CTX_new();
123     if (ctxt == NULL)
124         return NULL;
125     if ((cAB = OPENSSL_malloc(2 * longN)) == NULL)
126         goto err;
127
128     memset(cAB, 0, longN);
129
130     EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL);
131     EVP_DigestUpdate(ctxt, cAB + BN_bn2bin(A, cAB + longN), longN);
132     EVP_DigestUpdate(ctxt, cAB + BN_bn2bin(B, cAB + longN), longN);
133     OPENSSL_free(cAB);
134     EVP_DigestFinal_ex(ctxt, cu, NULL);
135
136     if ((u = BN_bin2bn(cu, sizeof(cu), NULL)) == NULL)
137         goto err;
138     if (BN_is_zero(u)) {
139         BN_free(u);
140         u = NULL;
141     }
142  err:
143     EVP_MD_CTX_free(ctxt);
144
145     return u;
146 }
147
148 BIGNUM *SRP_Calc_server_key(BIGNUM *A, BIGNUM *v, BIGNUM *u, BIGNUM *b,
149                             BIGNUM *N)
150 {
151     BIGNUM *tmp = NULL, *S = NULL;
152     BN_CTX *bn_ctx;
153
154     if (u == NULL || A == NULL || v == NULL || b == NULL || N == NULL)
155         return NULL;
156
157     if ((bn_ctx = BN_CTX_new()) == NULL ||
158         (tmp = BN_new()) == NULL || (S = BN_new()) == NULL)
159         goto err;
160
161     /* S = (A*v**u) ** b */
162
163     if (!BN_mod_exp(tmp, v, u, N, bn_ctx))
164         goto err;
165     if (!BN_mod_mul(tmp, A, tmp, N, bn_ctx))
166         goto err;
167     if (!BN_mod_exp(S, tmp, b, N, bn_ctx))
168         goto err;
169  err:
170     BN_CTX_free(bn_ctx);
171     BN_clear_free(tmp);
172     return S;
173 }
174
175 BIGNUM *SRP_Calc_B(BIGNUM *b, BIGNUM *N, BIGNUM *g, BIGNUM *v)
176 {
177     BIGNUM *kv = NULL, *gb = NULL;
178     BIGNUM *B = NULL, *k = NULL;
179     BN_CTX *bn_ctx;
180
181     if (b == NULL || N == NULL || g == NULL || v == NULL ||
182         (bn_ctx = BN_CTX_new()) == NULL)
183         return NULL;
184
185     if ((kv = BN_new()) == NULL ||
186         (gb = BN_new()) == NULL || (B = BN_new()) == NULL)
187         goto err;
188
189     /* B = g**b + k*v */
190
191     if (!BN_mod_exp(gb, g, b, N, bn_ctx)
192         || (k = srp_Calc_k(N, g)) == NULL
193         || !BN_mod_mul(kv, v, k, N, bn_ctx)
194         || !BN_mod_add(B, gb, kv, N, bn_ctx)) {
195         BN_free(B);
196         B = NULL;
197     }
198  err:
199     BN_CTX_free(bn_ctx);
200     BN_clear_free(kv);
201     BN_clear_free(gb);
202     BN_free(k);
203     return B;
204 }
205
206 BIGNUM *SRP_Calc_x(BIGNUM *s, const char *user, const char *pass)
207 {
208     unsigned char dig[SHA_DIGEST_LENGTH];
209     EVP_MD_CTX *ctxt;
210     unsigned char *cs;
211     BIGNUM *res = NULL;
212
213     if ((s == NULL) || (user == NULL) || (pass == NULL))
214         return NULL;
215
216     ctxt = EVP_MD_CTX_new();
217     if (ctxt == NULL)
218         return NULL;
219     if ((cs = OPENSSL_malloc(BN_num_bytes(s))) == NULL)
220         goto err;
221
222     EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL);
223     EVP_DigestUpdate(ctxt, user, strlen(user));
224     EVP_DigestUpdate(ctxt, ":", 1);
225     EVP_DigestUpdate(ctxt, pass, strlen(pass));
226     EVP_DigestFinal_ex(ctxt, dig, NULL);
227
228     EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL);
229     BN_bn2bin(s, cs);
230     EVP_DigestUpdate(ctxt, cs, BN_num_bytes(s));
231     OPENSSL_free(cs);
232     EVP_DigestUpdate(ctxt, dig, sizeof(dig));
233     EVP_DigestFinal_ex(ctxt, dig, NULL);
234
235     res = BN_bin2bn(dig, sizeof(dig), NULL);
236  err:
237     EVP_MD_CTX_free(ctxt);
238     return res;
239 }
240
241 BIGNUM *SRP_Calc_A(BIGNUM *a, BIGNUM *N, BIGNUM *g)
242 {
243     BN_CTX *bn_ctx;
244     BIGNUM *A = NULL;
245
246     if (a == NULL || N == NULL || g == NULL || (bn_ctx = BN_CTX_new()) == NULL)
247         return NULL;
248
249     if ((A = BN_new()) != NULL && !BN_mod_exp(A, g, a, N, bn_ctx)) {
250         BN_free(A);
251         A = NULL;
252     }
253     BN_CTX_free(bn_ctx);
254     return A;
255 }
256
257 BIGNUM *SRP_Calc_client_key(BIGNUM *N, BIGNUM *B, BIGNUM *g, BIGNUM *x,
258                             BIGNUM *a, BIGNUM *u)
259 {
260     BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;
261     BN_CTX *bn_ctx;
262
263     if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL
264         || a == NULL || (bn_ctx = BN_CTX_new()) == NULL)
265         return NULL;
266
267     if ((tmp = BN_new()) == NULL ||
268         (tmp2 = BN_new()) == NULL ||
269         (tmp3 = BN_new()) == NULL ||
270         (K = BN_new()) == NULL)
271         goto err;
272
273     if (!BN_mod_exp(tmp, g, x, N, bn_ctx))
274         goto err;
275     if ((k = srp_Calc_k(N, g)) == NULL)
276         goto err;
277     if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))
278         goto err;
279     if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))
280         goto err;
281     if (!BN_mod_mul(tmp3, u, x, N, bn_ctx))
282         goto err;
283     if (!BN_mod_add(tmp2, a, tmp3, N, bn_ctx))
284         goto err;
285     if (!BN_mod_exp(K, tmp, tmp2, N, bn_ctx))
286         goto err;
287
288  err:
289     BN_CTX_free(bn_ctx);
290     BN_clear_free(tmp);
291     BN_clear_free(tmp2);
292     BN_clear_free(tmp3);
293     BN_free(k);
294     return K;
295 }
296
297 int SRP_Verify_B_mod_N(BIGNUM *B, BIGNUM *N)
298 {
299     BIGNUM *r;
300     BN_CTX *bn_ctx;
301     int ret = 0;
302
303     if (B == NULL || N == NULL || (bn_ctx = BN_CTX_new()) == NULL)
304         return 0;
305
306     if ((r = BN_new()) == NULL)
307         goto err;
308     /* Checks if B % N == 0 */
309     if (!BN_nnmod(r, B, N, bn_ctx))
310         goto err;
311     ret = !BN_is_zero(r);
312  err:
313     BN_CTX_free(bn_ctx);
314     BN_free(r);
315     return ret;
316 }
317
318 int SRP_Verify_A_mod_N(BIGNUM *A, BIGNUM *N)
319 {
320     /* Checks if A % N == 0 */
321     return SRP_Verify_B_mod_N(A, N);
322 }
323
324 static SRP_gN knowngN[] = {
325     {"8192", (BIGNUM *)&bn_generator_19, (BIGNUM *)&bn_group_8192},
326     {"6144", (BIGNUM *)&bn_generator_5, (BIGNUM *)&bn_group_6144},
327     {"4096", (BIGNUM *)&bn_generator_5, (BIGNUM *)&bn_group_4096},
328     {"3072", (BIGNUM *)&bn_generator_5, (BIGNUM *)&bn_group_3072},
329     {"2048", (BIGNUM *)&bn_generator_2, (BIGNUM *)&bn_group_2048},
330     {"1536", (BIGNUM *)&bn_generator_2, (BIGNUM *)&bn_group_1536},
331     {"1024", (BIGNUM *)&bn_generator_2, (BIGNUM *)&bn_group_1024},
332 };
333
334 # define KNOWN_GN_NUMBER sizeof(knowngN) / sizeof(SRP_gN)
335
336 /*
337  * Check if G and N are kwown parameters. The values have been generated
338  * from the ietf-tls-srp draft version 8
339  */
340 char *SRP_check_known_gN_param(BIGNUM *g, BIGNUM *N)
341 {
342     size_t i;
343     if ((g == NULL) || (N == NULL))
344         return 0;
345
346     for (i = 0; i < KNOWN_GN_NUMBER; i++) {
347         if (BN_cmp(knowngN[i].g, g) == 0 && BN_cmp(knowngN[i].N, N) == 0)
348             return knowngN[i].id;
349     }
350     return NULL;
351 }
352
353 SRP_gN *SRP_get_default_gN(const char *id)
354 {
355     size_t i;
356
357     if (id == NULL)
358         return knowngN;
359     for (i = 0; i < KNOWN_GN_NUMBER; i++) {
360         if (strcmp(knowngN[i].id, id) == 0)
361             return knowngN + i;
362     }
363     return NULL;
364 }
365 #endif