Skip to content

Commit de72c67

Browse files
authored
elliptic-curve: make SecretKey::new failible (#1804)
This fixes an invariant violation where you could create a secret key from an all-zero scalar and convert it to a non-zero scala. Fixes: #1607
1 parent a8d6711 commit de72c67

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

elliptic-curve/src/secret_key.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ mod pkcs8;
1111
use crate::{Curve, Error, FieldBytes, Result, ScalarPrimitive};
1212
use core::fmt::{self, Debug};
1313
use hybrid_array::typenum::Unsigned;
14-
use subtle::{Choice, ConstantTimeEq};
14+
use subtle::{Choice, ConstantTimeEq, CtOption};
1515
use zeroize::{Zeroize, ZeroizeOnDrop, Zeroizing};
1616

1717
#[cfg(feature = "arithmetic")]
@@ -117,8 +117,12 @@ where
117117
}
118118

119119
/// Create a new secret key from a scalar value.
120-
pub fn new(scalar: ScalarPrimitive<C>) -> Self {
121-
Self { inner: scalar }
120+
///
121+
/// # Returns
122+
///
123+
/// This will return a none if the scalar is all-zero.
124+
pub fn new(scalar: ScalarPrimitive<C>) -> CtOption<Self> {
125+
CtOption::new(Self { inner: scalar }, !scalar.is_zero())
122126
}
123127

124128
/// Borrow the inner secret [`ScalarPrimitive`] value.

0 commit comments

Comments
 (0)