1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
19465: drivers/mtd: use XFA for pointers to defined MTDs r=benpicco a=gschorcht

### Contribution description

This PR provides the support to hold pointers to defined MTDs within a XFA. The XFA allows
- to access MTDs of different types (`mtd_flashpage`, `mtd_sdcard`, `mtd_emulated`, ...) by an index
- to determine the number of MTDs defined in the system.

### Testing procedure

To be defined once PR #19443 is merged because emulated MTDs will allow to test this PR on arbitrary boards.

### Porting Guide

For external boards:
 - remove the `MTD_NUMOF` definition from `board.h`
 - add `MTD_XFA_ADD(<mtd_dev>, <idx>);` to the definition of `<mtd_dev>`.
 - `MTD_0`, `MTD_1`, … defines are no longer needed.

### Issues/PRs references

 Related to PR #19443

19981: Fletcher32: Add incremental API r=benpicco a=bergzand

### Contribution description

This PR extends the current fletcher32 checksum with an incremental API mode. This way the bytes to be checksummed can be supplied via multiple successive calls and do not have to be provided in a single consecutive buffer.

I've also rephrased the warning with the original function a bit as that function uses an `unaligned_get_u16` to access the data. The data thus does not require alignment, but the length does need to be supplied as number of 16 bit words.

### Testing procedure

The test has been extended


### Issues/PRs references

None

19995: sys/psa_crypto: Fix macro for public key max size and SE example r=benpicco a=Einhornhool

### Contribution description
#### 1. Wrong public key size when using secure elements, introduced by  #19954
Fixed conditions for key size macros in `crypto_sizes.h`.

#### 2. EdDSA and ECDSA examples fail when using a secure element because of unsopported changes introduced by #19954
Updated `example/psa_crypto` to use only supported functions for secure elements.

### Testing procedure
Build `example/psa_crypto` for secure elements and run application

Output on master:
```
2023-10-19 14:33:24,372 # main(): This is RIOT! (Version: 2019.07-devel-22378-gb6772)
2023-10-19 14:33:24,372 # HMAC SHA256 took 56393 us
2023-10-19 14:33:24,372 # Cipher AES 128 took 68826 us
2023-10-19 14:33:24,372 # *** RIOT kernel panic:
2023-10-19 14:33:24,373 # HARD FAULT HANDLER
2023-10-19 14:33:24,373 # 
2023-10-19 14:33:24,373 # *** rebooting...

```
Output with fixes:
```
2023-10-19 13:35:24,715 # main(): This is RIOT! (Version: 2019.07-devel-22384-g8ef66-dev/psa-crypto-fixes)
2023-10-19 13:35:24,715 # HMAC SHA256 took 56374 us
2023-10-19 13:35:24,715 # Cipher AES 128 took 68805 us
2023-10-19 13:35:24,715 # ECDSA took 281164 us
2023-10-19 13:35:24,715 # All Done
```


Co-authored-by: Gunar Schorcht <gunar@schorcht.net>
Co-authored-by: Koen Zandberg <koen@bergzand.net>
Co-authored-by: Lena Boeckmann <lena.boeckmann@haw-hamburg.de>
This commit is contained in:
bors[bot] 2023-10-19 19:01:12 +00:00 committed by GitHub
commit 03d3874e51
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
35 changed files with 221 additions and 125 deletions

View File

@ -59,7 +59,7 @@ static mtd_spi_nor_t samd51_nor_dev = {
.params = &_samd51_nor_params,
};
mtd_dev_t *mtd0 = (mtd_dev_t *)&samd51_nor_dev;
MTD_XFA_ADD(samd51_nor_dev, 0);
#ifdef MODULE_VFS_DEFAULT
VFS_AUTO_MOUNT(littlefs2, VFS_MTD(samd51_nor_dev), VFS_DEFAULT_NVM(0), 0);

View File

@ -54,7 +54,7 @@ static mtd_spi_nor_t samd51_nor_dev = {
.params = &_samd51_nor_params,
};
mtd_dev_t *mtd0 = (mtd_dev_t *)&samd51_nor_dev;
MTD_XFA_ADD(samd51_nor_dev, 0);
#ifdef MODULE_VFS_DEFAULT
VFS_AUTO_MOUNT(littlefs2, VFS_MTD(samd51_nor_dev), VFS_DEFAULT_NVM(0), 0);

View File

@ -54,7 +54,7 @@ static mtd_spi_nor_t samd51_nor_dev = {
.params = &_samd51_nor_params,
};
mtd_dev_t *mtd0 = (mtd_dev_t *)&samd51_nor_dev;
MTD_XFA_ADD(samd51_nor_dev, 0);
#endif /* MODULE_MTD */
void board_init(void)

