Changeset 9
 Timestamp:
 05/11/06 08:16:07 (14 years ago)
 Location:
 camellia/trunk
 Files:

 3 edited
Legend:
 Unmodified
 Added
 Removed

camellia/trunk/camellia.py
r4 r9 305 305 306 306 # public 307 def Ekeygen(keyBitLength, rawKey): 308 '''Ekeygen(keyBitLength, rawKey) 309 310 keyBitLength: 128 or 192 or 256 311 rawKey: string 307 def Ekeygen(rawKey): 308 '''Ekeygen(rawKey) 309 310 rawKey: string; 16 or 24 or 32 character length 312 311 returns: keyTable''' 313 312 … … 320 319 ((x[idx1] << r)  (x[idx2] >> (32  r))) % 0x100000000) 321 320 322 if keyBitLength == 128: 321 keyLength = len(rawKey) 322 if keyLength == 16: 323 323 t = struct.unpack('!IIII', rawKey) 324 324 u = (0, 0, 0, 0) 325 elif key BitLength == 192:325 elif keyLength == 24: 326 326 t = struct.unpack('!IIIIII', rawKey) 327 327 (t, u) = (t[0:4], t[4:6]) 328 328 u = u + (~u[0] % 0x100000000, ~u[1] % 0x100000000) 329 elif key BitLength == 256:329 elif keyLength == 32: 330 330 t = struct.unpack('!IIIIIIII', rawKey) 331 331 (t, u) = (t[0:4], t[4:8]) … … 333 333 assert False 334 334 335 if key BitLength == 128:335 if keyLength == 16: 336 336 v = list(t) 337 337 feistel1(v, SIGMA1) … … 366 366 367 367 368 def EncryptBlock(keyBitLength, plainText, keyTable): 369 '''EncryptBlock(keyBitLength, plainText, keyTable) 370 371 keyBitLength: 128 or 192 or 256 372 plainText: 16 bytes string 368 def EncryptBlock(plainText, keyTable): 369 '''EncryptBlock(plainText, keyTable) 370 371 plainText: string; 16 characters 373 372 keyTable: key generated by Ekeygen() 374 373 returns: chipher text''' … … 395 394 feistel1(t, keyTable[44:48]) 396 395 397 if keyBitLength == 128: 396 if len(keyTable) == 52: 397 # 128 bit key 398 398 t = map(lambda a,b:a^b, (t[2], t[3], t[0], t[1]), keyTable[48:52]) 399 399 else: 400 # 192 or 256 bit key 400 401 t[1] ^= leftRotate1(t[0] & keyTable[48]) 401 402 t[0] ^= t[1]  keyTable[49] … … 410 411 411 412 412 def DecryptBlock(keyBitLength, cipherText, keyTable): 413 '''EncryptBlock(keyBitLength, plainText, keyTable) 414 415 keyBitLength: 128 or 192 or 256 413 def DecryptBlock(cipherText, keyTable): 414 '''EncryptBlock(plainText, keyTable) 415 416 416 cipherText: 16 bytes string 417 417 keyTable: key generated by Ekeygen() … … 420 420 t = list(struct.unpack('!IIII', cipherText)) 421 421 422 if keyBitLength == 128: 422 if len(keyTable) == 52: 423 # 128 bit key 423 424 t = map(lambda a,b:a^b, t, keyTable[48:52]) 424 425 else: 426 # 192 or 256 bit key 425 427 t = map(lambda a,b:a^b, t, keyTable[64:68]) 426 428 feistel2(t, keyTable[60:64]) … … 455 457 456 458 if __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") 460 462 461 463 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) 468 470 469 471 assert key128 == ( 
camellia/trunk/speed.py
r7 r9 10 10 key = sys.argv[1] 11 11 12 keylen = len(key) * 8 13 assert keylen in (128, 192, 256), "Key string should be 16, 24, 32 charaters." 12 assert len(key) in (16, 24, 32), "Key string should be 16, 24, 32 charaters." 14 13 15 keytable = camellia.Ekeygen(key len, key)14 keytable = camellia.Ekeygen(key) 16 15 17 16 for x in xrange(100000): 18 camellia.EncryptBlock( keylen,plain, keytable)17 camellia.EncryptBlock(plain, keytable) 
camellia/trunk/t_camellia.py
r5 r9 20 20 if op == 'K': 21 21 keyid = id 22 keylen = len(line) * 8 23 keytable = camellia.Ekeygen(keylen, line) 22 keytable = camellia.Ekeygen(line) 24 23 25 24 elif op == 'P': … … 29 28 elif op == 'C': 30 29 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) 33 32 34 33 print 'ok'
Note: See TracChangeset
for help on using the changeset viewer.