Skip to content

Commit 221e390

Browse files
committed
app: add a secure crypt container for secureshield app
Signed-off-by: Yiping Peng <yibingp@synopsys.com>
1 parent 616c4ac commit 221e390

File tree

11 files changed

+385
-29
lines changed

11 files changed

+385
-29
lines changed

aws_iot_smarthome_secureshield/src/container_cfg.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@
2929
--------------------------------------------- */
3030

3131
#include "embARC.h"
32-
#include "device.h"
32+
#include "device_container.h"
33+
#include "secure/crypt_container.h"
3334

3435
#define PERIPHERAL_ADDR_BASE 0xf0000000
3536

36-
static CONTAINER_AC_TABLE device_container_act[] = {
37+
static CONTAINER_AC_TABLE g_device_container_act[] = {
3738
{smarthome_init, 0, SECURESHIELD_AC_INTERFACE},
3839
{smarthome_close, 0, SECURESHIELD_AC_INTERFACE},
3940
{getRoomTemperature, 1, SECURESHIELD_AC_INTERFACE},
@@ -43,6 +44,13 @@ static CONTAINER_AC_TABLE device_container_act[] = {
4344
{(void *)(PERIPHERAL_ADDR_BASE + REL_REGBASE_I2C0), 0x1000, SECURESHIELD_ACDEF_UPERIPH}
4445
};
4546

47+
static CONTAINER_AC_TABLE g_crypt_container_act[] = {
48+
{init_crypt, 0, SECURESHIELD_AC_INTERFACE},
49+
{close_crypt, 0, SECURESHIELD_AC_INTERFACE},
50+
{operate_encrypt, 4, SECURESHIELD_AC_INTERFACE},
51+
{operate_decrypt, 4, SECURESHIELD_AC_INTERFACE}
52+
};
53+
4654
static CONTAINER_AC_TABLE g_main_container_act[] = {
4755
/* PINMUX, UART1 and GPIO0 are required */
4856
{(void *)(PERIPHERAL_ADDR_BASE + REL_REGBASE_PINMUX), 0x1000, SECURESHIELD_ACDEF_UPERIPH},
@@ -65,4 +73,7 @@ static CONTAINER_AC_TABLE g_main_container_act[] = {
6573
SECURESHIELD_CONTAINER_BACKGROUND(g_main_container_act);
6674

6775
/* configure the other container */
68-
SECURESHIELD_CONTAINER_CONFIG(device_container, device_container_act, 1024);
76+
SECURESHIELD_CONTAINER_CONFIG(device_container, g_device_container_act, 1024);
77+
78+
/* crypt container is secure container, it has access to most of the system resources */
79+
SECURESHIELD_CONTAINER_CONFIG(crypt_container, g_crypt_container_act, 4096, SECURESHIELD_CONTAINER_SECURE);

aws_iot_smarthome_secureshield/src/device.c renamed to aws_iot_smarthome_secureshield/src/device_container.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include "embARC_debug.h"
3535
#include "embARC_assert.h"
3636

37-
#include "device.h"
37+
#include "device_container.h"
3838

3939
#include "adt7420.h"
4040

aws_iot_smarthome_secureshield/src/main.c

Lines changed: 43 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@
110110
#include <sys/time.h>
111111
#include <limits.h>
112112

113-
114113
#include "aws_iot_config.h"
115114
#include "aws_iot_log.h"
116115
#include "aws_iot_version.h"
117116
#include "aws_iot_mqtt_client_interface.h"
118117
#include "aws_iot_shadow_interface.h"
119118

120119
#include "embARC.h"
121-
#include "device.h"
120+
#include "device_container.h"
121+
#include "secure/crypt_container.h"
122122

123123
#if defined(AWSDEMO_HAVE_OLED) && defined(MID_U8GLIB)
124124
#include "u8g.h"
@@ -135,6 +135,9 @@
135135

136136
#define MAX_LENGTH_OF_UPDATE_JSON_BUFFER 512
137137

138+
// use crypt container to encrypt upload data
139+
#define ENCRYPT_UPLOAD_DATA true
140+
138141
//#define SIMULATE_TEMPERATURE
139142

140143
#define CERT_ROOTDIR "cert/smarthome"
@@ -149,6 +152,10 @@ static bool LivingRoomLights_updated = false;
149152

150153
static char JsonDocumentBuffer[MAX_LENGTH_OF_UPDATE_JSON_BUFFER];
151154
static size_t sizeOfJsonDocumentBuffer = sizeof(JsonDocumentBuffer) / sizeof(JsonDocumentBuffer[0]);
155+
#if ENCRYPT_UPLOAD_DATA
156+
static char JsonDocumentBufferCipher[MAX_LENGTH_OF_UPDATE_JSON_BUFFER];
157+
static char JsonDocumentBufferDeCipher[MAX_LENGTH_OF_UPDATE_JSON_BUFFER];
158+
#endif
152159

153160
static float temperature = STARTING_ROOMTEMPERATURE;
154161
static bool DoorLocked = false;
@@ -201,8 +208,8 @@ static void u8g_draw(void)
201208

202209
u8g_DrawStr(&u8g, 35, 0, "SmartHome");
203210

204-
sprintf(strbuf, "Connection: %s", ConnectionStatus ? "ok" : "lost");
205-
u8g_DrawStr(&u8g, 15, 10, (const char*)strbuf);
211+
sprintf(strbuf, "Connection: %s", ConnectionStatus ? "ok" : "lost");
212+
u8g_DrawStr(&u8g, 15, 10, (const char*)strbuf);
206213

207214
sprintf(strbuf, "Temperature: %.1fC", temperature);
208215
u8g_DrawStr(&u8g, 15, 20, (const char*)strbuf);
@@ -261,8 +268,9 @@ void FrontDoorActuate_Callback(const char *pJsonString, uint32_t JsonStringDataL
261268
if (pContext != NULL) {
262269
bool temp = *(bool *)(pContext->pData);
263270
IOT_INFO("Delta - FrontDoor state changed to %d", temp);
264-
// delete controlFrontDoor(DoorLocked);
271+
vTaskSuspendAll();
265272
container_call(device_container, controlFrontDoor, DoorLocked);
273+
xTaskResumeAll();
266274
}
267275
}
268276

@@ -271,8 +279,9 @@ void KitchenLights_Callback(const char *pJsonString, uint32_t JsonStringDataLen,
271279
if (pContext != NULL) {
272280
bool temp = *(bool *)(pContext->pData);
273281
IOT_INFO("Delta - KitchenLights light state changed to %d", temp);
274-
//delete controlKitchenLights(KitchenLights);
282+
vTaskSuspendAll();
275283
container_call(device_container, controlKitchenLights, KitchenLights);
284+
xTaskResumeAll();
276285
}
277286
}
278287

@@ -281,8 +290,9 @@ void LivingRoomLights_Callback(const char *pJsonString, uint32_t JsonStringDataL
281290
if (pContext != NULL) {
282291
bool temp = *(bool *)(pContext->pData);
283292
IOT_INFO("Delta - LivingRoomLights light state changed to %d", temp);
284-
//delete controlLivingRoomLights(LivingRoomLights);
293+
vTaskSuspendAll();
285294
container_call(device_container, controlLivingRoomLights, LivingRoomLights);
295+
xTaskResumeAll();
286296
}
287297
}
288298

@@ -378,7 +388,7 @@ void disconnectCallbackHandler(AWS_IoT_Client *pClient, void *data)
378388

379389
IOT_UNUSED(data);
380390

381-
ConnectionStatus = false;
391+
ConnectionStatus = false;
382392

383393
if (aws_iot_is_autoreconnect_enabled(pClient)) {
384394
IOT_INFO("Auto Reconnect is enabled, Reconnecting attempt will start now");
@@ -407,7 +417,7 @@ int main(void)
407417
DoorLocked_updated = false;
408418
KitchenLights_updated = false;
409419
LivingRoomLights_updated = false;
410-
ConnectionStatus = false;
420+
ConnectionStatus = false;
411421

412422
FrontDoorActuator.cb = FrontDoorActuate_Callback;
413423
FrontDoorActuator.pData = &DoorLocked;
@@ -504,19 +514,15 @@ int main(void)
504514
IOT_INFO("Shadow Register Delta OK");
505515
}
506516

517+
vTaskSuspendAll();
507518
#ifndef SIMULATE_TEMPERATURE
508-
//delete getRoomTemperature(&temperature);
509519
container_call(device_container, getRoomTemperature, &temperature);
510520
#endif
511-
//delete smarthome_init();
512521
container_call(device_container, smarthome_init);
513-
514-
// delete controlFrontDoor(DoorLocked);
515522
container_call(device_container, controlFrontDoor, DoorLocked);
516-
//delete controlKitchenLights(KitchenLights);
517523
container_call(device_container, controlKitchenLights, KitchenLights);
518-
//delete controlLivingRoomLights(LivingRoomLights);
519524
container_call(device_container, controlLivingRoomLights, LivingRoomLights);
525+
xTaskResumeAll();
520526

521527
last_LivingRoomLights = !LivingRoomLights;
522528
last_KitchenLights = !KitchenLights;
@@ -531,32 +537,35 @@ int main(void)
531537
// If the client is attempting to reconnect we will skip the rest of the loop.
532538
continue;
533539
}
534-
ConnectionStatus = true;
540+
ConnectionStatus = true;
535541
/** Update desired messages */
536542
act_idx = 0;
537543
if (DoorLocked_updated) {
538544
curActuator[act_idx] = &FrontDoorActuator;
539545
act_idx ++;
540546
DoorLocked = !DoorLocked;
541547
DoorLocked_updated = false;
542-
// delete controlFrontDoor(DoorLocked);
548+
vTaskSuspendAll();
543549
container_call(device_container, controlFrontDoor, DoorLocked);
550+
xTaskResumeAll();
544551
}
545552
if (KitchenLights_updated) {
546553
curActuator[act_idx] = &KitchenLightsActuator;
547554
act_idx ++;
548555
KitchenLights = !KitchenLights;
549556
KitchenLights_updated = false;
550-
//delete controlKitchenLights(KitchenLights);
557+
vTaskSuspendAll();
551558
container_call(device_container, controlKitchenLights, KitchenLights);
559+
xTaskResumeAll();
552560
}
553561
if (LivingRoomLights_updated) {
554562
curActuator[act_idx] = &LivingRoomLightsActuator;
555563
act_idx ++;
556564
LivingRoomLights = !LivingRoomLights;
557565
LivingRoomLights_updated = false;
558-
//delete controlLivingRoomLights(LivingRoomLights);
566+
vTaskSuspendAll();
559567
container_call(device_container, controlLivingRoomLights, LivingRoomLights);
568+
xTaskResumeAll();
560569
}
561570
if (act_idx > 0) {
562571
rc = aws_iot_shadow_init_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer);
@@ -575,7 +584,17 @@ int main(void)
575584
rc = aws_iot_finalize_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer);
576585
if (rc == SUCCESS) {
577586
IOT_INFO("Update Shadow Desired: %s", JsonDocumentBuffer);
587+
#if ENCRYPT_UPLOAD_DATA
588+
IOT_INFO("now try to encrypt input:%s len:%d \r\n", JsonDocumentBuffer, sizeOfJsonDocumentBuffer);
589+
vTaskSuspendAll();
590+
container_call(crypt_container, operate_encrypt, JsonDocumentBuffer, sizeOfJsonDocumentBuffer, \
591+
JsonDocumentBufferCipher, &sizeOfJsonDocumentBuffer);
592+
xTaskResumeAll();
593+
IOT_INFO("result of encrypt output:%s len:%d \r\n", JsonDocumentBufferCipher, sizeOfJsonDocumentBuffer);
594+
rc = aws_iot_shadow_update(&mqttClient, AWS_IOT_MY_THING_NAME, JsonDocumentBufferCipher, ShadowUpdateStatusCallback, NULL, 4, true);
595+
#else
578596
rc = aws_iot_shadow_update(&mqttClient, AWS_IOT_MY_THING_NAME, JsonDocumentBuffer, ShadowUpdateStatusCallback, NULL, 4, true);
597+
#endif
579598
}
580599
}
581600
}
@@ -586,7 +605,9 @@ int main(void)
586605
continue;
587606
}
588607
/** Update reported messages */
608+
vTaskSuspendAll();
589609
temperature_updated = container_call(device_container, getRoomTemperature, &temperature);
610+
xTaskResumeAll();
590611