View File

@ -51,7 +51,7 @@ static mtd_spi_nor_t weact_nor_dev = {
.params = &_weact_nor_params,
};
mtd_dev_t *mtd0 = (mtd_dev_t *)&weact_nor_dev;
MTD_XFA_ADD(weact_nor_dev, 0);
#ifdef MODULE_VFS_DEFAULT
#include "vfs_default.h"

View File

@ -50,7 +50,7 @@ static mtd_spi_nor_t ikea_tradfri_nor_dev = {
.params = &_ikea_tradfri_nor_params,
};
mtd_dev_t *mtd0 = (mtd_dev_t *)&ikea_tradfri_nor_dev;
MTD_XFA_ADD(ikea_tradfri_nor_dev, 0);
#endif /* MODULE_MTD */
void board_init(void)

View File

@ -51,7 +51,7 @@ static mtd_spi_nor_t mtd_nor_dev = {
.params = &_mtd_nor_params,
};
mtd_dev_t *mtd0 = (mtd_dev_t *)&mtd_nor_dev;
MTD_XFA_ADD(mtd_nor_dev, 0);
#ifdef MODULE_VFS_DEFAULT
#include "vfs_default.h"

View File

@ -28,7 +28,7 @@
#ifdef MODULE_MTD_MCI
extern const mtd_desc_t mtd_mci_driver;
static mtd_dev_t _mtd_mci = { .driver = &mtd_mci_driver };
mtd_dev_t *mtd0 = &_mtd_mci;
MTD_XFA_ADD(_mtd_mci, 0);
#endif
#ifdef MODULE_VFS_DEFAULT

View File

@ -31,7 +31,7 @@
#ifdef MODULE_MTD_MCI
extern const mtd_desc_t mtd_mci_driver;
static mtd_dev_t _mtd_mci = { .driver = &mtd_mci_driver };
mtd_dev_t *mtd0 = &_mtd_mci;
MTD_XFA_ADD(_mtd_mci, 0);
#endif
#ifdef MODULE_VFS_DEFAULT

View File

@ -73,7 +73,7 @@ static mtd_spi_nor_t mulle_nor_dev = {
.params = &mulle_nor_params,
};
mtd_dev_t *mtd0 = (mtd_dev_t *)&mulle_nor_dev;
MTD_XFA_ADD(mulle_nor_dev, 0);
static devfs_t mulle_nor_devfs = {
.path = "/mtd0",

View File

@ -32,7 +32,7 @@ mtd_native_dev_t mtd0_dev = {
.fname = MTD_NATIVE_FILENAME,
};
mtd_dev_t *mtd0 = &mtd0_dev.base;
MTD_XFA_ADD(mtd0_dev.base, 0);
#endif
#ifdef MODULE_VFS_DEFAULT

View File

@ -52,7 +52,7 @@ static mtd_spi_nor_t nrf52840dk_nor_dev = {
.params = &_nrf52840dk_nor_params,
};
mtd_dev_t *mtd0 = (mtd_dev_t *)&nrf52840dk_nor_dev;
MTD_XFA_ADD(nrf52840dk_nor_dev, 0);
#ifdef MODULE_VFS_DEFAULT
#include "vfs_default.h"

View File

@ -50,7 +50,7 @@ static mtd_spi_nor_t nrf5340_nor_dev = {
},
.params = &_nrf5340_nor_params,
};
mtd_dev_t *mtd0 = (mtd_dev_t *)&nrf5340_nor_dev;
MTD_XFA_ADD(nrf5340_nor_dev, 0);
#ifdef MODULE_VFS_DEFAULT
VFS_AUTO_MOUNT(littlefs2, VFS_MTD(nrf5340_nor_dev), VFS_DEFAULT_NVM(0), 0);

View File

