Skip to content

Commit

Permalink
Switch caliptra_test::derive_ecdsa_keypair() to openssl.
Browse files Browse the repository at this point in the history
RustCrypto is used in the emulator, so to add implementation diversity
when generating test vectors, caliptra-test has by convention used
openssl.
  • Loading branch information
korran committed Sep 8, 2023
1 parent cac1cb8 commit 28e8146
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 24 deletions.
3 changes: 0 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ caliptra_common = { workspace = true, default-features = false }
caliptra-hw-model-types.workspace = true
caliptra-runtime = { workspace = true, default-features = false }
openssl.workspace = true
p384.workspace = true
rfc6979.workspace = true
sha2.workspace = true
zerocopy.workspace = true

[dev-dependencies]
Expand Down
34 changes: 16 additions & 18 deletions test/src/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,26 @@ use openssl::{
nid::Nid,
pkey::{PKey, Public},
};
use p384::ecdsa::SigningKey;
use rfc6979::HmacDrbg;
use sha2::Sha384;

// Derives a key using a DRBG. Returns (priv, pub_x, pub_y)
pub fn derive_ecdsa_keypair(seed: &[u8]) -> ([u8; 48], [u8; 48], [u8; 48]) {
let mut drbg = HmacDrbg::<Sha384>::new(seed, &[0_u8; 48], &[]);
let mut priv_key = [0u8; 48];
drbg.fill_bytes(&mut priv_key);
let priv_key = hmac384_drbg_keygen(seed, &[0; 48]);
let pub_key = derive_ecdsa_key(&priv_key);
let ec_key = EcKey::try_from(pub_key).unwrap();

let ecc_point = SigningKey::from_bytes(&priv_key)
.unwrap()
.verifying_key()
.to_encoded_point(false);

let mut pub_x = [0u8; 48];
let mut pub_y = [0u8; 48];

pub_x.copy_from_slice(ecc_point.x().unwrap().as_slice());
pub_y.copy_from_slice(ecc_point.y().unwrap().as_slice());

(priv_key, pub_x, pub_y)
let group = EcGroup::from_curve_name(Nid::SECP384R1).unwrap();
let mut bn_ctx = BigNumContext::new().unwrap();
let mut pub_x = BigNum::new().unwrap();
let mut pub_y = BigNum::new().unwrap();
ec_key
.public_key()
.affine_coordinates(&group, &mut pub_x, &mut pub_y, &mut bn_ctx)
.unwrap();
(
priv_key,
pub_x.to_vec_padded(48).unwrap().try_into().unwrap(),
pub_y.to_vec_padded(48).unwrap().try_into().unwrap(),
)
}

#[test]
Expand Down

0 comments on commit 28e8146

Please sign in to comment.