Skip to content

Commit

Permalink
add pikle test file (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
programmingAthlete authored Dec 15, 2023
1 parent 3a2cf56 commit 4f154a2
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 7 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ src/crypto_pkg/ciphers/asymmetric/rsa/__pycache__/
tests/__pycache__/
**/__pycache__/

test_file_name.pickle



Expand Down
4 changes: 3 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pycryptodome==3.19.0
typer[all]
pydantic<2
pydantic<2
numpy==1.24.2
matplotlib==3.7.1
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup, find_packages

__version__ = "1.4.0"
__version__ = "1.4.1"

with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
import logging
import multiprocessing
import os
import sys
import time
from multiprocessing import Pool
from typing import Tuple, List
import numpy as np
import pickle
import matplotlib.pyplot as plt

from crypto_pkg.ciphers.symmetric.aes import sbox_table

Expand All @@ -17,6 +19,25 @@
log.setLevel(logging.INFO)


def plot_c(data: np.ndarray, byte_position: int, plot: bool = False) -> None:
"""
Plot and save the correlation matrix results for the byte position 'byte_position'
Args:
data: correlation matrix C
byte_position: byte position corresponding to the entries of C
plot: show the plot or not - default = false
"""
log.debug(f"[Process {byte_position}] Generating the plot for the byte in position {byte_position}")
x = np.arange(len(data[0]))
for row in data:
plt.plot(x, row)
if plot:
plt.show()
plt.title(f"Correlation plot of the {byte_position + 1}th position key byte")
plt.savefig(f'plots/plot_{byte_position}.png')


def to_hex(n, size=16):
return hex(n)[2:].zfill(size * 2)

Expand Down Expand Up @@ -156,6 +177,7 @@ def attack_byte(self, byte_position: int = 0, plot: bool = False,
log.debug(f"[Process {byte_position}] Current predicted for all keys")
log.info(f"[Process {byte_position}] Calculating Correlation matrix C")
c = self.computeC(save=store, byte_position=byte_position, predicted_currents=predicted_current_keys)
plot_c(data=c, plot=plot, byte_position=byte_position)
log.info(f"[Process {byte_position}] Process {byte_position} finished")
return byte_position, np.unravel_index(np.argmax(c), c.shape)[0]

Expand Down Expand Up @@ -245,9 +267,15 @@ def full_attack(arguments):
' the provided byte position')

parser.set_defaults(store_correlation_matrices=False, re_calculate_correlation_matrices=False,
show_plot_correlations=False, filename="group4.pickle", verbose=False, max_datapoint=4000,
show_plot_correlations=False,
filename="src/crypto_pkg/attacks/power_analysis/test_file_name.pickle",
verbose=False, max_datapoint=4000,
byte_position=None)
args = parser.parse_args()
if args.verbose:
log.setLevel(logging.DEBUG)
print(os.getcwd())
print(os.path.dirname(os.path.abspath(__file__)))
if os.getcwd() != os.path.dirname(os.path.abspath(__file__)):
raise Exception(f"Run this script from the directory {os.path.dirname(os.path.abspath(__file__))}")
full_attack(arguments=args)
Binary file not shown.
9 changes: 6 additions & 3 deletions src/crypto_pkg/clis/attacks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import multiprocessing
import os
import time
from decimal import Decimal
Expand Down Expand Up @@ -169,9 +170,10 @@ def attack_double_encryption(

@app.command("correlation-power-analysis")
def attack_correlation_power_analysis(
filename: str = typer.Argument(str, help="Filename of the pickle file with the measurements"),
filename: str = typer.Argument('src/crypto_pkg/attacks/power_analysis/test_file_name.pickle',
help="Filename of the pickle file with the measurements"),
max_datapoints: Optional[int] = typer.Option(400, help="Maximum number of data points to consider"),
byte_position: Optional[int] = typer.Option(None, help="Byte position to attack"),
byte_position: Optional[int] = typer.Option(None, help="Byte position to attack")
):
"""
Example on how to use the power correlation attack.\n
Expand All @@ -193,7 +195,8 @@ def attack_correlation_power_analysis(
re_calculate=True)
print(f"Key byte found: {hex(key_byte[1])[2:]}")
return

cores = multiprocessing.cpu_count()
print(f"Number of cores: {cores}. The program wil run in chunks of {cores} byte positions")
# Run the full correlation attack
args_to_processes = tuple(
[[i, False, False, True] for i
Expand Down

0 comments on commit 4f154a2

Please sign in to comment.