Skip to content
Open
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,620 changes: 1,474 additions & 146 deletions MODULE.bazel.lock

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Binary file not shown.
6 changes: 5 additions & 1 deletion sw/host/hsmtool/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,12 @@ rust_library(
"src/commands/spx/verify.rs",
"src/commands/token.rs",
"src/error.rs",
"src/extra/mod.rs",
"src/extra/spxef.rs",
"src/extra/spxkms.rs",
"src/lib.rs",
"src/module.rs",
"src/profile.rs",
"src/spxef/mod.rs",
"src/util/attribute/attr.rs",
"src/util/attribute/data.rs",
"src/util/attribute/date.rs",
Expand Down Expand Up @@ -163,6 +165,7 @@ rust_library(
"//sw/host/hsmtool/acorn",
"//sw/host/sphincsplus",
"@crate_index//:anyhow",
"@crate_index//:base64ct",
"@crate_index//:clap",
"@crate_index//:cryptoki",
"@crate_index//:cryptoki-sys",
Expand All @@ -177,6 +180,7 @@ rust_library(
"@crate_index//:pem-rfc7468",
"@crate_index//:rand",
"@crate_index//:regex",
"@crate_index//:reqwest",
"@crate_index//:rsa",
"@crate_index//:rustix",
"@crate_index//:serde",
Expand Down
1 change: 1 addition & 0 deletions sw/host/hsmtool/acorn/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ rust_library(
crate_root = "lib.rs",
deps = [
"//sw/host/hsmtool/acorn/vendor:acorn_bindgen",
"//sw/host/sphincsplus",
"@crate_index//:anyhow",
"@crate_index//:bitflags",
"@crate_index//:libloading",
Expand Down
12 changes: 11 additions & 1 deletion sw/host/hsmtool/acorn/acorn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use anyhow::Result;
use libloading::Library;
use sphincsplus::SpxDomain;
use std::ffi::{CStr, CString};
use thiserror::Error;

Expand Down Expand Up @@ -436,9 +437,16 @@ impl SpxInterface for Acorn {
}
}

fn sign(&self, alias: Option<&str>, key_hash: Option<&str>, message: &[u8]) -> Result<Vec<u8>> {
fn sign(
&self,
alias: Option<&str>,
key_hash: Option<&str>,
domain: SpxDomain,
message: &[u8],
) -> Result<Vec<u8>> {
let alias = alias.map(CString::new).transpose()?;
let key_hash = key_hash.map(CString::new).transpose()?;
let message = domain.prepare(message);
// SAFETY: The signature returned by `sign` is copied into a rust Vec.
// The memory allocated by the acorn library is freed by the acorn library's
// free function.
Expand Down Expand Up @@ -478,11 +486,13 @@ impl SpxInterface for Acorn {
&self,
alias: Option<&str>,
key_hash: Option<&str>,
domain: SpxDomain,
message: &[u8],
signature: &[u8],
) -> Result<bool> {
let alias = alias.map(CString::new).transpose()?;
let key_hash = key_hash.map(CString::new).transpose()?;
let message = domain.prepare(message);
// SAFETY: The signature returned by `sign` is copied into a rust Vec.
// The memory allocated by the acorn library is freed by the acorn library's
// free function.
Expand Down
10 changes: 9 additions & 1 deletion sw/host/hsmtool/acorn/spx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use anyhow::Result;
use bitflags::bitflags;
use sphincsplus::SpxDomain;

#[derive(Debug, Default)]
pub struct KeyEntry {
Expand Down Expand Up @@ -71,13 +72,20 @@ pub trait SpxInterface {
) -> Result<KeyEntry>;

/// Sign a message.
fn sign(&self, alias: Option<&str>, key_hash: Option<&str>, message: &[u8]) -> Result<Vec<u8>>;
fn sign(
&self,
alias: Option<&str>,
key_hash: Option<&str>,
domain: SpxDomain,
message: &[u8],
) -> Result<Vec<u8>>;

/// Verify a message.
fn verify(
&self,
alias: Option<&str>,
key_hash: Option<&str>,
domain: SpxDomain,
message: &[u8],
signature: &[u8],
) -> Result<bool>;
Expand Down
7 changes: 6 additions & 1 deletion sw/host/hsmtool/src/commands/spx/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ impl Dispatch for Sign {

let data = std::fs::read(&self.input)?;
let data = self.format.spx_prepare(self.domain, &data)?;
let result = spx.sign(self.label.as_deref(), self.id.as_deref(), &data)?;
let result = spx.sign(
self.label.as_deref(),
self.id.as_deref(),
self.domain,
&data,
)?;
std::fs::write(&self.output, &result)?;
Ok(Box::<BasicResult>::default())
}
Expand Down
8 changes: 7 additions & 1 deletion sw/host/hsmtool/src/commands/spx/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ impl Dispatch for Verify {
let data = std::fs::read(&self.input)?;
let data = self.format.spx_prepare(self.domain, &data)?;
let signature = std::fs::read(&self.signature)?;
let result = spx.verify(self.label.as_deref(), self.id.as_deref(), &data, &signature)?;
let result = spx.verify(
self.label.as_deref(),
self.id.as_deref(),
self.domain,
&data,
&signature,
)?;
Ok(Box::new(BasicResult {
success: result,
error: if result {
Expand Down
9 changes: 9 additions & 0 deletions sw/host/hsmtool/src/extra/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Copyright lowRISC contributors (OpenTitan project).
// Licensed under the Apache License, Version 2.0, see LICENSE for details.
// SPDX-License-Identifier: Apache-2.0

mod spxef;
mod spxkms;

pub use spxef::SpxEf;
pub use spxkms::SpxKms;
Original file line number Diff line number Diff line change
Expand Up @@ -184,20 +184,27 @@ impl SpxInterface for SpxEf {
}

/// Sign a message.
fn sign(&self, alias: Option<&str>, key_hash: Option<&str>, message: &[u8]) -> Result<Vec<u8>> {
fn sign(
&self,
alias: Option<&str>,
key_hash: Option<&str>,
domain: SpxDomain,
message: &[u8],
) -> Result<Vec<u8>> {
let alias = alias.ok_or(HsmError::NoSearchCriteria)?;
if key_hash.is_some() {
log::warn!("ignored key_hash {key_hash:?}");
}
let sk = self.load_key(alias)?;
Ok(sk.sign(SpxDomain::None, message)?)
Ok(sk.sign(domain, message)?)
}

/// Verify a message.
fn verify(
&self,
alias: Option<&str>,
key_hash: Option<&str>,
domain: SpxDomain,
message: &[u8],
signature: &[u8],
) -> Result<bool> {
Expand All @@ -207,7 +214,7 @@ impl SpxInterface for SpxEf {
}
let sk = self.load_key(alias)?;
let pk = SpxPublicKey::from(&sk);
match pk.verify(SpxDomain::None, signature, message) {
match pk.verify(domain, signature, message) {
Ok(()) => Ok(true),
Err(SpxError::BadSignature) => Ok(false),
Err(e) => Err(e.into()),
Expand Down
Loading
Loading