Skip to content

Instantly share code, notes, and snippets.

@tarcieri tarcieri/ecb_test.rb
Last active Sep 20, 2016

Embed
What would you like to do?
Either ECB mode is broken in Ruby OpenSSL or I'm retarded...
#!/usr/bin/env ruby
require 'openssl'
# AES-128 ECB mode test vectors
# Taken from: http://www.inconteam.com/software-development/41-encryption/55-aes-test-vectors#aes-ecb-128
KEY = ["2b7e151628aed2a6abf7158809cf4f3c"].pack("H*")
PLAINTEXT = ["6bc1bee22e409f96e93d7e117393172a"].pack("H*")
CIPHERTEXT = ["3ad77bb40d7a3660a89ecaf32466ef97"].pack("H*")
cipher = OpenSSL::Cipher::Cipher.new("aes-128-ecb")
cipher.key = KEY
cipher.padding = 0 # Padding is enabled by default o_O
print "Testing encryption: "
cipher.encrypt
ciphertext = cipher.update(PLAINTEXT) << cipher.final
if ciphertext == CIPHERTEXT
puts "OK!"
else
puts "FAILED! Got #{ciphertext.inspect} instead of #{CIPHERTEXT.inspect}"
end
print "Testing decryption: "
cipher.reset
cipher.decrypt
plaintext = cipher.update(CIPHERTEXT) << cipher.final
if plaintext == PLAINTEXT
puts "OK!"
else
puts "FAILED! Got #{plaintexttext.inspect} instead of #{PLAINTEXT.inspect}"
end
$ ruby --version; ruby ecb_test.rb
ruby 1.9.3p392 (2013-02-22 revision 39386) [x86_64-darwin12.3.0]
Testing encryption: FAILED! Got "\xCE\x9Dp\xDFL\xD0\x95\xC3\x13\x18+\xAC\x1D2\xE7\x15" instead of ":\xD7{\xB4\rz6`\xA8\x9E\xCA\xF3$f\xEF\x97"
Testing decryption: OK!
$ ruby --version; ruby ecb_test.rb
ruby 2.0.0p0 (2013-02-24 revision 39474) [x86_64-darwin12.3.0]
Testing encryption: FAILED! Got "\xCE\x9Dp\xDFL\xD0\x95\xC3\x13\x18+\xAC\x1D2\xE7\x15" instead of ":\xD7{\xB4\rz6`\xA8\x9E\xCA\xF3$f\xEF\x97"
Testing decryption: OK!
$ ruby --version; ruby ecb_test.rb
jruby 1.7.2 (1.9.3p327) 2013-01-04 302c706 on Java HotSpot(TM) 64-Bit Server VM 1.7.0_21-b12 [darwin-x86_64]
Testing encryption: OK!
Testing decryption: OK!
@netjunki

This comment has been minimized.

Copy link

commented Aug 2, 2013

Ever find a solution to this one? It definitely looks like ECB mode is broken based on your tests. I filed a ticket on the ruby project's bug tracker if you want to follow whether it gets fixed. :-)

https://bugs.ruby-lang.org/issues/8720

@tarcieri

This comment has been minimized.

Copy link
Owner Author

commented Sep 20, 2016

@netjunki I finally filed a bug for this issue: ruby/openssl#73

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.