32 #define TRACE_LEVEL CRYPTO_TRACE_LEVEL
35 #include "esp_crypto_lock.h"
36 #include "soc/system_reg.h"
37 #include "soc/hwcrypto_reg.h"
38 #include "soc/dport_reg.h"
39 #include "soc/dport_access.h"
40 #include "esp_private/periph_ctrl.h"
49 #if (ESP32_S2_CRYPTO_PKC_SUPPORT == ENABLED)
59 periph_module_enable(PERIPH_RSA_MODULE);
62 DPORT_REG_CLR_BIT(DPORT_RSA_PD_CTRL_REG, DPORT_RSA_MEM_PD);
67 while(DPORT_REG_READ(RSA_QUERY_CLEAN_REG) == 0)
95 if(aLen <= 64 && bLen <= 64)
103 esp_crypto_mpi_lock_acquire();
106 DPORT_REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1);
108 DPORT_REG_WRITE(RSA_LENGTH_REG, (2 *
n) - 1);
111 for(i = 0; i <
n; i++)
115 DPORT_REG_WRITE(RSA_MEM_X_BLOCK_BASE + i * 4,
a->data[i]);
119 DPORT_REG_WRITE(RSA_MEM_X_BLOCK_BASE + i * 4, 0);
125 for(i = 0; i <
n; i++)
127 DPORT_REG_WRITE(RSA_MEM_Z_BLOCK_BASE + i * 4, 0);
132 for(i = 0; i <
n; i++)
136 DPORT_REG_WRITE(RSA_MEM_Z_BLOCK_BASE + (
n + i) * 4,
b->data[i]);
140 DPORT_REG_WRITE(RSA_MEM_Z_BLOCK_BASE + (
n + i) * 4, 0);
145 DPORT_REG_WRITE(RSA_MULT_START_REG, 1);
148 while(DPORT_REG_READ(RSA_QUERY_INTERRUPT_REG) == 0)
153 r->sign = (
a->sign ==
b->sign) ? 1 : -1;
162 DPORT_INTERRUPT_DISABLE();
165 for(i = 0; i <
r->size; i++)
169 r->data[i] = DPORT_SEQUENCE_REG_READ(RSA_MEM_Z_BLOCK_BASE + i * 4);
178 DPORT_INTERRUPT_RESTORE();
182 DPORT_REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1);
185 esp_crypto_mpi_lock_release();
228 if(modLen > 0 && modLen <= 4096 && expLen > 0 && expLen <= 4096)
231 n =
MAX(modLen, expLen);
257 esp_crypto_mpi_lock_acquire();
260 DPORT_REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1);
262 DPORT_REG_WRITE(RSA_LENGTH_REG,
n - 1);
265 for(i = 0; i <
n; i++)
269 DPORT_REG_WRITE(RSA_MEM_X_BLOCK_BASE + i * 4,
t.data[i]);
273 DPORT_REG_WRITE(RSA_MEM_X_BLOCK_BASE + i * 4, 0);
278 for(i = 0; i <
n; i++)
282 DPORT_REG_WRITE(RSA_MEM_Y_BLOCK_BASE + i * 4, e->
data[i]);
286 DPORT_REG_WRITE(RSA_MEM_Y_BLOCK_BASE + i * 4, 0);
291 for(i = 0; i <
n; i++)
295 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + i * 4,
p->data[i]);
299 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + i * 4, 0);
304 for(i = 0; i <
n; i++)
308 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + i * 4, r2.
data[i]);
312 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + i * 4, 0);
317 for(
m = 2 -
p->data[0], i = 0; i < 4; i++)
319 m =
m * (2 -
m *
p->data[0]);
326 DPORT_REG_WRITE(RSA_M_DASH_REG,
m);
329 DPORT_REG_WRITE(RSA_SEARCH_OPEN_REG, 1);
330 DPORT_REG_WRITE(RSA_SEARCH_POS_REG, expLen - 1);
333 DPORT_REG_WRITE(RSA_MODEXP_START_REG, 1);
336 while(DPORT_REG_READ(RSA_QUERY_INTERRUPT_REG) == 0)
347 DPORT_INTERRUPT_DISABLE();
350 for(i = 0; i <
r->size; i++)
354 r->data[i] = DPORT_SEQUENCE_REG_READ(RSA_MEM_Z_BLOCK_BASE + i * 4);
363 DPORT_INTERRUPT_RESTORE();
367 DPORT_REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1);
370 esp_crypto_mpi_lock_release();
388 #if (X25519_SUPPORT == ENABLED || ED25519_SUPPORT == ENABLED)
402 esp_crypto_mpi_lock_acquire();
405 DPORT_REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1);
407 DPORT_REG_WRITE(RSA_LENGTH_REG, 7);
410 for(i = 0; i < 8; i++)
412 DPORT_REG_WRITE(RSA_MEM_X_BLOCK_BASE + i * 4,
a[i]);
416 for(i = 0; i < 8; i++)
418 DPORT_REG_WRITE(RSA_MEM_Y_BLOCK_BASE + i * 4,
b[i]);
422 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE, 0xFFFFFFED);
423 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + 4, 0xFFFFFFFF);
424 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + 8, 0xFFFFFFFF);
425 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + 12, 0xFFFFFFFF);
426 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + 16, 0xFFFFFFFF);
427 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + 20, 0xFFFFFFFF);
428 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + 24, 0xFFFFFFFF);
429 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + 28, 0x7FFFFFFF);
432 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE, 0x000005A4);
433 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 4, 0x00000000);
434 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 8, 0x00000000);
435 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 12, 0x00000000);
436 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 16, 0x00000000);
437 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 20, 0x00000000);
438 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 24, 0x00000000);
439 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 28, 0x00000000);
442 DPORT_REG_WRITE(RSA_M_DASH_REG, 0x286BCA1B);
444 DPORT_REG_WRITE(RSA_MOD_MULT_START_REG, 1);
447 while(DPORT_REG_READ(RSA_QUERY_INTERRUPT_REG) == 0)
452 DPORT_INTERRUPT_DISABLE();
455 for(i = 0; i < 8; i++)
457 r[i] = DPORT_SEQUENCE_REG_READ(RSA_MEM_Z_BLOCK_BASE + i * 4);
461 DPORT_INTERRUPT_RESTORE();
463 DPORT_REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1);
466 esp_crypto_mpi_lock_release();
470 #if (X448_SUPPORT == ENABLED || ED448_SUPPORT == ENABLED)
484 esp_crypto_mpi_lock_acquire();
487 DPORT_REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1);
489 DPORT_REG_WRITE(RSA_LENGTH_REG, 13);
492 for(i = 0; i < 14; i++)
494 DPORT_REG_WRITE(RSA_MEM_X_BLOCK_BASE + i * 4,
a[i]);
498 for(i = 0; i < 14; i++)
500 DPORT_REG_WRITE(RSA_MEM_Y_BLOCK_BASE + i * 4,
b[i]);
504 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE, 0xFFFFFFFF);
505 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + 4, 0xFFFFFFFF);
506 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + 8, 0xFFFFFFFF);
507 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + 12, 0xFFFFFFFF);
508 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + 16, 0xFFFFFFFF);
509 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + 20, 0xFFFFFFFF);
510 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + 24, 0xFFFFFFFF);
511 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + 28, 0xFFFFFFFE);
512 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + 32, 0xFFFFFFFF);
513 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + 36, 0xFFFFFFFF);
514 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + 40, 0xFFFFFFFF);
515 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + 44, 0xFFFFFFFF);
516 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + 48, 0xFFFFFFFF);
517 DPORT_REG_WRITE(RSA_MEM_M_BLOCK_BASE + 52, 0xFFFFFFFF);
520 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE, 0x00000002);
521 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 4, 0x00000000);
522 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 8, 0x00000000);
523 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 12, 0x00000000);
524 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 16, 0x00000000);
525 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 20, 0x00000000);
526 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 24, 0x00000000);
527 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 28, 0x00000003);
528 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 32, 0x00000000);
529 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 36, 0x00000000);
530 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 40, 0x00000000);
531 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 44, 0x00000000);
532 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 48, 0x00000000);
533 DPORT_REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 52, 0x00000000);
536 DPORT_REG_WRITE(RSA_M_DASH_REG, 0x00000001);
538 DPORT_REG_WRITE(RSA_MOD_MULT_START_REG, 1);
541 while(DPORT_REG_READ(RSA_QUERY_INTERRUPT_REG) == 0)
546 DPORT_INTERRUPT_DISABLE();
549 for(i = 0; i < 14; i++)
551 r[i] = DPORT_SEQUENCE_REG_READ(RSA_MEM_Z_BLOCK_BASE + i * 4);
555 DPORT_INTERRUPT_RESTORE();
557 DPORT_REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1);
560 esp_crypto_mpi_lock_release();