Skip to content

Commit c906790

Browse files
committed
[NUC472/M487] Call BSP driver rather than direct register access in DES alter.
1 parent 9edda18 commit c906790

File tree

2 files changed

+68
-72
lines changed

2 files changed

+68
-72
lines changed

features/mbedtls/targets/TARGET_NUVOTON/TARGET_M480/des/des_alt.c

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -356,39 +356,40 @@ static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_S
356356
/* Init crypto module */
357357
crypto_init();
358358

359-
/* NOTE: Don't call driver function TDES_Open in BSP because we don't want its internal multiple context (channel) support.
360-
* Multiple context (channel) support has done in the upper layer.
359+
/* Configure TDES_CTL register
360+
*
361+
* BSP TDES driver supports multiple channels. Just use channel #0.
362+
*
363+
* Relationship of keying option and TDES H/W mode configuration
364+
* 1: All three keys are independent ==> TDES 3-key mode (TMODE=1, 3KEYS=1)
365+
* 2: K1 and K2 are independent, and K3 = K1 ==> TDES 2-key mode (TMODE=1, 3KEYS=0)
366+
* 3: All three keys are identical, i.e. K1 = K2 = K3 ==> DES mode (TMODE=0)
367+
*
368+
* tdes_opmode is combination of TMODE/OPMODE, but TDES_Open I/F requires TMODE/OPMODE to be separate.
369+
* We need to divide tdes_opmode to TMODE and OPMODE.
370+
*
371+
* TDES_IN_OUT_WHL_SWAP lets TDES H/W know input/output data are arranged in below for DMA transfer:
372+
* 1. BE for byte sequence in word
373+
* 2. BE for word sequence in double-word
361374
*/
362-
CRPT->TDES_CTL = (0 << CRPT_TDES_CTL_CHANNEL_Pos) | (enc << CRPT_TDES_CTL_ENCRPT_Pos) |
363-
tdes_opmode | (TDES_IN_OUT_WHL_SWAP << CRPT_TDES_CTL_BLKSWAP_Pos);
364-
365-
// Keying option 1: All three keys are independent.
366-
// Keying option 2: K1 and K2 are independent, and K3 = K1.
367-
// Keying option 3: All three keys are identical, i.e. K1 = K2 = K3.
368-
if (keyopt == 1) {
369-
CRPT->TDES_CTL |= CRPT_TDES_CTL_3KEYS_Msk;
370-
} else {
371-
CRPT->TDES_CTL &= ~CRPT_TDES_CTL_3KEYS_Msk;
372-
}
373-
374-
/* Set DES/TDES keys
375-
*
376-
* NOTE: TDES_SetKey in BSP is not used because we need to make up an extra 2-dimension array to pass keys.
375+
TDES_Open(0, // Channel number (0~4)
376+
enc, // 0: decode, 1: encode
377+
(tdes_opmode & CRPT_TDES_CTL_TMODE_Msk) ? 1 : 0, // 0: DES, 1: TDES
378+
(keyopt == 1) ? 1 : 0, // 0: TDES 2-key mode, 1: TDES 3-key mode
379+
tdes_opmode & CRPT_TDES_CTL_OPMODE_Msk, // ECB/CBC/CFB/OFB/CTR
380+
TDES_IN_OUT_WHL_SWAP); // TDES_NO_SWAP~TDES_IN_OUT_WHL_SWAP
381+
382+
/* Set DES/TDES keys
383+
*
384+
* TDES_SetKey requires 3x2 word array. Change 3x8 byte array to 3x2 word array.
377385
*/
378-
uint32_t val;
379-
volatile uint32_t *tdes_key = (uint32_t *) ((uint32_t) &CRPT->TDES0_KEY1H + (0x40 * 0));
380-
val = nu_get32_be(key[0] + 0);
381-
*tdes_key ++ = val;
382-
val = nu_get32_be(key[0] + 4);
383-
*tdes_key ++ = val;
384-
val = nu_get32_be(key[1] + 0);
385-
*tdes_key ++ = val;
386-
val = nu_get32_be(key[1] + 4);
387-
*tdes_key ++ = val;
388-
val = nu_get32_be(key[2] + 0);
389-
*tdes_key ++ = val;
390-
val = nu_get32_be(key[2] + 4);
391-
*tdes_key ++ = val;
386+
unsigned i;
387+
uint32_t keys3x2[3][2];
388+
for (i = 0; i < 3; i ++ ) {
389+
keys3x2[i][0] = nu_get32_be(key[i] + 0);
390+
keys3x2[i][1] = nu_get32_be(key[i] + 4);
391+
}
392+
TDES_SetKey(0, keys3x2);
392393

393394
uint32_t rmn = length;
394395
const unsigned char *in_pos = input;
@@ -406,11 +407,8 @@ static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_S
406407

407408
TDES_SetDMATransfer(0, (uint32_t) dmabuf_in, (uint32_t) dmabuf_out, data_len);
408409

409-
/* Start enc/dec.
410-
*
411-
* NOTE: Don't call driver function TDES_Start in BSP because of the multiple context (channel) reason as above.
412-
*/
413-
CRPT->TDES_CTL |= CRPT_TDES_CTL_START_Msk | (CRYPTO_DMA_ONE_SHOT << CRPT_TDES_CTL_DMALAST_Pos);
410+
/* Start enc/dec */
411+
TDES_Start(0, CRYPTO_DMA_ONE_SHOT);
414412
while (CRPT->TDES_STS & CRPT_TDES_STS_BUSY_Msk);
415413

