[CT437]: Assignment 2 progress

This commit is contained in:
2025-03-21 10:03:42 +00:00
parent f840e43500
commit 2b593f6dc5
9 changed files with 248 additions and 0 deletions

Binary file not shown.

View File

@ -0,0 +1,118 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/evp.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "benchmark.h"
double get_cpu_time() {
struct rusage usage;
getrusage(RUSAGE_SELF, &usage);
return (double) usage.ru_utime.tv_sec + (double) usage.ru_utime.tv_usec / 1e6;
}
int encrypt_decrypt(const EVP_CIPHER *cipher, unsigned char *key, unsigned char *iv,
unsigned char *input, unsigned char *output, int encrypt, int data_size) {
EVP_CIPHER_CTX *ctx = EVP_CIPHER_CTX_new();
if (!ctx) {
fprintf(stderr, "Failed to create cipher context\n");
return -1;
}
int len, outlen;
if (encrypt)
EVP_EncryptInit_ex(ctx, cipher, NULL, key, iv);
else
EVP_DecryptInit_ex(ctx, cipher, NULL, key, iv);
EVP_CipherUpdate(ctx, output, &len, input, data_size);
outlen = len;
EVP_CipherFinal_ex(ctx, output + len, &len);
outlen += len;
EVP_CIPHER_CTX_free(ctx);
return outlen;
}
void benchmark_cipher(const EVP_CIPHER *cipher, char *cipher_name, int key_size, char *mode, int data_size) {
unsigned char key[32] = {0};
unsigned char iv[16] = {0};
unsigned char *input = malloc(data_size);
unsigned char *output = malloc(data_size);
if (!input || !output) {
fprintf(stderr, "Memory allocation failed\n");
free(input);
free(output);
return;
}
memset(input, 'A', data_size);
double start, end;
// Encryption Benchmark
start = get_cpu_time();
encrypt_decrypt(cipher, key, iv, input, output, 1, data_size);
end = get_cpu_time();
double encrypt_time = end - start;
// Decryption Benchmark
start = get_cpu_time();
encrypt_decrypt(cipher, key, iv, output, input, 0, data_size);
end = get_cpu_time();
double decrypt_time = end - start;
// Write results to CSV file
FILE *file = fopen(RESULTS_FILE, "a");
if (!file) {
fprintf(stderr, "Could not open file %s\n", RESULTS_FILE);
free(input);
free(output);
return;
}
fprintf(file, "%s\t%d\t%s\t%d\t%.6f\t%.6f\n", cipher_name, key_size, mode, data_size / (1024 * 1024), encrypt_time, decrypt_time);
fclose(file);
free(input);
free(output);
}
int main() {
FILE *file = fopen(RESULTS_FILE, "w");
if (!file) {
fprintf(stderr, "Could not create file %s\n", RESULTS_FILE);
return 1;
}
fprintf(file, "Cipher\tKey Size\tMode\tData Size (MB)\tEncryption Time (s)\tDecryption Time (s)\n");
fclose(file);
struct {
const EVP_CIPHER *(*cipher_func)(void);
char *name;
int key_sizes[2];
char *modes[3];
} ciphers[] = {
{EVP_aes_128_ecb, "AES", {128, 256}, {"ECB", "CBC", "CTR"}},
{EVP_aria_128_ecb, "ARIA", {128, 256}, {"ECB", "CBC", "CTR"}},
{EVP_camellia_128_ecb, "Camellia", {128, 256}, {"ECB", "CBC", "CTR"}}
};
int data_sizes[] = {DATA_SIZE_100MB, DATA_SIZE_1000MB};
for (int i = 0; i < sizeof(ciphers) / sizeof(ciphers[0]); i++) {
for (int j = 0; j < 2; j++) {
for (int k = 0; k < 3; k++) {
for (int d = 0; d < 2; d++) {
const EVP_CIPHER *cipher = ciphers[i].cipher_func();
benchmark_cipher(cipher, ciphers[i].name, ciphers[i].key_sizes[j], ciphers[i].modes[k], data_sizes[d]);
}
}
}
}
printf("Benchmarking completed. Results saved in %s\n", RESULTS_FILE);
return 0;
}

View File

