From cbba4e772ec06dff01e6c14cb036895f1923125e Mon Sep 17 00:00:00 2001
From: Tom Smeding <tom.smeding@gmail.com>
Date: Mon, 7 Sep 2020 22:04:21 +0200
Subject: server: Add benchmarking mode to db module

---
 db.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/db.c b/db.c
index e58b284..6912cb4 100644
--- a/db.c
+++ b/db.c
@@ -17,6 +17,8 @@
 
 #define MAX_DATABASE_SIZE_BYTES ((i64)2 * 1024 * 1024 * 1024)
 
+// #define INSECURE_PURE_SPEED
+
 
 sqlite3 *database=NULL;
 
@@ -37,6 +39,13 @@ struct hashed_password {
 
 static struct hashed_password hash_password(const char *pass) {
 	struct hashed_password result;
+#ifdef INSECURE_PURE_SPEED
+	int len = strlen(pass);
+	if (len > (int)crypto_pwhash_STRBYTES) len = crypto_pwhash_STRBYTES;
+	memcpy(result.str, pass, len);
+	memset(result.str + len, 0, sizeof result.str - len);
+	result.length = crypto_pwhash_STRBYTES;
+#else
 	int ret = crypto_pwhash_str(
 			result.str, pass, strlen(pass),
 			PASSHASH_OPSLIMIT, PASSHASH_MEMLIMIT);
@@ -45,6 +54,7 @@ static struct hashed_password hash_password(const char *pass) {
 	} else {
 		result.length = crypto_pwhash_STRBYTES;
 	}
+#endif
 	return result;
 }
 
@@ -68,7 +78,13 @@ void db_init(void){
 	SQLITE(config, SQLITE_CONFIG_SERIALIZED);
 	SQLITE(initialize);
 
+#ifdef INSECURE_PURE_SPEED
+#warning INSECURE MODE, IN-MEMORY DATABASE, NO PASSWORDS CHECKED
+	fprintf(stderr, "WARNING: INSECURE MODE, IN-MEMORY DATABASE, NO PASSWORDS CHECKED\n");
+	SQLITE(open_v2, "db.db", &database,SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE | SQLITE_OPEN_MEMORY, NULL);
+#else
 	SQLITE(open_v2, "db.db", &database, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
+#endif
 	SQLITE(busy_timeout, database, 500);
 
 	SQLITE(exec, database, "pragma foreign_keys = 1", NULL, NULL, NULL);
@@ -337,11 +353,17 @@ bool db_check_pass(i64 userid, const char *pass) {
 		passhash_sq = sqlite3_column_text(stmt, 0);
 	}
 
+#ifdef INSECURE_PURE_SPEED
+	(void)passhash_sq;
+	bool success = true;
+	bool rehash = false;
+#else
 	int ret = crypto_pwhash_str_verify((const char*)passhash_sq, pass, strlen(pass));
 	bool success = ret == 0;
 	bool rehash = success &&
 					crypto_pwhash_str_needs_rehash(
 							(const char*)passhash_sq, PASSHASH_OPSLIMIT, PASSHASH_MEMLIMIT);
+#endif
 
 	reset_stmt(stmt);
 
-- 
cgit v1.2.3-70-g09d2