Skip to content

Commit

Permalink
Make test vectors prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
bifurcation committed Dec 5, 2023
1 parent d990bb5 commit 89f2f75
Show file tree
Hide file tree
Showing 4 changed files with 351 additions and 310 deletions.
64 changes: 36 additions & 28 deletions reference-implementation/examples/test_vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ mod header {
"~~~
kid: 0x{kid:016x}
ctr: 0x{ctr:016x}
header: {encoded}
header: {encoded:8}
~~~"
)
}
Expand Down Expand Up @@ -187,13 +187,13 @@ mod aes_ctr_hmac {
format!(
"~~~
cipher_suite: 0x{cipher_suite:04x}
key: {key}
enc_key: {enc_key}
auth_key: {auth_key}
nonce: {nonce}
aad: {aad}
pt: {pt}
ct: {ct}
key: {key:5}
enc_key: {enc_key:9}
auth_key: {auth_key:10}
nonce: {nonce:7}
aad: {aad:5}
pt: {pt:4}
ct: {ct:4}
~~~"
)
}
Expand Down Expand Up @@ -306,41 +306,49 @@ mod sframe {
cipher_suite: 0x{cipher_suite:04x}
kid: 0x{kid:016x}
ctr: 0x{ctr:016x}
base_key: {base_key}
sframe_key_label: {sframe_key_label}
sframe_salt_label: {sframe_salt_label}
sframe_secret: {sframe_secret}
sframe_key: {sframe_key}
sframe_salt: {sframe_salt}
metadata: {metadata}
nonce: {nonce}
aad: {aad}
pt: {pt}
ct: {ct}
base_key: {base_key:10}
sframe_key_label: {sframe_key_label:18}
sframe_salt_label: {sframe_salt_label:19}
sframe_secret: {sframe_secret:15}
sframe_key: {sframe_key:12}
sframe_salt: {sframe_salt:13}
metadata: {metadata:10}
nonce: {nonce:7}
aad: {aad:5}
pt: {pt:4}
ct: {ct:4}
~~~"
)
}
}
}

use std::fmt::Display;

use clap::{Parser, Subcommand, ValueEnum};
use serde::{Deserialize, Serialize};
use std::fmt::Display;

/// Type Hex encapsulates a byte string that will serialize as a single hex string in JSON, or as a
/// chunked, line-wrapped, and padded hex string in Markdown.
struct Hex(Vec<u8>);

impl Display for Hex {
// Divide the hex string into 16-byte chunks, each on its own line, and aligned to make space
// for a field name. We abuse the "width" formatting parameter to specify how much each line
// after the first should be indented.
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (i, c) in self.0.chunks(8).enumerate() {
if i % 3 == 0 {
if i > 0 {
f.write_str("\n ")?;
}
} else {
f.write_str(" ")?;
const SIZE: usize = 16;

let n_chunks = (self.0.len() + (SIZE - 1)) / SIZE;
for (i, c) in self.0.chunks(SIZE).enumerate() {
if i > 0 {
f.pad("")?;
}

f.write_str(&hex::encode(c))?;

if i < n_chunks - 1 {
f.write_str("\n")?;
}
}
Ok(())
}
Expand Down
66 changes: 36 additions & 30 deletions test-vectors/aes-ctr-hmac.md
Original file line number Diff line number Diff line change
@@ -1,42 +1,48 @@
~~~
cipher_suite: 0x0001
key: 0001020304050607 08090a0b0c0d0e0f 1011121314151617
18191a1b1c1d1e1f 2021222324252627 28292a2b2c2d2e2f
enc_key: 0001020304050607 08090a0b0c0d0e0f
auth_key: 1011121314151617 18191a1b1c1d1e1f 2021222324252627
28292a2b2c2d2e2f
nonce: 1011121314151617 18191a1b
aad: 4945544620534672 616d65205747
pt: 64726166742d6965 74662d736672616d 652d656e63
ct: 6339af04ada1d064 688a442b8dc69d5b 6bfa40f4bef0583e
8081069cc60705
key: 000102030405060708090a0b0c0d0e0f
101112131415161718191a1b1c1d1e1f
202122232425262728292a2b2c2d2e2f
enc_key: 000102030405060708090a0b0c0d0e0f
auth_key: 101112131415161718191a1b1c1d1e1f
202122232425262728292a2b2c2d2e2f
nonce: 101112131415161718191a1b
aad: 4945544620534672616d65205747
pt: 64726166742d696574662d736672616d
652d656e63
ct: 6339af04ada1d064688a442b8dc69d5b
6bfa40f4bef0583e8081069cc60705
~~~

~~~
cipher_suite: 0x0002
key: 0001020304050607 08090a0b0c0d0e0f 1011121314151617
18191a1b1c1d1e1f 2021222324252627 28292a2b2c2d2e2f
enc_key: 0001020304050607 08090a0b0c0d0e0f
auth_key: 1011121314151617 18191a1b1c1d1e1f 2021222324252627
28292a2b2c2d2e2f
nonce: 1011121314151617 18191a1b
aad: 4945544620534672 616d65205747
pt: 64726166742d6965 74662d736672616d 652d656e63
ct: 6339af04ada1d064 688a442b8dc69d5b 6bfa40f4be6e93b7
da076927bb
key: 000102030405060708090a0b0c0d0e0f
101112131415161718191a1b1c1d1e1f
202122232425262728292a2b2c2d2e2f
enc_key: 000102030405060708090a0b0c0d0e0f
auth_key: 101112131415161718191a1b1c1d1e1f
202122232425262728292a2b2c2d2e2f
nonce: 101112131415161718191a1b
aad: 4945544620534672616d65205747
pt: 64726166742d696574662d736672616d
652d656e63
ct: 6339af04ada1d064688a442b8dc69d5b
6bfa40f4be6e93b7da076927bb
~~~

~~~
cipher_suite: 0x0003
key: 0001020304050607 08090a0b0c0d0e0f 1011121314151617
18191a1b1c1d1e1f 2021222324252627 28292a2b2c2d2e2f
enc_key: 0001020304050607 08090a0b0c0d0e0f
auth_key: 1011121314151617 18191a1b1c1d1e1f 2021222324252627
28292a2b2c2d2e2f
nonce: 1011121314151617 18191a1b
aad: 4945544620534672 616d65205747
pt: 64726166742d6965 74662d736672616d 652d656e63
ct: 6339af04ada1d064 688a442b8dc69d5b 6bfa40f4be094805
09
key: 000102030405060708090a0b0c0d0e0f
101112131415161718191a1b1c1d1e1f
202122232425262728292a2b2c2d2e2f
enc_key: 000102030405060708090a0b0c0d0e0f
auth_key: 101112131415161718191a1b1c1d1e1f
202122232425262728292a2b2c2d2e2f
nonce: 101112131415161718191a1b
aad: 4945544620534672616d65205747
pt: 64726166742d696574662d736672616d
652d656e63
ct: 6339af04ada1d064688a442b8dc69d5b
6bfa40f4be09480509
~~~

Loading

0 comments on commit 89f2f75

Please sign in to comment.