New feature: SerializableState#48
Draft
ounsworth wants to merge 3 commits into
Draft
Conversation
Contributor
Author
|
For now, I have added the trait and impl'd it for SHA2. I'll pause here and give a chance for people to give feedback before I continue with doing the same for:
|
npajkovsky
reviewed
Jul 1, 2026
| ///* if a == b => 0 | ||
| ///* if a > b => 1 | ||
| pub fn cmp_lib_ver(a: &[u8; 3], b: &[u8; 3]) -> i8 { | ||
| if a[0] < b[0] { |
Collaborator
There was a problem hiding this comment.
let a_ver = a[2] << 16 | a[1] << 8 | a[0]
let b_ver = b[2] << 16 | b[1] << 8 | b[0]
if a_ver < b_ver
return -1;
else if ...Also, give it a type such as SemVer([u8; 3]), and then impl Ord would be a much rusty way. Or even better,
struct SemVer(u32);
impl SemVer
fn new(major: u8, minor: u8, patch: u8) -> Self {
Self(major << 16 | ...)
}| // insert the version tag | ||
| let out: &mut [u8; 105] = add_lib_ver(&mut out_to_return).try_into().unwrap(); | ||
|
|
||
| // state.h: [u32; 8] |
| out[40..104].copy_from_slice(&self.x_buf); | ||
|
|
||
| // x_buf_off: usize | ||
| // in general, a usize should be serialized into a u64, but in this case, it can't ever be larger than 64 |
Collaborator
There was a problem hiding this comment.
However, usize can be u32 on 32-bit platforms.
|
|
||
| impl<PARAMS: SHA2Params> SerializableState<108> for SHA256Internal<PARAMS> { | ||
| fn serialize_state(&self) -> [u8; 108] { | ||
| let mut out_to_return = [0u8; 108]; |
Collaborator
There was a problem hiding this comment.
Where 108 and other sizes coming from?
| [dependencies] | ||
| bouncycastle-core.workspace = true | ||
| bouncycastle-utils.workspace = true | ||
| serde = { version = "1.0.228", features = ["derive"] } |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This was a feature requested by Simo to be able to pause an operation -- particularly in the middle of a
do_update()...do_update()...do_final()flow -- stick the serialized crypto operation into a cache, and then resume it later. This is an important feature for implementing TLS loadbalancers that want to be able to cache out the HKDF state and resume it from a different box when the next flight of TCP messages come in.The only "creative" thing I've added here is that all serialized states are prefixed with a 3-byte semantic version of the library and it's checked on load back in. The idea is that if we ever need to change the serialization of some object, then we can add a guard to refuse to load a serialized object from before the guard (or keep both versions of the deserializer and switch based on the version tag, or whatever ... #futureProofing).