Skip to content

Commit 93906b0

Browse files
committed
Move into separate MultiPart* trait
1 parent 9e3f5ec commit 93906b0

File tree

3 files changed

+101
-15
lines changed

3 files changed

+101
-15
lines changed

async-signature/tests/mock_impl.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct Signature;
1111
struct MockSigner;
1212

1313
impl AsyncSigner<Signature> for MockSigner {
14-
async fn sign_async(&self, _msg: &[&[u8]]) -> Result<Signature, Error> {
14+
async fn sign_async(&self, _msg: &[u8]) -> Result<Signature, Error> {
1515
unimplemented!("just meant to check compilation")
1616
}
1717
}
@@ -33,7 +33,7 @@ impl async_signature::AsyncRandomizedSigner<Signature> for MockSigner {
3333
>(
3434
&self,
3535
_rng: &mut R,
36-
_msg: &[&[u8]],
36+
_msg: &[u8],
3737
) -> Result<Signature, Error> {
3838
unimplemented!("just meant to check compilation")
3939
}

signature/src/signer.rs

Lines changed: 93 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::rand_core::{CryptoRng, TryCryptoRng};
1212
/// or connection to an HSM), returning a digital signature.
1313
pub trait Signer<S> {
1414
/// Sign the given message and return a digital signature
15-
fn sign(&self, msg: &[&[u8]]) -> S {
15+
fn sign(&self, msg: &[u8]) -> S {
1616
self.try_sign(msg).expect("signature operation failed")
1717
}
1818

@@ -21,6 +21,17 @@ pub trait Signer<S> {
2121
///
2222
/// The main intended use case for signing errors is when communicating
2323
/// with external signers, e.g. cloud KMS, HSMs, or other hardware tokens.
24+
fn try_sign(&self, msg: &[u8]) -> Result<S, Error>;
25+
}
26+
27+
/// Equivalent of [`Signer`] but the message is provided in non-contiguous byte slices.
28+
pub trait MultiPartSigner<S> {
29+
/// See [`Signer::sign()`].
30+
fn sign(&self, msg: &[&[u8]]) -> S {
31+
self.try_sign(msg).expect("signature operation failed")
32+
}
33+
34+
/// See [`Signer::try_sign()`].
2435
fn try_sign(&self, msg: &[&[u8]]) -> Result<S, Error>;
2536
}
2637

@@ -29,7 +40,7 @@ pub trait Signer<S> {
2940
/// digital signature.
3041
pub trait SignerMut<S> {
3142
/// Sign the given message, update the state, and return a digital signature.
32-
fn sign(&mut self, msg: &[&[u8]]) -> S {
43+
fn sign(&mut self, msg: &[u8]) -> S {
3344
self.try_sign(msg).expect("signature operation failed")
3445
}
3546

@@ -38,16 +49,27 @@ pub trait SignerMut<S> {
3849
///
3950
/// Signing can fail, e.g., if the number of time periods allowed by the
4051
/// current key is exceeded.
41-
fn try_sign(&mut self, msg: &[&[u8]]) -> Result<S, Error>;
52+
fn try_sign(&mut self, msg: &[u8]) -> Result<S, Error>;
4253
}
4354

4455
/// Blanket impl of [`SignerMut`] for all [`Signer`] types.
4556
impl<S, T: Signer<S>> SignerMut<S> for T {
46-
fn try_sign(&mut self, msg: &[&[u8]]) -> Result<S, Error> {
57+
fn try_sign(&mut self, msg: &[u8]) -> Result<S, Error> {
4758
T::try_sign(self, msg)
4859
}
4960
}
5061

62+
/// Equivalent of [`SignerMut`] but the message is provided in non-contiguous byte slices.
63+
pub trait MultiPartSignerMut<S> {
64+
/// See [`SignerMut::sign()`].
65+
fn sign(&mut self, msg: &[&[u8]]) -> S {
66+
self.try_sign(msg).expect("signature operation failed")
67+
}
68+
69+
/// See [`SignerMut::try_sign()`].
70+
fn try_sign(&mut self, msg: &[&[u8]]) -> Result<S, Error>;
71+
}
72+
5173
/// Sign the given prehashed message [`Digest`] using `Self`.
5274
///
5375
/// ## Notes
@@ -86,7 +108,7 @@ pub trait DigestSigner<D: Digest, S> {
86108
#[cfg(feature = "rand_core")]
87109
pub trait RandomizedSigner<S> {
88110
/// Sign the given message and return a digital signature
89-
fn sign_with_rng<R: CryptoRng + ?Sized>(&self, rng: &mut R, msg: &[&[u8]]) -> S {
111+
fn sign_with_rng<R: CryptoRng + ?Sized>(&self, rng: &mut R, msg: &[u8]) -> S {
90112
self.try_sign_with_rng(rng, msg)
91113
.expect("signature operation failed")
92114
}
@@ -96,6 +118,23 @@ pub trait RandomizedSigner<S> {
96118
///
97119
/// The main intended use case for signing errors is when communicating
98120
/// with external signers, e.g. cloud KMS, HSMs, or other hardware tokens.
121+
fn try_sign_with_rng<R: TryCryptoRng + ?Sized>(
122+
&self,
123+
rng: &mut R,
124+
msg: &[u8],
125+
) -> Result<S, Error>;
126+
}
127+
128+
/// Equivalent of [`RandomizedSigner`] but the message is provided in non-contiguous byte slices.
129+
#[cfg(feature = "rand_core")]
130+
pub trait RandomizedMultiPartSigner<S> {
131+
/// See [`RandomizedSigner::sign_with_rng()`].
132+
fn sign_with_rng<R: CryptoRng + ?Sized>(&self, rng: &mut R, msg: &[&[u8]]) -> S {
133+
self.try_sign_with_rng(rng, msg)
134+
.expect("signature operation failed")
135+
}
136+
137+
/// See [`RandomizedSigner::try_sign_with_rng()`].
99138
fn try_sign_with_rng<R: TryCryptoRng + ?Sized>(
100139
&self,
101140
rng: &mut R,
@@ -130,7 +169,7 @@ pub trait RandomizedDigestSigner<D: Digest, S> {
130169
#[cfg(feature = "rand_core")]
131170
pub trait RandomizedSignerMut<S> {
132171
/// Sign the given message, update the state, and return a digital signature.
133-
fn sign_with_rng<R: CryptoRng + ?Sized>(&mut self, rng: &mut R, msg: &[&[u8]]) -> S {
172+
fn sign_with_rng<R: CryptoRng + ?Sized>(&mut self, rng: &mut R, msg: &[u8]) -> S {
134173
self.try_sign_with_rng(rng, msg)
135174
.expect("signature operation failed")
136175
}
@@ -143,7 +182,7 @@ pub trait RandomizedSignerMut<S> {
143182
fn try_sign_with_rng<R: TryCryptoRng + ?Sized>(
144183
&mut self,
145184
rng: &mut R,
146-
msg: &[&[u8]],
185+
msg: &[u8],
147186
) -> Result<S, Error>;
148187
}
149188

@@ -153,12 +192,29 @@ impl<S, T: RandomizedSigner<S>> RandomizedSignerMut<S> for T {
153192
fn try_sign_with_rng<R: TryCryptoRng + ?Sized>(
154193
&mut self,
155194
rng: &mut R,
156-
msg: &[&[u8]],
195+
msg: &[u8],
157196
) -> Result<S, Error> {
158197
T::try_sign_with_rng(self, rng, msg)
159198
}
160199
}
161200

201+
/// Equivalent of [`RandomizedSignerMut`] but the message is provided in non-contiguous byte slices.
202+
#[cfg(feature = "rand_core")]
203+
pub trait RandomizedMultiPartSignerMut<S> {
204+
/// See [`RandomizedSignerMut::sign_with_rng()`].
205+
fn sign_with_rng<R: CryptoRng + ?Sized>(&mut self, rng: &mut R, msg: &[u8]) -> S {
206+
self.try_sign_with_rng(rng, msg)
207+
.expect("signature operation failed")
208+
}
209+
210+
/// See [`RandomizedSignerMut::try_sign_with_rng()`].
211+
fn try_sign_with_rng<R: TryCryptoRng + ?Sized>(
212+
&mut self,
213+
rng: &mut R,
214+
msg: &[u8],
215+
) -> Result<S, Error>;
216+
}
217+
162218
/// Asynchronously sign the provided message bytestring using `Self`
163219
/// (e.g. client for a Cloud KMS or HSM), returning a digital signature.
164220
///
@@ -169,18 +225,24 @@ pub trait AsyncSigner<S> {
169225
///
170226
/// The main intended use case for signing errors is when communicating
171227
/// with external signers, e.g. cloud KMS, HSMs, or other hardware tokens.
172-
async fn sign_async(&self, msg: &[&[u8]]) -> Result<S, Error>;
228+
async fn sign_async(&self, msg: &[u8]) -> Result<S, Error>;
173229
}
174230

175231
impl<S, T> AsyncSigner<S> for T
176232
where
177233
T: Signer<S>,
178234
{
179-
async fn sign_async(&self, msg: &[&[u8]]) -> Result<S, Error> {
235+
async fn sign_async(&self, msg: &[u8]) -> Result<S, Error> {
180236
self.try_sign(msg)
181237
}
182238
}
183239

240+
/// Equivalent of [`AsyncSigner`] but the message is provided in non-contiguous byte slices.
241+
pub trait AsyncMultiPartSigner<S> {
242+
/// See [`AsyncSigner::sign_async()`].
243+
async fn sign_async(&self, msg: &[&[u8]]) -> Result<S, Error>;
244+
}
245+
184246
/// Asynchronously sign the given prehashed message [`Digest`] using `Self`.
185247
///
186248
/// This trait is an async equivalent of the [`DigestSigner`] trait.
@@ -198,7 +260,7 @@ where
198260
#[cfg(feature = "rand_core")]
199261
pub trait AsyncRandomizedSigner<S> {
200262
/// Sign the given message and return a digital signature
201-
async fn sign_with_rng_async<R: CryptoRng + ?Sized>(&self, rng: &mut R, msg: &[&[u8]]) -> S {
263+
async fn sign_with_rng_async<R: CryptoRng + ?Sized>(&self, rng: &mut R, msg: &[u8]) -> S {
202264
self.try_sign_with_rng_async(rng, msg)
203265
.await
204266
.expect("signature operation failed")
@@ -212,7 +274,7 @@ pub trait AsyncRandomizedSigner<S> {
212274
async fn try_sign_with_rng_async<R: TryCryptoRng + ?Sized>(
213275
&self,
214276
rng: &mut R,
215-
msg: &[&[u8]],
277+
msg: &[u8],
216278
) -> Result<S, Error>;
217279
}
218280

@@ -224,8 +286,26 @@ where
224286
async fn try_sign_with_rng_async<R: TryCryptoRng + ?Sized>(
225287
&self,
226288
rng: &mut R,
227-
msg: &[&[u8]],
289+
msg: &[u8],
228290
) -> Result<S, Error> {
229291
self.try_sign_with_rng(rng, msg)
230292
}
231293
}
294+
295+
/// Equivalent of [`AsyncRandomizedSigner`] but the message is provided in non-contiguous byte slices.
296+
#[cfg(feature = "rand_core")]
297+
pub trait AsyncRandomizedMultiPartSigner<S> {
298+
/// See [`AsyncRandomizedSigner::sign_with_rng_async()`].
299+
async fn sign_with_rng_async<R: CryptoRng + ?Sized>(&self, rng: &mut R, msg: &[&[u8]]) -> S {
300+
self.try_sign_with_rng_async(rng, msg)
301+
.await
302+
.expect("signature operation failed")
303+
}
304+
305+
/// See [`AsyncRandomizedSigner::try_sign_with_rng_async()`].
306+
async fn try_sign_with_rng_async<R: TryCryptoRng + ?Sized>(
307+
&self,
308+
rng: &mut R,
309+
msg: &[&[u8]],
310+
) -> Result<S, Error>;
311+
}

signature/src/verifier.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ pub trait Verifier<S> {
1111
/// bytestring is authentic.
1212
///
1313
/// Returns `Error` if it is inauthentic, or otherwise returns `()`.
14+
fn verify(&self, msg: &[u8], signature: &S) -> Result<(), Error>;
15+
}
16+
17+
/// Equivalent of [`Verifier`] but the message is provided in non-contiguous byte slices.
18+
pub trait MultiPartVerifier<S> {
19+
/// See [`Verifier::verify()`].
1420
fn verify(&self, msg: &[&[u8]], signature: &S) -> Result<(), Error>;
1521
}
1622

0 commit comments

Comments
 (0)