feat: add type lint and review some code
This commit is contained in:
parent
8d7133eb5a
commit
72cdedfcdb
28
sm4Drbg.py
28
sm4Drbg.py
@ -22,13 +22,13 @@ class SM4_RNG:
|
||||
|
||||
self.blocklen = 16
|
||||
|
||||
self.seed_material = ""
|
||||
self.seed_material = b""
|
||||
self.sm4 = CryptSM4()
|
||||
self.SM4_RNG_Instantiate(personalization_string, nonce)
|
||||
|
||||
def SM4_RNG_Instantiate(
|
||||
self, personalization_string: bytes = b"", nonce: bytes = b""
|
||||
):
|
||||
) -> None:
|
||||
self.min_entropy = self.min_entropy_input_length
|
||||
self.entropy_input = secrets.token_bytes(self.min_entropy)
|
||||
self.seed_material = self.entropy_input + nonce + personalization_string
|
||||
@ -39,12 +39,12 @@ class SM4_RNG:
|
||||
self.reseed_counter = 1
|
||||
self.last_reseed_time = int(time.time())
|
||||
|
||||
def SM4_RNG_Update(self, seed_material, Key, V):
|
||||
def SM4_RNG_Update(self, seed_material: bytes, Key: bytes, V: bytes) -> None:
|
||||
temp = b""
|
||||
self.sm4.set_key(Key, SM4_ENCRYPT)
|
||||
while len(temp) < self.seedlen:
|
||||
V = (int.from_bytes(V, "big") + 1) % (1 << self.blocklen)
|
||||
self.output_block = self.sm4.crypt_ecb(V.to_bytes(self.blocklen, "big"))
|
||||
V_int = (int.from_bytes(V, "big") + 1) % (1 << self.blocklen)
|
||||
self.output_block = self.sm4.crypt_ecb(V_int.to_bytes(self.blocklen, "big"))
|
||||
temp = temp + self.output_block
|
||||
temp = temp[: self.seedlen]
|
||||
temp = int.from_bytes(temp, "big") ^ int.from_bytes(seed_material, "big")
|
||||
@ -52,7 +52,7 @@ class SM4_RNG:
|
||||
self.Key = temp[: self.keylen]
|
||||
self.V = temp[-self.blocklen :]
|
||||
|
||||
def SM4_df(self, input_string: bytes, number_of_bits_to_return: int):
|
||||
def SM4_df(self, input_string: bytes, number_of_bits_to_return: int) -> bytes:
|
||||
L = len(input_string)
|
||||
N = number_of_bits_to_return
|
||||
S = L.to_bytes(4, "big") + N.to_bytes(4, "big") + input_string + b"\x80"
|
||||
@ -78,11 +78,11 @@ class SM4_RNG:
|
||||
requested_bits = tmp[:number_of_bits_to_return]
|
||||
return requested_bits
|
||||
|
||||
def CBC_MAC(self, Key, data_to_MAC):
|
||||
def CBC_MAC(self, Key: bytes, data_to_MAC: bytes) -> bytes:
|
||||
self.sm4.set_key(Key, SM4_ENCRYPT)
|
||||
chaining_value = b"\x00" * self.outlen
|
||||
n = len(data_to_MAC) / self.outlen
|
||||
for i in range(int(n)):
|
||||
n = len(data_to_MAC) // self.outlen
|
||||
for i in range(n):
|
||||
input_block = int.from_bytes(chaining_value, "big") ^ int.from_bytes(
|
||||
data_to_MAC[i * self.outlen : (i + 1) * self.outlen], "big"
|
||||
)
|
||||
@ -93,7 +93,7 @@ class SM4_RNG:
|
||||
output_block = chaining_value
|
||||
return output_block
|
||||
|
||||
def SM4_RNG_Reseed(self, additional_input: bytes):
|
||||
def SM4_RNG_Reseed(self, additional_input: bytes) -> None:
|
||||
self.min_entropy = self.min_entropy_input_length
|
||||
self.entropy_input = secrets.token_bytes(self.min_entropy)
|
||||
self.seed_material = self.entropy_input + additional_input
|
||||
@ -102,8 +102,8 @@ class SM4_RNG:
|
||||
self.reseed_counter = 1
|
||||
self.last_reseed_time = int(time.time())
|
||||
|
||||
def SM4_RNG_Generate(self, requested_number_of_bits, additional_input: bytes = b""):
|
||||
length = int(requested_number_of_bits / 8)
|
||||
def SM4_RNG_Generate(self, requested_number_of_bits:int, additional_input: bytes = b"")->bytes:
|
||||
length = requested_number_of_bits // 8
|
||||
returned_bits = b""
|
||||
if (
|
||||
self.reseed_counter > self.reseed_interval_in_counter
|
||||
@ -117,8 +117,8 @@ class SM4_RNG:
|
||||
additional_input = b"\x00" * self.seedlen
|
||||
self.sm4.set_key(self.Key, SM4_ENCRYPT)
|
||||
while len(returned_bits) < length:
|
||||
self.V = int.from_bytes(self.V, "big") + 1 % (1 << self.blocklen)
|
||||
self.V = self.V.to_bytes(self.blocklen, "big")
|
||||
V_int = int.from_bytes(self.V, "big") + 1 % (1 << self.blocklen)
|
||||
self.V = V_int.to_bytes(self.blocklen, "big")
|
||||
output_block = self.sm4.crypt_ecb(self.V)
|
||||
returned_bits = returned_bits + output_block
|
||||
self.SM4_RNG_Update(additional_input, self.Key, self.V)
|
||||
|
Loading…
x
Reference in New Issue
Block a user