Skip to content

Commit 129d67b

Browse files
authored
elliptic-curve: impl TryKeyInit for SecretKey<C> (#2174)
Impls the newly added trait from `crypto-common`. Also impls its supertrait `KeySizeUser`.
1 parent dc21c1a commit 129d67b

1 file changed

Lines changed: 22 additions & 4 deletions

File tree

elliptic-curve/src/secret_key.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ mod pkcs8;
1010

1111
use crate::{Curve, Error, FieldBytes, Result, ScalarValue};
1212
use array::typenum::Unsigned;
13-
use common::Generate;
13+
use common::{Generate, InvalidKey, KeySizeUser, TryKeyInit};
1414
use core::fmt::{self, Debug};
1515
use rand_core::{CryptoRng, TryCryptoRng};
1616
use subtle::{Choice, ConstantTimeEq, CtOption};
@@ -276,8 +276,6 @@ where
276276
}
277277
}
278278

279-
impl<C> ZeroizeOnDrop for SecretKey<C> where C: Curve {}
280-
281279
impl<C> Drop for SecretKey<C>
282280
where
283281
C: Curve,
@@ -286,6 +284,7 @@ where
286284
self.inner.zeroize();
287285
}
288286
}
287+
impl<C> ZeroizeOnDrop for SecretKey<C> where C: Curve {}
289288

290289
impl<C: Curve> Eq for SecretKey<C> {}
291290

@@ -298,7 +297,10 @@ where
298297
}
299298
}
300299

301-
impl<C: Curve> Generate for SecretKey<C> {
300+
impl<C> Generate for SecretKey<C>
301+
where
302+
C: Curve,
303+
{
302304
fn try_generate_from_rng<R: TryCryptoRng + ?Sized>(
303305
rng: &mut R,
304306
) -> core::result::Result<Self, R::Error> {
@@ -308,6 +310,22 @@ impl<C: Curve> Generate for SecretKey<C> {
308310
}
309311
}
310312

313+
impl<C> KeySizeUser for SecretKey<C>
314+
where
315+
C: Curve,
316+
{
317+
type KeySize = C::FieldBytesSize;
318+
}
319+
320+
impl<C> TryKeyInit for SecretKey<C>
321+
where
322+
C: Curve,
323+
{
324+
fn new(key_bytes: &FieldBytes<C>) -> core::result::Result<Self, InvalidKey> {
325+
Self::from_bytes(key_bytes).map_err(|_| InvalidKey)
326+
}
327+
}
328+
311329
#[cfg(feature = "sec1")]
312330
impl<C> sec1::DecodeEcPrivateKey for SecretKey<C>
313331
where

0 commit comments

Comments
 (0)