From 9bfdb3eadedf61388283a04ec603b52e38629e67 Mon Sep 17 00:00:00 2001 From: Christopher Swenson Date: Wed, 18 Nov 2026 15:11:04 -0800 Subject: [PATCH] veer: Add an ECDSA P-384 verify driver for Caliptra The driver implements the EcdsaVerify HAL trait with the ECDSA384_SIGNATURE_VERIFY mailbox command. The test verifies an RFC 6979 vector and checks that the driver rejects other digests, keys and signatures. It passes on the emulator and on the VCK190 FPGA. Assisted-by: Claude:claude-fable-5-2 --- target/veer/peripherals/BUILD.bazel | 15 ++ target/veer/peripherals/ecdsa.rs | 154 ++++++++++++++ target/veer/tests/ecdsa/BUILD.bazel | 97 +++++++++ target/veer/tests/ecdsa/system.json5.tmpl | 49 +++++ target/veer/tests/ecdsa/target.rs | 36 ++++ target/veer/tests/ecdsa/test_ecdsa.rs | 248 ++++++++++++++++++++++ 6 files changed, 599 insertions(+) create mode 100644 target/veer/peripherals/ecdsa.rs create mode 100644 target/veer/tests/ecdsa/BUILD.bazel create mode 100644 target/veer/tests/ecdsa/system.json5.tmpl create mode 100644 target/veer/tests/ecdsa/target.rs create mode 100644 target/veer/tests/ecdsa/test_ecdsa.rs diff --git a/target/veer/peripherals/BUILD.bazel b/target/veer/peripherals/BUILD.bazel index 194164dc9..dd93e340c 100644 --- a/target/veer/peripherals/BUILD.bazel +++ b/target/veer/peripherals/BUILD.bazel @@ -18,6 +18,21 @@ rust_library( ], ) +rust_library( + name = "ecdsa", + srcs = ["ecdsa.rs"], + crate_name = "veer_ecdsa", + edition = "2024", + visibility = ["//visibility:public"], + deps = [ + ":mailbox", + "//hal/blocking", + "@rust_caliptra_crates//:caliptra-api", + "@rust_caliptra_crates//:caliptra-error", + "@rust_caliptra_crates//:zerocopy", + ], +) + rust_library( name = "flash", srcs = [ diff --git a/target/veer/peripherals/ecdsa.rs b/target/veer/peripherals/ecdsa.rs new file mode 100644 index 000000000..5791216c8 --- /dev/null +++ b/target/veer/peripherals/ecdsa.rs @@ -0,0 +1,154 @@ +// Licensed under the Apache-2.0 license +// SPDX-License-Identifier: Apache-2.0 + +//! ECDSA P-384 signature verification that uses the Caliptra core. +//! +//! This driver sends the `ECDSA384_SIGNATURE_VERIFY` mailbox command. The +//! request is defined in the caliptra-sw `caliptra-api` crate +//! (`api/src/mailbox.rs`). This driver imports the command code and the size +//! of the request from that crate, and the error codes from the +//! `caliptra-error` crate. +//! +//! The driver implements the `EcdsaVerify` HAL trait for the P-384 curve. +//! The caller passes the SHA-384 digest of the message. Caliptra does not +//! hash the message. +//! +//! # Errors +//! +//! The driver reports `CaliptraEcdsaError::InvalidSignature` for two error +//! codes of the mailbox command. Caliptra returns +//! `RUNTIME_ECDSA_VERIFY_FAILED` when the signature does not verify, and +//! `DRIVER_ECC384_SCALAR_RANGE_CHECK_FAILED` when `r` or `s` is zero or not +//! less than the order of the curve. +//! +//! The driver reports `CaliptraEcdsaError::InvalidPoint` for +//! `DRIVER_ECC384_HW_ERROR`, which Caliptra returns when its ECC hardware +//! raises an error. In `ecc_dsa_ctrl.sv` of caliptra-rtl, a verify operation +//! raises an error in four cases. Two are about the public key: a coordinate +//! is not less than the prime of the curve, or the point is not on the +//! curve. The VCK190 FPGA returns `DRIVER_ECC384_HW_ERROR` for both. The +//! other two are an `r` or `s` that is out of range and a verify operation +//! with PCR signing selected. The hardware does not raise them for this +//! command: Caliptra's firmware checks the range of `r` and `s` before it +//! writes them to the ECC hardware and it does not select PCR signing. The +//! Caliptra emulator panics when it gets a public key that is not a point +//! on the curve. +//! +//! Every other failure of the command is a `CaliptraEcdsaError::Mailbox` +//! error. + +#![no_std] + +use caliptra_api::mailbox::{CommandId, EcdsaVerifyReq, MailboxReqHeader, MailboxRespHeader}; +use caliptra_error::CaliptraError; +use openprot_hal_blocking::digest::Digest; +use openprot_hal_blocking::ecdsa::{ + EcdsaVerify, Error, ErrorKind, ErrorType, P384PublicKey, P384Signature, PublicKey, Signature, + P384, +}; +use veer_mailbox::{Mailbox, MailboxError}; + +const ECDSA384_SIGNATURE_VERIFY: u32 = CommandId::ECDSA384_SIGNATURE_VERIFY.0; + +/// The signature is not a signature of the digest by the key. +const VERIFY_FAILED: u32 = CaliptraError::RUNTIME_ECDSA_VERIFY_FAILED.0.get(); + +/// `r` or `s` is zero or not less than the order of the curve. +const SCALAR_RANGE_CHECK_FAILED: u32 = CaliptraError::DRIVER_ECC384_SCALAR_RANGE_CHECK_FAILED + .0 + .get(); + +/// The ECC hardware of Caliptra raised an error. +const HW_ERROR: u32 = CaliptraError::DRIVER_ECC384_HW_ERROR.0.get(); + +/// Size in words of the public key and the signature in a request: `x`, `y`, +/// `r` and `s`, 48 bytes each. +const KEY_AND_SIGNATURE_WORDS: usize = size_of::<[[u8; 48]; 4]>() / 4; + +/// Size in words of the digest in a request. +const DIGEST_WORDS: usize = 12; + +/// Size in words of a response: the checksum and the FIPS status. +const RESPONSE_WORDS: usize = size_of::() / 4; + +// The body of the request is the public key, the signature and the digest. +const _: () = assert!( + size_of::() + == size_of::() + (KEY_AND_SIGNATURE_WORDS + DIGEST_WORDS) * 4 +); + +/// Errors from the driver. +#[derive(Debug, Copy, Clone, Eq, PartialEq)] +pub enum CaliptraEcdsaError { + /// The signature is not a signature of the digest by the key. + InvalidSignature, + /// The public key is not a point on the curve. + InvalidPoint, + /// The mailbox command failed for another reason. + Mailbox(MailboxError), + /// The response is shorter than a response header. + InvalidResponse, +} + +impl Error for CaliptraEcdsaError { + fn kind(&self) -> ErrorKind { + match self { + Self::InvalidSignature => ErrorKind::InvalidSignature, + Self::InvalidPoint => ErrorKind::InvalidPoint, + Self::Mailbox(MailboxError::Busy) => ErrorKind::Busy, + _ => ErrorKind::Other, + } + } +} + +/// ECDSA verifier that uses the Caliptra core through its mailbox. +pub struct CaliptraEcdsa<'m> { + mailbox: &'m Mailbox, +} + +impl<'m> CaliptraEcdsa<'m> { + pub fn new(mailbox: &'m Mailbox) -> Self { + Self { mailbox } + } +} + +impl ErrorType for CaliptraEcdsa<'_> { + type Error = CaliptraEcdsaError; +} + +impl EcdsaVerify for CaliptraEcdsa<'_> { + type PublicKey = P384PublicKey; + type Signature = P384Signature; + + /// Returns `Ok` only when Caliptra reports that `signature` is a + /// signature of `digest` by `public_key`. + fn verify( + &mut self, + public_key: &P384PublicKey, + digest: Digest, + signature: &P384Signature, + ) -> Result<(), Self::Error> { + let mut x = [0; 48]; + let mut y = [0; 48]; + public_key.coordinates(&mut x, &mut y); + let mut r = [0; 48]; + let mut s = [0; 48]; + signature.coordinates(&mut r, &mut s); + let key_and_signature: [u32; KEY_AND_SIGNATURE_WORDS] = zerocopy::transmute!([x, y, r, s]); + let request = key_and_signature.into_iter().chain(digest.value); + + let mut response = [0u32; RESPONSE_WORDS]; + match self + .mailbox + .execute(ECDSA384_SIGNATURE_VERIFY, request, &mut response) + { + Ok(len) if len >= size_of::() => Ok(()), + Ok(_) => Err(CaliptraEcdsaError::InvalidResponse), + Err(MailboxError::CommandFailed(VERIFY_FAILED | SCALAR_RANGE_CHECK_FAILED)) => { + Err(CaliptraEcdsaError::InvalidSignature) + } + Err(MailboxError::CommandFailed(HW_ERROR)) => Err(CaliptraEcdsaError::InvalidPoint), + Err(error) => Err(CaliptraEcdsaError::Mailbox(error)), + } + } +} diff --git a/target/veer/tests/ecdsa/BUILD.bazel b/target/veer/tests/ecdsa/BUILD.bazel new file mode 100644 index 000000000..0770d4fa2 --- /dev/null +++ b/target/veer/tests/ecdsa/BUILD.bazel @@ -0,0 +1,97 @@ +# Licensed under the Apache-2.0 license +# SPDX-License-Identifier: Apache-2.0 + +load("@pigweed//pw_kernel/tooling:rust_app.bzl", "rust_app") +load("@pigweed//pw_kernel/tooling:target_codegen.bzl", "target_codegen") +load("@pigweed//pw_kernel/tooling:target_linker_script.bzl", "target_linker_script") +load("@pigweed//pw_kernel/tooling/panic_detector:rust_binary_no_panics_test.bzl", "rust_binary_no_panics_test") +load("@rules_rust//rust:defs.bzl", "rust_binary") +load("//target:app_no_panics_test.bzl", "app_no_panics_test") +load("//target/veer:defs.bzl", "TARGET_COMPATIBLE_WITH", "veer_system_config", "veer_system_image") +load("//target/veer/tooling:caliptra_runner.bzl", "caliptra_on_device_test") + +rust_app( + name = "test_ecdsa", + srcs = ["test_ecdsa.rs"], + codegen_crate_name = "test_ecdsa_codegen", + crate_features = select({ + "//target/veer:fpga": ["fpga"], + "//conditions:default": [], + }), + edition = "2024", + tags = ["kernel"], + target_compatible_with = TARGET_COMPATIBLE_WITH, + deps = [ + "//hal/blocking", + "//target/veer:config", + "//target/veer/peripherals:ecdsa", + "//target/veer/peripherals:mailbox", + "//target/veer/peripherals:sha", + "@pigweed//pw_kernel/userspace", + "@pigweed//pw_log/rust:pw_log", + "@pigweed//pw_status/rust:pw_status", + ], +) + +veer_system_image( + name = "ecdsa", + apps = [":test_ecdsa"], + kernel = ":target", + platform = "//target/veer", + system_config = ":system_config", + tags = ["kernel"], +) + +target_linker_script( + name = "linker_script", + system_config = ":system_config", + tags = ["kernel"], + template = "//target/veer:linker_script_template", +) + +veer_system_config( + name = "system_config", + src = "system.json5.tmpl", +) + +target_codegen( + name = "codegen", + arch = "@pigweed//pw_kernel/arch/riscv:arch_riscv", + system_config = ":system_config", +) + +rust_binary( + name = "target", + srcs = ["target.rs"], + edition = "2024", + tags = ["kernel"], + target_compatible_with = TARGET_COMPATIBLE_WITH, + deps = [ + ":codegen", + ":linker_script", + "//target/veer:entry", + "@pigweed//pw_kernel/arch/riscv:arch_riscv", + "@pigweed//pw_kernel/kernel", + "@pigweed//pw_kernel/subsys/console:console_backend", + "@pigweed//pw_kernel/target:target_common", + "@pigweed//pw_kernel/userspace", + "@pigweed//pw_log/rust:pw_log", + ], +) + +caliptra_on_device_test( + name = "ecdsa_test", + mcu_image = ":ecdsa", +) + +rust_binary_no_panics_test( + name = "no_panics_test", + binary = ":ecdsa", + tags = ["kernel"], +) + +app_no_panics_test( + name = "app_no_panics_test", + image = ":ecdsa", + tags = ["kernel"], +) diff --git a/target/veer/tests/ecdsa/system.json5.tmpl b/target/veer/tests/ecdsa/system.json5.tmpl new file mode 100644 index 000000000..4c706af8d --- /dev/null +++ b/target/veer/tests/ecdsa/system.json5.tmpl @@ -0,0 +1,49 @@ +// Licensed under the Apache-2.0 license +// SPDX-License-Identifier: Apache-2.0 +{ + arch: { + type: "riscv", + }, + kernel: { + flash_start_address: @KERNEL_FLASH_START@, + flash_size_bytes: 65536, + ram_start_address: @KERNEL_RAM_START@, + ram_size_bytes: 32768, + interrupt_table: { + table: {} + }, + }, + apps: [ + { + name: "test_ecdsa", + flash_size_bytes: 32768, + processes: [ + { + name: "test_ecdsa", + ram_size_bytes: 8192, + objects: [ + { + type: "thread", + name: "main_thread", + kernel_stack_size_bytes: 2048, + }, + ], + memory_mappings: [ + { + name: "mbox", + type: "device", + start_address: @MBOX_BASE@, + size_bytes: 0x1000, + }, + { + name: "soc_ifc", + type: "device", + start_address: @SOC_IFC_BASE@, + size_bytes: 0x1000, + }, + ], + }, + ], + }, + ], +} diff --git a/target/veer/tests/ecdsa/target.rs b/target/veer/tests/ecdsa/target.rs new file mode 100644 index 000000000..3dd73c169 --- /dev/null +++ b/target/veer/tests/ecdsa/target.rs @@ -0,0 +1,36 @@ +// Licensed under the Apache-2.0 license +// SPDX-License-Identifier: Apache-2.0 + +#![no_std] +#![no_main] + +use arch_riscv::Arch; +use console_backend as _; +use entry::exit; +use target_common::{declare_target, TargetInterface}; + +pub struct Target {} + +impl TargetInterface for Target { + const NAME: &'static str = "Caliptra-MCU ECDSA"; + + fn main() -> ! { + codegen::start(); + // The bootstrap thread has nothing left to do. Sleep rather than spin: a + // spinning thread at the same priority as the app threads takes a time + // slice at every scheduler tick and delays every wake-up behind it. + loop { + let _ = kernel::sleep_until(Arch, kernel::Instant::MAX); + } + } + + fn shutdown(code: u32) -> ! { + match code { + 0 => pw_log::info!("PASS"), + _ => pw_log::info!("FAIL: {}", code as u32), + }; + exit(code); + } +} + +declare_target!(Target); diff --git a/target/veer/tests/ecdsa/test_ecdsa.rs b/target/veer/tests/ecdsa/test_ecdsa.rs new file mode 100644 index 000000000..6f196b66d --- /dev/null +++ b/target/veer/tests/ecdsa/test_ecdsa.rs @@ -0,0 +1,248 @@ +// Licensed under the Apache-2.0 license +// SPDX-License-Identifier: Apache-2.0 + +//! Checks the Caliptra ECDSA driver. The driver asks the Caliptra core over +//! its mailbox to verify a P-384 signature. +//! +//! The test verifies the signature of a known-answer vector. It then checks +//! that verification fails with another digest, with another key, with a +//! changed signature, and with an `r` and an `s` that are out of range. On +//! the FPGA it also checks that the driver rejects a public key that is not +//! a point on the curve. +//! +//! The test is a userspace app because the stack of the kernel's bootstrap +//! thread is too small for it. + +#![no_std] +#![no_main] + +use kernel_config::{MBOX_BASE, SOC_IFC_BASE}; +use openprot_hal_blocking::digest::scoped::{DigestInit, DigestOp}; +use openprot_hal_blocking::digest::{Digest, Sha2_384}; +use openprot_hal_blocking::ecdsa::{ + EcdsaVerify, Error as _, ErrorKind, P384PublicKey, P384Signature, +}; +use pw_status::Error; +use userspace::{entry, syscall}; +use veer_ecdsa::{CaliptraEcdsa, CaliptraEcdsaError}; +use veer_mailbox::{Mailbox, MailboxError}; +use veer_sha::CaliptraSha; + +// The vector is from RFC 6979, appendix A.2.6: the signature of the message +// "sample" with SHA-384. + +/// The message that is signed. +const MESSAGE: &[u8] = b"sample"; + +/// The x coordinate of the public key. +const KEY_X: [u8; 48] = [ + 0xec, 0x3a, 0x4e, 0x41, 0x5b, 0x4e, 0x19, 0xa4, 0x56, 0x86, 0x18, 0x02, 0x9f, 0x42, 0x7f, 0xa5, + 0xda, 0x9a, 0x8b, 0xc4, 0xae, 0x92, 0xe0, 0x2e, 0x06, 0xaa, 0xe5, 0x28, 0x6b, 0x30, 0x0c, 0x64, + 0xde, 0xf8, 0xf0, 0xea, 0x90, 0x55, 0x86, 0x60, 0x64, 0xa2, 0x54, 0x51, 0x54, 0x80, 0xbc, 0x13, +]; + +/// The y coordinate of the public key. +const KEY_Y: [u8; 48] = [ + 0x80, 0x15, 0xd9, 0xb7, 0x2d, 0x7d, 0x57, 0x24, 0x4e, 0xa8, 0xef, 0x9a, 0xc0, 0xc6, 0x21, 0x89, + 0x67, 0x08, 0xa5, 0x93, 0x67, 0xf9, 0xdf, 0xb9, 0xf5, 0x4c, 0xa8, 0x4b, 0x3f, 0x1c, 0x9d, 0xb1, + 0x28, 0x8b, 0x23, 0x1c, 0x3a, 0xe0, 0xd4, 0xfe, 0x73, 0x44, 0xfd, 0x25, 0x33, 0x26, 0x47, 0x20, +]; + +/// The y coordinate of the negative of the public key. The point with x +/// coordinate `KEY_X` and this y coordinate is on the curve. +const NEGATED_KEY_Y: [u8; 48] = [ + 0x7f, 0xea, 0x26, 0x48, 0xd2, 0x82, 0xa8, 0xdb, 0xb1, 0x57, 0x10, 0x65, 0x3f, 0x39, 0xde, 0x76, + 0x98, 0xf7, 0x5a, 0x6c, 0x98, 0x06, 0x20, 0x46, 0x0a, 0xb3, 0x57, 0xb4, 0xc0, 0xe3, 0x62, 0x4d, + 0xd7, 0x74, 0xdc, 0xe2, 0xc5, 0x1f, 0x2b, 0x01, 0x8c, 0xbb, 0x02, 0xdb, 0xcc, 0xd9, 0xb8, 0xdf, +]; + +/// The `r` component of the signature. +const SIGNATURE_R: [u8; 48] = [ + 0x94, 0xed, 0xbb, 0x92, 0xa5, 0xec, 0xb8, 0xaa, 0xd4, 0x73, 0x6e, 0x56, 0xc6, 0x91, 0x91, 0x6b, + 0x3f, 0x88, 0x14, 0x06, 0x66, 0xce, 0x9f, 0xa7, 0x3d, 0x64, 0xc4, 0xea, 0x95, 0xad, 0x13, 0x3c, + 0x81, 0xa6, 0x48, 0x15, 0x2e, 0x44, 0xac, 0xf9, 0x6e, 0x36, 0xdd, 0x1e, 0x80, 0xfa, 0xbe, 0x46, +]; + +/// The `s` component of the signature. +const SIGNATURE_S: [u8; 48] = [ + 0x99, 0xef, 0x4a, 0xeb, 0x15, 0xf1, 0x78, 0xce, 0xa1, 0xfe, 0x40, 0xdb, 0x26, 0x03, 0x13, 0x8f, + 0x13, 0x0e, 0x74, 0x0a, 0x19, 0x62, 0x45, 0x26, 0x20, 0x3b, 0x63, 0x51, 0xd0, 0xa3, 0xa9, 0x4f, + 0xa3, 0x29, 0xc1, 0x45, 0x78, 0x6e, 0x67, 0x9e, 0x7b, 0x82, 0xc7, 0x1a, 0x38, 0x62, 0x8a, 0xc8, +]; + +/// The order of the P-384 curve. A valid `r` or `s` is less than the order. +const ORDER: [u8; 48] = [ + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc7, 0x63, 0x4d, 0x81, 0xf4, 0x37, 0x2d, 0xdf, + 0x58, 0x1a, 0x0d, 0xb2, 0x48, 0xb0, 0xa7, 0x7a, 0xec, 0xec, 0x19, 0x6a, 0xcc, 0xc5, 0x29, 0x73, +]; + +/// Returns the SHA-384 digest of `message`. +fn sha384(mailbox: &Mailbox, message: &[u8]) -> Result, &'static str> { + let mut sha = CaliptraSha::new(mailbox); + let mut op = sha.init(Sha2_384).map_err(|_| "SHA init failed")?; + op.update(message).map_err(|_| "SHA update failed")?; + op.finalize().map_err(|_| "SHA finalize failed") +} + +/// Returns `bytes` with its last byte changed. +fn changed(mut bytes: [u8; 48]) -> [u8; 48] { + if let Some(last) = bytes.last_mut() { + *last ^= 1; + } + bytes +} + +/// Checks that the driver verifies the signature. +fn accepts( + ecdsa: &mut CaliptraEcdsa<'_>, + key: &P384PublicKey, + digest: Digest<12>, + signature: &P384Signature, + failure: &'static str, +) -> Result<(), &'static str> { + ecdsa.verify(key, digest, signature).map_err(|error| { + if let CaliptraEcdsaError::Mailbox(MailboxError::CommandFailed(code)) = error { + pw_log::error!("verify: Caliptra error {:#x}", code as u32); + } + failure + }) +} + +/// Checks that the driver reports that the signature is invalid. +fn rejects( + ecdsa: &mut CaliptraEcdsa<'_>, + key: &P384PublicKey, + digest: Digest<12>, + signature: &P384Signature, + failure: &'static str, +) -> Result<(), &'static str> { + match ecdsa.verify(key, digest, signature) { + Err(error) if error.kind() == ErrorKind::InvalidSignature => Ok(()), + Err(CaliptraEcdsaError::Mailbox(MailboxError::CommandFailed(code))) => { + pw_log::error!("verify: Caliptra error {:#x}", code as u32); + Err(failure) + } + _ => Err(failure), + } +} + +fn run(mailbox: &Mailbox) -> Result<(), &'static str> { + let mut ecdsa = CaliptraEcdsa::new(mailbox); + let digest = sha384(mailbox, MESSAGE)?; + let key = P384PublicKey::new(KEY_X, KEY_Y); + let signature = P384Signature::new(SIGNATURE_R, SIGNATURE_S); + + accepts( + &mut ecdsa, + &key, + digest, + &signature, + "signature does not verify", + )?; + pw_log::info!("valid signature: ok"); + + let other_digest = sha384(mailbox, b"simple")?; + rejects( + &mut ecdsa, + &key, + other_digest, + &signature, + "signature verifies with another digest", + )?; + pw_log::info!("other digest: ok"); + + let other_key = P384PublicKey::new(KEY_X, NEGATED_KEY_Y); + rejects( + &mut ecdsa, + &other_key, + digest, + &signature, + "signature verifies with another key", + )?; + pw_log::info!("other key: ok"); + + let changed_r = P384Signature::new(changed(SIGNATURE_R), SIGNATURE_S); + rejects( + &mut ecdsa, + &key, + digest, + &changed_r, + "signature with a changed r verifies", + )?; + let changed_s = P384Signature::new(SIGNATURE_R, changed(SIGNATURE_S)); + rejects( + &mut ecdsa, + &key, + digest, + &changed_s, + "signature with a changed s verifies", + )?; + pw_log::info!("changed signature: ok"); + + let zero_r = P384Signature::new([0; 48], SIGNATURE_S); + rejects( + &mut ecdsa, + &key, + digest, + &zero_r, + "signature with a zero r verifies", + )?; + let large_s = P384Signature::new(SIGNATURE_R, ORDER); + rejects( + &mut ecdsa, + &key, + digest, + &large_s, + "signature with an s that equals the order verifies", + )?; + pw_log::info!("out of range: ok"); + + // The Caliptra emulator panics when it gets a public key that is not a + // point on the curve, so these cases run only on the FPGA. + #[cfg(feature = "fpga")] + { + let off_curve = P384PublicKey::new(KEY_X, changed(KEY_Y)); + let large_x = P384PublicKey::new([0xff; 48], KEY_Y); + for invalid_key in [off_curve, large_x] { + match ecdsa.verify(&invalid_key, digest, &signature) { + Err(error) if error.kind() == ErrorKind::InvalidPoint => {} + Err(_) => return Err("invalid key gives the wrong error"), + Ok(()) => return Err("signature verifies with an invalid key"), + } + } + pw_log::info!("invalid key: ok"); + } + + // The driver still verifies the signature after commands that failed. + accepts( + &mut ecdsa, + &key, + digest, + &signature, + "signature does not verify after errors", + )?; + pw_log::info!("verify after errors: ok"); + Ok(()) +} + +#[entry] +fn entry() -> pw_status::Result<()> { + // SAFETY: the system config maps the registers for this process, and + // this image has no other user of the mailbox. + let mailbox = unsafe { Mailbox::new(MBOX_BASE, SOC_IFC_BASE) }; + let result = run(&mailbox).map_err(|reason| { + pw_log::error!("ECDSA test failed: {}", reason as &str); + Error::Internal + }); + // The kernel logs PASS or FAIL, which the test runner looks for. + let _ = syscall::debug_shutdown(result); + result +} + +#[panic_handler] +fn panic(_info: &core::panic::PanicInfo) -> ! { + pw_log::error!("FAIL: panic in ECDSA test"); + let _ = syscall::debug_shutdown(Err(Error::Internal)); + loop {} +}