Skip to content

Commit a9059db

Browse files
committed
Add more setters for EcKdf.
Adds setters for the ANSI X9.63 KDFs (`EcKdf::sha1`, etc) and the NIST SP800-56A KDFs (`EcKdf::sha1_sp800`) to `EcKdf`. Fixes `EcKdf::sha256`. Fixes #281. Signed-off-by: Neal H. Walfield <[email protected]>
1 parent 0169642 commit a9059db

File tree

1 file changed

+58
-11
lines changed

1 file changed

+58
-11
lines changed

cryptoki/src/mechanism/elliptic_curve.rs

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,45 @@ pub struct EcKdf<'a> {
8282
shared_data: Option<&'a [u8]>,
8383
}
8484

85-
impl EcKdf<'_> {
85+
macro_rules! ansi {
86+
{ $func_name: ident, $algo: ident, $algo_name: literal } => {
87+
#[doc = "The key derivation function based on "]
88+
#[doc = $algo_name]
89+
#[doc = " as defined in the ANSI X9.63 standard. The
90+
derived key is produced by concatenating hashes of
91+
the shared value followed by 00000001, 00000002,
92+
etc. until we find enough bytes to fill the
93+
`CKA_VALUE_LEN` of the derived key."]
94+
pub fn $func_name(shared_data: &'a [u8]) -> Self {
95+
Self {
96+
kdf_type: $algo,
97+
shared_data: Some(shared_data),
98+
}
99+
}
100+
}
101+
}
102+
103+
macro_rules! sp800 {
104+
{ $func_name: ident, $algo: ident, $algo_name: literal } => {
105+
#[doc = "The key derivation function based on "]
106+
#[doc = $algo_name]
107+
#[doc = " as defined in the [NIST SP800-56A standard, revision
108+
2](http://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-56Ar2.pdf),
109+
section 5.8.1.1. The derived key is produced by
110+
concatenating hashes of 00000001, 00000002,
111+
etc. followed by the shared value until we find
112+
enough bytes to fill the `CKA_VALUE_LEN` of the
113+
derived key."]
114+
pub fn $func_name(shared_data: &'a [u8]) -> Self {
115+
Self {
116+
kdf_type: $algo,
117+
shared_data: Some(shared_data),
118+
}
119+
}
120+
}
121+
}
122+
123+
impl<'a> EcKdf<'a> {
86124
/// The null transformation. The derived key value is produced by
87125
/// taking bytes from the left of the agreed value. The new key
88126
/// size is limited to the size of the agreed value.
@@ -93,16 +131,25 @@ impl EcKdf<'_> {
93131
}
94132
}
95133

96-
/// The key derivation function based on sha256 as defined in the ANSI X9.63 standard. The
97-
/// derived key is produced by concatenating hashes of the shared
98-
/// value followed by 00000001, 00000002, etc. until we find
99-
/// enough bytes to fill the `CKA_VALUE_LEN` of the derived key.
100-
pub fn sha256() -> Self {
101-
Self {
102-
kdf_type: CKD_SHA256_KDF,
103-
shared_data: None,
104-
}
105-
}
134+
ansi!(sha1, CKD_SHA1_KDF, "SHA1");
135+
ansi!(sha224, CKD_SHA224_KDF, "SHA224");
136+
ansi!(sha256, CKD_SHA256_KDF, "SHA256");
137+
ansi!(sha384, CKD_SHA384_KDF, "SHA384");
138+
ansi!(sha512, CKD_SHA512_KDF, "SHA512");
139+
ansi!(sha3_224, CKD_SHA3_224_KDF, "SHA3_224");
140+
ansi!(sha3_256, CKD_SHA3_256_KDF, "SHA3_256");
141+
ansi!(sha3_384, CKD_SHA3_384_KDF, "SHA3_384");
142+
ansi!(sha3_512, CKD_SHA3_512_KDF, "SHA3_512");
143+
144+
sp800!(sha1_sp800, CKD_SHA1_KDF_SP800, "SHA1");
145+
sp800!(sha224_sp800, CKD_SHA224_KDF_SP800, "SHA224");
146+
sp800!(sha256_sp800, CKD_SHA256_KDF_SP800, "SHA256");
147+
sp800!(sha384_sp800, CKD_SHA384_KDF_SP800, "SHA384");
148+
sp800!(sha512_sp800, CKD_SHA512_KDF_SP800, "SHA512");
149+
sp800!(sha3_224_sp800, CKD_SHA3_224_KDF_SP800, "SHA3_224");
150+
sp800!(sha3_256_sp800, CKD_SHA3_256_KDF_SP800, "SHA3_256");
151+
sp800!(sha3_384_sp800, CKD_SHA3_384_KDF_SP800, "SHA3_384");
152+
sp800!(sha3_512_sp800, CKD_SHA3_512_KDF_SP800, "SHA3_512");
106153

107154
// The intention here is to be able to support other methods with
108155
// shared data, without it being a breaking change, by just adding

0 commit comments

Comments
 (0)