@ -102,7 +102,6 @@ extern "C" {
*/
extern mtd_dev_t *mtd0;
#define MTD_0 mtd0
#define MTD_NUMOF 1
#define BOARD_QSPI_PIN_CS GPIO_PIN(0, 18) /**< SPI Flash Chip Select */
#define BOARD_QSPI_PIN_WP GPIO_PIN(0, 15) /**< SPI Flash Write Protect */

View File

@ -55,7 +55,7 @@ static mtd_spi_nor_t pinetime_nor_dev = {
.params = &_pinetime_nor_params,
};
mtd_dev_t *mtd0 = (mtd_dev_t *)&pinetime_nor_dev;
MTD_XFA_ADD(pinetime_nor_dev, 0);
#endif /* MODULE_MTD */
void board_init(void)

View File

@ -55,5 +55,5 @@ static mtd_spi_nor_t mtd_nor_dev = {
.params = &_mtd_nor_params,
};
mtd_dev_t *mtd0 = (mtd_dev_t *)&mtd_nor_dev;
MTD_XFA_ADD(mtd_nor_dev, 0);
#endif /* MODULE_MTD */

View File

@ -51,7 +51,7 @@ static mtd_spi_nor_t same54_nor_dev = {
},
.params = &_same54_nor_params,
};
mtd_dev_t *mtd0 = (mtd_dev_t *)&same54_nor_dev;
MTD_XFA_ADD(same54_nor_dev, 0);
#ifdef MODULE_VFS_DEFAULT
VFS_AUTO_MOUNT(littlefs2, VFS_MTD(same54_nor_dev), VFS_DEFAULT_NVM(0), 0);
@ -69,7 +69,8 @@ static mtd_at24cxxx_t at24mac_dev = {
.at24cxxx_eeprom = &at24cxxx_dev,
.params = at24cxxx_params,
};
mtd_dev_t *mtd1 = (mtd_dev_t *)&at24mac_dev;
MTD_XFA_ADD(at24mac_dev, 1);
#endif /* MODULE_MTD_AT24CXXX */
#ifdef MODULE_SAM0_SDHC
@ -84,7 +85,7 @@ static mtd_sam0_sdhc_t sdhc_dev = {
.wp = GPIO_PIN(PD, 21),
},
};
mtd_dev_t *mtd2 = (mtd_dev_t *)&sdhc_dev;
MTD_XFA_ADD(sdhc_dev, 2);
#ifdef MODULE_VFS_DEFAULT
/* default to FAT */

View File

@ -75,7 +75,6 @@ extern mtd_dev_t *mtd0, *mtd1, *mtd2;
#define MTD_0 mtd0
#define MTD_1 mtd1
#define MTD_2 mtd2
#define MTD_NUMOF 3
#define CONFIG_SDMMC_GENERIC_MTD_OFFSET 2 /**< mtd2 is used for SD Card */
/** @} */

View File

@ -57,7 +57,7 @@ static mtd_spi_nor_t _nor_dev = {
},
.params = &_mtd_nor_params,
};
mtd_dev_t *mtd0 = (mtd_dev_t *)&_nor_dev;
MTD_XFA_ADD(_nor_dev, 0);
#ifdef MODULE_VFS_DEFAULT
#include "vfs_default.h"

View File

@ -53,5 +53,5 @@ static mtd_spi_nor_t serpente_nor_dev = {
.params = &_serpente_nor_params,
};
mtd_dev_t *mtd0 = (mtd_dev_t *)&serpente_nor_dev;
MTD_XFA_ADD(serpente_nor_dev, 0);
#endif /* MODULE_MTD */

View File

