1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Bitcoin secp256k1 bindings
// Written in 2014 by
//   Dawid Ciężarkiewicz
//   Andrew Poelstra
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
// along with this software.
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
//

//! # FFI of the recovery module

use ::types::*;
#[cfg(not(feature = "fuzztarget"))]
use ::{Context, Signature, NonceFn, PublicKey};

/// Library-internal representation of a Secp256k1 signature + recovery ID
#[repr(C)]
pub struct RecoverableSignature([c_uchar; 65]);
impl_array_newtype!(RecoverableSignature, c_uchar, 65);
impl_raw_debug!(RecoverableSignature);

impl RecoverableSignature {
    /// Create a new (zeroed) signature usable for the FFI interface
    pub fn new() -> RecoverableSignature { RecoverableSignature([0; 65]) }
    /// Create a new (uninitialized) signature usable for the FFI interface
    #[deprecated(since = "0.15.3", note = "Please use the new function instead")]
    pub unsafe fn blank() -> RecoverableSignature { RecoverableSignature::new() }
}

impl Default for RecoverableSignature {
    fn default() -> Self {
        RecoverableSignature::new()
    }
}

#[cfg(not(feature = "fuzztarget"))]
extern "C" {
    #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_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;

    #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_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;

    #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_ecdsa_recoverable_signature_convert")]
    pub fn secp256k1_ecdsa_recoverable_signature_convert(cx: *const Context, sig: *mut Signature,
                                                         input: *const RecoverableSignature)
                                                         -> c_int;
    #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_ecdsa_sign_recoverable")]
    pub fn secp256k1_ecdsa_sign_recoverable(cx: *const Context,
                                            sig: *mut RecoverableSignature,
                                            msg32: *const c_uchar,
                                            sk: *const c_uchar,
                                            noncefn: NonceFn,
                                            noncedata: *const c_void)
                                            -> c_int;

    #[cfg_attr(not(feature = "external-symbols"), link_name = "rustsecp256k1_v0_1_1_ecdsa_recover")]
    pub fn secp256k1_ecdsa_recover(cx: *const Context,
                                   pk: *mut PublicKey,
                                   sig: *const RecoverableSignature,
                                   msg32: *const c_uchar)
                                   -> c_int;
}


#[cfg(feature = "fuzztarget")]
mod fuzz_dummy {
    extern crate std;
    use self::std::ptr;
    use super::RecoverableSignature;
    use types::*;
    use ::{Signature, Context, PublicKey, NonceFn, secp256k1_ec_seckey_verify,
        SECP256K1_START_NONE, SECP256K1_START_VERIFY, SECP256K1_START_SIGN};

    pub unsafe fn secp256k1_ecdsa_recoverable_signature_parse_compact(_cx: *const Context, _sig: *mut RecoverableSignature,
                                                                      _input64: *const c_uchar, _recid: c_int)
                                                                      -> c_int {
        unimplemented!();
    }

    pub unsafe fn secp256k1_ecdsa_recoverable_signature_serialize_compact(_cx: *const Context, _output64: *mut c_uchar,
                                                                          _recid: *mut c_int, _sig: *const RecoverableSignature)
                                                                          -> c_int {
        unimplemented!();
    }

    pub unsafe fn secp256k1_ecdsa_recoverable_signature_convert(_cx: *const Context, _sig: *mut Signature,
                                                                _input: *const RecoverableSignature)
                                                                -> c_int {
        unimplemented!();
    }

    /// Sets sig to (2|3)||msg32||sk
    pub unsafe fn secp256k1_ecdsa_sign_recoverable(cx: *const Context,
                                                   sig: *mut RecoverableSignature,
                                                   msg32: *const c_uchar,
                                                   sk: *const c_uchar,
                                                   _noncefn: NonceFn,
                                                   _noncedata: *const c_void)
                                                   -> c_int {
        assert!(!cx.is_null() && (*cx).flags() & !(SECP256K1_START_NONE | SECP256K1_START_VERIFY | SECP256K1_START_SIGN) == 0);
        assert!((*cx).flags() & SECP256K1_START_SIGN == SECP256K1_START_SIGN);
        if secp256k1_ec_seckey_verify(cx, sk) != 1 { return 0; }
        if *sk.offset(0) > 0x7f {
            (*sig).0[0] = 2;
        } else {
            (*sig).0[0] = 3;
        }
        ptr::copy(msg32, (*sig).0[1..33].as_mut_ptr(), 32);
        ptr::copy(sk, (*sig).0[33..65].as_mut_ptr(), 32);
        1
    }

    pub unsafe fn secp256k1_ecdsa_recover(_cx: *const Context,
                                          _pk: *mut PublicKey,
                                          _sig: *const RecoverableSignature,
                                          _msg32: *const c_uchar)
                                          -> c_int {
        unimplemented!();
    }
}
#[cfg(feature = "fuzztarget")]
pub use self::fuzz_dummy::*;