function verify($signatureB64, $data, $publicKeyRaw) { if ($publicKeyRaw == null) { global $thisEntity; // Load the public key if needed (from a hex string): if (!isset($thisEntity['PublicKeyBytes'])) { // Load it now: $thisEntity['PublicKeyBytes'] = hex2bin($thisEntity['Key']); } // Grab the raw public key (bytes): $publicKeyRaw = $thisEntity['PublicKeyBytes']; } // Decode the signature from base64: $decodedSignature = base64_decode($signatureB64, true); if ($decodedSignature === false) { // Invalid signature. error('field/invalid', 'signature'); } // Get the double hash of the data: $msg32 = hash('sha256', hash('sha256', $data, true), true); // Create a context: $ctx = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY); // Load up the public key from its bytes (into $publicKey): $publicKey; secp256k1_ec_pubkey_parse($ctx, $publicKeyRaw, $publicKey); // Load up the signature from its bytes (into $signature): $signature; secp256k1_ecdsa_signature_parse_der($ctx, $decodedSignature, $signature); if ($signature == null) { // Not a valid signature. error('field/invalid', 'signature'); } // Verify: return secp256k1_ecdsa_verify($ctx, $msg32, $signature, $publicKey); }
/** * @param $privkey * @param $msg * @param $sig * @param $ePubCreate * @param $eSigCreate */ private function genericTest($context, $privkey, $msg, $sig, $ePubCreate, $eSigCreate) { $seckey = $this->toBinary32($privkey); $msg = $this->toBinary32($msg); $sig = pack("H*", $sig); /** @var resource $pubkey */ $pubkey = ''; $this->assertEquals($ePubCreate, \secp256k1_ec_pubkey_create($context, $pubkey, $seckey)); /** @var resource $s */ $s = ''; secp256k1_ecdsa_signature_parse_der($context, $s, $sig); $this->assertEquals($eSigCreate, \secp256k1_ecdsa_verify($context, $s, $msg, $pubkey)); }
/** * @param $data * @return SignatureInterface */ public function parse($data) { $buffer = (new Parser($data))->getBuffer(); $binary = $buffer->getBinary(); $sig_t = ''; /** @var resource $sig_t */ if (!secp256k1_ecdsa_signature_parse_der($this->ecAdapter->getContext(), $binary, $sig_t)) { throw new \RuntimeException('Secp256k1: parse der failure'); } // Unfortunately, we need to use the Parser here to get r and s :/ list(, $inner) = $this->getOuterTemplate()->parse(new Parser($buffer)); list(, $r, , $s) = $this->getInnerTemplate()->parse(new Parser($inner)); /** @var Buffer $r */ /** @var Buffer $s */ return new Signature($this->ecAdapter, $r->getInt(), $s->getInt(), $sig_t); }
<?php $context = secp256k1_context_create(SECP256K1_CONTEXT_VERIFY | SECP256K1_CONTEXT_SIGN); $context = secp256k1_context_create(SECP256K1_CONTEXT_SIGN | SECP256K1_CONTEXT_VERIFY); $msg32 = hash('sha256', 'this is a message!', true); $signatureRaw = pack("H*", "3044022055ef6953afd139d917d947ba7823ab5dfb9239ba8a26295a218cad88fb7299ef022057147cf4233ff3b87fa64d82a0b9a327e9b6d5d0070ab3f671b795934c4f2074"); $publicKeyRaw = pack("H*", '04fae8f5e64c9997749ef65c5db9f0ec3e121dc6901096c30da0f105a13212b6db4315e65a2d63cc667c034fac05cdb3c7bc1abfc2ad90f7f97321613f901758c9'); // Load up the public key from its bytes (into $publicKey): /** @var resource $publicKey */ $publicKey = ''; secp256k1_ec_pubkey_parse($context, $publicKey, $publicKeyRaw); // Load up the signature from its bytes (into $signature): /** @var resource $signature */ $signature = ''; secp256k1_ecdsa_signature_parse_der($context, $signature, $signatureRaw); // Verify: for ($i = 0; $i < 10000; $i++) { $result = secp256k1_ecdsa_verify($context, $signature, $msg32, $publicKey); }