Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions ext/openssl/ossl_cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,13 @@ ossl_cipher_update(int argc, VALUE *argv, VALUE self)
}
out_len = in_len + EVP_MAX_BLOCK_LENGTH;

/*
* In-place EVP_CipherUpdate() is not reliable if the context may be
* holding a partial or withheld block.
*/
if (data == str && EVP_CIPHER_CTX_block_size(ctx) != 1)
data = rb_str_new_frozen(data);

if (NIL_P(str))
str = rb_str_buf_new(out_len);
else {
Expand All @@ -412,6 +419,7 @@ ossl_cipher_update(int argc, VALUE *argv, VALUE self)
(unsigned char *)RSTRING_PTR(data), in_len))
ossl_raise(eCipherError, "EVP_CipherUpdate");
rb_str_set_len(str, out_len);
RB_GC_GUARD(data);

return str;
}
Expand Down
23 changes: 23 additions & 0 deletions test/openssl/test_cipher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,29 @@ def test_update_with_buffer
assert_equal expected, shared + cipher.final
end

def test_update_in_place_with_buffered_data
data = "0123456789abcdef".b * 8
%w[aes-128-ecb aes-128-cbc].each do |algo|
[:encrypt, :decrypt].each do |direction|
# 3 bytes leave a partial block in the context. 16 bytes leave a whole
# block there when decrypting, withheld until #final can unpad it.
[3, 16].each do |buffered_len|
cipher = OpenSSL::Cipher.new(algo).__send__(direction)
cipher.random_key
cipher.random_iv
buffered = "a" * buffered_len
expected = cipher.update(buffered) << cipher.update(data)

cipher.reset
buf = data.dup
actual = cipher.update(buffered) << cipher.update(buf, buf)
assert_equal expected, actual,
"#{algo} #{direction} buffered_len=#{buffered_len}"
end
end
end
end

def test_ciphers
ciphers = OpenSSL::Cipher.ciphers
assert_kind_of Array, ciphers
Expand Down
Loading