Is there exists a working shards for generate ECDSA key pairs, generate public key from private key

What i means is a shard like this: GitHub - Axentro/crystal-ecdsa: C bindings for OpenSSL ECDSA

But, this shared not works any more, many syntax error, type error.

Thank you.

Try this one: GitHub - spider-gazelle/openssl_ext: Crystal bindings for OpenSSL RSA

It should also support ECDSA.

The jwt shard uses it and supports ECDSA tokens.

1 Like

There was an issue with crystal 1.5 using openssl 3.0
this is resolved now after a shards update when using spider-gazelle/openssl_ext

2 Likes

Thank you, i updated to 2.2.0 after shards update.

Hi, i guess i misuse something? or there is no way to generate a key pair from a string use openssl_ext shard?

Following is my code example:

      words = env.params.body["mnemonic"].as(String).split(" ")
      m1 = Bip0039::Mnemonic.new words
      key = m1.to_hex  # 3c3827b462c004f9f27b44b3c4c3c8db8a25f6f44f4dace4dc69825eb6fb9a02
      p! key
      x = LibCrypto.ec_key_generate_key(key: key) # i want to generate key pair use above string, but no luck.
      p! x
      x

When above code run, i get following error:

 key # => "3c3827b462c004f9f27b44b3c4c3c8db8a25f6f44f4dace4dc69825eb6fb9a02"
 Invalid memory access (signal 11) at address 0x0
 [0x563b85206836] *Exception::CallStack::print_backtrace:Nil +118 in ./magpie-code-admin_cr
 [0x563b851be4ea] ~procProc(Int32, Pointer(LibC::SiginfoT), Pointer(Void), Nil) +330 in ./magpie-code-admin_cr
 [0x7f29c4f3b304] ?? +139817374692100 in /lib/ld-musl-x86_64.so.1

I try use like esdsa_key_pair = OpenSSL::PKey::EC.new(key: key), but not works too, because the key is expected to be a valid private key, but i still not generate it yet.

Thank you.

Hi, @stakach , let me describe my demand, please give me some clue if this is possible when use with openssl_ext shards.

  1. The first step, i generate some mnemonics, like this: [“measure”, “come”, “cube”, “ostrich”, “wide”, “inspire”, “hello”, “essay”, “ready”, “cute”, “reform”, “sustain”], use can writ e it somewhere, which can be used to restore a hex string like this:
m1 = Bip0039::Mnemonic.new ["measure", "come", "cube", "ostrich", "wide", "inspire", "hello", "essay", "ready", "cute", "reform", "sustain"]
# => <Bip0039::Mnemonic:0x7f37ca6e4c80 @ent=128, @seed=183297182565288719506055787609377395053>
m1.to_hex
# => "89e5c0d5ce7faaea9ab269b2c6d6d16d"

I use bip39 shard for this, it works.

  1. Then i need use above hex string, 89e5c0d5ce7faaea9ab269b2c6d6d16d, or the array of mnemonics, to generate a pair of key, i am blocked on there.

When use go, those code like this:

import (
	"bytes"
	"crypto/ecdsa"
	"crypto/elliptic"
	"github.com/tyler-smith/go-bip39"
)

	seed, err := bip39.NewSeedWithErrorChecking(mnemonic, "password")
	if err != nil {
		return
	}
	return ecdsa.GenerateKey(elliptic.P256(), bytes.NewBuffer(seed))

Thank you.

not sure, pretty sure Go implements their crypto natively in Go.
You’ll need to find an OpenSSL example of the same and then we can look at implementing any missing functions