Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/srp/srp_lib.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2004-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 * Copyright (c) 2004, EdelKey Project. All Rights Reserved.
4
 *
5
 * Licensed under the OpenSSL license (the "License").  You may not use
6
 * this file except in compliance with the License.  You can obtain a copy
7
 * in the file LICENSE in the source distribution or at
8
 * https://www.openssl.org/source/license.html
9
 *
10
 * Originally written by Christophe Renou and Peter Sylvester,
11
 * for the EdelKey project.
12
 */
13
14
#ifndef OPENSSL_NO_SRP
15
# include "internal/cryptlib.h"
16
# include <openssl/sha.h>
17
# include <openssl/srp.h>
18
# include <openssl/evp.h>
19
# include "internal/bn_srp.h"
20
21
/* calculate = SHA1(PAD(x) || PAD(y)) */
22
23
static BIGNUM *srp_Calc_xy(const BIGNUM *x, const BIGNUM *y, const BIGNUM *N)
24
0
{
25
0
    unsigned char digest[SHA_DIGEST_LENGTH];
26
0
    unsigned char *tmp = NULL;
27
0
    int numN = BN_num_bytes(N);
28
0
    BIGNUM *res = NULL;
29
0
    if (x != N && BN_ucmp(x, N) >= 0)
30
0
        return NULL;
31
0
    if (y != N && BN_ucmp(y, N) >= 0)
32
0
        return NULL;
33
0
    if ((tmp = OPENSSL_malloc(numN * 2)) == NULL)
34
0
        goto err;
35
0
    if (BN_bn2binpad(x, tmp, numN) < 0
36
0
        || BN_bn2binpad(y, tmp + numN, numN) < 0
37
0
        || !EVP_Digest(tmp, numN * 2, digest, NULL, EVP_sha1(), NULL))
38
0
        goto err;
39
0
    res = BN_bin2bn(digest, sizeof(digest), NULL);
40
0
 err:
41
0
    OPENSSL_free(tmp);
42
0
    return res;
43
0
}
44
45
static BIGNUM *srp_Calc_k(const BIGNUM *N, const BIGNUM *g)
46
0
{
47
0
    /* k = SHA1(N | PAD(g)) -- tls-srp draft 8 */
48
0
    return srp_Calc_xy(N, g, N);
49
0
}
50
51
BIGNUM *SRP_Calc_u(const BIGNUM *A, const BIGNUM *B, const BIGNUM *N)
52
0
{
53
0
    /* k = SHA1(PAD(A) || PAD(B) ) -- tls-srp draft 8 */
54
0
    return srp_Calc_xy(A, B, N);
55
0
}
56
57
BIGNUM *SRP_Calc_server_key(const BIGNUM *A, const BIGNUM *v, const BIGNUM *u,
58
                            const BIGNUM *b, const BIGNUM *N)
59
0
{
60
0
    BIGNUM *tmp = NULL, *S = NULL;
61
0
    BN_CTX *bn_ctx;
62
0
63
0
    if (u == NULL || A == NULL || v == NULL || b == NULL || N == NULL)
64
0
        return NULL;
65
0
66
0
    if ((bn_ctx = BN_CTX_new()) == NULL || (tmp = BN_new()) == NULL)
67
0
        goto err;
68
0
69
0
    /* S = (A*v**u) ** b */
70
0
71
0
    if (!BN_mod_exp(tmp, v, u, N, bn_ctx))
72
0
        goto err;
73
0
    if (!BN_mod_mul(tmp, A, tmp, N, bn_ctx))
74
0
        goto err;
75
0
76
0
    S = BN_new();
77
0
    if (S != NULL && !BN_mod_exp(S, tmp, b, N, bn_ctx)) {
78
0
        BN_free(S);
79
0
        S = NULL;
80
0
    }
81
0
 err:
82
0
    BN_CTX_free(bn_ctx);
83
0
    BN_clear_free(tmp);
84
0
    return S;
85
0
}
86
87
BIGNUM *SRP_Calc_B(const BIGNUM *b, const BIGNUM *N, const BIGNUM *g,
88
                   const BIGNUM *v)
