diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2016-07-24 14:11:05 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2016-07-24 14:11:05 +0900 |
commit | e1361a0aae241e0426b91d3d864c8628e1fa94f5 (patch) | |
tree | d13f281ce6503258e2b27acb946800fc10d88dec | |
parent | 4b860f5fc9c8742f90e9609274638628c3253bc8 (diff) | |
parent | 2ed10db12deb114066443b4a0f1ffd49a8f612b2 (diff) | |
download | ruby-openssl-e1361a0aae241e0426b91d3d864c8628e1fa94f5.tar.gz |
Merge branch 'topic/cipher-fixes'
* topic/cipher-fixes:
cipher: use lower-case cipher name in OpenSSL::Cipher::*.new
cipher: fix handling huge data larger than INT_MAX bytes
-rw-r--r-- | ext/openssl/ossl_cipher.c | 34 | ||||
-rw-r--r-- | lib/openssl/cipher.rb | 8 |
2 files changed, 19 insertions, 23 deletions
diff --git a/ext/openssl/ossl_cipher.c b/ext/openssl/ossl_cipher.c index a786ffab..89744a18 100644 --- a/ext/openssl/ossl_cipher.c +++ b/ext/openssl/ossl_cipher.c @@ -331,25 +331,23 @@ ossl_cipher_update_long(EVP_CIPHER_CTX *ctx, unsigned char *out, long *out_len_p const unsigned char *in, long in_len) { int out_part_len; + int limit = INT_MAX / 2 + 1; long out_len = 0; -#define UPDATE_LENGTH_LIMIT INT_MAX - -#if SIZEOF_LONG > UPDATE_LENGTH_LIMIT - if (in_len > UPDATE_LENGTH_LIMIT) { - const int in_part_len = (UPDATE_LENGTH_LIMIT / 2 + 1) & ~1; - do { - if (!EVP_CipherUpdate(ctx, out ? (out + out_len) : 0, - &out_part_len, in, in_part_len)) - return 0; - out_len += out_part_len; - in += in_part_len; - } while ((in_len -= in_part_len) > UPDATE_LENGTH_LIMIT); - } -#endif - if (!EVP_CipherUpdate(ctx, out ? (out + out_len) : 0, - &out_part_len, in, (int)in_len)) - return 0; - if (out_len_ptr) *out_len_ptr = out_len += out_part_len; + + do { + int in_part_len = in_len > limit ? limit : in_len; + + if (!EVP_CipherUpdate(ctx, out ? (out + out_len) : 0, + &out_part_len, in, in_part_len)) + return 0; + + out_len += out_part_len; + in += in_part_len; + } while ((in_len -= limit) > 0); + + if (out_len_ptr) + *out_len_ptr = out_len; + return 1; } diff --git a/lib/openssl/cipher.rb b/lib/openssl/cipher.rb index 92e5e9f8..0b9be693 100644 --- a/lib/openssl/cipher.rb +++ b/lib/openssl/cipher.rb @@ -18,7 +18,7 @@ module OpenSSL klass = Class.new(Cipher){ define_method(:initialize){|*args| cipher_name = args.inject(name){|n, arg| "#{n}-#{arg}" } - super(cipher_name) + super(cipher_name.downcase) } } const_set(name, klass) @@ -26,10 +26,8 @@ module OpenSSL %w(128 192 256).each{|keylen| klass = Class.new(Cipher){ - define_method(:initialize){|mode| - mode ||= "CBC" - cipher_name = "AES-#{keylen}-#{mode}" - super(cipher_name) + define_method(:initialize){|mode = "CBC"| + super("aes-#{keylen}-#{mode}".downcase) } } const_set("AES#{keylen}", klass) |