Skip to content

Commit 32bc912

Browse files
committed
Add entropy inject to spm
1 parent a16dcc8 commit 32bc912

File tree

9 files changed

+147
-20
lines changed

9 files changed

+147
-20
lines changed

components/TARGET_PSA/services/crypto/COMPONENT_PSA_SRV_IPC/psa_crypto_spm.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,30 @@ psa_status_t psa_generate_random( uint8_t *output,
917917
return( ( psa_status_t ) err_call );
918918
}
919919

920+
#if defined(MBEDTLS_ENTROPY_NV_SEED)
921+
/****************************************************************/
922+
/* PSA_ENTROPY_INJECT */
923+
/****************************************************************/
924+
925+
psa_status_t mbedtls_psa_inject_entropy( const unsigned char *seed,
926+
size_t seed_size )
927+
{
928+
psa_error_t err_call;
929+
psa_handle_t handle = PSA_NULL_HANDLE;
930+
psa_invec_t in_vec = { seed, seed_size };
931+
932+
handle = psa_connect( PSA_ENTROPY_ID, MINOR_VER );
933+
if( handle <= 0 )
934+
return ( PSA_ERROR_COMMUNICATION_FAILURE );
935+
936+
err_call = psa_call( handle, &in_vec, 1, NULL, 0 );
937+
psa_close( handle );
938+
if( err_call < 0 )
939+
err_call = ( psa_error_t ) PSA_ERROR_COMMUNICATION_FAILURE;
920940

941+
return( ( psa_status_t ) err_call );
942+
}
943+
#endif
921944
/****************************************************************/
922945
/* PSA Generator */
923946
/****************************************************************/

components/TARGET_PSA/services/crypto/COMPONENT_SPE/crypto_spe.h

100644100755
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ extern "C" {
5858
#define mbedtls_psa_crypto_free mbedtls_psa_sec_crypto_free
5959
#define psa_key_derivation psa_sec_key_derivation
6060
#define psa_generator_abort psa_sec_generator_abort
61+
#define mbedtls_psa_inject_entropy mbedtls_psa_sec_inject_entropy
6162

6263
#include "crypto.h"
6364

components/TARGET_PSA/services/crypto/COMPONENT_SPE/psa_crypto_partition.c

100644100755
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "crypto_spe.h"
99
#include "crypto_platform_spe.h"
1010
#include "psa_psa_f_partition.h"
11+
#include "mbedtls/entropy.h"
1112

1213
#if defined(MBEDTLS_PLATFORM_C)
1314
#include "mbedtls/platform.h"
@@ -1114,6 +1115,57 @@ static void psa_key_management_operation( void )
11141115
psa_reply( msg.handle, status );
11151116
}
11161117

1118+
#if defined(MBEDTLS_ENTROPY_NV_SEED)
1119+
static void psa_entropy_operation( void )
1120+
{
1121+
psa_msg_t msg = { 0 };
1122+
psa_status_t status = PSA_SUCCESS;
1123+
psa_get( PSA_ENTROPY_INJECT, &msg );
1124+
1125+
switch ( msg.type )
1126+
{
1127+
case PSA_IPC_CONNECT:
1128+
{
1129+
break; /* do nothing */
1130+
}
1131+
case PSA_IPC_CALL:
1132+
{
1133+
uint32_t bytes_read;
1134+
size_t seed_size = msg.in_size[0];
1135+
if( MBEDTLS_ENTROPY_MAX_SEED_SIZE < seed_size )
1136+
{
1137+
status = PSA_ERROR_INVALID_ARGUMENT;
1138+
break;
1139+
}
1140+
unsigned char *seed = mbedtls_calloc( 1, seed_size );
1141+
if( seed == NULL )
1142+
{
1143+
status = PSA_ERROR_INSUFFICIENT_MEMORY;
1144+
break;
1145+
}
1146+
bytes_read = psa_read( msg.handle, 0, seed, seed_size );
1147+
if( bytes_read != seed_size )
1148+
{
1149+
SPM_PANIC("SPM read length mismatch");
1150+
}
1151+
status = mbedtls_psa_inject_entropy( seed, seed_size );
1152+
mbedtls_free( seed );
1153+
break;
1154+
}
1155+
case PSA_IPC_DISCONNECT:
1156+
{
1157+
break; /* do nothing */
1158+
}
1159+
default:
1160+
{
1161+
status = PSA_ERROR_NOT_SUPPORTED;
1162+
break;
1163+
}
1164+
}
1165+
psa_reply( msg.handle, status );
1166+
}
1167+
#endif
1168+
11171169
static void psa_rng_operation( void )
11181170
{
11191171
psa_msg_t msg = { 0 };
@@ -1378,5 +1430,11 @@ void part_main(void *ptr)
13781430
{
13791431
psa_crypto_generator_operations( );
13801432
}
1433+
#if defined(MBEDTLS_ENTROPY_NV_SEED)
1434+
if( signals & PSA_ENTROPY_INJECT )
1435+
{
1436+
psa_entropy_operation( );
1437+
}
1438+
#endif /* MBEDTLS_ENTROPY_NV_SEED */
13811439
}
13821440
}

