Skip to content

Nuvoton: TRNG_Get support 32 bytes unalignment #5454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 22 additions & 13 deletions targets/TARGET_NUVOTON/TARGET_M480/trng_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,17 @@
/*
* Get Random number generator.
*/

#define PRNG_KEY_SIZE (0x20UL)

static volatile int g_PRNG_done;
volatile int g_AES_done;

/* Implementation that should never be optimized out by the compiler */
static void trng_zeroize( void *v, size_t n ) {
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
}

void CRYPTO_IRQHandler()
{
if (PRNG_GET_INT_FLAG()) {
Expand Down Expand Up @@ -77,21 +85,22 @@ void trng_free(trng_t *obj)
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
{
(void)obj;

*output_length = 0;
if (length < 32) {
unsigned char tmpBuff[32];
unsigned char tmpBuff[PRNG_KEY_SIZE];
size_t cur_length = 0;

while (length >= sizeof(tmpBuff)) {
trng_get(output);
output += sizeof(tmpBuff);
cur_length += sizeof(tmpBuff);
length -= sizeof(tmpBuff);
}
if (length > 0) {
trng_get(tmpBuff);
memcpy(output, &tmpBuff, length);
*output_length = length;
} else {
for (unsigned i = 0; i < (length/32); i++) {
trng_get(output);
*output_length += 32;
output += 32;
}
memcpy(output, tmpBuff, length);
cur_length += length;
trng_zeroize(tmpBuff, sizeof(tmpBuff));
}

*output_length = cur_length;
return 0;
}

Expand Down
35 changes: 22 additions & 13 deletions targets/TARGET_NUVOTON/TARGET_NUC472/trng_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,17 @@
/*
* Get Random number generator.
*/

#define PRNG_KEY_SIZE (0x20UL)

static volatile int g_PRNG_done;
volatile int g_AES_done;

/* Implementation that should never be optimized out by the compiler */
static void trng_zeroize( void *v, size_t n ) {
volatile unsigned char *p = (unsigned char*)v; while( n-- ) *p++ = 0;
}

void CRYPTO_IRQHandler()
{
if (PRNG_GET_INT_FLAG()) {
Expand Down Expand Up @@ -82,21 +90,22 @@ void trng_free(trng_t *obj)
int trng_get_bytes(trng_t *obj, uint8_t *output, size_t length, size_t *output_length)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

trng_get_bytes looks pretty similar between TARGET_NUC472 and TARGET_M480. Would it be prudent to move it to a location shared by both TARGET_NUC472 and TARGET_M480?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Patater We will consider to be shared as while one more platform utilize the same HW IP.

{
(void)obj;

*output_length = 0;
if (length < 32) {
unsigned char tmpBuff[32];
unsigned char tmpBuff[PRNG_KEY_SIZE];
size_t cur_length = 0;

while (length >= sizeof(tmpBuff)) {
trng_get(output);
output += sizeof(tmpBuff);
cur_length += sizeof(tmpBuff);
length -= sizeof(tmpBuff);
}
if (length > 0) {
trng_get(tmpBuff);
memcpy(output, &tmpBuff, length);
*output_length = length;
} else {
for (int i = 0; i < (length/32); i++) {
trng_get(output);
*output_length += 32;
output += 32;
}
memcpy(output, tmpBuff, length);
cur_length += length;
trng_zeroize(tmpBuff, sizeof(tmpBuff));
}

*output_length = cur_length;
return 0;
}

Expand Down