forked from NiMouh/D-RSA
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathperformance.c
More file actions
146 lines (122 loc) · 4.69 KB
/
performance.c
File metadata and controls
146 lines (122 loc) · 4.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* @file performance.c
* @author Ana Raquel Neves Vidal (118408)
* @author Simão Augusto Ferreira Andrade (118345)
* @brief This file contains the performance tests of the setup of the D-RSA in C.
* @date 2023-12-21
*
* @copyright Copyright (c) 2023
*
*/
// Standard libraries
#include <stdio.h>
#include <string.h>
#include <time.h>
// OpenSSL libraries
#include <openssl/evp.h>
#include <openssl/sha.h>
// Constants
#define SEED_SIZE 32 // bytes
/**
* @brief This function generates the pbkdf2
*
* @param password The password to be used
* @param salt The salt to be used
* @param iterations The number of iterations to be used
* @param key_derivator The output pbkdf2
*
* @return 1 if the pbkdf2 was generated successfully, 0 otherwise
*/
int pbkdf2(const char password[], const char salt[], int iterations, uint8_t key_derivator[])
{
if (password == NULL || salt == NULL)
{
fprintf(stderr, "Invalid input: password or salt is NULL\n");
return 0;
}
size_t password_len = strlen(password);
size_t salt_len = strlen(salt);
if (PKCS5_PBKDF2_HMAC(password, password_len, (const unsigned char *)salt, salt_len, iterations, EVP_sha256(), SEED_SIZE, key_derivator) != 1)
{
fprintf(stderr, "Error generating key derivator\n");
return 0;
}
return 1;
}
/**
* @brief Function to use the generator to produce a pseudo-random stream of bytes
*
* @param buffer a pointer to an array where the bytes will be allocated
* @param size the size of the array
*/
void generate_random_bytes(void *buffer, size_t size)
{
FILE *urandom = fopen("/dev/urandom", "rb");
if (urandom == NULL)
{
perror("Error opening /dev/urandom");
exit(EXIT_FAILURE);
}
if (fread(buffer, 1, size, urandom) != size)
{
perror("Error reading random bytes from /dev/urandom");
fclose(urandom);
exit(EXIT_FAILURE);
}
fclose(urandom);
}
/**
* @brief this function is used to test the speed on the pbkdf2 function (consider using the /dev/urandom to generate random bytes to test)
* and send the time results to stdout
*
* @param password_size_interval An array with the password sizes to be tested
* @param salt_size_interval An array with the salt sizes to be tested
* @param iterations_interval An array with the number of iterations to be tested
* @param password_size_count The number of password sizes to be tested
* @param salt_size_count The number of salt sizes to be tested
* @param iterations_count The number of iterations to be tested
*
*/
void setup_performance(int password_size_interval[], int salt_size_interval[], int iterations_interval[], int password_size_count, int salt_size_count, int iterations_count)
{
// Create a CSV file with the results
FILE *results_file = fopen("performance.csv", "wb");
if (results_file == NULL)
{
perror("Error opening performance.csv");
exit(EXIT_FAILURE);
}
fprintf(results_file,"%s,%s,%s,%s\n", "password_size", "salt_size", "iterations", "time_seconds"); // header
for (int password_index = 0; password_index < password_size_count; password_index++)
{
for (int salt_index = 0; salt_index < salt_size_count; salt_index++)
{
for (int iterations_index = 0; iterations_index < iterations_count; iterations_index++)
{
char password[password_size_interval[password_index]];
char salt[salt_size_interval[salt_index]];
uint8_t key_derivator[SEED_SIZE];
generate_random_bytes(password, password_size_interval[password_index]);
generate_random_bytes(salt, salt_size_interval[salt_index]);
clock_t start = clock();
pbkdf2(password, salt, iterations_interval[iterations_index], key_derivator);
clock_t end = clock();
double time_spent = (double)(end - start) / CLOCKS_PER_SEC;
fprintf(results_file,"%d,%d,%d,%f\n", password_size_interval[password_index], salt_size_interval[salt_index], iterations_interval[iterations_index], time_spent);
}
}
}
fclose(results_file);
}
int main()
{
// values to be tested
int password_sizes[] = {10000, 100000, 200000};
int salt_sizes[] = {5000, 50000, 100000};
int iteration_counts[] = {1000, 10000, 100000};
int password_size_count = sizeof(password_sizes) / sizeof(password_sizes[0]);
int salt_size_count = sizeof(salt_sizes) / sizeof(salt_sizes[0]);
int iteration_count = sizeof(iteration_counts) / sizeof(iteration_counts[0]);
setup_performance(password_sizes, salt_sizes, iteration_counts, password_size_count, salt_size_count, iteration_count);
return 0;
}