416414
memcpy(out_pos, dmabuf_out, data_len);

features/mbedtls/targets/TARGET_NUVOTON/TARGET_NUC472/des/des_alt.c

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -356,39 +356,40 @@ static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_S
356356
/* Init crypto module */
357357
crypto_init();
358358

359-
/* NOTE: Don't call driver function TDES_Open in BSP because we don't want its internal multiple context (channel) support.
360-
* Multiple context (channel) support has done in the upper layer.
359+
/* Configure TDES_CTL register
360+
*
361+
* BSP TDES driver supports multiple channels. Just use channel #0.
362+
*
363+
* Relationship of keying option and TDES H/W mode configuration
364+
* 1: All three keys are independent ==> TDES 3-key mode (TMODE=1, 3KEYS=1)
365+
* 2: K1 and K2 are independent, and K3 = K1 ==> TDES 2-key mode (TMODE=1, 3KEYS=0)
366+
* 3: All three keys are identical, i.e. K1 = K2 = K3 ==> DES mode (TMODE=0)
367+
*
368+
* tdes_opmode is combination of TMODE/OPMODE, but TDES_Open I/F requires TMODE/OPMODE to be separate.
369+
* We need to divide tdes_opmode to TMODE and OPMODE.
370+
*
371+
* TDES_IN_OUT_WHL_SWAP lets TDES H/W know input/output data are arranged in below for DMA transfer:
372+
* 1. BE for byte sequence in word
373+
* 2. BE for word sequence in double-word
361374
*/
362-
CRPT->TDES_CTL = (0 << CRPT_TDES_CTL_CHANNEL_Pos) | (enc << CRPT_TDES_CTL_ENCRPT_Pos) |
363-
tdes_opmode | (TDES_IN_OUT_WHL_SWAP << CRPT_TDES_CTL_BLKSWAP_Pos);
364-
365-
// Keying option 1: All three keys are independent.
366-
// Keying option 2: K1 and K2 are independent, and K3 = K1.
367-
// Keying option 3: All three keys are identical, i.e. K1 = K2 = K3.
368-
if (keyopt == 1) {
369-
CRPT->TDES_CTL |= CRPT_TDES_CTL_3KEYS_Msk;
370-
} else {
371-
CRPT->TDES_CTL &= ~CRPT_TDES_CTL_3KEYS_Msk;
372-
}
373-
374-
/* Set DES/TDES keys
375-
*
376-
* NOTE: TDES_SetKey in BSP is not used because we need to make up an extra 2-dimension array to pass keys.
375+
TDES_Open(0, // Channel number (0~4)
376+
enc, // 0: decode, 1: encode
377+
(tdes_opmode & CRPT_TDES_CTL_TMODE_Msk) ? 1 : 0, // 0: DES, 1: TDES
378+
(keyopt == 1) ? 1 : 0, // 0: TDES 2-key mode, 1: TDES 3-key mode
379+
tdes_opmode & CRPT_TDES_CTL_OPMODE_Msk, // ECB/CBC/CFB/OFB/CTR
380+
TDES_IN_OUT_WHL_SWAP); // TDES_NO_SWAP~TDES_IN_OUT_WHL_SWAP
381+
382+
/* Set DES/TDES keys
383+
*
384+
* TDES_SetKey requires 3x2 word array. Change 3x8 byte array to 3x2 word array.
377385
*/
378-
uint32_t val;
379-
volatile uint32_t *tdes_key = (uint32_t *) ((uint32_t) &CRPT->TDES0_KEY1H + (0x40 * 0));
380-
val = nu_get32_be(key[0] + 0);
381-
*tdes_key ++ = val;
382-
val = nu_get32_be(key[0] + 4);
383-
*tdes_key ++ = val;
384-
val = nu_get32_be(key[1] + 0);
385-
*tdes_key ++ = val;
386-
val = nu_get32_be(key[1] + 4);
387-
*tdes_key ++ = val;
388-
val = nu_get32_be(key[2] + 0);
389-
*tdes_key ++ = val;
390-
val = nu_get32_be(key[2] + 4);
391-
*tdes_key ++ = val;
386+
unsigned i;
387+
uint32_t keys3x2[3][2];
388+
for (i = 0; i < 3; i ++ ) {
389+
keys3x2[i][0] = nu_get32_be(key[i] + 0);
390+
keys3x2[i][1] = nu_get32_be(key[i] + 4);
391+
}
392+
TDES_SetKey(0, keys3x2);
392393

393394
uint32_t rmn = length;
394395
const unsigned char *in_pos = input;
@@ -406,11 +407,8 @@ static int mbedtls_des_docrypt(uint16_t keyopt, uint8_t key[3][MBEDTLS_DES_KEY_S
406407

407408
TDES_SetDMATransfer(0, (uint32_t) dmabuf_in, (uint32_t) dmabuf_out, data_len);
408409

409-
/* Start enc/dec.
410-
*
411-
* NOTE: Don't call driver function TDES_Start in BSP because of the multiple context (channel) reason as above.
412-
*/
413-
CRPT->TDES_CTL |= CRPT_TDES_CTL_START_Msk | (CRYPTO_DMA_ONE_SHOT << CRPT_TDES_CTL_DMALAST_Pos);
410+
/* Start enc/dec */
411+
TDES_Start(0, CRYPTO_DMA_ONE_SHOT);
414412
while (CRPT->TDES_STS & CRPT_TDES_STS_BUSY_Msk);
415413

416414
memcpy(out_pos, dmabuf_out, data_len);

0 commit comments

Comments
 (0)