Add u2f headers from FIDO alliance
Make the structures all __attribute__((packed)) because that's what
the standard requires. The only structure this seems to make a
material difference to is U2FHID_INIT_RESP and then only in the size.
Signed-off-by: James Bottomley <James.Bottomley@HansenPartnership.com>
diff --git a/u2f.h b/u2f.h
new file mode 100644
index 0000000..a499dba
--- /dev/null
+++ b/u2f.h
@@ -0,0 +1,106 @@
+// Common U2F raw message format header - Review Draft
+// 2014-10-08
+// Editor: Jakob Ehrensvard, Yubico, jakob@yubico.com
+
+#ifndef __U2F_H_INCLUDED__
+#define __U2F_H_INCLUDED__
+
+#ifdef _MSC_VER // Windows
+typedef unsigned char uint8_t;
+typedef unsigned short uint16_t;
+typedef unsigned int uint32_t;
+typedef unsigned long int uint64_t;
+#else
+#include <stdint.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// General constants
+
+#define U2F_EC_KEY_SIZE 32 // EC key size in bytes
+#define U2F_EC_POINT_SIZE ((U2F_EC_KEY_SIZE * 2) + 1) // Size of EC point
+#define U2F_MAX_KH_SIZE 128 // Max size of key handle
+#define U2F_MAX_ATT_CERT_SIZE 2048 // Max size of attestation certificate
+#define U2F_MAX_EC_SIG_SIZE 72 // Max size of DER coded EC signature
+#define U2F_CTR_SIZE 4 // Size of counter field
+#define U2F_APPID_SIZE 32 // Size of application id
+#define U2F_CHAL_SIZE 32 // Size of challenge
+
+#define ENC_SIZE(x) ((x + 7) & 0xfff8)
+
+// EC (uncompressed) point
+
+#define U2F_POINT_UNCOMPRESSED 0x04 // Uncompressed point format
+
+typedef struct {
+ uint8_t pointFormat; // Point type
+ uint8_t x[U2F_EC_KEY_SIZE]; // X-value
+ uint8_t y[U2F_EC_KEY_SIZE]; // Y-value
+} __attribute__((packed)) U2F_EC_POINT;
+
+// U2F native commands
+
+#define U2F_REGISTER 0x01 // Registration command
+#define U2F_AUTHENTICATE 0x02 // Authenticate/sign command
+#define U2F_VERSION 0x03 // Read version string command
+
+#define U2F_VENDOR_FIRST 0x40 // First vendor defined command
+#define U2F_VENDOR_LAST 0xbf // Last vendor defined command
+
+// U2F_CMD_REGISTER command defines
+
+#define U2F_REGISTER_ID 0x05 // Version 2 registration identifier
+#define U2F_REGISTER_HASH_ID 0x00 // Version 2 hash identintifier
+
+typedef struct {
+ uint8_t chal[U2F_CHAL_SIZE]; // Challenge
+ uint8_t appId[U2F_APPID_SIZE]; // Application id
+} __attribute__((packed)) U2F_REGISTER_REQ;
+
+typedef struct {
+ uint8_t registerId; // Registration identifier (U2F_REGISTER_ID_V2)
+ U2F_EC_POINT pubKey; // Generated public key
+ uint8_t keyHandleLen; // Length of key handle
+ uint8_t keyHandleCertSig[
+ U2F_MAX_KH_SIZE + // Key handle
+ U2F_MAX_ATT_CERT_SIZE + // Attestation certificate
+ U2F_MAX_EC_SIG_SIZE]; // Registration signature
+} __attribute__((packed)) U2F_REGISTER_RESP;
+
+// U2F_CMD_AUTHENTICATE command defines
+
+// Authentication control byte
+
+#define U2F_AUTH_ENFORCE 0x03 // Enforce user presence and sign
+#define U2F_AUTH_CHECK_ONLY 0x07 // Check only
+#define U2F_AUTH_FLAG_TUP 0x01 // Test of user presence set
+
+typedef struct {
+ uint8_t chal[U2F_CHAL_SIZE]; // Challenge
+ uint8_t appId[U2F_APPID_SIZE]; // Application id
+ uint8_t keyHandleLen; // Length of key handle
+ uint8_t keyHandle[U2F_MAX_KH_SIZE]; // Key handle
+} __attribute__((packed)) U2F_AUTHENTICATE_REQ;
+
+typedef struct {
+ uint8_t flags; // U2F_AUTH_FLAG_ values
+ uint8_t ctr[U2F_CTR_SIZE]; // Counter field (big-endian)
+ uint8_t sig[U2F_MAX_EC_SIG_SIZE]; // Signature
+} __attribute__((packed)) U2F_AUTHENTICATE_RESP;
+
+// Command status responses
+
+#define U2F_SW_NO_ERROR 0x9000 // SW_NO_ERROR
+#define U2F_SW_WRONG_DATA 0x6A80 // SW_WRONG_DATA
+#define U2F_SW_CONDITIONS_NOT_SATISFIED 0x6985 // SW_CONDITIONS_NOT_SATISFIED
+#define U2F_SW_COMMAND_NOT_ALLOWED 0x6986 // SW_COMMAND_NOT_ALLOWED
+#define U2F_SW_INS_NOT_SUPPORTED 0x6D00 // SW_INS_NOT_SUPPORTED
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // __U2F_H_INCLUDED__
diff --git a/u2f_hid.h b/u2f_hid.h
new file mode 100644
index 0000000..0b168f2
--- /dev/null
+++ b/u2f_hid.h
@@ -0,0 +1,126 @@
+// Common U2F HID transport header - Review Draft
+// 2014-10-08
+// Editor: Jakob Ehrensvard, Yubico, jakob@yubico.com
+
+#ifndef __U2FHID_H_INCLUDED__
+#define __U2FHID_H_INCLUDED__
+
+#ifdef _MSC_VER // Windows
+typedef unsigned char uint8_t;
+typedef unsigned short uint16_t;
+typedef unsigned int uint32_t;
+typedef unsigned long int uint64_t;
+#else
+#include <stdint.h>
+#endif
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// Size of HID reports
+
+#define HID_RPT_SIZE 64 // Default size of raw HID report
+
+// Frame layout - command- and continuation frames
+
+#define CID_BROADCAST 0xffffffff // Broadcast channel id
+
+#define TYPE_MASK 0x80 // Frame type mask
+#define TYPE_INIT 0x80 // Initial frame identifier
+#define TYPE_CONT 0x00 // Continuation frame identifier
+
+typedef struct {
+ uint32_t cid; // Channel identifier
+ union {
+ uint8_t type; // Frame type - b7 defines type
+ struct {
+ uint8_t cmd; // Command - b7 set
+ uint8_t bcnth; // Message byte count - high part
+ uint8_t bcntl; // Message byte count - low part
+ uint8_t data[HID_RPT_SIZE - 7]; // Data payload
+ } init;
+ struct {
+ uint8_t seq; // Sequence number - b7 cleared
+ uint8_t data[HID_RPT_SIZE - 5]; // Data payload
+ } cont;
+ };
+} __attribute__((packed)) U2FHID_FRAME;
+
+#define FRAME_TYPE(f) ((f).type & TYPE_MASK)
+#define FRAME_CMD(f) ((f).init.cmd & ~TYPE_MASK)
+#define MSG_LEN(f) ((f).init.bcnth*256 + (f).init.bcntl)
+#define FRAME_SEQ(f) ((f).cont.seq & ~TYPE_MASK)
+
+// HID usage- and usage-page definitions
+
+#define FIDO_USAGE_PAGE 0xf1d0 // FIDO alliance HID usage page
+#define FIDO_USAGE_U2FHID 0x01 // U2FHID usage for top-level collection
+#define FIDO_USAGE_DATA_IN 0x20 // Raw IN data report
+#define FIDO_USAGE_DATA_OUT 0x21 // Raw OUT data report
+
+// General constants
+
+#define U2FHID_IF_VERSION 2 // Current interface implementation version
+#define U2FHID_TRANS_TIMEOUT 3000 // Default message timeout in ms
+
+// U2FHID native commands
+
+#define U2FHID_PING (TYPE_INIT | 0x01) // Echo data through local processor only
+#define U2FHID_MSG (TYPE_INIT | 0x03) // Send U2F message frame
+#define U2FHID_LOCK (TYPE_INIT | 0x04) // Send lock channel command
+#define U2FHID_INIT (TYPE_INIT | 0x06) // Channel initialization
+#define U2FHID_WINK (TYPE_INIT | 0x08) // Send device identification wink
+#define U2FHID_SYNC (TYPE_INIT | 0x3c) // Protocol resync command
+#define U2FHID_ERROR (TYPE_INIT | 0x3f) // Error response
+
+#define U2FHID_VENDOR_FIRST (TYPE_INIT | 0x40) // First vendor defined command
+#define U2FHID_VENDOR_LAST (TYPE_INIT | 0x7f) // Last vendor defined command
+
+// U2FHID_INIT command defines
+
+#define INIT_NONCE_SIZE 8 // Size of channel initialization challenge
+#define CAPFLAG_WINK 0x01 // Device supports WINK command
+
+typedef struct {
+ uint8_t nonce[INIT_NONCE_SIZE]; // Client application nonce
+} __attribute__((packed)) U2FHID_INIT_REQ;
+
+typedef struct {
+ uint8_t nonce[INIT_NONCE_SIZE]; // Client application nonce
+ uint32_t cid; // Channel identifier
+ uint8_t versionInterface; // Interface version
+ uint8_t versionMajor; // Major version number
+ uint8_t versionMinor; // Minor version number
+ uint8_t versionBuild; // Build version number
+ uint8_t capFlags; // Capabilities flags
+} __attribute__((packed)) U2FHID_INIT_RESP;
+
+// U2FHID_SYNC command defines
+
+typedef struct {
+ uint8_t nonce; // Client application nonce
+} __attribute__((packed)) U2FHID_SYNC_REQ;
+
+typedef struct {
+ uint8_t nonce; // Client application nonce
+} __attribute__((packed)) U2FHID_SYNC_RESP;
+
+// Low-level error codes. Return as negatives.
+
+#define ERR_NONE 0x00 // No error
+#define ERR_INVALID_CMD 0x01 // Invalid command
+#define ERR_INVALID_PAR 0x02 // Invalid parameter
+#define ERR_INVALID_LEN 0x03 // Invalid message length
+#define ERR_INVALID_SEQ 0x04 // Invalid message sequencing
+#define ERR_MSG_TIMEOUT 0x05 // Message has timed out
+#define ERR_CHANNEL_BUSY 0x06 // Channel busy
+#define ERR_LOCK_REQUIRED 0x0a // Command requires channel lock
+#define ERR_SYNC_FAIL 0x0b // SYNC command failed
+#define ERR_OTHER 0x7f // Other unspecified error
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif // __U2FHID_H_INCLUDED__