#!/usr/bin/python

# Copyright (c) 2006 by Accense Technology, Inc.

import binascii
import camellia

# Test vector
# 'http://info.isl.ntt.co.jp/crypt/camellia/dl/cryptrec/t_camellia.txt'
testFile = './t_camellia.txt'


for line in file(testFile).xreadlines():
  if line[1:5] != ' No.':
    continue
  op = line[0]
  id = line[5:8]
  line = binascii.a2b_hex(''.join(line[11:].rstrip().split(' ')))

  if op == 'K':
    keyid = id
    keytable = camellia.Ekeygen(line)

  elif op == 'P':
    testid = id
    plain = line

  elif op == 'C':
    assert id == testid
    assert camellia.EncryptBlock(plain, keytable) == line, 'Encrypt %s.%s' % (keyid, testid)
    assert camellia.DecryptBlock(line, keytable) == plain, 'Encrypt %s.%s' % (keyid, testid)

print 'ok'
