Skip to content

Commit 74be805

Browse files
Update crypto deps (#74)
* p256 & k256 from 0.10 -> 0.11 * p256 & k256 from 0.11 -> 0.12 - https://github.com/RustCrypto/traits/pull/1141/files - https://github.com/RustCrypto/traits/blob/master/signature/CHANGELOG.md#200-2023-01-15 * p256 & k256 from 0.12 -> 0.13 * Remove unused lazy_static dep * use non-deprecated solana_sdk::Keypair constructor from_bytes -> try_from(slice) * Update tpm ecdh p256 used to provide a From implementation for AffinePoint to SharedSecret. We can recreate what it was doing using the AffineCoordinates trait. RustCrypto/elliptic-curves@a7011d2#diff-c6f073a6c542e656510c60595c86058a65f58dc3f00824c7f505cf6b556d3b73L49-L53 - RustCrypto/elliptic-curves@a7011d2#diff-c6f073a6c542e656510c60595c86058a65f58dc3f00824c7f505cf6b556d3b73L49-L53 - https://docs.rs/elliptic-curve/0.13.8/elliptic_curve/point/trait.AffineCoordinates.html
1 parent 5ddcf09 commit 74be805

File tree

6 files changed

+37
-69
lines changed

6 files changed

+37
-69
lines changed

Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,18 @@ thiserror = "1"
1313
bs58 = { version = "0.5", features = ["check"] }
1414
base64 = ">=0.21"
1515
drop_guard = { version = "0.3.0", optional = true }
16-
signature = "1"
16+
signature = "2"
1717
serde = { version = "1", features = ["derive"] }
1818
rand_core = "^0.6"
1919
sha2 = { version = "0.10", default-features = false, features = ["std", "oid"] }
2020
ed25519-compact = { version = "2", features = ["std", "traits"] }
21-
p256 = { version = "0.10", default-features = false, features = [
21+
p256 = { version = "0.13", default-features = false, features = [
2222
"arithmetic",
2323
"ecdsa",
2424
"sha256",
2525
"ecdh",
2626
] }
27-
k256 = { version = "0.10", default-features = false, features = [
27+
k256 = { version = "0.13", default-features = false, features = [
2828
"arithmetic",
2929
"ecdsa",
3030
"sha256",
@@ -36,7 +36,6 @@ rsa = { version = "0.4", optional = true, default-features = false, features = [
3636
] }
3737
ecc608-linux = { version = "0", optional = true }
3838
tss-esapi = { version = "7", optional = true }
39-
lazy_static = "1.4.0"
4039
libc = { version = "0", optional = true }
4140
byteorder = { version = "1", optional = true }
4241
multihash = { version = "0.18", optional = true }

src/ecc_compact/mod.rs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::*;
22
use p256::{
33
ecdsa,
4-
elliptic_curve::{ecdh, sec1::ToCompactEncodedPoint, DecompactPoint},
4+
elliptic_curve::{ecdh, point::DecompactPoint, sec1::ToCompactEncodedPoint},
55
FieldBytes,
66
};
77
use std::{hash::Hasher, ops::Deref};
@@ -29,7 +29,7 @@ pub trait IsCompactable {
2929

3030
impl IsCompactable for p256::PublicKey {
3131
fn is_compactable(&self) -> bool {
32-
self.as_affine().to_compact_encoded_point().is_some()
32+
self.as_affine().to_compact_encoded_point().is_some().into()
3333
}
3434
}
3535

@@ -61,7 +61,7 @@ impl TryFrom<&[u8]> for Keypair {
6161
fn try_from(input: &[u8]) -> Result<Self> {
6262
let network = Network::try_from(input[0])?;
6363
let secret =
64-
p256::SecretKey::from_be_bytes(&input[1..usize::min(input.len(), KEYPAIR_LENGTH)])?;
64+
p256::SecretKey::from_slice(&input[1..usize::min(input.len(), KEYPAIR_LENGTH)])?;
6565
let public_key =
6666
public_key::PublicKey::for_network(network, PublicKey(secret.public_key()));
6767
Ok(Keypair {
@@ -98,7 +98,7 @@ impl Keypair {
9898
}
9999

100100
pub fn generate_from_entropy(network: Network, entropy: &[u8]) -> Result<Keypair> {
101-
let secret = p256::SecretKey::from_be_bytes(entropy)?;
101+
let secret = p256::SecretKey::from_slice(entropy)?;
102102
let public_key = secret.public_key();
103103
if !public_key.is_compactable() {
104104
return Err(Error::not_compact());
@@ -133,29 +133,13 @@ impl Keypair {
133133
C: TryInto<&'a PublicKey, Error = Error>,
134134
{
135135
let public_key = public_key.try_into()?;
136-
let secret_key = p256::SecretKey::from_be_bytes(&self.secret.to_bytes())?;
136+
let secret_key = p256::SecretKey::from_slice(&self.secret.to_bytes())?;
137137
let shared_secret =
138138
ecdh::diffie_hellman(secret_key.to_nonzero_scalar(), public_key.0.as_affine());
139139
Ok(SharedSecret(shared_secret))
140140
}
141141
}
142142

143-
impl signature::Signature for Signature {
144-
fn from_bytes(input: &[u8]) -> std::result::Result<Self, signature::Error> {
145-
Ok(Signature(signature::Signature::from_bytes(input)?))
146-
}
147-
148-
fn as_bytes(&self) -> &[u8] {
149-
self.0.as_bytes()
150-
}
151-
}
152-
153-
impl AsRef<[u8]> for Signature {
154-
fn as_ref(&self) -> &[u8] {
155-
self.0.as_ref()
156-
}
157-
}
158-
159143
impl signature::Signer<Signature> for Keypair {
160144
fn try_sign(&self, msg: &[u8]) -> std::result::Result<Signature, signature::Error> {
161145
Ok(Signature(self.secret.sign(msg)))
@@ -164,7 +148,9 @@ impl signature::Signer<Signature> for Keypair {
164148

165149
impl Signature {
166150
pub fn from_bytes(bytes: &[u8]) -> Result<Self> {
167-
Ok(Signature(signature::Signature::from_bytes(bytes)?))
151+
ecdsa::Signature::try_from(bytes)
152+
.map(Signature)
153+
.map_err(error::Error::from)
168154
}
169155

170156
pub fn to_vec(&self) -> Vec<u8> {
@@ -210,14 +196,16 @@ impl TryFrom<&[u8]> for PublicKey {
210196
use p256::elliptic_curve::sec1::FromEncodedPoint;
211197
let encoded_point =
212198
p256::EncodedPoint::from_bytes(input).map_err(p256::elliptic_curve::Error::from)?;
199+
213200
// Convert to an affine point, then to the compact encoded form.
214201
// Then finally convert to the p256 public key.
215-
let public_key = Option::from(p256::AffinePoint::from_encoded_point(&encoded_point))
216-
.and_then(|affine_point: p256::AffinePoint| affine_point.to_compact_encoded_point())
217-
.and_then(|compact_point| {
218-
Option::from(p256::PublicKey::from_encoded_point(&compact_point))
219-
});
220-
Ok(PublicKey(public_key.ok_or_else(Error::not_compact)?))
202+
let public_key = p256::AffinePoint::from_encoded_point(&encoded_point)
203+
.and_then(|affine_point| affine_point.to_compact_encoded_point())
204+
.and_then(|compact_point| p256::PublicKey::from_encoded_point(&compact_point));
205+
206+
// Break out of constant time operations
207+
let pub_key_opt = Option::from(public_key);
208+
Ok(PublicKey(pub_key_opt.ok_or_else(Error::not_compact)?))
221209
}
222210
}
223211
}
@@ -358,6 +346,9 @@ mod tests {
358346
// And now do an ecdh with my keypair and the other public key and
359347
// compare it with the shared secret that the erlang ecdh generated
360348
let shared_secret = keypair.ecdh(&other_public_key).expect("shared secret");
361-
assert_eq!(shared_secret.as_bytes().as_slice(), OTHER_SHARED_SECRET);
349+
assert_eq!(
350+
shared_secret.raw_secret_bytes().as_slice(),
351+
OTHER_SHARED_SECRET
352+
);
362353
}
363354
}

src/ed25519/mod.rs

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,6 @@ impl Keypair {
9595
}
9696
}
9797

98-
impl signature::Signature for Signature {
99-
fn from_bytes(input: &[u8]) -> std::result::Result<Self, signature::Error> {
100-
Ok(Signature(
101-
ed25519_compact::Signature::from_slice(input)
102-
.map_err(|_| signature::Error::default())?,
103-
))
104-
}
105-
106-
fn as_bytes(&self) -> &[u8] {
107-
self.0.as_ref()
108-
}
109-
}
110-
11198
impl AsRef<[u8]> for Signature {
11299
fn as_ref(&self) -> &[u8] {
113100
self.0.as_ref()
@@ -257,11 +244,11 @@ mod tests {
257244
use solana_sdk::signature as solana_sdk;
258245
use std::convert::TryInto;
259246

260-
let solana_wallet = solana_sdk::Keypair::from_bytes(&BYTES).unwrap();
247+
let solana_wallet = solana_sdk::Keypair::try_from(&BYTES[..]).unwrap();
261248
let solana_pubkey = solana_sdk::Signer::pubkey(&solana_wallet);
262249

263250
let entropy = &BYTES[..32];
264-
let keypair = Keypair::generate_from_entropy(Network::MainNet, &entropy).expect("keypair");
251+
let keypair = Keypair::generate_from_entropy(Network::MainNet, entropy).expect("keypair");
265252
let solana_pubkey_from_helium = keypair.public_key.try_into().unwrap();
266253
assert_eq!(solana_pubkey, solana_pubkey_from_helium);
267254
}

src/keypair.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,11 @@ mod tests {
279279
let other_shared = other
280280
.ecdh(key_pair.public_key())
281281
.expect("other shared secret");
282-
assert_eq!(keypair_shared.as_bytes(), other_shared.as_bytes());
282+
283+
assert_eq!(
284+
keypair_shared.raw_secret_bytes(),
285+
other_shared.raw_secret_bytes()
286+
);
283287
}
284288

285289
fn seed_roundtrip(key_tag: KeyTag) {

src/secp256k1/mod.rs

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl TryFrom<&[u8]> for Keypair {
4848
type Error = super::error::Error;
4949
fn try_from(input: &[u8]) -> Result<Self> {
5050
let network = Network::try_from(input[0])?;
51-
let secret = k256::SecretKey::from_be_bytes(&input[1..])?;
51+
let secret = k256::SecretKey::from_slice(&input[1..])?;
5252
let public_key =
5353
public_key::PublicKey::for_network(network, PublicKey(secret.public_key()));
5454
Ok(Keypair {
@@ -81,7 +81,7 @@ impl Keypair {
8181
}
8282

8383
pub fn generate_from_entropy(network: Network, entropy: &[u8]) -> Result<Keypair> {
84-
let secret = k256::SecretKey::from_be_bytes(entropy)?;
84+
let secret = k256::SecretKey::from_slice(entropy)?;
8585
let public_key = secret.public_key();
8686
Ok(Keypair {
8787
network,
@@ -109,22 +109,6 @@ impl Keypair {
109109
}
110110
}
111111

112-
impl signature::Signature for Signature {
113-
fn from_bytes(input: &[u8]) -> std::result::Result<Self, signature::Error> {
114-
Ok(Signature(signature::Signature::from_bytes(input)?))
115-
}
116-
117-
fn as_bytes(&self) -> &[u8] {
118-
self.0.as_bytes()
119-
}
120-
}
121-
122-
impl AsRef<[u8]> for Signature {
123-
fn as_ref(&self) -> &[u8] {
124-
self.0.as_ref()
125-
}
126-
}
127-
128112
impl signature::Signer<Signature> for Keypair {
129113
fn try_sign(&self, msg: &[u8]) -> std::result::Result<Signature, signature::Error> {
130114
Ok(Signature(self.secret.sign(msg)))
@@ -133,7 +117,9 @@ impl signature::Signer<Signature> for Keypair {
133117

134118
impl Signature {
135119
pub fn from_be_bytes(bytes: &[u8]) -> Result<Self> {
136-
Ok(Signature(signature::Signature::from_bytes(bytes)?))
120+
ecdsa::Signature::try_from(bytes)
121+
.map(Signature)
122+
.map_err(error::Error::from)
137123
}
138124

139125
pub fn to_vec(&self) -> Vec<u8> {

src/tpm/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,9 @@ impl KeypairHandle {
101101
let encoded_point = p256::EncodedPoint::from_bytes(shared_secret_bytes.as_slice())
102102
.map_err(p256::elliptic_curve::Error::from)?;
103103
let affine_point = p256::AffinePoint::from_encoded_point(&encoded_point).unwrap();
104+
104105
Ok(ecc_compact::SharedSecret(p256::ecdh::SharedSecret::from(
105-
&affine_point,
106+
p256::elliptic_curve::point::AffineCoordinates::x(&affine_point),
106107
)))
107108
}
108109
}

0 commit comments

Comments
 (0)