Skip to content

Commit 6283af5

Browse files
Add sha256 to hkdf name
1 parent f0babe5 commit 6283af5

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

src/hpke.rs

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rand_core::{CryptoRng, RngCore};
2121
use salty::agreement as x25519;
2222

2323
const X25519_KEM_SUITE_ID: &[u8] = b"KEM\x00\x20";
24-
const X25519_HKDF_CHACHA20_POLY1305_HPKE_SUITE_ID: &[u8] = b"HPKE\x00\x20\x00\x01\x00\x03";
24+
const X25519_HKDF_SHA256_CHACHA20_POLY1305_HPKE_SUITE_ID: &[u8] = b"HPKE\x00\x20\x00\x01\x00\x03";
2525

2626
fn labeled_extract(
2727
suite_id: &[u8],
@@ -120,43 +120,51 @@ trait Aead:
120120
>
121121
{
122122
const AEAD_ID: u16;
123-
const X25519_HKDF_SELF_HPKE_SUITE_ID: &'static [u8];
123+
const X25519_HKDF_SHA256_SELF_HPKE_SUITE_ID: &'static [u8];
124124
}
125125

126126
impl Aead for ChaCha20Poly1305 {
127127
const AEAD_ID: u16 = 0x0003;
128-
const X25519_HKDF_SELF_HPKE_SUITE_ID: &'static [u8] =
129-
X25519_HKDF_CHACHA20_POLY1305_HPKE_SUITE_ID;
128+
const X25519_HKDF_SHA256_SELF_HPKE_SUITE_ID: &'static [u8] =
129+
X25519_HKDF_SHA256_CHACHA20_POLY1305_HPKE_SUITE_ID;
130130
}
131131

132132
impl Aead for ChaCha8Poly1305 {
133133
/// Custom non-standard Id
134134
const AEAD_ID: u16 = 0xFFFE;
135-
const X25519_HKDF_SELF_HPKE_SUITE_ID: &'static [u8] = b"HPKE\x00\x20\x00\x01\xFF\xFE";
135+
const X25519_HKDF_SHA256_SELF_HPKE_SUITE_ID: &'static [u8] = b"HPKE\x00\x20\x00\x01\xFF\xFE";
136136
}
137137

138138
const NK: usize = 32;
139139
const NN: usize = 12;
140140
const NH: usize = 32;
141141

142142
fn key_schedule<T: Aead>(_role: Role, shared_secret: [u8; 32], info: &[u8]) -> Context {
143-
let (_, psk_id_hash) =
144-
labeled_extract(T::X25519_HKDF_SELF_HPKE_SUITE_ID, b"", b"psk_id_hash", b"");
145-
let (_, info_hash) =
146-
labeled_extract(T::X25519_HKDF_SELF_HPKE_SUITE_ID, b"", b"info_hash", info);
143+
let (_, psk_id_hash) = labeled_extract(
144+
T::X25519_HKDF_SHA256_SELF_HPKE_SUITE_ID,
145+
b"",
146+
b"psk_id_hash",
147+
b"",
148+
);
149+
let (_, info_hash) = labeled_extract(
150+
T::X25519_HKDF_SHA256_SELF_HPKE_SUITE_ID,
151+
b"",
152+
b"info_hash",
153+
info,
154+
);
147155
let mut key_schedule_context = [0; 65];
148156
key_schedule_context[0] = MODE_BASE;
149157
key_schedule_context[1..33].copy_from_slice(&psk_id_hash);
150158
key_schedule_context[33..].copy_from_slice(&info_hash);
151159
let (secret, _) = labeled_extract(
152-
T::X25519_HKDF_SELF_HPKE_SUITE_ID,
160+
T::X25519_HKDF_SHA256_SELF_HPKE_SUITE_ID,
153161
&shared_secret,
154162
b"secret",
155163
b"",
156164
);
157165
let mut key = [0; NK];
158166
labeled_expand(
159-
T::X25519_HKDF_SELF_HPKE_SUITE_ID,
167+
T::X25519_HKDF_SHA256_SELF_HPKE_SUITE_ID,
160168
&secret,
161169
b"key",
162170
&key_schedule_context,
@@ -165,7 +173,7 @@ fn key_schedule<T: Aead>(_role: Role, shared_secret: [u8; 32], info: &[u8]) -> C
165173
.expect("KEY is not too large");
166174
let mut base_nonce = [0; NN];
167175
labeled_expand(
168-
T::X25519_HKDF_SELF_HPKE_SUITE_ID,
176+
T::X25519_HKDF_SHA256_SELF_HPKE_SUITE_ID,
169177
&secret,
170178
b"base_nonce",
171179
&key_schedule_context,
@@ -174,7 +182,7 @@ fn key_schedule<T: Aead>(_role: Role, shared_secret: [u8; 32], info: &[u8]) -> C
174182
.expect("NONCE is not too large");
175183
let mut exporter_secret = [0; NH];
176184
labeled_expand(
177-
T::X25519_HKDF_SELF_HPKE_SUITE_ID,
185+
T::X25519_HKDF_SHA256_SELF_HPKE_SUITE_ID,
178186
&secret,
179187
b"exp",
180188
&key_schedule_context,
@@ -575,16 +583,16 @@ mod tests {
575583
}
576584

577585
const X25519_KEM_ID: u16 = 0x0020;
578-
const HKDF_KDF_ID: u16 = 0x0001;
586+
const HKDF_SHA256_KDF_ID: u16 = 0x0001;
579587
fn assert_suite_id<T: Aead>() {
580588
let calculated_id: Vec<u8> = b"HPKE"
581589
.iter()
582590
.copied()
583591
.chain(X25519_KEM_ID.to_be_bytes())
584-
.chain(HKDF_KDF_ID.to_be_bytes())
592+
.chain(HKDF_SHA256_KDF_ID.to_be_bytes())
585593
.chain(T::AEAD_ID.to_be_bytes())
586594
.collect();
587-
assert_eq!(T::X25519_HKDF_SELF_HPKE_SUITE_ID, &calculated_id);
595+
assert_eq!(T::X25519_HKDF_SHA256_SELF_HPKE_SUITE_ID, &calculated_id);
588596
}
589597

590598
#[test]

0 commit comments

Comments
 (0)