forked from zephyrproject-rtos/zephyr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
268 lines (229 loc) · 6.95 KB
/
main.c
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
259
260
261
262
263
264
265
266
267
268
/* main.c */
/*
* SPDX-License-Identifier: Apache-2.0
*/
/*
*
* Basic example of userspace thread protected memory
*
* NOTE: The encryption algorithm is unverified and
* based on a 1930's erra piece of hardware.
* DO NOT USE THIS CODE FOR SECURITY
*
*/
#include "main.h"
#include "enc.h"
/* the following definition name prefix is to avoid a conflict */
#define SAMP_BLOCKSIZE 50
/*
* The memory partitions have been named to simplify
* the definition of variables. A possible alternative
* is using one source file per thread and implementing
* a objcopy to rename the data and bss section for the
* thread to the partiotion name.
*/
/* prepare the memory partition structures */
FOR_EACH(K_APPMEM_PARTITION_DEFINE, (;), user_part, red_part, enc_part, blk_part, ct_part);
/* prepare the memory domain structures */
struct k_mem_domain pt_domain, enc_domain;
/* each variable starts with a name defined in main.h
* the names are symbolic for the memory partitions
* purpose.
*/
volatile _app_red_b BYTE fBUFIN;
volatile _app_red_b BYTE BUFIN[63];
volatile _app_blk_b BYTE fBUFOUT;
volatile _app_blk_b BYTE BUFOUT[63];
/* declare and set wheel and reflector */
/* To use add definition ALTMSG */
#ifdef ALTMSG
volatile _app_enc_d BYTE W1[26] = START_WHEEL;
#else
volatile _app_enc_d BYTE W1[26] = START_WHEEL2;
#endif
volatile _app_enc_d BYTE W2[26] = START_WHEEL;
volatile _app_enc_d BYTE W3[26] = START_WHEEL;
volatile _app_enc_d BYTE R[26] = REFLECT;
volatile _app_enc_b int IW1;
volatile _app_enc_b int IW2;
volatile _app_enc_b int IW3;
/*
* calculated by the enc thread at init and when the wheels
* change.
*/
volatile _app_enc_b BYTE W1R[26];
volatile _app_enc_b BYTE W2R[26];
volatile _app_enc_b BYTE W3R[26];
/*
* sync threads
*/
K_SEM_DEFINE(allforone, 0, 3);
struct k_thread enc_thread;
K_THREAD_STACK_DEFINE(enc_stack, STACKSIZE);
struct k_thread pt_thread;
K_THREAD_STACK_DEFINE(pt_stack, STACKSIZE);
struct k_thread ct_thread;
K_THREAD_STACK_DEFINE(ct_stack, STACKSIZE);
_app_enc_d char encMSG[] = "ENC!\n";
volatile _app_enc_b char enc_pt[50]; /* Copy form shared pt */
volatile _app_enc_b char enc_ct[50]; /* Copy to shared ct */
_app_user_d char ptMSG[] = "PT: message to encrypt\n";
/* encrypted message when W1 = START_WHEEL */
/* to use add definition ALTMSG */
#ifdef ALTMSG
_app_user_d char ptMSG2[] = "nfttbhfspfmdqzos\n";
#else
/* encrypted message when W1 = START_WHEEL2 */
_app_user_d char ptMSG2[] = "ofttbhfspgmeqzos\n";
#endif
_app_ct_d char ctMSG[] = "CT!\n";
void main(void)
{
struct k_mem_partition *enc_parts[] = {&enc_part, &red_part, &blk_part};
struct k_mem_partition *pt_parts[] = {&user_part, &red_part};
k_tid_t tPT, tENC, tCT;
fBUFIN = 0; /* clear flags */
fBUFOUT = 0;
calc_rev_wheel((BYTE *) &W1, (BYTE *)&W1R);
calc_rev_wheel((BYTE *) &W2, (BYTE *)&W2R);
calc_rev_wheel((BYTE *) &W3, (BYTE *)&W3R);
IW1 = 0;
IW2 = 0;
IW3 = 0;
k_thread_access_grant(k_current_get(), &allforone);
/*
* create an enc thread init the memory domain and add partitions
* then add the thread to the domain.
*/
tENC = k_thread_create(&enc_thread, enc_stack, STACKSIZE,
(k_thread_entry_t)enc, NULL, NULL, NULL,
-1, K_USER,
K_FOREVER);
k_thread_access_grant(tENC, &allforone);
/* use K_FOREVER followed by k_thread_start*/
printk("ENC Thread Created %p\n", tENC);
k_mem_domain_init(&enc_domain, 3, enc_parts);
printk("Partitions added to enc_domain\n");
k_mem_domain_add_thread(&enc_domain, tENC);
printk("enc_domain Created\n");
tPT = k_thread_create(&pt_thread, pt_stack, STACKSIZE,
(k_thread_entry_t)pt, NULL, NULL, NULL,
-1, K_USER,
K_FOREVER);
k_thread_access_grant(tPT, &allforone);
printk("PT Thread Created %p\n", tPT);
k_mem_domain_init(&pt_domain, 2, pt_parts);
k_mem_domain_add_thread(&pt_domain, tPT);
printk("pt_domain Created\n");
tCT = k_thread_create(&ct_thread, ct_stack, STACKSIZE,
(k_thread_entry_t)ct, NULL, NULL, NULL,
-1, K_USER,
K_FOREVER);
k_thread_access_grant(tCT, &allforone);
printk("CT Thread Created %p\n", tCT);
/* Re-using the default memory domain for CT */
k_mem_domain_add_partition(&k_mem_domain_default, &ct_part);
k_mem_domain_add_partition(&k_mem_domain_default, &blk_part);
printk("ct partitions installed\n");
k_thread_start(&enc_thread);
/* need to start all three threads. let enc go first to perform init step */
printk("ENC thread started\n");
k_thread_start(&pt_thread);
printk("PT thread started\n");
k_thread_start(&ct_thread);
k_sem_give(&allforone);
printk("CT thread started\n");
}
/*
* The enc thread.
* Function: initialize the the simulation of the wheels.
* Copy memory from pt thread and encrypt to a local buffer
* then copy to the ct thread.
*/
void enc(void)
{
int index, index_out;
while (1) {
k_sem_take(&allforone, K_FOREVER);
if (fBUFIN == 1) { /* 1 is process text */
printk("ENC Thread Received Data\n");
/* copy message form shared mem and clear flag */
memcpy((void *)&enc_pt, (void *)BUFIN, SAMP_BLOCKSIZE);
printk("ENC PT MSG: %s\n", (char *)&enc_pt);
fBUFIN = 0;
/* reset wheel: probably better as a flag option */
IW1 = 7;
IW2 = 2;
IW3 = 3;
/* encode */
memset((void *)&enc_ct, 0, SAMP_BLOCKSIZE);
for (index = 0, index_out = 0; index < SAMP_BLOCKSIZE; index++) {
if (enc_pt[index] == '\0') {
enc_ct[index_out] = '\0';
break;
}
if (enc_pt[index] >= 'a' && enc_pt[index] <= 'z') {
enc_ct[index_out] = (BYTE)enig_enc((BYTE) enc_pt[index]);
index_out++;
}
}
/* test for CT flag */
while (fBUFOUT != 0) {
k_sleep(K_MSEC(1));
}
/* ct thread has cleared the buffer */
memcpy((void *)&BUFOUT, (void *)&enc_ct,
SAMP_BLOCKSIZE);
fBUFOUT = 1;
}
k_sem_give(&allforone);
}
}
/*
* the pt function pushes data to the enc thread.
* It can be extended to receive data from a serial port
* and pass the data to enc
*/
void pt(void)
{
k_sleep(K_MSEC(20));
while (1) {
k_sem_take(&allforone, K_FOREVER);
if (fBUFIN == 0) { /* send message to encode */
printk("\nPT Sending Message 1\n");
memset((void *)&BUFIN, 0, SAMP_BLOCKSIZE);
memcpy((void *)&BUFIN, (void *)&ptMSG, sizeof(ptMSG));
/* strlen should not be used if user provided data, needs a max length set */
fBUFIN = 1;
}
k_sem_give(&allforone);
k_sem_take(&allforone, K_FOREVER);
if (fBUFIN == 0) { /* send message to decode */
printk("\nPT Sending Message 1'\n");
memset((void *)&BUFIN, 0, SAMP_BLOCKSIZE);
memcpy((void *)&BUFIN, (void *)&ptMSG2, sizeof(ptMSG2));
fBUFIN = 1;
}
k_sem_give(&allforone);
k_sleep(K_MSEC(50));
}
}
/*
* CT waits for fBUFOUT = 1 then copies
* the message clears the flag and prints
*/
void ct(void)
{
char tbuf[60];
while (1) {
k_sem_take(&allforone, K_FOREVER);
if (fBUFOUT == 1) {
printk("CT Thread Receivedd Message\n");
memset((void *)&tbuf, 0, sizeof(tbuf));
memcpy((void *)&tbuf, (void *)BUFOUT, SAMP_BLOCKSIZE);
fBUFOUT = 0;
printk("CT MSG: %s\n", (char *)&tbuf);
}
k_sem_give(&allforone);
}
}