Skip to content

Commit

Permalink
add same unittest for derive_symmetric_key of client-sdk/web-ts for g…
Browse files Browse the repository at this point in the history
…olang and rust
  • Loading branch information
James Zheng committed May 31, 2022
1 parent 8862481 commit 89d2e75
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

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

20 changes: 18 additions & 2 deletions go/common/crypto/mrae/deoxysii/asymmetric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,28 @@ package deoxysii

import (
"testing"

"encoding/hex"
"github.com/oasisprotocol/deoxysii"

curve25519 "github.com/oasisprotocol/curve25519-voi/primitives/x25519"
"github.com/oasisprotocol/oasis-core/go/common/crypto/mrae/api"
"github.com/stretchr/testify/require"
)

func Test_DeriveSymmetricKey(t *testing.T) {
p, _ := hex.DecodeString("c07b151fbc1e7a11dff926111188f8d872f62eba0396da97c0a24adb75161750")
var privateKey [32]byte
copy(privateKey[:], p)
var publicKey [32]byte
curve25519.ScalarBaseMult(&publicKey, &privateKey)
publicKeyHex := hex.EncodeToString(publicKey[:])
require.EqualValues(t, publicKeyHex, "3046db3fa70ce605457dc47c48837ebd8bd0a26abfde5994d033e1ced68e2576", "derive public key")

var sharedKey [deoxysii.KeySize]byte
Box.DeriveSymmetricKey(sharedKey[:], &publicKey, &privateKey)
sharedKeyHex := hex.EncodeToString(sharedKey[:])
require.EqualValues(t, sharedKeyHex, "e69ac21066a8c2284e8fdc690e579af4513547b9b31dd144792c1904b45cf586", "derive symmetric key")
}

func TestDeoxysII_Box_Integration(t *testing.T) {
api.TestBoxIntegration(t, Box, deoxysii.New, deoxysii.KeySize)
}
1 change: 1 addition & 0 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ oid-registry = "0.4.0"
rsa = "0.5.0"
base64-serde = "0.6.1"
lru = "0.7.5"
hex = "0.4"

[target.'cfg(not(target_env = "sgx"))'.dependencies.tokio]
version = "1"
Expand Down
24 changes: 23 additions & 1 deletion runtime/src/common/crypto/mrae/deoxysii.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,34 @@ pub fn box_open(

#[cfg(test)]
mod tests {
extern crate hex;
extern crate test;

use self::test::{black_box, Bencher};
use super::*;
use hex::FromHex;
use rand::RngCore;

#[test]
fn test_drive_symmetric_key() {
let private_key_buffer = <[u8; 32]>::from_hex(
"c07b151fbc1e7a11dff926111188f8d872f62eba0396da97c0a24adb75161750",
)
.expect("derive private key from hex string");
let private_key = x25519_dalek::StaticSecret::from(private_key_buffer);
let public_key = x25519_dalek::PublicKey::from(&private_key);
let public_key_hex = hex::encode(public_key.to_bytes());
assert_eq!(
public_key_hex,
"3046db3fa70ce605457dc47c48837ebd8bd0a26abfde5994d033e1ced68e2576"
);
let shared = derive_symmetric_key(&public_key.to_bytes(), &private_key.to_bytes());
let shared_hex = hex::encode(shared);
assert_eq!(
shared_hex,
"e69ac21066a8c2284e8fdc690e579af4513547b9b31dd144792c1904b45cf586"
);
}

#[test]
fn test_mrae_asymmetric() {
let (a_pub, a_priv) = generate_key_pair(); // Alice
Expand Down

0 comments on commit 89d2e75

Please sign in to comment.