3030 */
3131
3232#include "app.h"
33+
34+ #undef U2F_DISABLE
3335#ifndef U2F_DISABLE
3436#include "bsp.h"
3537#include "u2f.h"
3840#include "atecc508a.h"
3941
4042
43+ static void gen_u2f_zero_tag (uint8_t * dst , uint8_t * appid );
4144
4245static struct u2f_hid_msg res ;
4346static 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
238240int8_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
295303int8_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+
319343int8_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
345349uint32_t u2f_count ()
0 commit comments