-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(dpapi): implement encryption and key derivation functions #350
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
89f68d1
feat(dpapi): crypto: implement all needed key derivation functions ex…
TheBestTvarynka 1e11336
feat(dpapi): crypto: implement `kdf` (kbkdf);
TheBestTvarynka 17bfd2a
feat(dpapi): crypto: improve `kdf` implemenation: support different h…
TheBestTvarynka caacf63
feat(dpapi): crypto: add tests for DH;
TheBestTvarynka 151aaf1
feat(dpapi): crypto: implement aes256 key wrapping/unwrapping;
TheBestTvarynka 12f979c
feat(dpapi): crypto: implement content encryption/decryption;
TheBestTvarynka 3bca771
feat(dpapi): implement `GroupKeyEnvelope::new_kek` and `GroupKeyEnvel…
TheBestTvarynka f520201
refactor(dpapi): crypto: refactoring;
TheBestTvarynka ba37c1e
refactor(dpapi): crypto;
TheBestTvarynka 3b79c87
chore: move hmac to workspace deps;
TheBestTvarynka 48e0c4f
reactor(dpapi): small refactoring;"
TheBestTvarynka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use hmac::{Hmac, Mac}; | ||
use rust_kbkdf::{PseudoRandomFunction, PseudoRandomFunctionKey}; | ||
|
||
use super::{CryptoError, CryptoResult}; | ||
|
||
pub struct HmacShaPrfKey<'key>(&'key [u8]); | ||
|
||
impl<'key> HmacShaPrfKey<'key> { | ||
pub fn new(key: &'key [u8]) -> Self { | ||
Self(key) | ||
} | ||
|
||
pub fn key(&self) -> &[u8] { | ||
self.0 | ||
} | ||
} | ||
|
||
impl<'key> PseudoRandomFunctionKey for HmacShaPrfKey<'key> { | ||
type KeyHandle = HmacShaPrfKey<'key>; | ||
|
||
fn key_handle(&self) -> &Self::KeyHandle { | ||
self | ||
} | ||
} | ||
|
||
macro_rules! define_hmac_sha_prf { | ||
($name:ident, $sha:ty, $out_size:ty) => { | ||
pub struct $name { | ||
hmac: Option<Hmac<$sha>>, | ||
} | ||
|
||
impl $name { | ||
pub fn new() -> Self { | ||
Self { hmac: None } | ||
} | ||
} | ||
|
||
impl<'a> PseudoRandomFunction<'a> for $name { | ||
type KeyHandle = HmacShaPrfKey<'a>; | ||
type PrfOutputSize = $out_size; | ||
type Error = CryptoError; | ||
|
||
fn init( | ||
&mut self, | ||
key: &'a dyn PseudoRandomFunctionKey<KeyHandle = HmacShaPrfKey<'a>>, | ||
) -> CryptoResult<()> { | ||
self.hmac = Some(Hmac::<$sha>::new_from_slice(key.key_handle().key()).map_err(|_| { | ||
use hmac::digest::crypto_common::KeySizeUser; | ||
|
||
CryptoError::InvalidKeyLength { | ||
expected: Hmac::<$sha>::key_size(), | ||
actual: key.key_handle().key().len(), | ||
} | ||
})?); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn update(&mut self, msg: &[u8]) -> CryptoResult<()> { | ||
if let Some(hmac) = self.hmac.as_mut() { | ||
hmac.update(msg); | ||
|
||
Ok(()) | ||
} else { | ||
Err(CryptoError::Uninitialized("HMAC hasher")) | ||
} | ||
} | ||
|
||
fn finish(&mut self, out: &mut [u8]) -> CryptoResult<usize> { | ||
if let Some(hmac) = self.hmac.as_mut() { | ||
let hmac = hmac.clone().finalize().into_bytes(); | ||
|
||
out.copy_from_slice(hmac.as_slice()); | ||
|
||
Ok(hmac.as_slice().len()) | ||
} else { | ||
Err(CryptoError::Uninitialized("HMAC hasher")) | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
define_hmac_sha_prf!(HmacSha1Prf, sha1::Sha1, typenum::U20); | ||
define_hmac_sha_prf!(HmacSha256Prf, sha2::Sha256, typenum::U32); | ||
define_hmac_sha_prf!(HmacSha384Prf, sha2::Sha384, typenum::U48); | ||
define_hmac_sha_prf!(HmacSha512Prf, sha2::Sha512, typenum::U64); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is it awful? Absolutely yes.
Do we have any alternative? Not really. See this: RustCrypto/KDFs#75 (comment)
I think the best solution is to contribute to RustCrypto. Or we can fork the
rust-kbkdf
crateThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The best approach is typically to contribute to the upstream project, because this gives back to the open source community while removing maintenance burden from our shoulders. We definitely can’t release a crate with a git dependency. As you said, either we contribute back, either we go the fork route with a crate we maintain and push to crates.io. I’ll link this to Marc-André, and we’ll see how we prioritize this work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@TheBestTvarynka UPD: We agreed with Marc that the best course of action was to contribute KBKDF to RustCrypto.
Your priority should be to move this forward so we’re not stuck with a git dependency when it’s time to integrate dpdpi into sspi.
It’s fine to merge work on dpdpi even with the git dependency, because dpdpi is not depended on by our other crates that we are publishing to crates.io yet.
Feel free to rotate back on the dpdpi work when you wait for their feedback.
RustCrypto maintainers are pretty responsive, so I believe things should move forward quickly 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great!