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 "esp_private/periph_ctrl.h"
47 #if (ESP32_C3_CRYPTO_PKC_SUPPORT == ENABLED)
57 periph_module_enable(PERIPH_RSA_MODULE);
60 REG_CLR_BIT(SYSTEM_RSA_PD_CTRL_REG, SYSTEM_RSA_MEM_PD);
65 while(REG_READ(RSA_QUERY_CLEAN_REG) == 0)
93 if(aLen <= 48 && bLen <= 48)
101 esp_crypto_mpi_lock_acquire();
104 REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1);
106 REG_WRITE(RSA_LENGTH_REG, (2 *
n) - 1);
109 for(i = 0; i <
n; i++)
113 REG_WRITE(RSA_MEM_X_BLOCK_BASE + i * 4,
a->data[i]);
117 REG_WRITE(RSA_MEM_X_BLOCK_BASE + i * 4, 0);
123 for(i = 0; i <
n; i++)
125 REG_WRITE(RSA_MEM_Z_BLOCK_BASE + i * 4, 0);
130 for(i = 0; i <
n; i++)
134 REG_WRITE(RSA_MEM_Z_BLOCK_BASE + (
n + i) * 4,
b->data[i]);
138 REG_WRITE(RSA_MEM_Z_BLOCK_BASE + (
n + i) * 4, 0);
143 REG_WRITE(RSA_MULT_START_REG, 1);
146 while(REG_READ(RSA_QUERY_INTERRUPT_REG) == 0)
151 r->sign = (
a->sign ==
b->sign) ? 1 : -1;
160 for(i = 0; i <
r->size; i++)
164 r->data[i] = REG_READ(RSA_MEM_Z_BLOCK_BASE + i * 4);
174 REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1);
177 esp_crypto_mpi_lock_release();
220 if(modLen > 0 && modLen <= 3072 && expLen > 0 && expLen <= 3072)
223 n =
MAX(modLen, expLen);
249 esp_crypto_mpi_lock_acquire();
252 REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1);
254 REG_WRITE(RSA_LENGTH_REG,
n - 1);
257 for(i = 0; i <
n; i++)
261 REG_WRITE(RSA_MEM_X_BLOCK_BASE + i * 4,
t.data[i]);
265 REG_WRITE(RSA_MEM_X_BLOCK_BASE + i * 4, 0);
270 for(i = 0; i <
n; i++)
274 REG_WRITE(RSA_MEM_Y_BLOCK_BASE + i * 4, e->
data[i]);
278 REG_WRITE(RSA_MEM_Y_BLOCK_BASE + i * 4, 0);
283 for(i = 0; i <
n; i++)
287 REG_WRITE(RSA_MEM_M_BLOCK_BASE + i * 4,
p->data[i]);
291 REG_WRITE(RSA_MEM_M_BLOCK_BASE + i * 4, 0);
296 for(i = 0; i <
n; i++)
300 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + i * 4, r2.
data[i]);
304 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + i * 4, 0);
309 for(
m = 2 -
p->data[0], i = 0; i < 4; i++)
311 m =
m * (2 -
m *
p->data[0]);
318 REG_WRITE(RSA_M_DASH_REG,
m);
321 REG_WRITE(RSA_SEARCH_ENABLE_REG, 1);
322 REG_WRITE(RSA_SEARCH_POS_REG, expLen - 1);
325 REG_WRITE(RSA_MODEXP_START_REG, 1);
328 while(REG_READ(RSA_QUERY_INTERRUPT_REG) == 0)
339 for(i = 0; i <
r->size; i++)
343 r->data[i] = REG_READ(RSA_MEM_Z_BLOCK_BASE + i * 4);
353 REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1);
356 esp_crypto_mpi_lock_release();
374 #if (X25519_SUPPORT == ENABLED || ED25519_SUPPORT == ENABLED)
388 esp_crypto_mpi_lock_acquire();
391 REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1);
393 REG_WRITE(RSA_LENGTH_REG, 7);
396 for(i = 0; i < 8; i++)
398 REG_WRITE(RSA_MEM_X_BLOCK_BASE + i * 4,
a[i]);
402 for(i = 0; i < 8; i++)
404 REG_WRITE(RSA_MEM_Y_BLOCK_BASE + i * 4,
b[i]);
408 REG_WRITE(RSA_MEM_M_BLOCK_BASE, 0xFFFFFFED);
409 REG_WRITE(RSA_MEM_M_BLOCK_BASE + 4, 0xFFFFFFFF);
410 REG_WRITE(RSA_MEM_M_BLOCK_BASE + 8, 0xFFFFFFFF);
411 REG_WRITE(RSA_MEM_M_BLOCK_BASE + 12, 0xFFFFFFFF);
412 REG_WRITE(RSA_MEM_M_BLOCK_BASE + 16, 0xFFFFFFFF);
413 REG_WRITE(RSA_MEM_M_BLOCK_BASE + 20, 0xFFFFFFFF);
414 REG_WRITE(RSA_MEM_M_BLOCK_BASE + 24, 0xFFFFFFFF);
415 REG_WRITE(RSA_MEM_M_BLOCK_BASE + 28, 0x7FFFFFFF);
418 REG_WRITE(RSA_MEM_RB_BLOCK_BASE, 0x000005A4);
419 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 4, 0x00000000);
420 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 8, 0x00000000);
421 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 12, 0x00000000);
422 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 16, 0x00000000);
423 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 20, 0x00000000);
424 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 24, 0x00000000);
425 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 28, 0x00000000);
428 REG_WRITE(RSA_M_DASH_REG, 0x286BCA1B);
430 REG_WRITE(RSA_MOD_MULT_START_REG, 1);
433 while(REG_READ(RSA_QUERY_INTERRUPT_REG) == 0)
438 for(i = 0; i < 8; i++)
440 r[i] = REG_READ(RSA_MEM_Z_BLOCK_BASE + i * 4);
444 REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1);
447 esp_crypto_mpi_lock_release();
451 #if (X448_SUPPORT == ENABLED || ED448_SUPPORT == ENABLED)
465 esp_crypto_mpi_lock_acquire();
468 REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1);
470 REG_WRITE(RSA_LENGTH_REG, 13);
473 for(i = 0; i < 14; i++)
475 REG_WRITE(RSA_MEM_X_BLOCK_BASE + i * 4,
a[i]);
479 for(i = 0; i < 14; i++)
481 REG_WRITE(RSA_MEM_Y_BLOCK_BASE + i * 4,
b[i]);
485 REG_WRITE(RSA_MEM_M_BLOCK_BASE, 0xFFFFFFFF);
486 REG_WRITE(RSA_MEM_M_BLOCK_BASE + 4, 0xFFFFFFFF);
487 REG_WRITE(RSA_MEM_M_BLOCK_BASE + 8, 0xFFFFFFFF);
488 REG_WRITE(RSA_MEM_M_BLOCK_BASE + 12, 0xFFFFFFFF);
489 REG_WRITE(RSA_MEM_M_BLOCK_BASE + 16, 0xFFFFFFFF);
490 REG_WRITE(RSA_MEM_M_BLOCK_BASE + 20, 0xFFFFFFFF);
491 REG_WRITE(RSA_MEM_M_BLOCK_BASE + 24, 0xFFFFFFFF);
492 REG_WRITE(RSA_MEM_M_BLOCK_BASE + 28, 0xFFFFFFFE);
493 REG_WRITE(RSA_MEM_M_BLOCK_BASE + 32, 0xFFFFFFFF);
494 REG_WRITE(RSA_MEM_M_BLOCK_BASE + 36, 0xFFFFFFFF);
495 REG_WRITE(RSA_MEM_M_BLOCK_BASE + 40, 0xFFFFFFFF);
496 REG_WRITE(RSA_MEM_M_BLOCK_BASE + 44, 0xFFFFFFFF);
497 REG_WRITE(RSA_MEM_M_BLOCK_BASE + 48, 0xFFFFFFFF);
498 REG_WRITE(RSA_MEM_M_BLOCK_BASE + 52, 0xFFFFFFFF);
501 REG_WRITE(RSA_MEM_RB_BLOCK_BASE, 0x00000002);
502 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 4, 0x00000000);
503 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 8, 0x00000000);
504 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 12, 0x00000000);
505 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 16, 0x00000000);
506 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 20, 0x00000000);
507 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 24, 0x00000000);
508 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 28, 0x00000003);
509 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 32, 0x00000000);
510 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 36, 0x00000000);
511 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 40, 0x00000000);
512 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 44, 0x00000000);
513 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 48, 0x00000000);
514 REG_WRITE(RSA_MEM_RB_BLOCK_BASE + 52, 0x00000000);
517 REG_WRITE(RSA_M_DASH_REG, 0x00000001);
519 REG_WRITE(RSA_MOD_MULT_START_REG, 1);
522 while(REG_READ(RSA_QUERY_INTERRUPT_REG) == 0)
527 for(i = 0; i < 14; i++)
529 r[i] = REG_READ(RSA_MEM_Z_BLOCK_BASE + i * 4);
533 REG_WRITE(RSA_CLEAR_INTERRUPT_REG, 1);
536 esp_crypto_mpi_lock_release();