main_ns.c
6.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
/**********************************************************
*
* @file : main_ns.c
* @author : HaewonSeo
*
* @note : Main of Non-secure world, Non-secure code for Collaborative Secure Software Development
*
**********************************************************/
#include <arm_cmse.h>
#include <string.h>
#include "NuMicro.h" /* Device header */
#include "cssd_lib.h" /* Collaborative Secure Software Development Library header */
#include "wifi.h"
#include "OLED_GUI.h"
#define SW2 PB0_NS
#define SW3 PB1_NS
extern const char ATCommand_CIPSTART[];
extern void OLED_BPM(uint32_t bpm);
/*----------------------------------------------------------------------------
NonSecure Functions from NonSecure Region
*----------------------------------------------------------------------------*/
void Control_SW3()
{
int result = 0;
t_netData *send;
send = calloc(1, sizeof(t_netData));
send->data = calloc(33, sizeof(char));
send->len = 0;
//result = WIFI_ReceiveData(1, rec);
const char msg[] = "Hello..";
send->len = strlen(msg);
strcpy(send->data, msg);
WIFI_SendData(1, send, 1);
free(send->data);
free(send);
t_netData *rec = malloc(sizeof(t_netData));
rec->data = malloc(sizeof(char)*33);
(rec->data)[32] = '\0';
rec->len = 0;
WIFI_ReceiveData(1, rec);
free(rec->data);
free(rec);
return ;
}
/*----------------------------------------------------------------------------
SysTick IRQ Handler
*----------------------------------------------------------------------------*/
void SysTick_Handler(void)
{
static uint32_t u32Ticks;
switch(u32Ticks++)
{
case 0:
break;
case 100:
break;
case 200:
break;
case 300:
break;
case 400:
break;
case 500:
break;
case 600:
u32Ticks = 0;
break;
default:
if(u32Ticks > 600)
{
u32Ticks = 0;
}
}
}
/*----------------------------------------------------------------------------
Main function
*----------------------------------------------------------------------------*/
int main(void)
{
int32_t i = 0;
int32_t ispulse = 0;
uint32_t pulse = 0;
t_digitallySignedData dsd;
memset(&dsd, 0, sizeof(t_digitallySignedData));
int32_t SW2_toggle = 0;
int32_t ns_ticks = 0;
printf("\n");
printf("+---------------------------------------------+\n");
printf("| Non-Secure is running ... |\n");
printf("+---------------------------------------------+\n");
/* register NonSecure callbacks in Secure application */
Secure_OLED_On_callback(&OLED_BPM);
//Secure_OLED_Off_callback(&NonSecure_OLED_Off);
/* Call secure API to get system core clock */
SystemCoreClock = GetSystemCoreClock();
/* Generate Systick interrupt each 10 ms */
SysTick_Config(SystemCoreClock / 100);
printf("\n");
printf("+---------------------------------------------+\n");
printf("| SDK1327 OLED Init |\n");
printf("+---------------------------------------------+\n");
OLED_Config();
printf("\n");
printf("+---------------------------------------------+\n");
printf("| ESP8266 WiFi Module Start |\n");
printf("+---------------------------------------------+\n");
WIFI_Connect();
while(1)
{
/* SW2 */
if(SW2 == 0)
{
while(SW2 == 0);
SW2_toggle = !SW2_toggle;
printf("\nSW2 ON in Non-secure code ...\n\n");
if(SW2_toggle)
{
ns_ticks = 0;
printf("\nMAX30102 On ...\n\n");
M2351_LoadKey();
OLED_Background_On();
}
else
{
printf("\nMAX30102 Off ...\n\n");
M2351_DeleteKey();
OLED_Off();
}
}
/* SW3 */
if(SW3 == 0)
{
while(SW3 == 0);
printf("\nSW3 ON in Non-secure code ...\n\n");
Control_SW3();
}
if(SW2_toggle)
{
/* Get digitally signed data of BPM from MAX30102 */
ispulse = Get_EncryptedDigitallySignedData(&dsd);
ns_ticks++;
if (ns_ticks >= 60)
{
ns_ticks = 0;
if (ispulse)
{
printf("\nEncrypted digitallySignedData\n");
printDigitallySignedData(&dsd);
/* Send digitally signed data of BPM to Server */
//WIFI_Send_BPM(ispulse);
WIFI_Send_EncryptedDigitallySignedData(&dsd);
}
}
}
}
}
//CLK_SysTickLongDelay(300000); //300000us = 300ms = 0.3s
/*
void testCryptDeCrypt() {
//Test Function to cipher and decypher data
//__attribute__((aligned(4))) uint8_t plainData[16] = {0x32, 0x43, 0xf6, 0xa8, 0x88, 0x5a, 0x30, 0x8d, 0x31, 0x31, 0x98, 0xa2, 0xe0, 0x37, 0x07, 0x34};
__attribute__((aligned(4))) uint8_t plainData[16] = {0};
__attribute__((aligned(4))) uint8_t cipheredData[16] = {0};
__attribute__((aligned(4))) uint8_t resultData[16] = {0};
__attribute__((aligned(4))) uint8_t key[16] =
{
0x7f, 0x35, 0x91, 0xd3, 0x6f, 0xd5, 0x17, 0xa3, 0x7b, 0x6d, 0xe9, 0xe0, 0xdf, 0x93, 0x4b, 0x7a
};
__attribute__((aligned(4))) uint8_t iv[16] = {0};
sprintf((char *)plainData, "%d", 123);
printf("%s\n", plainData);
if (DEMO) {
//printSecure("&key = %p\n",key);
//printBlock(key);
//printSecure("&iv = %p\n",iv);
//printBlock(iv);
printSecure("&plainData = %p\n",plainData, NULL);
printBlock(plainData);
CLK_SysTickLongDelay(500000);
}
// Store_key(key);
// Store_iv(iv);
int c = Encrypt_data(plainData, cipheredData, 16);
if (c == NULL) printSecure("Error : 128bits only", NULL, NULL);
if (DEMO) {
CLK_SysTickLongDelay(500000);
printSecure("| Nonsecure is running ... |\n",NULL, NULL);
printSecure("&cipheredData = %p\n",cipheredData, NULL);
printBlock(cipheredData);
}
int r = Decrypt_data(cipheredData, resultData, 16);
if (r == NULL) printSecure("Error : 128bits only", NULL, NULL);
if (DEMO) {
CLK_SysTickLongDelay(500000);
printSecure("| Nonsecure is running ... |\n",NULL, NULL);
printSecure("&resultData = %p\n",resultData, NULL);
printBlock(resultData);
uint8_t error = 0;
for (uint8_t i = 0 ; i < 16 ; i++) {
if (resultData[i] != plainData[i]) error++;
}
if (error != 0) printSecure("Error plainData is not equal to resultData", NULL, NULL);
else printSecure("No error plainData is equal to resultData", NULL, NULL);
}
}
*/