-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test vector integration test, fix arg orders
- Loading branch information
Showing
7 changed files
with
90 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use hpke_dispatch::Config; | ||
use serde::Deserialize; | ||
|
||
#[derive(Deserialize, Debug)] | ||
struct EncryptionRecord { | ||
#[serde(with = "hex")] | ||
aad: Vec<u8>, | ||
#[serde(with = "hex", rename = "ct")] | ||
ciphertext: Vec<u8>, | ||
#[serde(with = "hex")] | ||
nonce: Vec<u8>, | ||
#[serde(with = "hex", rename = "pt")] | ||
plaintext: Vec<u8>, | ||
} | ||
|
||
/// This structure corresponds to the format of the JSON test vectors included with the HPKE | ||
/// RFC. Only a subset of fields are used; all intermediate calculations are ignored. | ||
#[derive(Deserialize, Debug)] | ||
struct TestVector { | ||
mode: u16, | ||
kem_id: u16, | ||
kdf_id: u16, | ||
aead_id: u16, | ||
#[serde(with = "hex")] | ||
info: Vec<u8>, | ||
#[serde(with = "hex", rename = "enc")] | ||
encapped_key: Vec<u8>, | ||
#[serde(with = "hex", rename = "skRm")] | ||
serialized_private_key: Vec<u8>, | ||
#[serde(with = "hex")] | ||
base_nonce: Vec<u8>, | ||
encryptions: Vec<EncryptionRecord>, | ||
} | ||
|
||
#[test] | ||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)] | ||
fn decrypt_test_vectors() { | ||
let test_vectors: Vec<TestVector> = | ||
serde_json::from_str(include_str!("./test-vectors.json")).unwrap(); // https://github.com/cfrg/draft-irtf-cfrg-hpke/raw/master/test-vectors.json | ||
|
||
let test_vectors = test_vectors | ||
.into_iter() | ||
.filter(|v| v.mode == 0) | ||
.filter_map(|test_vector| { | ||
let config = | ||
Config::try_from_ids(test_vector.aead_id, test_vector.kdf_id, test_vector.kem_id) | ||
.ok()?; | ||
|
||
Some((config, test_vector)) | ||
}) | ||
.collect::<Vec<_>>(); | ||
|
||
for (config, test_vector) in test_vectors { | ||
for encryption in &test_vector.encryptions { | ||
if encryption.nonce != test_vector.base_nonce { | ||
continue; | ||
} | ||
|
||
let plaintext = config | ||
.base_mode_open( | ||
&test_vector.serialized_private_key, | ||
&test_vector.encapped_key, | ||
&test_vector.info, | ||
&encryption.ciphertext, | ||
&encryption.aad, | ||
) | ||
.unwrap_or_else(|e| { | ||
panic!("{e:?}\n\n{config:?}\n\n{encryption:#?}\n\n{test_vector:#?}") | ||
}); | ||
|
||
assert_eq!(plaintext, encryption.plaintext); | ||
} | ||
} | ||
} |