components/TARGET_PSA/services/crypto/COMPONENT_SPE/psa_psa_f_partition.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
/* Copyright (c) 2017 ARM Limited
1+
/* Copyright (c) 2017-2018 ARM Limited
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
24
*
35
* Licensed under the Apache License, Version 2.0 (the "License");
46
* you may not use this file except in compliance with the License.
@@ -26,6 +28,7 @@
2628
#include "spm_internal.h"
2729
#include "psa_psa_f_partition.h"
2830
#include "psa_psa_f_ifs.h"
31+
#include "psa_its_ifs.h"
2932

3033

3134
/* Threads stacks */
@@ -168,8 +171,27 @@ spm_rot_service_t psa_f_rot_services[PSA_F_ROT_SRV_COUNT] = {
168171
.tail = NULL
169172
}
170173
},
174+
{
175+
.sid = PSA_ENTROPY_ID,
176+
.mask = PSA_ENTROPY_INJECT,
177+
.partition = NULL,
178+
.min_version = 1,
179+
.min_version_policy = PSA_MINOR_VERSION_POLICY_STRICT,
180+
.allow_nspe = true,
181+
.queue = {
182+
.head = NULL,
183+
.tail = NULL
184+
}
185+
},
171186
};
172187

188+
/* External SIDs used by PSA_F */
189+
const uint32_t psa_f_external_sids[4] = {
190+
PSA_ITS_GET,
191+
PSA_ITS_SET,
192+
PSA_ITS_INFO,
193+
PSA_ITS_REMOVE,
194+
};
173195

174196
static osRtxMutex_t psa_f_mutex = {0};
175197
static const osMutexAttr_t psa_f_mutex_attr = {

components/TARGET_PSA/services/crypto/COMPONENT_SPE/psa_psa_f_partition.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
/* Copyright (c) 2017 ARM Limited
1+
/* Copyright (c) 2017-2018 ARM Limited
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
24
*
35
* Licensed under the Apache License, Version 2.0 (the "License");
46
* you may not use this file except in compliance with the License.
@@ -24,8 +26,8 @@
2426

2527
#define PSA_F_ID 35
2628

27-
#define PSA_F_ROT_SRV_COUNT (10UL)
28-
#define PSA_F_EXT_ROT_SRV_COUNT (0UL)
29+
#define PSA_F_ROT_SRV_COUNT (11UL)
30+
#define PSA_F_EXT_ROT_SRV_COUNT (4UL)
2931

3032
/* PSA_F event flags */
3133
#define PSA_F_RESERVED1_POS (1UL)
@@ -56,6 +58,8 @@
5658
#define PSA_CRYPTO_FREE (1UL << PSA_CRYPTO_FREE_POS)
5759
#define PSA_GENERATOR_POS (13UL)
5860
#define PSA_GENERATOR (1UL << PSA_GENERATOR_POS)
61+
#define PSA_ENTROPY_INJECT_POS (14UL)
62+
#define PSA_ENTROPY_INJECT (1UL << PSA_ENTROPY_INJECT_POS)
5963

6064
#define PSA_F_WAIT_ANY_SID_MSK (\
6165
PSA_CRYPTO_INIT | \
@@ -67,7 +71,8 @@
6771
PSA_KEY_MNG | \
6872
PSA_RNG | \
6973
PSA_CRYPTO_FREE | \
70-
PSA_GENERATOR)
74+
PSA_GENERATOR | \
75+
PSA_ENTROPY_INJECT)
7176

