Skip to content

Commit a5d51c8

Browse files
committed
added some enhancements useful when debugging
1 parent ff7343c commit a5d51c8

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

ff3/ff3.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,15 @@ def __init__(self, key, tweak, radix=10, ):
9090
else:
9191
self.alphabet = None
9292

93+
# logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
94+
9395
# Calculate range of supported message lengths [minLen..maxLen]
9496
# per revised spec, radix^minLength >= 1,000,000.
9597
self.minLen = math.ceil(math.log(FF3Cipher.DOMAIN_MIN) / math.log(radix))
9698

9799
# We simplify the specs log[radix](2^96) to 96/log2(radix) using the log base
98100
# change rule
99101
self.maxLen = 2 * math.floor(96/math.log2(radix))
100-
101102
klen = len(keybytes)
102103

103104
# Check if the key is 128, 192, or 256 bits = 16, 24, or 32 bytes
@@ -188,7 +189,6 @@ def encrypt_with_tweak(self, plaintext, tweak):
188189
f" or 64 bits")
189190

190191
# Todo: Check message is in current radix
191-
192192
# Calculate split point
193193
u = math.ceil(n / 2)
194194
v = n - u
@@ -234,7 +234,7 @@ def encrypt_with_tweak(self, plaintext, tweak):
234234
S = self.aesCipher.encrypt(bytes(revP))
235235

236236
S = reverse_string(S)
237-
# logger.debug("S: ", S.hex())
237+
# logger.debug(f"S: {S.hex()}")
238238

239239
y = int.from_bytes(S, byteorder='big')
240240

@@ -373,10 +373,12 @@ def calculate_p(i, alphabet, W, B):
373373

374374
# The remaining 12 bytes of P are for rev(B) with padding
375375

376-
BBytes = decode_int_r(B, alphabet).to_bytes(12, "big")
377-
# logger.debug(f"B: {B} BBytes: {BBytes.hex()}")
376+
val = decode_int_r(B, alphabet)
377+
BBytes = val.to_bytes(12, "big")
378+
# logger.debug(f"B: {B} val: {val} BBytes: {BBytes.hex()}")
378379

379380
P[BLOCK_SIZE - len(BBytes):] = BBytes
381+
# logger.debug(f"[round: {i}] P: {P.hex()} W: {W.hex()} ")
380382
return P
381383

382384
def calculate_tweak64_ff3_1(tweak56):
@@ -391,7 +393,8 @@ def calculate_tweak64_ff3_1(tweak56):
391393
tweak64[7] = ((tweak56[3] & 0x0F) << 4)
392394
return tweak64
393395

394-
def encode_int_r(n, alphabet, length=0):
396+
397+
def encode_int_r(n, alphabet, length=0) -> str:
395398
"""
396399
Return a string representation of a number in the given base system for 2..62
397400
@@ -419,9 +422,11 @@ def encode_int_r(n, alphabet, length=0):
419422
return x
420423

421424

422-
def decode_int_r(astring, alphabet):
425+
def decode_int_r(astring, alphabet) -> int:
423426
"""Decode a Base X encoded string into the number
424427
428+
Returns an integer
429+
425430
Arguments:
426431
- `astring`: The encoded string
427432
- `alphabet`: The alphabet to use for decoding

0 commit comments

Comments
 (0)