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