X-Git-Url: http://wagner.pp.ru/gitweb/?a=blobdiff_plain;f=ctypescrypto%2Fcipher.py;h=367f2b937767a742d2a74b7cc1d2d501a303a722;hb=3af16f1cbc516e55cfc31af5dee0bc367a9ecbae;hp=f4a2d47893000cd409833b001ae032ed39333670;hpb=e8dffb7c99079c721654f783dbccf274147956d3;p=oss%2Fctypescrypto.git diff --git a/ctypescrypto/cipher.py b/ctypescrypto/cipher.py index f4a2d47..367f2b9 100644 --- a/ctypescrypto/cipher.py +++ b/ctypescrypto/cipher.py @@ -1,3 +1,7 @@ +""" +access to symmetric ciphers from libcrypto + +""" from ctypes import create_string_buffer,c_char_p,c_void_p,c_int,c_long,byref,POINTER from ctypescrypto import libcrypto from ctypescrypto.exception import LibCryptoError @@ -38,7 +42,7 @@ class CipherType: """ self.cipher = libcrypto.EVP_get_cipherbyname(cipher_name) if self.cipher is None: - raise CipherError, "Unknown cipher: %s" % cipher_name + raise CipherError("Unknown cipher: %s" % cipher_name) def __del__(self): pass @@ -109,7 +113,7 @@ class Cipher: iv_ptr = c_char_p(iv) self.ctx = libcrypto.EVP_CIPHER_CTX_new() if self.ctx == 0: - raise CipherError, "Unable to create cipher context" + raise CipherError("Unable to create cipher context") self.encrypt = encrypt if encrypt: enc = 1 @@ -121,17 +125,19 @@ class Cipher: if len(key) != cipher_type.key_length(): if (cipher_type.flags() & 8) != 0: # Variable key length cipher. - result = libcrypto.EVP_CipherInit_ex(self.ctx,cipher_type.cipher,None,None,None,c_int(enc)) + result = libcrypto.EVP_CipherInit_ex(self.ctx, cipher_type.cipher, None, None, None, c_int(enc)) result=libcrypto.EVP_CIPHER_CTX_set_key_length(self.ctx,len(key)) if result == 0: self._clean_ctx() raise CipherError("Unable to set key length") + result = libcrypto.EVP_CipherInit_ex(self.ctx, None, None, key_ptr, iv_ptr, c_int(enc)) else: raise ValueError("Invalid key length for this algorithm") - result = libcrypto.EVP_CipherInit_ex(self.ctx, cipher_type.cipher, None, key_ptr, iv_ptr, c_int(enc)) + else: + result = libcrypto.EVP_CipherInit_ex(self.ctx, cipher_type.cipher, None, key_ptr, iv_ptr, c_int(enc)) if result == 0: self._clean_ctx() - raise CipherError, "Unable to initialize cipher" + raise CipherError("Unable to initialize cipher") self.cipher_type = cipher_type self.block_size = self.cipher_type.block_size() self.cipher_finalized = False @@ -162,9 +168,9 @@ class Cipher: called """ if self.cipher_finalized : - raise CipherError, "No updates allowed" + raise CipherError("No updates allowed") if type(data) != type(""): - raise TypeError, "A string is expected" + raise TypeError("A string is expected") if len(data) <= 0: return "" outbuf=create_string_buffer(self.block_size+len(data)) @@ -184,14 +190,14 @@ class Cipher: state, they would be processed and returned. """ if self.cipher_finalized : - raise CipherError, "Cipher operation is already completed" + raise CipherError("Cipher operation is already completed") outbuf=create_string_buffer(self.block_size) self.cipher_finalized = True - outlen=c_int() + outlen=c_int(0) result = libcrypto.EVP_CipherFinal_ex(self.ctx,outbuf , byref(outlen)) if result == 0: self._clean_ctx() - raise CipherError, "Unable to finalize cipher" + raise CipherError("Unable to finalize cipher") if outlen.value>0: return outbuf.raw[:outlen.value] else: