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 / sha256_be.s
| 1 | // sha256_be.s -- canonical big-endian SHA-256 wrapper for PHYSICS M3. |
| 2 | // |
| 3 | // int sha256_be(const u8 *data /*x0*/, u64 len /*x1*/, u8 out[32] /*x2*/) |
| 4 | // |
| 5 | // Calls sha256_compute() from the repo-root sha256_clean.c (compiled, never |
| 6 | // copied) with its own 16-byte-aligned stack scratch[128] and a stack u32[8] |
| 7 | // state, then stores the digest to `out` as canonical big-endian bytes |
| 8 | // (REV of each native u32 word), i.e. the same bytes `sha256sum` prints. |
| 9 | // |
| 10 | // Fail closed: every precondition is checked before anything is written. |
| 11 | // On a nonzero return `out` is untouched and sha256_compute is never called. |
| 12 | // 0 SHA256_BE_OK |
| 13 | // 1 SHA256_BE_ERR_LEN len > SHA256_MAX_LEN |
| 14 | // 2 SHA256_BE_ERR_DATA_ALIGN data not 4-byte aligned (sha256_transform does u32 loads) |
| 15 | // 3 SHA256_BE_ERR_OUT_ALIGN out not 4-byte aligned (digest stored as u32 words) |
| 16 | // 4 SHA256_BE_ERR_WRAP data + len wraps the address space |
| 17 | // General registers only (no FP/SIMD). AAPCS64; clobbers x0-x18 as allowed. |
| 18 | |
| 19 | .equ SHA256_MAX_LEN, 0x10000 |
| 20 | .equ SHA256_BE_OK, 0 |
| 21 | .equ SHA256_BE_ERR_LEN, 1 |
| 22 | .equ SHA256_BE_ERR_DATA_ALIGN, 2 |
| 23 | .equ SHA256_BE_ERR_OUT_ALIGN, 3 |
| 24 | .equ SHA256_BE_ERR_WRAP, 4 |
| 25 | |
| 26 | // Stack frame (192 B, 16-aligned): |
| 27 | // [sp+0] x29, x30 |
| 28 | // [sp+16] x19 (+8 pad) |
| 29 | // [sp+32] u32 state[8] (native words from sha256_compute) |
| 30 | // [sp+64] u8 scratch[128] |
| 31 | .equ FRAME, 192 |
| 32 | .equ OFF_STATE, 32 |
| 33 | .equ OFF_SCR, 64 |
| 34 | |
| 35 | .global SHA256_MAX_LEN |
| 36 | .section .text.sha256_be, "ax", %progbits |
| 37 | .balign 4 |
| 38 | .global sha256_be |
| 39 | .type sha256_be, %function |
| 40 | sha256_be: |
| 41 | mov x9, #SHA256_MAX_LEN |
| 42 | cmp x1, x9 |
| 43 | b.hi .Lerr_len |
| 44 | tst x0, #3 |
| 45 | b.ne .Lerr_data |
| 46 | tst x2, #3 |
| 47 | b.ne .Lerr_out |
| 48 | adds x9, x0, x1 |
| 49 | b.cs .Lerr_wrap |
| 50 | |
| 51 | stp x29, x30, [sp, #-FRAME]! |
| 52 | mov x29, sp |
| 53 | str x19, [sp, #16] |
| 54 | mov x19, x2 // out |
| 55 | add x2, sp, #OFF_SCR // scratch[128] |
| 56 | add x3, sp, #OFF_STATE // u32 state[8] |
| 57 | bl sha256_compute |
| 58 | |
| 59 | add x9, sp, #OFF_STATE |
| 60 | mov x10, #4 // 4 pairs of words |
| 61 | 1: ldp w11, w12, [x9], #8 |
| 62 | rev w11, w11 |
| 63 | rev w12, w12 |
| 64 | stp w11, w12, [x19], #8 |
| 65 | subs x10, x10, #1 |
| 66 | b.ne 1b |
| 67 | |
| 68 | mov w0, #SHA256_BE_OK |
| 69 | ldr x19, [sp, #16] |
| 70 | ldp x29, x30, [sp], #FRAME |
| 71 | ret |
| 72 | |
| 73 | .Lerr_len: |
| 74 | mov w0, #SHA256_BE_ERR_LEN |
| 75 | ret |
| 76 | .Lerr_data: |
| 77 | mov w0, #SHA256_BE_ERR_DATA_ALIGN |
| 78 | ret |
| 79 | .Lerr_out: |
| 80 | mov w0, #SHA256_BE_ERR_OUT_ALIGN |
| 81 | ret |
| 82 | .Lerr_wrap: |
| 83 | mov w0, #SHA256_BE_ERR_WRAP |
| 84 | ret |
| 85 | .size sha256_be, . - sha256_be |