Coverage Report

Created: 2022-11-30 06:20

/src/openssl/crypto/srp/srp_lib.c
Line
Count
Source (jump to first uncovered line)
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 "cryptlib.h"
62
# include "srp_lcl.h"
63
# include <openssl/srp.h>
64
# include <openssl/evp.h>
65
66
# if (BN_BYTES == 8)
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)
71
#  else
72
#   define bn_pack4(a1,a2,a3,a4) ((a1##ULL<<48)|(a2##ULL<<32)|(a3##ULL<<16)|a4##ULL)
73
#  endif
74
# elif (BN_BYTES == 4)
75
#  define bn_pack4(a1,a2,a3,a4)  ((a3##UL<<16)|a4##UL), ((a1##UL<<16)|a2##UL)
76
# else
77
#  error "unsupported BN_BYTES"
78
# endif
79
80
# include "srp_grps.h"
81
82
static BIGNUM *srp_Calc_k(BIGNUM *N, BIGNUM *g)
83
0
{
84
    /* k = SHA1(N | PAD(g)) -- tls-srp draft 8 */
85
86
0
    unsigned char digest[SHA_DIGEST_LENGTH];
87
0
    unsigned char *tmp;
88
0
    EVP_MD_CTX ctxt;
89
0
    int longg;
90
0
    int longN = BN_num_bytes(N);
91
92
0
    if (BN_ucmp(g, N) >= 0)
93
0
        return NULL;
94
95
0
    if ((tmp = OPENSSL_malloc(longN)) == NULL)
96
0
        return NULL;
97
0
    BN_bn2bin(N, tmp);
98
99
0
    EVP_MD_CTX_init(&ctxt);
100
0
    EVP_DigestInit_ex(&ctxt, EVP_sha1(), NULL);
101
0
    EVP_DigestUpdate(&ctxt, tmp, longN);
102
103
0
    memset(tmp, 0, longN);
104
0
    longg = BN_bn2bin(g, tmp);
105
    /* use the zeros behind to pad on left */
106
0
    EVP_DigestUpdate(&ctxt, tmp + longg, longN - longg);
107
0
    EVP_DigestUpdate(&ctxt, tmp, longg);
108
0
    OPENSSL_free(tmp);
109
110
0
    EVP_DigestFinal_ex(&ctxt, digest, NULL);
111
0
    EVP_MD_CTX_cleanup(&ctxt);
112
0
    return BN_bin2bn(digest, sizeof(digest), NULL);
113
0
}
114
115
BIGNUM *SRP_Calc_u(BIGNUM *A, BIGNUM *B, BIGNUM *N)
116
0
{
117
    /* k = SHA1(PAD(A) || PAD(B) ) -- tls-srp draft 8 */
118
119
0
    BIGNUM *u;
120
0
    unsigned char cu[SHA_DIGEST_LENGTH];
121
0
    unsigned char *cAB;
122
0
    EVP_MD_CTX ctxt;
123
0
    int longN;
124
0
    if ((A == NULL) || (B == NULL) || (N == NULL))
125
0
        return NULL;
126
127
0
    if (BN_ucmp(A, N) >= 0 || BN_ucmp(B, N) >= 0)
128
0
        return NULL;
129
130
0
    longN = BN_num_bytes(N);
131
132
0
    if ((cAB = OPENSSL_malloc(2 * longN)) == NULL)
133
0
        return NULL;
134
135
0
    memset(cAB, 0, longN);
136
137
0
    EVP_MD_CTX_init(&ctxt);
138
0
    EVP_DigestInit_ex(&ctxt, EVP_sha1(), NULL);
139
0
    EVP_DigestUpdate(&ctxt, cAB + BN_bn2bin(A, cAB + longN), longN);
140
0
    EVP_DigestUpdate(&ctxt, cAB + BN_bn2bin(B, cAB + longN), longN);
141
0
    OPENSSL_free(cAB);
142
0
    EVP_DigestFinal_ex(&ctxt, cu, NULL);
143
0
    EVP_MD_CTX_cleanup(&ctxt);
144
145
0
    if (!(u = BN_bin2bn(cu, sizeof(cu), NULL)))
146
0
        return NULL;
147
0
    if (!BN_is_zero(u))
148
0
        return u;
149
0
    BN_free(u);
150
0
    return NULL;
151
0
}
152
153
BIGNUM *SRP_Calc_server_key(BIGNUM *A, BIGNUM *v, BIGNUM *u, BIGNUM *b,
154
                            BIGNUM *N)
