-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] Default-AES-Implementierung kann optional installiert werden.
Eine auf cryptography basierende AES-Implementierung kann optional (`bec2format[aes]`) installiert werden.
- Loading branch information
1 parent
af67776
commit 8849db9
Showing
4 changed files
with
184 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,3 +36,8 @@ | |
FormatError, | ||
UnsupportedLegacyFirmwareError, | ||
) | ||
|
||
try: | ||
from .extras import aes | ||
except ImportError: | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes | ||
|
||
from bec2format import AES128, register_AES128 | ||
|
||
|
||
@register_AES128 | ||
class AES128Proxy(AES128): | ||
def __init__(self, key: bytes, iv: bytes) -> None: | ||
super().__init__(key, iv) | ||
self._cipher = Cipher( | ||
algorithms.AES128(self._key), modes.CBC(self._iv or b"\x00" * 16) | ||
) | ||
|
||
def encrypt(self, data: bytes) -> bytes: | ||
pad_length = -len(data) % AES128.BLOCK_SIZE | ||
pad = b"" if pad_length == 0 else bytes([0x00] * pad_length) | ||
encryptor = self._cipher.encryptor() | ||
return encryptor.update(data + pad) + encryptor.finalize() | ||
|
||
def decrypt(self, data: bytes) -> bytes: | ||
decryptor = self._cipher.decryptor() | ||
plaintext_padded = decryptor.update(data) + decryptor.finalize() | ||
return plaintext_padded.rstrip(b"\0") | ||
|
||
def mac(self, data: bytes) -> bytes: | ||
return self.encrypt(data)[-16:] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters