summaryrefslogtreecommitdiff
path: root/reset-controller/fw/src/crypto.c
diff options
context:
space:
mode:
authorjaseg <git-bigdata-wsl-arch@jaseg.de>2021-04-09 18:38:02 +0200
committerjaseg <git-bigdata-wsl-arch@jaseg.de>2021-04-09 18:38:57 +0200
commit50998fcfb916ae251309bd4b464f2c122e8cb30d (patch)
tree4ecf7a7443b75ab51c4dc0c0fc9289342dc7d6a0 /reset-controller/fw/src/crypto.c
parent312fee491cfab436d52db4b6265107e20f3e1293 (diff)
downloadmaster-thesis-50998fcfb916ae251309bd4b464f2c122e8cb30d.tar.gz
master-thesis-50998fcfb916ae251309bd4b464f2c122e8cb30d.tar.bz2
master-thesis-50998fcfb916ae251309bd4b464f2c122e8cb30d.zip
Repo re-org
Diffstat (limited to 'reset-controller/fw/src/crypto.c')
-rw-r--r--reset-controller/fw/src/crypto.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/reset-controller/fw/src/crypto.c b/reset-controller/fw/src/crypto.c
new file mode 100644
index 0000000..f4f79a4
--- /dev/null
+++ b/reset-controller/fw/src/crypto.c
@@ -0,0 +1,80 @@
+
+#include <assert.h>
+#include <unistd.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <sodium.h>
+
+#include "crypto.h"
+#include "simulation.h"
+
+
+void debug_hexdump(const char *name, const uint8_t *buf, size_t len);
+int verify_trigger_dom(const uint8_t inkey[PRESIG_MSG_LEN],
+ const char *domain_string, const uint8_t refkey[PRESIG_MSG_LEN]);
+
+
+void debug_hexdump(const char *name, const uint8_t *buf, size_t len) {
+ DEBUG_PRINTN("%20s: ", name);
+ for (size_t i=0; i<len;) {
+ for (size_t j=0; j<8 && i<len; i++, j++)
+ DEBUG_PRINTN("%02x ", buf[i]);
+ DEBUG_PRINTN(" ");
+ }
+ DEBUG_PRINTN("\n");
+}
+
+/* Returns trigger sig height for correct trigger */
+int verify_trigger_dom(const uint8_t inkey[PRESIG_MSG_LEN],
+ const char *domain_string, const uint8_t refkey[PRESIG_MSG_LEN]) {
+ uint8_t key[crypto_auth_hmacsha512_KEYBYTES];
+ uint8_t key_out[crypto_auth_hmacsha512_BYTES];
+
+ static_assert(PRESIG_MSG_LEN <= crypto_auth_hmacsha512_KEYBYTES);
+ memcpy(key, inkey, PRESIG_MSG_LEN);
+ memset(key + PRESIG_MSG_LEN, 0, sizeof(key) - PRESIG_MSG_LEN);
+ DEBUG_PRINT("ds \"%s\"", domain_string);
+ debug_hexdump("ref", refkey, PRESIG_MSG_LEN);
+
+ for (int i=0; i<presig_height; i++) {
+ DEBUG_PRINT("Verifying height rel %d abs %d", i, presig_height-i);
+ debug_hexdump("key", key, sizeof(key));
+ (void)crypto_auth_hmacsha512(key_out, (uint8_t *)domain_string, strlen(domain_string), key);
+ debug_hexdump("out", key_out, sizeof(key_out));
+ memcpy(key, key_out, PRESIG_MSG_LEN);
+ memset(key + PRESIG_MSG_LEN, 0, sizeof(key) - PRESIG_MSG_LEN);
+
+ if (!memcmp(key, refkey, PRESIG_MSG_LEN))
+ return presig_height-i;
+ }
+
+ return 0;
+}
+
+int verify_trigger(const uint8_t inkey[PRESIG_MSG_LEN], int *height_out, int *domain_out) {
+ int res;
+ for (int i=0; i<_TRIGGER_DOMAIN_COUNT; i++) {
+ DEBUG_PRINT("Verifying domain %d", i);
+ if ((res = verify_trigger_dom(inkey, presig_domain_strings[i], presig_keys[i]))) {
+ DEBUG_PRINT("Match!");
+ if (height_out)
+ *height_out = res - 1;
+ if (domain_out)
+ *domain_out = i;
+ return 1;
+ }
+ }
+ return 0;
+}
+
+int oob_message_received(uint8_t msg[static OOB_TRIGGER_LEN]) {
+ int height, domain;
+ if (verify_trigger(msg, &height, &domain)) {
+ oob_trigger_activated(domain, height);
+ return 1;
+ }
+
+ return 0;
+}