diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index fb00d8a..a02faa4 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -45,13 +45,14 @@ jobs: grep -q "RCM Payload Emulator" out.txt \ || { echo "banner missing - binary did not start"; exit 1; } - - name: CCPLEX and display regression tests + - name: CCPLEX, SE and display regression tests # Small self-contained payloads. One boots CPU0 through bdk's # ccplex_boot_cpu0() register sequence: an unpowered release must be # refused, a powered one must run an AArch64 blob at EL3, and WFE must - # park the core. The others drive the display controller and VIC in - # six scenarios, and the frame the emulator shows must match the - # picture drawn, pixel for pixel. + # park the core. One runs the Security Engine's RSA, SHA-256, AES and + # RNG against vectors computed in Python. The others drive the + # display controller and VIC in six scenarios, and the frame the + # emulator shows must match the picture drawn, pixel for pixel. run: make test - name: hwtest end to end @@ -69,6 +70,7 @@ jobs: name: test-logs path: | tests/ccplex/out.log + tests/se/out.log tests/hwtest/out.log if-no-files-found: ignore diff --git a/DESIGN.md b/DESIGN.md index 28e977c..3dfa097 100644 --- a/DESIGN.md +++ b/DESIGN.md @@ -21,7 +21,7 @@ flowchart TB uc -->|MMIO callbacks| mmio["t210/mmio.cpp
mmio_bus_read / mmio_bus_write
shared by both masters"] mmio --> sdmmc["sdmmc
SDMMC1 / SDMMC4
CMD0..21, EXT_CSD, ADMA2
SD UHS-I, eMMC HS400"] - mmio --> se["se_engine
AES-128, SHA-256
BIS keyslot override"] + mmio --> se["se_engine
AES-128, SHA-256, RSA, RNG
BIS keyslot override"] mmio --> i2c["i2c3
STMFTS / FTS4 touch"] mmio --> i2c1["I2C_1 slaves (inline)
MAX17050, TMP451,
BQ24193, BM92T36"] mmio --> i2c5["I2C_5 slaves (inline)
MAX77620, MAX77621"] @@ -245,6 +245,18 @@ Models the SE (Security Engine) register block at `0x70012000`. Supports: save container hash. - **DST_KEYTABLE unwrap path.** The result of a single block is written into the destination slot's selected word quad. +- **RSA** modular exponentiation, as Lockpick's bdk drives it + (`se_rsa_key_set()` / `se_rsa_exp_mod()`): two keyslots of up to 2048 + bits, loaded a word at a time through `SE_RSA_KEYTABLE_ADDR/DATA` (least + significant word first), `KEY_SIZE` in 512-bit steps and `EXP_SIZE` in + words, the input a little-endian number over DMA and the result in + `SE_RSA_OUTPUT` (or memory). Montgomery multiplication on 32-bit words; + an even modulus fails the operation. +- **RNG** into memory or a keyslot quad (`se_rng_pseudo()`, TegraExplorer's + `se_generate_random()`): `LAST_BLOCK + 1` blocks, cut to the buffer size. + The bytes come from a SplitMix64 generator with a fixed seed, reset with + the SE, so runs repeat. Nyx uses them for GPT partition GUIDs and the SD + benchmark's random offsets. - **Context save** (`SE_OP_CTX_SAVE`), as bdk's `se_aes_ctx_get_keys()` uses it to read keyslots back: an RNG op seeds a secure random key (fixed, for reproducibility), each saved keyslot quad is written out encrypted under it @@ -258,10 +270,11 @@ Models the SE (Security Engine) register block at `0x70012000`. Supports: alone. For other keyslots, the SE behaves normally. The 16 keyslots are stored in a flat `Keyslot ks_table[16]` with -`key[32] / iv_orig[16] / iv_upd[16]`. RSA and RNG output are stubbed: they -return `OP_DONE` without producing data. Block counts are bounded by what one +`key[32] / iv_orig[16] / iv_upd[16]`. Block counts are bounded by what one linked-list entry can describe (16 MiB), and an operation whose buffers are unreachable finishes with `SE_INT_ERR_STAT` instead of computing on zeros. +`tests/se/` checks RSA-2048 and RSA-1024 both ways against Python, SHA-256 +one-shot and in parts, AES-ECB and the RNG. ### Touchscreen (`t210/i2c3.cpp`) @@ -676,14 +689,16 @@ final `for(;;) wfe;` would otherwise spin through a billion instructions per emulated second. `make test` runs `tests/ccplex/`, a self-contained payload that checks the -refusal, the EL3 entry, the mailbox, WFE parking and the reset, and +refusal, the EL3 entry, the mailbox, WFE parking and the reset; +`tests/se/`, which runs the SE's RSA, SHA-256, AES and RNG against vectors +computed in Python (`gen_vectors.py` writes `vectors.h`); and `tests/display/`, one payload built per scenario that drives the display controller and VIC the way bdk does (a pitch window; Nyx's VIC turn into pitch and into block-linear surfaces; a block-linear VIC source; the DC's own column scan with an address offset; a mirrored window with window D blended over it). `check.py` compares every pixel of the frame the emulator shows with the picture the payload drew. `tests/hwtest/` builds hwtest-rcm -with distro toolchains and runs its whole sweep; CI runs all three. +with distro toolchains and runs its whole sweep; CI runs all four. ## Bug history diff --git a/Makefile b/Makefile index 59d1328..f05f547 100644 --- a/Makefile +++ b/Makefile @@ -98,6 +98,10 @@ tests/ccplex/payload.bin: tests/ccplex/payload.c tests/ccplex/link.ld $(TEST_CC) $(TEST_CFLAGS) -T tests/ccplex/link.ld -o tests/ccplex/payload.elf $< $(TEST_OBJCOPY) -O binary tests/ccplex/payload.elf $@ +tests/se/payload.bin: tests/se/payload.c tests/se/vectors.h tests/se/link.ld + $(TEST_CC) $(TEST_CFLAGS) -T tests/se/link.ld -o tests/se/payload.elf $< + $(TEST_OBJCOPY) -O binary tests/se/payload.elf $@ + DISPLAY_SCENARIOS = 1 2 3 4 5 6 DISPLAY_PAYLOADS = $(DISPLAY_SCENARIOS:%=tests/display/payload_%.bin) @@ -105,12 +109,14 @@ tests/display/payload_%.bin: tests/display/payload.c tests/display/link.ld $(TEST_CC) $(TEST_CFLAGS) -DSCENARIO=$* -T tests/display/link.ld -o tests/display/payload_$*.elf $< $(TEST_OBJCOPY) -O binary tests/display/payload_$*.elf $@ -test: $(OUTPUT) tests/ccplex/payload.bin $(DISPLAY_PAYLOADS) +test: $(OUTPUT) tests/ccplex/payload.bin tests/se/payload.bin $(DISPLAY_PAYLOADS) sh tests/ccplex/run.sh ./$(OUTPUT) tests/ccplex/payload.bin + sh tests/se/run.sh ./$(OUTPUT) tests/se/payload.bin sh tests/display/run.sh ./$(OUTPUT) tests/display clean: rm -f $(OUTPUT) $(OBJS) $(OBJS:.o=.d) \ tests/ccplex/payload.elf tests/ccplex/payload.bin tests/ccplex/out.log \ + tests/se/payload.elf tests/se/payload.bin tests/se/out.log \ tests/display/payload_*.elf tests/display/payload_*.bin \ $(WIN_OUT) $(WIN_OBJS) $(WIN_OBJS:.o=.d) diff --git a/README.md b/README.md index 3c55a5b..6ea9c77 100644 --- a/README.md +++ b/README.md @@ -175,7 +175,7 @@ real consoles. Details in [DESIGN.md](DESIGN.md#ccplex-cpu0). ## Tests ```bash -make test # CCPLEX + display regression payloads (arm-none-eabi-gcc, python3) +make test # CCPLEX, SE + display regression payloads (arm-none-eabi-gcc, python3) tests/hwtest/build.sh build-hwtest # build hwtest-rcm (+ gcc-aarch64-linux-gnu) tests/hwtest/run.sh ./rcm_emu build-hwtest/hwtest-rcm/build/hwtest.bin ``` @@ -288,12 +288,12 @@ flowchart LR root --> top["main.cpp
emu_state.h
Makefile
Dockerfile
README.md / DESIGN.md"] root --> t210["t210/
(SoC peripheral models)"] root --> display["display/"] - root --> tests["tests/
ccplex/, hwtest/"] + root --> tests["tests/
ccplex/, se/, display/, hwtest/"] t210 --> mmio["mmio.{h,cpp}
memory_map.h
regcache.h, tegra_bl.h"] t210 --> cpus["bpmp.{h,cpp} BPMP clock
ccplex.{h,cpp} CPU0 (A57)"] t210 --> pcie["pcie.{h,cpp}
root complex + CYW4356"] - t210 --> se["se_engine.{h,cpp}
AES-128, SHA-256"] + t210 --> se["se_engine.{h,cpp}
AES-128, SHA-256, RSA, RNG"] t210 --> i2c["i2c3.{h,cpp}
STMFTS / FTS4 touch"] display --> sdl["sdl_display.{h,cpp}
DC window scan-out
+ SDL2"] @@ -314,7 +314,8 @@ For implementation details, see [DESIGN.md](DESIGN.md). firmware and the full master-key unwrapping flow are not faithfully emulated. `--prod-keys` is a pragmatic shortcut: when Lockpick writes a derived BIS (Boot Image Storage) key into a keyslot, the value from the user-supplied key - file is substituted. RSA, RNG, and chunked SHA are stubs. + file is substituted. The random number generator starts from a fixed + seed, so runs repeat. - **Single-threaded.** Both cores run in batches on the main thread between SDL event polls. That is also what keeps them deterministic. diff --git a/t210/se_engine.cpp b/t210/se_engine.cpp index 00e5163..8f65f25 100644 --- a/t210/se_engine.cpp +++ b/t210/se_engine.cpp @@ -260,6 +260,12 @@ constexpr uint32_t SE_CONTEXT_SAVE_CONFIG_REG = 0x070; constexpr uint32_t SE_SHA_CONFIG_REG = 0x200; constexpr uint32_t SE_SHA_MSG_LENGTH_REG = 0x204; // 4 dwords, in bits constexpr uint32_t SE_SHA_MSG_LEFT_REG = 0x214; // 4 dwords, in bits +constexpr uint32_t SE_RSA_CONFIG_REG = 0x400; +constexpr uint32_t SE_RSA_KEY_SIZE_REG = 0x404; +constexpr uint32_t SE_RSA_EXP_SIZE_REG = 0x408; +constexpr uint32_t SE_RSA_KEYTABLE_ADDR_REG = 0x420; +constexpr uint32_t SE_RSA_KEYTABLE_DATA_REG = 0x424; +constexpr uint32_t SE_RSA_OUTPUT_REG = 0x428; // 64 dwords constexpr uint32_t SE_INT_OP_DONE = 1u << 4; constexpr uint32_t SE_INT_ERR_STAT = 1u << 16; @@ -269,6 +275,7 @@ constexpr uint32_t SE_OP_CTX_SAVE = 3; constexpr uint32_t ALG_RNG = 2; constexpr uint32_t ALG_SHA = 3; +constexpr uint32_t ALG_RSA = 4; constexpr uint32_t MODE_SHA256 = 5; constexpr uint32_t DST_SRK = 3; constexpr uint32_t DST_RSAREG = 4; @@ -344,6 +351,23 @@ static uint32_t reg_err_status = 0; static uint32_t reg_rng_config = 0; static uint32_t spare_regs[256] = {0}; // catch-all for less critical regs +// The two RSA keyslots, as bdk's se_rsa_key_set() loads them: 32-bit words, +// least significant first (it writes the big-endian key backwards, each word +// byte-swapped). SE_RSA_KEYTABLE_ADDR picks the word: PKT in 5:0, EXP (0) or +// MOD (1) in bit 6, the slot in bit 7. +constexpr int kRsaWords = 64; // 2048 bits +struct RsaKey { uint32_t exp[kRsaWords]; uint32_t mod[kRsaWords]; }; +static RsaKey rsa_keys[2]; +static uint32_t rsa_config = 0; +static uint32_t rsa_key_size = 0; +static uint32_t rsa_exp_size = 0; +static uint32_t rsa_keytable_addr = 0; +static uint32_t rsa_output[kRsaWords] = {0}; + +// The random number generator's state. The SE's DRBG is seeded from an +// entropy source; this one starts from a constant, so runs repeat. +static uint64_t rng_state = 0; + // Secure random key for context save, and the CBC chain the saved blocks // are encrypted under. See ctx_save(). static uint8_t srk[16] = {0}; @@ -438,6 +462,181 @@ static void run_aes_blocks(uint32_t slot, bool encrypt, if (chained && blocks) std::memcpy(ks_table[slot].iv_upd, v, 16); } +// ---- RSA: x^e mod n, Montgomery multiplication on 32-bit words ------------- +// +// Numbers are arrays of words, least significant first, n words long. + +static bool big_geq(const uint32_t *a, const uint32_t *b, int n) { + for (int i = n - 1; i >= 0; --i) + if (a[i] != b[i]) return a[i] > b[i]; + return true; +} + +static void big_sub(uint32_t *a, const uint32_t *b, int n) { + uint64_t borrow = 0; + for (int i = 0; i < n; ++i) { + uint64_t d = (uint64_t)a[i] - b[i] - borrow; + a[i] = (uint32_t)d; + borrow = d >> 63; + } +} + +// x = (2x + bit) mod m, for x < m. +static void big_shl1_mod(uint32_t *x, uint32_t bit, const uint32_t *m, int n) { + uint32_t carry = bit; + for (int i = 0; i < n; ++i) { + uint32_t top = x[i] >> 31; + x[i] = x[i] << 1 | carry; + carry = top; + } + if (carry || big_geq(x, m, n)) big_sub(x, m, n); +} + +// r = a mod m, a being na words long. +static void big_mod(const uint32_t *a, int na, const uint32_t *m, int n, + uint32_t *r) { + std::memset(r, 0, n * sizeof(uint32_t)); + for (int i = na * 32 - 1; i >= 0; --i) + big_shl1_mod(r, a[i / 32] >> (i % 32) & 1, m, n); +} + +// t = a * b / 2^(32n) mod m, for a, b < m and m odd (CIOS). +static void mont_mul(const uint32_t *a, const uint32_t *b, const uint32_t *m, + uint32_t minv, int n, uint32_t *out) { + uint32_t t[kRsaWords + 2] = {0}; + for (int i = 0; i < n; ++i) { + uint64_t c = 0; + for (int j = 0; j < n; ++j) { + uint64_t s = (uint64_t)t[j] + (uint64_t)a[j] * b[i] + c; + t[j] = (uint32_t)s; + c = s >> 32; + } + uint64_t s = (uint64_t)t[n] + c; + t[n] = (uint32_t)s; + t[n + 1] = (uint32_t)(s >> 32); + uint32_t u = t[0] * minv; + c = ((uint64_t)t[0] + (uint64_t)u * m[0]) >> 32; + for (int j = 1; j < n; ++j) { + s = (uint64_t)t[j] + (uint64_t)u * m[j] + c; + t[j - 1] = (uint32_t)s; + c = s >> 32; + } + s = (uint64_t)t[n] + c; + t[n - 1] = (uint32_t)s; + t[n] = t[n + 1] + (uint32_t)(s >> 32); + } + if (t[n] || big_geq(t, m, n)) big_sub(t, m, n); + std::memcpy(out, t, n * sizeof(uint32_t)); +} + +// out = x^e mod m. m must be odd (every RSA modulus is); false if it is not. +static bool mod_exp(const uint32_t *x, const uint32_t *e, int ne, + const uint32_t *m, int n, uint32_t *out) { + if (!(m[0] & 1)) return false; + // -1/m mod 2^32 by Newton's iteration; m0 is its own inverse mod 8. + uint32_t inv = m[0]; + for (int i = 0; i < 4; ++i) inv *= 2 - m[0] * inv; + uint32_t minv = 0u - inv; + + // R^2 mod m, R = 2^(32n): 1 doubled 64n times. + uint32_t r2[kRsaWords] = {0}, one[kRsaWords] = {0}; + one[0] = 1; + big_mod(one, 1, m, n, r2); + for (int i = 0; i < 64 * n; ++i) big_shl1_mod(r2, 0, m, n); + + uint32_t xm[kRsaWords], acc[kRsaWords]; + big_mod(x, n, m, n, xm); + mont_mul(xm, r2, m, minv, n, xm); // x in Montgomery form + mont_mul(one, r2, m, minv, n, acc); // 1 in Montgomery form + for (int i = ne * 32 - 1; i >= 0; --i) { + mont_mul(acc, acc, m, minv, n, acc); + if (e[i / 32] >> (i % 32) & 1) mont_mul(acc, xm, m, minv, n, acc); + } + mont_mul(acc, one, m, minv, n, out); + return true; +} + +// SE_OP_START with ALG_RSA, as bdk's se_rsa_exp_mod() drives it: the input +// arrives over DMA as a little-endian number the size of the modulus, the +// key comes from the slot in SE_RSA_CONFIG, KEY_SIZE gives the modulus in +// 512-bit steps and EXP_SIZE the exponent in words. The result goes to +// SE_RSA_OUTPUT (least significant word first), or to memory. +static void rsa_op(EmuState *state, uint32_t dst_kind) { + const RsaKey &k = rsa_keys[(rsa_config >> 24) & 1]; + int n = (int)((rsa_key_size & 3) + 1) * 16; + int ne = (int)(rsa_exp_size > (uint32_t)kRsaWords ? kRsaWords : rsa_exp_size); + + uint32_t x[kRsaWords] = {0}; + LLDesc in_ll{}; + if (read_ll(state->uc, reg_in_ll_addr, &in_ll) && in_ll.size) { + uint32_t copy = in_ll.size < (uint32_t)n * 4 ? in_ll.size : n * 4; + if (uc_mem_read(state->uc, in_ll.addr, x, copy) != UC_ERR_OK) { + op_fail("RSA input", in_ll.addr); + return; + } + } + + std::memset(rsa_output, 0, sizeof(rsa_output)); + if (!mod_exp(x, k.exp, ne, k.mod, n, rsa_output)) { + std::printf("[se] RSA-%d: even modulus, no result\n", n * 32); + reg_int_status |= SE_INT_OP_DONE | SE_INT_ERR_STAT; + return; + } + std::printf("[se] RSA-%d exp mod, %d-bit exponent -> ...%08X\n", n * 32, + ne * 32, rsa_output[0]); + + if (dst_kind == DST_MEMORY) { + LLDesc out_ll{}; + if (read_ll(state->uc, reg_out_ll_addr, &out_ll) && out_ll.size) { + uint32_t copy = out_ll.size < (uint32_t)n * 4 ? out_ll.size : n * 4; + if (uc_mem_write(state->uc, out_ll.addr, rsa_output, copy) != UC_ERR_OK) { + op_fail("RSA output", out_ll.addr); + return; + } + } + } + reg_int_status |= SE_INT_OP_DONE; +} + +// SplitMix64: small, and its output has no visible pattern. +static uint64_t rng_next() { + uint64_t z = (rng_state += 0x9E3779B97F4A7C15ull); + z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ull; + z = (z ^ (z >> 27)) * 0x94D049BB133111EBull; + return z ^ (z >> 31); +} + +static void rng_fill(uint8_t *p, size_t len) { + for (size_t i = 0; i < len; i += 8) { + uint64_t v = rng_next(); + std::memcpy(p + i, &v, len - i < 8 ? len - i : 8); + } +} + +// SE_OP_START with ALG_RNG into memory or a keyslot: bdk's se_rng_pseudo() +// and TegraExplorer's se_generate_random() fill a buffer, SE_CRYPTO_LAST_BLOCK +// + 1 blocks of it; Atmosphère-style drivers fill a key quad. +static void rng_op(EmuState *state, uint32_t dst_kind) { + uint32_t blocks = (reg_block_count % MAX_AES_BLOCKS) + 1; + if (dst_kind == DST_KEYTABLE) { + Keyslot &k = ks_table[(reg_keytable_dst >> 8) & 0xF]; + uint8_t *target[4] = {k.key, k.key + 16, k.iv_orig, k.iv_upd}; + rng_fill(target[reg_keytable_dst & 0x3], 16); + } else if (dst_kind == DST_MEMORY) { + LLDesc out_ll{}; + if (read_ll(state->uc, reg_out_ll_addr, &out_ll) && out_ll.size) { + uint32_t len = out_ll.size < blocks * 16 ? out_ll.size : blocks * 16; + std::vector buf(len); + rng_fill(buf.data(), len); + if (uc_mem_write(state->uc, out_ll.addr, buf.data(), len) != UC_ERR_OK) { + op_fail("RNG output", out_ll.addr); + return; + } + } + } + reg_int_status |= SE_INT_OP_DONE; +} + // SHA-256, one-shot or in parts (bdk _se_sha_hash_256). SHA_INIT_HASH starts // from the standard initial state, SHA_CONTINUE from the state the previous // part left in SE_HASH_RESULT - which is where the hardware keeps it, too. @@ -504,9 +703,13 @@ static void op_start(EmuState *state) { return; } - if (dst_kind == DST_RSAREG) { - // RSA not implemented — leave output zero. - reg_int_status |= SE_INT_OP_DONE; + if (enc_alg == ALG_RNG) { + rng_op(state, dst_kind); + return; + } + + if (enc_alg == ALG_RSA) { + rsa_op(state, dst_kind); return; } @@ -667,6 +870,11 @@ static uint32_t keytable_read(uint32_t addr) { return v; } +static uint32_t &rsa_keytable_word() { + RsaKey &k = rsa_keys[(rsa_keytable_addr >> 7) & 1]; + return ((rsa_keytable_addr >> 6) & 1 ? k.mod : k.exp)[rsa_keytable_addr & 0x3F]; +} + } // namespace uint32_t se_engine_read(EmuState *state, uint64_t addr) { @@ -677,6 +885,9 @@ uint32_t se_engine_read(EmuState *state, uint64_t addr) { if (off >= SE_CRYPTO_LINEAR_CTR_REG && off < SE_CRYPTO_LINEAR_CTR_REG + 16) { return linear_ctr[(off - SE_CRYPTO_LINEAR_CTR_REG) / 4]; } + if (off >= SE_RSA_OUTPUT_REG && off < SE_RSA_OUTPUT_REG + kRsaWords * 4) { + return rsa_output[(off - SE_RSA_OUTPUT_REG) / 4]; + } switch (off) { case SE_OPERATION_REG: return reg_op; case SE_INT_ENABLE_REG: return reg_int_enable; @@ -692,6 +903,11 @@ uint32_t se_engine_read(EmuState *state, uint64_t addr) { case SE_RNG_CONFIG_REG: return reg_rng_config; case SE_STATUS_REG: return 0; // IDLE case SE_ERR_STATUS_REG: return reg_err_status; + case SE_RSA_CONFIG_REG: return rsa_config; + case SE_RSA_KEY_SIZE_REG: return rsa_key_size; + case SE_RSA_EXP_SIZE_REG: return rsa_exp_size; + case SE_RSA_KEYTABLE_ADDR_REG: return rsa_keytable_addr; + case SE_RSA_KEYTABLE_DATA_REG: return rsa_keytable_word(); default: if (off < sizeof(spare_regs)/sizeof(spare_regs[0])*4) return spare_regs[off / 4]; @@ -734,6 +950,11 @@ void se_engine_write(EmuState *state, uint64_t addr, uint32_t val) { case SE_CRYPTO_KEYTABLE_DST: reg_keytable_dst = val; return; case SE_RNG_CONFIG_REG: reg_rng_config = val; return; case SE_ERR_STATUS_REG: reg_err_status &= ~val; return; + case SE_RSA_CONFIG_REG: rsa_config = val; return; + case SE_RSA_KEY_SIZE_REG: rsa_key_size = val; return; + case SE_RSA_EXP_SIZE_REG: rsa_exp_size = val; return; + case SE_RSA_KEYTABLE_ADDR_REG: rsa_keytable_addr = val; return; + case SE_RSA_KEYTABLE_DATA_REG: rsa_keytable_word() = val; return; default: if (off < sizeof(spare_regs)/sizeof(spare_regs[0])*4) spare_regs[off / 4] = val; @@ -763,6 +984,10 @@ void se_engine_reset() { std::memset(spare_regs, 0, sizeof(spare_regs)); std::memset(srk, 0, sizeof(srk)); std::memset(ctx_chain, 0, sizeof(ctx_chain)); + std::memset(rsa_keys, 0, sizeof(rsa_keys)); + std::memset(rsa_output, 0, sizeof(rsa_output)); + rsa_config = rsa_key_size = rsa_exp_size = rsa_keytable_addr = 0; + rng_state = 0; } // ---- prod.keys parser ------------------------------------------------------- diff --git a/tests/se/gen_vectors.py b/tests/se/gen_vectors.py new file mode 100755 index 0000000..6e6c347 --- /dev/null +++ b/tests/se/gen_vectors.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 +# Writes vectors.h for tests/se/payload.c: two RSA keys (e = 65537), a message +# and its m^d mod n for each, and SHA-256 digests. Seeded, so the output is +# the same every run; vectors.h is checked in and this only needs rerunning +# to change the vectors. +import hashlib, os, random +rnd = random.Random(0x5E) +def is_prime(n): + if n < 2: return False + for p in [2,3,5,7,11,13,17,19,23,29,31,37]: + if n % p == 0: return n == p + d, s = n-1, 0 + while d % 2 == 0: d//=2; s+=1 + for _ in range(40): + a = rnd.randrange(2, n-2) + x = pow(a, d, n) + if x in (1, n-1): continue + for _ in range(s-1): + x = x*x % n + if x == n-1: break + else: return False + return True +def prime(bits): + while True: + p = rnd.getrandbits(bits) | (1 << (bits-1)) | (1 << (bits-2)) | 1 + if is_prime(p): return p +def key(bits, e=65537): + while True: + p, q = prime(bits//2), prime(bits//2) + phi = (p-1)*(q-1) + if p != q and phi % e: + n = p*q + if n.bit_length() == bits: + return n, pow(e, -1, phi) +def arr(name, v, nbytes): + b = v.to_bytes(nbytes, 'big') + lines = ['static const unsigned char %s[%d] = {' % (name, nbytes)] + for i in range(0, nbytes, 12): + lines.append(' ' + ', '.join('0x%02X' % x for x in b[i:i+12]) + ',') + lines.append('};') + return '\n'.join(lines) +out = [] +for bits, tag in ((2048, 'k2048'), (1024, 'k1024')): + n, d = key(bits) + m = int.from_bytes(hashlib.sha256(tag.encode()).digest() * (bits // 256), 'big') % n + s = pow(m, d, n) + assert pow(s, 65537, n) == m + nb = bits // 8 + out += [arr(tag + '_mod', n, nb), arr(tag + '_exp', d, nb), arr(tag + '_msg', m, nb), arr(tag + '_sig', s, nb)] +msg = bytes((i * 7 + 3) & 0xFF for i in range(200)) +out.append(arr('sha_msg', int.from_bytes(msg, 'big'), 200)) +out.append(arr('sha_msg_digest', int.from_bytes(hashlib.sha256(msg).digest(), 'big'), 32)) +out.append(arr('sha_abc_digest', int.from_bytes(hashlib.sha256(b'abc').digest(), 'big'), 32)) +open(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'vectors.h'), 'w').write('/* Generated by gen_vectors.py: RSA keys (e = 65537) with\n * m^d mod n, and SHA-256 digests, computed with Python integers and hashlib. */\n\n' + '\n\n'.join(out) + '\n') + diff --git a/tests/se/link.ld b/tests/se/link.ld new file mode 100644 index 0000000..bdc1203 --- /dev/null +++ b/tests/se/link.ld @@ -0,0 +1,10 @@ +/* Loaded and entered at 0x40010000, like any RCM payload. */ +ENTRY(_start) +SECTIONS { + . = 0x40010000; + .text : { KEEP(*(.text.start)) *(.text*) } + .rodata : { *(.rodata*) } + .data : { *(.data*) } + .bss : { *(.bss*) *(COMMON) } + /DISCARD/ : { *(.comment) *(.note*) *(.ARM.attributes) *(.ARM.exidx*) } +} diff --git a/tests/se/payload.c b/tests/se/payload.c new file mode 100644 index 0000000..387fae0 --- /dev/null +++ b/tests/se/payload.c @@ -0,0 +1,252 @@ +/* + * Security Engine regression payload. + * + * A freestanding BPMP payload that drives the emulator's SE model with the + * register sequences bdk uses, checks every result against vectors computed + * outside the emulator (vectors.h, from gen_vectors.py), and reports on + * UART-B. tests/se/run.sh checks the log. + * + * rsa se_rsa_key_set() / se_rsa_exp_mod() as Lockpick's bdk has them: + * a 2048-bit and a 1024-bit key in the two slots, each signing with + * its private exponent and verifying with e = 65537 + * sha SHA-256 one-shot, and in two parts (INIT, then CONTINUE) + * aes AES-128 ECB, Lockpick's keyslot-access vector + * rng se_rng_pseudo(): output that is not all zeros, differs from call + * to call, and stops at the size asked for + */ + +typedef unsigned int u32; +typedef unsigned char u8; + +#define REG(a) (*(volatile u32 *)(a)) + +#define UARTB_THR 0x70006040u +#define I2C5 0x7000D000u +#define SE 0x70012000u +#define SER(o) REG(SE + (o)) + +#define SE_OPERATION 0x008 +#define SE_INT_STATUS 0x010 +#define SE_CONFIG 0x014 +#define SE_IN_LL_ADDR 0x018 +#define SE_OUT_LL_ADDR 0x024 +#define SE_HASH_RESULT 0x030 +#define SE_SHA_CONFIG 0x200 +#define SE_SHA_MSG_LENGTH 0x204 +#define SE_SHA_MSG_LEFT 0x214 +#define SE_CRYPTO_CONFIG 0x304 +#define SE_LAST_BLOCK 0x318 +#define SE_KEYTABLE_ADDR 0x31C +#define SE_KEYTABLE_DATA 0x320 +#define SE_RNG_CONFIG 0x340 +#define SE_RSA_CONFIG 0x400 +#define SE_RSA_KEY_SIZE 0x404 +#define SE_RSA_EXP_SIZE 0x408 +#define SE_RSA_KT_ADDR 0x420 +#define SE_RSA_KT_DATA 0x424 +#define SE_RSA_OUTPUT 0x428 +#define SE_ERR_STATUS 0x804 + +#define OP_DONE (1u << 4) +#define ERR_STAT (1u << 16) + +#define DST(x) ((x) << 2) +#define DEC_ALG(x) ((x) << 8) +#define ENC_ALG(x) ((x) << 12) +#define ENC_MODE(x) ((x) << 24) +#define DST_MEMORY 0 +#define DST_HASHREG 1 +#define DST_RSAREG 4 +#define ALG_AES 1 +#define ALG_RNG 2 +#define ALG_SHA 3 +#define ALG_RSA 4 +#define MODE_SHA256 5 + +#include "vectors.h" + +static void puts_(const char *s) { + while (*s) + REG(UARTB_THR) = (u32)(unsigned char)*s++; +} + +static void i2c5_write(u32 dev, u32 reg, u32 val) { + REG(I2C5 + 0x04) = dev << 1; /* CMD_ADDR0 */ + REG(I2C5 + 0x0C) = reg | (val << 8); /* CMD_DATA1 */ + REG(I2C5 + 0x00) = (1u << 1) | (1u << 9); /* CNFG: 2 bytes, write, GO */ +} + +static int failed; + +static void check(int ok, const char *what) { + puts_(ok ? "se test: ok " : "se test: FAIL "); + puts_(what); + puts_("\n"); + if (!ok) + failed = 1; +} + +static int same(const u8 *a, const u8 *b, u32 n) { + for (u32 i = 0; i < n; i++) + if (a[i] != b[i]) + return 0; + return 1; +} + +static u32 be32(const u8 *p) { + return (u32)p[0] << 24 | (u32)p[1] << 16 | (u32)p[2] << 8 | p[3]; +} + +static void put_be32(u8 *p, u32 v) { + p[0] = v >> 24; + p[1] = v >> 16; + p[2] = v >> 8; + p[3] = v; +} + +/* bdk's _se_execute_oneshot(): one linked-list entry each way. */ +static volatile u32 ll_in[3], ll_out[3]; + +static int se_exec(void *dst, u32 dst_size, const void *src, u32 src_size) { + ll_in[0] = 0; + ll_in[1] = (u32)src; + ll_in[2] = src_size; + ll_out[0] = 0; + ll_out[1] = (u32)dst; + ll_out[2] = dst_size; + SER(SE_IN_LL_ADDR) = src ? (u32)ll_in : 0; + SER(SE_OUT_LL_ADDR) = dst ? (u32)ll_out : 0; + SER(SE_ERR_STATUS) = SER(SE_ERR_STATUS); + SER(SE_INT_STATUS) = SER(SE_INT_STATUS); + SER(SE_OPERATION) = 1; /* START */ + while (!(SER(SE_INT_STATUS) & OP_DONE)) + ; + return (SER(SE_INT_STATUS) & ERR_STAT) || SER(SE_ERR_STATUS); +} + +/* se_rsa_key_set(): the big-endian key goes in backwards, word by word. */ +static void rsa_key_set(u32 ks, const u8 *mod, u32 mod_size, const u8 *exp, + u32 exp_size) { + for (u32 i = 0; i < mod_size / 4; i++) { + SER(SE_RSA_KT_ADDR) = ks << 7 | 1u << 6 | i; /* MOD */ + SER(SE_RSA_KT_DATA) = be32(mod + mod_size - 4 - 4 * i); + } + for (u32 i = 0; i < exp_size / 4; i++) { + SER(SE_RSA_KT_ADDR) = ks << 7 | i; /* EXP */ + SER(SE_RSA_KT_DATA) = be32(exp + exp_size - 4 - 4 * i); + } +} + +/* se_rsa_exp_mod(): the input reversed into a little-endian number, the + * output read back from SE_RSA_OUTPUT into big-endian bytes. */ +static int rsa_exp_mod(u32 ks, u8 *dst, const u8 *src, u32 size, + u32 exp_size) { + static u8 buf[256]; + for (u32 i = 0; i < size; i++) + buf[i] = src[size - 1 - i]; + SER(SE_CONFIG) = ENC_ALG(ALG_RSA) | DST(DST_RSAREG); + SER(SE_RSA_CONFIG) = ks << 24; + SER(SE_RSA_KEY_SIZE) = (size >> 6) - 1; + SER(SE_RSA_EXP_SIZE) = exp_size >> 2; + int res = se_exec(0, 0, buf, size); + for (u32 i = 0; i < size / 4; i++) + put_be32(dst + size - 4 - 4 * i, SER(SE_RSA_OUTPUT + 4 * i)); + return res; +} + +/* _se_sha_hash_256(): total 0 or equal to the part means INIT; a part + * that is not the last says one byte more is left than it carries. */ +static int sha256(u8 *hash, u32 total, const u8 *src, u32 size) { + u32 left = total < size ? size + 1 : size; + SER(SE_CONFIG) = ENC_MODE(MODE_SHA256) | ENC_ALG(ALG_SHA) | DST(DST_HASHREG); + SER(SE_SHA_MSG_LENGTH) = total << 3; + SER(SE_SHA_MSG_LENGTH + 4) = total >> 29; + SER(SE_SHA_MSG_LENGTH + 8) = 0; + SER(SE_SHA_MSG_LENGTH + 12) = 0; + SER(SE_SHA_MSG_LEFT) = left << 3; + SER(SE_SHA_MSG_LEFT + 4) = left >> 29; + SER(SE_SHA_MSG_LEFT + 8) = 0; + SER(SE_SHA_MSG_LEFT + 12) = 0; + SER(SE_SHA_CONFIG) = (total == size || !total) ? 1 : 0; + int res = se_exec(0, 0, src, size); + for (u32 i = 0; i < 8; i++) + put_be32(hash + 4 * i, SER(SE_HASH_RESULT + 4 * i)); + return res; +} + +/* se_rng_pseudo() for a whole number of blocks. */ +static int rng(u8 *dst, u32 size) { + SER(SE_CONFIG) = ENC_ALG(ALG_RNG) | DST(DST_MEMORY); + SER(SE_CRYPTO_CONFIG) = 1u << 3; /* INPUT_RANDOM */ + SER(SE_RNG_CONFIG) = 1u << 2; /* SRC_ENTROPY, MODE_NORMAL */ + SER(SE_LAST_BLOCK) = (size >> 4) - 1; + return se_exec(dst, size, 0, 0); +} + +static const u8 e65537[4] = {0x00, 0x01, 0x00, 0x01}; + +__attribute__((section(".text.start"), noreturn)) +void _start(void) { + static u8 out[256], hash[32], blk[16], a[48], b[48]; + + puts_("se test: start\n"); + + /* RSA: both slots loaded before either is used. */ + rsa_key_set(0, k2048_mod, 256, k2048_exp, 256); + rsa_key_set(1, k1024_mod, 128, k1024_exp, 128); + check(!rsa_exp_mod(0, out, k2048_msg, 256, 256) && + same(out, k2048_sig, 256), "RSA-2048 m^d mod n"); + check(!rsa_exp_mod(1, out, k1024_msg, 128, 128) && + same(out, k1024_sig, 128), "RSA-1024 m^d mod n in slot 1"); + rsa_key_set(0, k2048_mod, 256, e65537, 4); + check(!rsa_exp_mod(0, out, k2048_sig, 256, 4) && + same(out, k2048_msg, 256), "RSA-2048 s^e mod n gives m back"); + rsa_key_set(1, k1024_mod, 128, e65537, 4); + check(!rsa_exp_mod(1, out, k1024_sig, 128, 4) && + same(out, k1024_msg, 128), "RSA-1024 s^e mod n gives m back"); + + /* SHA-256. */ + static const u8 abc[3] = {'a', 'b', 'c'}; + check(!sha256(hash, 3, abc, 3) && same(hash, sha_abc_digest, 32), + "SHA-256 one-shot"); + check(!sha256(hash, 0, sha_msg, 128) && + !sha256(hash, 200, sha_msg + 128, 72) && + same(hash, sha_msg_digest, 32), "SHA-256 in two parts"); + + /* AES-128 ECB decrypt of a zero block under 00..0F. */ + static const u8 aes_exp[16] = {0x7B, 0x1D, 0x29, 0xA1, 0x6C, 0xF8, + 0xCC, 0xAB, 0x84, 0xF0, 0xB8, 0xA5, + 0x98, 0xE4, 0x2F, 0xA6}; + for (u32 i = 0; i < 4; i++) { + SER(SE_KEYTABLE_ADDR) = 5u << 4 | i; /* slot 5, KEYS_0_3 */ + SER(SE_KEYTABLE_DATA) = (4 * i) | (4 * i + 1) << 8 | + (4 * i + 2) << 16 | (u32)(4 * i + 3) << 24; + } + for (u32 i = 0; i < 16; i++) + blk[i] = 0; + SER(SE_CONFIG) = DEC_ALG(ALG_AES) | DST(DST_MEMORY); + SER(SE_CRYPTO_CONFIG) = 5u << 24; /* KEY_INDEX 5, CORE_DECRYPT */ + SER(SE_LAST_BLOCK) = 0; + check(!se_exec(blk, 16, blk, 16) && same(blk, aes_exp, 16), + "AES-128 ECB decrypt"); + + /* RNG: 32 bytes twice, into zeroed buffers with a sentinel tail. */ + for (u32 i = 0; i < 48; i++) + a[i] = b[i] = i < 32 ? 0 : 0x5A; + int res = rng(a, 32) | rng(b, 32); + int zero = 1, tail = 1; + for (u32 i = 0; i < 32; i++) + if (a[i]) + zero = 0; + for (u32 i = 32; i < 48; i++) + if (a[i] != 0x5A || b[i] != 0x5A) + tail = 0; + check(!res && !zero, "RNG fills the buffer"); + check(!same(a, b, 32), "RNG output changes between calls"); + check(tail, "RNG stops at the size asked for"); + + puts_(failed ? "se test: done, with failures\n" : "se test: done\n"); + i2c5_write(0x3C, 0x41, 0x02); /* MAX77620 ONOFFCNFG1: PWR_OFF */ + for (;;) + ; +} diff --git a/tests/se/run.sh b/tests/se/run.sh new file mode 100755 index 0000000..854da3d --- /dev/null +++ b/tests/se/run.sh @@ -0,0 +1,26 @@ +#!/bin/sh +# Security Engine regression test: run tests/se/payload.c and check what it +# reports. Usage: tests/se/run.sh [rcm_emu] [payload] +EMU=${1:-./rcm_emu} +PAYLOAD=${2:-tests/se/payload.bin} +LOG=${LOG:-tests/se/out.log} + +SDL_VIDEODRIVER=dummy RCM_EMU_NO_AUDIO=1 timeout 60 "$EMU" "$PAYLOAD" \ + > "$LOG" 2>&1 +status=$? + +echo "se test (emulator exit $status):" +# The payload's lines arrive through the UART-B log, tagged [uartB]. +grep '^\[uartB\] se test: \(ok\|FAIL\) ' "$LOG" | + sed -e 's/^.*se test: ok */ ok /' -e 's/^.*se test: FAIL */ FAIL /' +fail=0 +grep -q 'se test: FAIL' "$LOG" && fail=1 +if ! grep -q '^\[uartB\] se test: done$' "$LOG"; then + echo " FAIL the payload did not finish cleanly" + fail=1 +fi +if [ "$status" -ne 0 ]; then + echo " FAIL the emulator exited with status $status (124 = timed out)" + fail=1 +fi +[ $fail -eq 0 ] && echo "se test: PASS" || { echo "se test: FAIL (log: $LOG)"; exit 1; } diff --git a/tests/se/vectors.h b/tests/se/vectors.h new file mode 100644 index 0000000..3d483ba --- /dev/null +++ b/tests/se/vectors.h @@ -0,0 +1,190 @@ +/* Generated by gen_vectors.py: RSA keys (e = 65537) with + * m^d mod n, and SHA-256 digests, computed with Python integers and hashlib. */ + +static const unsigned char k2048_mod[256] = { + 0xDB, 0x84, 0xB5, 0x8A, 0xA4, 0x6A, 0xC2, 0x45, 0x45, 0x22, 0x94, 0x72, + 0x4B, 0xAC, 0xC7, 0xB9, 0x34, 0x68, 0x99, 0xA8, 0x81, 0x91, 0x99, 0x8D, + 0xEF, 0x69, 0xA2, 0xFE, 0xAC, 0x99, 0xD5, 0x36, 0x8D, 0x80, 0x27, 0x17, + 0x39, 0xC2, 0x33, 0xE5, 0x12, 0x23, 0xF0, 0x81, 0x28, 0xD8, 0x30, 0x5B, + 0x20, 0x7E, 0x9B, 0xD2, 0xE1, 0xA0, 0xE4, 0xBB, 0x12, 0x9C, 0xB9, 0x44, + 0x45, 0x99, 0x02, 0xEF, 0xDE, 0x5E, 0x21, 0x9B, 0x5E, 0x3E, 0xBF, 0x91, + 0x9E, 0x07, 0xBA, 0x6D, 0xE1, 0x5E, 0xBB, 0x2D, 0x10, 0x26, 0x20, 0x97, + 0xFC, 0x7F, 0xD4, 0x51, 0xA7, 0x17, 0x94, 0xB1, 0x07, 0x56, 0xD5, 0x7E, + 0xE9, 0xBE, 0xD4, 0x8A, 0x0B, 0xA6, 0xF9, 0x2A, 0x7A, 0x78, 0xE6, 0xC0, + 0x36, 0xE7, 0x53, 0xD4, 0x77, 0x3D, 0x8A, 0x79, 0x7B, 0xAF, 0x72, 0x80, + 0x82, 0x37, 0x00, 0x7E, 0xE4, 0x40, 0xBB, 0x75, 0x85, 0xB1, 0x34, 0xC3, + 0x3B, 0xC4, 0xE7, 0xCF, 0xCA, 0x7B, 0xA3, 0x7B, 0xEF, 0x49, 0xC7, 0x52, + 0x15, 0x79, 0xDC, 0xF9, 0x6C, 0xAE, 0xFB, 0xDC, 0xA5, 0x7A, 0x5B, 0x35, + 0x4C, 0x74, 0xAD, 0x1F, 0x7A, 0xDB, 0x8A, 0x1E, 0x2B, 0xDC, 0xD4, 0x3B, + 0x65, 0x21, 0x15, 0x21, 0xB5, 0xD3, 0x20, 0x62, 0x94, 0xAC, 0x11, 0x9F, + 0x11, 0xEA, 0x8B, 0xB6, 0xE8, 0xCA, 0xD1, 0x54, 0x26, 0x16, 0xEB, 0xED, + 0xF4, 0x7A, 0xFB, 0x92, 0x5C, 0xD7, 0x72, 0xE8, 0x3B, 0xAF, 0x63, 0x58, + 0xCC, 0x8B, 0x2B, 0x8A, 0x1D, 0x78, 0x5F, 0xC2, 0xFA, 0xD9, 0xFB, 0x59, + 0x50, 0x43, 0x96, 0x1C, 0x03, 0x6C, 0x2D, 0x99, 0x8A, 0xE8, 0x92, 0xA2, + 0xE4, 0x62, 0x0A, 0x29, 0x35, 0x93, 0x02, 0x26, 0x31, 0x2D, 0x07, 0xE7, + 0x99, 0xE5, 0xDA, 0x37, 0x2B, 0xD4, 0xA4, 0x39, 0x9B, 0xC8, 0x2B, 0x8D, + 0x2A, 0x51, 0x34, 0x23, +}; + +static const unsigned char k2048_exp[256] = { + 0xB3, 0xBD, 0x44, 0xDC, 0x41, 0x65, 0xBC, 0xAD, 0xF5, 0x3E, 0xAC, 0x27, + 0xAD, 0xB8, 0xCA, 0x4B, 0xE6, 0x12, 0xA3, 0x86, 0x2A, 0x99, 0x2A, 0x0E, + 0xE2, 0x72, 0x2F, 0x68, 0xB1, 0xB0, 0xBF, 0xB8, 0x8C, 0x41, 0xDB, 0x40, + 0x14, 0x25, 0xE4, 0x51, 0xF2, 0xF9, 0x84, 0x6A, 0x6D, 0x24, 0x1E, 0xEB, + 0xEE, 0x7E, 0x5C, 0x5E, 0x5E, 0x2A, 0x5C, 0x3E, 0xA4, 0xAD, 0xB8, 0xB0, + 0xA2, 0xFF, 0xF7, 0x3C, 0xAA, 0x69, 0x8E, 0xC0, 0xE5, 0x79, 0x21, 0x68, + 0x3C, 0xE9, 0xFD, 0x07, 0x0D, 0x5A, 0x80, 0x8E, 0x14, 0x43, 0xF3, 0xBA, + 0x1E, 0xA0, 0x18, 0x1B, 0xC7, 0x61, 0xF2, 0x78, 0xC5, 0x16, 0x9F, 0x76, + 0xEC, 0x33, 0x39, 0x59, 0xD2, 0xD2, 0xA3, 0xFF, 0xD9, 0x09, 0xF7, 0x7E, + 0x79, 0x36, 0x0E, 0x18, 0xD0, 0x8D, 0x8E, 0x3D, 0x8C, 0x3D, 0x8A, 0xD6, + 0x98, 0x48, 0x2A, 0xDA, 0xBB, 0x50, 0x19, 0x6C, 0x4B, 0x8B, 0x24, 0x84, + 0xF4, 0x28, 0x0B, 0xFA, 0x6E, 0x64, 0xC4, 0x63, 0xDC, 0x3C, 0xDF, 0x90, + 0xF4, 0x8D, 0x02, 0xEC, 0x58, 0xFB, 0x3E, 0xAE, 0x05, 0x3C, 0xFC, 0x20, + 0xB8, 0x93, 0x02, 0x8E, 0x62, 0x3B, 0x29, 0xDA, 0x3E, 0xF7, 0x8C, 0xCC, + 0x16, 0x30, 0x83, 0xF1, 0xFD, 0xA4, 0x56, 0x0C, 0xD1, 0x09, 0x77, 0x61, + 0x58, 0xC0, 0xCE, 0xE7, 0x5E, 0xA1, 0x0F, 0x66, 0x18, 0x81, 0x96, 0x06, + 0xD1, 0xCF, 0xCC, 0x5B, 0x1F, 0x9A, 0x1F, 0x25, 0x79, 0xCB, 0x66, 0x28, + 0x6A, 0xE1, 0x61, 0x60, 0x16, 0x0E, 0x73, 0x5A, 0x50, 0xDC, 0x9C, 0xE4, + 0xB3, 0x0E, 0xFD, 0x50, 0x90, 0xD3, 0x63, 0x2D, 0x9E, 0xDB, 0xEA, 0x61, + 0x90, 0x65, 0xB0, 0xE0, 0xB8, 0xFF, 0xE6, 0x9D, 0x78, 0xB3, 0x8F, 0x68, + 0x19, 0xFE, 0x45, 0x2B, 0x91, 0xD8, 0x0F, 0xB2, 0x2E, 0x90, 0x47, 0xA7, + 0x80, 0xA0, 0x73, 0xB9, +}; + +static const unsigned char k2048_msg[256] = { + 0x7E, 0x8E, 0x33, 0x8D, 0x7B, 0xD2, 0xAE, 0x8D, 0x55, 0xB2, 0xFF, 0x7E, + 0x47, 0x6F, 0x6C, 0x3F, 0x8F, 0xAA, 0x20, 0x78, 0xEB, 0xF8, 0x6D, 0xA4, + 0x36, 0xF1, 0x6E, 0x98, 0x64, 0x00, 0xCC, 0x4A, 0x7E, 0x8E, 0x33, 0x8D, + 0x7B, 0xD2, 0xAE, 0x8D, 0x55, 0xB2, 0xFF, 0x7E, 0x47, 0x6F, 0x6C, 0x3F, + 0x8F, 0xAA, 0x20, 0x78, 0xEB, 0xF8, 0x6D, 0xA4, 0x36, 0xF1, 0x6E, 0x98, + 0x64, 0x00, 0xCC, 0x4A, 0x7E, 0x8E, 0x33, 0x8D, 0x7B, 0xD2, 0xAE, 0x8D, + 0x55, 0xB2, 0xFF, 0x7E, 0x47, 0x6F, 0x6C, 0x3F, 0x8F, 0xAA, 0x20, 0x78, + 0xEB, 0xF8, 0x6D, 0xA4, 0x36, 0xF1, 0x6E, 0x98, 0x64, 0x00, 0xCC, 0x4A, + 0x7E, 0x8E, 0x33, 0x8D, 0x7B, 0xD2, 0xAE, 0x8D, 0x55, 0xB2, 0xFF, 0x7E, + 0x47, 0x6F, 0x6C, 0x3F, 0x8F, 0xAA, 0x20, 0x78, 0xEB, 0xF8, 0x6D, 0xA4, + 0x36, 0xF1, 0x6E, 0x98, 0x64, 0x00, 0xCC, 0x4A, 0x7E, 0x8E, 0x33, 0x8D, + 0x7B, 0xD2, 0xAE, 0x8D, 0x55, 0xB2, 0xFF, 0x7E, 0x47, 0x6F, 0x6C, 0x3F, + 0x8F, 0xAA, 0x20, 0x78, 0xEB, 0xF8, 0x6D, 0xA4, 0x36, 0xF1, 0x6E, 0x98, + 0x64, 0x00, 0xCC, 0x4A, 0x7E, 0x8E, 0x33, 0x8D, 0x7B, 0xD2, 0xAE, 0x8D, + 0x55, 0xB2, 0xFF, 0x7E, 0x47, 0x6F, 0x6C, 0x3F, 0x8F, 0xAA, 0x20, 0x78, + 0xEB, 0xF8, 0x6D, 0xA4, 0x36, 0xF1, 0x6E, 0x98, 0x64, 0x00, 0xCC, 0x4A, + 0x7E, 0x8E, 0x33, 0x8D, 0x7B, 0xD2, 0xAE, 0x8D, 0x55, 0xB2, 0xFF, 0x7E, + 0x47, 0x6F, 0x6C, 0x3F, 0x8F, 0xAA, 0x20, 0x78, 0xEB, 0xF8, 0x6D, 0xA4, + 0x36, 0xF1, 0x6E, 0x98, 0x64, 0x00, 0xCC, 0x4A, 0x7E, 0x8E, 0x33, 0x8D, + 0x7B, 0xD2, 0xAE, 0x8D, 0x55, 0xB2, 0xFF, 0x7E, 0x47, 0x6F, 0x6C, 0x3F, + 0x8F, 0xAA, 0x20, 0x78, 0xEB, 0xF8, 0x6D, 0xA4, 0x36, 0xF1, 0x6E, 0x98, + 0x64, 0x00, 0xCC, 0x4A, +}; + +static const unsigned char k2048_sig[256] = { + 0x84, 0xFE, 0x52, 0x90, 0x0E, 0x74, 0xA7, 0x53, 0xCC, 0xEB, 0x46, 0x40, + 0x2D, 0x8A, 0x38, 0xFC, 0x14, 0x52, 0xD7, 0x8E, 0xC9, 0xC8, 0x17, 0xB7, + 0xE3, 0x17, 0x8E, 0x8D, 0x4C, 0xB8, 0xB1, 0xDB, 0x2D, 0xCA, 0xD5, 0x34, + 0xDE, 0xEC, 0x4B, 0x60, 0x7A, 0x53, 0xBC, 0x96, 0x62, 0xCA, 0xA5, 0x26, + 0xB3, 0x35, 0x9C, 0x4F, 0x30, 0xB4, 0xAC, 0x6A, 0x74, 0xDB, 0x6E, 0x13, + 0x70, 0xC6, 0xE4, 0x7F, 0xB5, 0x76, 0x4A, 0x41, 0x9D, 0x2C, 0x86, 0xDA, + 0x9C, 0xE6, 0x87, 0x32, 0x62, 0x71, 0xB6, 0xE7, 0xDB, 0x79, 0xC2, 0x07, + 0xF4, 0x24, 0x33, 0xC4, 0x60, 0x3E, 0xC8, 0x10, 0x17, 0x69, 0xA0, 0x95, + 0xBE, 0x6F, 0xD8, 0x69, 0x79, 0x25, 0xB0, 0x4D, 0x20, 0x9B, 0x33, 0xF1, + 0x86, 0x56, 0x2F, 0x8D, 0x79, 0xB8, 0xB7, 0x78, 0x63, 0x84, 0xB5, 0x4E, + 0xBA, 0x6A, 0xB0, 0x4D, 0xA0, 0x7E, 0x48, 0xDF, 0xD4, 0xC0, 0x96, 0xAD, + 0x2E, 0x23, 0x06, 0x7A, 0xE5, 0xC0, 0x79, 0x57, 0xBB, 0x60, 0xF5, 0x15, + 0x02, 0xBA, 0xED, 0x65, 0x54, 0x7B, 0x9D, 0x20, 0x03, 0xEB, 0x76, 0xDF, + 0xB5, 0xE1, 0xD1, 0x3D, 0x90, 0xD3, 0x73, 0x80, 0xA6, 0x52, 0x27, 0x01, + 0x02, 0x72, 0x4D, 0xCA, 0xBA, 0x30, 0xA8, 0xEF, 0x01, 0xC8, 0xC3, 0x3E, + 0xFA, 0xC4, 0x12, 0x00, 0x2F, 0x02, 0x12, 0x0F, 0xC4, 0x11, 0x74, 0x30, + 0x30, 0x68, 0x4F, 0x16, 0x74, 0x38, 0xBE, 0x25, 0xDA, 0x44, 0xE7, 0xE4, + 0xFF, 0x92, 0x3A, 0x20, 0xC5, 0x32, 0xCB, 0x86, 0xAE, 0x10, 0x47, 0x9B, + 0x71, 0x43, 0xD8, 0x2C, 0x66, 0xE4, 0x13, 0x93, 0xF6, 0xBE, 0x60, 0x15, + 0x51, 0x02, 0x61, 0xD0, 0xAA, 0xAF, 0x13, 0xA1, 0x38, 0x50, 0xF7, 0x0E, + 0x66, 0x4C, 0x8B, 0xF7, 0x46, 0x8A, 0x76, 0xD6, 0xC1, 0x6F, 0x87, 0xC2, + 0x37, 0x5A, 0x47, 0xB9, +}; + +static const unsigned char k1024_mod[128] = { + 0xA9, 0x97, 0xBA, 0xDB, 0x61, 0x4B, 0x0E, 0x12, 0x16, 0x22, 0xC5, 0xD9, + 0x65, 0xD3, 0x01, 0x6E, 0x08, 0xDF, 0x1B, 0x29, 0xFD, 0x01, 0x88, 0xAC, + 0xF3, 0x36, 0xE0, 0xA6, 0x21, 0xC2, 0xE5, 0xB9, 0x21, 0x33, 0xB4, 0x22, + 0x47, 0xEE, 0x31, 0x66, 0xF8, 0x57, 0x9D, 0xC7, 0x6F, 0x72, 0x7C, 0xD7, + 0x30, 0x9B, 0xE7, 0xDF, 0x2E, 0x81, 0xDF, 0xA5, 0x2D, 0x68, 0x0D, 0x5F, + 0xFC, 0x0C, 0xEF, 0xFF, 0x06, 0x27, 0x8A, 0xCF, 0xD5, 0xA2, 0x57, 0xB3, + 0xAB, 0xDE, 0xF6, 0x37, 0x0A, 0x63, 0xCF, 0xC2, 0xF5, 0x19, 0x03, 0x9D, + 0x1E, 0x3C, 0x61, 0xE0, 0x40, 0xAB, 0xAA, 0xC5, 0x39, 0x68, 0x05, 0x9F, + 0x6E, 0xE6, 0x8E, 0x0E, 0x08, 0x9F, 0x2C, 0x78, 0x11, 0xEA, 0xA6, 0x7E, + 0x16, 0xED, 0x58, 0xBF, 0x55, 0x0C, 0x6B, 0xB1, 0x4D, 0x3E, 0x64, 0x0A, + 0x49, 0x47, 0xB7, 0xE3, 0x8F, 0xCF, 0x91, 0x05, +}; + +static const unsigned char k1024_exp[128] = { + 0x61, 0xE6, 0xE6, 0x86, 0xC8, 0x96, 0xB4, 0x6B, 0xBA, 0x7D, 0x9E, 0x6B, + 0x8E, 0xBD, 0x8D, 0xC0, 0xC0, 0xBC, 0x53, 0x09, 0x1D, 0x36, 0x6A, 0x79, + 0x34, 0xFA, 0x44, 0x1B, 0x83, 0xB7, 0xE8, 0x37, 0xC4, 0x33, 0xFD, 0xD4, + 0x20, 0x64, 0x1F, 0xFE, 0xCA, 0x74, 0x81, 0xEF, 0xE4, 0xA5, 0x59, 0xEE, + 0x3B, 0xF0, 0x74, 0x9D, 0xC1, 0x52, 0x56, 0x12, 0x1B, 0xB1, 0x4A, 0xAF, + 0x53, 0x69, 0x00, 0x8D, 0xEE, 0x06, 0x63, 0x28, 0xA0, 0x34, 0xF8, 0x70, + 0xB6, 0x78, 0x5C, 0x0C, 0x9C, 0x16, 0x7D, 0xED, 0x59, 0xA5, 0x05, 0xD4, + 0x4B, 0x82, 0x6A, 0x86, 0x09, 0x1B, 0xC3, 0x85, 0x98, 0xE9, 0x71, 0x38, + 0x5E, 0x07, 0x3A, 0xF4, 0x51, 0xE2, 0xAF, 0xB3, 0xA6, 0x9C, 0x66, 0x6D, + 0x0F, 0x5D, 0xFF, 0x61, 0xF4, 0x45, 0x3B, 0xAE, 0x59, 0xA9, 0xCA, 0xAE, + 0x87, 0x95, 0x91, 0x65, 0xB9, 0x87, 0xD5, 0x01, +}; + +static const unsigned char k1024_msg[128] = { + 0x03, 0x64, 0xE7, 0xA2, 0x21, 0x6E, 0xB6, 0x96, 0x08, 0x46, 0xDD, 0x04, + 0xAB, 0xB9, 0xD7, 0x96, 0x2B, 0xCE, 0x7C, 0xE7, 0x11, 0xC9, 0x11, 0xD8, + 0xEB, 0x51, 0xFA, 0xA5, 0x21, 0xC3, 0xDC, 0x82, 0x03, 0x64, 0xE7, 0xA2, + 0x21, 0x6E, 0xB6, 0x96, 0x08, 0x46, 0xDD, 0x04, 0xAB, 0xB9, 0xD7, 0x96, + 0x2B, 0xCE, 0x7C, 0xE7, 0x11, 0xC9, 0x11, 0xD8, 0xEB, 0x51, 0xFA, 0xA5, + 0x21, 0xC3, 0xDC, 0x82, 0x03, 0x64, 0xE7, 0xA2, 0x21, 0x6E, 0xB6, 0x96, + 0x08, 0x46, 0xDD, 0x04, 0xAB, 0xB9, 0xD7, 0x96, 0x2B, 0xCE, 0x7C, 0xE7, + 0x11, 0xC9, 0x11, 0xD8, 0xEB, 0x51, 0xFA, 0xA5, 0x21, 0xC3, 0xDC, 0x82, + 0x03, 0x64, 0xE7, 0xA2, 0x21, 0x6E, 0xB6, 0x96, 0x08, 0x46, 0xDD, 0x04, + 0xAB, 0xB9, 0xD7, 0x96, 0x2B, 0xCE, 0x7C, 0xE7, 0x11, 0xC9, 0x11, 0xD8, + 0xEB, 0x51, 0xFA, 0xA5, 0x21, 0xC3, 0xDC, 0x82, +}; + +static const unsigned char k1024_sig[128] = { + 0x36, 0xF0, 0x8F, 0xE4, 0xDD, 0x4A, 0x6C, 0x54, 0x6F, 0xC7, 0x87, 0xC5, + 0xD3, 0x5E, 0x08, 0x6F, 0x07, 0x65, 0xDA, 0xCD, 0xC2, 0x05, 0xE7, 0x83, + 0x6D, 0x92, 0x6A, 0xB5, 0x26, 0xBA, 0xB0, 0xED, 0xC3, 0x09, 0x11, 0x27, + 0xD0, 0x4C, 0x67, 0xF1, 0xC7, 0xF7, 0xA2, 0xA2, 0x46, 0x7E, 0xCB, 0x58, + 0x48, 0x21, 0xAB, 0x77, 0x85, 0x75, 0x17, 0xDF, 0xD6, 0xE6, 0xEC, 0x25, + 0x3D, 0x77, 0x45, 0x31, 0xAB, 0x0B, 0xD8, 0x6F, 0xF8, 0xCD, 0xD6, 0x11, + 0x7B, 0xEF, 0xA5, 0xBB, 0xFC, 0xE6, 0xCB, 0x97, 0x0F, 0x2A, 0xA4, 0x11, + 0x25, 0xDA, 0xAC, 0x16, 0x2F, 0x2A, 0x85, 0x28, 0x7C, 0x62, 0xBD, 0xAB, + 0x44, 0xC3, 0x34, 0x33, 0x0B, 0x8B, 0x0C, 0x77, 0xEB, 0xDE, 0x07, 0xF3, + 0xC3, 0xAA, 0x9C, 0xA6, 0xE3, 0x62, 0x97, 0xF1, 0xA1, 0x05, 0x3C, 0x78, + 0x37, 0x99, 0xDE, 0xB7, 0x08, 0x4F, 0x9F, 0x46, +}; + +static const unsigned char sha_msg[200] = { + 0x03, 0x0A, 0x11, 0x18, 0x1F, 0x26, 0x2D, 0x34, 0x3B, 0x42, 0x49, 0x50, + 0x57, 0x5E, 0x65, 0x6C, 0x73, 0x7A, 0x81, 0x88, 0x8F, 0x96, 0x9D, 0xA4, + 0xAB, 0xB2, 0xB9, 0xC0, 0xC7, 0xCE, 0xD5, 0xDC, 0xE3, 0xEA, 0xF1, 0xF8, + 0xFF, 0x06, 0x0D, 0x14, 0x1B, 0x22, 0x29, 0x30, 0x37, 0x3E, 0x45, 0x4C, + 0x53, 0x5A, 0x61, 0x68, 0x6F, 0x76, 0x7D, 0x84, 0x8B, 0x92, 0x99, 0xA0, + 0xA7, 0xAE, 0xB5, 0xBC, 0xC3, 0xCA, 0xD1, 0xD8, 0xDF, 0xE6, 0xED, 0xF4, + 0xFB, 0x02, 0x09, 0x10, 0x17, 0x1E, 0x25, 0x2C, 0x33, 0x3A, 0x41, 0x48, + 0x4F, 0x56, 0x5D, 0x64, 0x6B, 0x72, 0x79, 0x80, 0x87, 0x8E, 0x95, 0x9C, + 0xA3, 0xAA, 0xB1, 0xB8, 0xBF, 0xC6, 0xCD, 0xD4, 0xDB, 0xE2, 0xE9, 0xF0, + 0xF7, 0xFE, 0x05, 0x0C, 0x13, 0x1A, 0x21, 0x28, 0x2F, 0x36, 0x3D, 0x44, + 0x4B, 0x52, 0x59, 0x60, 0x67, 0x6E, 0x75, 0x7C, 0x83, 0x8A, 0x91, 0x98, + 0x9F, 0xA6, 0xAD, 0xB4, 0xBB, 0xC2, 0xC9, 0xD0, 0xD7, 0xDE, 0xE5, 0xEC, + 0xF3, 0xFA, 0x01, 0x08, 0x0F, 0x16, 0x1D, 0x24, 0x2B, 0x32, 0x39, 0x40, + 0x47, 0x4E, 0x55, 0x5C, 0x63, 0x6A, 0x71, 0x78, 0x7F, 0x86, 0x8D, 0x94, + 0x9B, 0xA2, 0xA9, 0xB0, 0xB7, 0xBE, 0xC5, 0xCC, 0xD3, 0xDA, 0xE1, 0xE8, + 0xEF, 0xF6, 0xFD, 0x04, 0x0B, 0x12, 0x19, 0x20, 0x27, 0x2E, 0x35, 0x3C, + 0x43, 0x4A, 0x51, 0x58, 0x5F, 0x66, 0x6D, 0x74, +}; + +static const unsigned char sha_msg_digest[32] = { + 0x2C, 0x7E, 0x18, 0xC9, 0x42, 0xEF, 0x06, 0x5B, 0x52, 0x6A, 0x2D, 0x4E, + 0x55, 0x46, 0x28, 0x37, 0x49, 0xCD, 0x3D, 0xDF, 0xB5, 0x1D, 0x8F, 0xC7, + 0x1F, 0x42, 0x71, 0x73, 0x63, 0x68, 0x5F, 0x46, +}; + +static const unsigned char sha_abc_digest[32] = { + 0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA, 0x41, 0x41, 0x40, 0xDE, + 0x5D, 0xAE, 0x22, 0x23, 0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C, + 0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD, +};