pub struct SigningKey {
pub(super) id: KeyId,
inner: RawSigningKey,
}Expand description
A signing key is a private key used for signing data. An associated VerifyingKey can be
derived from it.
Fields§
§id: KeyId§inner: RawSigningKeyImplementations§
Source§impl SigningKey
impl SigningKey
Sourcefn sign_bytes(
&self,
serialized_message: &SerializedMessage,
namespace: &SigningNamespace,
) -> Result<SignedObject, CryptoError>
fn sign_bytes( &self, serialized_message: &SerializedMessage, namespace: &SigningNamespace, ) -> Result<SignedObject, CryptoError>
Signs the given payload with the signing key, under a given namespace.
This is is the underlying implementation of the sign method, and takes
a raw byte array as input.
Sourcepub fn sign<Message: Serialize>(
&self,
message: &Message,
namespace: &SigningNamespace,
) -> Result<SignedObject, CryptoError>
pub fn sign<Message: Serialize>( &self, message: &Message, namespace: &SigningNamespace, ) -> Result<SignedObject, CryptoError>
Signs the given payload with the signing key, under a given namespace.
This returns a SignedObject object, that contains the payload.
The payload is included in the signature, and does not need to be provided when verifying
the signature.
This should be used when only one signer is required, so that only one object needs to be kept track of.
use bitwarden_crypto::{SigningNamespace, SignatureAlgorithm, SigningKey};
use serde::{Serialize, Deserialize};
const EXAMPLE_NAMESPACE: SigningNamespace = SigningNamespace::SignedPublicKey;
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct TestMessage {
field1: String,
}
let signing_key = SigningKey::make(SignatureAlgorithm::Ed25519);
let message = TestMessage {
field1: "Test message".to_string(),
};
let namespace = EXAMPLE_NAMESPACE;
let signed_object = signing_key.sign(&message, &namespace).unwrap();
// The signed object can be verified using the verifying key:
let verifying_key = signing_key.to_verifying_key();
let payload: TestMessage = signed_object.verify_and_unwrap(&verifying_key, &namespace).unwrap();
assert_eq!(payload, message);Source§impl SigningKey
impl SigningKey
Sourcepub fn sign_detached<Message: Serialize>(
&self,
message: &Message,
namespace: &SigningNamespace,
) -> Result<(Signature, SerializedMessage), CryptoError>
pub fn sign_detached<Message: Serialize>( &self, message: &Message, namespace: &SigningNamespace, ) -> Result<(Signature, SerializedMessage), CryptoError>
Signs the given payload with the signing key, under a given SigningNamespace.
This returns a Signature object, that does not contain the payload.
The payload must be stored separately, and needs to be provided when verifying the
signature.
This should be used when multiple signers are required, or when signatures need to be replaceable without re-uploading the object, or if the signed object should be parseable by the server side, without the use of COSE on the server.
use bitwarden_crypto::{SigningNamespace, SignatureAlgorithm, SigningKey};
use serde::{Serialize, Deserialize};
const EXAMPLE_NAMESPACE: SigningNamespace = SigningNamespace::SignedPublicKey;
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct TestMessage {
field1: String,
}
let signing_key = SigningKey::make(SignatureAlgorithm::Ed25519);
let message = TestMessage {
field1: "Test message".to_string(),
};
let namespace = EXAMPLE_NAMESPACE;
let (signature, serialized_message) = signing_key.sign_detached(&message, &namespace).unwrap();
// Verification
let verifying_key = signing_key.to_verifying_key();
assert!(signature.verify(&serialized_message.as_bytes(), &verifying_key, &namespace));Sourcepub fn counter_sign_detached(
&self,
serialized_message_bytes: Vec<u8>,
initial_signature: &Signature,
namespace: &SigningNamespace,
) -> Result<Signature, CryptoError>
pub fn counter_sign_detached( &self, serialized_message_bytes: Vec<u8>, initial_signature: &Signature, namespace: &SigningNamespace, ) -> Result<Signature, CryptoError>
Given a serialized message, signature, this counter-signs the message. That is, if multiple parties want to sign the same message, one party creates the initial message, and the other parties then counter-sign it, and submit their signatures. This can be done as follows: ``` let alice_key = SigningKey::make(SignatureAlgorithm::Ed25519); let bob_key = SigningKey::make(SignatureAlgorithm::Ed25519);
let message = TestMessage { field1: “Test message”.to_string(), }; let namespace = SigningNamespace::ExampleNamespace; let (signature, serialized_message) = alice_key.sign_detached(&message, &namespace).unwrap();\ // Alice shares (signature, serialized_message) with Bob. // Bob verifies the contents of serialized_message using application logic, then signs it: let (bob_signature, serialized_message) = bob_key.counter_sign(&serialized_message, &signature, &namespace).unwrap(); ```
Sourcefn sign_detached_bytes(
&self,
message: &SerializedMessage,
namespace: &SigningNamespace,
) -> Signature
fn sign_detached_bytes( &self, message: &SerializedMessage, namespace: &SigningNamespace, ) -> Signature
Signs the given payload with the signing key, under a given namespace.
This is is the underlying implementation of the sign_detached method, and takes
a raw byte array as input.
Source§impl SigningKey
impl SigningKey
Sourcepub fn make(algorithm: SignatureAlgorithm) -> Self
pub fn make(algorithm: SignatureAlgorithm) -> Self
Makes a new signing key for the given signature scheme.
pub(super) fn cose_algorithm(&self) -> Algorithm
Sourcepub fn to_verifying_key(&self) -> VerifyingKey
pub fn to_verifying_key(&self) -> VerifyingKey
Derives the verifying key from the signing key. The key id is the same for the signing and verifying key, since they are a pair.
Trait Implementations§
Source§impl Clone for SigningKey
impl Clone for SigningKey
Source§fn clone(&self) -> SigningKey
fn clone(&self) -> SigningKey
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl CoseSerializable<CoseKeyContentFormat> for SigningKey
impl CoseSerializable<CoseKeyContentFormat> for SigningKey
Source§fn to_cose(&self) -> CoseKeyBytes
fn to_cose(&self) -> CoseKeyBytes
Serializes the signing key to a COSE-formatted byte array.
Source§fn from_cose(bytes: &CoseKeyBytes) -> Result<Self, EncodingError>
fn from_cose(bytes: &CoseKeyBytes) -> Result<Self, EncodingError>
Deserializes a COSE-formatted byte array into a signing key.
impl CryptoKey for SigningKey
impl ZeroizeOnDrop for SigningKey
Auto Trait Implementations§
impl Freeze for SigningKey
impl RefUnwindSafe for SigningKey
impl Send for SigningKey
impl Sync for SigningKey
impl Unpin for SigningKey
impl UnwindSafe for SigningKey
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> CompatExt for T
impl<T> CompatExt for T
§impl<T, UT> HandleAlloc<UT> for T
impl<T, UT> HandleAlloc<UT> for T
§fn new_handle(value: Arc<T>) -> Handle
fn new_handle(value: Arc<T>) -> Handle
§unsafe fn clone_handle(handle: Handle) -> Handle
unsafe fn clone_handle(handle: Handle) -> Handle
§unsafe fn consume_handle(handle: Handle) -> Arc<T>
unsafe fn consume_handle(handle: Handle) -> Arc<T>
Arc<> Read more§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more