diff --git a/lib/miscreant/aes/siv.rb b/lib/miscreant/aes/siv.rb index 3cf2cdd..d1f8eb6 100644 --- a/lib/miscreant/aes/siv.rb +++ b/lib/miscreant/aes/siv.rb @@ -68,7 +68,7 @@ def seal(plaintext, associated_data = []) # @raise [Miscreant::IntegrityError] ciphertext and/or associated data are corrupt or tampered with # @return [String] decrypted plaintext def open(ciphertext, associated_data = []) - raise TypeError, "expected String, got #{ciphertext.class}" unless ciphertext.is_a?(String) + Internals::Util.validate_bytestring("ciphertext", ciphertext) v = ciphertext[0, Internals::Block::SIZE] plaintext = @ctr.encrypt(_zero_iv_bits(v), ciphertext[Internals::Block::SIZE..-1]) diff --git a/spec/miscreant/aead_spec.rb b/spec/miscreant/aead_spec.rb index de71534..fbd9207 100644 --- a/spec/miscreant/aead_spec.rb +++ b/spec/miscreant/aead_spec.rb @@ -53,6 +53,15 @@ end.to raise_error(Miscreant::IntegrityError) end end + + it "should raise ArgumentError if wrong encoding" do + ex = test_vectors.first + aead = described_class.new(ex.alg, ex.key) + ciphertext = ex.ciphertext.dup.force_encoding(Encoding::UTF_8) + expect do + aead.open(ciphertext, nonce: example_nonce, ad: example_ad) + end.to raise_error(ArgumentError, "ciphertext must be Encoding::BINARY") + end end end end