1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#![doc = include_str!("../Readme.md")]
use error::Error;
use types::{AeadAlgorithm, KemAlgorithm};
pub mod error;
pub mod types;
pub use rand::{CryptoRng, RngCore};
pub trait HpkeCrypto: core::fmt::Debug + Send + Sync {
type HpkePrng: rand::RngCore + rand::CryptoRng + HpkeTestRng;
fn supports_kdf(alg: types::KdfAlgorithm) -> Result<(), Error>;
fn supports_kem(alg: types::KemAlgorithm) -> Result<(), Error>;
fn supports_aead(alg: types::AeadAlgorithm) -> Result<(), Error>;
fn prng() -> Self::HpkePrng;
#[inline(always)]
fn kdf_digest_length(alg: types::KdfAlgorithm) -> usize {
match alg {
types::KdfAlgorithm::HkdfSha256 => 32,
types::KdfAlgorithm::HkdfSha384 => 48,
types::KdfAlgorithm::HkdfSha512 => 64,
}
}
fn kdf_extract(alg: types::KdfAlgorithm, salt: &[u8], ikm: &[u8]) -> Vec<u8>;
fn kdf_expand(
alg: types::KdfAlgorithm,
prk: &[u8],
info: &[u8],
output_size: usize,
) -> Result<Vec<u8>, Error>;
fn kem_derive(alg: KemAlgorithm, pk: &[u8], sk: &[u8]) -> Result<Vec<u8>, Error>;
fn kem_derive_base(alg: KemAlgorithm, sk: &[u8]) -> Result<Vec<u8>, Error>;
fn kem_key_gen(alg: KemAlgorithm, prng: &mut Self::HpkePrng) -> Result<Vec<u8>, Error>;
fn kem_validate_sk(alg: KemAlgorithm, sk: &[u8]) -> Result<Vec<u8>, Error>;
fn aead_seal(
alg: AeadAlgorithm,
key: &[u8],
nonce: &[u8],
aad: &[u8],
msg: &[u8],
) -> Result<Vec<u8>, Error>;
fn aead_open(
alg: AeadAlgorithm,
key: &[u8],
nonce: &[u8],
aad: &[u8],
msg: &[u8],
) -> Result<Vec<u8>, Error>;
fn aead_key_length(alg: AeadAlgorithm) -> usize {
match alg {
AeadAlgorithm::Aes128Gcm => 16,
AeadAlgorithm::Aes256Gcm => 32,
AeadAlgorithm::ChaCha20Poly1305 => 32,
AeadAlgorithm::HpkeExport => 0,
}
}
fn aead_nonce_length(alg: AeadAlgorithm) -> usize {
match alg {
AeadAlgorithm::Aes128Gcm => 12,
AeadAlgorithm::Aes256Gcm => 12,
AeadAlgorithm::ChaCha20Poly1305 => 12,
AeadAlgorithm::HpkeExport => 0,
}
}
fn aead_tag_length(alg: AeadAlgorithm) -> usize {
match alg {
AeadAlgorithm::Aes128Gcm => 16,
AeadAlgorithm::Aes256Gcm => 16,
AeadAlgorithm::ChaCha20Poly1305 => 16,
AeadAlgorithm::HpkeExport => 0,
}
}
}
pub trait HpkeTestRng {
fn try_fill_test_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error>;
fn seed(&mut self, seed: &[u8]);
}