@ -0,0 +1,17 @@
#ifndef BENCHMARK_H
#define BENCHMARK_H
#include <openssl/evp.h>
#include <sys/resource.h>
#define DATA_SIZE_100MB (100 * 1024 * 1024)
#define DATA_SIZE_1000MB (1000 * 1024 * 1024)
#define AES_BLOCK_SIZE 16
#define RESULTS_FILE "results.tsv"
// Function prototypes
double get_cpu_time();
int encrypt_decrypt(const EVP_CIPHER *cipher, unsigned char *key, unsigned char *iv, unsigned char *input, unsigned char *output, int encrypt, int data_size);
void benchmark_cipher(const EVP_CIPHER *cipher, char *cipher_name, int key_size, char *mode, int data_size);
#endif // BENCHMARK_H

View File

@ -0,0 +1,43 @@
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
plt.style.use('seaborn-v0_8-muted')
file_path = "results.tsv" # Update this path if needed
df = pd.read_csv(file_path, sep='\t')
color_encrypt = "#AEC6CF" # Pastel blue
color_decrypt = "#F4C2C2" # Pastel red
# Filter data for 100MB and 1000MB
df_100MB = df[df["Data Size (MB)"] == 100]
df_1000MB = df[df["Data Size (MB)"] == 1000]
# Function to create and save bar chart
def create_bar_chart(df, title, filename):
labels = df.apply(lambda row: f"{row['Cipher']}, {row['Key Size']} bits, {row['Mode']}", axis=1)
x = np.arange(len(labels)) # Label locations
width = 0.35 # Bar width
fig, ax = plt.subplots(figsize=(12, 6))
ax.bar(x - width/2, df["Encryption Time (s)"], width, label="Encryption Time", color=color_encrypt)
ax.bar(x + width/2, df["Decryption Time (s)"], width, label="Decryption Time", color=color_decrypt)
# Labels and title
ax.set_xlabel("Cipher, Keysize (bits), Mode")
ax.set_ylabel("Time (s)")
ax.set_title(title)
ax.set_xticks(x)
ax.set_xticklabels(labels, rotation=45, ha="right")
ax.legend()
ax.grid(True, axis='y', linestyle='--', alpha=0.5)
plt.tight_layout()
plt.savefig(filename, dpi=300)
plt.close()
# Create and save bar charts
create_bar_chart(df_100MB, "Encryption & Decryption Times for 100MB Data", "../latex/images/100mb.png")
create_bar_chart(df_1000MB, "Encryption & Decryption Times for 1000MB Data", "../latex/images/1000mb.png")

View File