@ -57,12 +57,12 @@
#define ESP_PART_ENTRY_SIZE 0x20
#define ESP_PART_ENTRY_MAGIC ESP_PARTITION_MAGIC
/* the external pointer to the system MTD device */
mtd_dev_t* mtd0 = 0;
static mtd_dev_t _flash_dev;
static mtd_desc_t _flash_driver;
/* the external pointer to the system MTD device */
MTD_XFA_ADD(_flash_dev, 0);
#ifdef MODULE_VFS_DEFAULT
#include "vfs_default.h"
VFS_AUTO_MOUNT(littlefs2, { .dev = &_flash_dev }, VFS_DEFAULT_NVM(0), 0);
@ -205,8 +205,6 @@ void spi_flash_drive_init(void)
_flash_dev.driver = &_flash_driver;
_flash_dev.sector_count = _flash_size / _flashchip->sector_size;
mtd0 = &_flash_dev;
_flash_dev.pages_per_sector = _flashchip->sector_size / _flashchip->page_size;
_flash_dev.page_size = _flashchip->page_size;
/* Emulation for smaller / unaligned writes is present, but at reduced

View File

@ -75,6 +75,8 @@
#include <stdint.h>
#include "xfa.h"
#ifdef __cplusplus
extern "C" {
#endif
@ -116,6 +118,56 @@ typedef struct {
#endif
} mtd_dev_t;
/**
* @brief MTD device array as XFA
*
* The array contains the addresses of all MTD devices that are defined using
* the @ref MTD_XFA_ADD macro, for example:
* ```
* MTD_XFA_ADD(my_dev, 0);
* ```
* The MTD devices in this array can be used for automatic functions such as
* with the `mtd_default` module. The i-th device in this array can then be
* accessed with `mtd_dev_xfa[i]`. The number of MTDs defined in this array
* is `XFA_LEN(mtd_dev_xfa)`.
*/
#if !DOXYGEN
XFA_USE_CONST(mtd_dev_t *, mtd_dev_xfa);
#else
mtd_dev_t * const mtd_dev_xfa[];
#endif
/**
* @brief Define MTD device pointer variable `mtd<idx>`
*
* The macro defines the MTD device pointer variable `mtd<idx>`, sets it to
* the address of the MTD device specified by the @p dev parameter, and adds
* it to the XFA of MTD device pointers @ref mtd_dev_xfa. For example
* ```
* MTD_XFA_ADD(my_dev, 1);
* ```
* defines the variable `mtd1` pointing to the device `my_dev`.
*
* The parameter @p idx is used as priority of the MTD device pointer within
* the XFA. That means it determines the order of the MTD device pointers
* within @ref mtd_dev_xfa.
*
* @note Only if each MTD device is added with a unique priority and only if the
* priorities start at 0 and are used in consecutive order, the parameter
* @p idx corresponds to the position of the MTD device pointer within
* the @ref mtd_dev_xfa XFA and `mtd_dev_xfa[i]` points to the i-th MTD
* device.
*
* @param dev MTD device
* @param idx Priority of the MTD device pointer within the XFA
*/
#define MTD_XFA_ADD(dev, idx) XFA_CONST(mtd_dev_xfa, 0) mtd_dev_t *mtd ## idx = (mtd_dev_t *)&(dev)
/**
* @brief Number of MTDs defined in the MTD device array in XFA
*/
#define MTD_NUMOF XFA_LEN(mtd_dev_t *, mtd_dev_xfa)
/**
* @brief MTD driver can write any data to the storage without erasing it first.
*

View File

@ -33,54 +33,28 @@ extern "C" {
#include "mtd_emulated.h"
#endif
#if !defined(MTD_NUMOF) && !DOXYGEN
#if defined(MTD_3)
#define MTD_BOARD_NUMOF 4
#elif defined(MTD_2)
#define MTD_BOARD_NUMOF 3
#elif defined(MTD_1)
#define MTD_BOARD_NUMOF 2
#elif defined(MTD_0)
#define MTD_BOARD_NUMOF 1
#else
#define MTD_BOARD_NUMOF 0
#endif
#define MTD_SDCARD_NUMOF IS_USED(MODULE_MTD_SDCARD_DEFAULT)
#define MTD_EMULATED_NUMOF IS_USED(MODULE_MTD_EMULATED)
/**
* @brief Number of MTD devices
*/
#define MTD_NUMOF (MTD_BOARD_NUMOF + MTD_SDCARD_NUMOF + MTD_EMULATED_NUMOF)
#else
#define MTD_BOARD_NUMOF MTD_NUMOF
#endif /* !defined(MTD_NUMOF) && !DOXYGEN */
#if !DOXYGEN
/**
* @brief Declare `mtd*` according to the number of MTD devices
* @brief Declare `mtd*` according to the `MTD_*` symbols defined by the board
*/
#if MTD_NUMOF > 0
extern mtd_dev_t *mtd0;
#ifdef MTD_0
extern mtd_dev_t *MTD_0;
#endif
#if MTD_NUMOF > 1
extern mtd_dev_t *mtd1;
#ifdef MTD_1
extern mtd_dev_t *MTD_1;
#endif
#if MTD_NUMOF > 2
extern mtd_dev_t *mtd2;
#ifdef MTD_2
extern mtd_dev_t *MTD_2;
#endif
#if MTD_NUMOF > 3
extern mtd_dev_t *mtd3;
#ifdef MTD_3
extern mtd_dev_t *MTD_3;
#endif
#if MTD_NUMOF > 4
extern mtd_dev_t *mtd4;
#ifdef MTD_4
extern mtd_dev_t *MTD_4;
#endif
#if MTD_NUMOF > 5
extern mtd_dev_t *mtd5;
#ifdef MTD_5
extern mtd_dev_t *MTD_5;
#endif
#endif /* !DOXYGEN */
@ -102,27 +76,7 @@ extern mtd_emulated_t mtd_emulated_dev0;
*/
static inline mtd_dev_t *mtd_default_get_dev(unsigned idx)
{
switch (idx) {
#if MTD_BOARD_NUMOF > 0
case 0: return MTD_0;
#endif
#if MTD_BOARD_NUMOF > 1
case 1: return MTD_1;
#endif
#if MTD_BOARD_NUMOF > 2
case 2: return MTD_2;
#endif
#if MTD_BOARD_NUMOF > 3
case 3: return MTD_3;
#endif
#if MTD_SDCARD_NUMOF > 0
case MTD_BOARD_NUMOF: return (mtd_dev_t *)&mtd_sdcard_dev0;
#endif
#if MTD_EMULATED_NUMOF > 0
case MTD_BOARD_NUMOF + MTD_SDCARD_NUMOF: return (mtd_dev_t *)&mtd_emulated_dev0;
#endif
}
return NULL;
return ((MTD_NUMOF != 0) && (idx < MTD_NUMOF)) ? mtd_dev_xfa[idx] : NULL;
}
#ifdef __cplusplus

