Ruby/openssl: convert Elliptic Curve point octet string into OpenSSL::PKey::EC::Point -
i trying write ruby code check elliptic curve digital signature algorithm (ecdsa) signature on particular message found here.
the problem don't know how convert octet string public key openssl::pkey::ec::point object. if writing in c, pass octet string openssl's o2i_ecpublickey
, close want , in fact used reference implementation. however, searched source code of ruby (mri) , contains no calls o2i_ecpublickey
don't know how use function ruby without writing c extension.
here octet string, in hex. 0x04 byte followed 2 32-byte integers represent x , y coordinates of point on elliptic curve:
04fc9702847840aaf195de8442ebecedf5b095cdbb9bc716bda9110971b28a49e0ead8564ff0db22209e0374782c093bb899692d524e9d6a6956e7c5ecbcd68284
so know how convert string in openssl::pkey::ec::point
in ruby? once point object, use in following code believe verify signature:
key = openssl::pkey::ec.new('secp256k1') key.public_key = point result = key.dsa_verify_asn1(digest, signature)
update:
thanks jay-ar polidario got work. here full code have verifies signature using openssl. wrote gem called ecdsa , included code showing how use gem same thing.
# coding: ascii-8bit digest = "\xbf\x91\xfb\x0b\x4f\x63\x33\x77\x4a\x02\x2b\xd3\x07\x8e\xd6\xcc" \ "\xd1\x76\xee\x31\xed\x4f\xb3\xf9\xaf\xce\xb7\x2a\x37\xe7\x87\x86" signature_der_string = "\x30\x45" \ "\x02\x21\x00" \ "\x83\x89\xdf\x45\xf0\x70\x3f\x39\xec\x8c\x1c\xc4\x2c\x13\x81\x0f" \ "\xfc\xae\x14\x99\x5b\xb6\x48\x34\x02\x19\xe3\x53\xb6\x3b\x53\xeb" \ "\x02\x20" \ "\x09\xec\x65\xe1\xc1\xaa\xee\xc1\xfd\x33\x4c\x6b\x68\x4b\xde\x2b" \ "\x3f\x57\x30\x60\xd5\xb7\x0c\x3a\x46\x72\x33\x26\xe4\xe8\xa4\xf1" public_key_octet_string = "\x04" \ "\xfc\x97\x02\x84\x78\x40\xaa\xf1\x95\xde\x84\x42\xeb\xec\xed\xf5" \ "\xb0\x95\xcd\xbb\x9b\xc7\x16\xbd\xa9\x11\x09\x71\xb2\x8a\x49\xe0" \ "\xea\xd8\x56\x4f\xf0\xdb\x22\x20\x9e\x03\x74\x78\x2c\x09\x3b\xb8" \ "\x99\x69\x2d\x52\x4e\x9d\x6a\x69\x56\xe7\xc5\xec\xbc\xd6\x82\x84" # verifying openssl. require 'openssl' ec = openssl::pkey::ec.new('secp256k1') key_bn = openssl::bn.new(public_key_octet_string, 2) # 2 means binary ec.public_key = openssl::pkey::ec::point.new(ec.group, key_bn) result = ec.dsa_verify_asn1(digest, signature_der_string) puts result # => true # verifying new ecdsa gem wrote, version 0.1.5 require 'ecdsa' group = ecdsa::group::secp256k1 point = ecdsa::format::pointoctetstring.decode(public_key_octet_string, group) signature = ecdsa::format::signaturederstring.decode(signature_der_string) result = ecdsa.valid_signature?(point, digest, signature) puts result # => true
i think it's weird openssl makes represent public key temporarily single bn (big number), because 2 big numbers. gem can directly convert octet strings (as defined in sec2 standard) ecdsa::point
objects.
try following (tested without errors):
key = '04fc9702847840aaf195de8442ebecedf5b095cdbb9bc716bda9110971b28a49e0ead8564ff0db22209e0374782c093bb899692d524e9d6a6956e7c5ecbcd68284' key_bn = openssl::bn.new(key, 16) #input: 16=hexa, output: bignumber group = openssl::pkey::ec::group.new('secp256k1') point = openssl::pkey::ec::point.new(group, key_bn) #--> <openssl::pkey::ec::point:0x5288178>
Comments
Post a Comment