-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
124 lines (100 loc) · 2.69 KB
/
main.c
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
#define MESSAGE_SIZE 4096
#define ITERATIONS 1000000
#include "hydrogen.h"
#include "nss.h"
#include "openssl.h"
#include "sodium.h"
#include "wolfcrypt.h"
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
bool benchmark(const Crypto *crypto, const size_t message_size, const size_t iterations) {
if (!crypto) {
return false;
}
const char *name = crypto->name();
void *param = NULL;
if (!crypto->init(¶m)) {
printf("[%s] initialization failed!\n", name);
return false;
}
uint8_t *src = malloc(message_size);
uint8_t *dst = malloc(crypto->buffer_size(message_size));
if (!crypto->random(param, message_size, src)) {
printf("[%s] input randomization failed!\n", name);
}
bool ok = true;
const char **ciphers = crypto->ciphers();
for (size_t i = 0; ciphers[i] != NULL; ++i) {
const char *cipher = ciphers[i];
if (!crypto->set_cipher(param, cipher)) {
printf("[%s] failed to set %s, skipping it...\n", name, cipher);
continue;
}
pthread_t progress_thread;
Progress progress;
progress.iterations_total = iterations;
progress.lib_name = name;
printf("[%s] running %s benchmark...\n", name, cipher);
pthread_create(&progress_thread, NULL, progress_function, &progress);
const double start = seconds();
progress.start_time = start;
for (size_t i = 0; i < iterations; ++i) {
size_t ret = crypto->encrypt(param, message_size, dst, src);
if (!ret) {
printf("[%s] encryption failed!\n", name);
ok = false;
break;
}
ret = crypto->decrypt(param, ret, dst, dst);
if (!ret) {
printf("[%s] decryption failed!\n", name);
ok = false;
break;
}
progress.iterations_completed = i;
}
const double elapsed = seconds() - start;
pthread_join(progress_thread, NULL);
if (!validate(message_size, dst, src)) {
printf("[%s] decrypted message doesn't match original, encryption/decryption failure!\n", name);
ok = false;
continue;
}
printf("[%s] %f seconds for %zu iterations, %zu bytes message\n", name, elapsed, iterations, message_size);
}
crypto->free(param);
free(src);
free(dst);
return ok;
}
int main() {
bool ok = true;
#if BENCHMARK_HYDROGEN
if (!benchmark(hydrogen_get(), MESSAGE_SIZE, ITERATIONS)) {
ok = false;
}
#endif
#if BENCHMARK_NSS
if (!benchmark(nss_get(), MESSAGE_SIZE, ITERATIONS)) {
ok = false;
}
#endif
#if BENCHMARK_OPENSSL
if (!benchmark(openssl_get(), MESSAGE_SIZE, ITERATIONS)) {
ok = false;
}
#endif
#if BENCHMARK_SODIUM
if (!benchmark(sodium_get(), MESSAGE_SIZE, ITERATIONS)) {
ok = false;
}
#endif
#if BENCHMARK_WOLFCRYPT
if (!benchmark(wolfcrypt_get(), MESSAGE_SIZE, ITERATIONS)) {
ok = false;
}
#endif
return ok ? EXIT_SUCCESS : EXIT_FAILURE;
}