Hello,

In OpenSSL you are allowed to change the iv_length on an AES-BCM cipher. (
http://www.openssl.org/docs/crypto/EVP_EncryptInit.html#GCM_Mode) However
this was not implemented in the ruby-wrapper. Since I am a novice in C and
OpenSSL I think by no means my supplied patch is complete, it is a start
however. Maybe this missing function can be added to Ruby 2.0?

You can now set the iv_length using:

cipher = OpenSSL::Cipher.new('aes-128-gcm').encrypt
cipher.iv_len = 16

An issue I already spotted is that OpenSSL sets the ivlen on the
cipher_data (snippet from OpenSSL crypto/evp/e_aes.c):
EVP_AES_GCM_CTX *gctx = c->cipher_data;
gctx->ivlen = arg;

and not the c->cipher->iv_len. So querying for the iv_len in ruby by using
cipher.iv_len will still report the default which is 12. Encryption however
is done correctly using the new iv-length. I tested it by comparing it to
results from other programming languages (Java and C#).

Regards Andres
(supressed text/html)SW5kZXg6IG9zc2xfY2lwaGVyLmMKPT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09
PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PQotLS0gb3NzbF9jaXBoZXIuYwkocmV2aXNp
b24gNDIwNjQpCisrKyBvc3NsX2NpcGhlci5jCSh3b3JraW5nIGNvcHkpCkBAIC02NzcsNiArNjc3
LDI3IEBACiAgICAgcmV0dXJuIGtleV9sZW5ndGg7CiB9CiAKKy8qCisgKiAgY2FsbC1zZXE6Cisg
KiAgICAgY2lwaGVyLml2X2xlbiA9IGludGVnZXIgLT4gaW50ZWdlcgorICoKKyAqICBTZXRzIHRo
ZSBpdiBsZW5ndGggb2YgdGhlIGNpcGhlci4KKyAqCisgKiAgU2VlIEVWUF9DVFJMX0dDTV9TRVRf
SVZMRU4gZm9yIGZ1cnRoZXIgaW5mb3JtYXRpb24uCisgKi8KK3N0YXRpYyBWQUxVRQorb3NzbF9j
aXBoZXJfc2V0X2djbV9pdl9sZW5ndGgoVkFMVUUgc2VsZiwgVkFMVUUgaXZfbGVuZ3RoKQorewor
ICAgIGludCBpdmxlbiA9IE5VTTJJTlQoaXZfbGVu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