Skip to content

Commit

Permalink
Merge pull request #824 from rhenium/ky/cipher-update-make-buffer-ind…
Browse files Browse the repository at this point in the history
…ependent

cipher: make output buffer String independent
  • Loading branch information
rhenium authored Dec 18, 2024
2 parents f9ec66f + 9cc8a83 commit 3427222
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
5 changes: 4 additions & 1 deletion ext/openssl/ossl_cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,10 @@ ossl_cipher_update(int argc, VALUE *argv, VALUE self)
str = rb_str_new(0, out_len);
} else {
StringValue(str);
rb_str_resize(str, out_len);
if ((long)rb_str_capacity(str) >= out_len)
rb_str_modify(str);
else
rb_str_modify_expand(str, out_len - RSTRING_LEN(str));
}

if (!ossl_cipher_update_long(ctx, (unsigned char *)RSTRING_PTR(str), &out_len, in, in_len))
Expand Down
1 change: 1 addition & 0 deletions ext/openssl/ossl_digest.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ ossl_digest_finish(int argc, VALUE *argv, VALUE self)
str = rb_str_new(NULL, out_len);
} else {
StringValue(str);
rb_str_modify(str);
rb_str_resize(str, out_len);
}

Expand Down
24 changes: 24 additions & 0 deletions test/openssl/test_cipher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,30 @@ def test_ctr_if_exists
assert_equal pt, cipher.update(ct) << cipher.final
end

def test_update_with_buffer
cipher = OpenSSL::Cipher.new("aes-128-ecb").encrypt
cipher.random_key
expected = cipher.update("data") << cipher.final
assert_equal 16, expected.bytesize

# Buffer is supplied
cipher.reset
buf = String.new
assert_same buf, cipher.update("data", buf)
assert_equal expected, buf + cipher.final

# Buffer is frozen
cipher.reset
assert_raise(FrozenError) { cipher.update("data", String.new.freeze) }

# Buffer is a shared string [ruby-core:120141] [Bug #20937]
cipher.reset
buf = "x" * 1024
shared = buf[-("data".bytesize + 32)..-1]
assert_same shared, cipher.update("data", shared)
assert_equal expected, shared + cipher.final
end

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

0 comments on commit 3427222

Please sign in to comment.