Project

General

Profile

Feature #10420 ยป ossl_set_iv_len.patch

Patch that fixes this problem - tomykaira (tomy kaira), 10/23/2014 02:53 AM

View differences:

ext/openssl/ossl_cipher.c
return key_length;
}
/*
* call-seq:
* cipher.iv_len = integer -> integer
*
* Sets the iv length of the cipher. This method is available only for GCM or
* CCM (which is not supported yet) ciphers.
*
* See EVP_CIPHER_CTX_ctrl and EVP_CTRL_GCM_SET_IVLEN for further information.
*/
static VALUE
ossl_cipher_set_iv_length(VALUE self, VALUE iv_length)
{
EVP_CIPHER_CTX *ctx;
int nid;
int len = NUM2INT(iv_length);
GetCipher(self, ctx);
nid = EVP_CIPHER_CTX_nid(ctx);
if (ossl_is_gcm(nid)) {
if (!EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_GCM_SET_IVLEN, len, NULL))
ossl_raise(eCipherError, "unable to set GCM IV length");
} else {
ossl_raise(eCipherError, "IV length is not variable for this cipher");
}
return iv_length;
}
#if defined(HAVE_EVP_CIPHER_CTX_SET_PADDING)
/*
* call-seq:
......
rb_define_method(cCipher, "key_len", ossl_cipher_key_length, 0);
rb_define_method(cCipher, "iv=", ossl_cipher_set_iv, 1);
rb_define_method(cCipher, "iv_len", ossl_cipher_iv_length, 0);
rb_define_method(cCipher, "iv_len=", ossl_cipher_set_iv_length, 1);
rb_define_method(cCipher, "block_size", ossl_cipher_block_size, 0);
rb_define_method(cCipher, "padding=", ossl_cipher_set_padding, 1);
}
test/openssl/test_cipher.rb
end
end
def test_aes_gcm_custom_iv
pt = "You should all use Authenticated Encryption!"
cipher, key, iv = new_encryptor('aes-128-gcm')
cipher.iv = "\0" * 16
cipher2 = OpenSSL::Cipher.new('aes-128-gcm')
cipher2.encrypt
cipher2.key = key
cipher2.iv_len = 16
cipher2.iv = "\0" * 16
ct12 = cipher.update(pt) + cipher.final
ct16 = cipher2.update(pt) + cipher2.final
assert ct12 != ct16
decipher = OpenSSL::Cipher.new('aes-128-gcm')
decipher.auth_tag = cipher2.auth_tag
decipher.decrypt
decipher.key = key
decipher.iv_len = 16
decipher.iv = "\0" * 16
assert_equal pt, decipher.update(ct16) + decipher.final
end
def test_aes_gcm_set_wrong_iv_len
cipher, key, iv = new_encryptor('aes-128-gcm')
cipher = OpenSSL::Cipher.new('aes-128-gcm')
assert_raise OpenSSL::Cipher::CipherError do
cipher.iv_len = -1
end
end
end
private
    (1-1/1)