-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdk_export.txt
More file actions
3337 lines (2872 loc) · 127 KB
/
Copy pathsdk_export.txt
File metadata and controls
3337 lines (2872 loc) · 127 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
======================================================================
FILE: sdk/libtoob/toob_cloud_submit.c
======================================================================
/**
* ==============================================================================
* Toob-Boot libtoob: Cloud-Command Submission (toob_cloud_submit.c)
* ==============================================================================
*
* REFERENCED SPECIFICATIONS:
* - docs/plan.md Phase 7 (OS-Boundary Cloud-Command Pipeline)
* - docs/libtoob_api.md (Zero-Dependency Isolation, WAL Append semantics)
* - docs/concept_fusion.md (TOCTOU-safe Flash Writes, Anti-Glitch defense)
*
* ARCHITECTURE:
* The Feature-OS receives Cloud-Command Envelopes (CBOR, Ed25519-signed) from
* the server and writes them into the CHIP_CLOUD_CMD_SLOT via this module.
* The Bootloader evaluates the slot autonomously at every boot (Step 2.5).
*
* Pipeline: Guard → Erase → Write → CRC Read-Back → Zeroize.
*/
#include "libtoob.h"
#ifdef TOOB_HOST_FUZZING
#include "libtoob_config_sandbox.h"
#else
#include "generated_boot_config.h"
#endif
#include "toob_internal.h"
#include <stddef.h>
#include <string.h>
/* ==============================================================================
* PUBLIC API: toob_submit_cloud_command
* ============================================================================== */
toob_status_t toob_submit_cloud_command(const uint8_t *envelope, uint32_t len) {
if (!envelope || len == 0 || len > CHIP_CLOUD_CMD_SLOT_SIZE) {
return TOOB_ERR_INVALID_ARG;
}
/* Step 1: Erase the Cloud-Command Slot */
toob_status_t erase_stat =
toob_os_flash_erase(CHIP_CLOUD_CMD_SLOT_ABS_ADDR, CHIP_CLOUD_CMD_SLOT_SIZE);
volatile uint32_t erase_shield_1 = 0, erase_shield_2 = 0;
if (erase_stat == TOOB_OK)
erase_shield_1 = TOOB_OK;
TOOB_GLITCH_DELAY();
if (erase_shield_1 == TOOB_OK && erase_stat == TOOB_OK)
erase_shield_2 = TOOB_OK;
if (erase_shield_1 != TOOB_OK || erase_shield_2 != TOOB_OK ||
erase_shield_1 != erase_shield_2) {
return (erase_stat != TOOB_OK) ? erase_stat : TOOB_ERR_FLASH;
}
/* Step 2: Write the Envelope */
toob_status_t write_stat =
toob_os_flash_write(CHIP_CLOUD_CMD_SLOT_ABS_ADDR, envelope, len);
if (write_stat != TOOB_OK) {
return write_stat;
}
/* Step 3: Phase-Bound Chunked Read-Back Verify.
* We verify in 64-byte chunks to avoid a 4KB stack allocation
* that would overflow typical RTOS thread stacks (4-8KB). */
{
uint8_t chunk_buf[64] __attribute__((aligned(8)));
uint32_t pos = 0;
while (pos < len) {
uint32_t remaining = len - pos;
uint32_t chunk_len = (remaining > 64U) ? 64U : remaining;
toob_secure_zeroize(chunk_buf, sizeof(chunk_buf));
toob_status_t read_stat = toob_os_flash_read(
CHIP_CLOUD_CMD_SLOT_ABS_ADDR + pos, chunk_buf, chunk_len);
if (read_stat != TOOB_OK) {
toob_secure_zeroize(chunk_buf, sizeof(chunk_buf));
return TOOB_ERR_FLASH_HW;
}
if (toob_ct_memcmp_glitch_safe(envelope + pos, chunk_buf,
chunk_len) != TOOB_OK) {
toob_secure_zeroize(chunk_buf, sizeof(chunk_buf));
return TOOB_ERR_FLASH_HW;
}
pos += chunk_len;
}
toob_secure_zeroize(chunk_buf, sizeof(chunk_buf));
}
return TOOB_OK;
}
/* ==============================================================================
* PUBLIC API: toob_get_device_id
* ============================================================================== */
toob_status_t toob_get_device_id(uint8_t *out_id, size_t id_len) {
if (!out_id || id_len < 32) {
return TOOB_ERR_INVALID_ARG;
}
toob_handoff_t handoff __attribute__((aligned(8)));
toob_secure_zeroize(&handoff, sizeof(handoff));
toob_status_t status = toob_get_handoff(&handoff);
if (status != TOOB_OK) {
toob_secure_zeroize(&handoff, sizeof(handoff));
return status;
}
memcpy(out_id, handoff.device_id, 32);
toob_secure_zeroize(&handoff, sizeof(handoff));
return TOOB_OK;
}
======================================================================
FILE: sdk/libtoob/toob_confirm.c
======================================================================
/**
* @file toob_confirm.c
* @brief Boot Confirmation — OS-side confirm/recovery intent via Mailbox.
*
* Implements toob_confirm_boot() and toob_recovery_resolved().
* Writes Mailbox records instead of WAL entries. The RTC fast-path
* remains for hardware with backup registers.
*/
#include "libtoob.h"
#ifdef TOOB_HOST_FUZZING
#include "libtoob_config_sandbox.h"
#else
#include "generated_boot_config.h"
#endif
#include "toob_internal.h"
#include <stddef.h>
#ifdef ADDR_CONFIRM_RTC_RAM
uint64_t mock_rtc_ram = 0;
#endif
toob_status_t toob_confirm_boot(void) {
toob_handoff_t handoff __attribute__((aligned(8)));
toob_secure_zeroize(&handoff, sizeof(handoff));
toob_status_t status = toob_get_handoff(&handoff);
volatile uint32_t get_shield_1 = 0, get_shield_2 = 0;
if (status == TOOB_OK)
get_shield_1 = TOOB_OK;
TOOB_GLITCH_DELAY();
if (get_shield_1 == TOOB_OK && status == TOOB_OK)
get_shield_2 = TOOB_OK;
if (get_shield_1 != TOOB_OK || get_shield_2 != TOOB_OK ||
get_shield_1 != get_shield_2) {
toob_secure_zeroize(&handoff, sizeof(handoff));
return (status != TOOB_OK) ? status : TOOB_ERR_VERIFY;
}
#ifdef ADDR_CONFIRM_RTC_RAM
/* RTC fast-path: write nonce to backup register + mailbox. */
volatile uint64_t *rtc_ptr = (volatile uint64_t *)ADDR_CONFIRM_RTC_RAM;
*rtc_ptr = handoff.boot_nonce;
#if defined(__GNUC__) || defined(__clang__)
__sync_synchronize();
#endif
uint64_t verified_nonce = *rtc_ptr;
volatile uint32_t rtc_shield_1 = 0, rtc_shield_2 = 0;
if (verified_nonce == handoff.boot_nonce)
rtc_shield_1 = TOOB_OK;
TOOB_GLITCH_DELAY();
if (rtc_shield_1 == TOOB_OK && verified_nonce == handoff.boot_nonce)
rtc_shield_2 = TOOB_OK;
/* Mailbox write (fire-and-forget alongside RTC). */
(void)toob_mailbox_set_confirm(handoff.boot_nonce);
toob_secure_zeroize(&handoff, sizeof(handoff));
if (rtc_shield_1 != TOOB_OK || rtc_shield_2 != TOOB_OK ||
rtc_shield_1 != rtc_shield_2) {
return TOOB_ERR_FLASH_HW;
}
return TOOB_OK;
#else
/* Mailbox-only path. */
toob_status_t mb_stat = toob_mailbox_set_confirm(handoff.boot_nonce);
volatile uint32_t mb_shield_1 = 0, mb_shield_2 = 0;
if (mb_stat == TOOB_OK)
mb_shield_1 = TOOB_OK;
TOOB_GLITCH_DELAY();
if (mb_shield_1 == TOOB_OK && mb_stat == TOOB_OK)
mb_shield_2 = TOOB_OK;
toob_secure_zeroize(&handoff, sizeof(handoff));
if (mb_shield_1 == TOOB_OK && mb_shield_2 == TOOB_OK &&
mb_shield_1 == mb_shield_2) {
return TOOB_OK;
}
return (mb_stat != TOOB_OK) ? mb_stat : TOOB_ERR_FLASH_HW;
#endif
}
toob_status_t toob_recovery_resolved(void) {
toob_status_t status = toob_mailbox_set_recovery_resolved();
volatile uint32_t shield_1 = 0, shield_2 = 0;
if (status == TOOB_OK)
shield_1 = TOOB_OK;
TOOB_GLITCH_DELAY();
if (shield_1 == TOOB_OK && status == TOOB_OK)
shield_2 = TOOB_OK;
if (shield_1 == TOOB_OK && shield_2 == TOOB_OK && shield_1 == shield_2)
return TOOB_OK;
return (status != TOOB_OK) ? status : TOOB_ERR_FLASH_HW;
}
======================================================================
FILE: sdk/libtoob/toob_crc32.c
======================================================================
#include "toob_internal.h"
#include "toob_crc32_contract.h"
/* IEEE 802.3 CRC-32 — 1KB lookup table variant for OS-side throughput.
* Core (boot_crc32.c) uses a table-less byte-at-a-time variant for minimal BSS.
* Both produce identical results per toob_crc32_contract.h. */
static const uint32_t toob_crc32_table[256] = {
0x00000000, 0x77073096, 0xEE0E612C, 0x990951BA, 0x076DC419, 0x706AF48F,
0xE963A535, 0x9E6495A3, 0x0EDB8832, 0x79DCB8A4, 0xE0D5E91E, 0x97D2D988,
0x09B64C2B, 0x7EB17CBD, 0xE7B82D07, 0x90BF1D91, 0x1DB71064, 0x6AB020F2,
0xF3B97148, 0x84BE41DE, 0x1ADAD47D, 0x6DDDE4EB, 0xF4D4B551, 0x83D385C7,
0x136C9856, 0x646BA8C0, 0xFD62F97A, 0x8A65C9EC, 0x14015C4F, 0x63066CD9,
0xFA0F3D63, 0x8D080DF5, 0x3B6E20C8, 0x4C69105E, 0xD56041E4, 0xA2677172,
0x3C03E4D1, 0x4B04D447, 0xD20D85FD, 0xA50AB56B, 0x35B5A8FA, 0x42B2986C,
0xDBBBC9D6, 0xACBCF940, 0x32D86CE3, 0x45DF5C75, 0xDCD60DCF, 0xABD13D59,
0x26D930AC, 0x51DE003A, 0xC8D75180, 0xBFD06116, 0x21B4F4B5, 0x56B3C423,
0xCFBA9599, 0xB8BDA50F, 0x2802B89E, 0x5F058808, 0xC60CD9B2, 0xB10BE924,
0x2F6F7C87, 0x58684C11, 0xC1611DAB, 0xB6662D3D, 0x76DC4190, 0x01DB7106,
0x98D220BC, 0xEFD5102A, 0x71B18589, 0x06B6B51F, 0x9FBFE4A5, 0xE8B8D433,
0x7807C9A2, 0x0F00F934, 0x9609A88E, 0xE10E9818, 0x7F6A0DBB, 0x086D3D2D,
0x91646C97, 0xE6635C01, 0x6B6B51F4, 0x1C6C6162, 0x856530D8, 0xF262004E,
0x6C0695ED, 0x1B01A57B, 0x8208F4C1, 0xF50FC457, 0x65B0D9C6, 0x12B7E950,
0x8BBEB8EA, 0xFCB9887C, 0x62DD1DDF, 0x15DA2D49, 0x8CD37CF3, 0xFBD44C65,
0x4DB26158, 0x3AB551CE, 0xA3BC0074, 0xD4BB30E2, 0x4ADFA541, 0x3DD895D7,
0xA4D1C46D, 0xD3D6F4FB, 0x4369E96A, 0x346ED9FC, 0xAD678846, 0xDA60B8D0,
0x44042D73, 0x33031DE5, 0xAA0A4C5F, 0xDD0D7CC9, 0x5005713C, 0x270241AA,
0xBE0B1010, 0xC90C2086, 0x5768B525, 0x206F85B3, 0xB966D409, 0xCE61E49F,
0x5EDEF90E, 0x29D9C998, 0xB0D09822, 0xC7D7A8B4, 0x59B33D17, 0x2EB40D81,
0xB7BD5C3B, 0xC0BA6CAD, 0xEDB88320, 0x9ABFB3B6, 0x03B6E20C, 0x74B1D29A,
0xEAD54739, 0x9DD277AF, 0x04DB2615, 0x73DC1683, 0xE3630B12, 0x94643B84,
0x0D6D6A3E, 0x7A6A5AA8, 0xE40ECF0B, 0x9309FF9D, 0x0A00AE27, 0x7D079EB1,
0xF00F9344, 0x8708A3D2, 0x1E01F268, 0x6906C2FE, 0xF762575D, 0x806567CB,
0x196C3671, 0x6E6B06E7, 0xFED41B76, 0x89D32BE0, 0x10DA7A5A, 0x67DD4ACC,
0xF9B9DF6F, 0x8EBEEFF9, 0x17B7BE43, 0x60B08ED5, 0xD6D6A3E8, 0xA1D1937E,
0x38D8C2C4, 0x4FDFF252, 0xD1BB67F1, 0xA6BC5767, 0x3FB506DD, 0x48B2364B,
0xD80D2BDA, 0xAF0A1B4C, 0x36034AF6, 0x41047A60, 0xDF60EFC3, 0xA867DF55,
0x316E8EEF, 0x4669BE79, 0xCB61B38C, 0xBC66831A, 0x256FD2A0, 0x5268E236,
0xCC0C7795, 0xBB0B4703, 0x220216B9, 0x5505262F, 0xC5BA3BBE, 0xB2BD0B28,
0x2BB45A92, 0x5CB36A04, 0xC2D7FFA7, 0xB5D0CF31, 0x2CD99E8B, 0x5BDEAE1D,
0x9B64C2B0, 0xEC63F226, 0x756AA39C, 0x026D930A, 0x9C0906A9, 0xEB0E363F,
0x72076785, 0x05005713, 0x95BF4A82, 0xE2B87A14, 0x7BB12BAE, 0x0CB61B38,
0x92D28E9B, 0xE5D5BE0D, 0x7CDCEFB7, 0x0BDBDF21, 0x86D3D2D4, 0xF1D4E242,
0x68DDB3F8, 0x1FDA836E, 0x81BE16CD, 0xF6B9265B, 0x6FB077E1, 0x18B74777,
0x88085AE6, 0xFF0F6A70, 0x66063BCA, 0x11010B5C, 0x8F659EFF, 0xF862AE69,
0x616BFFD3, 0x166CCF45, 0xA00AE278, 0xD70DD2EE, 0x4E048354, 0x3903B3C2,
0xA7672661, 0xD06016F7, 0x4969474D, 0x3E6E77DB, 0xAED16A4A, 0xD9D65ADC,
0x40DF0B66, 0x37D83BF0, 0xA9BCAE53, 0xDEBB9EC5, 0x47B2CF7F, 0x30B5FFE9,
0xBDBDF21C, 0xCABAC28A, 0x53B39330, 0x24B4A3A6, 0xBAD03605, 0xCDD70693,
0x54DE5729, 0x23D967BF, 0xB3667A2E, 0xC4614AB8, 0x5D681B02, 0x2A6F2B94,
0xB40BBE37, 0xC30C8EA1, 0x5A05DF1B, 0x2D02EF8D
};
/* table[128] == poly proves the table was generated from the contract polynomial */
_Static_assert(sizeof(toob_crc32_table) / sizeof(toob_crc32_table[0]) == 256,
"CRC-32 table must contain exactly 256 entries");
uint32_t toob_lib_crc32(const uint8_t *data, size_t length) {
/* No NULL-guard: a page fault on NULL is the correct failure mode.
* Returning 0 would mimic a valid empty-buffer CRC and mask caller bugs. */
uint32_t crc = TOOB_CRC32_INIT;
for (size_t i = 0; i < length; i++) {
crc = toob_crc32_table[(crc ^ data[i]) & 0xFF] ^ (crc >> 8);
}
return crc ^ TOOB_CRC32_FINAL_XOR;
}
======================================================================
FILE: sdk/libtoob/toob_diag.c
======================================================================
/**
* @file toob_diag.c
* @brief Boot Diagnostics — telemetry extraction and diag struct management.
*
* Extracts toob_boot_diag_t from .noinit shared RAM with TOCTOU defense,
* CRC-32 validation, and CBOR telemetry encoding for cloud submission.
*/
#include "libtoob.h"
#include "toob_internal.h"
#include "toob_telemetry_encode.h"
#include <stddef.h>
#include <string.h>
/* ==============================================================================
* .noinit RAM Definition (GAP-39 / Diagnostics)
* ============================================================================== */
#ifndef TOOB_MOCK_TEST
TOOB_NOINIT toob_boot_diag_t toob_diag_state;
#endif
/* ==============================================================================
* PUBLIC API IMPLEMENTATION
* ==============================================================================
*/
toob_status_t toob_get_boot_diag(toob_boot_diag_t *diag) {
/* P10 Pointer Zero-Trust Check */
if (!diag) {
return TOOB_ERR_INVALID_ARG;
}
/* ====================================================================
* P10 LEAKAGE DEFENSE & MEMORY SANITIZATION
* ====================================================================
* Präemptive Nullifizierung des Ziel-Speichers. Verhindert, dass das OS
* nach einem TOOB_ERR_VERIFY auf Garbage/Secrets aus seinem eigenen
* uninitialisierten Thread-Stack zugreift (Memory Disclosure Attack).
* Da 'diag' den Scope überlebt, ist dieser Aufruf durch C-Compiler
* niemals als Dead Code eliminierbar (DCE-sicher).
*/
toob_secure_zeroize(diag, sizeof(toob_boot_diag_t));
/* ====================================================================
* TOCTOU SHIELDING (Time-Of-Check to Time-Of-Use)
* ====================================================================
* Zieht das asynchrone .noinit Struct in den lokalen, sicheren C-Stack.
* Das verhindert asynchrone Manipulationen (z.B. durch bösartiges DMA oder
* OS-Interrupts), die das RAM zwischen Verifikation und Nutzung verändern.
*/
toob_boot_diag_t local_diag __attribute__((aligned(8)));
toob_secure_zeroize(&local_diag,
sizeof(local_diag)); /* P10 Uninitialized Mem Trap */
/* Atomarer 1-Way Read aus dem Shared-RAM */
memcpy(&local_diag, &toob_diag_state, sizeof(toob_boot_diag_t));
/* ====================================================================
* MAGIC & ABI-VERSION CHECK (Glitch-Shielded)
* ==================================================================== */
volatile uint32_t version_shield_1 = 0;
volatile uint32_t version_shield_2 = 0;
bool is_version_ok = (local_diag.struct_version == TOOB_DIAG_STRUCT_VERSION);
if (is_version_ok) {
version_shield_1 = TOOB_OK;
}
TOOB_GLITCH_DELAY();
if (version_shield_1 == TOOB_OK && is_version_ok) {
version_shield_2 = TOOB_OK;
}
if (version_shield_1 != TOOB_OK || version_shield_2 != TOOB_OK ||
version_shield_1 != version_shield_2) {
toob_secure_zeroize(&local_diag, sizeof(local_diag));
return TOOB_ERR_VERIFY; /* Trapped Version Mismatch or Glitch */
}
/* ====================================================================
* P10 CRC-32 PAYLOAD VERIFICATION (Glitch-Shielded)
* ====================================================================
* P10 Robustness: CRC-32 Validation relies dynamically on offsetof,
* mitigating ABI tail-padding drifts (Padding bytes sind undefiniert!).
*/
size_t payload_len = offsetof(toob_boot_diag_t, crc32_trailer);
uint32_t calculated_crc =
toob_lib_crc32((const uint8_t *)&local_diag, payload_len);
volatile uint32_t crc_shield_1 = 0;
volatile uint32_t crc_shield_2 = 0;
bool is_crc_ok = (calculated_crc == local_diag.crc32_trailer);
if (is_crc_ok) {
crc_shield_1 = TOOB_OK;
}
TOOB_GLITCH_DELAY();
if (crc_shield_1 == TOOB_OK && calculated_crc == local_diag.crc32_trailer) {
crc_shield_2 = TOOB_OK;
}
/* P10 Leakage Defense: Wenn CRC fehlschlägt, den Klon SOFORT vernichten! */
if (crc_shield_1 != TOOB_OK || crc_shield_2 != TOOB_OK ||
crc_shield_1 != crc_shield_2) {
toob_secure_zeroize(&local_diag, sizeof(local_diag));
return TOOB_ERR_VERIFY; /* Trapped Hardware Bit-Rot or Glitch */
}
/* ====================================================================
* SAFE-PASSING (By-Value Copy) & P10 PADDING ZEROIZATION
* ==================================================================== */
/* P10 Zero-Trust: Sicherstellen, dass das explizite 3-Byte Padding genullt
* ist. Das verhindert subtiles Data-Leakage von Stack-Resten bei einer
* nachgelagerten CBOR Serialisierung durch das Betriebssystem (EU CRA
* Export). */
local_diag._padding[0] = 0;
local_diag._padding[1] = 0;
local_diag._padding[2] = 0;
/* Sicheres Kopieren des 100% verifizierten Klons in das Feature-OS RAM */
memcpy(diag, &local_diag, sizeof(toob_boot_diag_t));
/* O(1) Stack Clean-Up (DCE Proof) */
toob_secure_zeroize(&local_diag, sizeof(local_diag));
return TOOB_OK;
}
toob_status_t toob_get_boot_diag_cbor(uint8_t *out_buf, size_t max_len, size_t *out_len) {
if (!out_buf || !out_len) {
return TOOB_ERR_INVALID_ARG;
}
toob_boot_diag_t diag;
toob_status_t status = toob_get_boot_diag(&diag);
if (status != TOOB_OK) {
return status;
}
struct toob_telemetry tel;
toob_secure_zeroize(&tel, sizeof(tel));
/* P10 Fix: Sicherer Shift des 32-bit Magic Headers in die 8-Bit CDDL Schema-Version */
tel.toob_telemetry_uint0uint = (uint8_t)(diag.struct_version >> 24);
tel.toob_telemetry_uint1uint = diag.boot_duration_ms;
tel.toob_telemetry_uint2uint = diag.edge_recovery_events;
tel.toob_telemetry_uint3uint = diag.hardware_fault_record;
tel.toob_telemetry_uint4uint = diag.vendor_error;
tel.toob_telemetry_uint5uint = diag.wdt_kicks;
tel.toob_telemetry_uint6uint = diag.current_svn;
tel.toob_telemetry_uint7uint = (uint8_t)diag.active_key_index;
tel.toob_telemetry_uint8bool = (diag.fallback_occurred != 0);
tel.toob_telemetry_uint9bstr.value = diag.sbom_digest;
tel.toob_telemetry_uint9bstr.len = sizeof(diag.sbom_digest);
tel.toob_telemetry_uint11uint = diag.boot_session_id;
/* P10 Fix: Akkurates C-Struct Mapping auf das CDDL Element */
if (diag.ext_health_present) {
tel.toob_telemetry_ext_health_m_present = true;
tel.toob_telemetry_ext_health_m.toob_telemetry_ext_health_m.ext_health_uint101uint = diag.ext_health.wal_erase_count;
tel.toob_telemetry_ext_health_m.toob_telemetry_ext_health_m.ext_health_uint102uint = diag.ext_health.app_slot_erase_count;
tel.toob_telemetry_ext_health_m.toob_telemetry_ext_health_m.ext_health_uint103uint = diag.ext_health.staging_slot_erase_count;
tel.toob_telemetry_ext_health_m.toob_telemetry_ext_health_m.ext_health_uint104uint = diag.ext_health.swap_buffer_erase_count;
} else {
tel.toob_telemetry_ext_health_m_present = false;
}
bool encoded = cbor_encode_toob_telemetry(out_buf, max_len, &tel, out_len);
toob_secure_zeroize(&tel, sizeof(tel));
toob_secure_zeroize(&diag, sizeof(diag));
if (!encoded) {
return TOOB_ERR_VERIFY;
}
return TOOB_OK;
}
======================================================================
FILE: sdk/libtoob/toob_handoff.c
======================================================================
/**
* @file toob_handoff.c
* @brief Handoff Reader — reads bootloader diagnostic state from shared RAM.
*
* Validates and extracts the toob_handoff_t struct from the .noinit RAM
* section. CRC-32 + magic verification with TOCTOU defense (local copy)
* and glitch-resistant double-check patterns.
*/
#include "libtoob.h"
#include "toob_internal.h"
#include <string.h>
/* ==============================================================================
* .noinit RAM Definition (GAP-39)
* (Wird durch den Linker in die uninitialisierte Sektion gemappt)
*
* Arch-Note (Zero-Dependency): This library defines the OS-side boundary.
* The Bootloader (S1) uses its own isolated definition. Since the OS and S1
* are compiled into strictly separate binaries, there are no linker collisions.
* ==============================================================================
*/
/* Arch-Note: Für Cross-Compilation Linker-Collisions (Unit Tests) */
#ifndef TOOB_MOCK_TEST
TOOB_NOINIT toob_handoff_t toob_handoff_state;
#endif
toob_status_t toob_validate_handoff(void) {
/* ====================================================================
* 1. MAGIC & ABI-VERSION CHECK (Glitch-Shielded)
* ====================================================================
* LOGIK-FIX: Das OS MUSS TENTATIVE Boots zulassen, um sie via
* toob_confirm_boot() in COMMITTED wandeln zu können!
*/
volatile uint32_t shield_1 = 0;
volatile uint32_t shield_2 = 0;
bool is_magic_ok = (toob_handoff_state.magic == TOOB_STATE_COMMITTED ||
toob_handoff_state.magic == TOOB_STATE_TENTATIVE);
bool is_version_ok =
(toob_handoff_state.struct_version == TOOB_HANDOFF_STRUCT_VERSION);
if (is_magic_ok && is_version_ok) {
shield_1 = TOOB_OK;
}
TOOB_GLITCH_DELAY();
if (shield_1 == TOOB_OK && (is_magic_ok && is_version_ok)) {
shield_2 = TOOB_OK;
}
if (shield_1 != TOOB_OK || shield_2 != TOOB_OK || shield_1 != shield_2) {
return TOOB_ERR_VERIFY;
}
/* ====================================================================
* 2. P10 CRC-32 PAYLOAD VERIFICATION
* ====================================================================
* Robustness: Berechne CRC per offsetof statt sizeof zwecks Tail-Padding
* Immunität
*/
size_t payload_len = offsetof(toob_handoff_t, crc32_trailer);
uint32_t calculated_crc =
toob_lib_crc32((const uint8_t *)&toob_handoff_state, payload_len);
volatile uint32_t crc_shield_1 = 0;
volatile uint32_t crc_shield_2 = 0;
if (calculated_crc == toob_handoff_state.crc32_trailer) {
crc_shield_1 = TOOB_OK;
}
TOOB_GLITCH_DELAY();
if (crc_shield_1 == TOOB_OK &&
calculated_crc == toob_handoff_state.crc32_trailer) {
crc_shield_2 = TOOB_OK;
}
if (crc_shield_1 != TOOB_OK || crc_shield_2 != TOOB_OK ||
crc_shield_1 != crc_shield_2) {
return TOOB_ERR_VERIFY;
}
return TOOB_OK;
}
toob_status_t toob_get_handoff(toob_handoff_t *out_handoff) {
/* P10 Pointer Zero-Trust Check */
if (!out_handoff) {
return TOOB_ERR_INVALID_ARG;
}
/* P10 Leakage Defense: Zielspeicher sofort nullen.
* Verhindert, dass das Betriebssystem bei einem Boot-Fail auf veralteten
* Stack-Garbage (Memory Leakage) zugreift, falls es den Return-Status
* fahrlässig ignoriert! Da 'out_handoff' den Scope überlebt, ist dieser
* memset() durch C-Compiler niemals als Dead Code eliminierbar (DCE-sicher).
*/
memset(out_handoff, 0, sizeof(toob_handoff_t));
/* Strikte Garbage-Abwehr: Wenn CRC nicht stimmt, Vorgang abbrechen! */
toob_status_t status = toob_validate_handoff();
/* Glitch-Schild für die Error-Propagation:
* Verhindert, dass ein fehlerhafter Status (z.B. TOOB_ERR_VERIFY)
* durch EMFI in der Register-Zuweisung zu einem TOOB_OK geflippt wird.
*/
volatile uint32_t prop_shield_1 = 0;
volatile uint32_t prop_shield_2 = 0;
if (status == TOOB_OK) {
prop_shield_1 = TOOB_OK;
}
TOOB_GLITCH_DELAY();
if (prop_shield_1 == TOOB_OK && status == TOOB_OK) {
prop_shield_2 = TOOB_OK;
}
if (prop_shield_1 != TOOB_OK || prop_shield_2 != TOOB_OK ||
prop_shield_1 != prop_shield_2) {
/* Force Error-Propagation mask, preventing logic manipulation */
return (status != TOOB_OK) ? status : TOOB_ERR_VERIFY;
}
/* Sicheres Kopieren by-value ins isolierte OS-Feature-RAM */
memcpy(out_handoff, &toob_handoff_state, sizeof(toob_handoff_t));
/* ====================================================================
* Post-Copy Re-Validation (TOCTOU Defense)
* ====================================================================
* In Multithreading/Interrupt-lastigen RTOS Umgebungen könnte der asynchrone
* .noinit RAM zwischen der Validierung und dem memcpy() von außen korrumpiert
* worden sein (Time-of-Check to Time-of-Use). Wir verifizieren den isolierten
* Klon mathematisch, bevor das OS ihn nutzt!
*/
size_t payload_len = offsetof(toob_handoff_t, crc32_trailer);
uint32_t local_crc =
toob_lib_crc32((const uint8_t *)out_handoff, payload_len);
bool local_magic_ok = (out_handoff->magic == TOOB_STATE_COMMITTED ||
out_handoff->magic == TOOB_STATE_TENTATIVE);
bool local_version_ok =
(out_handoff->struct_version == TOOB_HANDOFF_STRUCT_VERSION);
if (local_crc != out_handoff->crc32_trailer || !local_magic_ok ||
!local_version_ok) {
/* Zeiger-Inhalt radikal radieren, damit das Feature-OS niemals Garbage
* liest! */
memset(out_handoff, 0, sizeof(toob_handoff_t));
return TOOB_ERR_VERIFY;
}
/* P10 Zero-Trust: Sicherstellen, dass das Struct-Padding im OS-Heap
* genullt ist (Verhindert subtiles Data-Leakage bei Serialisierung). */
return TOOB_OK;
}
======================================================================
FILE: sdk/libtoob/toob_internal.h
======================================================================
#ifndef TOOB_INTERNAL_H
#define TOOB_INTERNAL_H
/**
* ==============================================================================
* Toob-Boot libtoob: Internal Utilities (toob_internal.h)
* ==============================================================================
*
* OS-side equivalent of the Bootloader's utility headers:
* - boot_secure_zeroize.h → toob_secure_zeroize()
* - boot_ct_utils.h → toob_ct_memcmp_glitch_safe()
* - boot_types.h (GLITCH) → TOOB_GLITCH_DELAY()
* - boot_crc32.h → toob_lib_crc32() (contract: toob_crc32_contract.h)
*
* ARCHITECTURE: libtoob must NEVER include bootloader-internal headers.
* These are independent re-implementations that maintain Zero-Dependency
* isolation while sharing the same security contract.
*/
#include "libtoob_types.h"
#include <stdint.h>
#include <stddef.h>
/* ==============================================================================
* 1. Cross-Compiler Glitch-Delay Injection (Fault-Injection Defense)
*
* OS-side equivalent of BOOT_GLITCH_DELAY() from boot_types.h.
* Inserts a NOP sled to resist voltage/clock faults that skip branch checks.
* ============================================================================== */
#if defined(__GNUC__) || defined(__clang__)
#define TOOB_GLITCH_DELAY() __asm__ volatile("nop; nop; nop;" ::: "memory")
#elif defined(__ICCARM__)
#include <intrinsics.h>
#define TOOB_GLITCH_DELAY() \
do { \
__no_operation(); \
__no_operation(); \
__no_operation(); \
} while (0)
#elif defined(__CC_ARM) || defined(__ARMCC_VERSION)
#define TOOB_GLITCH_DELAY() \
do { \
__nop(); \
__nop(); \
__nop(); \
} while (0)
#else
#define TOOB_GLITCH_DELAY() \
do { \
volatile uint32_t _delay = 0; \
_delay = 1; \
(void)_delay; \
} while (0)
#endif
/* ==============================================================================
* 2. Secure Memory Zeroization (Anti-DCE)
*
* ABI-CONTRACT: Intentionally separate implementations exist:
* Core: Assembly (boot_secure_zeroize.S) — DCE-proof by ISA guarantee.
* OS: volatile + compiler barrier (below) — sufficient for RTOS guest
* context where the compiler is not cross-TU aware.
* Both MUST zero exactly len bytes at ptr. Neither may return before completion.
* ============================================================================== */
static inline void toob_secure_zeroize(void *ptr, size_t len) {
volatile uint8_t *p = (volatile uint8_t *)ptr;
while (len--) {
*p++ = 0;
}
#if defined(__GNUC__) || defined(__clang__)
__asm__ volatile("" : : "g"(ptr) : "memory");
#endif
}
/* ==============================================================================
* 3. Constant-Time Comparison (Glitch-Protected)
*
* OS-side equivalent of constant_time_memcmp_glitch_safe() from boot_ct_utils.h.
* Dual-accumulator (forward + reverse) with double-check gating.
*
* SECURITY CONTRACT:
* - No early exit on first mismatch (timing side-channel safe)
* - len=0 is rejected (prevents glitched-length bypass)
* - Double-check via TOOB_GLITCH_DELAY() resists voltage faults
* ============================================================================== */
static inline toob_status_t toob_ct_memcmp_glitch_safe(const uint8_t *a,
const uint8_t *b,
size_t len) {
if (len == 0)
return TOOB_ERR_VERIFY;
volatile uint32_t acc_fwd = 0;
volatile uint32_t acc_rev = 0;
for (size_t i = 0; i < len; i++) {
acc_fwd |= (uint32_t)(a[i] ^ b[i]);
acc_rev |= (uint32_t)(a[len - 1 - i] ^ b[len - 1 - i]);
}
volatile uint32_t shield_1 = 0, shield_2 = 0;
if (acc_fwd == 0)
shield_1 = TOOB_OK;
TOOB_GLITCH_DELAY();
if (shield_1 == TOOB_OK && acc_rev == 0)
shield_2 = TOOB_OK;
if (shield_1 == TOOB_OK && shield_2 == TOOB_OK && shield_1 == shield_2)
return TOOB_OK;
return TOOB_ERR_VERIFY;
}
/* ==============================================================================
* 4. CRC-32 (IEEE 802.3)
*
* OS-side equivalent of compute_boot_crc32() from boot_crc32.h.
* Implementation lives in toob_crc32.c (not inlined due to table size).
* ============================================================================== */
uint32_t toob_lib_crc32(const uint8_t *data, size_t length);
#include "toob_mailbox.h"
/* Private SDK Linkage: Hidden from public API to prevent unvalidated memory access */
extern TOOB_NOINIT toob_handoff_t toob_handoff_state;
#endif /* TOOB_INTERNAL_H */
======================================================================
FILE: sdk/libtoob/toob_mailbox.c
======================================================================
/**
* @file toob_mailbox.c
* @brief OS-side Mailbox writer (two-sector A/B, torn-write safe).
*
* Replaces the previous erase-both-then-write-slot-0 design, which defeated
* double-buffering: a torn write wiped the only surviving record and reset the
* sequence, causing the Core to silently ignore updates (seq <= last_consumed).
*
* This implementation writes only the STALE slot and erases only that slot's
* sector, so:
* - at every instant at least one slot holds a valid, complete record, and
* - `seq` is strictly monotonic across brownouts (it is derived from the
* surviving good slot, which a torn write never touches).
*
* The Core reader mirrors the slot scan below: read all slots, take the valid
* one with the highest `seq`.
*/
#include "toob_mailbox.h"
#include "libtoob.h"
#ifdef TOOB_HOST_FUZZING
#include "libtoob_config_sandbox.h"
#else
#include "generated_boot_config.h"
#endif
#include "toob_internal.h"
#include <stddef.h>
/* Geometry sanity: the writer erases exactly one slot sector at a time, so the
* mailbox region must be TOOB_MAILBOX_SLOTS distinct erase sectors. */
_Static_assert(CHIP_MAILBOX_SIZE == TOOB_MAILBOX_SLOTS * CHIP_MAILBOX_SLOT_SIZE,
"mailbox region must be exactly TOOB_MAILBOX_SLOTS slot sectors");
_Static_assert(sizeof(toob_mailbox_t) <= CHIP_MAILBOX_SLOT_SIZE,
"mailbox record must fit in one slot sector");
_Static_assert(sizeof(toob_mailbox_t) % CHIP_FLASH_WRITE_ALIGNMENT == 0,
"mailbox record must be a multiple of the flash write alignment");
static inline uint32_t slot_addr(uint32_t i) {
return CHIP_MAILBOX_ABS_ADDR + i * CHIP_MAILBOX_SLOT_SIZE;
}
/**
* @brief Read one slot and validate magic + CRC.
* @return true and fills *out if the slot holds a complete valid record.
*/
static bool slot_read_valid(uint32_t i, toob_mailbox_t *out) {
toob_secure_zeroize(out, sizeof(*out));
if (toob_os_flash_read(slot_addr(i), (uint8_t *)out, sizeof(*out)) != TOOB_OK) {
return false;
}
if (out->magic != TOOB_MAILBOX_MAGIC) {
return false;
}
uint32_t expect = toob_lib_crc32((const uint8_t *)out, TOOB_MAILBOX_CRC_LEN);
return expect == out->crc32;
}
/**
* @brief Write a request into the stale slot with seq = best_seq + 1.
*
* Sequence: scan slots -> pick target (oldest / invalid) and best_seq ->
* build record -> erase target sector -> write -> glitch-safe read-back verify.
*/
static toob_status_t mailbox_put(toob_req_t req, uint32_t tbm1_addr, uint64_t nonce) {
toob_mailbox_t rec;
uint32_t best_seq = 0;
uint32_t target = 0;
uint32_t target_seq = 0xFFFFFFFFu; /* pick the slot with the LOWEST seq (invalid = 0) */
for (uint32_t i = 0; i < TOOB_MAILBOX_SLOTS; i++) {
bool ok = slot_read_valid(i, &rec);
uint32_t seq = ok ? rec.seq : 0u; /* an invalid slot is treated as the oldest */
if (ok && rec.seq > best_seq) {
best_seq = rec.seq;
}
if (seq < target_seq) {
target_seq = seq;
target = i;
}
toob_secure_zeroize(&rec, sizeof(rec)); /* slot may carry a CONFIRM nonce */
}
/* target is now the slot NOT holding the freshest record; best_seq is preserved
* there and is never erased below. NOTE: seq is uint32 and single-writer; a wrap
* needs 2^32 successful writes (unreachable in device lifetime). */
/* Build the new record (zeroize first: sets _reserved and any pad to 0). */
toob_mailbox_t m;
toob_secure_zeroize(&m, sizeof(m));
m.magic = TOOB_MAILBOX_MAGIC;
m.version = TOOB_MAILBOX_VERSION;
m.request = (uint16_t)req;
m.seq = best_seq + 1u;
m.tbm1_addr = tbm1_addr;
m.nonce = nonce;
m.crc32 = toob_lib_crc32((const uint8_t *)&m, TOOB_MAILBOX_CRC_LEN);
/* Erase ONLY the target slot's sector. The freshest good slot stays intact. */
toob_status_t status = toob_os_flash_erase(slot_addr(target), CHIP_MAILBOX_SLOT_SIZE);
if (status != TOOB_OK) {
toob_secure_zeroize(&m, sizeof(m));
return status;
}
status = toob_os_flash_write(slot_addr(target), (const uint8_t *)&m, sizeof(m));
if (status != TOOB_OK) {
toob_secure_zeroize(&m, sizeof(m));
return status;
}
/* Phase-bound read-back verify (glitch-safe, ghost-match-proof). */
toob_mailbox_t verify;
toob_secure_zeroize(&verify, sizeof(verify));
status = toob_os_flash_read(slot_addr(target), (uint8_t *)&verify, sizeof(verify));
if (status != TOOB_OK ||
toob_ct_memcmp_glitch_safe((const uint8_t *)&m,
(const uint8_t *)&verify,
sizeof(m)) != TOOB_OK) {
/* Torn/failed write. The other slot still holds the previous good record,
* so the Core keeps working; the caller retries and seq will not regress. */
status = TOOB_ERR_FLASH_HW;
} else {
status = TOOB_OK;
}
toob_secure_zeroize(&m, sizeof(m));
toob_secure_zeroize(&verify, sizeof(verify));
return status;
}
/* ===== Public API (called from toob_update.c / toob_confirm.c) ===== */
toob_status_t toob_mailbox_set_update(uint32_t manifest_flash_addr) {
return mailbox_put(TOOB_REQ_UPDATE_PENDING, manifest_flash_addr, 0);
}
toob_status_t toob_mailbox_set_confirm(uint64_t nonce) {
return mailbox_put(TOOB_REQ_CONFIRM, 0, nonce);
}
toob_status_t toob_mailbox_set_recovery_resolved(void) {
return mailbox_put(TOOB_REQ_RECOVERY_RESOLVED, 0, 0);
}
======================================================================
FILE: sdk/libtoob/toob_ota.c
======================================================================
/**
* ==============================================================================
* Toob-Boot libtoob: OTA Daemon Implementation (toob_ota.c)
* ==============================================================================
*
* REFERENCED SPECIFICATIONS:
* - docs/lol.md (Case 1 & Case 2 Update Cycle logic)
* - docs/libtoob_api.md (toob_set_next_update delegation)
* - docs/testing_requirements.md (P10 compliance, zero dynamic allocation)
* - docs/concept_fusion.md (WAL transaction architecture)
*
* ARCHITECTURAL PROPERTIES:
* 1. Network-Agnostic Stream Writer: The RTOS pushes arbitrary-length
* chunks. This module internally buffers and aligns them to the
* CHIP_FLASH_WRITE_ALIGNMENT before flushing to the Staging Slot.
* 2. Zero Dynamic Allocation: All buffers are statically sized and
* bounded by _Static_assert at compile time.
* 3. Glitch-Resistant Flash Verification: Every flash write is
* double-checked via the established TOOB_GLITCH_DELAY shield pattern.
* 4. Single-Exit Cleanup: Sensitive alignment buffers are zeroized
* before every return path (P10 Stack Leakage Defense).
* 5. Context-Based Re-Entrancy: All session state lives in a caller-
* allocated toob_ota_ctx_t, enabling parallel OTA sessions.
*/
#include "libtoob.h"
#ifdef TOOB_HOST_FUZZING
#include "libtoob_config_sandbox.h"
#else
#include "generated_boot_config.h"
#endif
#include "toob_internal.h"
#include <string.h>
/* SWEV-T3 Notifier Registration */
static toob_swap_notify_fn g_swap_notifier = NULL;
void toob_set_swap_notifier(toob_swap_notify_fn cb) {
#if TOOB_SWAP_EVENT_STATE
g_swap_notifier = cb;
#else
(void)cb;
#endif
}
/* ==============================================================================
* Internal State Machine Constants
* ==============================================================================
*/
typedef enum {
TOOB_OTA_STATE_IDLE = 0x00,
TOOB_OTA_STATE_RECEIVING = 0x5A,
TOOB_OTA_STATE_DONE = 0xA5,
TOOB_OTA_STATE_ERROR = 0xFF
} toob_ota_state_t;
/* P10: Alignment buffer must be an exact multiple of flash write alignment */