@@ -656,10 +656,19 @@ impl ReadyToRun {
656656 expect_pointer_constant_size ! ( 2 , 32 ) ,
657657 ) ;
658658 if let Ok ( public_key) = public_key {
659- let signature =
660- ed25519_zebra:: Signature :: from ( expect_pointer_constant_size ! ( 0 , 64 ) ) ;
659+ let signature = expect_pointer_constant_size ! ( 0 , 64 ) ;
661660 let message = expect_pointer_size ! ( 1 ) ;
662- public_key. verify ( & signature, message. as_ref ( ) ) . is_ok ( )
661+
662+ if self . inner . mock_signature_verification_host_functions {
663+ is_magic_signature ( signature. as_ref ( ) )
664+ } else {
665+ public_key
666+ . verify (
667+ & ed25519_zebra:: Signature :: from ( signature) ,
668+ message. as_ref ( ) ,
669+ )
670+ . is_ok ( )
671+ }
663672 } else {
664673 false
665674 }
@@ -684,13 +693,17 @@ impl ReadyToRun {
684693 let signature = expect_pointer_constant_size ! ( 0 , 64 ) ;
685694 let message = expect_pointer_size ! ( 1 ) ;
686695
687- signing_public_key
688- . verify_simple_preaudit_deprecated (
689- b"substrate" ,
690- message. as_ref ( ) ,
691- & signature,
692- )
693- . is_ok ( )
696+ if self . inner . mock_signature_verification_host_functions {
697+ is_magic_signature ( signature. as_ref ( ) )
698+ } else {
699+ signing_public_key
700+ . verify_simple_preaudit_deprecated (
701+ b"substrate" ,
702+ message. as_ref ( ) ,
703+ & signature,
704+ )
705+ . is_ok ( )
706+ }
694707 } ;
695708
696709 HostVm :: ReadyToRun ( ReadyToRun {
@@ -705,13 +718,21 @@ impl ReadyToRun {
705718 let signing_public_key =
706719 schnorrkel:: PublicKey :: from_bytes ( & expect_pointer_constant_size ! ( 2 , 32 ) )
707720 . unwrap ( ) ;
708- let signature =
709- schnorrkel:: Signature :: from_bytes ( & expect_pointer_constant_size ! ( 0 , 64 ) )
710- . unwrap ( ) ;
721+ let signature = expect_pointer_constant_size ! ( 0 , 64 ) ;
711722
712- signing_public_key
713- . verify_simple ( b"substrate" , expect_pointer_size ! ( 1 ) . as_ref ( ) , & signature)
714- . is_ok ( )
723+ if self . inner . mock_signature_verification_host_functions {
724+ is_magic_signature ( signature. as_ref ( ) )
725+ } else {
726+ let signature = schnorrkel:: Signature :: from_bytes ( & signature) . unwrap ( ) ;
727+
728+ signing_public_key
729+ . verify_simple (
730+ b"substrate" ,
731+ expect_pointer_size ! ( 1 ) . as_ref ( ) ,
732+ & signature,
733+ )
734+ . is_ok ( )
735+ }
715736 } ;
716737
717738 HostVm :: ReadyToRun ( ReadyToRun {
@@ -759,20 +780,25 @@ impl ReadyToRun {
759780
760781 // signature (64 bytes) + recovery ID (1 byte)
761782 let sig_bytes = expect_pointer_constant_size ! ( 0 , 65 ) ;
762- if let Ok ( sig) = libsecp256k1:: Signature :: parse_standard_slice ( & sig_bytes[ ..64 ] )
763- {
764- if let Ok ( ri) = libsecp256k1:: RecoveryId :: parse ( sig_bytes[ 64 ] ) {
765- if let Ok ( actual) = libsecp256k1:: recover ( & message, & sig, & ri) {
766- expect_pointer_constant_size ! ( 2 , 33 ) [ ..]
767- == actual. serialize_compressed ( ) [ ..]
783+ if self . inner . mock_signature_verification_host_functions {
784+ is_magic_signature ( sig_bytes. as_ref ( ) )
785+ } else {
786+ if let Ok ( sig) =
787+ libsecp256k1:: Signature :: parse_standard_slice ( & sig_bytes[ ..64 ] )
788+ {
789+ if let Ok ( ri) = libsecp256k1:: RecoveryId :: parse ( sig_bytes[ 64 ] ) {
790+ if let Ok ( actual) = libsecp256k1:: recover ( & message, & sig, & ri) {
791+ expect_pointer_constant_size ! ( 2 , 33 ) [ ..]
792+ == actual. serialize_compressed ( ) [ ..]
793+ } else {
794+ false
795+ }
768796 } else {
769797 false
770798 }
771799 } else {
772800 false
773801 }
774- } else {
775- false
776802 }
777803 } ;
778804
@@ -2113,6 +2139,8 @@ pub struct Inner {
21132139
21142140 /// Memory allocator in order to answer the calls to `malloc` and `free`.
21152141 pub ( crate ) allocator : allocator:: FreeingBumpHeapAllocator ,
2142+
2143+ pub ( crate ) mock_signature_verification_host_functions : bool ,
21162144}
21172145
21182146impl Inner {
@@ -2278,6 +2306,8 @@ impl Inner {
22782306 heap_pages : self . heap_pages ,
22792307 allow_unresolved_imports : self . allow_unresolved_imports ,
22802308 memory_total_pages : self . memory_total_pages ,
2309+ mock_signature_verification_host_functions : self
2310+ . mock_signature_verification_host_functions ,
22812311 }
22822312 }
22832313}
@@ -2551,13 +2581,31 @@ impl<'a> allocator::Memory for MemAccess<'a> {
25512581 }
25522582}
25532583
2584+ fn is_magic_signature ( signature : & [ u8 ] ) -> bool {
2585+ signature. starts_with ( & [ 0xde , 0xad , 0xbe , 0xef ] ) && signature[ 4 ..] . iter ( ) . all ( |& b| b == 0xcd )
2586+ }
2587+
25542588#[ cfg( test) ]
25552589mod tests {
2556- use super :: HostVm ;
2590+ use super :: { is_magic_signature , HostVm } ;
25572591
25582592 #[ test]
25592593 fn is_send ( ) {
25602594 fn req < T : Send > ( ) { }
25612595 req :: < HostVm > ( ) ;
25622596 }
2597+
2598+ #[ test]
2599+ fn is_magic_signature_works ( ) {
2600+ assert ! ( is_magic_signature( & [ 0xde , 0xad , 0xbe , 0xef , 0xcd , 0xcd ] ) ) ;
2601+ assert ! ( is_magic_signature( & [
2602+ 0xde , 0xad , 0xbe , 0xef , 0xcd , 0xcd , 0xcd , 0xcd
2603+ ] ) ) ;
2604+ assert ! ( !is_magic_signature( & [
2605+ 0xde , 0xad , 0xbe , 0xef , 0xcd , 0xcd , 0xcd , 0x00
2606+ ] ) ) ;
2607+ assert ! ( !is_magic_signature( & [
2608+ 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00 , 0x00
2609+ ] ) ) ;
2610+ }
25632611}
0 commit comments