Repository navigation
Add a compile-time option to use the new, very fast sha1dc Rust crate
#2240
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
Open
dscho
wants to merge
4
commits into
gitgitgadget:main
Choose a base branch
from
dscho:optionally-use-sha1dc-rs
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.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b1f30a6
libgitcore: add `sha1dc` as an optional feature
dscho 5a414a4
sha1dc: allow selecting the C backend without rebuilding
dscho 2f3077b
pthread: provide `pthread_once()` shims for Windows and for NO_PTHREADS
dscho aac6a83
sha1dc: make `sha1dc_init()` thread-safe
dscho 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 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
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #ifndef SHA1DC_RS_H | ||
| #define SHA1DC_RS_H | ||
|
|
||
| #define platform_SHA_CTX union sha1dc_ctx | ||
| #include "sha1dc_git.h" | ||
|
|
||
| typedef struct sha1dc_rs_hasher *sha1dc_rs_ctx; | ||
|
|
||
| union sha1dc_ctx { | ||
| SHA1_CTX c; | ||
| sha1dc_rs_ctx rs; | ||
| }; | ||
|
|
||
| void sha1dc_rs_init(SHA1_CTX *); | ||
| void sha1dc_rs_clone(SHA1_CTX *, const SHA1_CTX *); | ||
| void sha1dc_rs_update(SHA1_CTX *, const void *, size_t); | ||
| void sha1dc_rs_final(unsigned char [20], SHA1_CTX *, | ||
| void (*die_fn)(const char *, ...)); | ||
| void sha1dc_rs_discard(SHA1_CTX *); | ||
|
|
||
| extern void (*sha1dc_init)(SHA1_CTX *); | ||
| extern void (*sha1dc_clone)(SHA1_CTX *, const SHA1_CTX *); | ||
| extern void (*sha1dc_update)(SHA1_CTX *, const void *, size_t); | ||
| extern void (*sha1dc_final)(unsigned char [20], SHA1_CTX *, | ||
| void (*die_fn)(const char *, ...)); | ||
| extern void (*sha1dc_discard)(SHA1_CTX *); | ||
|
|
||
| #define platform_SHA1_Init(ctx) sha1dc_init(&(ctx)->c) | ||
| #define platform_SHA1_Update(ctx, data, len) \ | ||
| sha1dc_update(&(ctx)->c, (data), (len)) | ||
| #define platform_SHA1_Final(hash, ctx) \ | ||
| sha1dc_final((hash), &(ctx)->c, die) | ||
| #define SHA1_NEEDS_CLONE_HELPER | ||
| #define platform_SHA1_Clone(dst, src) sha1dc_clone(&(dst)->c, &(src)->c) | ||
| #define platform_SHA1_Discard(ctx) sha1dc_discard(&(ctx)->c) | ||
|
|
||
| #endif |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| pub mod csum_file; | ||
| pub mod hash; | ||
| pub mod loose; | ||
| #[cfg(feature = "sha1dc-rs")] | ||
| mod sha1dc_rs; | ||
| pub mod varint; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| use sha1dc::Hasher; | ||
| use std::ffi::{c_void, CString}; | ||
| use std::os::raw::c_char; | ||
| use std::{ptr, slice}; | ||
|
|
||
| /// Initialize a collision-detecting SHA-1 context. | ||
| /// | ||
| /// # Safety | ||
| /// `ctx` must point to an uninitialized SHA-1 context. | ||
| #[no_mangle] | ||
| pub unsafe extern "C" fn sha1dc_rs_init(ctx: *mut c_void) { | ||
| let ctx = ctx.cast::<*mut Hasher>(); | ||
| *ctx = Box::into_raw(Box::new(Hasher::new())); | ||
| } | ||
|
|
||
| /// Replace a SHA-1 context with a clone of another. | ||
| /// | ||
| /// # Safety | ||
| /// Both contexts must be initialized. | ||
| #[no_mangle] | ||
| pub unsafe extern "C" fn sha1dc_rs_clone(dst: *mut c_void, src: *const c_void) { | ||
| let dst = dst.cast::<*mut Hasher>(); | ||
| let src = src.cast::<*mut Hasher>(); | ||
| let hasher = Box::new((**src).clone()); | ||
| drop(Box::from_raw(*dst)); | ||
| *dst = Box::into_raw(hasher); | ||
| } | ||
|
|
||
| /// Update the SHA-1 hasher with the given bytes. | ||
| /// | ||
| /// # Safety | ||
| /// `ctx` must be initialized and `data` must point to `len` bytes unless | ||
| /// `len` is zero. | ||
| #[no_mangle] | ||
| pub unsafe extern "C" fn sha1dc_rs_update(ctx: *mut c_void, data: *const c_void, len: usize) { | ||
| let ctx = ctx.cast::<*mut Hasher>(); | ||
| if len != 0 { | ||
| (**ctx).update(slice::from_raw_parts(data.cast::<u8>(), len)); | ||
| } | ||
| } | ||
|
|
||
| /// Finalize SHA-1, reporting detected collisions through `die`. | ||
| /// | ||
| /// # Safety | ||
| /// `ctx` must be initialized, `hash` must point to at least 20 bytes, and | ||
| /// `die` must be a non-returning C variadic function. | ||
| #[no_mangle] | ||
| pub unsafe extern "C" fn sha1dc_rs_final( | ||
| hash: *mut u8, | ||
| ctx: *mut c_void, | ||
| die: unsafe extern "C" fn(*const c_char, ...) -> !, | ||
| ) { | ||
| let ctx = ctx.cast::<*mut Hasher>(); | ||
| let hasher = *Box::from_raw(*ctx); | ||
| *ctx = ptr::null_mut(); | ||
| match hasher.finalize() { | ||
| Ok(digest) => ptr::copy_nonoverlapping(digest.as_bytes().as_ptr(), hash, 20), | ||
| Err(collision) => { | ||
| let message = CString::new(format!( | ||
| "SHA-1 appears to be part of a collision attack: {}", | ||
| collision.digest() | ||
| )) | ||
| .expect("collision message contains no NUL"); | ||
| die(message.as_ptr()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Discard a SHA-1 context without producing a digest. | ||
| /// | ||
| /// # Safety | ||
| /// `ctx` must be initialized. | ||
| #[no_mangle] | ||
| pub unsafe extern "C" fn sha1dc_rs_discard(ctx: *mut c_void) { | ||
| let ctx = ctx.cast::<*mut Hasher>(); | ||
| drop(Box::from_raw(*ctx)); | ||
| *ctx = ptr::null_mut(); | ||
| } |
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.
"brian m. carlson" wrote on the Git mailing list (how to reply to this email):