Skip to content

Commit

Permalink
feat: implement hash_alg() and public_params() for SecretKeyTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
hko-s committed Mar 2, 2024
1 parent 512cd65 commit bb7782d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/composed/signed_key/secret.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use crate::crypto::public_key::PublicKeyAlgorithm;
use crate::errors::Result;
use crate::packet::{self, write_packet, SignatureType};
use crate::ser::Serialize;
use crate::types::{KeyId, KeyTrait, Mpi, PublicKeyTrait, SecretKeyRepr, SecretKeyTrait};
use crate::types::{
KeyId, KeyTrait, Mpi, PublicKeyTrait, PublicParams, SecretKeyRepr, SecretKeyTrait,
};
use crate::{armor, SignedPublicKey};

/// Represents a secret signed PGP key.
Expand Down Expand Up @@ -193,6 +195,10 @@ impl SecretKeyTrait for SignedSecretKey {
subkeys,
)
}

fn public_params(&self) -> &PublicParams {
self.primary_key.public_params()
}
}

impl PublicKeyTrait for SignedSecretKey {
Expand Down Expand Up @@ -300,6 +306,10 @@ impl SecretKeyTrait for SignedSecretSubKey {

PublicSubkey::new(self.key.public_key(), keyflags)
}

fn public_params(&self) -> &PublicParams {
self.key.public_params()
}
}

impl PublicKeyTrait for SignedSecretSubKey {
Expand Down
4 changes: 4 additions & 0 deletions src/packet/secret_key_macro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ macro_rules! impl_secret_key {
fn public_key(&self) -> $details {
self.details.clone()
}

fn public_params(&self) -> &$crate::types::PublicParams {
&self.details.public_params()
}
}

impl $crate::ser::Serialize for $name {
Expand Down
18 changes: 17 additions & 1 deletion src/types/secret_key.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::crypto::hash::HashAlgorithm;
use crate::errors::Result;
use crate::types::{Mpi, PublicKeyTrait, SecretKeyRepr};
use crate::types::{EcdsaPublicParams, Mpi, PublicKeyTrait, PublicParams, SecretKeyRepr};

pub trait SecretKeyTrait: PublicKeyTrait {
type PublicKey;
Expand All @@ -15,6 +15,18 @@ pub trait SecretKeyTrait: PublicKeyTrait {
F: FnOnce() -> String;

fn public_key(&self) -> Self::PublicKey;

fn public_params(&self) -> &PublicParams;

/// The suggested hash algorithm to calculate the signature hash digest with, when using this
/// key as a signer
fn hash_alg(&self) -> HashAlgorithm {
match self.public_params() {
PublicParams::ECDSA(EcdsaPublicParams::P384 { .. }) => HashAlgorithm::SHA2_384,
PublicParams::ECDSA(EcdsaPublicParams::P521 { .. }) => HashAlgorithm::SHA2_512,
_ => HashAlgorithm::default(),
}
}
}

impl<'a, T: SecretKeyTrait> SecretKeyTrait for &'a T {
Expand All @@ -38,4 +50,8 @@ impl<'a, T: SecretKeyTrait> SecretKeyTrait for &'a T {
fn public_key(&self) -> Self::PublicKey {
(*self).public_key()
}

fn public_params(&self) -> &PublicParams {
(*self).public_params()
}
}

0 comments on commit bb7782d

Please sign in to comment.