@@ -50,13 +50,16 @@ pub struct SigningKey {
5050}
5151
5252impl SigningKey {
53- /// Construct a new private key from the public key and private component
53+ /// Construct a new private key from the public key and private component.
54+ ///
55+ /// # Errors
56+ /// Returns an error in the event `x` is zero or equal to or larger than `q`.
5457 pub fn from_components ( verifying_key : VerifyingKey , x : BoxedUint ) -> signature:: Result < Self > {
5558 let x = NonZero :: new ( x)
5659 . into_option ( )
5760 . ok_or_else ( signature:: Error :: new) ?;
5861
59- if x > * verifying_key. components ( ) . q ( ) {
62+ if x >= * verifying_key. components ( ) . q ( ) {
6063 return Err ( signature:: Error :: new ( ) ) ;
6164 }
6265
@@ -66,7 +69,10 @@ impl SigningKey {
6669 } )
6770 }
6871
69- /// Generate a new DSA keypair
72+ /// Generate a new DSA keypair.
73+ ///
74+ /// # Errors
75+ /// Propagates errors from `R`.
7076 #[ cfg( feature = "hazmat" ) ]
7177 #[ inline]
7278 pub fn try_generate_from_rng_with_components < R : TryCryptoRng + ?Sized > (
@@ -81,9 +87,17 @@ impl SigningKey {
8187 & self . verifying_key
8288 }
8389
84- /// DSA private component
90+ /// DSA private component.
91+ ///
92+ /// <div class="warning">
93+ /// <b>Security Warning</b>
94+ ///
95+ /// This value is key material. Please treat it with care!
8596 ///
86- /// If you decide to clone this value, please consider using [`Zeroize::zeroize`](::zeroize::Zeroize::zeroize()) to zero out the memory after you're done using the clone
97+ /// If you decide to clone this value, please consider using
98+ /// [`Zeroize::zeroize`](::zeroize::Zeroize::zeroize) to zero out the memory after you're done
99+ /// using the clone.
100+ /// </div>
87101 #[ must_use]
88102 pub fn x ( & self ) -> & NonZero < BoxedUint > {
89103 & self . x
@@ -94,15 +108,26 @@ impl SigningKey {
94108 ///
95109 /// [RFC6979]: https://datatracker.ietf.org/doc/html/rfc6979
96110 #[ cfg( feature = "hazmat" ) ]
111+ #[ allow(
112+ clippy:: missing_errors_doc,
113+ reason = "errors shouldn't occur in practice"
114+ ) ]
97115 pub fn sign_prehashed_rfc6979 < D > ( & self , prehash : & [ u8 ] ) -> Result < Signature , signature:: Error >
98116 where
99117 D : BlockSizeUser + Digest ,
100118 {
119+ // TODO(tarcieri): make this operation infallible by retrying with a different `k`
101120 let k_kinv = generate:: secret_number_rfc6979 :: < D > ( self , prehash) ;
102121 self . sign_prehashed ( k_kinv, prehash)
103122 }
104123
105- /// Sign some pre-hashed data
124+ /// Sign some pre-hashed data.
125+ #[ allow(
126+ clippy:: as_conversions,
127+ clippy:: cast_possible_truncation,
128+ clippy:: integer_division_remainder_used,
129+ reason = "TODO"
130+ ) ]
106131 fn sign_prehashed (
107132 & self ,
108133 ( k, inv_k) : ( BoxedUint , BoxedUint ) ,
@@ -285,7 +310,11 @@ impl<'a> TryFrom<PrivateKeyInfoRef<'a>> for SigningKey {
285310 . into_option ( )
286311 . ok_or ( pkcs8:: KeyError :: Invalid ) ?;
287312
288- let y = if let Some ( y_bytes) = value. public_key . as_ref ( ) . and_then ( |bs| bs. as_bytes ( ) ) {
313+ let y = if let Some ( y_bytes) = value
314+ . public_key
315+ . as_ref ( )
316+ . and_then ( der:: asn1:: BitStringRef :: as_bytes)
317+ {
289318 let y = UintRef :: from_der ( y_bytes) ?;
290319 BoxedUint :: from_be_slice ( y. as_bytes ( ) , precision)
291320 . map_err ( |_| pkcs8:: KeyError :: Invalid ) ?
0 commit comments