2016-03-29 04:35:26 +00:00
|
|
|
/*
|
2016-06-18 14:40:01 +00:00
|
|
|
* Copyright (c) 2016, Conor Patrick
|
|
|
|
* All rights reserved.
|
2016-03-29 04:35:26 +00:00
|
|
|
*
|
2016-06-18 14:40:01 +00:00
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
* 2. 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.
|
|
|
|
|
2016-06-20 03:41:52 +00:00
|
|
|
*
|
|
|
|
* U2F spec: https://fidoalliance.org/specs/fido-u2f-v1.0-nfc-bt-amendment-20150514/fido-u2f-raw-message-formats.html
|
2016-03-29 04:35:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _U2F_H_
|
|
|
|
#define _U2F_H_
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#define U2F_EC_FMT_UNCOMPRESSED 0x04
|
|
|
|
|
|
|
|
#define U2F_EC_POINT_SIZE 32
|
|
|
|
#define U2F_EC_PUBKEY_SIZE 65
|
|
|
|
#define U2F_APDU_SIZE 7
|
|
|
|
#define U2F_CHALLENGE_SIZE 32
|
|
|
|
#define U2F_APPLICATION_SIZE 32
|
2017-01-28 17:26:16 +00:00
|
|
|
#define U2F_KEY_HANDLE_SIZE 36
|
2016-03-29 04:35:26 +00:00
|
|
|
#define U2F_REGISTER_REQUEST_SIZE (U2F_CHALLENGE_SIZE+U2F_APPLICATION_SIZE)
|
|
|
|
#define U2F_MAX_REQUEST_PAYLOAD (1 + U2F_CHALLENGE_SIZE+U2F_APPLICATION_SIZE + 1 + U2F_KEY_HANDLE_SIZE)
|
|
|
|
|
|
|
|
|
|
|
|
// U2F native commands
|
|
|
|
#define U2F_REGISTER 0x01
|
|
|
|
#define U2F_AUTHENTICATE 0x02
|
|
|
|
#define U2F_VERSION 0x03
|
|
|
|
#define U2F_VENDOR_FIRST 0xc0
|
|
|
|
#define U2F_VENDOR_LAST 0xff
|
|
|
|
|
|
|
|
// U2F_CMD_REGISTER command defines
|
|
|
|
#define U2F_REGISTER_ID 0x05
|
|
|
|
#define U2F_REGISTER_HASH_ID 0x00
|
|
|
|
|
|
|
|
// U2F Authenticate
|
|
|
|
#define U2F_AUTHENTICATE_CHECK 0x7
|
2016-08-17 05:18:11 +00:00
|
|
|
#define U2F_AUTHENTICATE_SIGN 0x3
|
2016-03-29 04:35:26 +00:00
|
|
|
|
|
|
|
|
2016-06-20 03:41:52 +00:00
|
|
|
// Command status responses
|
|
|
|
#define U2F_SW_NO_ERROR 0x9000
|
|
|
|
#define U2F_SW_WRONG_DATA 0x6984
|
|
|
|
#define U2F_SW_CONDITIONS_NOT_SATISFIED 0x6985
|
|
|
|
#define U2F_SW_INS_NOT_SUPPORTED 0x6d00
|
2016-08-17 05:18:11 +00:00
|
|
|
#define U2F_SW_WRONG_LENGTH 0x6700
|
|
|
|
#define U2F_SW_CLASS_NOT_SUPPORTED 0x6E00
|
|
|
|
#define U2F_SW_WRONG_PAYLOAD 0x6a80
|
2016-08-17 18:04:06 +00:00
|
|
|
#define U2F_SW_INSUFFICIENT_MEMORY 0x9210
|
2016-06-20 03:41:52 +00:00
|
|
|
|
2016-08-17 05:47:14 +00:00
|
|
|
// Delay in milliseconds to wait for user input
|
2017-01-28 17:26:16 +00:00
|
|
|
#define U2F_MS_USER_INPUT_WAIT 3000
|
2016-08-17 05:47:14 +00:00
|
|
|
|
2016-03-29 04:35:26 +00:00
|
|
|
struct u2f_request_apdu
|
|
|
|
{
|
|
|
|
uint8_t cla;
|
|
|
|
uint8_t ins;
|
|
|
|
uint8_t p1;
|
|
|
|
uint8_t p2;
|
|
|
|
uint8_t LC1;
|
|
|
|
uint8_t LC2;
|
|
|
|
uint8_t LC3;
|
|
|
|
uint8_t payload[U2F_MAX_REQUEST_PAYLOAD];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct u2f_ec_point
|
|
|
|
{
|
|
|
|
uint8_t fmt;
|
|
|
|
uint8_t x[U2F_EC_POINT_SIZE];
|
|
|
|
uint8_t y[U2F_EC_POINT_SIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct u2f_register_request
|
|
|
|
{
|
|
|
|
uint8_t chal[U2F_CHALLENGE_SIZE];
|
|
|
|
uint8_t app[U2F_APPLICATION_SIZE];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct u2f_authenticate_request
|
|
|
|
{
|
|
|
|
uint8_t chal[U2F_CHALLENGE_SIZE];
|
|
|
|
uint8_t app[U2F_APPLICATION_SIZE];
|
|
|
|
uint8_t khl;
|
|
|
|
uint8_t kh[U2F_KEY_HANDLE_SIZE];
|
|
|
|
} ;
|
|
|
|
|
2016-06-20 03:41:52 +00:00
|
|
|
// u2f_request send a U2F message to U2F protocol
|
|
|
|
// @req U2F message
|
2016-03-29 04:35:26 +00:00
|
|
|
void u2f_request(struct u2f_request_apdu* req);
|
|
|
|
|
|
|
|
|
2016-06-20 03:41:52 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////
|
|
|
|
/* Platform specific functions that must be implemented by user */
|
|
|
|
//////////////////////////////////////////////////////////////////
|
2016-03-29 04:35:26 +00:00
|
|
|
|
|
|
|
// Key id/handle for the private key for attestation
|
2017-01-28 03:07:19 +00:00
|
|
|
#define U2F_ATTESTATION_HANDLE NULL
|
2016-03-29 04:35:26 +00:00
|
|
|
|
2016-06-20 03:41:52 +00:00
|
|
|
// u2f_attestation_cert_size return size of the DER formatted attestation certificate
|
|
|
|
extern uint16_t u2f_attestation_cert_size();
|
2016-03-29 04:35:26 +00:00
|
|
|
|
2016-06-20 03:41:52 +00:00
|
|
|
// u2f_response_writeback callback for u2f to send back response data
|
2016-03-29 04:35:26 +00:00
|
|
|
// @buf data to write back
|
|
|
|
// @len length of buf in bytes
|
2016-06-20 03:41:52 +00:00
|
|
|
extern void u2f_response_writeback(uint8_t * buf, uint16_t len);
|
2016-03-29 04:35:26 +00:00
|
|
|
|
2016-06-20 03:41:52 +00:00
|
|
|
// u2f_response_flush callback when u2f finishes and will
|
2016-03-29 04:35:26 +00:00
|
|
|
// indicate when all buffer data, if any, should be written
|
2016-06-20 03:41:52 +00:00
|
|
|
extern void u2f_response_flush();
|
2016-03-29 04:35:26 +00:00
|
|
|
|
2016-06-20 03:41:52 +00:00
|
|
|
// u2f_response_start callback when u2f starts a new transaction
|
|
|
|
extern void u2f_response_start();
|
2016-03-29 04:35:26 +00:00
|
|
|
|
2016-06-20 03:41:52 +00:00
|
|
|
// u2f_get_user_feedback return 0 if user provides feedback, -1 if not
|
2016-03-29 04:35:26 +00:00
|
|
|
// This should block as long as it needs to get feedback
|
|
|
|
// before failing.
|
2016-06-20 03:41:52 +00:00
|
|
|
extern int8_t u2f_get_user_feedback();
|
2016-03-29 04:35:26 +00:00
|
|
|
|
2016-06-20 03:41:52 +00:00
|
|
|
// u2f_sha256_start callback for u2f to start a sha256 hash
|
|
|
|
extern void u2f_sha256_start();
|
2016-03-29 04:35:26 +00:00
|
|
|
|
2016-06-20 03:41:52 +00:00
|
|
|
// u2f_sha256_update callback for u2f to add data to started sha256 state
|
2016-03-29 04:35:26 +00:00
|
|
|
// @buf data to update hash with
|
|
|
|
// @len length of buf in bytes
|
2016-06-20 03:41:52 +00:00
|
|
|
extern void u2f_sha256_update(uint8_t * buf, uint8_t len);
|
2016-03-29 04:35:26 +00:00
|
|
|
|
2016-06-20 03:41:52 +00:00
|
|
|
// u2f_sha256_finish callback for u2f to havest hash from
|
2016-03-29 04:35:26 +00:00
|
|
|
// @buf final data to update hash with
|
|
|
|
// @len length of buf in bytes
|
2016-06-20 03:41:52 +00:00
|
|
|
extern void u2f_sha256_finish();
|
2016-03-29 04:35:26 +00:00
|
|
|
|
|
|
|
|
2016-06-20 03:41:52 +00:00
|
|
|
// u2f_ecdsa_sign callback for u2f to compute signature on the previously computed sha256 digest
|
2016-03-29 04:35:26 +00:00
|
|
|
// @dest atleast 64 bytes to write back signature R and S values
|
|
|
|
// @handle for the private key to use
|
|
|
|
// @return -1 for failure, 0 for success
|
2017-01-28 03:07:19 +00:00
|
|
|
extern int8_t u2f_ecdsa_sign(uint8_t * dest, uint8_t * handle, uint8_t * appid);
|
2016-03-29 04:35:26 +00:00
|
|
|
|
2016-08-17 05:18:11 +00:00
|
|
|
|
2016-06-20 03:41:52 +00:00
|
|
|
// u2f_new_keypair callback to get a new key handle
|
2016-03-29 04:35:26 +00:00
|
|
|
// @handle location to write the key handle (should be U2F_KEY_HANDLE_SIZE bytes long)
|
2017-01-28 03:07:19 +00:00
|
|
|
// @appid the new application ID to associate with key pair. 32 bytes.
|
2016-03-29 04:35:26 +00:00
|
|
|
// @pubkey location to write the public key R & S (64 bytes)
|
|
|
|
// @return -1 for failure, 0 for success
|
2016-08-17 05:18:11 +00:00
|
|
|
int8_t u2f_new_keypair(uint8_t * handle, uint8_t * appid, uint8_t * pubkey);
|
|
|
|
|
|
|
|
// u2f_appid_eq must check if app id is equal to app id associated with key pair handle
|
|
|
|
/// @handle the handle for the key pair
|
|
|
|
/// @appid the 32 byte app id to check against
|
|
|
|
/// @return non-zero not equal, 0 equal
|
|
|
|
int8_t u2f_appid_eq(uint8_t * handle, uint8_t * appid);
|
2016-03-29 04:35:26 +00:00
|
|
|
|
2016-06-20 03:41:52 +00:00
|
|
|
// u2f_load_key Load a key into memory or check to see that the handle exists
|
2016-03-29 04:35:26 +00:00
|
|
|
// @handle the key handle to check
|
|
|
|
// @return -1 if it doesn't exist, 0 for success
|
2017-01-28 03:07:19 +00:00
|
|
|
extern int8_t u2f_load_key(uint8_t * handle, uint8_t * appid);
|
2016-03-29 04:35:26 +00:00
|
|
|
|
2016-06-20 03:41:52 +00:00
|
|
|
// u2f_get_attestation_cert method to return pointer to attestation cert
|
|
|
|
extern uint8_t * u2f_get_attestation_cert();
|
2016-03-29 04:35:26 +00:00
|
|
|
|
2016-06-20 03:41:52 +00:00
|
|
|
// u2f_count Should increment a 4 byte persistent/atomic counter and return it.
|
2016-03-29 04:35:26 +00:00
|
|
|
// @return the counter
|
2016-06-20 03:41:52 +00:00
|
|
|
extern uint32_t u2f_count();
|
2016-03-29 04:35:26 +00:00
|
|
|
|
2016-06-20 03:41:52 +00:00
|
|
|
// set_response_length method to set the total length of the response for use by underlying layer
|
|
|
|
// @len the length of U2F response in bytes
|
|
|
|
extern void set_response_length(uint16_t len);
|
2016-03-29 04:35:26 +00:00
|
|
|
|
|
|
|
#endif /* U2F_H_ */
|