Debian Codegraph @

src/crypto/srp.c

raw symbols SCIP
1 #include "srp.h"
2
3 #include "config.h"
4 #include "random.h"
5 #include "crypto/srp_constants.h"
6 #include "vcs_version.h"
7
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <stdbool.h>
13
14 static int constant_time_memcmp(const uint8_t *a, const uint8_t *b, size_t n) {
15 uint8_t diff = 0;
16 for (size_t i = 0; i < n; i++)
17 diff |= a[i] ^ b[i];
18 return diff != 0;
19 }
20
21 #ifndef WINAPI
22 #define WINAPI
23 #endif
24
25 #define DEBUG_EXTRACT_SRP_EXCHANGE 0
26 #ifndef DEBUG_USE_EXAMPLE_CONSTANTS
27 #define DEBUG_USE_EXAMPLE_CONSTANTS 0
28 #endif
29
30 #if DEBUG_EXTRACT_SRP_EXCHANGE
31 static void print_hash(const uint8_t *buf, char *specifier) {
32 char outbuf[SHA256_DIGEST_LENGTH*2 +1];
33 size_t j;
34 for (j = 0; j < SHA256_DIGEST_LENGTH; j++) {
35 outbuf[2 * j] = (buf[j] >> 4) + 48;
36 outbuf[2 * j + 1] = (buf[j] & 15) + 48;
37 if (outbuf[2 * j] > 57)
38 outbuf[2 * j] += 7;
39 if (outbuf[2 * j + 1] > 57)
40 outbuf[2 * j + 1] += 7;
41 }
42 outbuf[2 * j] = '\0';
43
44 fprintf(stderr, "%s%s\n", specifier, outbuf);
45 }
46 #endif
47
48 #if HAVE_MBEDTLS
49 #include <mbedtls/bignum.h>
50 #include <mbedtls/sha256.h>
51 #include <mbedtls/version.h>
52
53 #if MBEDTLS_VERSION_NUMBER < 0x02070000
54 #error "librist requires mbedTLS >= 2.7 (the *_ret SHA-256 API). Upgrade your mbedTLS or build with the bundled copy in contrib/mbedtls."
55 #endif
56
57 #if MBEDTLS_VERSION_NUMBER >= 0x03000000
58 /* mbedTLS 3.x dropped the _ret suffix; provide our own compat macros
59 * instead of pulling in the deprecated compat-2.x.h header (which
60 * triggers -Werror on distro builds with MBEDTLS_DEPRECATED_WARNING
61 * and is removed entirely in mbedTLS 4.x). */
62 #define mbedtls_sha256_starts_ret mbedtls_sha256_starts
63 #define mbedtls_sha256_update_ret mbedtls_sha256_update
64 #define mbedtls_sha256_finish_ret mbedtls_sha256_finish
65 #define mbedtls_sha256_ret mbedtls_sha256
66 #endif
67
68 int _librist_srp_mbedtls_wrap_random(void *unused, unsigned char * buf, size_t size) {
69 RIST_MARK_UNUSED(unused);
70 return _librist_crypto_ramdom_get_bytes(buf, size);
71 }
72
73 #define BIGNUM mbedtls_mpi
74 #define BIGNUM_INIT(num) mbedtls_mpi_init(num)
75 #define BIGNUM_FREE(num) mbedtls_mpi_free(num)
76 #define BIGNUM_GET_BINARY_SIZE(num) mbedtls_mpi_size(num)
77 #define BIGNUM_FROM_ARRAY(num, array, size) ret = mbedtls_mpi_read_binary(num, array, size)
78 #define BIGNUM_FROM_STRING(num, str) ret = mbedtls_mpi_read_string(num, 16, str)
79 #if MBEDTLS_HAS_MPI_RANDOM
80 #define BIGNUM_RANDOM(num, max) ret = mbedtls_mpi_random(num, 0, max, _librist_srp_mbedtls_wrap_random, NULL);
81 #else
82 #define BIGNUM_RANDOM(num, max) do { \
83 size_t _rlen = mbedtls_mpi_size(max); \
84 do { \
85 ret = mbedtls_mpi_fill_random(num, _rlen, _librist_srp_mbedtls_wrap_random, NULL); \
86 if (ret != 0) break; \
87 } while (mbedtls_mpi_cmp_mpi(num, max) >= 0 || mbedtls_mpi_bitlen(num) == 0); \
88 } while (0)
89 #endif
90 #define BIGNUM_MOD_RED(out, a, b) ret = mbedtls_mpi_mod_mpi(out, a, b)
91 #define BIGNUM_EXP_MOD(out, base, exp, mod) ret = mbedtls_mpi_exp_mod(out, base, exp, mod, NULL)
92 #define BIGNUM_MULT_BIG(prod, a, b) ret = mbedtls_mpi_mul_mpi(prod, a, b)
93 #define BIGNUM_ADD_BIG(prod, a, b) ret = mbedtls_mpi_add_mpi(prod, a, b)
94 #define BIGNUM_SUB_BIG(prod, a, b) ret = mbedtls_mpi_sub_mpi(prod, a, b)
95 #define BIGNUM_EQUALS(num, comp) (mbedtls_mpi_cmp_int(num, comp) == 0)
96 #define BIGNUM_WRITE_BYTES(num, bytes, bytes_size) if (mbedtls_mpi_write_binary(num, bytes, bytes_size) != 0) {return -1; }
97 /* Variant that jumps to a cleanup label instead of returning, for use inside
98 * functions that own heap that needs freeing on failure. */
99 #define BIGNUM_WRITE_BYTES_OR_GOTO(num, bytes, bytes_size, lbl) do { \
100 if (mbedtls_mpi_write_binary(num, bytes, bytes_size) != 0) goto lbl; \
101 } while (0)
102 #define BIGNUM_WRITE_BYTES_ALLOC(num, bytes_pp, len_p, lbl) do {\
103 *len_p = mbedtls_mpi_size(num); \
104 *bytes_pp = malloc(*len_p); \
105 if (!*bytes_pp) { goto lbl; } \
106 if (mbedtls_mpi_write_binary(num, *bytes_pp, *len_p) != 0) {goto lbl; } \
107 } while(false)
108 #define BIGNUM_PRINT(prefix, num) mbedtls_mpi_write_file(prefix, num, 16, stderr)
109 #define HASH_CONTEXT mbedtls_sha256_context
110
111 void librist_crypto_srp_mbedtls_hash_init(HASH_CONTEXT *ctx, bool correct_init) {
112 mbedtls_sha256_init(ctx);
113 if (correct_init) {
114 mbedtls_sha256_starts_ret(ctx, 0);
115 }
116 }
117
118 #define HASH_CONTEXT_INIT(ctx, correct) librist_crypto_srp_mbedtls_hash_init(ctx, correct)
119 #define HASH_CONTEXT_FREE(ctx) mbedtls_sha256_free(ctx)
120
121 #elif HAVE_NETTLE
122 #include <nettle/sha2.h>
123 #include <nettle/bignum.h>
124 #include <nettle/version.h>
125 #define BIGNUM MP_INT
126 #define BIGNUM_INIT(num) mpz_init(num)
127 #define BIGNUM_FREE(num) mpz_clear(num)
128 #define BIGNUM_GET_BINARY_SIZE(num) ((mpz_sizeinbase(num, 2) +7) /8)
129 #define BIGNUM_FROM_ARRAY(num, array, size) mpz_import(num, size, 1, 1, 0, 0, array)
130 #define BIGNUM_FROM_STRING(num, str) ret = mpz_set_str(num, str, 16)
131 /* Pass &ret as the void* context so the wrapper can signal CSPRNG failure. */
132 #define BIGNUM_RANDOM(num, max) do { \
133 ret = 0; \
134 nettle_mpz_random(num, &ret, _librist_srp_nettle_wrap_random, max); \
135 } while (0)
136 #define BIGNUM_MOD_RED(out, a, b) mpz_mod(out, a, b)
137 #define BIGNUM_EXP_MOD(out, base, exp, mod) mpz_powm(out, base, exp, mod)
138 #define BIGNUM_MULT_BIG(prod, a, b) mpz_mul(prod, a, b)
139 #define BIGNUM_ADD_BIG(prod, a, b) mpz_add(prod, a, b)
140 #define BIGNUM_SUB_BIG(prod, a, b) mpz_sub(prod, a, b)
141 #define BIGNUM_EQUALS(num, comp) (mpz_cmp_ui(num, comp) == 0)
142 #define BIGNUM_WRITE_BYTES(num, bytes, bytes_size) mpz_export(bytes, NULL, 1, 1, 0, 0, num)
143 /* Nettle's mpz_export into a caller-provided buffer cannot fail; mirror the
144 * mbedTLS-side macro so the source compiles unchanged. The lbl argument is a
145 * goto target (not an expression), so we cannot reference it here. */
146 #define BIGNUM_WRITE_BYTES_OR_GOTO(num, bytes, bytes_size, lbl) \
147 mpz_export(bytes, NULL, 1, 1, 0, 0, num)
148 #define BIGNUM_WRITE_BYTES_ALLOC(num, bytes_pp, len_p, lbl) do {\
149 *bytes_pp = mpz_export(NULL, len_p, 1, 1, 0, 0, num); \
150 if (!*bytes_pp) { goto lbl; } \
151 } while (false)
152 #define BIGNUM_PRINT(prefix, num) do { \
153 fprintf(stderr, prefix); \
154 mpz_out_str(stderr, 16, num); \
155 fprintf(stderr, "\n"); \
156 } while(false)
157 #define HASH_CONTEXT struct sha256_ctx
158 #define HASH_CONTEXT_INIT(ctx, correct) (void)(correct); nettle_sha256_init(ctx)
159 #define HASH_CONTEXT_FREE(ctx)
160
161 void _librist_srp_nettle_wrap_random(void *err_out, size_t size, uint8_t* buf) {
162 /* Nettle's callback returns void; we route CSPRNG failure through
163 * the caller's int* (passed via the otherwise-unused ctx arg) and
164 * zero the buffer so a missed return check feeds SRP a value that
165 * its mod-N checks will reject. */
166 int *err = (int *)err_out;
167 if (_librist_crypto_ramdom_get_bytes(buf, size) != 0) {
168 memset(buf, 0, size);
169 if (err)
170 *err = -1;
171 }
172 }
173
174 #endif
175
176 static int librist_crypto_srp_hash_update(HASH_CONTEXT *hash_ctx, const void *data, size_t len)
177 {
178 #if HAVE_MBEDTLS
179 return mbedtls_sha256_update_ret( hash_ctx, data, len );
180 #else
181 nettle_sha256_update( hash_ctx, len, data);
182 return 0;
183 #endif
184 }
185
186 static int librist_crypto_srp_hash_update_bignum(HASH_CONTEXT *hash_ctx, const BIGNUM *num) {
187 uint8_t bytes[1024];
188 size_t size = BIGNUM_GET_BINARY_SIZE(num);
189 if (size > sizeof(bytes))
190 return -1;
191
192 BIGNUM_WRITE_BYTES(num, bytes, size);
193
194 return librist_crypto_srp_hash_update(hash_ctx, bytes, size);
195 }
196
197 static int librist_crypto_srp_hash_final(HASH_CONTEXT *hash_ctx, uint8_t *data)
198 {
199 #if HAVE_MBEDTLS
200 return mbedtls_sha256_finish_ret( hash_ctx, data);
201 #else
202 /* nettle 4.0 dropped the length argument; this digest is never truncated. */
203 #if NETTLE_VERSION_MAJOR >= 4
204 nettle_sha256_digest( hash_ctx, data);
205 #else
206 nettle_sha256_digest( hash_ctx, SHA256_DIGEST_LENGTH, data);
207 #endif
208 return 0;
209 #endif
210 }
211
212 static int librist_crypto_srp_hash(const uint8_t *indata, size_t inlen, uint8_t outdata[SHA256_DIGEST_LENGTH])
213 {
214 #if HAVE_MBEDTLS
215 return mbedtls_sha256_ret(indata, inlen, outdata, 0);
216 #else
217 HASH_CONTEXT hash_ctx;
218 HASH_CONTEXT_INIT(&hash_ctx, true);
219 librist_crypto_srp_hash_update(&hash_ctx, indata, inlen);
220 librist_crypto_srp_hash_final(&hash_ctx, outdata);
221 return 0;
222 #endif
223 }
224
225 //Calculates the value of x as follows: x = SHA256(s, SHA256(I | “:” | P))
226 static int librist_crypto_srp_calc_x(BIGNUM *salt, const char * username, const char * password, size_t password_len, BIGNUM *x, bool correct)
227 {
228 HASH_CONTEXT hash_ctx;
229 HASH_CONTEXT_INIT(&hash_ctx, correct);
230 int ret = 0;
231
232 if (librist_crypto_srp_hash_update(&hash_ctx, username, strlen(username)) != 0)
233 return -1;
234 if (librist_crypto_srp_hash_update(&hash_ctx, ":", 1) != 0)
235 return -1;
236 if (librist_crypto_srp_hash_update(&hash_ctx, password, password_len) != 0)
237 return -1;
238
239 size_t salt_len = BIGNUM_GET_BINARY_SIZE(salt);
240 uint8_t *hash_data = malloc(salt_len + SHA256_DIGEST_LENGTH);
241 if (!hash_data)
242 return -1;
243
244 if (librist_crypto_srp_hash_final(&hash_ctx, &hash_data[salt_len]) != 0)
245 goto failed;
246
247 BIGNUM_WRITE_BYTES_OR_GOTO(salt, hash_data, salt_len, failed);
248
249 HASH_CONTEXT_FREE(&hash_ctx);
250
251 uint8_t x_hash[SHA256_DIGEST_LENGTH];
252
253 if (librist_crypto_srp_hash( hash_data, salt_len + SHA256_DIGEST_LENGTH, x_hash) != 0)
254 goto failed;
255
256 BIGNUM_FROM_ARRAY(x, x_hash, SHA256_DIGEST_LENGTH);
257 if (ret != 0)
258 goto failed;
259
260 free(hash_data);
261 return 0;
262
263 failed:
264 free(hash_data);
265 return -1;
266 }
267
268 /* RFC 5054 PAD variant: zero-extends both inputs to pad_len bytes before
269 * hashing. Required for u = SHA(PAD(A) | PAD(B)) and k = SHA(N | PAD(g))
270 * where PAD(x) is x left-zero-padded to the byte length of N. */
271 static int librist_crypto_srp_hash_2_bignum_padded(size_t pad_len, BIGNUM *A, BIGNUM *B, uint8_t hash_out[SHA256_DIGEST_LENGTH])
272 {
273 uint8_t AB[2048];
274
275 if (pad_len * 2 > sizeof(AB))
276 return -1;
277
278 size_t A_size = BIGNUM_GET_BINARY_SIZE(A);
279 size_t B_size = BIGNUM_GET_BINARY_SIZE(B);
280 if (A_size > pad_len || B_size > pad_len)
281 return -1;
282
283 memset(AB, 0, pad_len * 2);
284 BIGNUM_WRITE_BYTES(A, AB + (pad_len - A_size), A_size);
285 BIGNUM_WRITE_BYTES(B, AB + pad_len + (pad_len - B_size), B_size);
286
287 return librist_crypto_srp_hash(AB, pad_len * 2, hash_out);
288 }
289
290 /* Pre-0.2.16 unpadded variant; only reachable when ctx->legacy_pad is set. */
291 static int librist_crypto_srp_hash_2_bignum_unpadded(BIGNUM *A, BIGNUM *B, uint8_t hash_out[SHA256_DIGEST_LENGTH])
292 {
293 size_t A_size = BIGNUM_GET_BINARY_SIZE(A);
294 size_t B_size = BIGNUM_GET_BINARY_SIZE(B);
295 uint8_t AB[2048];
296
297 if (A_size + B_size > sizeof(AB))
298 return -1;
299
300 BIGNUM_WRITE_BYTES(A, AB, A_size);
301 BIGNUM_WRITE_BYTES(B, AB + A_size, B_size);
302
303 return librist_crypto_srp_hash(AB, A_size + B_size, hash_out);
304 }
305
306 static int srp_hash_uk(bool legacy_pad, size_t pad_len, BIGNUM *X, BIGNUM *Y, uint8_t hash_out[SHA256_DIGEST_LENGTH])
307 {
308 if (legacy_pad)
309 return librist_crypto_srp_hash_2_bignum_unpadded(X, Y, hash_out);
310 return librist_crypto_srp_hash_2_bignum_padded(pad_len, X, Y, hash_out);
311 }
312
313 static int librist_crypto_srp_hash_bignum(BIGNUM *in, uint8_t hash_out[SHA256_DIGEST_LENGTH])
314 {
315 size_t size = BIGNUM_GET_BINARY_SIZE(in);
316 uint8_t in_bytes[1024];
317 if (size > sizeof(in_bytes))
318 return -1;
319 BIGNUM_WRITE_BYTES(in, in_bytes, size);
320 return librist_crypto_srp_hash(in_bytes, size, hash_out);
321 }
322
323 static int librist_crypto_srp_calculate_m(BIGNUM *N, BIGNUM *g, const char *I, BIGNUM *s, BIGNUM *A, BIGNUM *B, const uint8_t K[SHA256_DIGEST_LENGTH], uint8_t m1_out[SHA256_DIGEST_LENGTH], bool correct) {
324 #if DEBUG_EXTRACT_SRP_EXCHANGE
325 fprintf(stderr, "\n\n%s\n", __func__);
326 fprintf(stderr, "DOING CORRECT? %d\n", correct);
327 BIGNUM_PRINT("N: ", N);
328 BIGNUM_PRINT("g: ", g);
329 fprintf(stderr, "I: %s\n", I);
330 BIGNUM_PRINT("s: ",s);
331 BIGNUM_PRINT("A: ", A);
332 BIGNUM_PRINT("B: ", B);
333 print_hash(K, "K: ");
334 #endif
335 uint8_t hash_tmp[SHA256_DIGEST_LENGTH];
336 int ret = -1;
337 HASH_CONTEXT hash_ctx;
338 {
339 uint8_t hash_N[SHA256_DIGEST_LENGTH];
340 uint8_t hash_g[SHA256_DIGEST_LENGTH];
341
342 /* hash_bignum returns -1 (without writing the output) when the
343 * bignum exceeds its 1024-byte staging buffer. Bail out instead
344 * of XOR-ing uninitialised stack into the SRP transcript. */
345 if (librist_crypto_srp_hash_bignum(N, hash_N) != 0 ||
346 librist_crypto_srp_hash_bignum(g, hash_g) != 0)
347 return -1;
348
349 for (size_t i=0; i < sizeof(hash_tmp); i++)
350 hash_tmp[i] = hash_N[i] ^ hash_g[i];
351 }
352
353 #if DEBUG_EXTRACT_SRP_EXCHANGE
354 print_hash(hash_tmp, "XOR: ");
355 #endif
356
357 HASH_CONTEXT_INIT(&hash_ctx, correct);
358
359 if (librist_crypto_srp_hash_update(&hash_ctx, hash_tmp, sizeof(hash_tmp)) != 0)
360 goto out;
361
362 if (librist_crypto_srp_hash((const uint8_t *)I, strlen(I), hash_tmp) != 0)
363 goto out;
364
365 if (librist_crypto_srp_hash_update(&hash_ctx, hash_tmp, sizeof(hash_tmp)) != 0)
366 goto out;
367
368 if (librist_crypto_srp_hash_update_bignum(&hash_ctx, s) != 0)
369 goto out;
370
371 if (librist_crypto_srp_hash_update_bignum(&hash_ctx, A) != 0)
372 goto out;
373
374 if (librist_crypto_srp_hash_update_bignum(&hash_ctx, B) != 0)
375 goto out;
376
377 if (librist_crypto_srp_hash_update(&hash_ctx, K, SHA256_DIGEST_LENGTH) != 0)
378 goto out;
379
380 if (librist_crypto_srp_hash_final(&hash_ctx, m1_out) != 0)
381 goto out;
382
383 ret = 0;
384
385 #if DEBUG_EXTRACT_SRP_EXCHANGE
386 print_hash(m1_out, "M1: ");
387 fprintf(stderr, "\n\n");
388 #endif
389
390 out:
391 HASH_CONTEXT_FREE(&hash_ctx);
392 return ret;
393 }
394
395 static int librist_crypto_srp_calculate_m2(const BIGNUM *A,const uint8_t M1[SHA256_DIGEST_LENGTH],const uint8_t K[SHA256_DIGEST_LENGTH], uint8_t M2_OUT[SHA256_DIGEST_LENGTH], bool correct) {
396 HASH_CONTEXT hash_ctx;
397 HASH_CONTEXT_INIT(&hash_ctx, correct);
398 int ret;
399 ret = librist_crypto_srp_hash_update_bignum(&hash_ctx, A);
400 if (ret != 0)
401 goto out;
402
403 ret = librist_crypto_srp_hash_update(&hash_ctx, M1, SHA256_DIGEST_LENGTH);
404 if (ret != 0)
405 goto out;
406
407 ret = librist_crypto_srp_hash_update(&hash_ctx, K, SHA256_DIGEST_LENGTH);
408 if (ret != 0)
409 goto out;
410
411 ret = librist_crypto_srp_hash_final(&hash_ctx, M2_OUT);
412
413 out:
414 HASH_CONTEXT_FREE(&hash_ctx);
415
416 return ret;
417 }
418
419
420 struct librist_crypto_srp_authenticator_ctx {
421 //Static values once created
422 BIGNUM N;
423 BIGNUM g;
424 BIGNUM v;
425 BIGNUM s;
426
427 //Supplied by client
428 BIGNUM A;
429
430 //Random number in range 0, N-1
431 BIGNUM b;
432 BIGNUM k;
433 BIGNUM B;
434
435 //Session key
436 uint8_t key[SHA256_DIGEST_LENGTH];
437
438 uint8_t m2[SHA256_DIGEST_LENGTH];
439
440 bool correct_hashing_init;
441 bool legacy_pad; //pre-0.2.16 unpadded u/k (srp-compat=1)
442 };
443
444 struct librist_crypto_srp_authenticator_ctx *librist_crypto_srp_authenticator_ctx_create(const char* n_hex, const char *g_hex, const uint8_t *v_bytes, size_t v_len, const uint8_t *s_bytes, size_t s_len, bool correct, bool legacy_pad) {
445 if (!v_bytes || !s_bytes || v_len == 0 || s_len == 0)
446 return NULL;
447 struct librist_crypto_srp_authenticator_ctx *ctx = calloc(1, sizeof(*ctx));
448 if (!ctx)
449 return NULL;
450
451 ctx->correct_hashing_init = correct;
452 ctx->legacy_pad = legacy_pad;
453 BIGNUM_INIT(&ctx->N);
454 BIGNUM_INIT(&ctx->g);
455 BIGNUM_INIT(&ctx->v);
456 BIGNUM_INIT(&ctx->s);
457
458 int ret = 0;
459 BIGNUM_FROM_STRING(&ctx->N, n_hex);
460 if (ret != 0)
461 goto fail;
462
463 BIGNUM_FROM_STRING(&ctx->g, g_hex);
464 if (ret != 0)
465 goto fail;
466
467 BIGNUM_FROM_ARRAY(&ctx->v, v_bytes, v_len);
468 if (ret != 0)
469 goto fail;
470
471 /* RFC 5054: reject v == 0 (session key would not depend on verifier) */
472 if (BIGNUM_EQUALS(&ctx->v, 0))
473 goto fail;
474
475 BIGNUM_FROM_ARRAY(&ctx->s, s_bytes, s_len);
476 if (ret != 0)
477 goto fail;
478
479
480 BIGNUM_INIT(&ctx->A);
481 BIGNUM_INIT(&ctx->b);
482 BIGNUM_INIT(&ctx->k);
483 BIGNUM_INIT(&ctx->B);
484 return ctx;
485
486 fail:
487 BIGNUM_FREE(&ctx->N);
488 BIGNUM_FREE(&ctx->g);
489 BIGNUM_FREE(&ctx->v);
490 BIGNUM_FREE(&ctx->s);
491 free(ctx);
492 return NULL;
493 }
494
495 void librist_crypto_srp_authenticator_ctx_free(struct librist_crypto_srp_authenticator_ctx *ctx) {
496 if (ctx == NULL)
497 return;
498 BIGNUM_FREE(&ctx->N);
499 BIGNUM_FREE(&ctx->g);
500 BIGNUM_FREE(&ctx->v);
501 BIGNUM_FREE(&ctx->s);
502
503 BIGNUM_FREE(&ctx->A);
504 BIGNUM_FREE(&ctx->b);
505 BIGNUM_FREE(&ctx->k);
506 BIGNUM_FREE(&ctx->B);
507
508 free(ctx);
509 }
510
511 int librist_crypto_srp_authenticator_write_g_bytes(struct librist_crypto_srp_authenticator_ctx *ctx, uint8_t *g_buf, size_t g_buf_len) {
512 size_t size = BIGNUM_GET_BINARY_SIZE(&ctx->g);
513 if (g_buf_len < size)
514 return -1;
515
516 BIGNUM_WRITE_BYTES(&ctx->g, g_buf, size);
517 return (int)size;
518 }
519
520 int librist_crypto_srp_authenticator_write_n_bytes(struct librist_crypto_srp_authenticator_ctx *ctx, uint8_t *n_buf, size_t n_buf_len) {
521 size_t size = BIGNUM_GET_BINARY_SIZE(&ctx->N);
522 if (n_buf_len < size)
523 return -1;
524
525 BIGNUM_WRITE_BYTES(&ctx->N, n_buf, size);
526 return (int)size;
527 }
528
529 int librist_crypto_srp_authenticator_write_B_bytes(struct librist_crypto_srp_authenticator_ctx *ctx, uint8_t* B_buf, size_t B_buf_len) {
530 size_t size = BIGNUM_GET_BINARY_SIZE(&ctx->B);
531 if (B_buf_len < size)
532 return -1;
533
534 BIGNUM_WRITE_BYTES(&ctx->B, B_buf, size);
535 return (int)size;
536 }
537
538 void librist_crypto_srp_authenticator_write_M2_bytes(struct librist_crypto_srp_authenticator_ctx *ctx, uint8_t* m2_buf) {
539 memcpy(m2_buf, ctx->m2, sizeof(ctx->m2));
540 }
541
542 const uint8_t *librist_crypto_srp_authenticator_get_key(struct librist_crypto_srp_authenticator_ctx *ctx) {
543 return ctx->key;
544 }
545
546 //We received A, verify that it's secure, set b and then calculate:
547 //k=SHA256(N,g)
548 //B=(kv + g^b) % N
549 int librist_crypto_srp_authenticator_handle_A(struct librist_crypto_srp_authenticator_ctx *ctx, uint8_t *A_buf, size_t A_buf_len) {
550 int ret = 0;
551 /* Bound the attacker-supplied operand against N. Without this,
552 * mbedtls_mpi_exp_mod() runs in time proportional to A_buf_len,
553 * giving a peer-pre-auth CPU DoS handle. */
554 if (A_buf_len == 0 || A_buf_len > BIGNUM_GET_BINARY_SIZE(&ctx->N))
555 return -1;
556 BIGNUM_FROM_ARRAY(&ctx->A, A_buf, A_buf_len);
557 if (ret != 0)
558 return -1;
559
560 BIGNUM tmp;
561 BIGNUM_INIT(&tmp);
562 BIGNUM_MOD_RED(&tmp, &ctx->A, &ctx->N);
563 if (ret != 0)
564 goto out;
565
566 // RFC 5054 / SRP-6a: server aborts if A mod N == 0
567 if (BIGNUM_EQUALS(&tmp, 0)) {
568 ret = -1;
569 goto out;
570 }
571
572
573
574 //Set b
575 #if DEBUG_USE_EXAMPLE_CONSTANTS
576 const char b_hex[] = "ED0D58FF861A1FC75A0829BEA5F1392D2B13AB2B05CBCD6ED1E71AAAD761E856";
577 BIGNUM_FROM_STRING(&ctx->b, b_hex);
578 #else
579 BIGNUM_RANDOM(&ctx->b, &ctx->N);
580 if (ret != 0)
581 goto out;
582 #endif
583
584
585 //calc k = SHA256(PAD(N) | PAD(g)) (RFC 5054); legacy_pad strips the PAD.
586 uint8_t k_hash[SHA256_DIGEST_LENGTH];
587 size_t N_size = BIGNUM_GET_BINARY_SIZE(&ctx->N);
588 if (srp_hash_uk(ctx->legacy_pad, N_size, &ctx->N, &ctx->g, k_hash) != 0) {
589 ret = -1;
590 goto out;
591 }
592 BIGNUM_FROM_ARRAY(&ctx->k, k_hash, sizeof(k_hash));
593 if (ret != 0)
594 goto out;
595
596 BIGNUM tmp2;
597 BIGNUM_INIT(&tmp2);
598
599 //calc B
600 BIGNUM_MULT_BIG(&tmp, &ctx->k, &ctx->v);
601 if (ret != 0)
602 goto b_out;
603
604 BIGNUM_EXP_MOD(&tmp2, &ctx->g, &ctx->b, &ctx->N);
605 if (ret != 0)
606 goto b_out;
607
608 BIGNUM_ADD_BIG(&tmp, &tmp, &tmp2);
609 if (ret != 0)
610 goto b_out;
611
612 BIGNUM_MOD_RED(&ctx->B, &tmp, &ctx->N);
613 if (ret != 0)
614 goto b_out;
615
616 /* RFC 5054: authenticator aborts if B mod N == 0 */
617 if (BIGNUM_EQUALS(&ctx->B, 0)) {
618 ret = -1;
619 goto b_out;
620 }
621
622 #if DEBUG_EXTRACT_SRP_EXCHANGE
623 fprintf(stderr, "%s\n", __func__);
624 BIGNUM_PRINT("b: ", &ctx->b);
625 BIGNUM_PRINT("k: ", &ctx->k);
626 BIGNUM_PRINT("B: ", &ctx->B);
627 #endif
628
629
630 b_out:
631 BIGNUM_FREE(&tmp2);
632
633 out:
634 BIGNUM_FREE(&tmp);
635 return ret;
636 }
637
638 //Calculate M1 & verify against client supplied M1 value
639 //u = SHA256(A, B)
640 //S = ((Av^u) ^ b) % N
641 //K = SHA256(S)
642 //M1 = SHA256(SHA256(N) xor SHA256(g), SHA256(I, s, A, B, K)
643 //Verify that client supplied M1 matches our calculation
644 //Then calculate M2:
645 //M2 = SHA256(A, M1, K)
646 int librist_crypto_srp_authenticator_verify_m1(struct librist_crypto_srp_authenticator_ctx *ctx, const char *username, uint8_t *client_m1_buf) {
647 uint8_t u_hash[SHA256_DIGEST_LENGTH];
648 size_t pad = BIGNUM_GET_BINARY_SIZE(&ctx->N);
649 if (srp_hash_uk(ctx->legacy_pad, pad, &ctx->A, &ctx->B, u_hash) != 0) {
650 return -1;
651 }
652
653 int ret = 0;
654 BIGNUM u;
655 BIGNUM tmp1;
656 BIGNUM tmp2;
657 BIGNUM_INIT(&u);
658 BIGNUM_INIT(&tmp1);
659 BIGNUM_INIT(&tmp2);
660
661 BIGNUM_FROM_ARRAY(&u, u_hash, sizeof(u_hash));
662 if (ret != 0)
663 goto out;
664
665 /* RFC 5054: authenticator aborts if u == 0 */
666 {
667 BIGNUM u_mod;
668 BIGNUM_INIT(&u_mod);
669 BIGNUM_MOD_RED(&u_mod, &u, &ctx->N);
670 bool u_zero = BIGNUM_EQUALS(&u_mod, 0);
671 BIGNUM_FREE(&u_mod);
672 if (ret != 0 || u_zero) {
673 ret = -1;
674 goto out;
675 }
676 }
677
678 BIGNUM_EXP_MOD(&tmp1, &ctx->v, &u, &ctx->N);
679 if (ret != 0)
680 goto out;
681
682 BIGNUM_MULT_BIG(&tmp2, &ctx->A, &tmp1);
683 if (ret != 0)
684 goto out;
685
686 //tmp1 -> S
687 BIGNUM_EXP_MOD(&tmp1, &tmp2, &ctx->b, &ctx->N);
688 if (ret != 0)
689 goto out;
690
691 if (librist_crypto_srp_hash_bignum(&tmp1, ctx->key) != 0) {
692 ret = -1;
693 goto out;
694 }
695
696 #if DEBUG_EXTRACT_SRP_EXCHANGE
697 fprintf(stderr, "%s\n", __func__);
698 BIGNUM_PRINT("u: ", &u);
699 BIGNUM_PRINT("S: ", &tmp1);
700 print_hash(ctx->key, "K: ");
701 #endif
702
703 uint8_t m1_buf[SHA256_DIGEST_LENGTH];
704 ret = librist_crypto_srp_calculate_m(&ctx->N, &ctx->g, username, &ctx->s, &ctx->A, &ctx->B, ctx->key, m1_buf, ctx->correct_hashing_init);
705 if (ret != 0)
706 goto out;
707
708 #if DEBUG_EXTRACT_SRP_EXCHANGE
709 print_hash(m1_buf, "M1: ");
710 #endif
711
712 ret = constant_time_memcmp(m1_buf, client_m1_buf, sizeof(m1_buf));
713 if (ret != 0)
714 goto out;
715
716 ret = librist_crypto_srp_calculate_m2(&ctx->A, m1_buf, ctx->key, ctx->m2, ctx->correct_hashing_init);
717
718 #if DEBUG_EXTRACT_SRP_EXCHANGE
719 print_hash(ctx->m2, "M2: ");
720 #endif
721 out:
722 BIGNUM_FREE(&u);
723 BIGNUM_FREE(&tmp1);
724 BIGNUM_FREE(&tmp2);
725
726 return ret;
727 }
728
729 struct librist_crypto_srp_client_ctx {
730 //Static values once created
731 BIGNUM N;
732 BIGNUM g;
733 BIGNUM s;
734 BIGNUM a;
735 BIGNUM A;
736
737 //Supplied by server
738 BIGNUM B;
739
740 BIGNUM k;
741
742 //Session key
743 uint8_t key[SHA256_DIGEST_LENGTH];
744
745 uint8_t m1[SHA256_DIGEST_LENGTH];
746
747 bool correct_hashing_init;
748 bool legacy_pad; //pre-0.2.16 unpadded u/k (srp-compat=1)
749 };
750
751 int librist_crypto_srp_client_write_A_bytes(struct librist_crypto_srp_client_ctx *ctx, uint8_t *A_buf, size_t A_buf_len) {
752 size_t size = BIGNUM_GET_BINARY_SIZE(&ctx->A);
753 if (A_buf_len < size)
754 return -1;
755
756 BIGNUM_WRITE_BYTES(&ctx->A, A_buf, size);
757 return (int)size;
758 }
759
760 void librist_crypto_srp_client_write_M1_bytes(struct librist_crypto_srp_client_ctx *ctx, uint8_t M1_buf[SHA256_DIGEST_LENGTH]) {
761 memcpy(M1_buf, ctx->m1, sizeof(ctx->m1));
762 }
763
764
765 //We've received B from the server so now we compute:
766 //x = SHA256(s, SHA256(I, “:”, P))
767 //k = SHA256(N, g)
768 //u = SHA256(A, B)
769 //S = ((B – kg^x) ^ (a +ux)) % N
770 //K = SHA256(S)
771 //M1 = SHA256(SHA256(N) xor SHA256(g), SHA256(I, s, A, B, K))
772 int librist_crypto_srp_client_handle_B(struct librist_crypto_srp_client_ctx *ctx, uint8_t *B_bytes, size_t B_len, const char *username, const char *password) {
773 int ret = 0;
774
775 /* Same operand-size bound as the authenticator side; a malicious
776 * server otherwise drives mbedtls_mpi_exp_mod runtime via B. */
777 if (B_len == 0 || B_len > BIGNUM_GET_BINARY_SIZE(&ctx->N))
778 return -1;
779 BIGNUM_FROM_ARRAY(&ctx->B, B_bytes, B_len);
780 if (ret != 0)
781 return -1;
782
783 BIGNUM u;
784 BIGNUM x;
785 BIGNUM tmp1;
786 BIGNUM tmp2;
787 BIGNUM tmp3;
788 BIGNUM tmp4;
789
790 BIGNUM_INIT(&u);
791 BIGNUM_INIT(&x);
792 BIGNUM_INIT(&tmp1);
793 BIGNUM_INIT(&tmp2);
794 BIGNUM_INIT(&tmp3);
795 BIGNUM_INIT(&tmp4);
796
797 // RFC 5054 / SRP-6a: client aborts if B mod N == 0
798 BIGNUM_MOD_RED(&tmp1, &ctx->B, &ctx->N);
799 if (ret != 0)
800 goto out;
801
802 if (BIGNUM_EQUALS(&tmp1, 0)) {
803 ret = -1;
804 goto out;
805 }
806
807 //Calculate u = SHA256(PAD(A) | PAD(B)) (RFC 5054); legacy_pad strips PAD.
808 {
809 uint8_t u_hash[SHA256_DIGEST_LENGTH];
810 size_t u_pad = BIGNUM_GET_BINARY_SIZE(&ctx->N);
811 ret = srp_hash_uk(ctx->legacy_pad, u_pad, &ctx->A, &ctx->B, u_hash);
812 if (ret != 0)
813 goto out;
814
815
816 BIGNUM_FROM_ARRAY(&u, u_hash, sizeof(u_hash));
817 // And aborts if u mod N == 0
818 BIGNUM_MOD_RED(&tmp1, &u, &ctx->N);
819 if (ret != 0)
820 goto out;
821
822 if (BIGNUM_EQUALS(&tmp1, 0)) {
823 ret = -1;
824 goto out;
825 }
826 }
827
828 //Calculate k = SHA256(PAD(N) | PAD(g)) (RFC 5054); legacy_pad strips PAD.
829 {
830 uint8_t k_hash[SHA256_DIGEST_LENGTH];
831 size_t k_pad = BIGNUM_GET_BINARY_SIZE(&ctx->N);
832 ret = srp_hash_uk(ctx->legacy_pad, k_pad, &ctx->N, &ctx->g, k_hash);
833 if (ret != 0)
834 goto out;
835
836 BIGNUM_FROM_ARRAY(&tmp4, k_hash, sizeof(k_hash));
837 }
838
839 ret = librist_crypto_srp_calc_x(&ctx->s, username, password, strlen(password), &x, ctx->correct_hashing_init);
840 if (ret != 0)
841 goto out;
842
843 #if DEBUG_EXTRACT_SRP_EXCHANGE
844 fprintf(stderr, "%s\n", __func__);
845 BIGNUM_PRINT("u: ", &u);
846 BIGNUM_PRINT("k: ", &tmp4);
847 BIGNUM_PRINT("x: ", &x);
848 #endif
849
850 //Calculate 'v' (g^x)
851 BIGNUM_EXP_MOD(&tmp2, &ctx->g, &x, &ctx->N);
852 if (ret != 0)
853 goto out;
854
855 // k (tmp4) * g^x (tmp2)
856 BIGNUM_MULT_BIG(&tmp3, &tmp4, &tmp2);
857 if (ret != 0)
858 goto out;
859
860 // B - kg^x (tmp3)
861 BIGNUM_SUB_BIG(&tmp1, &ctx->B, &tmp3);
862 if (ret != 0)
863 goto out;
864
865 //u * x
866 BIGNUM_MULT_BIG(&tmp3, &u, &x);
867 if (ret != 0)
868 goto out;
869
870 //a + ux (tmp3)
871 BIGNUM_ADD_BIG(&tmp2, &ctx->a, &tmp3);
872 if (ret != 0)
873 goto out;
874
875 //S = ((B – kg^x) (tmp1) ^ (a +ux)) (tmp2) % N
876 BIGNUM_EXP_MOD(&tmp3, &tmp1, &tmp2, &ctx->N);
877 if (ret != 0)
878 goto out;
879
880 ret = librist_crypto_srp_hash_bignum(&tmp3, ctx->key);
881 if (ret != 0)
882 goto out;
883
884 #if DEBUG_EXTRACT_SRP_EXCHANGE
885 BIGNUM_PRINT("S: ", &tmp3);
886 print_hash(ctx->key, "K: ");
887 #endif
888
889 ret = librist_crypto_srp_calculate_m(&ctx->N, &ctx->g, username, &ctx->s, &ctx->A, &ctx->B, ctx->key, ctx->m1, ctx->correct_hashing_init);
890
891 out:
892 BIGNUM_FREE(&u);
893 BIGNUM_FREE(&x);
894 BIGNUM_FREE(&tmp4);
895 BIGNUM_FREE(&tmp1);
896 BIGNUM_FREE(&tmp2);
897 BIGNUM_FREE(&tmp3);
898 return ret;
899 }
900
901 int librist_crypto_srp_client_verify_m2(struct librist_crypto_srp_client_ctx *ctx, uint8_t *m2) {
902 uint8_t calc_m2[SHA256_DIGEST_LENGTH];
903 int ret = librist_crypto_srp_calculate_m2(&ctx->A, ctx->m1, ctx->key, calc_m2, ctx->correct_hashing_init);
904 if (ret != 0)
905 return ret;
906
907 return constant_time_memcmp(m2, calc_m2, sizeof(calc_m2));
908 }
909
910 const uint8_t *librist_crypto_srp_client_get_key(struct librist_crypto_srp_client_ctx *ctx) {
911 return ctx->key;
912 }
913
914 struct librist_crypto_srp_client_ctx *librist_crypto_srp_client_ctx_create(bool default_ng, uint8_t *N_bytes, size_t N_len, uint8_t *g_bytes, size_t g_len, uint8_t *s_bytes, size_t s_len, bool correct, bool legacy_pad) {
915 if (!s_bytes || s_len == 0 || s_len > 64)
916 return NULL;
917 if (!default_ng) {
918 if (!N_bytes || N_len < 128 || N_len > 1024 || !g_bytes || g_len == 0 || g_len > N_len)
919 return NULL;
920 }
921
922 struct librist_crypto_srp_client_ctx *ctx = calloc(1, sizeof(*ctx));
923 if (!ctx)
924 return NULL;
925
926 ctx->correct_hashing_init = correct;
927 ctx->legacy_pad = legacy_pad;
928 BIGNUM_INIT(&ctx->N);
929 BIGNUM_INIT(&ctx->g);
930 BIGNUM_INIT(&ctx->s);
931 BIGNUM_INIT(&ctx->a);
932 BIGNUM_INIT(&ctx->A);
933 BIGNUM_INIT(&ctx->B);
934 BIGNUM_INIT(&ctx->k);
935
936 int ret = 0;
937 BIGNUM_FROM_ARRAY(&ctx->s, s_bytes, s_len);
938 if (ret != 0)
939 goto fail;
940
941 if (default_ng) {
942 const char *N = NULL;
943 const char *g = NULL;
944 librist_get_ng_constants(LIBRIST_SRP_NG_DEFAULT, &N, &g);
945 BIGNUM_FROM_STRING(&ctx->N, N);
946 if (ret != 0)
947 goto fail;
948
949 BIGNUM_FROM_STRING(&ctx->g, g);
950 if (ret != 0)
951 goto fail;
952 } else {
953 BIGNUM_FROM_ARRAY(&ctx->g, g_bytes,g_len);
954 if (ret != 0)
955 goto fail;
956
957 BIGNUM_FROM_ARRAY(&ctx->N, N_bytes, N_len);
958 if (ret != 0)
959 goto fail;
960 }
961
962 #if DEBUG_USE_EXAMPLE_CONSTANTS
963 const char a_hex[] = "138AB4045633AD14961CB1AD0720B1989104151C0708794491113302CCCC27D5";
964 BIGNUM_FROM_STRING(&ctx->a, a_hex);
965 #else
966 BIGNUM_RANDOM(&ctx->a, &ctx->N);
967 if (ret != 0)
968 goto fail;
969 #endif
970
971 BIGNUM_EXP_MOD(&ctx->A, &ctx->g, &ctx->a, &ctx->N);
972 if (ret != 0)
973 goto fail;
974
975 #if DEBUG_EXTRACT_SRP_EXCHANGE
976 fprintf(stderr, "%s\n", __func__);
977 BIGNUM_PRINT("N: ", &ctx->N);
978 BIGNUM_PRINT("g: ", &ctx->g);
979 BIGNUM_PRINT("s: ", &ctx->s);
980 BIGNUM_PRINT("a: ", &ctx->a);
981 BIGNUM_PRINT("A: ", &ctx->A);
982 #endif
983
984 return ctx;
985 fail:
986 librist_crypto_srp_client_ctx_free(ctx);
987 return NULL;
988 }
989
990 void librist_crypto_srp_client_ctx_free(struct librist_crypto_srp_client_ctx *ctx) {
991 if (!ctx)
992 return;
993 BIGNUM_FREE(&ctx->N);
994 BIGNUM_FREE(&ctx->g);
995 BIGNUM_FREE(&ctx->s);
996 BIGNUM_FREE(&ctx->a);
997 BIGNUM_FREE(&ctx->A);
998 BIGNUM_FREE(&ctx->B);
999 BIGNUM_FREE(&ctx->k);
1000
1001 free(ctx);
1002 }
1003
1004 //Creates a verifier & salt as follows:
1005 //salt 32 byte random
1006 //v: v = g^x
1007 int librist_crypto_srp_create_verifier(
1008 const char *n_hex, const char *g_hex,
1009 const char *username, const char *password,
1010 unsigned char **bytes_s, size_t *len_s,
1011 unsigned char **bytes_v, size_t *len_v,
1012 bool correct)
1013
1014 {
1015 if (*bytes_s != NULL || *bytes_v != NULL)
1016 return -1;
1017
1018 int ret = 0;
1019 BIGNUM s;
1020 BIGNUM v;
1021 BIGNUM x;
1022 BIGNUM n;
1023 BIGNUM g;
1024
1025 BIGNUM_INIT(&s);
1026 BIGNUM_INIT(&v);
1027 BIGNUM_INIT(&x);
1028 BIGNUM_INIT(&n);
1029 BIGNUM_INIT(&g);
1030
1031 BIGNUM_FROM_STRING(&n, n_hex);
1032 if (ret != 0)
1033 goto failed;
1034
1035 BIGNUM_FROM_STRING(&g, g_hex);
1036 if (ret != 0)
1037 goto failed;
1038
1039 /* Generate salt as raw bytes so leading zeros are preserved.
1040 * Bignum export strips them, shrinking the effective salt domain. */
1041 uint8_t salt_raw[32];
1042 #if DEBUG_USE_EXAMPLE_CONSTANTS
1043 const char salt_hex[] = "72F9D5383B7EB7599FB63028F47475B60A55F313D40E0BE023E026C97C0A2C32";
1044 BIGNUM_FROM_STRING(&s, salt_hex);
1045 if (ret != 0)
1046 goto failed;
1047 memset(salt_raw, 0, sizeof(salt_raw));
1048 {
1049 size_t ss = BIGNUM_GET_BINARY_SIZE(&s);
1050 if (ss <= sizeof(salt_raw))
1051 BIGNUM_WRITE_BYTES(&s, salt_raw + (sizeof(salt_raw) - ss), ss);
1052 }
1053 #else
1054 if (_librist_crypto_ramdom_get_bytes(salt_raw, sizeof(salt_raw)) != 0)
1055 goto failed;
1056 BIGNUM_FROM_ARRAY(&s, salt_raw, sizeof(salt_raw));
1057 if (ret != 0)
1058 goto failed;
1059 #endif
1060
1061 // Calculate x
1062 if (librist_crypto_srp_calc_x(&s, username, password, strlen(password),
1063 &x, correct) != 0) goto failed;
1064
1065 BIGNUM_EXP_MOD(&v, &g, &x, &n);
1066 if (ret != 0)
1067 goto failed;
1068
1069 *len_s = sizeof(salt_raw);
1070 *bytes_s = malloc(sizeof(salt_raw));
1071 if (!*bytes_s) goto failed;
1072 memcpy(*bytes_s, salt_raw, sizeof(salt_raw));
1073
1074 BIGNUM_WRITE_BYTES_ALLOC(&v, bytes_v, len_v, failed);
1075
1076 #if DEBUG_EXTRACT_SRP_EXCHANGE
1077 fprintf(stderr, "%s\n", __func__);
1078 BIGNUM_PRINT("N: ", &n);
1079 BIGNUM_PRINT("G: ", &g);
1080 BIGNUM_PRINT("s: ", &s);
1081 BIGNUM_PRINT("v: ", &v);
1082 BIGNUM_PRINT("x: ", &x);
1083 #endif
1084
1085 BIGNUM_FREE(&s);
1086 BIGNUM_FREE(&v);
1087 BIGNUM_FREE(&x);
1088 BIGNUM_FREE(&n);
1089 BIGNUM_FREE(&g);
1090 return 0;
1091
1092 failed:
1093 BIGNUM_FREE(&s);
1094 BIGNUM_FREE(&v);
1095 BIGNUM_FREE(&x);
1096 BIGNUM_FREE(&n);
1097 BIGNUM_FREE(&g);
1098
1099 free(*bytes_s);
1100 free(*bytes_v);
1101 *bytes_s = NULL;
1102 *bytes_v = NULL;
1103 *len_s = 0;
1104 *len_v = 0;
1105
1106 return -1;
1107 }