# Author:: Victor Costan
# Copyright:: Copyright (C) 2008 Massachusetts Institute of Technology
# License:: MIT

require 'rubygems'
require 'smartcard'

require 'test/unit'

class DesTest < Test::Unit::TestCase
  Des = Smartcard::Gp::Des
  
  def test_crypt
    key = "@ABCDEFGHIJKLMNO"
    data = "Quick brown fox "
    iv = "0123456789ABCDEF"
    golden = [0xC3, 0x21, 0xFE, 0x0D, 0xD1, 0x26, 0x34, 0xBE, 0xA6, 0x27, 0x7A,
              0x00, 0x27, 0x26, 0xF0, 0xAA].pack('C*')
    assert_equal golden, Des.crypt(key, data, iv)
  end
  
  def test_mac_3des
    key = "@ABCDEFGHIJKLMNO"
    data = "Quick brown fox "
    golden = [0x99, 0xB8, 0x86, 0x86, 0x16, 0xBF, 0xDE, 0x01].pack('C*')

    assert_equal golden, Des.mac_3des(key, data)    
  end
  
  def test_mac_retail
    data = [0x72, 0xC2, 0x9C, 0x23, 0x71, 0xCC, 0x9B, 0xDB, 0x65, 0xB7, 0x79,
        0xB8, 0xE8, 0xD3, 0x7B, 0x29, 0xEC, 0xC1, 0x54, 0xAA, 0x56, 0xA8, 0x79,
        0x9F, 0xAE, 0x2F, 0x49, 0x8F, 0x76, 0xED, 0x92, 0xF2].pack('C*')
    key = [0x79, 0x62, 0xD9, 0xEC, 0xE0, 0x3D, 0x1A, 0xCD, 0x4C, 0x76, 0x08,
           0x9D, 0xCE, 0x13, 0x15, 0x43].pack('C*')
    golden = [0x5F, 0x14, 0x48, 0xEE, 0xA8, 0xAD, 0x90, 0xA7].pack('C*')
    
    assert_equal golden, Des.mac_retail(key, data)
  end
end
