Skip to content

Commit

Permalink
drivers: entropy: add support for Microchip ATECC608
Browse files Browse the repository at this point in the history
Add entropy support for Microchip ATECC608
and ATECC508.

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
  • Loading branch information
maass-hamburg committed Mar 20, 2024
1 parent e0fff6d commit ea5f4b5
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions drivers/entropy/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ zephyr_syscall_header(${ZEPHYR_BASE}/include/zephyr/drivers/entropy.h)

zephyr_library()

zephyr_library_sources_ifdef(CONFIG_ENTROPY_ATECCX08 entropy_ateccx08.c)
zephyr_library_sources_ifdef(CONFIG_ENTROPY_TELINK_B91_TRNG entropy_b91_trng.c)
zephyr_library_sources_ifdef(CONFIG_ENTROPY_CC13XX_CC26XX_RNG entropy_cc13xx_cc26xx.c)
zephyr_library_sources_ifdef(CONFIG_ENTROPY_ESP32_RNG entropy_esp32.c)
Expand Down
1 change: 1 addition & 0 deletions drivers/entropy/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ config ENTROPY_INIT_PRIORITY
help
Entropy driver device initialization priority.

source "drivers/entropy/Kconfig.ateccx08"
source "drivers/entropy/Kconfig.b91"
source "drivers/entropy/Kconfig.cc13xx_cc26xx"
source "drivers/entropy/Kconfig.mcux"
Expand Down
13 changes: 13 additions & 0 deletions drivers/entropy/Kconfig.ateccx08
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Microchip ATECCx08 entropy generator driver configuration

# Copyright (c) 2024 Vogl Electronic GmbH
# SPDX-License-Identifier: Apache-2.0

config ENTROPY_ATECCX08
bool "Microchip ATECCx08 RNG driver"
default y
depends on DT_HAS_MICROCHIP_ATECCX08_ENTROPY_ENABLED
depends on EEPROM_ATECCX08
select ENTROPY_HAS_DRIVER
help
This option enables the Microchip ATECCx08 RNG driver.
75 changes: 75 additions & 0 deletions drivers/entropy/entropy_ateccx08.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (c) 2024 Vogl Electronic GmbH
*
* SPDX-License-Identifier: Apache-2.0
*/

#define DT_DRV_COMPAT microchip_ateccx08_entropy

#include <errno.h>
#include <string.h>
#include <zephyr/device.h>
#include <zephyr/drivers/entropy.h>
#include <zephyr/drivers/eeprom/ateccx08.h>
#include <zephyr/init.h>
#include <zephyr/kernel.h>

#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(entropy_ateccx08, CONFIG_ENTROPY_LOG_LEVEL);

struct entropy_ateccx08_config {
const struct device *parent;
};

static int entropy_ateccx08_get_entropy(const struct device *dev, uint8_t *buffer, uint16_t length)
{
const struct entropy_ateccx08_config *config = dev->config;
const static uint8_t not_random[] = {0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00};

Check warning on line 27 in drivers/entropy/entropy_ateccx08.c

View workflow job for this annotation

GitHub Actions / Run compliance checks on patch series (PR)

LINE_SPACING

drivers/entropy/entropy_ateccx08.c:27 Missing a blank line after declarations

int ret = 0;

while (length > 0) {
size_t to_copy;
uint8_t random_var[32] = {0};

ret = atecc_calib_random(config->parent, random_var);
if (ret < 0) {
return ret;
}

if (memcmp(random_var, not_random, sizeof(not_random)) == 0) {
LOG_ERR("Config not locked, no random data available.");
return -EIO;
}

to_copy = MIN(length, sizeof(random_var));

memcpy(buffer, random_var, to_copy);
buffer += to_copy;
length -= to_copy;
}
return ret;
}

static int entropy_ateccx08_init(const struct device *dev)
{
const struct entropy_ateccx08_config *config = dev->config;

if (!device_is_ready(config->parent)) {
return -ENODEV;
}

return 0;
}

static const struct entropy_driver_api entropy_ateccx08_api = {
.get_entropy = entropy_ateccx08_get_entropy};

#define DEFINE_ATECCX08_ENTROPY(_num) \
static const struct entropy_ateccx08_config entropy_ateccx08_config##_num = { \
.parent = DEVICE_DT_GET(DT_INST_BUS(_num))}; \
DEVICE_DT_INST_DEFINE(_num, entropy_ateccx08_init, NULL, NULL, \
&entropy_ateccx08_config##_num, POST_KERNEL, \
CONFIG_EEPROM_INIT_PRIORITY, &entropy_ateccx08_api);

DT_INST_FOREACH_STATUS_OKAY(DEFINE_ATECCX08_ENTROPY);
2 changes: 2 additions & 0 deletions dts/bindings/mtd/microchip,ateccx08-base.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

include: ["eeprom-base.yaml", i2c-device.yaml]

bus: ateccx08

properties:
size:
required: true
10 changes: 10 additions & 0 deletions dts/bindings/rng/microchip,ateccx08-entropy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Copyright (c) 2024 Vogl Electronic GmbH
# SPDX-License-Identifier: Apache-2.0

description: Microchip ATECCx08 entropy binding

compatible: "microchip,ateccx08-entropy"

include: base.yaml

on-bus: ateccx08

0 comments on commit ea5f4b5

Please sign in to comment.