Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support different keymaps via attempting to decode #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Libyubikey NEWS -- History of user-visible changes. -*- outline -*-
** Better self tests.
** Fix memory leak.
** Document modhex alphabet.
** Add different ModHex alphabets

* Version 1.13 (released 2015-03-05)
** Correct hex decode with uneven characters, for example "abc".
Expand Down
3 changes: 3 additions & 0 deletions modhex.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ The modhex alphabet is a base16 alphabet just as hex, they map as follows:
0 1 2 3 4 5 6 7 8 9 a b c d e f
c b d e f g h i j k l n r t u v

When using yubikey devices on non-QWERTY keyboard layouts, these keys may map
to different letters.

SEE ALSO
---------
YubiKeys can be obtained from Yubico: http://www.yubico.com/
8 changes: 6 additions & 2 deletions modhex.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,13 @@ main (int argc, char *argv[])
return 1;
}

if (decode_p)
if (decode_p) {
if (!yubikey_modhex_p(data)) {
printf("Invalid modhex string\n");
return 0;
}
yubikey_modhex_decode (buf, data, inlen / 2);
else
} else
yubikey_modhex_encode (buf, data, inlen);

if (decode_p && hex_p)
Expand Down
42 changes: 42 additions & 0 deletions tests/selftest.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,45 @@ modhex_test3 (void)
printf ("Modhex-3 success\n");
}

static void
modhex_test4 (void)
{
char buf[1024];
int rc;

strcpy (buf, "jxe.uidchtnbpygk");
rc = yubikey_modhex_p (buf);
printf ("modhex-p(\"%s\") = %d\n", buf, rc);
assert (rc == 1);
printf ("Modhex-4 success\n");
}

static void
modhex_test5 (void)
{
char buf[1024];
int rc;

strcpy (buf, "cbsftdhuneikpglv");
rc = yubikey_modhex_p (buf);
printf ("modhex-p(\"%s\") = %d\n", buf, rc);
assert (rc == 1);
printf ("Modhex-5 success\n");
}

static void
modhex_test6 (void)
{
char buf[1024];
char buf2[1024];

strcpy (buf, "vvnhvuuvihlstuhpkgvfthfpeifbnbbuscbhhphffisc");
yubikey_modhex_decode(buf2, buf, sizeof (buf2));
assert (memcmp (buf2, "\xff\xb6\xfe\xef\x76\xa0\xde\x60\x95\xf4\xd6\x40\x37\x41\xb1\x1e\x00\x16\x60\x64\x47\x00", 22) == 0);

printf ("Modhex-6 success\n");
}

static void
hex_test1 (void)
{
Expand Down Expand Up @@ -315,6 +354,9 @@ main (void)
modhex_test1 ();
modhex_test2 ();
modhex_test3 ();
modhex_test4 ();
modhex_test5 ();
modhex_test6 ();
hex_test1 ();
hex_test2 ();
hex_test3 ();
Expand Down
30 changes: 26 additions & 4 deletions ykhex.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include "yubikey.h"

static const char hex_trans[] = "0123456789abcdef";
static const char modhex_trans[] = YUBIKEY_MODHEX_MAP;
//static const char modhex_trans[] = YUBIKEY_MODHEX_MAPS[0];

static void
_yubikey_encode (char *dst, const char *src, size_t srcSize,
Expand Down Expand Up @@ -113,17 +113,39 @@ yubikey_hex_p (const char *str)
void
yubikey_modhex_encode (char *dst, const char *src, size_t srcSize)
{
_yubikey_encode (dst, src, srcSize, modhex_trans);
_yubikey_encode (dst, src, srcSize, YUBIKEY_MODHEX_MAPS[0]);
}

void
yubikey_modhex_decode (char *dst, const char *src, size_t dstSize)
{
_yubikey_decode (dst, src, dstSize, modhex_trans);
size_t i;
for (i = 0; i < N_MODHEX_MAPS; i++) {
if (_yubikey_p(src, YUBIKEY_MODHEX_MAPS[i])) {
_yubikey_decode (dst, src, dstSize, YUBIKEY_MODHEX_MAPS[0]);
}
}
_yubikey_decode (dst, src, dstSize, YUBIKEY_MODHEX_MAPS[0]);
}

void
yubikey_modhex_decode_map(char *dst, const char *src, size_t dstSize, const char *map)
{
_yubikey_decode(dst, src, dstSize, map);
}

int yubikey_modhex_map_p(const char *str, const char *map) {
return _yubikey_p(str, map);
}

int
yubikey_modhex_p (const char *str)
{
return _yubikey_p (str, modhex_trans);
size_t i;
for (i = 0; i < N_MODHEX_MAPS; i++) {
if (_yubikey_p(str, YUBIKEY_MODHEX_MAPS[i])) {
return 1;
}
}
return 0;
}
16 changes: 14 additions & 2 deletions yubikey.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,13 @@ extern "C"
* Low-level functions; ModHex.
*/

#define YUBIKEY_MODHEX_MAP "cbdefghijklnrtuv"
#define N_MODHEX_MAPS (sizeof YUBIKEY_MODHEX_MAPS / sizeof YUBIKEY_MODHEX_MAPS[0])
const static char* YUBIKEY_MODHEX_MAPS[] = {
"cbdefghijklnrtuv", /* default (QWERTY, AZERTY) */
"jxe.uidchtnbpygk", /* English Dvorak */
"cbsftdhuneikpglv", /* English Colemak */
};


/* ModHex encode input string SRC of length SRCSIZE and put the zero
terminated output string in DST. The size of the output string DST
Expand All @@ -102,9 +108,14 @@ extern "C"

/* ModHex decode input string SRC of length DSTSIZE/2 into output
string DST. The output string DST is always DSTSIZE/2 large plus
the terminating zero. */
the terminating zero.

Tries to guess the modhex map
*/
extern void yubikey_modhex_decode (char *dst,
const char *src, size_t dstsize);
extern void yubikey_modhex_decode_map (char *dst, const char *src,
size_t dstsize, const char *map);

/* Hex encode/decode data, same interface as modhex functions. */
extern void yubikey_hex_encode (char *dst, const char *src, size_t srcsize);
Expand All @@ -113,6 +124,7 @@ extern "C"
/* Return non-zero if zero-terminated input STR is a valid (mod)hex
string, and zero if any non-alphabetic characters are found. */
extern int yubikey_modhex_p (const char *str);
extern int yubikey_modhex_map_p (const char *str, const char *map);
extern int yubikey_hex_p (const char *str);

/*
Expand Down