155
0
{
156
0
    BIGNUM *tmp = NULL, *S = NULL;
157
0
    BN_CTX *bn_ctx;
158
159
0
    if (u == NULL || A == NULL || v == NULL || b == NULL || N == NULL)
160
0
        return NULL;
161
162
0
    if ((bn_ctx = BN_CTX_new()) == NULL || (tmp = BN_new()) == NULL)
163
0
        goto err;
164
165
    /* S = (A*v**u) ** b */
166
167
0
    if (!BN_mod_exp(tmp, v, u, N, bn_ctx))
168
0
        goto err;
169
0
    if (!BN_mod_mul(tmp, A, tmp, N, bn_ctx))
170
0
        goto err;
171
172
0
    S = BN_new();
173
0
    if (S != NULL && !BN_mod_exp(S, tmp, b, N, bn_ctx)) {
174
0
        BN_free(S);
175
0
        S = NULL;
176
0
    }
177
0
 err:
178
0
    BN_CTX_free(bn_ctx);
179
0
    BN_clear_free(tmp);
180
0
    return S;
181
0
}
182
183
BIGNUM *SRP_Calc_B(BIGNUM *b, BIGNUM *N, BIGNUM *g, BIGNUM *v)
184
0
{
185
0
    BIGNUM *kv = NULL, *gb = NULL;
186
0
    BIGNUM *B = NULL, *k = NULL;
187
0
    BN_CTX *bn_ctx;
188
189
0
    if (b == NULL || N == NULL || g == NULL || v == NULL ||
190
0
        (bn_ctx = BN_CTX_new()) == NULL)
191
0
        return NULL;
192
193
0
    if ((kv = BN_new()) == NULL ||
194
0
        (gb = BN_new()) == NULL || (B = BN_new()) == NULL)
195
0
        goto err;
196
197
    /* B = g**b + k*v */
198
199
0
    if (!BN_mod_exp(gb, g, b, N, bn_ctx) ||
200
0
        !(k = srp_Calc_k(N, g)) ||
201
0
        !BN_mod_mul(kv, v, k, N, bn_ctx) ||
202
0
        !BN_mod_add(B, gb, kv, N, bn_ctx)) {
203
0
        BN_free(B);
204
0
        B = NULL;
205
0
    }
206
0
 err:
207
0
    BN_CTX_free(bn_ctx);
208
0
    BN_clear_free(kv);
209
0
    BN_clear_free(gb);
210
0
    BN_free(k);
211
0
    return B;
212
0
}
213
214
BIGNUM *SRP_Calc_x(BIGNUM *s, const char *user, const char *pass)
215
0
{
216
0
    unsigned char dig[SHA_DIGEST_LENGTH];
217
0
    EVP_MD_CTX ctxt;
218
0
    unsigned char *cs;
219
220
0
    if ((s == NULL) || (user == NULL) || (pass == NULL))
221
0
        return NULL;
222
223
0
    if ((cs = OPENSSL_malloc(BN_num_bytes(s))) == NULL)
224
0
        return NULL;
225
226
0
    EVP_MD_CTX_init(&ctxt);
227
0
    EVP_DigestInit_ex(&ctxt, EVP_sha1(), NULL);
228
0
    EVP_DigestUpdate(&ctxt, user, strlen(user));
229
0
    EVP_DigestUpdate(&ctxt, ":", 1);
230
0
    EVP_DigestUpdate(&ctxt, pass, strlen(pass));
231
0
    EVP_DigestFinal_ex(&ctxt, dig, NULL);
232
233
0
    EVP_DigestInit_ex(&ctxt, EVP_sha1(), NULL);
234
0
    BN_bn2bin(s, cs);
235
0
    EVP_DigestUpdate(&ctxt, cs, BN_num_bytes(s));
236
0
    OPENSSL_free(cs);
237
0
    EVP_DigestUpdate(&ctxt, dig, sizeof(dig));
238
0
    EVP_DigestFinal_ex(&ctxt, dig, NULL);
239
0
    EVP_MD_CTX_cleanup(&ctxt);
240
241
0
    return BN_bin2bn(dig, sizeof(dig), NULL);
242
0
}
243
244
BIGNUM *SRP_Calc_A(BIGNUM *a, BIGNUM *N, BIGNUM *g)
245
0
{
246
0
    BN_CTX *bn_ctx;
247
0
    BIGNUM *A = NULL;
248
249
0
    if (a == NULL || N == NULL || g == NULL ||
250
0
        (bn_ctx = BN_CTX_new()) == NULL)
251
0
        return NULL;
252
253
0
    if ((A = BN_new()) != NULL && !BN_mod_exp(A, g, a, N, bn_ctx)) {
254
0
        BN_free(A);
255
0
        A = NULL;
256
0
    }
257
0
    BN_CTX_free(bn_ctx);
258
0
    return A;
259
0
}
260
261
BIGNUM *SRP_Calc_client_key(BIGNUM *N, BIGNUM *B, BIGNUM *g, BIGNUM *x,
262
                            BIGNUM *a, BIGNUM *u)
