This commit is contained in:
Didactic Drunk 2020-07-10 17:06:53 -07:00
parent 87d1c12f5b
commit 3d700b6157
4 changed files with 31 additions and 1 deletions

View File

@ -1,5 +1,6 @@
require "../../spec_helper" require "../../spec_helper"
require "../../../src/sodium/sign/secret_key" require "../../../src/sodium/sign/secret_key"
require "../../../src/sodium/crypto_box/secret_key"
detached_test_vectors = [ detached_test_vectors = [
{ {
@ -71,6 +72,12 @@ describe Sodium::Sign::SecretKey do
end end
end end
it "to_curve25519" do
message = "foo"
sskey = Sodium::Sign::SecretKey.new
cskey = sskey.to_curve25519
end
it "RbNaCl detached test vectors" do it "RbNaCl detached test vectors" do
detached_test_vectors.each do |vec| detached_test_vectors.each do |vec|
seckey, plaintext, signature = sign_from_vec vec seckey, plaintext, signature = sign_from_vec vec

View File

@ -55,6 +55,22 @@ class Sodium::CryptoBox
end end
end end
# Use existing secret and public keys.
#
# Takes ownership of an existing key in a SecureBuffer.
# Recomputes the public key from a secret key if missing.
def initialize(@sbuf : SecureBuffer, pkey : Bytes? = nil)
raise ArgumentError.new("Secret key must be #{KEY_SIZE} bytes, got #{@sbuf.bytesize}") if @sbuf.bytesize != KEY_SIZE
if pk = pkey
@public_key = PublicKey.new pk
else
@public_key = PublicKey.new
if LibSodium.crypto_scalarmult_base(@public_key.to_slice, self.to_slice) != 0
raise Sodium::Error.new("crypto_scalarmult_base")
end
end
end
# Use existing secret and public keys. # Use existing secret and public keys.
# #
# Copies secret key to a SecureBuffer. # Copies secret key to a SecureBuffer.

View File

@ -83,5 +83,12 @@ module Sodium
end end
sig sig
end end
def to_curve25519 : CryptoBox::SecretKey
key = SecureBuffer.new CryptoBox::SecretKey::KEY_SIZE
LibSodium.crypto_sign_ed25519_sk_to_curve25519 key.to_slice, @sbuf.to_slice
key.readonly
CryptoBox::SecretKey.new key
end
end end
end end