Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ testpaths =
tools/test_build_flags_stamp.py
tools/test_device_lock_timeout.py
tools/test_device_prep.py
tools/test_drbg_state_owner.py
tools/test_flags_stamp_skip_is_loud.py
tools/test_http_body_checks_unit.py
tools/test_ip65_hw_checks_unit.py
Expand Down
85 changes: 67 additions & 18 deletions src/crypto/hmac_drbg.s
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
;
; Routines:
; hmac_sha256 - HMAC-SHA256(hmac_key, hmac_data_buf[hmac_data_len])
; drbg_hmac - hmac_sha256 keyed with the DRBG's private K (drbg_k)
; hmac_drbg_instantiate - Initialize DRBG from drbg_seed[drbg_seed_len]
; hmac_drbg_generate - Generate 32 bytes into drbg_output
; drbg_init_entropy - Collect 32B SID+CIA entropy, instantiate DRBG
; drbg_reseed - Collect 32B SID+CIA entropy, mix it into K/V
; drbg_random_byte - Return 1 buffered random byte in A (preserves X,Y)
; drbg_fill_bytes - Fill buffer: zp_ptr=dest, A=count
;
Expand All @@ -19,6 +21,15 @@
; Data labels (hmac_key, hmac_val, hmac_opad_block, hmac_data_buf,
; hmac_data_len, hmac_result, drbg_seed, drbg_seed_len,
; drbg_output, drbg_buf_idx, sha256_block, sha256_hash) in data.asm
;
; The DRBG's K is drbg_k, below, and is NOT exported. hmac_key is only the
; key INPUT of hmac_sha256, and HKDF and the Finished MAC write it, so it
; ends every handshake holding a key-schedule secret the peer knows. K used
; to BE hmac_key, which made the next connection's client_random and ECDHE
; private key a function of the last connection's key schedule. drbg_hmac
; copies K into hmac_key on every DRBG call, and only this file can name
; drbg_k, so no other HMAC user can write it: a module that tries fails the
; link. tools/test_drbg_isolation.py is the runtime check.
; =============================================================================

.include "constants.inc"
Expand All @@ -31,6 +42,7 @@
.export extra_sid_lo
.export extra_sid_hi
.export drbg_init_entropy
.export drbg_reseed
.export drbg_random_byte
.export drbg_fill_bytes

Expand All @@ -53,12 +65,27 @@
.import sha256_block
.import sha256_hash

.segment "BSS"
drbg_k: .res 32 ; DRBG K. Private: see the header.

; Phase C.4 fit: hmac_drbg occupies a separate aux-code segment so the
; ip65 cfg can route it to NET_CODE tail while sha256 (larger) rides
; NET_BSS_TAIL alongside TLS_CODE. Under UCI both segments flow into
; NET_CODE via identical cfg rules, preserving Phase C.2's behavior.
.segment "CRYPTO_AUX_CODE2"

; =============================================================================
; drbg_hmac - HMAC-SHA256(drbg_k, hmac_data_buf[hmac_data_len])
; Every HMAC the DRBG computes goes through here. Falls into hmac_sha256.
; =============================================================================
drbg_hmac:
ldx #31
@load_k:
lda drbg_k,x
sta hmac_key,x
dex
bpl @load_k

; =============================================================================
; hmac_sha256 - compute HMAC-SHA256
; Input: hmac_key (32 bytes), hmac_data_buf (hmac_data_len bytes, max 97)
Expand Down Expand Up @@ -329,7 +356,7 @@ hmac_sha256:
; =============================================================================
; hmac_drbg_update - HMAC-DRBG update(provided_data)
; Input: drbg_seed (provided_data), drbg_seed_len (0 if no provided_data)
; Uses/updates: hmac_key (K), hmac_val (V)
; Uses/updates: drbg_k (K), hmac_val (V)
; =============================================================================
hmac_drbg_update:
; --- Step 1: K = HMAC(K, V || 0x00 || provided_data) ---
Expand Down Expand Up @@ -369,13 +396,13 @@ hmac_drbg_update:
sta hmac_data_len

@do_hmac1:
jsr hmac_sha256
jsr drbg_hmac

; K = hmac_result
ldx #0
@update_k1:
lda hmac_result,x
sta hmac_key,x
sta drbg_k,x
inx
cpx #32
bne @update_k1
Expand All @@ -391,7 +418,7 @@ hmac_drbg_update:
lda #32
sta hmac_data_len

jsr hmac_sha256
jsr drbg_hmac

; V = hmac_result
ldx #0
Expand Down Expand Up @@ -432,13 +459,13 @@ hmac_drbg_update:
adc #33
sta hmac_data_len

jsr hmac_sha256
jsr drbg_hmac

; K = hmac_result
ldx #0
@update_k2:
lda hmac_result,x
sta hmac_key,x
sta drbg_k,x
inx
cpx #32
bne @update_k2
Expand All @@ -454,7 +481,7 @@ hmac_drbg_update:
lda #32
sta hmac_data_len

jsr hmac_sha256
jsr drbg_hmac

