11#!/usr/bin/env python3
2+ r"""Camellia implementation for Python.
3+
4+ Example:
5+
6+ >>> import camellia
7+ >>> cipher = camellia.new(b'\x80'+b'\x00*15, mode=camellia.MODE_ECB)
8+ >>> cipher.encrypt(b'\x00'*16)
9+ b'l"\x7ft\x93\x19\xa3\xaa}\xa25\xa9\xbb\xa0Z,'
10+
11+ """
212
313try :
414 from ._camellia import lib , ffi
2030#: CTR mode of operation
2131MODE_CTR = 6
2232
23- #: All currently supported blockcipher modes of operation
24- SUPPORTED_MODES = [MODE_ECB , MODE_CBC , MODE_CTR ]
2533
2634if sys .version_info .major <= 2 :
2735 b = lambda b : "" .join (map (chr , b ))
@@ -51,6 +59,7 @@ def Camellia_Ekeygen(rawKey):
5159 return keytable
5260
5361def Camellia_Encrypt (keyLength , keytable , plainText ):
62+ r"""Encrypt a plaintext block by given arguments."""
5463 if keyLength not in [128 , 192 , 256 ]:
5564 raise ValueError ("Invalid key length, "
5665 "it must be 16, 24 or 32 bytes long!" )
@@ -67,6 +76,7 @@ def Camellia_Encrypt(keyLength, keytable, plainText):
6776
6877
6978def Camellia_Decrypt (keyLength , keytable , cipherText ):
79+ r"""Decrypt a plaintext block by given arguments."""
7080 if keyLength not in [128 , 192 , 256 ]:
7181 raise ValueError ("Invalid key length, "
7282 "it must be 16, 24 or 32 bytes long!" )
@@ -82,15 +92,14 @@ def Camellia_Decrypt(keyLength, keytable, cipherText):
8292 return b (out )[:- 1 ]
8393
8494
85- def new (key , mode = MODE_ECB , ** kwargs ):
95+ def new (key , mode , ** kwargs ):
8696 """Create an "CamelliaCipher" object.
87- The default mode is ECB.
8897
8998 :param key: The key for encrytion/decryption. Must be 16/24/32 in length.
9099 :type key: bytestring
91100
92- :param mode: Mode of operation, only ECB (0) and CBC (1) are supported .
93- :type mode: int, on of MODE_* constants
101+ :param mode: Mode of operation.
102+ :type mode: int, one of MODE_* constants
94103
95104 :param IV: Initialisation vector for CBC/CFB/OFB, must be 16 in length.
96105 :type IV: bytestring
@@ -107,55 +116,29 @@ def new(key, mode=MODE_ECB, **kwargs):
107116block_size = 16
108117
109118class CamelliaCipher (PEP272Cipher ):
110- """
111- The CamelliaCipher object.
112- """
119+ """The CamelliaCipher object."""
113120
114121 #: block size of the camellia cipher
115122 block_size = 16
116123
117124 def __init__ (self , key , mode , ** kwargs ):
118- """
119- Constructer of Cipher class. See :func:`camellia.new`.
120- """
121-
125+ """Constructer of Cipher class. See :func:`camellia.new`."""
122126 keytable = Camellia_Ekeygen (key )
123127 self .key_length = len (key )* 8
124-
125128
126129 PEP272Cipher .__init__ (self , keytable , mode , ** kwargs )
127130
128131 def encrypt_block (self , key , block , ** kwargs ):
132+ """Encrypt a single block with camellia."""
129133 return Camellia_Encrypt (self .key_length , key , block )
130134
131135 def decrypt_block (self , key , block , ** kwargs ):
136+ """Decrypt a single block with camellia."""
132137 return Camellia_Decrypt (self .key_length , key , block )
133138
134139
135-
136- CamelliaCipher .encrypt .__doc__ = """\
137- Encrypt string.
138-
139- :param string:
140- The data to encrypt.
141- For the most modes of operation it must be a multiple
142- of 16 in length.
143- :type string: bytestring
144- """
145-
146-
147- CamelliaCipher .decrypt .__doc__ = """\
148- Decrypt string.
149-
150- :param string:
151- The data to decrypt.
152- For the most modes of operation it must be a multiple
153- of 16 in length.
154- :type string: bytestring
155- """
156-
157-
158140def test (v = True ):
141+ """Small selftest of camellia with a single test vector."""
159142 key = b"80000000000000000000000000000000"
160143 plain = b"00000000000000000000000000000000"
161144 cipher = b"6C227F749319A3AA7DA235A9BBA05A2C"
@@ -172,7 +155,7 @@ def test(v=True):
172155 print ("Required:\t cipher=%s" % cipher .decode ())
173156 return "failed"
174157 else :
175- raise
158+ raise Exception ( "Camellia does not work as expected!" )
176159
177160 dc = c .decrypt (ec )
178161
@@ -182,9 +165,12 @@ def test(v=True):
182165 print ("Required:\t cipher=%s" % plain .decode ())
183166 return "failed"
184167 else :
185- raise
168+ raise Exception ( "Camellia does not work as expected!" )
186169
187170 return "passed"
188171
172+
173+ assert test (False ) == "passed"
174+
189175if __name__ == "__main__" :
190176 print ("Test: " + test ())
0 commit comments