johanns / sha3

SHA3 for Ruby is a XKCP based native (C) binding to SHA3 (FIPS 202) cryptographic hashing algorithm
MIT License
38 stars 6 forks source link

How can I use sha3 with OpenSSL in ruby? #9

Closed DaHoopster closed 5 years ago

DaHoopster commented 5 years ago

I am trying to use sha3 as the digest to sign a message with OpenSSL. But it looks like Ruby 2.5.1 doesn't work with this library. Any thoughts on how I can combine the two? Thanks!

johanns commented 5 years ago

But it looks like Ruby 2.5.1 doesn't work with this library.

Is there an error message you're receiving when trying to use it? What happens if you run the following code from IRB or Pry (assuming your already installed the gem gem install sha3)?

require 'sha3'
SHA3::Digest::SHA224.new()

Please post any error message(s), and operating system information (sample code would be great too).

Any thoughts on how I can combine the two?

I'd love to help, but I'm unsure how you're trying to "combine" them. Please provide more context.

DaHoopster commented 5 years ago

Sorry, pardon the ambiguities, here are the details 😄

I was attempting to do the below:

require 'sha3'
require 'openssl'

p_key = OpenSSL::PKey::EC.new('secp256k1').generate_key
p_key.sign(SHA3::Digest.new(:sha256), 'a_super_secret')

Because sha3 isn't a subclass of OpenSSL::Digest, I got the follow error:

TypeError:
       wrong argument type SHA3::Digest (expected OpenSSL/Digest)

And I am stuck ...

johanns commented 5 years ago

That's helpful, but unfortunately I don't have a solution for your particular use case. I suppose it would be possible to change the base class to OpenSSL::Digest from Digest::Class (which is also the parent class for OpenSSL::Digest), but that doesn't like a good idea because the actual SHA3 implementation isn't part of OpenSSL library.

Assuming that you must use SHA3, you can either fork this project, and change the base class to OpenSSL::Digest (see below), or create a new OpenSSL::Digest subclass that proxies data to SHA3::Digest.

https://github.com/johanns/sha3/blob/2534b73dd75c5c66695300f007392b2623924de0/ext/sha3/digest.c#L234

DaHoopster commented 5 years ago

Thanks @johanns , I will try forking the project

DaHoopster commented 5 years ago

Hmm still no good. It looks like the error message comes from the C extension within ruby's openssl code. I read it on stackoverflow (lost the link) that in order to add a custom digest, a C struct needs to be used as the context, like this line: https://github.com/ruby/openssl/blob/master/ext/openssl/ossl_digest.h#L16

The type error comes from this line: https://github.com/ruby/openssl/blob/fdcda971a26895ea5c5015a90671ee73039d55e8/ext/openssl/ossl.h#L59

I am not good with C tho ...

DaHoopster commented 5 years ago

Actually I made it work via pure Ruby by subclassing OpenSSL::Digest. The trick is also call super in the constructor. Thanks!

katpadi commented 5 years ago

Hi @DaHoopster,

How were you able to make it work? Did you actually monkeypatch the SHA3::Digest class? Can you point me to the right direction? Thanks.