diff --git a/Cargo.toml b/Cargo.toml index 9530a36..fa15606 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -30,8 +30,10 @@ recovery = ["secp256k1-sys/recovery"] endomorphism = ["secp256k1-sys/endomorphism"] lowmemory = ["secp256k1-sys/lowmemory"] -# Do not use this feature! HAZMAT. (meant for Bitcoin Core only) -dont_replace_c_symbols = ["secp256k1-sys/dont_replace_c_symbols"] +# Use this feature to not compile the bundled libsecp256k1 C symbols, +# but use external ones. Use this only if you know what you are doing! +external-symbols = ["secp256k1-sys/external-symbols"] + # Do not use this feature! HAZMAT. (meant for Fuzzing only. this is *BROKEN CRYPTOGRAPHY*) fuzztarget = ["secp256k1-sys/fuzztarget"] diff --git a/secp256k1-sys/Cargo.toml b/secp256k1-sys/Cargo.toml index ff84686..298e499 100644 --- a/secp256k1-sys/Cargo.toml +++ b/secp256k1-sys/Cargo.toml @@ -32,7 +32,9 @@ endomorphism = [] lowmemory = [] std = [] -# Do not use this feature! HAZMAT. (meant for Bitcoin Core only) -dont_replace_c_symbols = [] +# Use this feature to not compile the bundled libsecp256k1 C symbols, +# but use external ones. Use this only if you know what you are doing! +external-symbols = [] + # Do not use this feature! HAZMAT. (meant for Fuzzing only. this is *BROKEN CRYPTOGRAPHY*) fuzztarget = [] diff --git a/secp256k1-sys/build.rs b/secp256k1-sys/build.rs index 85e5502..0fb1173 100644 --- a/secp256k1-sys/build.rs +++ b/secp256k1-sys/build.rs @@ -26,6 +26,11 @@ extern crate cc; use std::env; fn main() { + if cfg!(feature = "external-symbols") { + println!("cargo:rustc-link-lib=static=secp256k1"); + return; + } + // Check whether we can use 64-bit compilation let use_64bit_compilation = if env::var("CARGO_CFG_TARGET_POINTER_WIDTH").unwrap() == "64" { let check = cc::Build::new().file("depend/check_uint128_t.c") diff --git a/secp256k1-sys/src/lib.rs b/secp256k1-sys/src/lib.rs index f51864b..3d189f5 100644 --- a/secp256k1-sys/src/lib.rs +++ b/secp256k1-sys/src/lib.rs @@ -157,91 +157,91 @@ impl Default for SharedSecret { #[cfg(not(feature = "fuzztarget"))] extern "C" { /// Default ECDH hash function - #[link_name = "rustsecp256k1_v0_1_0_ecdh_hash_function_default"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdh_hash_function_default")] pub static secp256k1_ecdh_hash_function_default: EcdhHashFn; - #[link_name = "rustsecp256k1_v0_1_0_nonce_function_rfc6979"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_nonce_function_rfc6979")] pub static secp256k1_nonce_function_rfc6979: NonceFn; - #[link_name = "rustsecp256k1_v0_1_0_nonce_function_default"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_nonce_function_default")] pub static secp256k1_nonce_function_default: NonceFn; - #[link_name = "rustsecp256k1_v0_1_0_context_no_precomp"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_context_no_precomp")] pub static secp256k1_context_no_precomp: *const Context; // Contexts - #[link_name = "rustsecp256k1_v0_1_0_context_preallocated_size"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_context_preallocated_size")] pub fn secp256k1_context_preallocated_size(flags: c_uint) -> usize; - #[link_name = "rustsecp256k1_v0_1_0_context_preallocated_create"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_context_preallocated_create")] pub fn secp256k1_context_preallocated_create(prealloc: *mut c_void, flags: c_uint) -> *mut Context; - #[link_name = "rustsecp256k1_v0_1_0_context_preallocated_destroy"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_context_preallocated_destroy")] pub fn secp256k1_context_preallocated_destroy(cx: *mut Context); - #[link_name = "rustsecp256k1_v0_1_0_context_preallocated_clone_size"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_context_preallocated_clone_size")] pub fn secp256k1_context_preallocated_clone_size(cx: *const Context) -> usize; - #[link_name = "rustsecp256k1_v0_1_0_context_preallocated_clone"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_context_preallocated_clone")] pub fn secp256k1_context_preallocated_clone(cx: *const Context, prealloc: *mut c_void) -> *mut Context; - #[link_name = "rustsecp256k1_v0_1_0_context_randomize"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_context_randomize")] pub fn secp256k1_context_randomize(cx: *mut Context, seed32: *const c_uchar) -> c_int; // Pubkeys - #[link_name = "rustsecp256k1_v0_1_0_ec_pubkey_parse"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ec_pubkey_parse")] pub fn secp256k1_ec_pubkey_parse(cx: *const Context, pk: *mut PublicKey, input: *const c_uchar, in_len: usize) -> c_int; - #[link_name = "rustsecp256k1_v0_1_0_ec_pubkey_serialize"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ec_pubkey_serialize")] pub fn secp256k1_ec_pubkey_serialize(cx: *const Context, output: *mut c_uchar, out_len: *mut usize, pk: *const PublicKey, compressed: c_uint) -> c_int; // Signatures - #[link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_parse_der"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_parse_der")] pub fn secp256k1_ecdsa_signature_parse_der(cx: *const Context, sig: *mut Signature, input: *const c_uchar, in_len: usize) -> c_int; - #[link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_parse_compact"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_parse_compact")] pub fn secp256k1_ecdsa_signature_parse_compact(cx: *const Context, sig: *mut Signature, input64: *const c_uchar) -> c_int; - #[link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_parse_der_lax"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_parse_der_lax")] pub fn ecdsa_signature_parse_der_lax(cx: *const Context, sig: *mut Signature, input: *const c_uchar, in_len: usize) -> c_int; - #[link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_serialize_der"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_serialize_der")] pub fn secp256k1_ecdsa_signature_serialize_der(cx: *const Context, output: *mut c_uchar, out_len: *mut usize, sig: *const Signature) -> c_int; - #[link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_serialize_compact"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_serialize_compact")] pub fn secp256k1_ecdsa_signature_serialize_compact(cx: *const Context, output64: *mut c_uchar, sig: *const Signature) -> c_int; - #[link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_normalize"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_signature_normalize")] pub fn secp256k1_ecdsa_signature_normalize(cx: *const Context, out_sig: *mut Signature, in_sig: *const Signature) -> c_int; // ECDSA - #[link_name = "rustsecp256k1_v0_1_0_ecdsa_verify"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_verify")] pub fn secp256k1_ecdsa_verify(cx: *const Context, sig: *const Signature, msg32: *const c_uchar, pk: *const PublicKey) -> c_int; - #[link_name = "rustsecp256k1_v0_1_0_ecdsa_sign"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_sign")] pub fn secp256k1_ecdsa_sign(cx: *const Context, sig: *mut Signature, msg32: *const c_uchar, @@ -251,49 +251,49 @@ extern "C" { -> c_int; // EC - #[link_name = "rustsecp256k1_v0_1_0_ec_seckey_verify"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ec_seckey_verify")] pub fn secp256k1_ec_seckey_verify(cx: *const Context, sk: *const c_uchar) -> c_int; - #[link_name = "rustsecp256k1_v0_1_0_ec_pubkey_create"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ec_pubkey_create")] pub fn secp256k1_ec_pubkey_create(cx: *const Context, pk: *mut PublicKey, sk: *const c_uchar) -> c_int; //TODO secp256k1_ec_privkey_export //TODO secp256k1_ec_privkey_import - #[link_name = "rustsecp256k1_v0_1_0_ec_privkey_tweak_add"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ec_privkey_tweak_add")] pub fn secp256k1_ec_privkey_tweak_add(cx: *const Context, sk: *mut c_uchar, tweak: *const c_uchar) -> c_int; - #[link_name = "rustsecp256k1_v0_1_0_ec_pubkey_tweak_add"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ec_pubkey_tweak_add")] pub fn secp256k1_ec_pubkey_tweak_add(cx: *const Context, pk: *mut PublicKey, tweak: *const c_uchar) -> c_int; - #[link_name = "rustsecp256k1_v0_1_0_ec_privkey_tweak_mul"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ec_privkey_tweak_mul")] pub fn secp256k1_ec_privkey_tweak_mul(cx: *const Context, sk: *mut c_uchar, tweak: *const c_uchar) -> c_int; - #[link_name = "rustsecp256k1_v0_1_0_ec_pubkey_tweak_mul"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ec_pubkey_tweak_mul")] pub fn secp256k1_ec_pubkey_tweak_mul(cx: *const Context, pk: *mut PublicKey, tweak: *const c_uchar) -> c_int; - #[link_name = "rustsecp256k1_v0_1_0_ec_pubkey_combine"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ec_pubkey_combine")] pub fn secp256k1_ec_pubkey_combine(cx: *const Context, out: *mut PublicKey, ins: *const *const PublicKey, n: c_int) -> c_int; - #[link_name = "rustsecp256k1_v0_1_0_ecdh"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdh")] pub fn secp256k1_ecdh( cx: *const Context, output: *mut SharedSecret, @@ -305,8 +305,6 @@ extern "C" { } -#[cfg(all(feature = "std", not(feature = "dont_replace_c_symbols")))] -#[no_mangle] /// A reimplementation of the C function `secp256k1_context_create` in rust. /// /// This function allocates memory, the pointer should be deallocated using `secp256k1_context_destroy` @@ -315,6 +313,8 @@ extern "C" { /// This will create a secp256k1 raw context. // Returns: a newly created context object. // In: flags: which parts of the context to initialize. +#[no_mangle] +#[cfg(all(feature = "std", not(feature = "external_symbols")))] pub unsafe extern "C" fn rustsecp256k1_v0_1_0_context_create(flags: c_uint) -> *mut Context { use std::mem; assert!(mem::align_of::() >= mem::align_of::()); @@ -331,19 +331,19 @@ pub unsafe extern "C" fn rustsecp256k1_v0_1_0_context_create(flags: c_uint) -> * secp256k1_context_preallocated_create(ptr as *mut c_void, flags) } -#[cfg(all(feature = "std", not(feature = "dont_replace_c_symbols")))] +#[cfg(all(feature = "std", not(feature = "external_symbols")))] pub unsafe fn secp256k1_context_create(flags: c_uint) -> *mut Context { rustsecp256k1_v0_1_0_context_create(flags) } -#[cfg(all(feature = "std", not(feature = "dont_replace_c_symbols")))] -#[no_mangle] /// A reimplementation of the C function `secp256k1_context_destroy` in rust. /// /// This function destroys and deallcates the context created by `secp256k1_context_create`. /// /// The pointer shouldn't be used after passing to this function, consider it as passing it to `free()`. /// +#[no_mangle] +#[cfg(all(feature = "std", not(feature = "external_symbols")))] pub unsafe extern "C" fn rustsecp256k1_v0_1_0_context_destroy(ctx: *mut Context) { secp256k1_context_preallocated_destroy(ctx); let ctx: *mut usize = ctx as *mut usize; @@ -354,14 +354,12 @@ pub unsafe extern "C" fn rustsecp256k1_v0_1_0_context_destroy(ctx: *mut Context) let _ = Box::from_raw(slice as *mut [usize]); } -#[cfg(all(feature = "std", not(feature = "dont_replace_c_symbols")))] +#[cfg(all(feature = "std", not(feature = "external_symbols")))] pub unsafe fn secp256k1_context_destroy(ctx: *mut Context) { rustsecp256k1_v0_1_0_context_destroy(ctx) } -#[cfg(not(feature = "dont_replace_c_symbols"))] -#[no_mangle] /// **This function is an override for the C function, this is the an edited version of the original description:** /// /// A callback function to be called when an illegal argument is passed to @@ -380,6 +378,8 @@ pub unsafe fn secp256k1_context_destroy(ctx: *mut Context) { /// /// See also secp256k1_default_error_callback_fn. /// +#[no_mangle] +#[cfg(not(feature = "external_symbols"))] pub unsafe extern "C" fn rustsecp256k1_v0_1_0_default_illegal_callback_fn(message: *const c_char, _data: *mut c_void) { use core::str; let msg_slice = slice::from_raw_parts(message as *const u8, strlen(message)); @@ -387,8 +387,6 @@ pub unsafe extern "C" fn rustsecp256k1_v0_1_0_default_illegal_callback_fn(messag panic!("[libsecp256k1] illegal argument. {}", msg); } -#[cfg(not(feature = "dont_replace_c_symbols"))] -#[no_mangle] /// **This function is an override for the C function, this is the an edited version of the original description:** /// /// A callback function to be called when an internal consistency check @@ -403,6 +401,8 @@ pub unsafe extern "C" fn rustsecp256k1_v0_1_0_default_illegal_callback_fn(messag /// /// See also secp256k1_default_illegal_callback_fn. /// +#[no_mangle] +#[cfg(not(feature = "external_symbols"))] pub unsafe extern "C" fn rustsecp256k1_v0_1_0_default_error_callback_fn(message: *const c_char, _data: *mut c_void) { use core::str; let msg_slice = slice::from_raw_parts(message as *const u8, strlen(message)); diff --git a/secp256k1-sys/src/recovery.rs b/secp256k1-sys/src/recovery.rs index 8e65bd3..0797ad4 100644 --- a/secp256k1-sys/src/recovery.rs +++ b/secp256k1-sys/src/recovery.rs @@ -41,21 +41,21 @@ impl Default for RecoverableSignature { #[cfg(not(feature = "fuzztarget"))] extern "C" { - #[link_name = "rustsecp256k1_v0_1_0_ecdsa_recoverable_signature_parse_compact"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_recoverable_signature_parse_compact")] pub fn secp256k1_ecdsa_recoverable_signature_parse_compact(cx: *const Context, sig: *mut RecoverableSignature, input64: *const c_uchar, recid: c_int) -> c_int; - #[link_name = "rustsecp256k1_v0_1_0_ecdsa_recoverable_signature_serialize_compact"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_recoverable_signature_serialize_compact")] pub fn secp256k1_ecdsa_recoverable_signature_serialize_compact(cx: *const Context, output64: *mut c_uchar, recid: *mut c_int, sig: *const RecoverableSignature) -> c_int; - #[link_name = "rustsecp256k1_v0_1_0_ecdsa_recoverable_signature_convert"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_recoverable_signature_convert")] pub fn secp256k1_ecdsa_recoverable_signature_convert(cx: *const Context, sig: *mut Signature, input: *const RecoverableSignature) -> c_int; - #[link_name = "rustsecp256k1_v0_1_0_ecdsa_sign_recoverable"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_sign_recoverable")] pub fn secp256k1_ecdsa_sign_recoverable(cx: *const Context, sig: *mut RecoverableSignature, msg32: *const c_uchar, @@ -64,7 +64,7 @@ extern "C" { noncedata: *const c_void) -> c_int; - #[link_name = "rustsecp256k1_v0_1_0_ecdsa_recover"] + #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_0_ecdsa_recover")] pub fn secp256k1_ecdsa_recover(cx: *const Context, pk: *mut PublicKey, sig: *const RecoverableSignature,