1 /*  main.c  */
2 
3 /*
4  *   SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /*
8  *
9  *  Basic example of userspace thread protected memory
10  *
11  *  NOTE: The encryption algorithm is unverified and
12  *  based on a 1930's era piece of hardware.
13  *  DO NOT USE THIS CODE FOR SECURITY
14  *
15  */
16 
17 #include <zephyr/sys/__assert.h>
18 #include <zephyr/sys/libc-hooks.h> /* for z_libc_partition */
19 
20 #include "main.h"
21 #include "enc.h"
22 /* the following definition name prefix is to avoid a conflict */
23 #define SAMP_BLOCKSIZE 50
24 
25 /*
26  *  The memory partitions have been named to simplify
27  * the definition of variables.  A possible alternative
28  * is using one source file per thread and implementing
29  * a objcopy to rename the data and bss section for the
30  * thread to the partition name.
31  */
32 
33 /* prepare the memory partition structures  */
34 FOR_EACH(K_APPMEM_PARTITION_DEFINE, (;), user_part, red_part, enc_part, blk_part, ct_part);
35 /* prepare the memory domain structures  */
36 struct k_mem_domain pt_domain, enc_domain;
37 /* each variable starts with a name defined in main.h
38  * the names are symbolic for the memory partitions
39  * purpose.
40  */
41 volatile _app_red_b BYTE fBUFIN;
42 volatile _app_red_b BYTE BUFIN[63];
43 
44 volatile _app_blk_b BYTE fBUFOUT;
45 volatile _app_blk_b BYTE BUFOUT[63];
46 
47 /* declare and set wheel and reflector  */
48 /* To use add definition ALTMSG */
49 #ifdef ALTMSG
50 volatile _app_enc_d BYTE W1[26] = START_WHEEL;
51 #else
52 volatile _app_enc_d BYTE W1[26] = START_WHEEL2;
53 #endif
54 volatile _app_enc_d BYTE W2[26] = START_WHEEL;
55 volatile _app_enc_d BYTE W3[26] = START_WHEEL;
56 volatile _app_enc_d BYTE R[26] = REFLECT;
57 
58 volatile _app_enc_b int IW1;
59 volatile _app_enc_b int IW2;
60 volatile _app_enc_b int IW3;
61 
62 /*
63  *   calculated by the enc thread at init and when the wheels
64  *   change.
65  */
66 volatile _app_enc_b BYTE W1R[26];
67 volatile _app_enc_b BYTE W2R[26];
68 volatile _app_enc_b BYTE W3R[26];
69 
70 /*
71  *	sync threads
72  */
73 K_SEM_DEFINE(allforone, 0, 3);
74 
75 struct k_thread enc_thread;
76 K_THREAD_STACK_DEFINE(enc_stack, STACKSIZE);
77 
78 struct k_thread pt_thread;
79 K_THREAD_STACK_DEFINE(pt_stack, STACKSIZE);
80 
81 struct k_thread ct_thread;
82 K_THREAD_STACK_DEFINE(ct_stack, STACKSIZE);
83 
84 _app_enc_d char encMSG[] = "ENC!\n";
85 volatile _app_enc_b char enc_pt[50];  /* Copy form shared pt */
86 volatile _app_enc_b char enc_ct[50]; /* Copy to shared ct */
87 
88 _app_user_d char ptMSG[] = "PT: message to encrypt\n";
89 
90 /* encrypted message when W1 = START_WHEEL */
91 /* to use add definition ALTMSG  */
92 #ifdef ALTMSG
93 _app_user_d char ptMSG2[] = "nfttbhfspfmdqzos\n";
94 #else
95 /* encrypted message when W1 = START_WHEEL2 */
96 _app_user_d char ptMSG2[] = "ofttbhfspgmeqzos\n";
97 #endif
98 _app_ct_d char ctMSG[] = "CT!\n";
99 
100 
101 
102 
main(void)103 int main(void)
104 {
105 	struct k_mem_partition *enc_parts[] = {
106 #if Z_LIBC_PARTITION_EXISTS
107 		&z_libc_partition,
108 #endif
109 		&enc_part, &red_part, &blk_part
110 	};
111 	struct k_mem_partition *pt_parts[] = {
112 #if Z_LIBC_PARTITION_EXISTS
113 		&z_libc_partition,
114 #endif
115 		&user_part, &red_part
116 	};
117 	k_tid_t tPT, tENC, tCT;
118 	int ret;
119 
120 	fBUFIN = 0; /* clear flags */
121 	fBUFOUT = 0;
122 	calc_rev_wheel((BYTE *) &W1, (BYTE *)&W1R);
123 	calc_rev_wheel((BYTE *) &W2, (BYTE *)&W2R);
124 	calc_rev_wheel((BYTE *) &W3, (BYTE *)&W3R);
125 	IW1 = 0;
126 	IW2 = 0;
127 	IW3 = 0;
128 
129 	k_thread_access_grant(k_current_get(), &allforone);
130 
131 	/*
132 	 * create an enc thread init the memory domain and add partitions
133 	 * then add the thread to the domain.
134 	 */
135 	tENC = k_thread_create(&enc_thread, enc_stack, STACKSIZE,
136 			(k_thread_entry_t)enc, NULL, NULL, NULL,
137 			-1, K_USER,
138 			K_FOREVER);
139 	k_thread_access_grant(tENC, &allforone);
140 	/* use K_FOREVER followed by k_thread_start*/
141 	printk("ENC Thread Created %p\n", tENC);
142 
143 	ret = k_mem_domain_init(&enc_domain, ARRAY_SIZE(enc_parts), enc_parts);
144 	__ASSERT(ret == 0, "k_mem_domain_init() on enc_domain failed %d", ret);
145 	ARG_UNUSED(ret);
146 
147 	printk("Partitions added to enc_domain\n");
148 	k_mem_domain_add_thread(&enc_domain, tENC);
149 	printk("enc_domain Created\n");
150 
151 
152 	tPT = k_thread_create(&pt_thread, pt_stack, STACKSIZE,
153 			(k_thread_entry_t)pt, NULL, NULL, NULL,
154 			-1, K_USER,
155 			K_FOREVER);
156 	k_thread_access_grant(tPT, &allforone);
157 	printk("PT Thread Created %p\n", tPT);
158 
159 	ret = k_mem_domain_init(&pt_domain, ARRAY_SIZE(pt_parts), pt_parts);
160 	__ASSERT(ret == 0, "k_mem_domain_init() on pt_domain failed %d", ret);
161 
162 	k_mem_domain_add_thread(&pt_domain, tPT);
163 	printk("pt_domain Created\n");
164 
165 	tCT = k_thread_create(&ct_thread, ct_stack, STACKSIZE,
166 			(k_thread_entry_t)ct, NULL, NULL, NULL,
167 			-1, K_USER,
168 			K_FOREVER);
169 	k_thread_access_grant(tCT, &allforone);
170 	printk("CT Thread Created %p\n", tCT);
171 	/* Re-using the default memory domain for CT */
172 	ret = k_mem_domain_add_partition(&k_mem_domain_default, &ct_part);
173 	if (ret != 0) {
174 		printk("Failed to add ct_part to mem domain (%d)\n", ret);
175 		k_oops();
176 	}
177 	printk("ct partitions installed\n");
178 
179 	ret = k_mem_domain_add_partition(&k_mem_domain_default, &blk_part);
180 	if (ret != 0) {
181 		printk("Failed to add blk_part to mem domain (%d)\n", ret);
182 		k_oops();
183 	}
184 	printk("blk partitions installed\n");
185 
186 	k_thread_start(&enc_thread);
187 	/* need to start all three threads.  let enc go first to perform init step */
188 
189 	printk("ENC thread started\n");
190 	k_thread_start(&pt_thread);
191 	printk("PT thread started\n");
192 
193 	k_thread_start(&ct_thread);
194 	k_sem_give(&allforone);
195 	printk("CT thread started\n");
196 	return 0;
197 }
198 
199 
200 
201 /*
202  * The enc thread.
203  * Function: initialize the simulation of the wheels.
204  * Copy memory from pt thread and encrypt to a local buffer
205  * then copy to the ct thread.
206  */
enc(void)207 void enc(void)
208 {
209 
210 	int index, index_out;
211 
212 	while (1) {
213 		k_sem_take(&allforone, K_FOREVER);
214 		if (fBUFIN == 1) { /* 1 is process text */
215 			printk("ENC Thread Received Data\n");
216 			/* copy message form shared mem and clear flag */
217 			memcpy((void *)&enc_pt, (void *)BUFIN, SAMP_BLOCKSIZE);
218 			printk("ENC PT MSG: %s\n", (char *)&enc_pt);
219 			fBUFIN = 0;
220 			/* reset wheel: probably better as a flag option  */
221 			IW1 = 7;
222 			IW2 = 2;
223 			IW3 = 3;
224 			/* encode */
225 			memset((void *)&enc_ct, 0, SAMP_BLOCKSIZE);
226 			for (index = 0, index_out = 0; index < SAMP_BLOCKSIZE; index++) {
227 				if (enc_pt[index] == '\0') {
228 					enc_ct[index_out] = '\0';
229 					break;
230 				}
231 				if (enc_pt[index] >= 'a' && enc_pt[index] <= 'z') {
232 					enc_ct[index_out] = (BYTE)enig_enc((BYTE) enc_pt[index]);
233 					index_out++;
234 				}
235 			}
236 			/* test for CT flag */
237 			while (fBUFOUT != 0) {
238 				k_sleep(K_MSEC(1));
239 			}
240 			/* ct thread has cleared the buffer */
241 			memcpy((void *)&BUFOUT, (void *)&enc_ct,
242 			       SAMP_BLOCKSIZE);
243 			fBUFOUT = 1;
244 
245 		}
246 		k_sem_give(&allforone);
247 	}
248 }
249 
250 /*
251  * the pt function pushes data to the enc thread.
252  * It can be extended to receive data from a serial port
253  * and pass the data to enc
254  */
pt(void)255 void pt(void)
256 {
257 
258 	k_sleep(K_MSEC(20));
259 	while (1) {
260 		k_sem_take(&allforone, K_FOREVER);
261 		if (fBUFIN == 0) { /* send message to encode */
262 			printk("\nPT Sending Message 1\n");
263 			memset((void *)&BUFIN, 0, SAMP_BLOCKSIZE);
264 			memcpy((void *)&BUFIN, (void *)&ptMSG, sizeof(ptMSG));
265 /* strlen should not be used if user provided data, needs a max length set  */
266 			fBUFIN = 1;
267 		}
268 		k_sem_give(&allforone);
269 		k_sem_take(&allforone, K_FOREVER);
270 		if (fBUFIN == 0) { /* send message to decode  */
271 			printk("\nPT Sending Message 1'\n");
272 			memset((void *)&BUFIN, 0, SAMP_BLOCKSIZE);
273 			memcpy((void *)&BUFIN, (void *)&ptMSG2, sizeof(ptMSG2));
274 			fBUFIN = 1;
275 		}
276 		k_sem_give(&allforone);
277 		k_sleep(K_MSEC(50));
278 	}
279 }
280 
281 /*
282  * CT waits for fBUFOUT = 1 then copies
283  * the message clears the flag and prints
284  */
ct(void)285 void ct(void)
286 {
287 
288 	char tbuf[60];
289 
290 	while (1) {
291 		k_sem_take(&allforone, K_FOREVER);
292 		if (fBUFOUT == 1) {
293 			printk("CT Thread Received Message\n");
294 			memset((void *)&tbuf, 0, sizeof(tbuf));
295 			memcpy((void *)&tbuf, (void *)BUFOUT, SAMP_BLOCKSIZE);
296 			fBUFOUT = 0;
297 			printk("CT MSG: %s\n", (char *)&tbuf);
298 		}
299 		k_sem_give(&allforone);
300 	}
301 }
302