mode constants are only in blockcipher
[python-cryptoplus.git] / src / Cipher / RC5.py
blobd8e761f3242c7449df140c5d8aa915d2bb2ed951
1 from blockcipher import *
2 try:
3 import Crypto.Cipher.RC5
4 except ImportError:
5 print "Crypto.Cipher.RC5 isn't available. You're probably using the Debian pycrypto version. Install the original pycrypto for RC5."
6 raise
8 def new(key,mode=MODE_ECB,IV=None,counter=None,rounds=12,word_size=32):
9 """Create a new cipher object
11 RC5 using pycrypto for algo and pycryptoplus for ciphermode
13 new(key,mode=MODE_ECB,IV=None,counter=None):
14 key = raw string containing the keys
15 mode = python_AES.MODE_ECB/CBC/CFB/OFB/CTR/CMAC, default is ECB
16 IV = IV as a raw string
17 -> only needed for CBC mode
18 counter = counter object (CryptoPlus.Util.util.Counter)
19 -> only needed for CTR mode
21 https://www.cosic.esat.kuleuven.be/nessie/testvectors/
22 -----------------------------------------
23 >>> from CryptoPlus.Cipher import RC5
24 >>> key = "00000000000000000000000000000000".decode('hex')
25 >>> plaintext = "0000000000000000".decode('hex')
26 >>> rounds = 12
27 >>> cipher = RC5.new(key,RC5.MODE_ECB,rounds=rounds)
28 >>> cipher.encrypt(plaintext).encode('hex')
29 '21a5dbee154b8f6d'
30 """
31 return RC5(key,mode,IV,counter,rounds,word_size)
33 class RC5(BlockCipher):
34 def __init__(self,key,mode,IV,counter,rounds,word_size):
35 if mode == MODE_XTS:
36 #XTS implementation only works with blocksizes of 16 bytes
37 assert type(key) is tuple
38 self.cipher = Crypto.Cipher.RC5.new(key[0],rounds=rounds,word_size=word_size)
39 self.cipher2 = Crypto.Cipher.RC5.new(key[1],rounds=rounds,word_size=word_size)
40 assert self.cipher.block_size == 16
41 elif mode == MODE_CMAC:
42 #CMAC implementation only supports blocksizes of 8 and 16 bytes
43 self.cipher = Crypto.Cipher.RC5.new(key,rounds=rounds,word_size=word_size)
44 assert self.cipher.block_size in (8,16)
45 else:
46 self.cipher = Crypto.Cipher.RC5.new(key,rounds=rounds,word_size=word_size)
47 self.blocksize = self.cipher.block_size
48 BlockCipher.__init__(self,key,mode,IV,counter)
50 def _test():
51 import doctest
52 doctest.testmod()
54 if __name__ == "__main__":
55 _test()