49 lines
1.5 KiB
Elixir
49 lines
1.5 KiB
Elixir
# Test AES cipher with known values
|
|
|
|
Code.require_file("lib/odinsea/util/bit_tools.ex", ".")
|
|
Code.require_file("lib/odinsea/net/cipher/ig_cipher.ex", ".")
|
|
Code.require_file("lib/odinsea/net/cipher/aes_cipher.ex", ".")
|
|
|
|
alias Odinsea.Net.Cipher.AESCipher
|
|
|
|
import Bitwise
|
|
|
|
# Test: AES crypt should be self-inverse (since it's just XOR)
|
|
iv = <<0x0F, 0x0C, 0x0C, 0xA8>>
|
|
data = <<0xBF, 0x0A, 0xCD, 0xDE, 0xC7, 0x71, 0xAC>>
|
|
|
|
IO.puts("Testing AES cipher:")
|
|
IO.puts("IV: #{Base.encode16(iv)}")
|
|
IO.puts("Data: #{Base.encode16(data)}")
|
|
IO.puts("")
|
|
|
|
# Expand IV to 16 bytes
|
|
expanded_iv = :binary.copy(iv, 4)
|
|
IO.puts("Expanded IV (16 bytes): #{Base.encode16(expanded_iv)}")
|
|
|
|
# The AES key (16 bytes)
|
|
aes_key = <<
|
|
0x13, 0x00, 0x00, 0x00,
|
|
0x08, 0x00, 0x00, 0x00,
|
|
0x06, 0x00, 0x00, 0x00,
|
|
0xB4, 0x00, 0x00, 0x00
|
|
>>
|
|
|
|
# Encrypt the expanded IV to get keystream
|
|
keystream = :crypto.crypto_one_time(:aes_128_ecb, aes_key, expanded_iv, true)
|
|
IO.puts("Keystream: #{Base.encode16(keystream)}")
|
|
|
|
# XOR data with keystream (only use as many bytes as data)
|
|
keystream_bytes = :binary.bin_to_list(keystream) |> Enum.take(byte_size(data))
|
|
data_bytes = :binary.bin_to_list(data)
|
|
result_bytes = Enum.zip_with(data_bytes, keystream_bytes, fn x, y -> bxor(x, y) end)
|
|
result = :binary.list_to_bin(result_bytes)
|
|
|
|
IO.puts("XOR result: #{Base.encode16(result)}")
|
|
IO.puts("")
|
|
|
|
# Compare with AESCipher.crypt
|
|
aes_result = AESCipher.crypt(data, iv)
|
|
IO.puts("AESCipher.crypt result: #{Base.encode16(aes_result)}")
|
|
IO.puts("Match: #{result == aes_result}")
|