Last active
June 30, 2021 14:54
-
-
Save tarcieri/5550786 to your computer and use it in GitHub Desktop.
Either ECB mode is broken in Ruby OpenSSL or I'm retarded...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ 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! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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