Skip to content

Commit aafd053

Browse files
committed
identify key handle by 8 byte hmac addition
1 parent c3bb8ce commit aafd053

File tree

11 files changed

+108
-83
lines changed

11 files changed

+108
-83
lines changed

firmware/inc/u2f.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@
3838
#define U2F_APDU_SIZE 7
3939
#define U2F_CHALLENGE_SIZE 32
4040
#define U2F_APPLICATION_SIZE 32
41-
#define U2F_KEY_HANDLE_SIZE 36
41+
#define U2F_KEY_HANDLE_ID_SIZE 8
42+
#define U2F_KEY_HANDLE_KEY_SIZE 36
43+
#define U2F_KEY_HANDLE_SIZE (U2F_KEY_HANDLE_KEY_SIZE+U2F_KEY_HANDLE_ID_SIZE)
4244
#define U2F_REGISTER_REQUEST_SIZE (U2F_CHALLENGE_SIZE+U2F_APPLICATION_SIZE)
4345
#define U2F_MAX_REQUEST_PAYLOAD (1 + U2F_CHALLENGE_SIZE+U2F_APPLICATION_SIZE + 1 + U2F_KEY_HANDLE_SIZE)
4446

firmware/src/atecc508a.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ int8_t atecc_recv(uint8_t * buf, uint8_t buflen, struct atecc_response* res)
8787
if (SMB_FLAGS & SMB_READ_TRUNC)
8888
{
8989
set_app_error(ERROR_READ_TRUNCATED);
90+
return -1;
9091
}
9192

9293
if (pkt_len <= buflen && pkt_len >= 4)

firmware/src/main.c

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,6 @@ int16_t main(void) {
141141
uint16_t ms_grad;
142142
uint8_t winks = 0, light = 1, grad_dir = 0;
143143
int8_t grad_inc = 0;
144-
int8_t ii;
145-
uint16_t i;
146144
data uint8_t xdata * clear = 0;
147145

148146
enter_DefaultMode_from_RESET();
@@ -162,7 +160,7 @@ int16_t main(void) {
162160

163161
if (RSTSRC & RSTSRC_WDTRSF__SET)
164162
{
165-
error = ERROR_DAMN_WATCHDOG;
163+
//error = ERROR_DAMN_WATCHDOG;
166164
}
167165

168166
run_tests();
@@ -259,9 +257,9 @@ int16_t main(void) {
259257
{
260258
u2f_printx("error: ", 1, (uint16_t)error);
261259
#ifdef U2F_BLINK_ERRORS
262-
for (ii=0; ii < 8; ii++)
260+
for (ms_grad=0; ms_grad < 8; ms_grad++)
263261
{
264-
if (error & (1<<ii))
262+
if (error & (1<<ms_grad))
265263
{
266264
rgb_hex(U2F_DEFAULT_COLOR_INPUT_SUCCESS);
267265
}
@@ -277,17 +275,16 @@ int16_t main(void) {
277275
#else
278276
rgb_hex(U2F_DEFAULT_COLOR_ERROR);
279277
// wipe ram
280-
for (i=0; i<0x400;i++)
278+
for (ms_grad=0; ms_grad<0x400;ms_grad++)
281279
{
282280
*(clear++) = 0x0;
283281
watchdog();
284282
}
285283
#endif
286-
error = 0;
287-
while(!ms_since(ms_heart,500))
288-
{
289-
watchdog();
290-
}
284+
285+
// wait for watchdog to reset
286+
while(1)
287+
;
291288
}
292289

293290

firmware/src/u2f.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ static int16_t u2f_authenticate(struct u2f_authenticate_request * req, uint8_t c
146146
if (control == U2F_AUTHENTICATE_CHECK)
147147
{
148148
u2f_hid_set_len(2);
149-
if (u2f_load_key(req->kh) == 0 )//&& u2f_appid_eq(req->kh, req->app) == 0)
149+
if (u2f_appid_eq(req->kh, req->app) == 0)
150150
{
151151
return U2F_SW_CONDITIONS_NOT_SATISFIED;
152152
}
@@ -155,12 +155,13 @@ static int16_t u2f_authenticate(struct u2f_authenticate_request * req, uint8_t c
155155
return U2F_SW_WRONG_DATA;
156156
}
157157
}
158-
if (
158+
if (
159159
control != U2F_AUTHENTICATE_SIGN ||
160160
req->khl != U2F_KEY_HANDLE_SIZE ||
161-
u2f_load_key(req->kh, req->app) != 0 //||
162-
//u2f_appid_eq(req->kh, req->app) != 0
163-
)
161+
u2f_appid_eq(req->kh, req->app) != 0 || // Order of checks is important
162+
u2f_load_key(req->kh, req->app) != 0
163+
164+
)
164165
{
165166
u2f_hid_set_len(2);
166167
return U2F_SW_WRONG_PAYLOAD;

firmware/src/u2f_atecc.c

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
*/
3131

3232
#include "app.h"
33+
34+
#undef U2F_DISABLE
3335
#ifndef U2F_DISABLE
3436
#include "bsp.h"
3537
#include "u2f.h"
@@ -38,6 +40,7 @@
3840
#include "atecc508a.h"
3941

4042

43+
static void gen_u2f_zero_tag(uint8_t * dst, uint8_t * appid);
4144

4245
static struct u2f_hid_msg res;
4346
static uint8_t* resbuf = (uint8_t*)&res;
@@ -152,14 +155,14 @@ static int atecc_prep_encryption()
152155
appdata.tmp, 32,
153156
appdata.tmp, 40, &res) != 0 )
154157
{
155-
u2f_prints("pass through to tempkey failed\r\n");
158+
// u2f_prints("pass through to tempkey failed\r\n");
156159
return -1;
157160
}
158161
if( atecc_send_recv(ATECC_CMD_GENDIG,
159162
ATECC_RW_DATA, U2F_MASTER_KEY_SLOT, NULL, 0,
160163
appdata.tmp, 40, &res) != 0)
161164
{
162-
u2f_prints("GENDIG failed\r\n");
165+
// u2f_prints("GENDIG failed\r\n");
163166
return -1;
164167
}
165168

@@ -233,7 +236,6 @@ int8_t u2f_ecdsa_sign(uint8_t * dest, uint8_t * handle, uint8_t * appid)
233236
}
234237

235238

236-
237239
// bad if this gets interrupted
238240
int8_t u2f_new_keypair(uint8_t * handle, uint8_t * appid, uint8_t * pubkey)
239241
{
@@ -242,6 +244,8 @@ int8_t u2f_new_keypair(uint8_t * handle, uint8_t * appid, uint8_t * pubkey)
242244
int i;
243245

244246
watchdog();
247+
// u2f_prints("new key appid,khandle\r\n");
248+
// dump_hex(appid,32);
245249

246250
if (atecc_send_recv(ATECC_CMD_RNG,ATECC_RNG_P1,ATECC_RNG_P2,
247251
NULL, 0,
@@ -270,7 +274,7 @@ int8_t u2f_new_keypair(uint8_t * handle, uint8_t * appid, uint8_t * pubkey)
270274
}
271275
watchdog();
272276
compute_key_hash(private_key, WMASK);
273-
memmove(handle+4, res_digest.buf, 32); // size of key handle must be 36
277+
memmove(handle+4, res_digest.buf, 32); // size of key handle must be 36+8
274278

275279

276280
if ( atecc_privwrite(U2F_TEMP_KEY_SLOT, private_key, WMASK, handle+4) != 0)
@@ -289,15 +293,21 @@ int8_t u2f_new_keypair(uint8_t * handle, uint8_t * appid, uint8_t * pubkey)
289293

290294
memmove(pubkey, res.buf, 64);
291295

296+
// the + 8
297+
gen_u2f_zero_tag(handle + U2F_KEY_HANDLE_KEY_SIZE, appid);
298+
//dump_hex(handle,U2F_KEY_HANDLE_SIZE);
299+
292300
return 0;
293301
}
294302

295303
int8_t u2f_load_key(uint8_t * handle, uint8_t * appid)
296304
{
297-
struct atecc_response res;
298305
uint8_t private_key[36];
299306
int i;
300307

308+
// u2f_prints("load key appid,rnum\r\n");
309+
// dump_hex(appid,32);
310+
// dump_hex(handle,4);
301311
SHA_HMAC_KEY = U2F_MASTER_KEY_SLOT;
302312
SHA_FLAGS = ATECC_SHA_HMACSTART;
303313
u2f_sha256_start();
@@ -316,30 +326,24 @@ int8_t u2f_load_key(uint8_t * handle, uint8_t * appid)
316326
return atecc_privwrite(U2F_TEMP_KEY_SLOT, private_key, WMASK, handle+4);
317327
}
318328

329+
static void gen_u2f_zero_tag(uint8_t * dst, uint8_t * appid)
330+
{
331+
const char * u2f_zero_const = "\xc1\xff\x67\x0d\x66\xe5\x55\xbb\xdc\x56\xaf\x7b\x41\x27\x4a\x21";
332+
SHA_HMAC_KEY = U2F_MASTER_KEY_SLOT;
333+
SHA_FLAGS = ATECC_SHA_HMACSTART;
334+
u2f_sha256_start();
335+
u2f_sha256_update(appid,32);
336+
u2f_sha256_update(u2f_zero_const,16);
337+
SHA_FLAGS = ATECC_SHA_HMACEND;
338+
u2f_sha256_finish();
339+
340+
if (dst) memmove(dst, res_digest.buf, U2F_KEY_HANDLE_ID_SIZE);
341+
}
342+
319343
int8_t u2f_appid_eq(uint8_t * handle, uint8_t * appid)
320344
{
321-
// struct atecc_response res;
322-
// uint8_t private_key[36];
323-
// int i;
324-
//
325-
// SHA_HMAC_KEY = U2F_MASTER_KEY_SLOT;
326-
// SHA_FLAGS = ATECC_SHA_HMACSTART;
327-
// u2f_sha256_start();
328-
// u2f_sha256_update(appid,32);
329-
// SHA_FLAGS = ATECC_SHA_HMACEND;
330-
// u2f_sha256_finish();
331-
//
332-
// memset(private_key,0,4);
333-
// memmove(private_key+4, res_digest.buf, 32);
334-
//
335-
// for (i=4; i<36; i++)
336-
// {
337-
// private_key[i] ^= RMASK[i];
338-
// }
339-
//
340-
// compute_key_hash(private_key, WMASK);
341-
// return memcmp(handle, res_digest.buf, U2F_KEY_HANDLE_SIZE);
342-
return 0;
345+
gen_u2f_zero_tag(NULL,appid);
346+
return memcmp(handle+U2F_KEY_HANDLE_KEY_SIZE, res_digest.buf, U2F_KEY_HANDLE_ID_SIZE);
343347
}
344348

345349
uint32_t u2f_count()

firmware/src/u2f_hid.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ static struct hid_layer_param
7777
// total length of response in bytes
7878
uint16_t res_len;
7979

80-
#define BUFFER_SIZE 270
80+
#define BUFFER_SIZE (270 - 70)
8181
uint8_t buffer[BUFFER_SIZE];
8282

8383
} hid_layer;
@@ -87,7 +87,7 @@ uint32_t _hid_lockt = 0;
8787
uint32_t _hid_lock_cid = 0;
8888
#endif
8989

90-
static struct CID CIDS[5];
90+
static struct CID CIDS[4];
9191

9292
static uint8_t CID_NUM = 0;
9393

@@ -149,7 +149,6 @@ void u2f_hid_writeback(uint8_t * payload, uint16_t len)
149149

150150
do
151151
{
152-
153152
if (_hid_offset == 0)
154153
{
155154
r->cid = hid_layer.current_cid;
@@ -354,24 +353,19 @@ static uint8_t hid_u2f_parse(struct u2f_hid_msg* req)
354353

355354
break;
356355
case U2FHID_MSG:
357-
358-
if (U2FHID_LEN(req) < 4)
359-
{
360-
stamp_error(hid_layer.current_cid, ERR_INVALID_LEN);
361-
goto fail;
362-
}
363-
// buffer 2 payloads (120 bytes) to get full U2F message
364-
// assuming key handle is < 45 bytes
365-
// 7 bytes for apdu header
366-
// 7 + 66 bytes + key handle for authenticate message
367-
// 7 + 64 for register message
368356
if (hid_layer.bytes_buffered == 0)
369357
{
358+
if (U2FHID_LEN(req) < 4)
359+
{
360+
stamp_error(hid_layer.current_cid, ERR_INVALID_LEN);
361+
goto fail;
362+
}
370363
start_buffering(req);
371364
if (hid_layer.bytes_buffered >= U2FHID_LEN(req))
372365
{
373366
u2f_request((struct u2f_request_apdu *)hid_layer.buffer);
374367
}
368+
375369
}
376370
else
377371
{
@@ -380,6 +374,7 @@ static uint8_t hid_u2f_parse(struct u2f_hid_msg* req)
380374
{
381375
u2f_request((struct u2f_request_apdu *)hid_layer.buffer);
382376
}
377+
383378
}
384379

385380

tools/flashing/erase.sh

Lines changed: 0 additions & 3 deletions
This file was deleted.

tools/flashing/program.sh

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11
#!/bin/bash
22

3-
# silabs utility debugger file debugger id power C2
4-
FlashUtilCL.exe DownloadUSB -R $1 "$2" 0 1
3+
4+
ret=$(curl --request POST http://127.0.0.1:4040/ --data "port=$2" --data "firmware=$(cat "$1")")
5+
6+
if [[ $ret != *"Success"* ]]
7+
then
8+
exit 1
9+
fi
10+
11+
exit 0
12+
#export FW=$2
13+
14+
#PORT=$1 python - <<END
15+
16+
#import requests, sys, os
17+
18+
19+
#url = 'http://127.0.0.1:4040/'
20+
21+
#payload = {'port': os.environ['PORT'], 'firmware': open(os.environ['FW'], 'r').read()}
22+
23+
#print requests.post(url, data = payload)
24+
25+
#END

tools/monitor.sh

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
export PATH=$PATH:`pwd`/flashing:../../../u2f_zero_client:../../../gencert
55

66
export attest_priv=gencert/ca/key.pem
7-
export attest_pub=gencert/ca/cert.der
7+
export attest_pub=gencert/ca/attest.der
88
adapters[0]=0
9-
num_adapters=0
9+
adapters[1]=COM3
10+
adapters[2]=COM4
11+
num_adapters=2
1012
firmware=../firmware
1113
export setup=setup_device.sh
12-
export starting_SN=CAFEBABE00000000
14+
export starting_SN=DAFE1E340AB70000
1315
setup_SNs=(0 CAFEBABEFFFFFFF0 CAFEBABEFFFFFFF1 CAFEBABEFFFFFFF2)
1416

1517
if [[ -n "$1" ]] ; then
@@ -57,17 +59,17 @@ function start_programming {
5759
}
5860

5961

60-
for i in `seq 1 100` ; do
62+
#for i in `seq 1 100` ; do
6163

62-
adapters[$i]=$(FlashUtilCL.exe DeviceSN $i)
64+
#adapters[$i]=$(FlashUtilCL.exe DeviceSN $i)
6365

64-
if [[ ${adapters[$i]} = *"out of range"* ]]
65-
then
66-
break
67-
fi
66+
#if [[ ${adapters[$i]} = *"out of range"* ]]
67+
#then
68+
#break
69+
#fi
6870

69-
num_adapters=$(($num_adapters + 1))
70-
done
71+
#num_adapters=$(($num_adapters + 1))
72+
#done
7173

7274
export num_adapters=$num_adapters
7375
export adapters=$adapters

tools/setup_device.sh

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,18 @@ if [[ $FLASH_TOOLS = 1 ]]
3232
then
3333

3434
# setup atecc
35-
echo "erasing..."
36-
erase.sh $SN
35+
#echo "erasing..."
36+
#erase.sh $SN
3737

38+
echo "programming setup..."
39+
program.sh $SETUP_HEX $SN
40+
3841
while [[ "$?" -ne "0" ]] ; do
39-
echo "$SN is retrying erase ... "
42+
echo "$SN is retrying program... "
4043
sleep 0.2
41-
erase.sh $SN
44+
program.sh $SETUP_HEX $SN
4245
done
4346

44-
echo "programming setup..."
45-
program.sh $SETUP_HEX $SN
46-
47-
[[ "$?" -ne "0" ]] && exit 1
48-
4947
fi
5048

5149
echo "configuring..."

0 commit comments

Comments
 (0)