Select one of the symbols to view example projects that use it.
 
Outline
#include "esp_system.h"
#include "mbedtls/bignum.h"
#include "utils/includes.h"
#include "utils/common.h"
#include "crypto.h"
#include "random.h"
#include "sha256.h"
#include "mbedtls/pk.h"
crypto_rng_wrapper(void *, unsigned char *, size_t)
crypto_bignum_init()
crypto_bignum_init_set(const u8 *, size_t)
crypto_bignum_init_uint(unsigned int)
crypto_bignum_deinit(struct crypto_bignum *, int)
crypto_bignum_to_bin(const struct crypto_bignum *, u8 *, size_t, size_t)
crypto_bignum_add(const struct crypto_bignum *, const struct crypto_bignum *, struct crypto_bignum *)
crypto_bignum_mod(const struct crypto_bignum *, const struct crypto_bignum *, struct crypto_bignum *)
crypto_bignum_exptmod(const struct crypto_bignum *, const struct crypto_bignum *, const struct crypto_bignum *, struct crypto_bignum *)
crypto_bignum_inverse(const struct crypto_bignum *, const struct crypto_bignum *, struct crypto_bignum *)
crypto_bignum_sub(const struct crypto_bignum *, const struct crypto_bignum *, struct crypto_bignum *)
crypto_bignum_div(const struct crypto_bignum *, const struct crypto_bignum *, struct crypto_bignum *)
crypto_bignum_mulmod(const struct crypto_bignum *, const struct crypto_bignum *, const struct crypto_bignum *, struct crypto_bignum *)
crypto_bignum_sqrmod(const struct crypto_bignum *, const struct crypto_bignum *, struct crypto_bignum *)
crypto_bignum_rshift(const struct crypto_bignum *, int, struct crypto_bignum *)
crypto_bignum_cmp(const struct crypto_bignum *, const struct crypto_bignum *)
crypto_bignum_bits(const struct crypto_bignum *)
crypto_bignum_is_zero(const struct crypto_bignum *)
crypto_bignum_is_one(const struct crypto_bignum *)
crypto_bignum_is_odd(const struct crypto_bignum *)
crypto_bignum_rand(struct crypto_bignum *, const struct crypto_bignum *)
crypto_bignum_legendre(const struct crypto_bignum *, const struct crypto_bignum *)
crypto_bignum_to_string(const struct crypto_bignum *, u8 *, size_t, size_t)
crypto_bignum_addmod(const struct crypto_bignum *, const struct crypto_bignum *, const struct crypto_bignum *, struct crypto_bignum *)
crypto_free_buffer(unsigned char *)
Files
loading (4/5)...
SourceVuESP-IDF Framework and ExamplesESP-IDFcomponents/wpa_supplicant/esp_supplicant/src/crypto/crypto_mbedtls-bignum.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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/* * SPDX-FileCopyrightText: 2015-2024 Espressif Systems (Shanghai) CO LTD * * SPDX-License-Identifier: Apache-2.0 *//* ... */ #ifdef ESP_PLATFORM #include "esp_system.h" #include "mbedtls/bignum.h"/* ... */ #endif #include "utils/includes.h" #include "utils/common.h" #include "crypto.h" #include "random.h" #include "sha256.h" #include "mbedtls/pk.h"6 includes static int crypto_rng_wrapper(void *ctx, unsigned char *buf, size_t len) { return random_get_bytes(buf, len); }{ ... } struct crypto_bignum *crypto_bignum_init(void) { mbedtls_mpi *bn = os_zalloc(sizeof(mbedtls_mpi)); if (bn == NULL) { return NULL; }{...} mbedtls_mpi_init(bn); return (struct crypto_bignum *)bn; }{ ... } struct crypto_bignum *crypto_bignum_init_set(const u8 *buf, size_t len) { int ret = 0; mbedtls_mpi *bn = os_zalloc(sizeof(mbedtls_mpi)); if (bn == NULL) { return NULL; }{...} MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(bn, buf, len)); return (struct crypto_bignum *) bn; cleanup: os_free(bn); return NULL; }{ ... } struct crypto_bignum * crypto_bignum_init_uint(unsigned int val) { mbedtls_mpi *bn = os_zalloc(sizeof(mbedtls_mpi)); if (bn == NULL) { return NULL; }{...} mbedtls_mpi_init(bn); mbedtls_mpi_lset(bn, val); return (struct crypto_bignum *)bn; }{ ... } void crypto_bignum_deinit(struct crypto_bignum *n, int clear) { mbedtls_mpi_free((mbedtls_mpi *)n); os_free((mbedtls_mpi *)n); }{ ... } int crypto_bignum_to_bin(const struct crypto_bignum *a, u8 *buf, size_t buflen, size_t padlen) { int num_bytes, offset; int ret; if (padlen > buflen) { return -1; }{...} num_bytes = mbedtls_mpi_size((mbedtls_mpi *) a); if ((size_t) num_bytes > buflen) { return -1; }{...} if (padlen > (size_t) num_bytes) { offset = padlen - num_bytes; }{...} else { offset = 0; }{...} os_memset(buf, 0, offset); MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary((mbedtls_mpi *) a, buf + offset, mbedtls_mpi_size((mbedtls_mpi *)a))); return num_bytes + offset; cleanup: return ret; }{ ... } int crypto_bignum_add(const struct crypto_bignum *a, const struct crypto_bignum *b, struct crypto_bignum *c) { return mbedtls_mpi_add_mpi((mbedtls_mpi *) c, (const mbedtls_mpi *) a, (const mbedtls_mpi *) b) ? -1 : 0; }{ ... } int crypto_bignum_mod(const struct crypto_bignum *a, const struct crypto_bignum *b, struct crypto_bignum *c) { return mbedtls_mpi_mod_mpi((mbedtls_mpi *) c, (const mbedtls_mpi *) a, (const mbedtls_mpi *) b) ? -1 : 0; }{ ... } int crypto_bignum_exptmod(const struct crypto_bignum *a, const struct crypto_bignum *b, const struct crypto_bignum *c, struct crypto_bignum *d) { return mbedtls_mpi_exp_mod((mbedtls_mpi *) d, (const mbedtls_mpi *) a, (const mbedtls_mpi *) b, (const mbedtls_mpi *) c, NULL) ? -1 : 0; }{ ... } int crypto_bignum_inverse(const struct crypto_bignum *a, const struct crypto_bignum *b, struct crypto_bignum *c) { return mbedtls_mpi_inv_mod((mbedtls_mpi *) c, (const mbedtls_mpi *) a, (const mbedtls_mpi *) b) ? -1 : 0; }{ ... } int crypto_bignum_sub(const struct crypto_bignum *a, const struct crypto_bignum *b, struct crypto_bignum *c) { return mbedtls_mpi_sub_mpi((mbedtls_mpi *) c, (const mbedtls_mpi *) a, (const mbedtls_mpi *) b) ? -1 : 0; }{ ... } int crypto_bignum_div(const struct crypto_bignum *a, const struct crypto_bignum *b, struct crypto_bignum *c) { return mbedtls_mpi_div_mpi((mbedtls_mpi *) c, NULL, (const mbedtls_mpi *) a, (const mbedtls_mpi *) b) ? -1 : 0; }{ ... } int crypto_bignum_mulmod(const struct crypto_bignum *a, const struct crypto_bignum *b, const struct crypto_bignum *c, struct crypto_bignum *d) { return mbedtls_mpi_mul_mpi((mbedtls_mpi *)d, (const mbedtls_mpi *)a, (const mbedtls_mpi *)b) || mbedtls_mpi_mod_mpi((mbedtls_mpi *)d, (mbedtls_mpi *)d, (const mbedtls_mpi *)c) ? -1 : 0; }{ ... } int crypto_bignum_sqrmod(const struct crypto_bignum *a, const struct crypto_bignum *b, struct crypto_bignum *c) { int res; struct crypto_bignum *tmp = crypto_bignum_init(); if (!tmp) { return -1; }{...} res = mbedtls_mpi_copy((mbedtls_mpi *) tmp, (const mbedtls_mpi *) a); res = crypto_bignum_mulmod(a, tmp, b, c); crypto_bignum_deinit(tmp, 0); return res ? -1 : 0; }{ ... } int crypto_bignum_rshift(const struct crypto_bignum *a, int n, struct crypto_bignum *r) { int res; res = mbedtls_mpi_copy((mbedtls_mpi *) r, (const mbedtls_mpi *) a); if (res) { return -1; }{...} res = mbedtls_mpi_shift_r((mbedtls_mpi *)r, n); return res ? -1 : 0; }{ ... } int crypto_bignum_cmp(const struct crypto_bignum *a, const struct crypto_bignum *b) { return mbedtls_mpi_cmp_mpi((const mbedtls_mpi *) a, (const mbedtls_mpi *) b); }{ ... } int crypto_bignum_bits(const struct crypto_bignum *a) { return mbedtls_mpi_bitlen((const mbedtls_mpi *) a); }{ ... } int crypto_bignum_is_zero(const struct crypto_bignum *a) { return (mbedtls_mpi_cmp_int((const mbedtls_mpi *) a, 0) == 0); }{ ... } int crypto_bignum_is_one(const struct crypto_bignum *a) { return (mbedtls_mpi_cmp_int((const mbedtls_mpi *) a, 1) == 0); }{ ... } int crypto_bignum_is_odd(const struct crypto_bignum *a) { return (mbedtls_mpi_get_bit((const mbedtls_mpi *) a, 0) == 1); }{ ... } int crypto_bignum_rand(struct crypto_bignum *r, const struct crypto_bignum *m) { return ((mbedtls_mpi_random((mbedtls_mpi *) r, 0, (const mbedtls_mpi *) m, crypto_rng_wrapper, NULL) != 0) ? -1 : 0); }{ ... } int crypto_bignum_legendre(const struct crypto_bignum *a, const struct crypto_bignum *p) { mbedtls_mpi exp, tmp; int res = -2, ret; mbedtls_mpi_init(&exp); mbedtls_mpi_init(&tmp); /* exp = (p-1) / 2 */ MBEDTLS_MPI_CHK(mbedtls_mpi_sub_int(&exp, (const mbedtls_mpi *) p, 1)); MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(&exp, 1)); MBEDTLS_MPI_CHK(mbedtls_mpi_exp_mod(&tmp, (const mbedtls_mpi *) a, &exp, (const mbedtls_mpi *) p, NULL)); if (mbedtls_mpi_cmp_int(&tmp, 1) == 0) { res = 1; }{...} else if (mbedtls_mpi_cmp_int(&tmp, 0) == 0 /* The below check is workaround for the case where HW * does not behave properly for X ^ A mod M when X is * power of M. Instead of returning value 0, value M is * returned.*//* ... */ || mbedtls_mpi_cmp_mpi(&tmp, (const mbedtls_mpi *)p) == 0) { res = 0; }{...} else { res = -1; }{...} cleanup: mbedtls_mpi_free(&tmp); mbedtls_mpi_free(&exp); return res; }{ ... } int crypto_bignum_to_string(const struct crypto_bignum *a, u8 *buf, size_t buflen, size_t padlen) { int num_bytes, offset; size_t outlen; if (padlen > buflen) { return -1; }{...} num_bytes = mbedtls_mpi_size((mbedtls_mpi *) a); if (padlen > (size_t) num_bytes) { offset = padlen - num_bytes; }{...} else { offset = 0; }{...} os_memset(buf, 0, offset); mbedtls_mpi_write_string((mbedtls_mpi *) a, 16, (char *)(buf + offset), mbedtls_mpi_size((mbedtls_mpi *)a), &outlen); return outlen; }{ ... } int crypto_bignum_addmod(const struct crypto_bignum *a, const struct crypto_bignum *b, const struct crypto_bignum *c, struct crypto_bignum *d) { struct crypto_bignum *tmp = crypto_bignum_init(); int ret = -1; if (mbedtls_mpi_add_mpi((mbedtls_mpi *) tmp, (const mbedtls_mpi *) a, (const mbedtls_mpi *) b) < 0) { goto fail; }{...} if (mbedtls_mpi_mod_mpi((mbedtls_mpi *) d, (const mbedtls_mpi *) tmp, (const mbedtls_mpi *) c) < 0) { goto fail; }{...} ret = 0; fail: crypto_bignum_deinit(tmp, 0); return ret; }{ ... } void crypto_free_buffer(unsigned char *buf) { os_free(buf); }{ ... }
Details
Show:
from
Types: Columns:
This file uses the notable symbols shown below. Click anywhere in the file to view more details.