591612
rc = aws_iot_shadow_init_json_document(JsonDocumentBuffer, sizeOfJsonDocumentBuffer);
592613
if (rc == SUCCESS) {
@@ -600,16 +621,17 @@ int main(void)
600621
}
601622
}
602623
}
603-
u8g_draw();
624+
u8g_draw();
604625

605626
toggleHeartbeatLed();
606627

607628
IOT_INFO("*****************************************************************************************\n");
608629
vTaskDelay(delay_ms);
609630
}
610631

611-
//delete smarthome_close();
632+
vTaskSuspendAll();
612633
container_call(device_container, smarthome_close);
634+
xTaskResumeAll();
613635
if (SUCCESS != rc) {
614636
IOT_ERROR("An error occurred in the loop %d", rc);
615637
}

aws_iot_smarthome_secureshield/src/makefile

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ LOG_FLAGS += -DENABLE_IOT_WARN
1414
LOG_FLAGS += -DENABLE_IOT_ERROR
1515

1616
APPL_DEFINES = $(LOG_FLAGS) -DMBEDTLS_CONFIG_FILE=\"mbedtls_config.h\" \
17-
-DTASK_STACK_SIZE_MAIN=4096*2 -DARC_FEATURE_STACK_CHECK=0
17+
-DTASK_STACK_SIZE_MAIN=4096*2 -DARC_FEATURE_STACK_CHECK=0 \
18+
-DSECURESHIELD_SECURE_RAM_USAGE=0x1800000
1819
## Define whether demo include OLED
1920
# 1: Yes
2021
# 0: No
@@ -43,6 +44,14 @@ APPL_ASMSRC_DIR = .
4344
# application include dirs
4445
APPL_INC_DIR = .
4546

47+
# application include dirs
48+
APPL_INC_DIR = . ./secure
49+
50+
51+
# the dir where src files should be compiled and linked with secure world
52+
APPL_SECURE_CSRC_DIR = ./secure
53+
SECURE_SYMBOL_LIST = ./secure/secure_symbol.txt
54+
4655
# include current project makefile
4756
COMMON_COMPILE_PREREQUISITES += makefile
4857

0 commit comments

Comments
 (0)