Skip to content

Commit a86c598

Browse files
fix: Fix the issue of "Too many arguments for this mode" in AES CTR mode (fixed #16)
1 parent f9d00dc commit a86c598

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

bkcrypto/symmetric/ciphers/aes.py

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from dataclasses import dataclass
1414

1515
from Cryptodome.Cipher import AES
16+
from Cryptodome.Util import Counter
1617

1718
from bkcrypto import constants, types
1819

@@ -54,16 +55,29 @@ class AESSymmetricCipher(base.BaseSymmetricCipher):
5455
def get_block_size(self) -> int:
5556
return self.config.key_size
5657

57-
def _encrypt(self, plaintext_bytes: bytes, encryption_metadata: base.EncryptionMetadata) -> bytes:
58-
58+
def init_ctx(self, encryption_metadata: base.EncryptionMetadata):
5959
mode_init_args: typing.List[bytes] = []
60-
if self.config.enable_iv:
61-
mode_init_args.append(encryption_metadata.iv)
60+
mode_init_kwargs: typing.Dict[str : typing.Any] = {}
6261

63-
cipher_ctx = AES.new(self.config.key, self.config.mode_class, *mode_init_args)
62+
if self.config.enable_iv:
63+
if self.config.mode == constants.SymmetricMode.CTR:
64+
# Size of the counter block must match block size
65+
mode_init_kwargs["counter"] = Counter.new(
66+
self.get_block_size() * 8, initial_value=int.from_bytes(encryption_metadata.iv, byteorder="big")
67+
)
68+
else:
69+
mode_init_args.append(encryption_metadata.iv)
70+
71+
cipher_ctx = AES.new(self.config.key, self.config.mode_class, *mode_init_args, **mode_init_kwargs)
6472
if self.config.enable_aad:
6573
cipher_ctx.update(encryption_metadata.aad)
6674

75+
return cipher_ctx
76+
77+
def _encrypt(self, plaintext_bytes: bytes, encryption_metadata: base.EncryptionMetadata) -> bytes:
78+
79+
cipher_ctx = self.init_ctx(encryption_metadata)
80+
6781
if self.config.mode == constants.SymmetricMode.GCM:
6882
ciphertext_bytes, tag = cipher_ctx.encrypt_and_digest(plaintext_bytes)
6983
encryption_metadata.tag = tag
@@ -73,13 +87,7 @@ def _encrypt(self, plaintext_bytes: bytes, encryption_metadata: base.EncryptionM
7387

7488
def _decrypt(self, ciphertext_bytes: bytes, encryption_metadata: base.EncryptionMetadata) -> bytes:
7589

76-
mode_init_args: typing.List[bytes] = []
77-
if self.config.enable_iv:
78-
mode_init_args.append(encryption_metadata.iv)
79-
80-
cipher_ctx = AES.new(self.config.key, self.config.mode_class, *mode_init_args)
81-
if self.config.enable_aad:
82-
cipher_ctx.update(encryption_metadata.aad)
90+
cipher_ctx = self.init_ctx(encryption_metadata)
8391

8492
if self.config.mode == constants.SymmetricMode.GCM:
8593
plaintext_bytes: bytes = cipher_ctx.decrypt_and_verify(ciphertext_bytes, encryption_metadata.tag)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "bk-crypto-python-sdk"
3-
version = "1.0.3"
3+
version = "1.0.4"
44
description = "bk-crypto-python-sdk is a lightweight cryptography toolkit for Python applications based on Cryptodome / tongsuopy and other encryption libraries."
55
authors = ["TencentBlueKing <[email protected]>"]
66
readme = "readme.md"

release.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,10 @@
3131
### Feature
3232

3333
* [ Feature ] Support configuring AsymmetricCipherManager through Django settings ([#14](https://github.com/TencentBlueKing/crypto-python-sdk/issues/14))
34+
35+
36+
## 1.0.4 - 2023-07-20
37+
38+
### Fixed
39+
40+
* [ Fixed ] Fix the issue of "Too many arguments for this mode" in AES CTR mode ([#16](https://github.com/TencentBlueKing/crypto-python-sdk/issues/16))

0 commit comments

Comments
 (0)