From 70d8dcb6f6eaffd0c5717ffdca24ebc25a3b8de2 Mon Sep 17 00:00:00 2001 From: jaseg Date: Mon, 12 Nov 2018 12:48:47 +0900 Subject: Confirmed pairing works --- src/demo.c | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) (limited to 'src/demo.c') diff --git a/src/demo.c b/src/demo.c index 3a76a5d..ba76d2b 100644 --- a/src/demo.c +++ b/src/demo.c @@ -136,21 +136,27 @@ int pairing_check(struct NoiseState *st, const char *buf); void pairing_input(uint8_t keycode); void pairing_parse_report(struct hid_report *buf, uint8_t len); +/* Minimum number of bytes of handshake hash to confirm during pairing */ +#define MIN_PAIRING_SEQUENCE_LENGTH 8 + int pairing_check(struct NoiseState *st, const char *buf) { + LOG_PRINTF("Checking pairing\n"); const char *p = buf; int idx = 0; do { const char *found = strchr(p, ' '); size_t plen = found ? (size_t)(found - p) : strlen(p); /* p >= found */ int num = -1; + /* FIXME ignore "and", ignore commata and dots, handle letter case correctly (currently it's ignored). */ for (int i=0; i<256; i++) { - if (!strncmp(p, adjectives[i], plen) || !strncmp(p, nouns[i], plen)) { + if ((!strncmp(p, adjectives[i], plen)) || (!strncmp(p, nouns[i], plen))) { + LOG_PRINTF(" idx=%02d h=%02x i=%02x adj=%s n=%s plen=%d s=%s\n", idx, st->handshake_hash[idx], i, adjectives[i], nouns[i], plen, p); num = i; break; } } if (num == -1) { - LOG_PRINTF("Pairing word not found in dictionary\n"); + LOG_PRINTF("Pairing word \"%s\" not found in dictionary\n", p); return -1; } if (st->handshake_hash[idx] != num) { @@ -159,9 +165,12 @@ int pairing_check(struct NoiseState *st, const char *buf) { } idx++; p = strchr(p, ' '); - } while (p != NULL && idx < BLAKE2S_HASH_SIZE); + if (!p) + break; /* end of string */ + p++; /* skip space */ + } while (idx < BLAKE2S_HASH_SIZE); - if (idx < 8) { + if (idx < MIN_PAIRING_SEQUENCE_LENGTH) { LOG_PRINTF("Pairing sequence too short, only %d bytes of hash checked\n", idx); return -1; } @@ -180,20 +189,24 @@ void pairing_input(uint8_t keycode) { break; case KEY_BACKSPACE: + pairing_buf[pairing_buf_pos] = '\0'; /* FIXME debug */ if (pairing_buf_pos > 0) pairing_buf_pos--; break; default: - for (size_t i=0; i