E34 — CH32 hardware CRC, AES-128 / SM4 (ECDC) and RNG
| ID | E34 |
| Date | 2026-12-04 |
| Status | done: CRC = MPEG-2, 42 MB/s (370× the bitwise software CRC from flash); AES-128 and SM4 known-answer vectors pass, RAM (DMA) mode 169 / 138 MB/s; single-block input must be byte-reversed; RNG has little entropy: ~800 distinct 32-bit words in 2048 reads |
| Decisions | D115 (DECISIONS.md) |
| Board | #1 (rev A), CH32 ×32 (HCLK 96 MHz, PLL 384 MHz) |
| Firmware + results | fw/tests/2026-12-04-1-links, raw: crypto.txt |
| Scripts | links_crypto.py; CH32 crc, aes, sm4, rng (fw/ch32/links.c) |
Summary: The CRC unit and the ECDC cipher block work and are fast. The RNG does not deliver usable randomness: in 2048 consecutive words only about 800 values are distinct, some appear 13–18 times, whichever clock source feeds it and however slowly it is read.
Results
CRC (56 KB xorshift buffer, CPU feeds DATAR, loop in RAM): test vector 199ac3ca = software; buffer CRC
da7af8b3 on both. Hardware 1361 µs (42 MB/s), bitwise software from flash 502 ms.
ECDC (1024 blocks = 16 KB, key/plaintext FIPS-197 C.1 and the GB/T 32907 SM4 example for the vectors):
| vector | ECDC clock PLL/1 (384 MHz) | PLL/2 | single-block mode, CPU per block | |
|---|---|---|---|---|
| AES-128 encrypt | 69c4e0d8…70b4c55a ✔ |
97 µs = 169 MB/s | 161 µs = 102 MB/s | ~10 µs |
| SM4 encrypt | 681edf34…536e4246 ✔ |
119 µs = 138 MB/s | 215 µs = 76 MB/s | ~10 µs |
Decrypt takes the same time and every round trip matched (rt_bad 0). The RAM-mode ciphertext of block 0 matches
Python (cryptography) AES-ECB with the same key and data.
- Single-block mode wants the input fully byte-reversed: SGSD_127T96 = bswap(word 3) … SGSD_31T0 = bswap(word 0), while the outputs, the key registers and RAM mode use memory order. Found by searching all orderings in Python against the known ciphertext. Not in RM V1.8: bug list H25.
- Single-block mode costs ~10 µs per block for register traffic; only RAM mode is worth using for data.
RNG (rng 14336 0 0 <dly>, then a 2048-word dump analysed on the PC):
| read pace | time / word | byte χ² (14336 words, 255 dof; 0.1 % limit 330) | equal consecutive words |
|---|---|---|---|
| as fast as DRDY | 2.8 µs | 51 088 | 0 |
| 2 µs gap | 6.4 µs | 50 929 | 0 |
| 20 µs gap | 24 µs | 52 009 | 0 |
| 200 µs gap | 205 µs | 51 237 | 32 |
2048-word dump: ~800 distinct words (832 with SYSCLK source, 807 with the PLL source, 793 with 20 µs gaps); most frequent word 18×; byte lanes 0–3 take 203 / 245 / 146 / 121 of 256 values; per-bit P(1) 0.44–0.55. Mean Hamming distance between consecutive words is 16.0, so the output looks random word to word but cycles through a small set.
Lesson
Run a statistical test on any hardware RNG before trusting it: a monobit count (49 % ones here) passes, the byte χ² and the distinct-value count do not.
Chip facts and numbers: fw/LEARNINGS.md. All experiments: index.