1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
# encoding: binary
# frozen_string_literal: true
RSpec.shared_examples "aead" do
let(:corrupt_ciphertext) { ciphertext.succ }
let(:trunc_ciphertext) { ciphertext[0, 20] }
let(:invalid_nonce) { nonce[0, nonce.bytesize / 2] } # too short!
let(:invalid_nonce_long) { nonce + nonce } # too long!
let(:nonce_error_regex) { /Nonce.*(Expected #{aead.nonce_bytes})/ }
let(:corrupt_ad) { ad.succ }
let(:trunc_ad) { ad[0, ad.bytesize / 2] }
let(:aead) { described_class.new(key) }
context "new" do
it "accepts strings" do
expect { described_class.new(key) }.to_not raise_error
end
it "raises on a nil key" do
expect { described_class.new(nil) }.to raise_error(TypeError)
end
it "raises on a short key" do
expect { described_class.new("hello") }.to raise_error RbNaCl::LengthError
end
it "raises on a long key" do
expect { described_class.new("hello" + key) }.to raise_error RbNaCl::LengthError
end
end
context "encrypt" do
it "encrypts a message" do
expect(aead.encrypt(nonce, message, ad)).to eq ciphertext
end
it "raises on a short nonce" do
expect do
aead.encrypt(invalid_nonce, message, ad)
end.to raise_error(RbNaCl::LengthError, nonce_error_regex)
end
it "raises on a long nonce" do
expect do
aead.encrypt(invalid_nonce_long, message, ad)
end.to raise_error(RbNaCl::LengthError, nonce_error_regex)
end
it "works with an empty message" do
expect do
aead.encrypt(nonce, nil, ad)
end.to_not raise_error
end
it "works with an empty additional data" do
expect do
aead.encrypt(nonce, message, nil)
end.to_not raise_error
end
end
context "decrypt" do
it "decrypts a message" do
expect(aead.decrypt(nonce, ciphertext, ad)).to eq message
end
it "raises on a truncated message to decrypt" do
expect do
aead.decrypt(nonce, trunc_ciphertext, ad)
end.to raise_error(RbNaCl::CryptoError, /Decryption failed. Ciphertext failed verification./)
end
it "raises on a corrupt ciphertext" do
expect do
aead.decrypt(nonce, corrupt_ciphertext, ad)
end.to raise_error(RbNaCl::CryptoError, /Decryption failed. Ciphertext failed verification./)
end
it "raises when the additional data is truncated" do
expect do
aead.decrypt(nonce, ciphertext, corrupt_ad)
end.to raise_error(RbNaCl::CryptoError, /Decryption failed. Ciphertext failed verification./)
end
it "raises when the additional data is corrupt " do
expect do
aead.decrypt(nonce, ciphertext, trunc_ad)
end.to raise_error(RbNaCl::CryptoError, /Decryption failed. Ciphertext failed verification./)
end
it "raises on a short nonce" do
expect do
aead.decrypt(invalid_nonce, message, ad)
end.to raise_error(RbNaCl::LengthError, nonce_error_regex)
end
it "raises on a long nonce" do
expect do
aead.decrypt(invalid_nonce_long, message, ad)
end.to raise_error(RbNaCl::LengthError, nonce_error_regex)
end
end
end
|