Select one of the symbols to view example projects that use it.
 
Outline
#include <string.h>
#include "esp_log.h"
#include "esp_err.h"
#include <mbedtls/sha512.h>
#include "esp_srp_mpi.h"
#include "esp_srp.h"
#define SHA512_HASH_SZ
TAG
esp_srp_handle
hexdump_mpi(const char *, esp_mpi_t *)
N_3072
g_3072
esp_srp_init(esp_ng_type_t)
esp_srp_free(esp_srp_handle_t *)
calculate_x(char *, int, const char *, int, const char *, int)
calculate_padded_hash(esp_srp_handle_t *, const char *, int, const char *, int)
calculate_k(esp_srp_handle_t *)
calculate_u(esp_srp_handle_t *, char *, int)
__esp_srp_srv_pubkey(esp_srp_handle_t *, char **, int *)
_esp_srp_gen_salt_verifier(esp_srp_handle_t *, const char *, int, const char *, int, int)
esp_srp_srv_pubkey(esp_srp_handle_t *, const char *, int, const char *, int, int, char **, int *, char **)
esp_srp_srv_pubkey_from_salt_verifier(esp_srp_handle_t *, char **, int *)
esp_srp_gen_salt_verifier(const char *, int, const char *, int, char **, int, char **, int *)
esp_srp_set_salt_verifier(esp_srp_handle_t *, const char *, int, const char *, int)
esp_srp_get_session_key(esp_srp_handle_t *, char *, int, char **, uint16_t *)
esp_srp_exchange_proofs(esp_srp_handle_t *, char *, uint16_t, char *, char *)
Files
loading (3/5)...
SourceVuESP-IDF Framework and ExamplesESP-IDFcomponents/protocomm/src/crypto/srp6a/esp_srp.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2022-2023 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ // This file contains SRP6a implementation based on RFC 5054 #include <string.h> #include "esp_log.h" #include "esp_err.h" #include <mbedtls/sha512.h> #include "esp_srp_mpi.h" #include "esp_srp.h"6 includes #define SHA512_HASH_SZ 64 static const char *TAG = "srp6a"; typedef struct esp_srp_handle { esp_ng_type_t type; esp_mpi_ctx_t *ctx; /* N * the bytes_n simply points to the static array *//* ... */ esp_mpi_t *n; const char *bytes_n; int len_n; /* g * the bytes_g simply points to the static array *//* ... */ esp_mpi_t *g; const char *bytes_g; int len_g; /* Salt */ esp_mpi_t *s; char *bytes_s; int len_s; /* Verifier */ esp_mpi_t *v; /* B */ esp_mpi_t *B; char *bytes_B; int len_B; /* b */ esp_mpi_t *b; /* A */ esp_mpi_t *A; char *bytes_A; int len_A; /* K - session key*/ char *session_key; }{ ... } esp_srp_handle; static void hexdump_mpi(const char *name, esp_mpi_t *bn) { int len = 0; char *str = esp_mpi_to_bin(bn, &len); if (str) { ESP_LOGD(TAG, "%s ->", name); ESP_LOG_BUFFER_HEX_LEVEL(TAG, str, len, ESP_LOG_DEBUG); free(str); }{...} }{ ... } /************************* SRP Stuff *************************/ static const char N_3072[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xC9, 0x0F, 0xDA, 0xA2, 0x21, 0x68, 0xC2, 0x34, 0xC4, 0xC6, 0x62, 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const char g_3072[] = { 5 }; esp_srp_handle_t *esp_srp_init(esp_ng_type_t ng) { esp_srp_handle_t *hd = calloc(1, sizeof(esp_srp_handle)); if (!hd) { return NULL; }{...} hd->ctx = esp_mpi_ctx_new(); if (! hd->ctx) { goto error; }{...} if (ng != ESP_NG_3072) { goto error; }{...} hd->n = esp_mpi_new_from_bin(N_3072, sizeof(N_3072)); hd->bytes_n = N_3072; hd->len_n = sizeof(N_3072); if (! hd->n) { goto error; }{...} hd->g = esp_mpi_new_from_bin(g_3072, sizeof(g_3072)); hd->bytes_g = g_3072; hd->len_g = sizeof(g_3072); if (! hd->g) { goto error; }{...} hd->type = ng; return hd; error: esp_srp_free(hd); return NULL; }{ ... } void esp_srp_free(esp_srp_handle_t *hd) { if (!hd) { return; }{...} if (hd->ctx) { esp_mpi_ctx_free(hd->ctx); }{...} if (hd->n) { esp_mpi_free(hd->n); }{...} if (hd->g) { esp_mpi_free(hd->g); }{...} if (hd->s) { esp_mpi_free(hd->s); }{...} if (hd->bytes_s) { free(hd->bytes_s); }{...} if (hd->v) { esp_mpi_free(hd->v); }{...} if (hd->B) { esp_mpi_free(hd->B); }{...} if (hd->bytes_B) { free(hd->bytes_B); }{...} if (hd->b) { esp_mpi_free(hd->b); }{...} if (hd->A) { esp_mpi_free(hd->A); }{...} if (hd->bytes_A) { free(hd->bytes_A); }{...} if (hd->session_key) { free(hd->session_key); }{...} free(hd); }{ ... } static esp_mpi_t *calculate_x(char *bytes_salt, int salt_len, const char *username, int username_len, const char *pass, int pass_len) { unsigned char digest[SHA512_HASH_SZ]; mbedtls_sha512_context ctx; ESP_LOGD(TAG, "Username: %s | Passphrase: %s | Passphrase length: %d", username, pass, pass_len); mbedtls_sha512_init(&ctx); mbedtls_sha512_starts(&ctx, 0); mbedtls_sha512_update(&ctx, (unsigned char *)username, username_len); mbedtls_sha512_update(&ctx, (unsigned char *)":", 1); mbedtls_sha512_update(&ctx, (unsigned char *)pass, pass_len); mbedtls_sha512_finish(&ctx, digest); mbedtls_sha512_init(&ctx); mbedtls_sha512_starts(&ctx, 0); mbedtls_sha512_update(&ctx, (unsigned char *)bytes_salt, salt_len); mbedtls_sha512_update(&ctx, digest, sizeof(digest)); mbedtls_sha512_finish(&ctx, digest); mbedtls_sha512_free(&ctx); return esp_mpi_new_from_bin((char *)digest, sizeof(digest)); }{ ... } static esp_mpi_t *calculate_padded_hash(esp_srp_handle_t *hd, const char *a, int len_a, const char *b, int len_b) { unsigned char digest[SHA512_HASH_SZ]; mbedtls_sha512_context ctx; int pad_len; char *s = NULL; if (len_a > len_b) { pad_len = hd->len_n - len_b; }{...} else { pad_len = hd->len_n - len_a; }{...} if (pad_len) { s = malloc(pad_len); if (s) { memset(s, 0, pad_len); }{...} }{...} mbedtls_sha512_init(&ctx); mbedtls_sha512_starts(&ctx, 0); /* PAD (a) */ if (s && (len_a != hd->len_n)) { mbedtls_sha512_update(&ctx, (unsigned char *)s, hd->len_n - len_a); }{...} mbedtls_sha512_update(&ctx, (unsigned char *)a, len_a); /* PAD (b) */ if (s && (len_b != hd->len_n)) { mbedtls_sha512_update(&ctx, (unsigned char *)s, hd->len_n - len_b); }{...} mbedtls_sha512_update(&ctx, (unsigned char *)b, len_b); mbedtls_sha512_finish(&ctx, digest); mbedtls_sha512_free(&ctx); if (s) { free(s); }{...} return esp_mpi_new_from_bin((char *)digest, sizeof(digest)); }{ ... } /* k = SHA (N, PAD(g)) * * https://tools.ietf.org/html/draft-ietf-tls-srp-08 *//* ... */ static esp_mpi_t *calculate_k(esp_srp_handle_t *hd) { return calculate_padded_hash(hd, hd->bytes_n, hd->len_n, hd->bytes_g, hd->len_g); }{ ... } static esp_mpi_t *calculate_u(esp_srp_handle_t *hd, char *A, int len_A) { return calculate_padded_hash(hd, A, len_A, hd->bytes_B, hd->len_B); }{ ... } static esp_err_t __esp_srp_srv_pubkey(esp_srp_handle_t *hd, char **bytes_B, int *len_B) { esp_mpi_t *k = calculate_k(hd); esp_mpi_t *kv = NULL; esp_mpi_t *gb = NULL; if (!k) { goto error; }{...} hexdump_mpi("k", k); hd->b = esp_mpi_new(); if (!hd->b) { goto error; }{...} esp_mpi_get_rand(hd->b, 256, -1, 0); hexdump_mpi("b", hd->b); /* B = kv + g^b */ kv = esp_mpi_new(); gb = esp_mpi_new(); hd->B = esp_mpi_new(); if (!kv || !gb || ! hd->B) { goto error; }{...} esp_mpi_a_mul_b_mod_c(kv, k, hd->v, hd->n, hd->ctx); esp_mpi_a_exp_b_mod_c(gb, hd->g, hd->b, hd->n, hd->ctx); esp_mpi_a_add_b_mod_c(hd->B, kv, gb, hd->n, hd->ctx); hd->bytes_B = esp_mpi_to_bin(hd->B, len_B); hd->len_B = *len_B; *bytes_B = hd->bytes_B; esp_mpi_free(k); esp_mpi_free(kv); esp_mpi_free(gb); return ESP_OK; error: if (k) { esp_mpi_free(k); }{...} if (kv) { esp_mpi_free(kv); }{...} if (gb) { esp_mpi_free(gb); }{...} if (hd->B) { esp_mpi_free(hd->B); hd->B = NULL; }{...} if (hd->b) { esp_mpi_free(hd->b); hd->b = NULL; }{...} return ESP_FAIL; }{ ... } static esp_err_t _esp_srp_gen_salt_verifier(esp_srp_handle_t *hd, const char *username, int username_len, const char *pass, int pass_len, int salt_len) { /* Get Salt */ int str_salt_len; esp_mpi_t *x = NULL; hd->s = esp_mpi_new(); if (!hd->s) { ESP_LOGE(TAG, "Failed to allocate bignum s"); goto error; }{...} esp_mpi_get_rand(hd->s, 8 * salt_len, -1, 0); hd->bytes_s = esp_mpi_to_bin(hd->s, &str_salt_len); if (!hd->bytes_s) { ESP_LOGE(TAG, "Failed to generate salt of len %d", salt_len); goto error; }{...} hd->len_s = salt_len; ESP_LOGD(TAG, "Salt ->"); ESP_LOG_BUFFER_HEX_LEVEL(TAG, hd->bytes_s, str_salt_len, ESP_LOG_DEBUG); /* Calculate X which is simply a hash for all these things */ x = calculate_x(hd->bytes_s, str_salt_len, username, username_len, pass, pass_len); if (!x) { ESP_LOGE(TAG, "Failed to calculate x"); goto error; }{...} hexdump_mpi("x", x); /* v = g^x % N */ hd->v = esp_mpi_new(); if (!hd->v) { ESP_LOGE(TAG, "Failed to allocate bignum v"); goto error; }{...} esp_mpi_a_exp_b_mod_c(hd->v, hd->g, x, hd->n, hd->ctx); hexdump_mpi("Verifier", hd->v); esp_mpi_free(x); return ESP_OK; error: if (hd->s) { esp_mpi_free(hd->s); hd->s = NULL; }{...} if (hd->bytes_s) { free(hd->bytes_s); hd->bytes_s = NULL; hd->len_s = 0; }{...} if (x) { esp_mpi_free(x); x = NULL; }{...} if (hd->v) { esp_mpi_free(hd->v); hd->v = NULL; }{...} return ESP_FAIL; }{ ... } esp_err_t esp_srp_srv_pubkey(esp_srp_handle_t *hd, const char *username, int username_len, const char *pass, int pass_len, int salt_len, char **bytes_B, int *len_B, char **bytes_salt) { if (!hd || !username || !pass) { return ESP_ERR_INVALID_ARG; }{...} if (ESP_OK != _esp_srp_gen_salt_verifier(hd, username, username_len, pass, pass_len, salt_len)) { goto error; }{...} *bytes_salt = hd->bytes_s; if (__esp_srp_srv_pubkey(hd, bytes_B, len_B) < 0 ) { goto error; }{...} return ESP_OK; error: if (hd->s) { esp_mpi_free(hd->s); hd->s = NULL; }{...} if (*bytes_salt) { free(*bytes_salt); *bytes_salt = NULL; hd->bytes_s = NULL; hd->len_s = 0; }{...} if (hd->v) { esp_mpi_free(hd->v); hd->v = NULL; }{...} return ESP_FAIL; }{ ... } esp_err_t esp_srp_srv_pubkey_from_salt_verifier(esp_srp_handle_t *hd, char **bytes_B, int *len_B) { if (!hd || !bytes_B || !len_B) { return ESP_ERR_INVALID_ARG; }{...} return __esp_srp_srv_pubkey(hd, bytes_B, len_B); }{ ... } /* Generate salt-verifier pair for given username and password */ esp_err_t esp_srp_gen_salt_verifier(const char *username, int username_len, const char *pass, int pass_len, char **bytes_salt, int salt_len, char **verifier, int *verifier_len) { esp_err_t ret = ESP_FAIL; /* allocate and init temporary SRP handle */ esp_srp_handle_t *srp_hd = esp_srp_init(ESP_NG_3072); if (!srp_hd) { ESP_LOGE(TAG, "Failed to initialise security context!"); return ESP_ERR_NO_MEM; }{...} // get salt and verifier if (ESP_OK != _esp_srp_gen_salt_verifier(srp_hd, username, username_len, pass, pass_len, salt_len)) { goto cleanup; }{...} // convert to verifier bytes *verifier = esp_mpi_to_bin(srp_hd->v, verifier_len); if (!*verifier) { ESP_LOGE(TAG, "Failed to allocate verifier bytes!"); ret = ESP_ERR_NO_MEM; goto cleanup; }{...} *bytes_salt = srp_hd->bytes_s; srp_hd->bytes_s = NULL; // so that it won't be freed in `esp_srp_free` step ret = ESP_OK; cleanup: esp_srp_free(srp_hd); return ret; }{ ... } esp_err_t esp_srp_set_salt_verifier(esp_srp_handle_t *hd, const char *salt, int salt_len, const char *verifier, int verifier_len) { hd->bytes_s = malloc(salt_len); if (!hd->bytes_s) { goto error; }{...} memcpy(hd->bytes_s, salt, salt_len); hd->len_s = salt_len; hd->s = esp_mpi_new_from_bin(salt, salt_len); if (!hd->s) { goto error; }{...} hd->v = esp_mpi_new_from_bin(verifier, verifier_len); if (!hd->v) { goto error; }{...} return ESP_OK; error: if (hd->bytes_s) { free(hd->bytes_s); hd->bytes_s = NULL; hd->len_s = 0; }{...} if (hd->s) { esp_mpi_free(hd->s); hd->s = NULL; }{...} if (hd->v) { esp_mpi_free(hd->v); hd->v = NULL; }{...} return ESP_FAIL; }{ ... } esp_err_t esp_srp_get_session_key(esp_srp_handle_t *hd, char *bytes_A, int len_A, char **bytes_key, uint16_t *len_key) { esp_mpi_t *u = NULL; esp_mpi_t *vu = NULL; esp_mpi_t *avu = NULL; esp_mpi_t *S = NULL; char *bytes_S; int len_S; u = vu = avu = S = NULL; bytes_S = NULL; hd->bytes_A = malloc(len_A); if (! hd->bytes_A) { goto error; }{...} memcpy(hd->bytes_A, bytes_A, len_A); hd->len_A = len_A; hd->A = esp_mpi_new_from_bin(bytes_A, len_A); if (! hd->A) { goto error; }{...} u = calculate_u(hd, bytes_A, len_A); if (! u) { goto error; }{...} hexdump_mpi("u", u); /* S = (A v^u)^b */ vu = esp_mpi_new(); avu = esp_mpi_new(); S = esp_mpi_new(); if (!vu || !avu || !S ) { goto error; }{...} esp_mpi_a_exp_b_mod_c(vu, hd->v, u, hd->n, hd->ctx); esp_mpi_a_mul_b_mod_c(avu, hd->A, vu, hd->n, hd->ctx); esp_mpi_a_exp_b_mod_c(S, avu, hd->b, hd->n, hd->ctx); hexdump_mpi("S", S); bytes_S = esp_mpi_to_bin(S, &len_S); hd->session_key = malloc(SHA512_HASH_SZ); if (!hd->session_key || ! bytes_S) { goto error; }{...} mbedtls_sha512((unsigned char *)bytes_S, len_S, (unsigned char *)hd->session_key, 0); *bytes_key = hd->session_key; *len_key = SHA512_HASH_SZ; free(bytes_S); esp_mpi_free(vu); esp_mpi_free(avu); esp_mpi_free(S); esp_mpi_free(u); return ESP_OK; error: if (bytes_S) { free(bytes_S); }{...} if (vu) { esp_mpi_free(vu); }{...} if (avu) { esp_mpi_free(avu); }{...} if (S) { esp_mpi_free(S); }{...} if (u) { esp_mpi_free(u); }{...} if (hd->session_key) { free(hd->session_key); hd->session_key = NULL; }{...} if (hd->A) { esp_mpi_free(hd->A); hd->A = NULL; }{...} if (hd->bytes_A) { free(hd->bytes_A); hd->bytes_A = NULL; }{...} return ESP_FAIL; }{ ... } esp_err_t esp_srp_exchange_proofs(esp_srp_handle_t *hd, char *username, uint16_t username_len, char *bytes_user_proof, char *bytes_host_proof) { /* First calculate M */ unsigned char hash_n[SHA512_HASH_SZ]; unsigned char hash_g[SHA512_HASH_SZ]; unsigned char hash_n_xor_g[SHA512_HASH_SZ]; int i; unsigned char hash_I[SHA512_HASH_SZ]; mbedtls_sha512((unsigned char *)username, username_len, (unsigned char *)hash_I, 0); mbedtls_sha512((unsigned char *)hd->bytes_n, hd->len_n, (unsigned char *)hash_n, 0); int pad_len = hd->len_n - hd->len_g; char *s = calloc(pad_len, sizeof(char)); if (!s) { return ESP_ERR_NO_MEM; }{...} mbedtls_sha512_context ctx; mbedtls_sha512_init(&ctx); mbedtls_sha512_starts(&ctx, 0); mbedtls_sha512_update(&ctx, (unsigned char *)s, pad_len); mbedtls_sha512_update(&ctx, (unsigned char *)hd->bytes_g, hd->len_g); mbedtls_sha512_finish(&ctx, hash_g); mbedtls_sha512_free(&ctx); for (i = 0; i < SHA512_HASH_SZ; i++) { hash_n_xor_g[i] = hash_n[i] ^ hash_g[i]; }{...} unsigned char digest[SHA512_HASH_SZ]; mbedtls_sha512_init(&ctx); mbedtls_sha512_starts(&ctx, 0); mbedtls_sha512_update(&ctx, hash_n_xor_g, SHA512_HASH_SZ); mbedtls_sha512_update(&ctx, hash_I, SHA512_HASH_SZ); mbedtls_sha512_update(&ctx, (unsigned char *)hd->bytes_s, hd->len_s); mbedtls_sha512_update(&ctx, (unsigned char *)hd->bytes_A, hd->len_A); mbedtls_sha512_update(&ctx, (unsigned char *)hd->bytes_B, hd->len_B); mbedtls_sha512_update(&ctx, (unsigned char *)hd->session_key, SHA512_HASH_SZ); mbedtls_sha512_finish(&ctx, digest); mbedtls_sha512_free(&ctx); ESP_LOGD(TAG, "M ->"); ESP_LOG_BUFFER_HEX_LEVEL(TAG, (char *)digest, sizeof(digest), ESP_LOG_DEBUG); if (memcmp(bytes_user_proof, digest, SHA512_HASH_SZ) != 0) { free(s); return ESP_FAIL; }{...} /* M is now validated, let's proceed to H(AMK) */ mbedtls_sha512_init(&ctx); mbedtls_sha512_starts(&ctx, 0); mbedtls_sha512_update(&ctx, (unsigned char *)hd->bytes_A, hd->len_A); mbedtls_sha512_update(&ctx, digest, SHA512_HASH_SZ); mbedtls_sha512_update(&ctx, (unsigned char *)hd->session_key, SHA512_HASH_SZ); mbedtls_sha512_finish(&ctx, (unsigned char *)bytes_host_proof); mbedtls_sha512_free(&ctx); ESP_LOGD(TAG, "AMK ->"); ESP_LOG_BUFFER_HEX_LEVEL(TAG, (char *)bytes_host_proof, SHA512_HASH_SZ, ESP_LOG_DEBUG); if (s) { free(s); }{...} return ESP_OK; }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.