Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add derive symmetric key unit test for X25519-Deoxys-II MRAE scheme #4772

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.

19 changes: 19 additions & 0 deletions go/common/crypto/mrae/deoxysii/asymmetric_test.go
@@ -1,13 +1,32 @@
package deoxysii

import (
"encoding/hex"
"testing"

curve25519 "github.com/oasisprotocol/curve25519-voi/primitives/x25519"
"github.com/oasisprotocol/deoxysii"
"github.com/stretchr/testify/require"

"github.com/oasisprotocol/oasis-core/go/common/crypto/mrae/api"
)

func Test_DeriveSymmetricKey(t *testing.T) {
// use the same test Hex string for rust at: oasis-core/runtime/src/common/crypto/mrae/deoxysii.rs
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
Expand Up @@ -67,6 +67,7 @@ features = ["full"]

[dev-dependencies]
# For storage interoperability tests only.
hex = "0.4"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved below as the above comment applies to the jsonrpc dependency.

jsonrpc = { version = "0.12.1", features = ["simple_uds"] }
tempfile = "3.3.0"

Expand Down
25 changes: 24 additions & 1 deletion runtime/src/common/crypto/mrae/deoxysii.rs
Expand Up @@ -84,12 +84,35 @@ 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() {
// use the same test Hex string for golang at: oasis-core/go/common/crypto/mrae/deoxysii/asymmetric_test.go
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