89
0
{
90
0
    BIGNUM *kv = NULL, *gb = NULL;
91
0
    BIGNUM *B = NULL, *k = NULL;
92
0
    BN_CTX *bn_ctx;
93
0
94
0
    if (b == NULL || N == NULL || g == NULL || v == NULL ||
95
0
        (bn_ctx = BN_CTX_new()) == NULL)
96
0
        return NULL;
97
0
98
0
    if ((kv = BN_new()) == NULL ||
99
0
        (gb = BN_new()) == NULL || (B = BN_new()) == NULL)
100
0
        goto err;
101
0
102
0
    /* B = g**b + k*v */
103
0
104
0
    if (!BN_mod_exp(gb, g, b, N, bn_ctx)
105
0
        || (k = srp_Calc_k(N, g)) == NULL
106
0
        || !BN_mod_mul(kv, v, k, N, bn_ctx)
107
0
        || !BN_mod_add(B, gb, kv, N, bn_ctx)) {
108
0
        BN_free(B);
109
0
        B = NULL;
110
0
    }
111
0
 err:
112
0
    BN_CTX_free(bn_ctx);
113
0
    BN_clear_free(kv);
114
0
    BN_clear_free(gb);
115
0
    BN_free(k);
116
0
    return B;
117
0
}
118
119
BIGNUM *SRP_Calc_x(const BIGNUM *s, const char *user, const char *pass)
120
0
{
121
0
    unsigned char dig[SHA_DIGEST_LENGTH];
122
0
    EVP_MD_CTX *ctxt;
123
0
    unsigned char *cs = NULL;
124
0
    BIGNUM *res = NULL;
125
0
126
0
    if ((s == NULL) || (user == NULL) || (pass == NULL))
127
0
        return NULL;
128
0
129
0
    ctxt = EVP_MD_CTX_new();
130
0
    if (ctxt == NULL)
131
0
        return NULL;
132
0
    if ((cs = OPENSSL_malloc(BN_num_bytes(s))) == NULL)
133
0
        goto err;
134
0
135
0
    if (!EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL)
136
0
        || !EVP_DigestUpdate(ctxt, user, strlen(user))
137
0
        || !EVP_DigestUpdate(ctxt, ":", 1)
138
0
        || !EVP_DigestUpdate(ctxt, pass, strlen(pass))
139
0
        || !EVP_DigestFinal_ex(ctxt, dig, NULL)
140
0
        || !EVP_DigestInit_ex(ctxt, EVP_sha1(), NULL))
141
0
        goto err;
142
0
    BN_bn2bin(s, cs);
143
0
    if (!EVP_DigestUpdate(ctxt, cs, BN_num_bytes(s)))
144
0
        goto err;
145
0
146
0
    if (!EVP_DigestUpdate(ctxt, dig, sizeof(dig))
147
0
        || !EVP_DigestFinal_ex(ctxt, dig, NULL))
148
0
        goto err;
149
0
150
0
    res = BN_bin2bn(dig, sizeof(dig), NULL);
151
0
152
0
 err:
153
0
    OPENSSL_free(cs);
154
0
    EVP_MD_CTX_free(ctxt);
155
0
    return res;
156
0
}
157
158
BIGNUM *SRP_Calc_A(const BIGNUM *a, const BIGNUM *N, const BIGNUM *g)
159
0
{
160
0
    BN_CTX *bn_ctx;
161
0
    BIGNUM *A = NULL;
162
0
163
0
    if (a == NULL || N == NULL || g == NULL || (bn_ctx = BN_CTX_new()) == NULL)
164
0
        return NULL;
165
0
166
0
    if ((A = BN_new()) != NULL && !BN_mod_exp(A, g, a, N, bn_ctx)) {
167
0
        BN_free(A);
168
0
        A = NULL;
169
0
    }
170
0
    BN_CTX_free(bn_ctx);
171
0
    return A;
172
0
}
173
174
BIGNUM *SRP_Calc_client_key(const BIGNUM *N, const BIGNUM *B, const BIGNUM *g,
175
                            const BIGNUM *x, const BIGNUM *a, const BIGNUM *u)