7277
/*
7378
#define PSA_F_WAIT_ANY_MSK (\

components/TARGET_PSA/services/crypto/crypto_partition_psa.json

100644100755
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,22 @@
8686
"non_secure_clients": true,
8787
"minor_version": 1,
8888
"minor_policy": "STRICT"
89+
},
90+
{
91+
"name": "PSA_ENTROPY_ID",
92+
"identifier": "0x00000F0A",
93+
"signal": "PSA_ENTROPY_INJECT",
94+
"non_secure_clients": true,
95+
"minor_version": 1,
96+
"minor_policy": "STRICT"
8997
}
9098
],
99+
"extern_sids": [
100+
"PSA_ITS_GET",
101+
"PSA_ITS_SET",
102+
"PSA_ITS_INFO",
103+
"PSA_ITS_REMOVE"
104+
],
91105
"source_files": [
92106
"COMPONENT_SPE/psa_crypto_partition.c"
93107
]

components/TARGET_PSA/services/crypto/psa_psa_f_ifs.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
/* Copyright (c) 2017 ARM Limited
1+
/* Copyright (c) 2017-2018 ARM Limited
2+
*
3+
* SPDX-License-Identifier: Apache-2.0
24
*
35
* Licensed under the Apache License, Version 2.0 (the "License");
46
* you may not use this file except in compliance with the License.
@@ -32,5 +34,6 @@
3234
#define PSA_RNG_ID 0x00000F07
3335
#define PSA_CRYPTO_FREE_ID 0x00000F08
3436
#define PSA_GENERATOR_ID 0x00000F09
37+
#define PSA_ENTROPY_ID 0x00000F0A
3538

3639
#endif // PSA_PSA_F_PARTITION_ROT_SERVICES_H

components/TARGET_PSA/spm/COMPONENT_SPE/psa_setup.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,32 +25,33 @@
2525
#include "spm_internal.h"
2626
#include "handles_manager.h"
2727
#include "cmsis.h"
28-
#include "psa_psa_f_partition.h"
2928
#include "psa_its_partition.h"
29+
#include "psa_psa_f_partition.h"
3030

31+
extern const uint32_t psa_f_external_sids[4];
3132

3233
__attribute__((weak))
3334
spm_partition_t g_partitions[2] = {
3435
{
35-
.partition_id = PSA_F_ID,
36+
.partition_id = ITS_ID,
3637
.thread_id = 0,
37-
.flags_rot_srv = PSA_F_WAIT_ANY_SID_MSK,
38+
.flags_rot_srv = ITS_WAIT_ANY_SID_MSK,
3839
.flags_interrupts = 0,
3940
.rot_services = NULL,
40-
.rot_services_count = PSA_F_ROT_SRV_COUNT,
41+
.rot_services_count = ITS_ROT_SRV_COUNT,
4142
.extern_sids = NULL,
42-
.extern_sids_count = PSA_F_EXT_ROT_SRV_COUNT,
43+
.extern_sids_count = ITS_EXT_ROT_SRV_COUNT,
4344
.irq_mapper = NULL,
4445
},
4546
{
46-
.partition_id = ITS_ID,
47+
.partition_id = PSA_F_ID,
4748
.thread_id = 0,
48-
.flags_rot_srv = ITS_WAIT_ANY_SID_MSK,
49+
.flags_rot_srv = PSA_F_WAIT_ANY_SID_MSK,
4950
.flags_interrupts = 0,
5051
.rot_services = NULL,
51-
.rot_services_count = ITS_ROT_SRV_COUNT,
52-
.extern_sids = NULL,
53-
.extern_sids_count = ITS_EXT_ROT_SRV_COUNT,
52+
.rot_services_count = PSA_F_ROT_SRV_COUNT,
53+
.extern_sids = psa_f_external_sids,
54+
.extern_sids_count = PSA_F_EXT_ROT_SRV_COUNT,
5455
.irq_mapper = NULL,
5556
},
5657
};
@@ -65,8 +66,8 @@ __attribute__((weak))
6566
const uint32_t mem_region_count = 0;
6667

6768
// forward declaration of partition initializers
68-
void psa_f_init(spm_partition_t *partition);
6969
void its_init(spm_partition_t *partition);
70+
void psa_f_init(spm_partition_t *partition);
7071

7172
__attribute__((weak))
7273
uint32_t init_partitions(spm_partition_t **partitions)
@@ -75,8 +76,8 @@ uint32_t init_partitions(spm_partition_t **partitions)
7576
SPM_PANIC("partitions is NULL!\n");
7677
}
7778

78-
psa_f_init(&(g_partitions[0]));
79-
its_init(&(g_partitions[1]));
79+
its_init(&(g_partitions[0]));
80+
psa_f_init(&(g_partitions[1]));
8081

8182
*partitions = g_partitions;
8283
return 2;

targets/targets.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7461,7 +7461,7 @@
74617461
"inherits": ["SPE_Target", "FUTURE_SEQUANA_M0"],
74627462
"components_add": ["SPM_MAILBOX", "FLASHIAP"],
74637463
"extra_labels_add": ["PSA"],
7464-
"macros_add": ["PSOC6_DYNSRM_DISABLE=1", "MBEDTLS_PSA_CRYPTO_SPM"],
7464+
"macros_add": ["PSOC6_DYNSRM_DISABLE=1", "MBEDTLS_PSA_CRYPTO_SPM", "MBEDTLS_PSA_HAS_ITS_IO", "MBEDTLS_ENTROPY_NV_SEED", "MBEDTLS_PLATFORM_NV_SEED_READ_MACRO=mbed_default_seed_read", "MBEDTLS_PLATFORM_NV_SEED_WRITE_MACRO=mbed_default_seed_write", "MBEDTLS_PSA_CRYPTO_C" ],
74657465
"deliver_to_target": "FUTURE_SEQUANA_PSA",
74667466
"overrides": {
74677467
"secure-rom-start": "0x10000000",

0 commit comments

Comments
 (0)