; V = hmac_result
ldx #0
Expand All @@ -471,14 +498,14 @@ hmac_drbg_update:
; =============================================================================
; hmac_drbg_instantiate - initialize DRBG state from seed
; Input: drbg_seed (seed material), drbg_seed_len (length, typically 64)
; Output: hmac_key and hmac_val initialized
; Output: drbg_k and hmac_val initialized
; =============================================================================
hmac_drbg_instantiate:
; K = 0x00 * 32
ldx #0
lda #$00
@init_k:
sta hmac_key,x
sta drbg_k,x
inx
cpx #32
bne @init_k
Expand All @@ -498,7 +525,7 @@ hmac_drbg_instantiate:

; =============================================================================
; hmac_drbg_generate - generate 32 bytes of output
; Input: DRBG state (hmac_key, hmac_val) must be instantiated
; Input: DRBG state (drbg_k, hmac_val) must be instantiated
; Output: drbg_output (32 bytes)
; =============================================================================
hmac_drbg_generate:
Expand All @@ -513,7 +540,7 @@ hmac_drbg_generate:
lda #32
sta hmac_data_len

jsr hmac_sha256
jsr drbg_hmac

; V = hmac_result, also copy to output
ldx #0
Expand Down Expand Up @@ -549,6 +576,35 @@ extra_sid_hi:
; Clobbers: A, X, Y
; =============================================================================
drbg_init_entropy:
jsr drbg_collect_seed
jsr hmac_drbg_instantiate
jmp drbg_mark_empty

; =============================================================================
; drbg_reseed - mix a fresh drbg_collect_seed sample into the running DRBG
; update(seed) keeps K and V, so whatever entropy the state already holds
; survives; tls_connect calls it before every handshake's draws. The 32
; bytes are NOT 32 bytes of entropy: CIA1 timer A decrements at a fixed
; rate between reads and osc3 moves slowly, so a sample carries roughly the
; entropy of the instant it started (how long the user took to press 'G').
; That entropy is unmeasured. Defence in depth only; the fix for a key
; schedule reaching the DRBG is that drbg_k is private.
; Clobbers: A, X, Y
; =============================================================================
drbg_reseed:
jsr drbg_collect_seed
jsr hmac_drbg_update
drbg_mark_empty:
; Force a fresh generate on the next drbg_random_byte call
lda #32
sta drbg_buf_idx
rts

; =============================================================================
; drbg_collect_seed - drbg_seed[0..31] = SID osc3 XOR CIA1 timer A, len = 32
; Clobbers: A, X, Y
; =============================================================================
drbg_collect_seed:
ldx #0
@collect:
; Read SID oscillator 3 XOR CIA timer A low
Expand Down Expand Up @@ -593,15 +649,8 @@ drbg_init_entropy:
cpx #32
bne @collect

; Set seed length and instantiate DRBG
lda #32
sta drbg_seed_len
jsr hmac_drbg_instantiate

; Force fresh generate on first drbg_random_byte call
lda #32
sta drbg_buf_idx

rts

; =============================================================================
Expand Down
2 changes: 1 addition & 1 deletion src/data.s
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ sha256_len: .res 2 ; message length in bits
.export drbg_seed
.export drbg_seed_len
.export drbg_output
hmac_key: .res 32 ; HMAC key / DRBG K state
hmac_key: .res 32 ; HMAC key input (the DRBG K is drbg_k)
hmac_val: .res 32 ; DRBG V state
hmac_opad_block: .res 64 ; Scratch: K XOR opad
hmac_data_buf: .res 97 ; V(32) + 0x00/0x01(1) + seed(64)
Expand Down
4 changes: 4 additions & 0 deletions src/tls13.s
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@

; --- Crypto / DRBG / ECDH helpers ---
.import drbg_fill_bytes
.import drbg_reseed
.import tls_ecdh_generate_keypair
.import tls_ecdh_compute_shared

Expand Down Expand Up @@ -145,6 +146,9 @@ tls_connect:
sta tls_state
sta tls_reached_connected ; #204: this attempt has not connected yet

; fresh SID/CIA entropy into the DRBG before this handshake draws
jsr drbg_reseed

; generate client random (32 bytes)
lda #<tls_client_random
sta zp_ptr
Expand Down
8 changes: 8 additions & 0 deletions tools/run_all_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"body_truncation",
"aead_fail_closed",
"tls_connected_latch",
"drbg_isolation",
)

# Suites that define run_tests() but are deliberately NOT dispatched here,
Expand Down Expand Up @@ -224,6 +225,13 @@ def run_test_suite(name, transport, labels, seed):
from test_tls_connected_latch import run_tests as latch_run
passed, failed = latch_run(transport, labels)

elif name == "drbg_isolation":
# The DRBG's K is drbg_k, not hmac_key: the key schedule must not
# reach the next tls_connect's draws. Backend-agnostic, no
# transport; patches it makes are restored.
from test_drbg_isolation import run_tests as drbg_iso_run
passed, failed = drbg_iso_run(transport, labels)

elif name == "reu_row_abi":
from test_reu_row_abi import run_tests as reu_row_abi_run
passed, failed = reu_row_abi_run(transport, labels)
Expand Down
Loading