263
0
{
264
0
    BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;
265
0
    BN_CTX *bn_ctx;
266
267
0
    if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL
268
0
        || a == NULL || (bn_ctx = BN_CTX_new()) == NULL)
269
0
        return NULL;
270
271
0
    if ((tmp = BN_new()) == NULL ||
272
0
        (tmp2 = BN_new()) == NULL ||
273
0
        (tmp3 = BN_new()) == NULL)
274
0
        goto err;
275
276
0
    if (!BN_mod_exp(tmp, g, x, N, bn_ctx))
277
0
        goto err;
278
0
    if (!(k = srp_Calc_k(N, g)))
279
0
        goto err;
280
0
    if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))
281
0
        goto err;
282
0
    if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))
283
0
        goto err;
284
285
0
    if (!BN_mul(tmp3, u, x, bn_ctx))
286
0
        goto err;
287
0
    if (!BN_add(tmp2, a, tmp3))
288
0
        goto err;
289
0
    K = BN_new();
290
0
    if (K != NULL && !BN_mod_exp(K, tmp, tmp2, N, bn_ctx)) {
291
0
        BN_free(K);
292
0
        K = NULL;
293
0
    }
294
295
0
 err:
296
0
    BN_CTX_free(bn_ctx);
297
0
    BN_clear_free(tmp);
298
0
    BN_clear_free(tmp2);
299
0
    BN_clear_free(tmp3);
300
0
    BN_free(k);
301
0
    return K;
302
0
}
303
304
int SRP_Verify_B_mod_N(BIGNUM *B, BIGNUM *N)
305
0
{
306
0
    BIGNUM *r;
307
0
    BN_CTX *bn_ctx;
308
0
    int ret = 0;
309
310
0
    if (B == NULL || N == NULL || (bn_ctx = BN_CTX_new()) == NULL)
311
0
        return 0;
312
313
0
    if ((r = BN_new()) == NULL)
314
0
        goto err;
315
    /* Checks if B % N == 0 */
316
0
    if (!BN_nnmod(r, B, N, bn_ctx))
317
0
        goto err;
318
0
    ret = !BN_is_zero(r);
319
0
 err:
320
0
    BN_CTX_free(bn_ctx);
321
0
    BN_free(r);
322
0
    return ret;
323
0
}
324
325
int SRP_Verify_A_mod_N(BIGNUM *A, BIGNUM *N)
326
0
{
327
    /* Checks if A % N == 0 */
328
0
    return SRP_Verify_B_mod_N(A, N);
329
0
}
330
331
/*
332
 * Check if G and N are kwown parameters. The values have been generated
333
 * from the ietf-tls-srp draft version 8
334
 */
335
char *SRP_check_known_gN_param(BIGNUM *g, BIGNUM *N)
336
0
{
337
0
    size_t i;
338
0
    if ((g == NULL) || (N == NULL))
339
0
        return 0;
340
341
0
    srp_bn_print(g);
342
0
    srp_bn_print(N);
343
344
0
    for (i = 0; i < KNOWN_GN_NUMBER; i++) {
345
0
        if (BN_cmp(knowngN[i].g, g) == 0 && BN_cmp(knowngN[i].N, N) == 0)
346
0
            return knowngN[i].id;
347
0
    }
348
0
    return NULL;
349
0
}
350
351
SRP_gN *SRP_get_default_gN(const char *id)
352
0
{
353
0
    size_t i;
354
355
0
    if (id == NULL)
356
0
        return knowngN;
357
0
    for (i = 0; i < KNOWN_GN_NUMBER; i++) {
358
0
        if (strcmp(knowngN[i].id, id) == 0)
359
0
            return knowngN + i;
360
0
    }
361
0
    return NULL;
362
0
}
363
#endif