View File

@ -38,7 +38,10 @@ extern "C" {
* creates the emulated MTD device `mtd_emulated_dev0` with 16 sectors, 4 pages
* per sector and a page size of 64 bytes. The write size is always 1 byte.
*
* @param n index of the emulated MTD (results into symbol `mtd_emulated_devn`)
* @note The emulated devices are added to the XFA of MTD device pointers
* @ref mtd_dev_xfa with priority 99 to place them at the end of the XFA.
*
* @param n index of the emulated MTD (results into symbol `mtd_emulated_dev<n>`)
* @param sc sectors of the emulated MTD
* @param pps pages per sector of the emulated MTD
* @param ps page size in bytes
@ -57,7 +60,9 @@ extern "C" {
.size = sc * pps * ps, \
.memory = _mtd_emulated_memory ## n, \
.init_done = false, \
} \
}; \
\
XFA_CONST(mtd_dev_xfa, 99) mtd_dev_t CONCAT(*mtd_emulated, n) = (mtd_dev_t *)&mtd_emulated_dev ## n
#if MODULE_VFS_AUTO_MOUNT || DOXYGEN
/**
@ -70,7 +75,7 @@ extern "C" {
* automatically mounts the emulated MTD `mtd_emulated_dev0` with FAT file
* system under mount point `/mtde0` with unique index 2.
*
* @param n index of the emulated MTD (symbol `mtd_emulated_devn`, mount point `/mtde0`)
* @param n index of the emulated MTD (symbol `mtd_emulated_dev<n>`, mount point `/mtde0`)
* @param m unique overall index of VFS mount point
* @param fs filesystem type used
*/

View File

@ -28,6 +28,10 @@
#include "bitarithm.h"
#include "mtd.h"
#include "xfa.h"
/* Automatic MTD handling */
XFA_INIT_CONST(mtd_dev_t *, mtd_dev_xfa);
static bool out_of_bounds(mtd_dev_t *mtd, uint32_t page, uint32_t offset, uint32_t len)
{

View File

@ -241,7 +241,7 @@ const mtd_desc_t mtd_sdcard_driver = {
.params = &sdcard_spi_params[n] \
}; \
\
mtd_dev_t CONCAT(*mtd, m) = (mtd_dev_t *)&mtd_sdcard_dev ## n
XFA_CONST(mtd_dev_xfa, m) mtd_dev_t CONCAT(*mtd, m) = (mtd_dev_t *)&mtd_sdcard_dev ## n
#define MTD_SDCARD_DEV_FS(n, m, filesystem) \
VFS_AUTO_MOUNT(filesystem, VFS_MTD(mtd_sdcard_dev ## n), VFS_DEFAULT_SD(n), m)

View File

@ -235,7 +235,7 @@ const mtd_desc_t mtd_sdmmc_driver = {
.sdmmc_idx = n, \
}; \
\
mtd_dev_t CONCAT(*mtd, m) = (mtd_dev_t *)&mtd_sdmmc_dev ##n
XFA_CONST(mtd_dev_xfa, m) mtd_dev_t CONCAT(*mtd, m) = (mtd_dev_t *)&mtd_sdmmc_dev ## n
#if IS_USED(MODULE_MTD_SDCARD_DEFAULT)
/* we use /sd1 as default mount point for coexistence with mtd_sdcard */

View File

@ -81,10 +81,13 @@ psa_status_t example_ecdsa_p256(void)
}
#ifdef SECURE_ELEMENT
/* Currently there is no support for message signature and verification on secure elements */
psa_set_key_lifetime(&pubkey_attr, lifetime);
psa_set_key_usage_flags(&pubkey_attr, PSA_KEY_USAGE_VERIFY_HASH);
#else
psa_set_key_usage_flags(&pubkey_attr, PSA_KEY_USAGE_VERIFY_MESSAGE);
#endif
psa_set_key_algorithm(&pubkey_attr, ECC_ALG);
psa_set_key_usage_flags(&pubkey_attr, PSA_KEY_USAGE_VERIFY_MESSAGE);
psa_set_key_bits(&pubkey_attr, PSA_BYTES_TO_BITS(pubkey_length));
psa_set_key_type(&pubkey_attr, PSA_KEY_TYPE_ECC_PUBLIC_KEY(PSA_ECC_FAMILY_SECP_R1));
@ -99,6 +102,12 @@ psa_status_t example_ecdsa_p256(void)
return status;
}
#ifdef SECURE_ELEMENT
/* Currently there is only support for hash signature and verification on secure elements,
so we can't verify the message, but only the hash */
return psa_verify_hash(pubkey_id, ECC_ALG, hash, sizeof(hash), signature, sig_length);
#endif
/* verify on original message with internal hashing operation */
return psa_verify_message(pubkey_id, ECC_ALG, msg, sizeof(msg), signature, sig_length);
}

