Skip to content

Commit 117ef29

Browse files
committed
[opentitantool] Enable use of raw keys with the ecdsa command
Enable the use of raw binary or hex-encoded public keys with the `ecdsa` commands. Signed-off-by: Chris Frantz <[email protected]>
1 parent 12a2262 commit 117ef29

File tree

1 file changed

+18
-3
lines changed
  • sw/host/opentitantool/src/command

1 file changed

+18
-3
lines changed

sw/host/opentitantool/src/command/ecdsa.rs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use clap::{Args, Subcommand};
77
use regex::Regex;
88
use serde_annotate::Annotate;
99
use std::any::Any;
10+
use std::ffi::OsStr;
1011
use std::fs::File;
1112
use std::io::Write;
1213
use std::path::{Path, PathBuf};
@@ -23,6 +24,17 @@ use opentitanlib::crypto::sha256::Sha256Digest;
2324
/// the path to a private key, extracts the public key from the private
2425
/// key and returns the public key.
2526
fn load_pub_or_priv_key(path: &Path) -> Result<EcdsaPublicKey> {
27+
if path.extension() == Some(OsStr::new("bin")) {
28+
let mut f = File::open(path)?;
29+
let k = EcdsaRawPublicKey::read(&mut f)?;
30+
return Ok(EcdsaPublicKey::try_from(&k)?);
31+
}
32+
if path.extension() == Some(OsStr::new("hex")) {
33+
let data = std::fs::read_to_string(path)?;
34+
let mut data = std::io::Cursor::new(hex::decode(data.trim())?);
35+
let k = EcdsaRawPublicKey::read(&mut data)?;
36+
return Ok(EcdsaPublicKey::try_from(&k)?);
37+
}
2638
if let Ok(key) = EcdsaPublicKey::load(path) {
2739
return Ok(key);
2840
}
@@ -33,7 +45,8 @@ fn load_pub_or_priv_key(path: &Path) -> Result<EcdsaPublicKey> {
3345
/// Show public information of a private or public ECDSA key
3446
#[derive(Debug, Args)]
3547
pub struct EcdsaKeyShowCommand {
36-
/// ECDSA public or private key file in DER format.
48+
/// ECDSA public key file in DER format or a raw little-endian key in binary (.bin)
49+
/// or hexadecimal (.hex) form..
3750
der_file: PathBuf,
3851
}
3952

@@ -112,7 +125,8 @@ impl CommandDispatch for EcdsaKeyGenerateCommand {
112125
/// to a C header that can be used in the ROM or ROM_EXT
113126
#[derive(Debug, Args)]
114127
pub struct EcdsaKeyExportCommand {
115-
/// ECDSA public or private key file in DER format.
128+
/// ECDSA public or private key file in DER format or a raw little-endian key in binary (.bin)
129+
/// or hexadecimal (.hex) form..
116130
der_file: PathBuf,
117131
/// output header file to generate.
118132
output_file: Option<PathBuf>,
@@ -255,7 +269,8 @@ pub struct EcdsaVerifyCommand {
255269
/// Digest to be verified (binary file)
256270
#[arg(long, short, conflicts_with = "signature")]
257271
digest_file: Option<PathBuf>,
258-
/// Key file in DER format.
272+
/// ECDSA public key file in DER format or a raw little-endian key in binary (.bin)
273+
/// or hexadecimal (.hex) form..
259274
#[arg(value_name = "KEY")]
260275
der_file: PathBuf,
261276
/// SHA256 digest of the message as a hex string.

0 commit comments

Comments
 (0)