Repository navigation
Description
I need to write a 639-byte DER certificate into the data partition (NVM zone index 1) of the STSAFE-A110, which has 700 bytes available. I cannot write all 639 bytes in a single call because we configured our Platform Abstraction Layer to use a buffer of STSAFEA_MAX_FRAME_LENGTH_A110 + 3 (507 + 3 = 510) bytes for I2C communication. So I considered implementing a function that writes the certificate by sending it in chunks of up to STSAFEA_MAX_FRAME_LENGTH_A110 (507) bytes via multiple calls to stse_data_storage_update_data_zone().
The problem is that if I try to write a chunk of 507 bytes, STSE_SERVICE_FRAME_SIZE_ERROR is immediately returned because the following condition evaluates to true (507 >= 507). I’m wondering why >= is used instead of simply >, is that a BUG?:
STSELib/services/stsafea/stsafea_data_partition.c
Lines 381 to 383 in 7b34b7c
| if (data_length >= stsafea_maximum_frame_length[pSTSE->device_type]) { | |
| return STSE_SERVICE_FRAME_SIZE_ERROR; | |
| } |
In any case, I reduced the maximum chunk size to STSAFEA_MAX_FRAME_LENGTH_A110 - 1 (506), but then the failure occurs later due to stse_platform_i2c_send_start(), which returns STSE_PLATFORM_BUFFER_ERR, because frame_length is 513 bytes:
stse_ReturnCode_t stse_platform_i2c_send_start(PLAT_UI8 bus_id, PLAT_UI8 dev_addr, PLAT_UI16 speed,
PLAT_UI16 frame_length)
{
/* Check buffer overflow */
if (esc_stse_i2c[bus_id].tx_rx_buffer_length < frame_length) // 510 < 513
{
return STSE_PLATFORM_BUFFER_ERR;
}
esc_stse_i2c[bus_id].tx_rx_frame_length = frame_length;
esc_stse_i2c[bus_id].tx_rx_frame_offset = 0;
return STSE_OK;
}Note that esc_stse_i2c[bus_id].tx_rx_buffer_length is set by us to STSAFEA_MAX_FRAME_LENGTH_A110 + 3 (507 + 3 = 510).
I would have expected the frame_length argument to be 509 (506 + 3), since here (#53 (comment)) @TofMassilia13320 mentioned that setting the I2C platform buffer to the maximum value STSAFEA_MAX_FRAME_LENGTH_Axxx + 3 bytes ensures full command coverage of the selected STSAFE-A without overflow at the I2C platform layer.
This led me to assume that STSELib would invoke stse_platform_i2c_send_start() with a frame_length argument that could reach at most STSAFEA_MAX_FRAME_LENGTH_Axxx + 3 (510 in my case).
In other words, I understood that STSELib adds at most 3 bytes to the user data (2 bytes for the response length + 1 byte for the command or response header), as mentioned by @nils-cercariolo-st here: #53 (comment). However, this does not seem to hold true, as in my case frame_length is actually 513.
Digging into STSELib, I concluded that the extra 7 bytes are added by stsafea_update_data_zone() and stsafea_frame_transmit(). Specifically:
-
stsafea_update_data_zone()adds 5 bytes at the beginning of the I2C frame: 1 byte header, 1 byte zone access option, 1 byte zone index, and 2 bytes offset:
STSELib/services/stsafea/stsafea_data_partition.c
Lines 374 to 379 in 7b34b7c
stse_frame_allocate(CmdFrame); stse_frame_element_allocate_push(&CmdFrame, eCmdHeader, STSAFEA_HEADER_SIZE, &cmd_header); stse_frame_element_allocate_push(&CmdFrame, eOption, STSAFEA_ZONE_ACCESS_OPTION_SIZE, (PLAT_UI8 *)&option); stse_frame_element_allocate_push(&CmdFrame, eZoneIndex, STSAFEA_ZONE_INDEX_SIZE, (PLAT_UI8 *)&zone_index); stse_frame_element_allocate_push(&CmdFrame, eOffset, STSAFEA_ZONE_OFFSET_SIZE, (PLAT_UI8 *)&offset); stse_frame_element_allocate_push(&CmdFrame, eData, data_length, pData); -
stsafea_frame_transmit()adds 2 bytes of CRC at the end of the I2C frame:
STSELib/services/stsafea/stsafea_frame_transfer.c
Lines 63 to 64 in 7b34b7c
stse_frame_element_allocate(crc_element, STSE_FRAME_CRC_SIZE, crc); stse_frame_push_element(pFrame, &crc_element);
So the problem is: how can I determine the maximum length of the buffer pBuffer that I am allowed to pass to stse_data_storage_update_data_zone()?
STSELib/api/stse_data_storage.h
Lines 88 to 112 in 7b34b7c
| /*! | |
| * \brief Update one memory zone of the STSE device | |
| * \param[in] pSTSE Pointer to target STSE handler | |
| * \param[in] zone Target STSE zone index | |
| * \param[in] offset Update offset | |
| * \param[in] pBuffer Pointer to applicative read buffer | |
| * \param[in] length Update length in byte | |
| * \param[in] atomicity \ref stse_zone_update_atomicity_t atomicity of the update access | |
| * \param[in] protection \ref stse_cmd_protection_t command response protection indicator | |
| * \return \ref STSE_OK on success ; \ref stse_ReturnCode_t error code otherwise | |
| * \note - A target STSE handler must be initialized using the \ref stse_init routine prior to execute this API function | |
| * \note - If command response protection is required an active session between Host/Companion and STSE must be open | |
| * \note - The device may enforce a monotonic policy on zone's access condition (stse_zone_ac_t enum): | |
| * Once set to a more restrictive condition (e.g. STSE_AC_ALWAYS -> STSE_AC_HOST -> STSE_AC_AUTH_AND_HOST), | |
| * it's not possible to revert to a less restrictive one (e.g. STSE_AC_HOST -> STSE_AC_ALWAYS). | |
| * \details \include{doc} stse_data_storage_update_zone.dox | |
| */ | |
| stse_ReturnCode_t stse_data_storage_update_data_zone( | |
| stse_Handler_t *pSTSE, | |
| PLAT_UI32 zone, | |
| PLAT_UI16 offset, | |
| PLAT_UI8 *pBuffer, | |
| PLAT_UI16 length, | |
| stse_zone_update_atomicity_t atomicity, | |
| stse_cmd_protection_t protection); |
Activity
[-]Max chunk size for `stse_data_storage_update_data_zone()` unclear due to hidden overhead bytes[/-][+]Max `pBuffer` size for `stse_data_storage_update_data_zone()` unclear due to hidden overhead bytes[/+]TofMassilia13320 commentedon Apr 29, 2026
The preprocessing flag #define STSAFEA_MAX_FRAME_LENGTH_xyyy corresponds to the maximum length of all bytes that make up a command (including Header + Length + payload + CRC).
I agree It’s quite complicated to determine the maximum payload length for a customer in particular for certain services where the payload (input parameters) varies in length/size in bytes.
We’ll identify these services and add a note in the Doxygen description indicating the maximum size of these payload for all STSAFE-A & L products.
If you need support, I can put you in touch with our local field application engineer.
parmi93 commentedon Apr 29, 2026
What do you think about adding new APIs for these specific cases where the frame length depends on the user payload?
For example, you could add an API like
stse_data_storage_get_update_max_data_len()instse_data_storage.c.Of course, this solution would also require the user, in the Platform Abstraction Layer, to define porting functions such as
stse_platform_i2c_frame_len(PLAT_UI8 bus_id)andstse_platform_st1wire_frame_len(PLAT_UI8 bus_id).What do you think?
(Currently, we have implemented a workaround similar to the one shown in this example.)
TofMassilia13320 commentedon May 2, 2026
The STSELib services format commands to facilitate the integration of STSAFE-A or L into applications.
User Manual (UM) documentation is available; your local Field Application Engineer (FAE) can provide it or assist you.
Binary code size is a critical factor in the embedded domain. The goal is therefore not to increase the binary size for checks performed by the hardware product itself.
I agree that users (without documentation) must analyze the code for services with variable-sized parameters (such as a payload buffer) in order to construct a complete command whose size remains below the maximum limit. This is why we include details in the Doxygen documentation for certain specific services, to clarify the sizing of variable parameters (such as data partitioning services in your case).
parmi93 commentedon May 29, 2026
Hi, thanks for the detailed report!
Regarding the concern about code size for adding a helper function like
stse_data_storage_get_update_max_data_len(): I don't think this should be an issue. In C, if a function is never called, the linker will simply discard it from the final binary (assuming the usual--gc-sections/-ffunction-sectionsflags are used, which is standard practice in embedded projects). So users who don't need it won't pay any cost for it.That said, I think the ideal solution would actually combine both approaches:
A helper function
stse_data_storage_get_update_max_data_len()that computes the maximum allowedpBuffersize at runtime given the platform buffer size, so users can determine the correct chunk size programmatically without having to dig into the internals of the library.Doxygen documentation on
stse_data_storage_update_data_zone()clearly explaining the overhead bytes (5 bytes from the command frame + 2 bytes CRC = 7 bytes total), so that users who prefer to compute the maximum size statically have all the information they need directly in the API documentation.This way both use cases are covered: those who want a programmatic solution and those who just need clear documentation to do the math themselves.