Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Camellia encryption #48

Merged
merged 3 commits into from
Dec 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions docs/crypto.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,48 @@ Supported modes: ECB.
.. autofunction:: malduck.blowfish.ecb.encrypt
.. autofunction:: malduck.blowfish.ecb.decrypt

Camellia
--------------

Camellia block cipher.

Supported modes: ECB, CBC, CTR, CFB, OFB.

.. code-block:: python

from malduck import camellia

key = b'A'*16
iv = b'B'*16
plaintext = b'data'*16
ciphertext = camellia.ecb.encrypt(key, iv, plaintext)


Camellia-ECB mode
~~~~~~~~~~~~
.. autofunction:: malduck.camellia.ecb.encrypt
.. autofunction:: malduck.camellia.ecb.decrypt

Camellia-CBC mode
~~~~~~~~~~~~
.. autofunction:: malduck.camellia.cbc.encrypt
.. autofunction:: malduck.camellia.cbc.decrypt

Camellia-CTR mode
~~~~~~~~~~~~
.. autofunction:: malduck.camellia.ctr.encrypt
.. autofunction:: malduck.camellia.ctr.decrypt

Camellia-CFB mode
~~~~~~~~~~~~
.. autofunction:: malduck.camellia.cfb.encrypt
.. autofunction:: malduck.camellia.cfb.decrypt

Camellia-OFB mode
~~~~~~~~~~~~
.. autofunction:: malduck.camellia.ofb.encrypt
.. autofunction:: malduck.camellia.ofb.decrypt

ChaCha20
--------

Expand Down
2 changes: 2 additions & 0 deletions malduck/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

