From 58a256c870a338096165c595a28080cff04285df Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Thu, 9 Jan 2014 09:43:43 +0100 Subject: [PATCH] add functions to set AES and HMAC key from raw bytes --- libykpers-1.map | 2 ++ ykpers.c | 21 +++++++++++++++++++++ ykpers.h | 2 ++ 3 files changed, 25 insertions(+) diff --git a/libykpers-1.map b/libykpers-1.map index d92dbb8..e49d81b 100644 --- a/libykpers-1.map +++ b/libykpers-1.map @@ -247,5 +247,7 @@ LIBYKPERS_1.15 { global: # Functions: ykp_get_supported_key_length; + ykp_AES_key_from_raw; + ykp_HMAC_key_from_raw; # Variables: } LIBYKPERS_1.14; diff --git a/ykpers.c b/ykpers.c index 6e8fee5..c46c9e8 100644 --- a/ykpers.c +++ b/ykpers.c @@ -268,6 +268,27 @@ int ykp_AES_key_from_hex(YKP_CONFIG *cfg, const char *hexkey) { return 0; } +/* Store a 16 byte AES key. + * + * copy 16 bytes from key to cfg->ykcore_config.key + */ +int ykp_AES_key_from_raw(YKP_CONFIG *cfg, const char *key) { + memcpy(cfg->ykcore_config.key, key, sizeof(cfg->ykcore_config.key)); + return 0; +} + +/* Store a 20 byte HMAC key. + * + * store the first 16 bytes of key in cfg->ykcore_config.key + * and the remaining 4 bytes in cfg->ykcore_config.uid + */ +int ykp_HMAC_key_from_raw(YKP_CONFIG *cfg, const char *key) { + size_t size = sizeof(cfg->ykcore_config.key); + memcpy(cfg->ykcore_config.key, key, size); + memcpy(cfg->ykcore_config.uid, key + size, 20 - size); + return 0; +} + /* Decode 160 bits HMAC key, used with OATH and HMAC challenge-response. * * The first 128 bits of the HMAC go key into cfg->ykcore_config.key, diff --git a/ykpers.h b/ykpers.h index 50234bb..8b1eabe 100644 --- a/ykpers.h +++ b/ykpers.h @@ -62,9 +62,11 @@ int ykp_configure_command(YKP_CONFIG *cfg, uint8_t command); int ykp_configure_for(YKP_CONFIG *cfg, int confnum, YK_STATUS *st); int ykp_AES_key_from_hex(YKP_CONFIG *cfg, const char *hexkey); +int ykp_AES_key_from_raw(YKP_CONFIG *cfg, const char *key); int ykp_AES_key_from_passphrase(YKP_CONFIG *cfg, const char *passphrase, const char *salt); int ykp_HMAC_key_from_hex(YKP_CONFIG *cfg, const char *hexkey); +int ykp_HMAC_key_from_raw(YKP_CONFIG *cfg, const char *key); /* Functions for constructing the YK_NDEF struct before writing it to a neo */ YK_NDEF *ykp_alloc_ndef(void); -- 2.39.5