@ -0,0 +1,37 @@
Cipher Key Size Mode Data Size (MB) Encryption Time (s) Decryption Time (s)
AES 128 ECB 100 0.011060 0.015951
AES 128 ECB 1000 0.114782 0.138175
AES 128 CBC 100 0.009856 0.013979
AES 128 CBC 1000 0.115273 0.138416
AES 128 CTR 100 0.011900 0.014040
AES 128 CTR 1000 0.127054 0.139111
AES 256 ECB 100 0.009927 0.013479
AES 256 ECB 1000 0.117038 0.138959
AES 256 CBC 100 0.011405 0.014018
AES 256 CBC 1000 0.119599 0.139258
AES 256 CTR 100 0.012812 0.013950
AES 256 CTR 1000 0.114078 0.139007
ARIA 128 ECB 100 0.487708 0.480942
ARIA 128 ECB 1000 4.844433 4.846121
ARIA 128 CBC 100 0.487954 0.484266
ARIA 128 CBC 1000 4.870137 4.865404
ARIA 128 CTR 100 0.484228 0.486427
ARIA 128 CTR 1000 4.864186 4.871898
ARIA 256 ECB 100 0.504303 0.489358
ARIA 256 ECB 1000 5.145704 4.868282
ARIA 256 CBC 100 0.506407 0.538013
ARIA 256 CBC 1000 4.915265 4.887134
ARIA 256 CTR 100 0.506468 0.492493
ARIA 256 CTR 1000 5.093478 5.360305
Camellia 128 ECB 100 0.404620 0.406571
Camellia 128 ECB 1000 4.303221 4.169631
Camellia 128 CBC 100 0.442378 0.418638
Camellia 128 CBC 1000 4.174943 4.098693
Camellia 128 CTR 100 0.416031 0.409108
Camellia 128 CTR 1000 4.116773 4.160921
Camellia 256 ECB 100 0.411045 0.439396
Camellia 256 ECB 1000 4.498530 4.246740
Camellia 256 CBC 100 0.424309 0.421895
Camellia 256 CBC 1000 4.230347 4.341175
Camellia 256 CTR 100 0.413531 0.424235
Camellia 256 CTR 1000 4.198038 4.237361
1 Cipher Key Size Mode Data Size (MB) Encryption Time (s) Decryption Time (s)
2 AES 128 ECB 100 0.011060 0.015951
3 AES 128 ECB 1000 0.114782 0.138175
4 AES 128 CBC 100 0.009856 0.013979
5 AES 128 CBC 1000 0.115273 0.138416
6 AES 128 CTR 100 0.011900 0.014040
7 AES 128 CTR 1000 0.127054 0.139111
8 AES 256 ECB 100 0.009927 0.013479
9 AES 256 ECB 1000 0.117038 0.138959
10 AES 256 CBC 100 0.011405 0.014018
11 AES 256 CBC 1000 0.119599 0.139258
12 AES 256 CTR 100 0.012812 0.013950
13 AES 256 CTR 1000 0.114078 0.139007
14 ARIA 128 ECB 100 0.487708 0.480942
15 ARIA 128 ECB 1000 4.844433 4.846121
16 ARIA 128 CBC 100 0.487954 0.484266
17 ARIA 128 CBC 1000 4.870137 4.865404
18 ARIA 128 CTR 100 0.484228 0.486427
19 ARIA 128 CTR 1000 4.864186 4.871898
20 ARIA 256 ECB 100 0.504303 0.489358
21 ARIA 256 ECB 1000 5.145704 4.868282
22 ARIA 256 CBC 100 0.506407 0.538013
23 ARIA 256 CBC 1000 4.915265 4.887134
24 ARIA 256 CTR 100 0.506468 0.492493
25 ARIA 256 CTR 1000 5.093478 5.360305
26 Camellia 128 ECB 100 0.404620 0.406571
27 Camellia 128 ECB 1000 4.303221 4.169631
28 Camellia 128 CBC 100 0.442378 0.418638
29 Camellia 128 CBC 1000 4.174943 4.098693
30 Camellia 128 CTR 100 0.416031 0.409108
31 Camellia 128 CTR 1000 4.116773 4.160921
32 Camellia 256 ECB 100 0.411045 0.439396
33 Camellia 256 ECB 1000 4.498530 4.246740
34 Camellia 256 CBC 100 0.424309 0.421895
35 Camellia 256 CBC 1000 4.230347 4.341175
36 Camellia 256 CTR 100 0.413531 0.424235
37 Camellia 256 CTR 1000 4.198038 4.237361

View File

@ -5,6 +5,8 @@
\usepackage{microtype} % Slightly tweak font spacing for aesthetics
\usepackage[english]{babel} % Language hyphenation and typographical rules
\usepackage{changepage} % adjust margins on the fly
\usepackage{booktabs} % For better-looking tables
\usepackage{pgfplotstable} % For reading and displaying CSV/TSV files
\usepackage[final, colorlinks = true, urlcolor = black, linkcolor = black, citecolor = black]{hyperref}
\usepackage{fontspec}
@ -94,6 +96,37 @@
\medskip
\section{Block Cipher Benchmarking}
\begin{table}[H]
\centering
\pgfplotstabletypeset[
col sep=tab, % Specifies that the file is tab-separated
string type, % Ensures text columns are treated correctly
header=true, % Includes the header row
columns/Cipher/.style={column name=Cipher},
columns/Key Size/.style={column name=Key Size (bits)},
columns/Mode/.style={column name=Mode},
columns/Data Size (MB)/.style={column name=Data Size (MB)},
columns/Encryption Time (s)/.style={column name=Encryption Time (s)},
columns/Decryption Time (s)/.style={column name=Decryption Time (s)},
every head row/.style={before row=\toprule, after row=\midrule},
every last row/.style={after row=\bottomrule}
] {../code/results.tsv} % Filename
\caption{Benchmarking results from TSV file}
\end{table}
\begin{figure}[H]
\centering
\includegraphics[width=\textwidth]{./images/100mb.png}
\caption{Encryption \& decryption times for 100MB data}
\end{figure}
\begin{figure}[H]
\centering
\includegraphics[width=\textwidth]{./images/1000mb.png}
\caption{Encryption \& decryption times for 1000MB data}
\end{figure}
\section{Implementing \& Benchmarking Triple-DES}
\end{document}

Binary file not shown.

After

Width:  |  Height:  |  Size: 195 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 200 KiB