2019-06-28 12:30:33 +02:00
|
|
|
require "../../spec_helper"
|
2019-06-29 01:17:09 +02:00
|
|
|
require "../../../src/sodium/sign/secret_key"
|
2020-07-11 02:06:53 +02:00
|
|
|
require "../../../src/sodium/crypto_box/secret_key"
|
2019-06-28 12:30:33 +02:00
|
|
|
|
2019-08-06 11:20:47 +02:00
|
|
|
detached_test_vectors = [
|
|
|
|
{
|
|
|
|
seed: "b18e1d0045995ec3d010c387ccfeb984d783af8fbb0f40fa7db126d889f6dadd",
|
|
|
|
secret_key: "b18e1d0045995ec3d010c387ccfeb984d783af8fbb0f40fa7db126d889f6dadd" \
|
|
|
|
"77f48b59caeda77751ed138b0ec667ff50f8768c25d48309a8f386a2bad187fb",
|
|
|
|
public_key: "77f48b59caeda77751ed138b0ec667ff50f8768c25d48309a8f386a2bad187fb",
|
|
|
|
plaintext: "916c7d1d268fc0e77c1bef238432573c39be577bbea0998936add2b50a653171" \
|
|
|
|
"ce18a542b0b7f96c1691a3be6031522894a8634183eda38798a0c5d5d79fbd01" \
|
|
|
|
"dd04a8646d71873b77b221998a81922d8105f892316369d5224c9983372d2313" \
|
|
|
|
"c6b1f4556ea26ba49d46e8b561e0fc76633ac9766e68e21fba7edca93c4c7460" \
|
|
|
|
"376d7f3ac22ff372c18f613f2ae2e856af40",
|
|
|
|
signature: "6bd710a368c1249923fc7a1610747403040f0cc30815a00f9ff548a896bbda0b" \
|
|
|
|
"4eb2ca19ebcf917f0f34200a9edbad3901b64ab09cc5ef7b9bcc3c40c0ff7509",
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
private def sign_from_vec(vec)
|
|
|
|
seckey = Sodium::Sign::SecretKey.new seed: vec[:seed].hexbytes
|
|
|
|
seckey.to_slice.should eq vec[:secret_key].hexbytes
|
|
|
|
seckey.public_key.to_slice.should eq vec[:public_key].hexbytes
|
|
|
|
plaintext = vec[:plaintext].hexbytes
|
|
|
|
signature = vec[:signature].hexbytes
|
|
|
|
{seckey, plaintext, signature}
|
|
|
|
end
|
|
|
|
|
2019-07-04 02:56:02 +02:00
|
|
|
private def new_sign_key_to_slice
|
|
|
|
Sodium::Sign::SecretKey.new.to_slice
|
2019-06-29 21:44:47 +02:00
|
|
|
end
|
|
|
|
|
2019-06-29 01:17:09 +02:00
|
|
|
describe Sodium::Sign::SecretKey do
|
2019-06-29 21:44:47 +02:00
|
|
|
it "loads keys" do
|
|
|
|
key1 = Sodium::Sign::SecretKey.new
|
2019-07-04 02:56:02 +02:00
|
|
|
key2 = Sodium::Sign::SecretKey.new key1.to_slice, key1.public_key.to_slice
|
|
|
|
key1.to_slice.should eq key2.to_slice
|
|
|
|
key1.public_key.to_slice.should eq key2.public_key.to_slice
|
2019-06-30 02:21:00 +02:00
|
|
|
end
|
2019-06-29 21:44:47 +02:00
|
|
|
|
2019-06-30 02:21:00 +02:00
|
|
|
it "recomputes the public key" do
|
|
|
|
key1 = Sodium::Sign::SecretKey.new
|
2019-07-04 02:56:02 +02:00
|
|
|
key2 = Sodium::Sign::SecretKey.new key1.to_slice
|
|
|
|
key1.to_slice.should eq key2.to_slice
|
|
|
|
key1.public_key.to_slice.should eq key2.public_key.to_slice
|
2019-06-29 21:44:47 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
it "seed keys" do
|
|
|
|
seed = Bytes.new Sodium::Sign::SecretKey::SEED_SIZE
|
|
|
|
key1 = Sodium::Sign::SecretKey.new seed: seed
|
|
|
|
key2 = Sodium::Sign::SecretKey.new seed: seed
|
2019-07-04 02:56:02 +02:00
|
|
|
key1.to_slice.should eq key2.to_slice
|
|
|
|
key1.public_key.to_slice.should eq key2.public_key.to_slice
|
2019-06-29 21:44:47 +02:00
|
|
|
end
|
|
|
|
|
2019-06-28 12:30:33 +02:00
|
|
|
it "signs and verifies" do
|
|
|
|
message = "foo"
|
2019-06-29 01:17:09 +02:00
|
|
|
skey = Sodium::Sign::SecretKey.new
|
2019-06-28 12:30:33 +02:00
|
|
|
sig = skey.sign_detached message
|
|
|
|
|
|
|
|
skey.public_key.verify_detached message, sig
|
|
|
|
end
|
|
|
|
|
|
|
|
it "signs and fails" do
|
|
|
|
message = "foo"
|
2019-06-29 01:17:09 +02:00
|
|
|
skey = Sodium::Sign::SecretKey.new
|
2019-06-28 12:30:33 +02:00
|
|
|
sig = skey.sign_detached message
|
|
|
|
|
2019-06-29 01:17:09 +02:00
|
|
|
expect_raises Sodium::Error::VerificationFailed do
|
2019-06-28 12:30:33 +02:00
|
|
|
skey.public_key.verify_detached "bar", sig
|
|
|
|
end
|
|
|
|
end
|
2019-08-06 11:20:47 +02:00
|
|
|
|
2020-07-11 02:06:53 +02:00
|
|
|
it "to_curve25519" do
|
|
|
|
message = "foo"
|
|
|
|
sskey = Sodium::Sign::SecretKey.new
|
|
|
|
cskey = sskey.to_curve25519
|
|
|
|
end
|
|
|
|
|
2019-08-06 11:20:47 +02:00
|
|
|
it "RbNaCl detached test vectors" do
|
|
|
|
detached_test_vectors.each do |vec|
|
|
|
|
seckey, plaintext, signature = sign_from_vec vec
|
|
|
|
sig = seckey.sign_detached plaintext
|
|
|
|
sig.should eq signature
|
|
|
|
end
|
|
|
|
end
|
2019-06-28 12:30:33 +02:00
|
|
|
end
|