#!/usr/bin/env ruby
#
# $Id: testbf.rb,v 1.2 2001/05/11 07:18:04 hds Exp $
#

require "Blowfish"

def print_hex( s )
  s.each_byte { |b|
    print( "%02X" % [b] )
  }
  print( "\n" )
end


ECB_KEY = "\x00" * 8
ECB_CLR = "\x00" * 8

CBC_KEY = "\x01\x23\x45\x67\x89\xAB\xCD\xEF\xF0\xE1\xD2\xC3\xB4\xA5\x96\x87"
CBC_CLR = "\x37\x36\x35\x34\x33\x32\x31\x20\x4E\x6F\x77\x20\x69\x73\x20\x74\x68\x65\x20\x74\x69\x6D\x65\x20\x66\x6F\x72\x20\x00\x00\x00\x00"

CFB64_KEY = "\x01\x23\x45\x67\x89\xAB\xCD\xEF\xF0\xE1\xD2\xC3\xB4\xA5\x96\x87"
CFB64_CLR = "\x37\x36\x35\x34\x33\x32\x31\x20\x4E\x6F\x77\x20\x69\x73\x20\x74\x68\x65\x20\x74\x69\x6D\x65\x20\x66\x6F\x72\x20\x00"

OFB64_KEY = "\x01\x23\x45\x67\x89\xAB\xCD\xEF\xF0\xE1\xD2\xC3\xB4\xA5\x96\x87"
OFB64_CLR = "\x37\x36\x35\x34\x33\x32\x31\x20\x4E\x6F\x77\x20\x69\x73\x20\x74\x68\x65\x20\x74\x69\x6D\x65\x20\x66\x6F\x72\x20\x00"

ECB_OK   = "\x4E\xF9\x97\x45\x61\x98\xDD\x78"
CBC_OK   = "\x6B\x77\xB4\xD6\x30\x06\xDE\xE6\x05\xB1\x56\xE2\x74\x03\x97\x93\x58\xDE\xB9\xE7\x15\x46\x16\xD9\x59\xF1\x65\x2B\xD5\xFF\x92\xCC"
CFB64_OK = "\xE7\x32\x14\xA2\x82\x21\x39\xCA\xF2\x6E\xCF\x6D\x2E\xB9\xE7\x6E\x3D\xA3\xDE\x04\xD1\x51\x72\x00\x51\x9D\x57\xA6\xC3"
OFB64_OK = "\xE7\x32\x14\xA2\x82\x21\x39\xCA\x62\xB3\x43\xCC\x5B\x65\x58\x73\x10\xDD\x90\x8D\x0C\x24\x1B\x22\x63\xC2\xCF\x80\xDA"


#
# Start test suite
#

print( "\nRuby Blowfish Test Suite:\n" )

#
# ECB-Test
#

puts
e = Blowfish.new( ECB_KEY )
s = e.ecb_encrypt( ECB_CLR )
if s != ECB_OK then
  print( "*** ECB cipher test failed!\n" )
  print( "Expected ECB cipher:\n" )
  print_hex( ECB_OK )
  print( "Actual ECB cipher:\n" )
  print_hex( s )
else
  print( "ECB cipher test ok!\n" )
  print( "ECB cipher:\n" )
  print_hex( s )
end

#
# CBC-Test
#

puts
e = Blowfish.new( CBC_KEY )
s = e.cbc_encrypt( CBC_CLR )
if s != CBC_OK then
  print( "**** CBC cipher test failed!\n" )
  print( "Expected CBC cipher:\n" )
  print_hex( CBC_OK )
  print( "Actual CBC cipher:\n" )
  print_hex( s )
else
  print( "CBC cipher test ok!\n" )
  print( "CBC cipher:\n" )
  print_hex( s )
end
d = Blowfish.new( CBC_KEY )
t = d.cbc_decrypt( s )
print( "Cleartext: %s\n" % [t] )

key = "abcdefg"
str = "12345678" * 1000
e = Blowfish.new( key )
s = e.cbc_encrypt( str )
d = Blowfish.new( key )
t = d.cbc_decrypt( s )
if str != t then
  print( "**** CBC with 8000 bytes failed!\n" )
else
  print( "CBC with 8000 bytes worked!\n" )
end

#
# CFB64-Test
#

puts
e = Blowfish.new( CFB64_KEY )
s = e.cfb64_encrypt( CFB64_CLR )
if s != CFB64_OK then
  print( "**** CFB64 cipher test failed!\n" )
  print( "Expected CFB64 cipher:\n" )
  print_hex( CBC_OK )
  print( "Actual CFB64 cipher:\n" )
  print_hex( s )
else
  print( "CFB64 cipher test ok!\n" )
  print( "CFB64 cipher:\n" )
  print_hex( s )
end
d = Blowfish.new( CFB64_KEY )
t = d.cfb64_decrypt( s )
print( "Cleartext: %s\n" % [t] )

key = "abcdefg"
str = "1234567890" * 1000
e = Blowfish.new( key )
s = e.cfb64_encrypt( str )
d = Blowfish.new( key )
t = d.cfb64_decrypt( s )
if str != t then
  print( "**** CFB64 with 10000 bytes failed!\n" )
else
  print( "CFB64 with 10000 bytes worked!\n" )
end

#
# OFB64-Test
#

puts
e = Blowfish.new( OFB64_KEY )
s = e.ofb64_encrypt( OFB64_CLR )
if s != OFB64_OK then
  print( "**** OFB64 cipher test failed!\n" )
  print( "Expected OFB64 cipher:\n" )
  print_hex( OFB64_OK )
  print( "Actual OFB64 cipher:\n" )
  print_hex( s )
else
  print( "OFB64 cipher test ok!\n" )
  print( "OFB64 cipher:\n" )
  print_hex( s )
end
d = Blowfish.new( OFB64_KEY )
t = d.ofb64_decrypt( s )
print( "Cleartext: %s\n" % [t] )

key = "abcdefg"
str = "1234567890" * 1000
e = Blowfish.new( key )
s = e.ofb64_encrypt( str )
d = Blowfish.new( key )
t = d.ofb64_decrypt( s )
if str != t then
  print( "**** OFB64 with 10000 bytes failed!\n" )
else
  print( "OFB64 with 10000 bytes worked!\n" )
end

puts

#
# EOF
#
