Skip to content

RZ_A1LU: Fix TRNG function #5970

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 1 commit into from
Feb 8, 2018
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
#include "platform/mbed_wait_api.h"

#define ESP32_I2C_ADDR (0x28<<1)
#define RETRY_CNT_MAX (20)

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

extern "C" void trng_init_esp32(void)
{
Expand Down Expand Up @@ -59,27 +65,35 @@ extern "C" int trng_get_bytes_esp32(uint8_t *output, size_t length, size_t *outp
char recv_data[4];
size_t idx = 0;
int i;
int err_cnt = 0;
int retry_cnt = 0;

if ((output == NULL) || (output_length == NULL)) {
return -1;
}

while (idx < length) {
while ((retry_cnt < RETRY_CNT_MAX) && (idx < length)) {
send_data[0] = 0;
ret = mI2c.write(ESP32_I2C_ADDR, send_data, 1);
ret = mI2c.write(ESP32_I2C_ADDR, send_data, sizeof(send_data));
if (ret == 0) {
mI2c.read(ESP32_I2C_ADDR, recv_data, sizeof(recv_data));
for (i = 0; (i < 4) && (idx < length); i++) {
output[idx++] = recv_data[i];
}
} else {
err_cnt++;
if (err_cnt >= 20) {
break;
ret = mI2c.read(ESP32_I2C_ADDR, recv_data, sizeof(recv_data));
if (ret == 0) {
for (i = 0; (i < sizeof(recv_data)) && (idx < length); i++) {
output[idx++] = recv_data[i];
}
}
}
if (ret != 0) {
retry_cnt++;
wait_ms(100);
}
}
if (output_length != NULL) {
*output_length = idx;
if (retry_cnt >= RETRY_CNT_MAX) {
idx = 0;
mbedtls_zeroize(output, length);
}
*output_length = idx;

mbedtls_zeroize(recv_data, sizeof(recv_data));

return (idx != 0 ? 0 : -1);
}
Expand Down