176
0
{
177
0
    BIGNUM *tmp = NULL, *tmp2 = NULL, *tmp3 = NULL, *k = NULL, *K = NULL;
178
0
    BN_CTX *bn_ctx;
179
0
180
0
    if (u == NULL || B == NULL || N == NULL || g == NULL || x == NULL
181
0
        || a == NULL || (bn_ctx = BN_CTX_new()) == NULL)
182
0
        return NULL;
183
0
184
0
    if ((tmp = BN_new()) == NULL ||
185
0
        (tmp2 = BN_new()) == NULL ||
186
0
        (tmp3 = BN_new()) == NULL)
187
0
        goto err;
188
0
189
0
    if (!BN_mod_exp(tmp, g, x, N, bn_ctx))
190
0
        goto err;
191
0
    if ((k = srp_Calc_k(N, g)) == NULL)
192
0
        goto err;
193
0
    if (!BN_mod_mul(tmp2, tmp, k, N, bn_ctx))
194
0
        goto err;
195
0
    if (!BN_mod_sub(tmp, B, tmp2, N, bn_ctx))
196
0
        goto err;
197
0
    if (!BN_mul(tmp3, u, x, bn_ctx))
198
0
        goto err;
199
0
    if (!BN_add(tmp2, a, tmp3))
200
0
        goto err;
201
0
    K = BN_new();
202
0
    if (K != NULL && !BN_mod_exp(K, tmp, tmp2, N, bn_ctx)) {
203
0
        BN_free(K);
204
0
        K = NULL;
205
0
    }
206
0
207
0
 err:
208
0
    BN_CTX_free(bn_ctx);
209
0
    BN_clear_free(tmp);
210
0
    BN_clear_free(tmp2);
211
0
    BN_clear_free(tmp3);
212
0
    BN_free(k);
213
0
    return K;
214
0
}
215
216
int SRP_Verify_B_mod_N(const BIGNUM *B, const BIGNUM *N)
217
0
{
218
0
    BIGNUM *r;
219
0
    BN_CTX *bn_ctx;
220
0
    int ret = 0;
221
0
222
0
    if (B == NULL || N == NULL || (bn_ctx = BN_CTX_new()) == NULL)
223
0
        return 0;
224
0
225
0
    if ((r = BN_new()) == NULL)
226
0
        goto err;
227
0
    /* Checks if B % N == 0 */
228
0
    if (!BN_nnmod(r, B, N, bn_ctx))
229
0
        goto err;
230
0
    ret = !BN_is_zero(r);
231
0
 err:
232
0
    BN_CTX_free(bn_ctx);
233
0
    BN_free(r);
234
0
    return ret;
235
0
}
236
237
int SRP_Verify_A_mod_N(const BIGNUM *A, const BIGNUM *N)
238
0
{
239
0
    /* Checks if A % N == 0 */
240
0
    return SRP_Verify_B_mod_N(A, N);
241
0
}
242
243
static SRP_gN knowngN[] = {
244
    {"8192", &bn_generator_19, &bn_group_8192},
245
    {"6144", &bn_generator_5, &bn_group_6144},
246
    {"4096", &bn_generator_5, &bn_group_4096},
247
    {"3072", &bn_generator_5, &bn_group_3072},
248
    {"2048", &bn_generator_2, &bn_group_2048},
249
    {"1536", &bn_generator_2, &bn_group_1536},
250
    {"1024", &bn_generator_2, &bn_group_1024},
251
};
252
253
0
# define KNOWN_GN_NUMBER sizeof(knowngN) / sizeof(SRP_gN)
254
255
/*
256
 * Check if G and N are known parameters. The values have been generated
257
 * from the ietf-tls-srp draft version 8
258
 */
259
char *SRP_check_known_gN_param(const BIGNUM *g, const BIGNUM *N)
260
0
{
261
0
    size_t i;
262
0
    if ((g == NULL) || (N == NULL))
263
0
        return 0;
264
0
265
0
    for (i = 0; i < KNOWN_GN_NUMBER; i++) {
266
0
        if (BN_cmp(knowngN[i].g, g) == 0 && BN_cmp(knowngN[i].N, N) == 0)
267
0
            return knowngN[i].id;
268
0
    }
269
0
    return NULL;
270
0
}
271
272
SRP_gN *SRP_get_default_gN(const char *id)
273
0
{
274
0
    size_t i;
275
0
276
0
    if (id == NULL)
277
0
        return knowngN;
278
0
    for (i = 0; i < KNOWN_GN_NUMBER; i++) {
279
0
        if (strcmp(knowngN[i].id, id) == 0)
280
0
            return knowngN + i;
281
0
    }
282
0
    return NULL;
283
0
}
284
#endif