added CDID function. fix bug is CDID wrapper
This commit is contained in:
parent
a46958aedf
commit
53a10fa0f8
126
trial1/layer1.c
126
trial1/layer1.c
@ -1,10 +1,13 @@
|
|||||||
#include "layer1.h"
|
#include "layer1.h"
|
||||||
|
|
||||||
|
|
||||||
//Layer 1 functions
|
//ROM functions
|
||||||
|
|
||||||
#define UDSFILE "./out/RANDFILE"
|
#define UDSFILE "./out/RANDFILE"
|
||||||
#define UDSsize 8 //bytes
|
#define UDSsize 8 //bytes
|
||||||
|
#define FW_file "layer1.c"
|
||||||
|
#define FW_size 1000
|
||||||
|
////need to find a way to determine file size using BIO tools
|
||||||
|
|
||||||
|
|
||||||
int createUDS()
|
int createUDS()
|
||||||
@ -80,53 +83,128 @@ int readFWID(uint8_t * FW_M)
|
|||||||
//1. Read layer1.c into memory
|
//1. Read layer1.c into memory
|
||||||
//2. Calcualte hash into arg
|
//2. Calcualte hash into arg
|
||||||
|
|
||||||
printf("in readFWID\n");
|
//uint8_t * source;
|
||||||
|
//FW_size shoudl not be static.
|
||||||
uint8_t * source = NULL;
|
//Use indefinite array or determine FW_size
|
||||||
|
uint8_t* source = calloc(1,sizeof(uint8_t)*(FW_size));
|
||||||
BIO *fp, *out;
|
BIO *fp, *out;
|
||||||
int filesize = 0;
|
int buf_size = 0;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
out = BIO_new_fp(stdout, BIO_NOCLOSE);
|
out = BIO_new_fp(stdout, BIO_NOCLOSE);
|
||||||
fp = BIO_new_file("layer1.c", "r");
|
fp = BIO_new_file(FW_file, "r");
|
||||||
printf("in 2readFWID\n");
|
|
||||||
if(!fp)
|
if(!fp)
|
||||||
perror("Opening FW to read failed\n");
|
perror("Opening FW to read failed\n");
|
||||||
|
|
||||||
if(BIO_eof(fp))
|
if(BIO_eof(fp))
|
||||||
perror("File empty\n");
|
perror("File empty\n");
|
||||||
printf("in 3readFWID\n"); //SEG FAULT!
|
|
||||||
while(!BIO_eof(fp))
|
while(!BIO_eof(fp) && (buf_size < FW_size))
|
||||||
{
|
{
|
||||||
BIO_read(fp, source[filesize], 1);
|
BIO_read(fp, source[buf_size], 1);
|
||||||
filesize++; //filesize includes EOF
|
buf_size++; //buf_size includes EOF
|
||||||
|
//break;
|
||||||
}
|
}
|
||||||
printf("in 4readFWID\n");
|
|
||||||
if(SHA256(source, filesize -1, FW_M) == NULL)
|
if(SHA256(source, buf_size -1, FW_M) == NULL)
|
||||||
perror("FW measurement failed\n");
|
perror("FW measurement failed\n");
|
||||||
|
|
||||||
|
|
||||||
//print block
|
//print block
|
||||||
printf("in 5readFWID\n");
|
|
||||||
BIO_printf(out,"FW digest : ");
|
BIO_printf(out,"FW digest : ");
|
||||||
for(i = 0; i < SHA256_dig_t; i++)
|
for(i = 0; i < SHA256_dig_t; i++)
|
||||||
BIO_printf(out,"%x",FW_M[i]);
|
BIO_printf(out,"%x",FW_M[i]);
|
||||||
BIO_printf(out, "\n");
|
BIO_printf(out, "\n");
|
||||||
|
|
||||||
//free(source);
|
free(source);
|
||||||
BIO_free(fp);
|
BIO_free(fp);
|
||||||
BIO_free(out);
|
BIO_free(out);
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// int calcCDID(uint8_t * UDS_M, uint8_t * FW_M, uint8_t * CDID)
|
int calcCDID(uint8_t * UDS_M, uint8_t * FW_M, uint8_t * CDID)
|
||||||
// {
|
{
|
||||||
// //0. internally call readUDS and readFWID? abstraction of UDS against layer2
|
//0. internally call readUDS and readFWID? abstraction of UDS against layer2
|
||||||
|
|
||||||
// //1. create sha256 context
|
//1. create sha256 context
|
||||||
// //2. add UDS hash
|
//2. add UDS hash
|
||||||
// //3. add FW hash
|
//3. add FW hash
|
||||||
// //4. calc composite hash into CDID arg
|
//4. calc composite hash into CDID arg
|
||||||
// return 1;
|
|
||||||
// }
|
BIO * out = BIO_new_fp(stdout, BIO_NOCLOSE);
|
||||||
|
|
||||||
|
SHA256_CTX *ctx;
|
||||||
|
if(!SHA256_Init(ctx))
|
||||||
|
perror("SHA init failed\n");
|
||||||
|
|
||||||
|
if(!SHA256_Update(ctx, UDS_M, UDSsize))
|
||||||
|
perror("SHA update failed\n");
|
||||||
|
if(!SHA256_Update(ctx, FW_M, FW_size))
|
||||||
|
perror("SHA update2 failed\n");
|
||||||
|
|
||||||
|
if(!SHA256_Final(CDID, ctx))
|
||||||
|
perror("SHA close failed\n");
|
||||||
|
|
||||||
|
//print block
|
||||||
|
|
||||||
|
BIO_printf(out,"UDID_M : ");
|
||||||
|
for(int i = 0; i < SHA256_dig_t; i++)
|
||||||
|
BIO_printf(out,"%x",UDS_M[i]);
|
||||||
|
BIO_printf(out, "\n");
|
||||||
|
|
||||||
|
BIO_printf(out,"FWID_M : ");
|
||||||
|
for(int i = 0; i < SHA256_dig_t; i++)
|
||||||
|
BIO_printf(out,"%x",FW_M[i]);
|
||||||
|
BIO_printf(out, "\n");
|
||||||
|
|
||||||
|
BIO_printf(out,"CDI : ");
|
||||||
|
for(int i = 0; i < SHA256_dig_t; i++)
|
||||||
|
BIO_printf(out,"%x",CDID[i]);
|
||||||
|
BIO_printf(out, "\n");
|
||||||
|
BIO_free(out);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int _calcCDID(uint8_t * _CDID)
|
||||||
|
{
|
||||||
|
|
||||||
|
BIO * out = BIO_new_fp(stdout, BIO_NOCLOSE);
|
||||||
|
BIO_printf(out, "\n");BIO_printf(out, "\n");BIO_printf(out, "\n");
|
||||||
|
|
||||||
|
//step 1 : Derive Device ID
|
||||||
|
uint8_t* UDS_ID = calloc(1,sizeof(uint8_t)*SHA256_dig_t);
|
||||||
|
readUDS(UDS_ID);
|
||||||
|
|
||||||
|
//step 2 : Derive Firmware ID
|
||||||
|
uint8_t* FW_ID = calloc(1,sizeof(uint8_t)*SHA256_dig_t);
|
||||||
|
readFWID(FW_ID);
|
||||||
|
|
||||||
|
//step3 : call calcCDID
|
||||||
|
calcCDID(UDS_ID,FW_ID,_CDID);
|
||||||
|
calcCDID(UDS_ID,FW_ID,_CDID);
|
||||||
|
calcCDID(UDS_ID,FW_ID,_CDID);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
BIO_printf(out,"_UDID : ");
|
||||||
|
for(int i = 0; i < SHA256_dig_t; i++)
|
||||||
|
BIO_printf(out,"%x",UDS_ID[i]);
|
||||||
|
BIO_printf(out, "\n");
|
||||||
|
|
||||||
|
BIO_printf(out,"_FWID : ");
|
||||||
|
for(int i = 0; i < SHA256_dig_t; i++)
|
||||||
|
BIO_printf(out,"%x",FW_ID[i]);
|
||||||
|
BIO_printf(out, "\n");
|
||||||
|
|
||||||
|
|
||||||
|
BIO_printf(out,"_CDI : ");
|
||||||
|
for(int i = 0; i < SHA256_dig_t; i++)
|
||||||
|
BIO_printf(out,"%x",_CDID[i]);
|
||||||
|
BIO_printf(out, "\n");
|
||||||
|
BIO_free(out);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
@ -11,5 +11,5 @@ int readUDS(uint8_t* UDSdigest);
|
|||||||
int createUDS();
|
int createUDS();
|
||||||
int readFWID(uint8_t * FW_M);
|
int readFWID(uint8_t * FW_M);
|
||||||
int calcCDID(uint8_t * UDS_M, uint8_t * FW_M, uint8_t * CDID);
|
int calcCDID(uint8_t * UDS_M, uint8_t * FW_M, uint8_t * CDID);
|
||||||
|
int _calcCDID(uint8_t * CDID);
|
||||||
|
|
||||||
|
@ -9,19 +9,31 @@ void startProtocol()
|
|||||||
uint8_t* UDS_ID = calloc(1,sizeof(uint8_t)*SHA256_dig_t);
|
uint8_t* UDS_ID = calloc(1,sizeof(uint8_t)*SHA256_dig_t);
|
||||||
readUDS(UDS_ID);
|
readUDS(UDS_ID);
|
||||||
|
|
||||||
|
// for(int i = 0; i < SHA256_dig_t; i++)
|
||||||
|
// printf("%x",UDS_ID[i]);
|
||||||
|
// printf("\n");
|
||||||
|
|
||||||
//step 2 : Derive Firmware ID
|
//step 2 : Derive Firmware ID
|
||||||
uint8_t* FW_ID = calloc(1,sizeof(uint8_t)*SHA256_dig_t);
|
uint8_t* FW_ID = calloc(1,sizeof(uint8_t)*SHA256_dig_t);
|
||||||
readFWID(FW_ID);
|
readFWID(FW_ID);
|
||||||
|
|
||||||
|
// for(int i = 0; i < SHA256_dig_t; i++)
|
||||||
|
// printf("%x",FW_ID[i]);
|
||||||
|
// printf("\n");
|
||||||
|
|
||||||
// //setp 3 : Derive Composite Device ID
|
|
||||||
// uint8_t* CD_ID = calloc(1,sizeof(uint8_t)*SHA256_dig_t);
|
|
||||||
// calcCDID(CD_ID);
|
|
||||||
|
|
||||||
|
//setp 3 : Derive Composite Device ID
|
||||||
|
uint8_t* CD_ID = calloc(1,sizeof(uint8_t)*SHA256_dig_t);
|
||||||
|
calcCDID(UDS_ID,FW_ID,CD_ID);
|
||||||
|
|
||||||
|
|
||||||
|
uint8_t* _CD_ID = calloc(1,sizeof(uint8_t)*SHA256_dig_t);
|
||||||
|
_calcCDID(_CD_ID);
|
||||||
|
|
||||||
//End block
|
//End block
|
||||||
free(UDS_ID);
|
free(UDS_ID);
|
||||||
free(FW_ID);
|
free(FW_ID);
|
||||||
// free(CD_ID);
|
free(CD_ID);
|
||||||
|
free(_CD_ID);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BIN
trial1/out/main2
BIN
trial1/out/main2
Binary file not shown.
Loading…
Reference in New Issue
Block a user