constify SRP
[oweals/openssl.git] / crypto / srp / srp_lib.c
1 /*
2  * Copyright 2011-2016 The OpenSSL Project Authors. All Rights Reserved.
3  *
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
8  */
9
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"
16
17 static BIGNUM *srp_Calc_k(const BIGNUM *N, const BIGNUM *g)
18 {
19     /* k = SHA1(N | PAD(g)) -- tls-srp draft 8 */
20
21     unsigned char digest[SHA_DIGEST_LENGTH];
22     unsigned char *tmp;
23     EVP_MD_CTX *ctxt = NULL;
24     int longg;
25     int longN = BN_num_bytes(N);
26     BIGNUM *res = NULL;
27
28     if (BN_ucmp(g, N) >= 0)
29         return NULL;
30
31     ctxt = EVP_MD_CTX_new();
32     if (ctxt == NULL)
33         return NULL;
34     if ((tmp = OPENSSL_malloc(longN)) == NULL)
35         goto err;
36     BN_bn2bin(N, tmp);
37
38     EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL);
39     EVP_DigestUpdate(ctxt, tmp, longN);
40
41     memset(tmp, 0, longN);
42     longg = BN_bn2bin(g, tmp);
43     /* use the zeros behind to pad on left */
44     EVP_DigestUpdate(ctxt, tmp + longg, longN - longg);
45     EVP_DigestUpdate(ctxt, tmp, longg);
46     OPENSSL_free(tmp);
47
48     EVP_DigestFinal_ex(ctxt, digest, NULL);
49     res = BN_bin2bn(digest, sizeof(digest), NULL);
50  err:
51     EVP_MD_CTX_free(ctxt);
52     return res;
53 }
54
55 BIGNUM *SRP_Calc_u(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N)
56 {
57     /* k = SHA1(PAD(A) || PAD(B) ) -- tls-srp draft 8 */
58
59     BIGNUM *u = NULL;
60     unsigned char cu[SHA_DIGEST_LENGTH];
61     unsigned char *cAB = NULL;
62     EVP_MD_CTX *ctxt = NULL;
63     int longN;
64     if ((A == NULL) || (B == NULL) || (N == NULL))
65         return NULL;
66
67     if (BN_ucmp(A, N) >= 0 || BN_ucmp(B, N) >= 0)
68         return NULL;
69
70     longN = BN_num_bytes(N);
71
72     ctxt = EVP_MD_CTX_new();
73     if (ctxt == NULL)
74         return NULL;
75     if ((cAB = OPENSSL_malloc(2 * longN)) == NULL)
76         goto err;
77
78     memset(cAB, 0, longN);
79
80     EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL);
81     EVP_DigestUpdate(ctxt, cAB + BN_bn2bin(A, cAB + longN), longN);
82     EVP_DigestUpdate(ctxt, cAB + BN_bn2bin(B, cAB + longN), longN);
83     OPENSSL_free(cAB);
84     EVP_DigestFinal_ex(ctxt, cu, NULL);
85
86     if ((u = BN_bin2bn(cu, sizeof(cu), NULL)) == NULL)
87         goto err;
88     if (BN_is_zero(u)) {
89         BN_free(u);
90         u = NULL;
91     }
92  err:
93     EVP_MD_CTX_free(ctxt);
94
95     return u;
96 }
97
98 BIGNUM *SRP_Calc_server_key(const BIGNUM *A, const BIGNUM *v, const BIGNUM *u,
99                             const BIGNUM *b, const BIGNUM *N)
100 {
101     BIGNUM *tmp = NULL, *S = NULL;
102     BN_CTX *bn_ctx;
103
104     if (u == NULL || A == NULL || v == NULL || b == NULL || N == NULL)
105         return NULL;
106
107     if ((bn_ctx = BN_CTX_new()) == NULL || (tmp = BN_new()) == NULL)
108         goto err;
109
110     /* S = (A*v**u) ** b */
111
112     if (!BN_mod_exp(tmp, v, u, N, bn_ctx))
113         goto err;
114     if (!BN_mod_mul(tmp, A, tmp, N, bn_ctx))
115         goto err;
116
117     S = BN_new();
118     if (S != NULL && !BN_mod_exp(S, tmp, b, N, bn_ctx)) {
119         BN_free(S);
120         S = NULL;
121     }
122  err:
123     BN_CTX_free(bn_ctx);
124     BN_clear_free(tmp);
125     return S;
126 }
127
128 BIGNUM *SRP_Calc_B(const BIGNUM *b, const BIGNUM *N, const BIGNUM *g,
129                    const BIGNUM *v)
130 {
131     BIGNUM *kv = NULL, *gb = NULL;
132     BIGNUM *B = NULL, *k = NULL;
133     BN_CTX *bn_ctx;
134
135     if (b == NULL || N == NULL || g == NULL || v == NULL ||
136         (bn_ctx = BN_CTX_new()) == NULL)
137         return NULL;
138
139     if ((kv = BN_new()) == NULL ||
140         (gb = BN_new()) == NULL || (B = BN_new()) == NULL)
141         goto err;
142
143     /* B = g**b + k*v */
144
145     if (!BN_mod_exp(gb, g, b, N, bn_ctx)
146         || (k = srp_Calc_k(N, g)) == NULL
147         || !BN_mod_mul(kv, v, k, N, bn_ctx)
148         || !BN_mod_add(B, gb, kv, N, bn_ctx)) {
149         BN_free(B);
150         B = NULL;
151     }
152  err:
153     BN_CTX_free(bn_ctx);
154     BN_clear_free(kv);
155     BN_clear_free(gb);
156     BN_free(k);
157     return B;
158 }
159
160 BIGNUM *SRP_Calc_x(const BIGNUM *s, const char *user, const char *pass)
161 {
162     unsigned char dig[SHA_DIGEST_LENGTH];
163     EVP_MD_CTX *ctxt;
164     unsigned char *cs;
165     BIGNUM *res = NULL;
166
167     if ((s == NULL) || (user == NULL) || (pass == NULL))
168         return NULL;
169
170     ctxt = EVP_MD_CTX_new();
171     if (ctxt == NULL)
172         return NULL;
173     if ((cs = OPENSSL_malloc(BN_num_bytes(s))) == NULL)
174         goto err;
175
176     EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL);
177     EVP_DigestUpdate(ctxt, user, strlen(user));
178     EVP_DigestUpdate(ctxt, ":", 1);
179     EVP_DigestUpdate(ctxt, pass, strlen(pass));
180     EVP_DigestFinal_ex(ctxt, dig, NULL);
181
182     EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL);
183     BN_bn2bin(s, cs);
184     EVP_DigestUpdate(ctxt, cs, BN_num_bytes(s));
185     OPENSSL_free(cs);
186     EVP_DigestUpdate(ctxt, dig, sizeof(dig));
187     EVP_DigestFinal_ex(ctxt, dig, NULL);
188
189     res = BN_bin2bn(dig, sizeof(dig), NULL);
190  err:
191     EVP_MD_CTX_free(ctxt);
192     return res;
193 }
194
195 BIGNUM *SRP_Calc_A(const BIGNUM *a, const BIGNUM *N, const BIGNUM *g)
196 {
197     BN_CTX *bn_ctx;
198     BIGNUM *A = NULL;
199
200     if (a == NULL || N == NULL || g == NULL || (bn_ctx = BN_CTX_new()) == NULL)
201         return NULL;
202
203     if ((A = BN_new()) != NULL && !BN_mod_exp(A, g, a, N, bn_ctx)) {
204         BN_free(A);
205         A = NULL;
206     }
207     BN_CTX_free(bn_ctx);
208     return A;
209 }
210
211 BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,
212                             const BIGNUM *x, const BIGNUM *a, const BIGNUM *u)
213 {
214     BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;
215     BN_CTX *bn_ctx;
216
217     if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL
218         || a == NULL || (bn_ctx = BN_CTX_new()) == NULL)
219         return NULL;
220
221     if ((tmp = BN_new()) == NULL ||
222         (tmp2 = BN_new()) == NULL ||
223         (tmp3 = BN_new()) == NULL)
224         goto err;
225
226     if (!BN_mod_exp(tmp, g, x, N, bn_ctx))
227         goto err;
228     if ((k = srp_Calc_k(N, g)) == NULL)
229         goto err;
230     if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))
231         goto err;
232     if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))
233         goto err;
234     if (!BN_mul(tmp3, u, x, bn_ctx))
235         goto err;
236     if (!BN_add(tmp2, a, tmp3))
237         goto err;
238     K = BN_new();
239     if (K != NULL && !BN_mod_exp(K, tmp, tmp2, N, bn_ctx)) {
240         BN_free(K);
241         K = NULL;
242     }
243
244  err:
245     BN_CTX_free(bn_ctx);
246     BN_clear_free(tmp);
247     BN_clear_free(tmp2);
248     BN_clear_free(tmp3);
249     BN_free(k);
250     return K;
251 }
252
253 int SRP_Verify_B_mod_N(const BIGNUM *B, const BIGNUM *N)
254 {
255     BIGNUM *r;
256     BN_CTX *bn_ctx;
257     int ret = 0;
258
259     if (B == NULL || N == NULL || (bn_ctx = BN_CTX_new()) == NULL)
260         return 0;
261
262     if ((r = BN_new()) == NULL)
263         goto err;
264     /* Checks if B % N == 0 */
265     if (!BN_nnmod(r, B, N, bn_ctx))
266         goto err;
267     ret = !BN_is_zero(r);
268  err:
269     BN_CTX_free(bn_ctx);
270     BN_free(r);
271     return ret;
272 }
273
274 int SRP_Verify_A_mod_N(const BIGNUM *A, const BIGNUM *N)
275 {
276     /* Checks if A % N == 0 */
277     return SRP_Verify_B_mod_N(A, N);
278 }
279
280 static SRP_gN knowngN[] = {
281     {"8192", &bn_generator_19, &bn_group_8192},
282     {"6144", &bn_generator_5, &bn_group_6144},
283     {"4096", &bn_generator_5, &bn_group_4096},
284     {"3072", &bn_generator_5, &bn_group_3072},
285     {"2048", &bn_generator_2, &bn_group_2048},
286     {"1536", &bn_generator_2, &bn_group_1536},
287     {"1024", &bn_generator_2, &bn_group_1024},
288 };
289
290 # define KNOWN_GN_NUMBER sizeof(knowngN) / sizeof(SRP_gN)
291
292 /*
293  * Check if G and N are known parameters. The values have been generated
294  * from the ietf-tls-srp draft version 8
295  */
296 char *SRP_check_known_gN_param(const BIGNUM *g, const BIGNUM *N)
297 {
298     size_t i;
299     if ((g == NULL) || (N == NULL))
300         return 0;
301
302     for (i = 0; i < KNOWN_GN_NUMBER; i++) {
303         if (BN_cmp(knowngN[i].g, g) == 0 && BN_cmp(knowngN[i].N, N) == 0)
304             return knowngN[i].id;
305     }
306     return NULL;
307 }
308
309 SRP_gN *SRP_get_default_gN(const char *id)
310 {
311     size_t i;
312
313     if (id == NULL)
314         return knowngN;
315     for (i = 0; i < KNOWN_GN_NUMBER; i++) {
316         if (strcmp(knowngN[i].id, id) == 0)
317             return knowngN + i;
318     }
319     return NULL;
320 }
321 #endif