nrf52840dk_test_kernel/test/
ecdsa_p256_test.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2023.
4
5//! This tests a software ECDSA P256 implementation. To run this test,
6//! add this line to the boot sequence:
7//! ```
8//! test::ecdsa_p256_test::run_ecdsa_p256();
9//! ```
10
11use capsules_core::test::capsule_test::{CapsuleTest, CapsuleTestClient};
12use core::ptr::addr_of_mut;
13use ecdsa_sw::p256_signer::EcdsaP256SignatureSigner;
14use ecdsa_sw::test::p256::TestEcdsaP256Sign;
15use kernel::static_init;
16
17// HHASH is the hash to sign: this is just the SHA-256 hash of "hello world" as
18// in the SHA-256 test.
19pub static mut HHASH: [u8; 32] = [
20    0xB9, 0x4D, 0x27, 0xB9, 0x93, 0x4D, 0x3E, 0x08, 0xA5, 0x2E, 0x52, 0xD7, 0xDA, 0x7D, 0xAB, 0xFA,
21    0xC4, 0x84, 0xEF, 0xE3, 0x7A, 0x53, 0x80, 0xEE, 0x90, 0x88, 0xF7, 0xAC, 0xE2, 0xEF, 0xCD, 0xE9,
22];
23
24// SKEY is the secret key used to for signing, encoded as the secret scalar d in
25// big-endian byte order.
26//
27// - `ec-secp256r1-priv-key.pem`:
28//
29//   -----BEGIN EC PRIVATE KEY-----
30//   MHcCAQEEIGU0zCXHLqxDmrHHAWEQP5zNfWRQrAiIpH9YwxHlqysmoAoGCCqGSM49
31//   AwEHoUQDQgAE4BM6kKdKNWFRjuFECfFpwc9q239+Uvi3QXniTVdBI1IuthIDs4UQ
32//   5fMlB2KPVJWCV0VQvaPiF+g0MIkmTCNisQ==
33//   -----END EC PRIVATE KEY-----
34//
35pub static mut SKEY: [u8; 32] = [
36    0x65, 0x34, 0xCC, 0x25, 0xC7, 0x2E, 0xAC, 0x43, 0x9A, 0xB1, 0xC7, 0x01, 0x61, 0x10, 0x3F, 0x9C,
37    0xCD, 0x7D, 0x64, 0x50, 0xAC, 0x08, 0x88, 0xA4, 0x7F, 0x58, 0xC3, 0x11, 0xE5, 0xAB, 0x2B, 0x26,
38];
39
40// HSIG is the buffer for storing the resulting signature of the hash in HHASH.
41pub static mut HSIG: [u8; 64] = [0; 64];
42
43// SSIG is the buffer storing the correct ECDSA P-256 signature using
44// deterministic (RFC 6979) nonce generation to compare with, encoded as the
45// values r and s both in big-endian byte order concatenated.
46pub static mut CSIG: [u8; 64] = [
47    0x9E, 0xB8, 0x19, 0x40, 0xD4, 0xA9, 0xE5, 0x5E, 0x84, 0x08, 0xDB, 0xE8, 0xCB, 0x5A, 0x1F, 0x3C,
48    0x01, 0x18, 0x1C, 0xD1, 0x92, 0xEC, 0xCE, 0x1E, 0x4B, 0x80, 0x22, 0x94, 0xB1, 0xFB, 0x67, 0x31,
49    0xFE, 0xEF, 0xDD, 0x23, 0x08, 0x76, 0x41, 0x0B, 0x03, 0x9E, 0x2A, 0x62, 0xCA, 0xA8, 0x32, 0x03,
50    0x4A, 0x63, 0x2C, 0x91, 0xC8, 0xDE, 0xDE, 0x70, 0x5E, 0x67, 0xBA, 0x3A, 0xBE, 0xE1, 0xFE, 0x96,
51];
52
53pub unsafe fn run_ecdsa_p256(client: &'static dyn CapsuleTestClient) {
54    let t = static_init_test_ecdsa_p256(client);
55    t.run();
56}
57
58unsafe fn static_init_test_ecdsa_p256(
59    client: &'static dyn CapsuleTestClient,
60) -> &'static TestEcdsaP256Sign {
61    let ecdsa = static_init!(
62        EcdsaP256SignatureSigner<'static>,
63        EcdsaP256SignatureSigner::new(&mut *addr_of_mut!(SKEY)),
64    );
65    kernel::deferred_call::DeferredCallClient::register(ecdsa);
66
67    let test = static_init!(
68        TestEcdsaP256Sign,
69        TestEcdsaP256Sign::new(
70            ecdsa,
71            &mut *addr_of_mut!(HHASH),
72            &mut *addr_of_mut!(HSIG),
73            &mut *addr_of_mut!(CSIG)
74        )
75    );
76
77    test.set_client(client);
78
79    test
80}