Changeset 9


Ignore:
Timestamp:
05/11/06 08:16:07 (13 years ago)
Author:
sgk
Message:

API変更。keyBitLength引数を無くした。

Location:
camellia/trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • camellia/trunk/camellia.py

    r4 r9  
    305305 
    306306# public 
    307 def Ekeygen(keyBitLength, rawKey): 
    308   '''Ekeygen(keyBitLength, rawKey) 
    309  
    310   keyBitLength: 128 or 192 or 256 
    311   rawKey: string 
     307def Ekeygen(rawKey): 
     308  '''Ekeygen(rawKey) 
     309 
     310  rawKey: string; 16 or 24 or 32 character length 
    312311  returns: keyTable''' 
    313312 
     
    320319      ((x[idx1] << r) | (x[idx2] >> (32 - r))) % 0x100000000) 
    321320 
    322   if keyBitLength == 128: 
     321  keyLength = len(rawKey) 
     322  if keyLength == 16: 
    323323    t = struct.unpack('!IIII', rawKey) 
    324324    u = (0, 0, 0, 0) 
    325   elif keyBitLength == 192: 
     325  elif keyLength == 24: 
    326326    t = struct.unpack('!IIIIII', rawKey) 
    327327    (t, u) = (t[0:4], t[4:6]) 
    328328    u = u + (~u[0] % 0x100000000, ~u[1] % 0x100000000) 
    329   elif keyBitLength == 256: 
     329  elif keyLength == 32: 
    330330    t = struct.unpack('!IIIIIIII', rawKey) 
    331331    (t, u) = (t[0:4], t[4:8]) 
     
    333333    assert False 
    334334 
    335   if keyBitLength == 128: 
     335  if keyLength == 16: 
    336336    v = list(t) 
    337337    feistel1(v, SIGMA1) 
     
    366366 
    367367 
    368 def EncryptBlock(keyBitLength, plainText, keyTable): 
    369   '''EncryptBlock(keyBitLength, plainText, keyTable) 
    370  
    371   keyBitLength: 128 or 192 or 256 
    372   plainText: 16 bytes string 
     368def EncryptBlock(plainText, keyTable): 
     369  '''EncryptBlock(plainText, keyTable) 
     370 
     371  plainText: string; 16 characters 
    373372  keyTable: key generated by Ekeygen() 
    374373  returns: chipher text''' 
     
    395394  feistel1(t, keyTable[44:48]) 
    396395 
    397   if keyBitLength == 128: 
     396  if len(keyTable) == 52: 
     397    # 128 bit key 
    398398    t = map(lambda a,b:a^b, (t[2], t[3], t[0], t[1]), keyTable[48:52]) 
    399399  else: 
     400    # 192 or 256 bit key 
    400401    t[1] ^= leftRotate1(t[0] & keyTable[48]) 
    401402    t[0] ^= t[1] | keyTable[49] 
     
    410411 
    411412 
    412 def DecryptBlock(keyBitLength, cipherText, keyTable): 
    413   '''EncryptBlock(keyBitLength, plainText, keyTable) 
    414  
    415   keyBitLength: 128 or 192 or 256 
     413def DecryptBlock(cipherText, keyTable): 
     414  '''EncryptBlock(plainText, keyTable) 
     415 
    416416  cipherText: 16 bytes string 
    417417  keyTable: key generated by Ekeygen() 
     
    420420  t = list(struct.unpack('!IIII', cipherText)) 
    421421 
    422   if keyBitLength == 128: 
     422  if len(keyTable) == 52: 
     423    # 128 bit key 
    423424    t = map(lambda a,b:a^b, t, keyTable[48:52]) 
    424425  else: 
     426    # 192 or 256 bit key 
    425427    t = map(lambda a,b:a^b, t, keyTable[64:68]) 
    426428    feistel2(t, keyTable[60:64]) 
     
    455457 
    456458if __name__ == '__main__': 
    457   key128 = Ekeygen(128, "0123456789abcdef") 
    458   key192 = Ekeygen(192, "0123456789abcdef01234567") 
    459   key256 = Ekeygen(256, "0123456789abcdef0123456789abcdef") 
     459  key128 = Ekeygen("0123456789abcdef") 
     460  key192 = Ekeygen("0123456789abcdef01234567") 
     461  key256 = Ekeygen("0123456789abcdef0123456789abcdef") 
    460462 
    461463  plain = "0123456789abcdef" 
    462   enc128 = EncryptBlock(128, plain, key128) 
    463   dec128 = DecryptBlock(128, enc128, key128) 
    464   enc192 = EncryptBlock(192, plain, key192) 
    465   dec192 = DecryptBlock(192, enc192, key192) 
    466   enc256 = EncryptBlock(256, plain, key256) 
    467   dec256 = DecryptBlock(256, enc256, key256) 
     464  enc128 = EncryptBlock(plain, key128) 
     465  dec128 = DecryptBlock(enc128, key128) 
     466  enc192 = EncryptBlock(plain, key192) 
     467  dec192 = DecryptBlock(enc192, key192) 
     468  enc256 = EncryptBlock(plain, key256) 
     469  dec256 = DecryptBlock(enc256, key256) 
    468470 
    469471  assert key128 == ( 
  • camellia/trunk/speed.py

    r7 r9  
    1010  key = sys.argv[1] 
    1111 
    12 keylen = len(key) * 8 
    13 assert keylen in (128, 192, 256), "Key string should be 16, 24, 32 charaters." 
     12assert len(key) in (16, 24, 32), "Key string should be 16, 24, 32 charaters." 
    1413 
    15 keytable = camellia.Ekeygen(keylen, key) 
     14keytable = camellia.Ekeygen(key) 
    1615 
    1716for x in xrange(100000): 
    18   camellia.EncryptBlock(keylen, plain, keytable) 
     17  camellia.EncryptBlock(plain, keytable) 
  • camellia/trunk/t_camellia.py

    r5 r9  
    2020  if op == 'K': 
    2121    keyid = id 
    22     keylen = len(line) * 8 
    23     keytable = camellia.Ekeygen(keylen, line) 
     22    keytable = camellia.Ekeygen(line) 
    2423 
    2524  elif op == 'P': 
     
    2928  elif op == 'C': 
    3029    assert id == testid 
    31     assert camellia.EncryptBlock(keylen, plain, keytable) == line, 'Encrypt %s.%s' % (keyid, testid) 
    32     assert camellia.DecryptBlock(keylen, line, keytable) == plain, 'Encrypt %s.%s' % (keyid, testid) 
     30    assert camellia.EncryptBlock(plain, keytable) == line, 'Encrypt %s.%s' % (keyid, testid) 
     31    assert camellia.DecryptBlock(line, keytable) == plain, 'Encrypt %s.%s' % (keyid, testid) 
    3332 
    3433print 'ok' 
Note: See TracChangeset for help on using the changeset viewer.