from .crypto import (
aes,
camellia,
blowfish,
chacha20,
des3,
Expand Down Expand Up @@ -135,6 +136,7 @@
"lznt1",
# crypto
"aes",
"camellia",
"blowfish",
"chacha20",
"des3",
Expand Down
2 changes: 2 additions & 0 deletions malduck/crypto/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .aes import aes
from .camellia import camellia
from .blowfish import blowfish
from .chacha20 import chacha20
from .des3 import des3
Expand All @@ -11,6 +12,7 @@

__all__ = [
"aes",
"camellia",
"blowfish",
"chacha20",
"des3",
Expand Down
201 changes: 201 additions & 0 deletions malduck/crypto/camellia.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes


__all__ = ["camellia"]


class CamelliaCbc:
def encrypt(self, key: bytes, iv: bytes, data: bytes) -> bytes:
"""
Encrypts buffer using Camellia algorithm in CBC mode.

:param key: Cryptographic key (128, 192 or 256 bits)
:type key: bytes
:param iv: Initialization vector
:type iv: bytes
:param data: Buffer to be encrypted
:type data: bytes
:return: Encrypted data
:rtype: bytes
"""
algo = algorithms.Camellia(key)
cipher = Cipher(algo, modes.CBC(iv))
enc = cipher.encryptor()
return enc.update(data) + enc.finalize()

def decrypt(self, key: bytes, iv: bytes, data: bytes) -> bytes:
"""
Decrypts buffer using Camellia algorithm in CBC mode.

:param key: Cryptographic key (128, 192 or 256 bits)
:type key: bytes
:param iv: Initialization vector
:type iv: bytes
:param data: Buffer to be decrypted
:type data: bytes
:return: Decrypted data
:rtype: bytes
"""
algo = algorithms.Camellia(key)
cipher = Cipher(algo, modes.CBC(iv))
dec = cipher.decryptor()
return dec.update(data) + dec.finalize()


class CamelliaCfb:
def encrypt(self, key: bytes, iv: bytes, data: bytes) -> bytes:
"""
Encrypts buffer using Camellia algorithm in CFB mode.

:param key: Cryptographic key (128, 192 or 256 bits)
:type key: bytes
:param iv: Initialization vector
:type iv: bytes
:param data: Buffer to be encrypted
:type data: bytes
:return: Encrypted data
:rtype: bytes
"""
algo = algorithms.Camellia(key)
cipher = Cipher(algo, modes.CFB(iv))
enc = cipher.encryptor()
return enc.update(data) + enc.finalize()

def decrypt(self, key: bytes, iv: bytes, data: bytes) -> bytes:
"""
Decrypts buffer using Camellia algorithm in CFB mode.

:param key: Cryptographic key (128, 192 or 256 bits)
:type key: bytes
:param iv: Initialization vector
:type iv: bytes
:param data: Buffer to be decrypted
:type data: bytes
:return: Decrypted data
:rtype: bytes
"""
algo = algorithms.Camellia(key)
cipher = Cipher(algo, modes.CFB(iv))
dec = cipher.decryptor()
return dec.update(data) + dec.finalize()


class CamelliaOfb:
def encrypt(self, key: bytes, iv: bytes, data: bytes) -> bytes:
"""
Encrypts buffer using Camellia algorithm in OFB mode.

:param key: Cryptographic key (128, 192 or 256 bits)
:type key: bytes
:param iv: Initialization vector
:type iv: bytes
:param data: Buffer to be encrypted
:type data: bytes
:return: Encrypted data
:rtype: bytes
"""
algo = algorithms.Camellia(key)
cipher = Cipher(algo, modes.OFB(iv))
enc = cipher.encryptor()
return enc.update(data) + enc.finalize()

def decrypt(self, key: bytes, iv: bytes, data: bytes) -> bytes:
"""
Decrypts buffer using Camellia algorithm in OFB mode.

:param key: Cryptographic key (128, 192 or 256 bits)
:type key: bytes
:param iv: Initialization vector
:type iv: bytes
:param data: Buffer to be decrypted
:type data: bytes
:return: Decrypted data
:rtype: bytes
"""
algo = algorithms.Camellia(key)
cipher = Cipher(algo, modes.OFB(iv))
dec = cipher.decryptor()
return dec.update(data) + dec.finalize()


class CamelliaEcb:
def encrypt(self, key: bytes, data: bytes) -> bytes:
"""
Encrypts buffer using Camellia algorithm in ECB mode.

:param key: Cryptographic key (128, 192 or 256 bits)
:type key: bytes
:param data: Buffer to be encrypted
:type data: bytes
:return: Encrypted data
:rtype: bytes
"""
algo = algorithms.Camellia(key)
cipher = Cipher(algo, modes.ECB())
enc = cipher.encryptor()
return enc.update(data) + enc.finalize()

def decrypt(self, key: bytes, data: bytes) -> bytes:
"""
Decrypts buffer using Camellia algorithm in ECB mode.

:param key: Cryptographic key (128, 192 or 256 bits)
:type key: bytes
:param data: Buffer to be decrypted
:type data: bytes
:return: Decrypted data
:rtype: bytes
"""
algo = algorithms.Camellia(key)
cipher = Cipher(algo, modes.ECB())
dec = cipher.decryptor()
return dec.update(data) + dec.finalize()


class CamelliaCtr:
def encrypt(self, key: bytes, nonce: bytes, data: bytes) -> bytes:
"""
Encrypts buffer using Camellia algorithm in CTR mode.

:param key: Cryptographic key (128, 192 or 256 bits)
:type key: bytes
:param nonce: Initial counter value, big-endian encoded
:type nonce: bytes
:param data: Buffer to be encrypted
:type data: bytes
:return: Encrypted data
:rtype: bytes
"""
algo = algorithms.Camellia(key)
cipher = Cipher(algo, modes.CTR(nonce))
enc = cipher.encryptor()
return enc.update(data) + enc.finalize()

def decrypt(self, key: bytes, nonce: bytes, data: bytes) -> bytes:
"""
Decrypts buffer using Camellia algorithm in CTR mode.

:param key: Cryptographic key (128, 192 or 256 bits)
:type key: bytes
:param nonce: Initial counter value, big-endian encoded
:type nonce: bytes
:param data: Buffer to be decrypted
:type data: bytes
:return: Decrypted data
:rtype: bytes
"""
algo = algorithms.Camellia(key)
cipher = Cipher(algo, modes.CTR(nonce))
dec = cipher.decryptor()
return dec.update(data) + dec.finalize()


class Camellia:
cbc = CamelliaCbc()
ecb = CamelliaEcb()
ctr = CamelliaCtr()
cfb = CamelliaCfb()
ofb = CamelliaOfb()


camellia = Camellia()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ pycryptodomex>=3.8.2
capstone>=4.0.1
yara-python
typing-extensions>=3.7.4.2
cryptography>=3.1
Loading