-
Notifications
You must be signed in to change notification settings - Fork 231
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
Attempt at implementing streaming verification #303
Draft
Monadic-Cat
wants to merge
1
commit into
dalek-cryptography:main
Choose a base branch
from
Monadic-Cat:streaming-verification
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
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
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,172 @@ | ||
use crate::{SignatureError, VerifyingKey}; | ||
use crate::Signature; | ||
use crate::hazmat; | ||
use sha2::{Sha512, Digest}; | ||
|
||
pub struct StreamingVerifier { | ||
/// Public key to verify with. | ||
pub(crate) public_key: VerifyingKey, | ||
|
||
/// Candidate signature to verify against. | ||
pub(crate) signature: Signature, | ||
|
||
/// Hash state. | ||
pub(crate) hasher: Sha512, | ||
} | ||
|
||
impl StreamingVerifier { | ||
// Note: I changed the signature parameter to use Signature | ||
// instead of InternalSignature, because raw_verify consumes &Signature. | ||
/// Constructs a new stream verifier. | ||
/// | ||
/// Seeds hash state with public key and signature components. | ||
pub(crate) fn new(public_key: VerifyingKey, signature: Signature) -> Self { | ||
let mut hasher = Sha512::new(); | ||
hasher.update(signature.r_bytes()); | ||
hasher.update(public_key.as_bytes()); | ||
|
||
Self { public_key, hasher, signature } | ||
} | ||
|
||
// Note: I changed the chunk parameter to use &[u8] instead of | ||
// impl AsRef<[u8]> because I think it's better. :V | ||
/// Digest message chunk. | ||
pub fn update(&mut self, chunk: &[u8]) { | ||
self.hasher.update(chunk); | ||
} | ||
|
||
pub fn finalize_and_verify(self) -> Result<(), SignatureError> { | ||
hazmat::raw_verify::<dirty_workaround::DirtyWorkaround>(&self.public_key, self.hasher.finalize().as_slice(), &self.signature) | ||
} | ||
} | ||
|
||
// So, in normal usage, hazmat::raw_verify uses CtxDigest to hash | ||
// together the things we already did in the process of running through new() and a series | ||
// of update()s. | ||
// | ||
// Here, we workaround the fact that there is no method in hazmat to provide | ||
// that hash already computed (raw_verify_prehashed is not it), | ||
// by making a hasher that plucks out the hash when provided as the message | ||
// inside verifying::compute_challenge(). | ||
// | ||
// It would be *much* better if such a method were just added to hazmat, | ||
// as we'd avoid the need for this whole module. | ||
mod dirty_workaround { | ||
use curve25519_dalek::digest::{Update, FixedOutput, FixedOutputReset, Output, Reset}; | ||
use curve25519_dalek::digest::typenum::Unsigned; | ||
use curve25519_dalek::digest::generic_array::typenum::U64; | ||
use sha2::Digest; | ||
use sha2::digest::OutputSizeUser; | ||
|
||
/// The number of bytes which precede the message | ||
/// argument to hazmat::raw_verify being fed to this hasher. | ||
/// It is 2 * (the length of CompressedEdwardsY). | ||
const PREFIX_BYTES: usize = 64; | ||
/// The number of bytes which we need to sneak through | ||
/// the message argument into our hash. | ||
const HASH_LENGTH: usize = 64; | ||
|
||
pub(crate) struct DirtyWorkaround { | ||
step: usize, | ||
hash: [u8; HASH_LENGTH], | ||
} | ||
|
||
impl Default for DirtyWorkaround { | ||
fn default() -> Self { | ||
Self { | ||
step: 0, | ||
hash: [0; HASH_LENGTH], | ||
} | ||
} | ||
} | ||
|
||
impl OutputSizeUser for DirtyWorkaround { | ||
type OutputSize = U64; | ||
} | ||
|
||
impl Update for DirtyWorkaround { | ||
fn update(&mut self, data: &[u8]) { | ||
if self.step >= PREFIX_BYTES && self.step < PREFIX_BYTES + HASH_LENGTH { | ||
let buf = &mut self.hash[self.step - PREFIX_BYTES..]; | ||
buf.copy_from_slice(data); | ||
} | ||
if self.step == PREFIX_BYTES { | ||
self.hash.copy_from_slice(data.as_ref()); | ||
} else if self.step > PREFIX_BYTES + HASH_LENGTH { | ||
unreachable!("this should never happen") | ||
} | ||
self.step += data.len(); | ||
} | ||
} | ||
|
||
impl Reset for DirtyWorkaround { | ||
fn reset(&mut self) { | ||
self.step = 0; | ||
self.hash = [0; HASH_LENGTH]; | ||
} | ||
} | ||
|
||
impl FixedOutput for DirtyWorkaround { | ||
fn finalize_into(self, out: &mut Output<Self>) { | ||
out.copy_from_slice(&self.hash); | ||
} | ||
} | ||
|
||
impl FixedOutputReset for DirtyWorkaround { | ||
fn finalize_into_reset(&mut self, out: &mut Output<Self>) { | ||
out.copy_from_slice(&self.hash); | ||
<Self as Reset>::reset(self); | ||
} | ||
} | ||
|
||
impl Digest for DirtyWorkaround { | ||
fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
fn new_with_prefix(data: impl AsRef<[u8]>) -> Self { | ||
let mut hasher = Self::new(); | ||
<Self as Digest>::update(&mut hasher, data); | ||
hasher | ||
} | ||
|
||
fn update(&mut self, data: impl AsRef<[u8]>) { | ||
<Self as Update>::update(self, data.as_ref()) | ||
} | ||
|
||
fn chain_update(mut self, data: impl AsRef<[u8]>) -> Self { | ||
<Self as Digest>::update(&mut self, data); | ||
self | ||
} | ||
|
||
fn finalize(self) -> Output<Self> { | ||
let mut out = Output::<Self>::default(); | ||
<Self as Digest>::finalize_into(self, &mut out); | ||
out | ||
} | ||
|
||
fn finalize_into(self, out: &mut Output<Self>) { | ||
out.copy_from_slice(&self.hash); | ||
} | ||
|
||
fn finalize_reset(&mut self) -> Output<Self> where Self: FixedOutputReset { | ||
<Self as FixedOutputReset>::finalize_fixed_reset(self) | ||
} | ||
|
||
fn finalize_into_reset(&mut self, out: &mut Output<Self>) where Self: FixedOutputReset { | ||
<Self as FixedOutputReset>::finalize_into_reset(self, out) | ||
} | ||
|
||
fn reset(&mut self) where Self: Reset { | ||
<Self as Reset>::reset(self) | ||
} | ||
|
||
fn output_size() -> usize { | ||
Self::OutputSize::to_usize() | ||
} | ||
|
||
fn digest(data: impl AsRef<[u8]>) -> Output<Self> { | ||
Self::new_with_prefix(data).finalize() | ||
} | ||
} | ||
} |
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
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.
Perhaps this module could be
src/verifying/streaming.rs
?