1 /* Copyright (c) 2024 Nordic Semiconductor
2  * SPDX-License-Identifier: Apache-2.0
3  */
4 #include <zephyr/secure_storage/its/transform.h>
5 #include <zephyr/sys/__assert.h>
6 #include <string.h>
7 
secure_storage_its_transform_to_store(secure_storage_its_uid_t uid,size_t data_len,const void * data,secure_storage_packed_create_flags_t create_flags,uint8_t stored_data[static SECURE_STORAGE_ITS_TRANSFORM_MAX_STORED_DATA_SIZE],size_t * stored_data_len)8 psa_status_t secure_storage_its_transform_to_store(
9 		secure_storage_its_uid_t uid, size_t data_len, const void *data,
10 		secure_storage_packed_create_flags_t create_flags,
11 		uint8_t stored_data[static SECURE_STORAGE_ITS_TRANSFORM_MAX_STORED_DATA_SIZE],
12 		size_t *stored_data_len)
13 {
14 	*stored_data_len = data_len + sizeof(secure_storage_packed_create_flags_t);
15 	__ASSERT_NO_MSG(data_len <= CONFIG_SECURE_STORAGE_ITS_MAX_DATA_SIZE);
16 	__ASSERT_NO_MSG(*stored_data_len <= SECURE_STORAGE_ITS_TRANSFORM_MAX_STORED_DATA_SIZE);
17 	memcpy(stored_data, data, data_len);
18 	*(secure_storage_packed_create_flags_t *)(&stored_data[data_len]) = create_flags;
19 	return PSA_SUCCESS;
20 }
21 
secure_storage_its_transform_from_store(secure_storage_its_uid_t uid,size_t stored_data_len,const uint8_t stored_data[static SECURE_STORAGE_ITS_TRANSFORM_MAX_STORED_DATA_SIZE],size_t data_size,void * data,size_t * data_len,psa_storage_create_flags_t * create_flags)22 psa_status_t secure_storage_its_transform_from_store(
23 		secure_storage_its_uid_t uid, size_t stored_data_len,
24 		const uint8_t stored_data[static SECURE_STORAGE_ITS_TRANSFORM_MAX_STORED_DATA_SIZE],
25 		size_t data_size, void *data, size_t *data_len,
26 		psa_storage_create_flags_t *create_flags)
27 {
28 	*data_len = stored_data_len - sizeof(secure_storage_packed_create_flags_t);
29 	__ASSERT_NO_MSG(data_size >= *data_len);
30 	memcpy(data, stored_data, *data_len);
31 	*create_flags = *(secure_storage_packed_create_flags_t *)(&stored_data[*data_len]);
32 	return PSA_SUCCESS;
33 }
34