Files
trussed-staging/tests/hpke.rs
T

125 lines
3.7 KiB
Rust

// Copyright (C) Nitrokey GmbH
// SPDX-License-Identifier: Apache-2.0 or MIT
#![cfg(all(feature = "virt", feature = "hpke"))]
use littlefs2_core::path;
use trussed::client::{CryptoClient, X255};
use trussed::{
syscall,
types::{Bytes, KeyId, Location, Mechanism, SignatureSerialization},
virt::StoreConfig,
};
use trussed_hpke::HpkeClient;
use trussed_staging::virt;
fn assert_symkey_eq<C: CryptoClient>(this: KeyId, other: KeyId, client: &mut C) {
let hmac_this = syscall!(client.sign(
Mechanism::HmacSha256,
this,
b"DATA",
SignatureSerialization::Raw
))
.signature;
let hmac_other = syscall!(client.sign(
Mechanism::HmacSha256,
other,
b"DATA",
SignatureSerialization::Raw
))
.signature;
assert_eq!(hmac_other, hmac_this);
}
#[test]
fn hpke_message() {
virt::with_client(StoreConfig::ram(), "hpke_test_message", |mut client| {
let secret_key = syscall!(client.generate_x255_secret_key(Location::Volatile)).key;
let public_key =
syscall!(client.derive_x255_public_key(secret_key, Location::Volatile)).key;
let pl = Bytes::from(b"Plaintext");
let aad = Bytes::from(b"AAD");
let info = Bytes::from(b"INFO");
let seal = syscall!(client.hpke_seal(
public_key,
pl.clone(),
aad.clone(),
info.clone(),
Location::Volatile
));
assert!(seal.ciphertext != b"Plaintext");
let opened =
syscall!(client.hpke_open(secret_key, seal.enc, seal.ciphertext, seal.tag, aad, info));
assert_eq!(opened.plaintext, pl);
})
}
#[test]
fn hpke_wrap_key() {
virt::with_client(StoreConfig::ram(), "hpke_test_wrap_key", |mut client| {
let secret_key = syscall!(client.generate_x255_secret_key(Location::Volatile)).key;
let public_key =
syscall!(client.derive_x255_public_key(secret_key, Location::Volatile)).key;
let key_to_wrap = syscall!(client.generate_secret_key(32, Location::Volatile)).key;
let aad = Bytes::from(b"AAD");
let info = Bytes::from(b"INFO");
let seal =
syscall!(client.hpke_seal_key(public_key, key_to_wrap, aad.clone(), info.clone()));
let unwrapped =
syscall!(client.hpke_open_key(secret_key, seal.data, aad, info, Location::Volatile))
.key;
assert_ne!(unwrapped, key_to_wrap);
assert_symkey_eq(key_to_wrap, unwrapped, &mut client);
})
}
#[test]
fn hpke_wrap_key_to_file() {
virt::with_client(
StoreConfig::ram(),
"hpke_test_wrap_key_to_file",
|mut client| {
let secret_key = syscall!(client.generate_x255_secret_key(Location::Volatile)).key;
let public_key =
syscall!(client.derive_x255_public_key(secret_key, Location::Volatile)).key;
let key_to_wrap = syscall!(client.generate_secret_key(32, Location::Volatile)).key;
let path = path!("WRAPPED_KEY");
let aad = Bytes::from(b"AAD");
let info = Bytes::from(b"INFO");
syscall!(client.hpke_seal_key_to_file(
path.into(),
Location::Volatile,
public_key,
key_to_wrap,
aad.clone(),
info.clone()
));
let unwrapped = syscall!(client.hpke_open_key_from_file(
secret_key,
path.into(),
Location::Volatile,
Location::Volatile,
aad,
info
))
.key;
assert_ne!(unwrapped, key_to_wrap);
assert_symkey_eq(key_to_wrap, unwrapped, &mut client);
},
)
}