|
|
|
@ -1,5 +1,9 @@
|
|
|
|
|
#!/usr/bin/env python
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
"""
|
|
|
|
|
src/pyelliptic/cipher.py
|
|
|
|
|
========================
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
# Copyright (C) 2011 Yann GUIBET <yannguibet@gmail.com>
|
|
|
|
|
# See LICENSE for details.
|
|
|
|
@ -7,7 +11,8 @@
|
|
|
|
|
from openssl import OpenSSL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Cipher:
|
|
|
|
|
# pylint: disable=redefined-builtin
|
|
|
|
|
class Cipher(object):
|
|
|
|
|
"""
|
|
|
|
|
Symmetric encryption
|
|
|
|
|
|
|
|
|
@ -44,30 +49,34 @@ class Cipher:
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def get_blocksize(ciphername):
|
|
|
|
|
"""This Method returns cipher blocksize"""
|
|
|
|
|
cipher = OpenSSL.get_cipher(ciphername)
|
|
|
|
|
return cipher.get_blocksize()
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
def gen_IV(ciphername):
|
|
|
|
|
"""Generate random initialization vector"""
|
|
|
|
|
cipher = OpenSSL.get_cipher(ciphername)
|
|
|
|
|
return OpenSSL.rand(cipher.get_blocksize())
|
|
|
|
|
|
|
|
|
|
def update(self, input):
|
|
|
|
|
"""Update result with more data"""
|
|
|
|
|
i = OpenSSL.c_int(0)
|
|
|
|
|
buffer = OpenSSL.malloc(b"", len(input) + self.cipher.get_blocksize())
|
|
|
|
|
inp = OpenSSL.malloc(input, len(input))
|
|
|
|
|
if OpenSSL.EVP_CipherUpdate(self.ctx, OpenSSL.byref(buffer),
|
|
|
|
|
OpenSSL.byref(i), inp, len(input)) == 0:
|
|
|
|
|
raise Exception("[OpenSSL] EVP_CipherUpdate FAIL ...")
|
|
|
|
|
return buffer.raw[0:i.value]
|
|
|
|
|
return buffer.raw[0:i.value] # pylint: disable=invalid-slice-index
|
|
|
|
|
|
|
|
|
|
def final(self):
|
|
|
|
|
"""Returning the final value"""
|
|
|
|
|
i = OpenSSL.c_int(0)
|
|
|
|
|
buffer = OpenSSL.malloc(b"", self.cipher.get_blocksize())
|
|
|
|
|
if (OpenSSL.EVP_CipherFinal_ex(self.ctx, OpenSSL.byref(buffer),
|
|
|
|
|
OpenSSL.byref(i))) == 0:
|
|
|
|
|
raise Exception("[OpenSSL] EVP_CipherFinal_ex FAIL ...")
|
|
|
|
|
return buffer.raw[0:i.value]
|
|
|
|
|
return buffer.raw[0:i.value] # pylint: disable=invalid-slice-index
|
|
|
|
|
|
|
|
|
|
def ciphering(self, input):
|
|
|
|
|
"""
|
|
|
|
@ -77,6 +86,7 @@ class Cipher:
|
|
|
|
|
return buff + self.final()
|
|
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
|
# pylint: disable=protected-access
|
|
|
|
|
if OpenSSL._hexversion > 0x10100000 and not OpenSSL._libreSSL:
|
|
|
|
|
OpenSSL.EVP_CIPHER_CTX_reset(self.ctx)
|
|
|
|
|
else:
|
|
|
|
|