View File

@ -24,7 +24,10 @@
extern psa_status_t example_cipher_aes_128(void);
extern psa_status_t example_hmac_sha256(void);
extern psa_status_t example_ecdsa_p256(void);
#ifndef SECURE_ELEMENT
extern psa_status_t example_eddsa(void);
#endif
#ifdef MULTIPLE_SE
extern psa_status_t example_cipher_aes_128_sec_se(void);
@ -61,12 +64,14 @@ int main(void)
printf("ECDSA failed: %s\n", psa_status_to_humanly_readable(status));
}
#ifndef SECURE_ELEMENT
start = ztimer_now(ZTIMER_USEC);
status = example_eddsa();
printf("EdDSA took %d us\n", (int)(ztimer_now(ZTIMER_USEC) - start));
if (status != PSA_SUCCESS) {
printf("EdDSA failed: %s\n", psa_status_to_humanly_readable(status));
}
#endif
#ifdef MULTIPLE_SE
puts("Running Examples with secondary SE:");

View File

@ -21,21 +21,43 @@
#include "unaligned.h"
#include "checksum/fletcher32.h"
uint32_t fletcher32(const uint16_t *data, size_t words)
static inline void _reduce(fletcher32_ctx_t *ctx)
{
uint32_t sum1 = 0xffff, sum2 = 0xffff;
ctx->sum1 = (ctx->sum1 & 0xffff) + (ctx->sum1 >> 16);
ctx->sum2 = (ctx->sum2 & 0xffff) + (ctx->sum2 >> 16);
}
void fletcher32_init(fletcher32_ctx_t *ctx)
{
ctx->sum1 = 0xffff;
ctx->sum2 = 0xffff;
}
uint32_t fletcher32_finish(fletcher32_ctx_t *ctx)
{
/* Second reduction step to reduce sums to 8 bits */
_reduce(ctx);
return (ctx->sum2 << 16) | ctx->sum1;
}
void fletcher32_update(fletcher32_ctx_t *ctx, const void *data, size_t words)
{
const uint16_t *u16_data = (const uint16_t*)data;
while (words) {
unsigned tlen = words > 359 ? 359 : words;
words -= tlen;
do {
sum2 += sum1 += unaligned_get_u16(data++);
ctx->sum1 += unaligned_get_u16(u16_data++);
ctx->sum2 += ctx->sum1;
} while (--tlen);
sum1 = (sum1 & 0xffff) + (sum1 >> 16);
sum2 = (sum2 & 0xffff) + (sum2 >> 16);
_reduce(ctx);
}
/* Second reduction step to reduce sums to 16 bits */
sum1 = (sum1 & 0xffff) + (sum1 >> 16);
sum2 = (sum2 & 0xffff) + (sum2 >> 16);
return (sum2 << 16) | sum1;
}
uint32_t fletcher32(const uint16_t *data, size_t words)
{
fletcher32_ctx_t ctx;
fletcher32_init(&ctx);
fletcher32_update(&ctx, data, words);
return fletcher32_finish(&ctx);
}

View File

