1
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
62
63
64
74
75
76
77
78
79
80
81
82
83
87
88
89
90
91
92
100
101
102
103
104
116
117
118
119
120
121
122
123
127
128
129
131
132
133
134
135
136
137
138
139
140
142
146
147
150
151
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
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
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
285
286
287
290
291
292
293
294
295
296
297
298
299
300
301
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
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
372
373
374
377
378
381
382
385
386
389
390
392
398
401
402
403
404
405
406
408
409
410
411
412
418
419
420
421
425
426
427
433
434
435
436
437
438
439
440
441
447
448
449
452
453
454
455
456
457
460
461
462
463
464
465
466
472
474
475
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
504
508
513
518
519
520
521
522
529
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
604
605
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/* ... */
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <limits.h>
#include <assert.h>
#include <stdlib.h>
#include <sys/param.h>
#include "esp_system.h"
#include "esp_log.h"
#include "esp_attr.h"
#include "esp_intr_alloc.h"11 includes
#if CONFIG_PM_ENABLE
#include "esp_pm.h"
#endif
#include "esp_private/periph_ctrl.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "bignum_impl.h"
#include "mbedtls/bignum.h"
#include "hal/mpi_hal.h"6 includes
/* ... */
static const __attribute__((unused)) char *TAG = "bignum";
#define ciL (sizeof(mbedtls_mpi_uint))
#define biL (ciL << 3)
#if defined(CONFIG_MBEDTLS_MPI_USE_INTERRUPT)
static SemaphoreHandle_t op_complete_sem;
#if defined(CONFIG_PM_ENABLE)
static esp_pm_lock_handle_t s_pm_cpu_lock;
static esp_pm_lock_handle_t s_pm_sleep_lock;/* ... */
#endif
static IRAM_ATTR void esp_mpi_complete_isr(void *arg)
{
BaseType_t higher_woken;
mpi_hal_clear_interrupt();
xSemaphoreGiveFromISR(op_complete_sem, &higher_woken);
if (higher_woken) {
portYIELD_FROM_ISR();
}{...}
}{...}
static esp_err_t esp_mpi_isr_initialise(void)
{
mpi_hal_clear_interrupt();
mpi_hal_interrupt_enable(true);
if (op_complete_sem == NULL) {
static StaticSemaphore_t op_sem_buf;
op_complete_sem = xSemaphoreCreateBinaryStatic(&op_sem_buf);
if (op_complete_sem == NULL) {
ESP_LOGE(TAG, "Failed to create intr semaphore");
return ESP_FAIL;
}{...}
const int isr_flags = esp_intr_level_to_flags(CONFIG_MBEDTLS_MPI_INTERRUPT_LEVEL);
esp_err_t ret;
ret = esp_intr_alloc(ETS_RSA_INTR_SOURCE, isr_flags, esp_mpi_complete_isr, NULL, NULL);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to allocate RSA interrupt %d", ret);
abort();
}{...}
}{...}
#ifdef CONFIG_PM_ENABLE
if (s_pm_cpu_lock == NULL) {
if (esp_pm_lock_create(ESP_PM_NO_LIGHT_SLEEP, 0, "mpi_sleep", &s_pm_sleep_lock) != ESP_OK) {
ESP_LOGE(TAG, "Failed to create PM sleep lock");
return ESP_FAIL;
}{...}
if (esp_pm_lock_create(ESP_PM_CPU_FREQ_MAX, 0, "mpi_cpu", &s_pm_cpu_lock) != ESP_OK) {
ESP_LOGE(TAG, "Failed to create PM CPU lock");
return ESP_FAIL;
}{...}
}{...}
esp_pm_lock_acquire(s_pm_cpu_lock);
esp_pm_lock_acquire(s_pm_sleep_lock);/* ... */
#endif
return ESP_OK;
}{...}
static int esp_mpi_wait_intr(void)
{
if (!xSemaphoreTake(op_complete_sem, 2000 / portTICK_PERIOD_MS)) {
ESP_LOGE("MPI", "Timed out waiting for completion of MPI Interrupt");
return -1;
}{...}
#ifdef CONFIG_PM_ENABLE
esp_pm_lock_release(s_pm_cpu_lock);
esp_pm_lock_release(s_pm_sleep_lock);/* ... */
#endif
mpi_hal_interrupt_enable(false);
return 0;
}{...}
/* ... */
#endif
/* ... */
static inline size_t bits_to_words(size_t bits)
{
return (bits + 31) / 32;
}{ ... }
/* ... */
#if defined(MBEDTLS_MPI_EXP_MOD_ALT) || defined(MBEDTLS_MPI_EXP_MOD_ALT_FALLBACK)
static size_t mpi_words(const mbedtls_mpi *mpi)
{
for (size_t i = mpi->MBEDTLS_PRIVATE(n); i > 0; i--) {
if (mpi->MBEDTLS_PRIVATE(p[i - 1]) != 0) {
return i;
}{...}
}{...}
return 0;
}{ ... }
/* ... */#endif
/* ... */
static mbedtls_mpi_uint modular_inverse(const mbedtls_mpi *M)
{
int i;
uint64_t t = 1;
uint64_t two_2_i_minus_1 = 2;
uint64_t two_2_i = 4;
uint64_t N = M->MBEDTLS_PRIVATE(p[0]);
for (i = 2; i <= 32; i++) {
if ((mbedtls_mpi_uint) N * t % two_2_i >= two_2_i_minus_1) {
t += two_2_i_minus_1;
}{...}
two_2_i_minus_1 <<= 1;
two_2_i <<= 1;
}{...}
return (mbedtls_mpi_uint)(UINT32_MAX - t + 1);
}{ ... }
/* ... */
static int calculate_rinv(mbedtls_mpi *Rinv, const mbedtls_mpi *M, int num_words)
{
int ret;
size_t num_bits = num_words * 32;
mbedtls_mpi RR;
mbedtls_mpi_init(&RR);
MBEDTLS_MPI_CHK(mbedtls_mpi_set_bit(&RR, num_bits * 2, 1));
MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(Rinv, &RR, M));
cleanup:
mbedtls_mpi_free(&RR);
return ret;
}{ ... }
/* ... */
int esp_mpi_mul_mpi_mod(mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi *Y, const mbedtls_mpi *M)
{
int ret = 0;
size_t x_bits = mbedtls_mpi_bitlen(X);
size_t y_bits = mbedtls_mpi_bitlen(Y);
size_t m_bits = mbedtls_mpi_bitlen(M);
size_t z_bits = MIN(m_bits, x_bits + y_bits);
size_t x_words = bits_to_words(x_bits);
size_t y_words = bits_to_words(y_bits);
size_t m_words = bits_to_words(m_bits);
size_t z_words = bits_to_words(z_bits);
size_t hw_words = mpi_hal_calc_hardware_words(MAX(x_words, MAX(y_words, m_words)));
mbedtls_mpi Rinv;
mbedtls_mpi_uint Mprime;
mbedtls_mpi_init(&Rinv);
MBEDTLS_MPI_CHK(calculate_rinv(&Rinv, M, hw_words));
Mprime = modular_inverse(M);
esp_mpi_enable_hardware_hw_op();
esp_mpi_mul_mpi_mod_hw_op(X, Y, M, &Rinv, Mprime, hw_words);
MBEDTLS_MPI_CHK(mbedtls_mpi_grow(Z, z_words));
mpi_hal_read_result_hw_op(Z->MBEDTLS_PRIVATE(p), Z->MBEDTLS_PRIVATE(n), z_words);
Z->MBEDTLS_PRIVATE(s) = X->MBEDTLS_PRIVATE(s) * Y->MBEDTLS_PRIVATE(s);
cleanup:
mbedtls_mpi_free(&Rinv);
esp_mpi_disable_hardware_hw_op();
return ret;
}{ ... }
#if defined(MBEDTLS_MPI_EXP_MOD_ALT) || defined(MBEDTLS_MPI_EXP_MOD_ALT_FALLBACK)
#ifdef ESP_MPI_USE_MONT_EXP
/* ... */
static size_t mbedtls_mpi_msb( const mbedtls_mpi *X )
{
int i, j;
if (X != NULL && X->MBEDTLS_PRIVATE(n) != 0) {
for (i = X->MBEDTLS_PRIVATE(n) - 1; i >= 0; i--) {
if (X->MBEDTLS_PRIVATE(p[i]) != 0) {
for (j = biL - 1; j >= 0; j--) {
if ((X->MBEDTLS_PRIVATE(p[i]) & (1 << j)) != 0) {
return (i * biL) + j;
}{...}
}{...}
}{...}
}{...}
}{...}
return 0;
}{ ... }
/* ... */
static int mpi_montgomery_exp_calc( mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi *Y, const mbedtls_mpi *M,
mbedtls_mpi *Rinv,
size_t hw_words,
mbedtls_mpi_uint Mprime )
{
int ret = 0;
mbedtls_mpi X_, one;
mbedtls_mpi_init(&X_);
mbedtls_mpi_init(&one);
if ( ( ( ret = mbedtls_mpi_grow(&one, hw_words) ) != 0 ) ||
( ( ret = mbedtls_mpi_set_bit(&one, 0, 1) ) != 0 ) ) {
goto cleanup2;
}{...}
{
int t = mbedtls_mpi_msb(Y);
esp_mpi_enable_hardware_hw_op();
MBEDTLS_MPI_CHK( esp_mont_hw_op(&X_, X, Rinv, M, Mprime, hw_words, false) );
MBEDTLS_MPI_CHK( esp_mont_hw_op(Z, Rinv, &one, M, Mprime, hw_words, true) );
for (int i = t; i >= 0; i--) {
if (i != t) {
MBEDTLS_MPI_CHK( esp_mont_hw_op(Z, Z, Z, M, Mprime, hw_words, true) );
}{...}
if (mbedtls_mpi_get_bit(Y, i)) {
MBEDTLS_MPI_CHK( esp_mont_hw_op(Z, Z, &X_, M, Mprime, hw_words, true) );
}{...}
}{...}
MBEDTLS_MPI_CHK( esp_mont_hw_op(Z, Z, &one, M, Mprime, hw_words, true) );
}{...}
cleanup:
esp_mpi_disable_hardware_hw_op();
cleanup2:
mbedtls_mpi_free(&X_);
mbedtls_mpi_free(&one);
return ret;
}{ ... }
/* ... */#endif
/* ... */
static int esp_mpi_exp_mod( mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi *Y, const mbedtls_mpi *M, mbedtls_mpi *_Rinv )
{
int ret = 0;
mbedtls_mpi Rinv_new;
mbedtls_mpi *Rinv;
mbedtls_mpi_uint Mprime;
size_t x_words = mpi_words(X);
size_t y_words = mpi_words(Y);
size_t m_words = mpi_words(M);
/* ... */
size_t num_words = mpi_hal_calc_hardware_words(MAX(m_words, MAX(x_words, y_words)));
if (num_words * 32 > SOC_RSA_MAX_BIT_LEN) {
return MBEDTLS_ERR_MPI_NOT_ACCEPTABLE;
}{...}
if (mbedtls_mpi_cmp_int(M, 0) <= 0 || (M->MBEDTLS_PRIVATE(p[0]) & 1) == 0) {
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
}{...}
if (mbedtls_mpi_cmp_int(Y, 0) < 0) {
return MBEDTLS_ERR_MPI_BAD_INPUT_DATA;
}{...}
if (mbedtls_mpi_cmp_int(Y, 0) == 0) {
return mbedtls_mpi_lset(Z, 1);
}{...}
/* ... */
if (_Rinv == NULL) {
mbedtls_mpi_init(&Rinv_new);
Rinv = &Rinv_new;
}{...} else {
Rinv = _Rinv;
}{...}
if (Rinv->MBEDTLS_PRIVATE(p) == NULL) {
MBEDTLS_MPI_CHK(calculate_rinv(Rinv, M, num_words));
}{...}
Mprime = modular_inverse(M);
#ifdef ESP_MPI_USE_MONT_EXP
ret = mpi_montgomery_exp_calc(Z, X, Y, M, Rinv, num_words, Mprime) ;
MBEDTLS_MPI_CHK(ret);/* ... */
#else
esp_mpi_enable_hardware_hw_op();
#if defined (CONFIG_MBEDTLS_MPI_USE_INTERRUPT)
if (esp_mpi_isr_initialise() != ESP_OK) {
ret = -1;
esp_mpi_disable_hardware_hw_op();
goto cleanup;
}{...}
#endif/* ... */
esp_mpi_exp_mpi_mod_hw_op(X, Y, M, Rinv, Mprime, num_words);
ret = mbedtls_mpi_grow(Z, m_words);
if (ret != 0) {
esp_mpi_disable_hardware_hw_op();
goto cleanup;
}{...}
#if defined(CONFIG_MBEDTLS_MPI_USE_INTERRUPT)
ret = esp_mpi_wait_intr();
if (ret != 0) {
esp_mpi_disable_hardware_hw_op();
goto cleanup;
}{...}
#endif/* ... */
mpi_hal_read_result_hw_op(Z->MBEDTLS_PRIVATE(p), Z->MBEDTLS_PRIVATE(n), m_words);
esp_mpi_disable_hardware_hw_op();/* ... */
#endif
if (X->MBEDTLS_PRIVATE(s) == -1 && (Y->MBEDTLS_PRIVATE(p[0]) & 1) != 0) {
Z->MBEDTLS_PRIVATE(s) = -1;
MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(Z, M, Z));
}{...} else {
Z->MBEDTLS_PRIVATE(s) = 1;
}{...}
cleanup:
if (_Rinv == NULL) {
mbedtls_mpi_free(&Rinv_new);
}{...}
return ret;
}{ ... }
/* ... */#endif
/* ... */
int mbedtls_mpi_exp_mod( mbedtls_mpi *X, const mbedtls_mpi *A,
const mbedtls_mpi *E, const mbedtls_mpi *N,
mbedtls_mpi *_RR )
{
int ret;
#if defined(MBEDTLS_MPI_EXP_MOD_ALT_FALLBACK)
ret = esp_mpi_exp_mod( X, A, E, N, _RR );
if( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE ) {
ret = mbedtls_mpi_exp_mod_soft( X, A, E, N, _RR );
}{...}
#else/* ... */
ret = esp_mpi_exp_mod( X, A, E, N, _RR );/* ... */
#endif
/* ... */
return ret;
}{ ... }
#if defined(MBEDTLS_MPI_MUL_MPI_ALT)
static int mpi_mult_mpi_failover_mod_mult( mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi *Y, size_t z_words);
static int mpi_mult_mpi_overlong(mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi *Y, size_t y_words, size_t z_words);
int mbedtls_mpi_mul_mpi( mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi *Y )
{
int ret = 0;
size_t x_bits = mbedtls_mpi_bitlen(X);
size_t y_bits = mbedtls_mpi_bitlen(Y);
size_t x_words = bits_to_words(x_bits);
size_t y_words = bits_to_words(y_bits);
size_t z_words = bits_to_words(x_bits + y_bits);
size_t hw_words = mpi_hal_calc_hardware_words(MAX(x_words, y_words));
/* ... */
if (x_bits == 0 || y_bits == 0) {
ret = mbedtls_mpi_lset(Z, 0);
return ret;
}{...}
if (x_bits == 1) {
ret = mbedtls_mpi_copy(Z, Y);
Z->MBEDTLS_PRIVATE(s) *= X->MBEDTLS_PRIVATE(s);
return ret;
}{...}
if (y_bits == 1) {
ret = mbedtls_mpi_copy(Z, X);
Z->MBEDTLS_PRIVATE(s) *= Y->MBEDTLS_PRIVATE(s);
return ret;
}{...}
MBEDTLS_MPI_CHK( mbedtls_mpi_grow(Z, z_words) );
/* ... */
if (hw_words * 32 > SOC_RSA_MAX_BIT_LEN/2) {
if (z_words * 32 <= SOC_RSA_MAX_BIT_LEN) {
/* ... */
return mpi_mult_mpi_failover_mod_mult(Z, X, Y, z_words);
}{...} else {
if (y_words > x_words) {
return mpi_mult_mpi_overlong(Z, X, Y, y_words, z_words);
}{...} else {
return mpi_mult_mpi_overlong(Z, Y, X, x_words, z_words);
}{...}
}{...}
}{...}
esp_mpi_enable_hardware_hw_op();
esp_mpi_mul_mpi_hw_op(X, Y, hw_words);
mpi_hal_read_result_hw_op(Z->MBEDTLS_PRIVATE(p), Z->MBEDTLS_PRIVATE(n), z_words);
esp_mpi_disable_hardware_hw_op();
Z->MBEDTLS_PRIVATE(s) = X->MBEDTLS_PRIVATE(s) * Y->MBEDTLS_PRIVATE(s);
cleanup:
return ret;
}{ ... }
int mbedtls_mpi_mul_int( mbedtls_mpi *X, const mbedtls_mpi *A, mbedtls_mpi_uint b )
{
mbedtls_mpi _B;
mbedtls_mpi_uint p[1];
_B.MBEDTLS_PRIVATE(s) = 1;
_B.MBEDTLS_PRIVATE(n) = 1;
_B.MBEDTLS_PRIVATE(p) = p;
p[0] = b;
return( mbedtls_mpi_mul_mpi( X, A, &_B ) );
}{ ... }
/* ... */
static int mpi_mult_mpi_overlong(mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi *Y, size_t y_words, size_t z_words)
{
int ret = 0;
mbedtls_mpi Ztemp;
const size_t words_slice = y_words / 2;
const mbedtls_mpi Yp = {
.MBEDTLS_PRIVATE(p) = Y->MBEDTLS_PRIVATE(p),
.MBEDTLS_PRIVATE(n) = words_slice,
.MBEDTLS_PRIVATE(s) = Y->MBEDTLS_PRIVATE(s)
}{...};
const mbedtls_mpi Ypp = {
.MBEDTLS_PRIVATE(p) = Y->MBEDTLS_PRIVATE(p) + words_slice,
.MBEDTLS_PRIVATE(n) = y_words - words_slice,
.MBEDTLS_PRIVATE(s) = Y->MBEDTLS_PRIVATE(s)
}{...};
mbedtls_mpi_init(&Ztemp);
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi(&Ztemp, X, &Yp) );
MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi(Z, X, &Ypp) );
MBEDTLS_MPI_CHK( mbedtls_mpi_shift_l(Z, words_slice * 32) );
MBEDTLS_MPI_CHK( mbedtls_mpi_add_mpi(Z, Z, &Ztemp) );
cleanup:
mbedtls_mpi_free(&Ztemp);
return ret;
}{ ... }
static int mpi_mult_mpi_failover_mod_mult( mbedtls_mpi *Z, const mbedtls_mpi *X, const mbedtls_mpi *Y, size_t z_words)
{
int ret;
size_t hw_words = mpi_hal_calc_hardware_words(z_words);
esp_mpi_enable_hardware_hw_op();
esp_mpi_mult_mpi_failover_mod_mult_hw_op(X, Y, hw_words );
MBEDTLS_MPI_CHK( mbedtls_mpi_grow(Z, hw_words) );
mpi_hal_read_result_hw_op(Z->MBEDTLS_PRIVATE(p), Z->MBEDTLS_PRIVATE(n), hw_words);
Z->MBEDTLS_PRIVATE(s) = X->MBEDTLS_PRIVATE(s) * Y->MBEDTLS_PRIVATE(s);
/* ... */
assert(z_words - mpi_words(Z) <= (size_t)1);
cleanup:
esp_mpi_disable_hardware_hw_op();
return ret;
}{ ... }
/* ... */#endif