use super::{Command, Response};
#[cfg(all(feature = "http", not(feature = "usb")))]
use adapter::http::HttpAdapter;
#[cfg(feature = "usb")]
use adapter::usb::UsbAdapter;
use adapter::Adapter;
#[cfg(all(feature = "mockhsm", not(feature = "doc")))]
use mockhsm::MockAdapter;
use session::{Session, SessionError};
#[cfg(
all(
feature = "sha2",
any(feature = "http", feature = "usb"),
any(feature = "doc", not(feature = "mockhsm"))
)
)]
use sha2::{Digest, Sha256};
use {CommandType, ObjectId};
pub fn sign_ecdsa_raw_digest<A, T>(
session: &mut Session<A>,
key_id: ObjectId,
digest: T,
) -> Result<ECDSASignature, SessionError>
where
A: Adapter,
T: Into<Vec<u8>>,
{
session.send_command(SignDataECDSACommand {
key_id,
digest: digest.into(),
})
}
#[cfg(all(feature = "http", not(feature = "usb")))]
#[allow(dead_code)]
type AdapterType = HttpAdapter;
#[cfg(feature = "usb")]
#[allow(dead_code)]
type AdapterType = UsbAdapter;
#[cfg(
all(
feature = "sha2",
any(feature = "http", feature = "usb"),
any(feature = "doc", not(feature = "mockhsm"))
)
)]
pub fn sign_ecdsa_sha256(
session: &mut Session<AdapterType>,
key_id: ObjectId,
data: &[u8],
) -> Result<ECDSASignature, SessionError> {
sign_ecdsa_raw_digest(session, key_id, Sha256::digest(data).as_slice())
}
#[cfg(all(feature = "mockhsm", not(feature = "doc")))]
pub fn sign_ecdsa_sha256(
session: &mut Session<MockAdapter>,
key_id: ObjectId,
data: &[u8],
) -> Result<ECDSASignature, SessionError> {
session.send_command(SignDataECDSACommand {
key_id,
digest: data.into(),
})
}
#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct SignDataECDSACommand {
pub key_id: ObjectId,
pub digest: Vec<u8>,
}
impl Command for SignDataECDSACommand {
type ResponseType = ECDSASignature;
}
#[derive(Serialize, Deserialize, Debug)]
pub struct ECDSASignature(pub Vec<u8>);
impl Response for ECDSASignature {
const COMMAND_TYPE: CommandType = CommandType::SignDataECDSA;
}
#[allow(
unknown_lints,
renamed_and_removed_lints,
len_without_is_empty
)]
impl ECDSASignature {
pub fn into_vec(self) -> Vec<u8> {
self.into()
}
#[inline]
pub fn len(&self) -> usize {
self.0.len()
}
#[inline]
pub fn as_slice(&self) -> &[u8] {
self.as_ref()
}
}
impl AsRef<[u8]> for ECDSASignature {
fn as_ref(&self) -> &[u8] {
self.0.as_ref()
}
}
impl Into<Vec<u8>> for ECDSASignature {
fn into(self) -> Vec<u8> {
self.0
}
}