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