From a9ef2c5f824ea73c66b146d833a1241b29a04817 Mon Sep 17 00:00:00 2001 From: Richard Levitte Date: Thu, 7 May 2009 21:03:50 +0000 Subject: [PATCH] Move all internal functions to the new header file ykcore_lcl.h. Rename structures in ykcore.h so they are in the namespace. Add a hack in ykcore_lcl.h to map ykcore.h structure names to ykdef.h ones. Have ykcore_lcl.h include ykcore.h and ykdef.h, so we get the order right. Change relevant .c files accordingly. --- ykcore/Makefile.am | 2 +- ykcore/ykcore.c | 42 ++++++++++++------------ ykcore/ykcore.h | 53 ++++++------------------------ ykcore/ykcore_lcl.h | 78 +++++++++++++++++++++++++++++++++++++++++++++ ykcore/ykstatus.c | 21 ++++++------ ykcore/ykstatus.h | 24 +++++++------- 6 files changed, 132 insertions(+), 88 deletions(-) create mode 100644 ykcore/ykcore_lcl.h diff --git a/ykcore/Makefile.am b/ykcore/Makefile.am index 9022d0c..0346a16 100644 --- a/ykcore/Makefile.am +++ b/ykcore/Makefile.am @@ -26,7 +26,7 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. noinst_LTLIBRARIES = libykcore.la -libykcore_la_SOURCES = ykdef.h ykcore.h ykcore_backend.h ykcore.c \ +libykcore_la_SOURCES = ykdef.h ykcore.h ykcore_lcl.h ykcore_backend.h ykcore.c \ ykstatus.h ykstatus.c libykcore_la_LIBADD = $(LTLIBYUBIKEY) $(LTLIBUSB) diff --git a/ykcore/ykcore.c b/ykcore/ykcore.c index 010468a..ebcf1de 100644 --- a/ykcore/ykcore.c +++ b/ykcore/ykcore.c @@ -28,10 +28,10 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "ykcore.h" -#include "ykdef.h" +#include "ykcore_lcl.h" #include "ykcore_backend.h" +/* To get modhex and crc16 */ #include #ifndef _WIN32 @@ -48,15 +48,15 @@ int yk_release(void) _ykusb_start(); } -YUBIKEY *yk_open_first_key(void) +YK_KEY *yk_open_first_key(void) { struct usb_bus *bus; struct usb_device *dev; - YUBIKEY *yk = _ykusb_open_device(YUBICO_VID, YUBIKEY_PID); + YK_KEY *yk = _ykusb_open_device(YUBICO_VID, YUBIKEY_PID); int rc = yk_errno; if (yk) { - STATUS st; + YK_STATUS st; if (!yk_get_status(yk, &st)) { rc = yk_errno; @@ -76,32 +76,32 @@ YUBIKEY *yk_open_first_key(void) return yk; } -int yk_close_key(YUBIKEY *yk) +int yk_close_key(YK_KEY *yk) { return _ykusb_close_device(yk); } -int yk_get_status(YUBIKEY *k, STATUS *status) +int yk_get_status(YK_KEY *k, YK_STATUS *status) { unsigned int status_count = 0; - if (!yk_read_from_key(k, 0, status, sizeof(STATUS), &status_count)) + if (!yk_read_from_key(k, 0, status, sizeof(YK_STATUS), &status_count)) return 0; - if (status_count != sizeof(STATUS)) { + if (status_count != sizeof(YK_STATUS)) { yk_errno = YK_EWRONGSIZ; return 0; } - status->touchLevel = endian_swap_16(status->touchLevel); + status->touchLevel = yk_endian_swap_16(status->touchLevel); return 1; } -int yk_write_config(YUBIKEY *yk, CONFIG *cfg, unsigned char *acc_code) +int yk_write_config(YK_KEY *yk, YK_CONFIG *cfg, unsigned char *acc_code) { - unsigned char buf[sizeof(CONFIG) + ACC_CODE_SIZE]; - STATUS stat; + unsigned char buf[sizeof(YK_CONFIG) + ACC_CODE_SIZE]; + YK_STATUS stat; int seq; /* Get current seqence # from status block */ @@ -117,15 +117,15 @@ int yk_write_config(YUBIKEY *yk, CONFIG *cfg, unsigned char *acc_code) if (cfg) { cfg->crc = ~yubikey_crc16 ((unsigned char *) cfg, - sizeof(CONFIG) - sizeof(cfg->crc)); - cfg->crc = endian_swap_16(cfg->crc); - memcpy(buf, cfg, sizeof(CONFIG)); + sizeof(YK_CONFIG) - sizeof(cfg->crc)); + cfg->crc = yk_endian_swap_16(cfg->crc); + memcpy(buf, cfg, sizeof(YK_CONFIG)); } /* Append current access code if present */ if (acc_code) - memcpy(buf + sizeof(CONFIG), acc_code, ACC_CODE_SIZE); + memcpy(buf + sizeof(YK_CONFIG), acc_code, ACC_CODE_SIZE); /* Write to Yubikey */ @@ -182,7 +182,7 @@ const char *yk_usb_strerror() /* Note: we currently have no idea whatsoever how to read things larger than FEATURE_RPT_SIZE - 1. We also have no idea what to do with the slot parameter, it currently is there for future purposes only. */ -int yk_read_from_key(YUBIKEY *yk, uint8_t slot, +int yk_read_from_key(YK_KEY *yk, uint8_t slot, void *buf, unsigned int bufsize, unsigned int *bufcount) { unsigned char data[FEATURE_RPT_SIZE]; @@ -205,7 +205,7 @@ int yk_read_from_key(YUBIKEY *yk, uint8_t slot, return 1; } -int yk_write_to_key(YUBIKEY *yk, uint8_t slot, const void *buf, int bufcount) +int yk_write_to_key(YK_KEY *yk, uint8_t slot, const void *buf, int bufcount) { unsigned char repbuf[FEATURE_RPT_SIZE]; unsigned char data[SLOT_DATA_SIZE + FEATURE_RPT_SIZE]; @@ -269,7 +269,7 @@ int yk_write_to_key(YUBIKEY *yk, uint8_t slot, const void *buf, int bufcount) return 1; } -int yk_force_key_update(YUBIKEY *yk) +int yk_force_key_update(YK_KEY *yk) { unsigned char buf[FEATURE_RPT_SIZE]; @@ -281,7 +281,7 @@ int yk_force_key_update(YUBIKEY *yk) return 1; } -uint16_t endian_swap_16(uint16_t x) +uint16_t yk_endian_swap_16(uint16_t x) { static int testflag = -1; diff --git a/ykcore/ykcore.h b/ykcore/ykcore.h index 702c8f7..3fd121e 100644 --- a/ykcore/ykcore.h +++ b/ykcore/ykcore.h @@ -48,14 +48,14 @@ * ****/ -typedef struct yubikey_st YUBIKEY; /* Really a USB device handle. */ -typedef struct status_st STATUS; /* Status structure, +typedef struct yk_key_st YK_KEY; /* Really a USB device handle. */ +typedef struct yk_status_st YK_STATUS; /* Status structure, filled by yk_get_status(). */ -typedef struct ticket_st TICKET; /* Ticket structure... */ -typedef struct config_st CONFIG; /* Configuration structure. +typedef struct yk_ticket_st YK_TICKET; /* Ticket structure... */ +typedef struct yk_config_st YK_CONFIG; /* Configuration structure. Other libraries provide access. */ -typedef struct nav_st NAV; /* Navigation structure. +typedef struct yk_nav_st YK_NAV; /* Navigation structure. Other libraries provide access. */ /************************************************************************* @@ -71,8 +71,8 @@ extern int yk_release(void); * Functions to get and release the key itself. * ****/ -extern YUBIKEY *yk_open_first_key(void); /* opens the first key available */ -extern int yk_close_key(YUBIKEY *k); /* closes a previously opened key */ +extern YK_KEY *yk_open_first_key(void); /* opens the first key available */ +extern int yk_close_key(YK_KEY *k); /* closes a previously opened key */ /************************************************************************* * @@ -80,7 +80,7 @@ extern int yk_close_key(YUBIKEY *k); /* closes a previously opened key */ * ****/ /* fetches key status into the structure given by `status' */ -extern int yk_get_status(YUBIKEY *k, STATUS *status /*, int forceUpdate */); +extern int yk_get_status(YK_KEY *k, YK_STATUS *status /*, int forceUpdate */); /************************************************************************* * @@ -91,7 +91,7 @@ extern int yk_get_status(YUBIKEY *k, STATUS *status /*, int forceUpdate */); /* writes the given configuration to the key. If the configuration is NULL, zap the key configuration. acc_code has to be provided of the key has a protecting access code. */ -extern int yk_write_config(YUBIKEY *k, CONFIG *cfg, unsigned char *acc_code); +extern int yk_write_config(YK_KEY *k, YK_CONFIG *cfg, unsigned char *acc_code); /************************************************************************* * @@ -116,39 +116,4 @@ const char *yk_usb_strerror(); #define YK_ENOSTATUS 0x07 #define YK_ENOTYETIMPL 0x08 -/*=======================================================================* - -/************************************************************************* - ** - ** = = = = = = = = = B I G F A T W A R N I N G = = = = = = = = = - ** - ** DO NOT USE THE FOLLOWING FUCTIONS DIRECTLY UNLESS YOU WRITE CORE ROUTINES! - ** - ** These functions are declared here only to make sure they get defined - ** correctly internally. - ** - ** YOU HAVE BEEN WARNED! - ** - ****/ - -/************************************************************************* - * - * Functions to send and receive data to/from the key. - * - ****/ -extern int yk_read_from_key(YUBIKEY *k, uint8_t slot, - void *buf, unsigned int bufsize, - unsigned int *bufcount); -extern int yk_write_to_key(YUBIKEY *k, uint8_t slot, - const void *buf, int bufcount); - -/************************************************************************* - * - * Internal helper functions - * - ****/ - -/* Swaps the two bytes between little and big endian on big endian machines */ -extern uint16_t endian_swap_16(uint16_t x); - #endif /* __YKCORE_H_INCLUDED__ */ diff --git a/ykcore/ykcore_lcl.h b/ykcore/ykcore_lcl.h new file mode 100644 index 0000000..8b960c2 --- /dev/null +++ b/ykcore/ykcore_lcl.h @@ -0,0 +1,78 @@ +/* -*- mode:C; c-file-style: "bsd" -*- */ +/* + * Copyright (c) 2008, 2009, Yubico AB + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following + * disclaimer in the documentation and/or other materials provided + * with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef __YKCORE_LCL_H_INCLUDED__ +#define __YKCORE_LCL_H_INCLUDED__ + +/* This is a hack to map official structure names (in ykcore.h) to + internal ones (in ykdef.h) */ +#define yk_key_st yubikey_st +#define yk_status_st status_st +#define yk_ticket_st ticket_st +#define yk_config_st config_st +#define yk_nav_st nav_st + +#include "ykcore.h" +#include "ykdef.h" + +/************************************************************************* + ** + ** = = = = = = = = = B I G F A T W A R N I N G = = = = = = = = = + ** + ** DO NOT USE THE FOLLOWING FUCTIONS DIRECTLY UNLESS YOU WRITE CORE ROUTINES! + ** + ** These functions are declared here only to make sure they get defined + ** correctly internally. + ** + ** YOU HAVE BEEN WARNED! + ** + ****/ + +/************************************************************************* + * + * Functions to send and receive data to/from the key. + * + ****/ +extern int yk_read_from_key(YK_KEY *k, uint8_t slot, + void *buf, unsigned int bufsize, + unsigned int *bufcount); +extern int yk_write_to_key(YK_KEY *k, uint8_t slot, + const void *buf, int bufcount); + +/************************************************************************* + * + * Internal helper functions + * + ****/ + +/* Swaps the two bytes between little and big endian on big endian machines */ +extern uint16_t yk_endian_swap_16(uint16_t x); + +#endif /* __YKCORE_LCL_H_INCLUDED__ */ diff --git a/ykcore/ykstatus.c b/ykcore/ykstatus.c index 7f11bda..f4aff01 100644 --- a/ykcore/ykstatus.c +++ b/ykcore/ykstatus.c @@ -29,58 +29,59 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "ykcore_lcl.h" #include "ykdef.h" #include "ykstatus.h" -STATUS *ykds_alloc(void) +YK_STATUS *ykds_alloc(void) { - STATUS *st = malloc(sizeof(STATUS)); + YK_STATUS *st = malloc(sizeof(YK_STATUS)); if (!st) { yk_errno = YK_ENOMEM; } return st; } -void ykds_free(STATUS *st) +void ykds_free(YK_STATUS *st) { free(st); } -STATUS *ykds_static(void) +YK_STATUS *ykds_static(void) { - static STATUS st; + static YK_STATUS st; return &st; } -extern int ykds_version_major(const STATUS *st) +extern int ykds_version_major(const YK_STATUS *st) { if (st) return st->versionMajor; yk_errno = YK_ENOSTATUS; return 0; } -extern int ykds_version_minor(const STATUS *st) +extern int ykds_version_minor(const YK_STATUS *st) { if (st) return st->versionMinor; yk_errno = YK_ENOSTATUS; return 0; } -extern int ykds_version_build(const STATUS *st) +extern int ykds_version_build(const YK_STATUS *st) { if (st) return st->versionBuild; yk_errno = YK_ENOSTATUS; return 0; } -extern int ykds_pgm_seq(const STATUS *st) +extern int ykds_pgm_seq(const YK_STATUS *st) { if (st) return st->pgmSeq; yk_errno = YK_ENOSTATUS; return 0; } -extern int ykds_touch_level(const STATUS *st) +extern int ykds_touch_level(const YK_STATUS *st) { if (st) return st->touchLevel; diff --git a/ykcore/ykstatus.h b/ykcore/ykstatus.h index 5e80cc8..44c105d 100644 --- a/ykcore/ykstatus.h +++ b/ykcore/ykstatus.h @@ -29,24 +29,24 @@ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#ifndef __YKDATA_H_INCLUDED__ -#define __YKDATA_H_INCLUDED__ +#ifndef __YKSTATUS_H_INCLUDED__ +#define __YKSTATUS_H_INCLUDED__ -#include "ykcore.h" +#include /* Allocate and free status structures */ -extern STATUS *ykds_alloc(void); -extern void ykds_free(STATUS *st); +extern YK_STATUS *ykds_alloc(void); +extern void ykds_free(YK_STATUS *st); /* Return static status structure, to be used for quick checks. USE WITH CAUTION, as this is a SHARED OBJECT. */ -extern STATUS *ykds_static(void); +extern YK_STATUS *ykds_static(void); /* Accessor functions */ -extern int ykds_version_major(const STATUS *st); -extern int ykds_version_minor(const STATUS *st); -extern int ykds_version_build(const STATUS *st); -extern int ykds_pgm_seq(const STATUS *st); -extern int ykds_touch_level(const STATUS *st); +extern int ykds_version_major(const YK_STATUS *st); +extern int ykds_version_minor(const YK_STATUS *st); +extern int ykds_version_build(const YK_STATUS *st); +extern int ykds_pgm_seq(const YK_STATUS *st); +extern int ykds_touch_level(const YK_STATUS *st); -#endif /* __YKDATA_H_INCLUDED__ */ +#endif /* __YKSTATUS_H_INCLUDED__ */ -- 2.39.5