You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
is it possible to access digest state prior to finalizing it? #975
Howdy. I am fairly new to Rust and even newer to cryptographic stuff so please feel free to set me straight if I say funky nonsensical things.
I am currently looking to implement an HTTP API spec that calls for supporting chunked uploads of byte-oriented data to some backend. As part of upload processing, implementations are supposed to validate that the content received matches a digest given at the start of uploading. What I want to do is figure out how to persist partial digest state between HTTP requests when different HTTP servers may handling each subsequent requests in the series of chunked uploads. I do have available to me a database backend where I could store a hypothetical serialized hasher state to be updated and retrieved on each successive request. In this context i believe it is safe to assume that chunk uploads will happen serially across the distributed system so concurrent access to the persisted state isn't a major concern.
In my use case I need to support a variety of different hash algorithms so I am using DynDigest to represent the hasher and incrementally update the state using the update method while streaming data to the backend. So my ideal solution here would be some method on DynDigest that allows one to serialize the hasher state then deserialize it to continue updating at a later time.
Does this sound like a use case that is either already supported or which the maintainers of this project would be open to?
Almost nothing "happens serially across a distributed system" and "supporting a variety of different hash algorithms" tends to be a mistake, but..
blake3 is designed for streaming and incremental updates, which maybe fits your use case.
merlin provides a "transcript" abstraction using STROBE, which avoids any finalization notion. It fits zero knowledge proofs nicely, but probably not great for you.
Neither of these is going to be compatible with the traits here obviously.
The spec I'm implementing doesn't give implementations a choice of hash algorithm. Clients already exist in the wild using sha256 and sha512, and maybe sha384 though it's not officially required to be supported by the spec.
Almost nothing "happens serially across a distributed system"
In this case, chunked uploads are of binary objects where the object is broken down by the single client uploading it to make it easier to reduce the risk of timeouts through various network layers.
I don't think it matters much, but this is the spec I am implementing so the prototypical client is a docker daemon or some other container runtime or container building apparatus. The objects being uploaded are typically compressed archives containing image layers. The spec supports chunking because image layers can be quite large (usually for not great reasons, sometimes for understandable reasons). It's hard for me to imagine that the object would be chunked then the chunks uploaded concurrently -- having had the chance to dig around in the code that implements docker daemon chunk uploading, I vaguely recall it being serialized.
This is why I feel comfortable assuming the chunks will be received serially. But you're probably right, someone out there may think it somehow makes sense to chunk a blob and try to upload the chunks concurrently.
"supporting a variety of different hash algorithms" tends to be a mistake, but..
I don't know man. I'm pretty ignorant of hash algorithms or why anyone would choose one over the other. I trust you when you say it's a mistake, but like I said I'm just implementing a spec. Or trying to. Mostly because I don't like working with the go code that does the same thing, but also arguably because I enjoy pain and suffering.
Anyway, I guess it sounds like you don't want to support something like this? It seems conceptually possible for any algorithm that implements DynDigest given that digests can be calculated incrementally with the update method...
In this case, one should normally use a Merkle tree, which is what blake3 does under the hood. Yet here, you do not require blake3's fine resolution, only leaves being individual chunk hashes, so any hash function works. If the spec uses a Merkle tree here then that's how you should achieve the desired effect.
@tarcieri thanks for the suggestion! Currently trying to wrap my head around how to convert a &[u8] into the &[GenericArray<u8, U64>] that's necessary to use this. If you have any suggestions or examples I'd appreciate them!
@newpavlov that's helpful! But what if buf isn't evenly divisible by 64? Is it okay to pad the slice with 0s or would that break the digest calculation? I'm curious because in my use case the input bytes are going to be of variable, non-predetermined length.
Hmm, it looks to me like https://docs.rs/block-buffer/0.10.2/block_buffer/struct.BlockBuffer.html# may help out with this. Would I just call digest_blocks with a closure that handles accepting the blocks it produces and compressing them into the state that i maintain in my own struct...? Then every time I've reached the end of a stream of bytes I call pad_with_zeros followed by digest_blocks one final time? (assuming that compressing 0-valued bytes into the digest state doesn't break anything)
I'll give this a try and write some unit tests (in my code that is) to verify the same digest is calculated using this approach as with the high-level API with different byte length edge cases.
Activity
burdges commentedon Apr 1, 2022
Almost nothing "happens serially across a distributed system" and "supporting a variety of different hash algorithms" tends to be a mistake, but..
blake3 is designed for streaming and incremental updates, which maybe fits your use case.
merlin provides a "transcript" abstraction using STROBE, which avoids any finalization notion. It fits zero knowledge proofs nicely, but probably not great for you.
Neither of these is going to be compatible with the traits here obviously.
waynr commentedon Apr 1, 2022
The spec I'm implementing doesn't give implementations a choice of hash algorithm. Clients already exist in the wild using sha256 and sha512, and maybe sha384 though it's not officially required to be supported by the spec.
In this case, chunked uploads are of binary objects where the object is broken down by the single client uploading it to make it easier to reduce the risk of timeouts through various network layers.
I don't think it matters much, but this is the spec I am implementing so the prototypical client is a docker daemon or some other container runtime or container building apparatus. The objects being uploaded are typically compressed archives containing image layers. The spec supports chunking because image layers can be quite large (usually for not great reasons, sometimes for understandable reasons). It's hard for me to imagine that the object would be chunked then the chunks uploaded concurrently -- having had the chance to dig around in the code that implements docker daemon chunk uploading, I vaguely recall it being serialized.
This is why I feel comfortable assuming the chunks will be received serially. But you're probably right, someone out there may think it somehow makes sense to chunk a blob and try to upload the chunks concurrently.
I don't know man. I'm pretty ignorant of hash algorithms or why anyone would choose one over the other. I trust you when you say it's a mistake, but like I said I'm just implementing a spec. Or trying to. Mostly because I don't like working with the go code that does the same thing, but also arguably because I enjoy pain and suffering.
Anyway, I guess it sounds like you don't want to support something like this? It seems conceptually possible for any algorithm that implements
DynDigestgiven that digests can be calculated incrementally with theupdatemethod...burdges commentedon Apr 1, 2022
In this case, one should normally use a Merkle tree, which is what blake3 does under the hood. Yet here, you do not require blake3's fine resolution, only leaves being individual chunk hashes, so any hash function works. If the spec uses a Merkle tree here then that's how you should achieve the desired effect.
newpavlov commentedon Apr 1, 2022
Currently it's not possible, but you are not the first one to ask for such functionality, so we probably will add it. See: RustCrypto/hashes#310
I am going to close this issue in favor of the linked one.
tarcieri commentedon Apr 1, 2022
Note that the SHA2 compression function can be accessed as part of the public API.
You can write your own type which looks something like this as a stopgap: https://github.com/RustCrypto/hashes/blob/08c995c/sha2/src/core_api.rs#L13-L69
waynr commentedon Apr 24, 2022
@tarcieri thanks for the suggestion! Currently trying to wrap my head around how to convert a
&[u8]into the&[GenericArray<u8, U64>]that's necessary to use this. If you have any suggestions or examples I'd appreciate them!newpavlov commentedon Apr 24, 2022
@waynr
Currently, it requires a bit of unsafe code:
In future you will be able to replace it with
as_chunks.waynr commentedon Apr 24, 2022
@newpavlov that's helpful! But what if
bufisn't evenly divisible by 64? Is it okay to pad the slice with 0s or would that break the digest calculation? I'm curious because in my use case the input bytes are going to be of variable, non-predetermined length.waynr commentedon Apr 24, 2022
Hmm, it looks to me like https://docs.rs/block-buffer/0.10.2/block_buffer/struct.BlockBuffer.html# may help out with this. Would I just call
digest_blockswith a closure that handles accepting the blocks it produces and compressing them into the state that i maintain in my own struct...? Then every time I've reached the end of a stream of bytes I callpad_with_zerosfollowed bydigest_blocksone final time? (assuming that compressing 0-valued bytes into the digest state doesn't break anything)I'll give this a try and write some unit tests (in my code that is) to verify the same digest is calculated using this approach as with the high-level API with different byte length edge cases.