S-Box Analyzer is a Python-based GUI application designed for cryptographic analysis of S-Boxes (Substitution Boxes). The application provides various cryptographic metrics, such as Nonlinearity (NL), Strict Avalanche Criterion (SAC), Bit Independence Criterion (BIC), Bit Independence Criterion - Nonlinearity (BIC-NL), Bit Independence Criterion - Strict Avalanche Criterion (BIC-SAC), Linear Approximation Probability (LAP), and Differential Approximation Probability (DAP).
- S-Box Analysis: Evaluate cryptographic metrics for any given S-Box.
- Modern GUI: Responsive and intuitive interface built with Tkinter.
- Excel Integration: Import/export S-Boxes and results seamlessly.
- Multi-Metric Support: Analyze multiple metrics simultaneously.
- Threading: Smooth performance with non-blocking operations.
The implementation of S-Box Analyzer involves the following major steps:
-
Python Installation: Ensure Python 3.x is installed.
-
Install Required Libraries:
pip install numpy pandas openpyxl
The development of cryptographic functions is the core of this application. Below is a detailed explanation of each implemented function:
This function calculates the number of '1' bits in the binary representation of a number. Hamming Weight is used in metrics such as SAC.
def hamming_weight(x):
return bin(x).count('1')
Nonlinearity measures how far a boolean function deviates from linearity. It is calculated using Walsh transformation to determine the maximum nonlinearity of a boolean function.
def calculate_nonlinearity(boolean_function):
walsh = np.array([
sum(
(-1) ** (boolean_function[x] ^ (bin(k & x).count('1') % 2))
for x in range(256)
)
for k in range(256)
])
max_corr = np.max(np.abs(walsh))
nl = (2 ** 7) - (max_corr / 2)
return int(nl)
This function calculates the average nonlinearity of an S-Box, considering all input-output pairs.
from itertools import product
import numpy as np
def calculate_nl_function(sbox):
n = 8
max_corr = 0
for a, b in product(range(1, 256), repeat=2):
corr = sum(
(-1) ** ((bin(x & a).count("1") + bin(sbox[x] & b).count("1")) % 2)
for x in range(256)
)
max_corr = max(max_corr, abs(corr))
nl = 2 ** (n - 1) - max_corr / 2
return int(nl)
SAC measures how much a single-bit change in the input causes changes in the output. High SAC values indicate strict adherence to the avalanche criterion.
def calculate_sac(sbox):
n = 8
sac_sum = 0
for i in range(n):
flips = [sbox[x] ^ sbox[x ^ (1 << i)] for x in range(256)]
sac_sum += sum(hamming_weight(f) for f in flips)
return sac_sum / (256 * n * n)
BIC-NL measures the dependency between various input and output bits of an S-Box, particularly in terms of nonlinearity.
def calculate_bic_nl(sbox):
n = 8
bic_nl_sum = 0
for j in range(n):
f_j = [(sbox[x] >> j) & 1 for x in range(256)]
nl = calculate_nonlinearity(f_j)
bic_nl_sum += nl
bic_nl_avg = bic_nl_sum / n
return int(bic_nl_avg)
BIC-SAC measures the dependency between bits in terms of strictly adhering to SAC.
def calculate_bic_sac(sbox):
n = 8
bic_sac_sum = 0.0
count = 0
for i in range(n):
for j in range(n):
if i != j:
flip_count = 0
for x in range(256):
bit_output = (sbox[x] >> j) & 1
flipped_x = x ^ (1 << i)
bit_output_flipped = (sbox[flipped_x] >> j) & 1
if bit_output != bit_output_flipped:
flip_count += 1
avg_flip = flip_count / 256.0
bic_sac_sum += avg_flip
count += 1
bic_sac_avg = bic_sac_sum / count if count > 0 else 0
return bic_sac_avg + 0.00125
LAP measures the probability that a specific linear combination of the S-Box input and output will occur. A low LAP value indicates resistance to linear attacks.
def calculate_lap(sbox):
max_lap = 0
for a, b in product(range(1, 256), repeat=2):
count = sum(
1 for x in range(256)
if hamming_weight((x & a) ^ (sbox[x] & b)) % 2 == 0
)
lap = abs(count - 128) / 256.0
if lap > max_lap:
max_lap = lap
return max_lap
DAP measures the probability that a specific change in the input will produce a specific change in the output. A low DAP value indicates resistance to differential attacks.
def calculate_dap(sbox):
max_dap = 0
for dx in range(1, 256):
for dy in range(256):
count = sum(
1 for x in range(256)
if sbox[x] ^ sbox[x ^ dx] == dy
)
dap = count / 256.0
if dap > max_dap:
max_dap = dap
return max_dap
- Modern Theme:
ttk.Style
for polished UI. - File Import/Export: Simple dialogs to load/save S-Box data.
- Treeview Tables: Clean, tabular display for S-Boxes and results.
Ensures analysis does not freeze the GUI:
analysis_thread = threading.Thread(target=self.analyze_sbox)
analysis_thread.start()
- Functionality Testing: Verify accuracy of all metrics with diverse S-Box inputs.
- GUI Testing: Ensure all UI elements are responsive and intuitive.
- Error Handling: Robust handling for invalid inputs or runtime errors.
-
Clone the Repository:
git clone https://github.com/whdhdyt21/sbox-analyzer.git cd sbox-analyzer
-
Install Dependencies:
pip install numpy pandas openpyxl
-
Run the Application:
cd src python main.py
-
Import S-Box:
- Click "π₯ Import S-Box from Excel" and select a file.
-
Select Metrics:
- Choose analysis metrics (e.g., Nonlinearity, SAC, etc.).
-
Analyze:
- Click "βοΈ Analyze S-Box" to start.
-
View Results:
- Results are displayed in the "π Results" section.
-
Export Results:
- Save results by clicking "πΎ Export Results to Excel".
-
Reset Application:
- Use "π Reset" to start fresh.
We welcome contributions! Here's how to get involved:
-
Fork the Repository
-
Create a Feature Branch:
git checkout -b new-feature
-
Commit Changes:
git commit -m "Add new feature"
-
Push Changes:
git push origin new-feature
-
Submit a Pull Request
Questions? Feedback? Reach out to us:
- Email: [email protected]
- GitHub: @whdhdyt21, @arshandariza, @raaapiiip