@ -15,6 +15,7 @@
*
* @file
* @author Joakim Nohlgård <joakim.nohlgard@eistec.se>
* @author Koen Zandberg <koen@bergzand.net>
*/
#ifndef CHECKSUM_FLETCHER32_H
@ -27,6 +28,14 @@
extern "C" {
#endif
/**
* @brief Fletcher's 32 bit checksum context struct
*/
typedef struct {
uint32_t sum1; /**< First sum of the checksum */
uint32_t sum2; /**< Second sum of the checksum */
} fletcher32_ctx_t;
/**
* @brief Fletcher's 32 bit checksum
*
@ -34,8 +43,7 @@ extern "C" {
* http://en.wikipedia.org/w/index.php?title=Fletcher%27s_checksum&oldid=661273016#Optimizations
*
* @note the returned sum is never 0
* @note pay attention to alignment issues since this operates on an input
* buffer containing 16 bit words, not bytes.
* @note pay attention to the @p words parameter buffer containing 16 bit words, not bytes.
*
* @param buf input buffer to hash
* @param words length of buffer, in 16 bit words
@ -43,6 +51,37 @@ extern "C" {
*/
uint32_t fletcher32(const uint16_t *buf, size_t words);
/**
* @brief Initialize a fletcher32 context
*
* Multi-part version of @ref fletcher32.
*
* @param[in] ctx fletcher32 context to initialize
*/
void fletcher32_init(fletcher32_ctx_t *ctx);
/**
* @brief Incrementally update the fletcher32 context with new data. Can be an arbitrary amount of
* times with new data to checksum.
*
* @note @p words is the number of 16 bit words in the buffer
* @note @p data should contain an integer number of 16 bit words
*
* @param[in] ctx fletcher32 context
* @param[in] data Data to add to the context
* @param[in] words Length of the data in 16 bit words
*/
void fletcher32_update(fletcher32_ctx_t *ctx, const void *data, size_t words);
/**
* @brief Finalize the checksum operation and return the checksum
*
* @param[in] ctx fletcher32 context
*
* @return 32 bit sized hash in the interval [1..2^32]
*/
uint32_t fletcher32_finish(fletcher32_ctx_t *ctx);
#ifdef __cplusplus
}
#endif

View File

@ -965,7 +965,9 @@ extern "C" {
*
* See also @ref PSA_EXPORT_PUBLIC_KEY_OUTPUT_SIZE(@p key_type, @p key_bits).
*/
#if IS_USED(MODULE_PSA_ASYMMETRIC_ECC_P256R1) || IS_USED(MODULE_PSA_ASYMMETRIC_ECC_P192R1)
#if (IS_USED(MODULE_PSA_ASYMMETRIC_ECC_P256R1) || \
IS_USED(MODULE_PSA_ASYMMETRIC_ECC_P192R1) || \
IS_USED(MODULE_PSA_SECURE_ELEMENT_ATECCX08A_ECC_P256))
#define PSA_EXPORT_PUBLIC_KEY_MAX_SIZE \
(PSA_KEY_EXPORT_ECC_PUBLIC_KEY_MAX_SIZE(PSA_ECC_FAMILY_SECT_R1, PSA_MAX_PRIV_KEY_SIZE))
#else

View File

