-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #617 from KeystoneHQ/feat/update_bin_check
Feat/update bin check
- Loading branch information
Showing
33 changed files
with
1,539 additions
and
1,053 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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,14 @@ | ||
[package] | ||
name = "signature_rust_c" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
secp256k1={ version = "0.27.0", default_features = false, features = ["alloc", "lowmemory"]} | ||
thiserror = { version = "1.0", package = "thiserror-core", default-features = false } | ||
hex = { version = "0.4.3", default-features = false, features = ["alloc"] } | ||
cty = "0.2.2" | ||
cstr_core = "0.2.6" | ||
common_rust_c = {path = "../common"} |
This file contains 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,10 @@ | ||
use alloc::string::String; | ||
use thiserror::Error; | ||
|
||
pub type Result<T> = core::result::Result<T, RustCError>; | ||
|
||
#[derive(Error, Debug, PartialEq)] | ||
pub enum RustCError { | ||
#[error("FormatTypeError, type is {0}")] | ||
FormatTypeError(String), | ||
} |
This file contains 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,26 @@ | ||
#![no_std] | ||
#![feature(error_in_core)] | ||
#![allow(unused_unsafe)] | ||
extern crate alloc; | ||
mod errors; | ||
mod signature; | ||
|
||
use common_rust_c::utils::recover_c_char; | ||
use core::slice; | ||
use cty::c_char; | ||
use signature::verify_signature; | ||
|
||
#[no_mangle] | ||
pub extern "C" fn verify_frimware_signature( | ||
signature_ptr: *mut c_char, | ||
message_hash_ptr: *mut u8, | ||
pubkey_ptr: *mut u8, | ||
) -> bool { | ||
let signature = recover_c_char(signature_ptr); | ||
let message_hash = unsafe { slice::from_raw_parts(message_hash_ptr, 32) }; | ||
let publick_key = unsafe { slice::from_raw_parts(pubkey_ptr, 65) }; | ||
match hex::decode(signature) { | ||
Ok(data) => verify_signature(&data, message_hash, publick_key).unwrap_or(false), | ||
Err(_) => false, | ||
} | ||
} |
This file contains 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,49 @@ | ||
use crate::errors::{Result, RustCError}; | ||
use alloc::string::ToString; | ||
use secp256k1::{ecdsa, Message, PublicKey, Secp256k1, SecretKey}; | ||
|
||
pub fn verify_signature(signature: &[u8], message_hash: &[u8], pubkey: &[u8]) -> Result<bool> { | ||
let secp = Secp256k1::verification_only(); | ||
let public_key = | ||
PublicKey::from_slice(pubkey).map_err(|e| RustCError::FormatTypeError(e.to_string()))?; | ||
let message = Message::from_slice(message_hash) | ||
.map_err(|e| RustCError::FormatTypeError(e.to_string()))?; | ||
let mut sig = ecdsa::Signature::from_compact(signature) | ||
.map_err(|e| RustCError::FormatTypeError(e.to_string()))?; | ||
sig.normalize_s(); | ||
let result = secp.verify_ecdsa(&message, &sig, &public_key).is_ok(); | ||
Ok(result) | ||
} | ||
|
||
pub fn sign_message_by_key(message_hash: &[u8], private_key: &[u8]) -> Result<[u8; 64]> { | ||
let secp = Secp256k1::signing_only(); | ||
let message = Message::from_slice(message_hash) | ||
.map_err(|e| RustCError::FormatTypeError(e.to_string()))?; | ||
|
||
let private_key = SecretKey::from_slice(private_key) | ||
.map_err(|e| RustCError::FormatTypeError(e.to_string()))?; | ||
|
||
let sig = secp.sign_ecdsa(&message, &private_key); | ||
Ok(sig.serialize_compact()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn it_should_sign_and_verify_the_signature() { | ||
let test_key = "f254b030d04cdd902e9219d8390e1deb5a585f3c25bacf5c74bd07803a8dd873"; | ||
let private_key = hex::decode(test_key).unwrap(); | ||
let message_hash = | ||
hex::decode("0D94D045A7E0D4547E161AC360C73581A95383435A48D8869AB08FF34A8DB5E7") | ||
.unwrap(); | ||
|
||
let sig = hex::decode("168c267d21968b1447a676276d7ee7055810d58ac5524457361a09647bf19d2b108dd831a9d590019c93151d700e1c20eaf95fef24c60e645c04178227880e94").unwrap(); | ||
let pubkey_bytes = hex::decode(PUBKEY_STRING).unwrap(); | ||
// test pubkey | ||
const PUBKEY_STRING: &str = "04e3003fa1467452743ed7b97cc8c0786f3b9c255d31ccca9e6dc59915b17fa8ed5933cf74ce8ec3614a503422f0e77b495a07567e29256858d6282f63c6dbfebd"; | ||
let result = verify_signature(&sig, &message_hash, &pubkey_bytes).unwrap(); | ||
assert_eq!(result, true); | ||
} | ||
} |
This file contains 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
Oops, something went wrong.