physics-mv
FORGE: request-bound machine realization, providers and verification for AienOS. Host, simulated and GB10 hardware paths have separate evidence.
Brought by drakestapleton
physics-mv / m3 / tests / sha256_kat.s
| 1 | // sha256_kat.s -- bare-metal SHA-256 known-answer test for m3/sha256_be.s. |
| 2 | // |
| 3 | // Loaded raw at 0x40200000 (entered from tests/kat_stub.s in flash) on |
| 4 | // qemu-system-aarch64 -M virt -cpu cortex-a57, EL1, MMU off. |
| 5 | // Enables SCTLR_EL1.A, runs the vectors below, writes results to RES, prints |
| 6 | // "M3_KAT_DONE" (or "M3_KAT_FAULT" from any exception) on the PL011, then wfe. |
| 7 | // The host checks results by pmemsave state inspection, not by UART text. |
| 8 | // |
| 9 | // RES layout (0x40280000, 0x240 bytes, all little-endian): |
| 10 | // +0x000 u64 magic "M3KATRES" (written last; absent => incomplete run) |
| 11 | // +0x008 u64 SCTLR_EL1 read back after enabling A (bit 1) |
| 12 | // +0x010 u64 CurrentEL |
| 13 | // +0x020 u32 rc[9] return code of each call below |
| 14 | // +0x080 u8 digest[5][32] vectors 0..4 (canonical big-endian bytes) |
| 15 | // +0x140 u8 out buffers of refusal vectors 5..8, 0x30 B each, prefilled 0xA5 |
| 16 | // +0x200 u64 fault magic "M3FAULT!", ESR_EL1, ELR_EL1, FAR_EL1, vector offset |
| 17 | // Vectors: |
| 18 | // 0 "" 1 "abc" |
| 19 | // 2 NIST 56-byte message 3 PAT[0..1000) |
| 20 | // 4 PAT[0..SHA256_MAX_LEN) 5 len = SHA256_MAX_LEN+1 -> rc 1 |
| 21 | // 6 data = "abc"+1 (misalign) -> rc 2 7 out misaligned -> rc 3 |
| 22 | // 8 data+len wraps -> rc 4 |
| 23 | // PAT (0x40290000, 0x10000 bytes): byte i = i & 0xff. |
| 24 | |
| 25 | .equ RES, 0x40280000 |
| 26 | .equ PAT, 0x40290000 |
| 27 | .equ PAT_LEN, 0x10000 |
| 28 | .equ STACK_TOP, 0x40270000 |
| 29 | .equ UART_DR, 0x09000000 |
| 30 | // SHA256_MAX_LEN is the absolute symbol exported by m3/sha256_be.s; |
| 31 | // kat.ld asserts it fits in PAT. |
| 32 | |
| 33 | .equ R_MAGIC, 0x000 |
| 34 | .equ R_SCTLR, 0x008 |
| 35 | .equ R_EL, 0x010 |
| 36 | .equ R_RC, 0x020 |
| 37 | .equ R_DIG, 0x080 |
| 38 | .equ R_REF, 0x140 |
| 39 | .equ R_REFSZ, 0x30 |
| 40 | .equ R_FAULT, 0x200 |
| 41 | .equ R_END, 0x240 |
| 42 | |
| 43 | .section .text.boot, "ax", %progbits |
| 44 | .global _start |
| 45 | _start: |
| 46 | msr daifset, #0xf |
| 47 | ldr x0, =STACK_TOP |
| 48 | mov sp, x0 |
| 49 | adr x0, vectors |
| 50 | msr vbar_el1, x0 |
| 51 | isb |
| 52 | mrs x0, sctlr_el1 |
| 53 | orr x0, x0, #(1 << 1) // SCTLR_EL1.A: alignment checking |
| 54 | msr sctlr_el1, x0 |
| 55 | isb |
| 56 | |
| 57 | // Clear RES, then prefill the refusal out buffers with 0xA5. |
| 58 | ldr x20, =RES |
| 59 | mov x0, x20 |
| 60 | mov x1, #(R_END / 8) |
| 61 | 1: str xzr, [x0], #8 |
| 62 | subs x1, x1, #1 |
| 63 | b.ne 1b |
| 64 | add x0, x20, #R_REF |
| 65 | mov x1, #((R_FAULT - R_REF) / 8) |
| 66 | ldr x2, =0xa5a5a5a5a5a5a5a5 |
| 67 | 2: str x2, [x0], #8 |
| 68 | subs x1, x1, #1 |
| 69 | b.ne 2b |
| 70 | |
| 71 | mrs x0, sctlr_el1 |
| 72 | str x0, [x20, #R_SCTLR] |
| 73 | mrs x0, CurrentEL |
| 74 | str x0, [x20, #R_EL] |
| 75 | |
| 76 | // PAT[i] = i & 0xff |
| 77 | ldr x0, =PAT |
| 78 | mov x1, #0 |
| 79 | 3: strb w1, [x0, x1] |
| 80 | add x1, x1, #1 |
| 81 | cmp x1, #PAT_LEN |
| 82 | b.ne 3b |
| 83 | |
| 84 | .ifdef KAT_FAULT_PROBE |
| 85 | // Deliberate misaligned load: must trap to the vector table. |
| 86 | ldr x0, =PAT + 1 |
| 87 | ldr w0, [x0] |
| 88 | .endif |
| 89 | |
| 90 | .macro KAT idx, data, len, out |
| 91 | ldr x0, =\data |
| 92 | ldr x1, =\len |
| 93 | ldr x2, =\out |
| 94 | bl sha256_be |
| 95 | str w0, [x20, #(R_RC + 4 * \idx)] |
| 96 | .endm |
| 97 | |
| 98 | KAT 0, msg_empty, 0, RES + R_DIG + 0 * 32 |
| 99 | KAT 1, msg_abc, 3, RES + R_DIG + 1 * 32 |
| 100 | KAT 2, msg_nist, 56, RES + R_DIG + 2 * 32 |
| 101 | KAT 3, PAT, 1000, RES + R_DIG + 3 * 32 |
| 102 | KAT 4, PAT, SHA256_MAX_LEN, RES + R_DIG + 4 * 32 |
| 103 | KAT 5, PAT, SHA256_MAX_LEN+1, RES + R_REF + 0 * R_REFSZ |
| 104 | KAT 6, msg_abc+1, 3, RES + R_REF + 1 * R_REFSZ |
| 105 | KAT 7, msg_abc, 3, RES + R_REF + 2 * R_REFSZ + 1 |
| 106 | KAT 8, 0xffffffffffffff00, 0x200, RES + R_REF + 3 * R_REFSZ |
| 107 | |
| 108 | ldr x0, =0x534552544b41334d // "M3KATRES" |
| 109 | str x0, [x20, #R_MAGIC] |
| 110 | dsb sy |
| 111 | adr x0, str_done |
| 112 | bl puts |
| 113 | halt: wfe |
| 114 | b halt |
| 115 | |
| 116 | // puts(x0 = NUL-terminated string) -- byte writes to the PL011 data register. |
| 117 | puts: ldr x1, =UART_DR |
| 118 | 1: ldrb w2, [x0], #1 |
| 119 | cbz w2, 2f |
| 120 | strb w2, [x1] |
| 121 | b 1b |
| 122 | 2: ret |
| 123 | |
| 124 | fault_common: // x9 = vector offset |
| 125 | ldr x20, =RES + R_FAULT |
| 126 | ldr x0, =0x21544c554146334d // "M3FAULT!" |
| 127 | str x0, [x20, #0] |
| 128 | mrs x0, esr_el1 |
| 129 | str x0, [x20, #8] |
| 130 | mrs x0, elr_el1 |
| 131 | str x0, [x20, #16] |
| 132 | mrs x0, far_el1 |
| 133 | str x0, [x20, #24] |
| 134 | str x9, [x20, #32] |
| 135 | dsb sy |
| 136 | adr x0, str_fault |
| 137 | bl puts |
| 138 | b halt |
| 139 | |
| 140 | .ltorg |
| 141 | |
| 142 | .balign 2048 |
| 143 | vectors: |
| 144 | .irp n, 0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 |
| 145 | .balign 0x80 |
| 146 | mov x9, #(\n * 0x80) |
| 147 | b fault_common |
| 148 | .endr |
| 149 | |
| 150 | .section .rodata, "a", %progbits |
| 151 | .balign 8 |
| 152 | msg_empty: |
| 153 | .balign 4 |
| 154 | msg_abc: |
| 155 | .ascii "abc" |
| 156 | .balign 4 |
| 157 | msg_nist: |
| 158 | .ascii "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" |
| 159 | .balign 4 |
| 160 | str_done: |
| 161 | .asciz "M3_KAT_DONE\n" |
| 162 | str_fault: |
| 163 | .asciz "M3_KAT_FAULT\n" |