@ -50,17 +50,6 @@ extern "C" {
*/
#define USBUS_MSC_EP_OUT_REQUIRED_NUMOF 1
/**
* @brief USBUS MSC Number of exported MTD device through USB
*/
#ifndef USBUS_MSC_EXPORTED_NUMOF
#ifdef MTD_NUMOF
#define USBUS_MSC_EXPORTED_NUMOF MTD_NUMOF
#else
#define USBUS_MSC_EXPORTED_NUMOF 0
#endif
#endif /* USBUS_MSC_EXPORTED_NUMOF */
/**
* @brief USBUS MSC internal state machine enum
*/
@ -102,8 +91,7 @@ typedef struct usbus_msc_device {
uint16_t block_nb; /**< Number of block to transfer for READ and
WRITE operations */
uint16_t block_offset; /**< Internal offset for endpoint size chunk transfer */
usbus_msc_lun_t lun_dev[USBUS_MSC_EXPORTED_NUMOF]; /**< Array holding exported logical
unit descriptor */
usbus_msc_lun_t *lun_dev; /**< Array holding exported logical unit descriptor */
} usbus_msc_device_t;
/**

View File

@ -198,7 +198,7 @@ static unsigned _get_lun(usbus_msc_device_t *msc)
/* Count only registered MTD devices as USB LUN, (using usbus_msc_add_lun)
* not every MTD devices available on board */
for (int i = 0; i < USBUS_MSC_EXPORTED_NUMOF; i++) {
for (unsigned i = 0; i < MTD_NUMOF; i++) {
if (msc->lun_dev[i].mtd != NULL) {
count++;
}
@ -217,14 +217,14 @@ int usbus_msc_add_lun(usbus_t *usbus, mtd_dev_t *dev)
}
/* Check if MTD isn't already registered */
for (int i = 0; i < USBUS_MSC_EXPORTED_NUMOF; i++) {
for (unsigned i = 0; i < MTD_NUMOF; i++) {
if (dev == msc->lun_dev[i].mtd) {
return -EBUSY;
}
}
/* Store new MTD device in first slot available
Also re alloc internal buffer if needed */
for (int i = 0; i < USBUS_MSC_EXPORTED_NUMOF; i++) {
for (unsigned i = 0; i < MTD_NUMOF; i++) {
if (msc->lun_dev[i].mtd == NULL) {
int ret = mtd_init(dev);
if (ret != 0) {
@ -283,7 +283,7 @@ int usbus_msc_remove_lun(usbus_t *usbus, mtd_dev_t *dev)
usbus_msc_device_t *msc = _get_msc_handler(usbus);
/* Identify the LUN to unexport */
for (int i = 0; i < USBUS_MSC_EXPORTED_NUMOF; i++) {
for (unsigned i = 0; i < MTD_NUMOF; i++) {
if (dev == msc->lun_dev[i].mtd) {
/* Wait for any pending transaction to end */
@ -329,7 +329,11 @@ static void _init(usbus_t *usbus, usbus_handler_t *handler)
msc->block = 0;
msc->state = WAITING;
for (int i = 0; i < USBUS_MSC_EXPORTED_NUMOF; i++) {
/* allocate the array holding exported logical unit descriptors */
msc->lun_dev = malloc(sizeof(usbus_msc_lun_t) * MTD_NUMOF);
assert(msc->lun_dev);
for (unsigned i = 0; i < MTD_NUMOF; i++) {
msc->lun_dev[i].mtd = NULL;
msc->lun_dev[i].block_size = 0;
msc->lun_dev[i].block_nb = 0;
@ -369,7 +373,7 @@ static void _init(usbus_t *usbus, usbus_handler_t *handler)
/* Auto-configure all MTD devices */
if (CONFIG_USBUS_MSC_AUTO_MTD) {
for (int i = 0; i < USBUS_MSC_EXPORTED_NUMOF; i++) {
for (unsigned i = 0; i < MTD_NUMOF; i++) {
usbus_msc_add_lun(usbus, mtd_default_get_dev(i));
}
}

View File

@ -281,7 +281,7 @@ static int cmd_info(int argc, char **argv)
if (argc < 2) {
printf("mtd devices: %d\n", MTD_NUMOF);
for (int i = 0; i < MTD_NUMOF; ++i) {
for (unsigned i = 0; i < MTD_NUMOF; ++i) {
printf(" -=[ MTD_%d ]=-\n", i);
_print_info(mtd_default_get_dev(i));
}
@ -450,7 +450,7 @@ int main(void)
puts("no MTD device present on the board.");
}
for (int i = 0; i < MTD_NUMOF; ++i) {
for (unsigned i = 0; i < MTD_NUMOF; ++i) {
printf("init MTD_%d… ", i);
mtd_dev_t *dev = mtd_default_get_dev(i);

View File

@ -85,6 +85,20 @@ static void test_checksum_fletcher32_wrap_around(void)
sizeof(wrap_around_data) - 1, expect));
}
static void test_checksum_fletcher32_wrap_around_piecewise(void)
{
/* XXX: not verified with external implementation yet */
uint32_t expect = 0x5bac8c3d;
fletcher32_ctx_t ctx;
fletcher32_init(&ctx);
size_t full_len = sizeof(wrap_around_data) - 1;
size_t initial_len = full_len / 2;
fletcher32_update(&ctx, wrap_around_data, initial_len / 2);
fletcher32_update(&ctx, (wrap_around_data + initial_len), (full_len - initial_len) / 2);
uint32_t result = fletcher32_finish(&ctx);
TEST_ASSERT_EQUAL_INT(result, expect);
}
Test *tests_checksum_fletcher32_tests(void)
{
EMB_UNIT_TESTFIXTURES(fixtures) {
@ -92,6 +106,7 @@ Test *tests_checksum_fletcher32_tests(void)
new_TestFixture(test_checksum_fletcher32_0to1_undetected),
new_TestFixture(test_checksum_fletcher32_atof),
new_TestFixture(test_checksum_fletcher32_wrap_around),
new_TestFixture(test_checksum_fletcher32_wrap_around_piecewise),
};
EMB_UNIT_TESTCALLER(checksum_fletcher32_tests, NULL, NULL, fixtures);