summaryrefslogtreecommitdiffstats
path: root/utils/benchmark.c
diff options
context:
space:
mode:
Diffstat (limited to 'utils/benchmark.c')
-rw-r--r--utils/benchmark.c49
1 files changed, 48 insertions, 1 deletions
diff --git a/utils/benchmark.c b/utils/benchmark.c
index d8c5d2d..24f4511 100644
--- a/utils/benchmark.c
+++ b/utils/benchmark.c
@@ -25,7 +25,54 @@
#include <assert.h>
#include <sys/time.h>
-void benchmark_hashing(int rounds);
+#include <eurephia_nullsafe.h>
+#include <eurephia_context.h>
+
+#define ROUNDS_MIN 1000
+#define ROUNDS_MAX 999999999
+
+// Internal functions found in passwd.c
+int pack_saltinfo(char *buf, int buflen, int rounds, int saltlen, const char *pwd);
+inline char *sha512_crypt_r(const char *key, const char *salt, size_t maxrounds_cfg, char *buffer, int buflen);
+int gen_randsaltstr(eurephiaCTX *ctx, char *saltstr, int len);
+
+// A function which calculates a hash based on a standard password, used
+// for measuring the time used for hash calculation
+void benchmark_hashing(int rounds) {
+
+ char *buffer = NULL;
+ char *pwdhash = NULL;
+ static char randstr[34];
+ static int rand_set = 0;
+ char pwdsalt[64], saltstr[32];
+
+ // Get random data for our salt
+ if( rand_set == 0 ) {
+ memset(&randstr, 0, 34);
+ if( gen_randsaltstr(NULL, randstr, 32) == 0 ) {
+ fprintf(stderr, "Could not retrieve enough random data for hashing");
+ exit(19);
+ };
+ rand_set = 1;
+ }
+
+ // Prepare a salt package
+ memset(&pwdsalt, 0, 64);
+ memset(&saltstr, 0, 32);
+ pack_saltinfo(saltstr, 30, rounds, 32, "benchmarkpassword");
+ strncpy(pwdsalt, saltstr, strlen(saltstr));
+ strncat(pwdsalt, randstr, 62 - strlen(saltstr));
+ memset(&randstr, 0, 32);
+
+ buffer = (char *) malloc(1024);
+ assert( buffer != NULL );
+ memset(buffer, 0, 1024);
+
+ pwdhash = sha512_crypt_r("benchmarkpassword", pwdsalt, ROUNDS_MAX, buffer, 1024);
+
+ free_nullsafe(buffer);
+}
+
int timeval_subtract (result, x, y)
struct timeval *result, *x, *y;