FWID seg fault
This commit is contained in:
parent
351686119b
commit
a46958aedf
132
trial1/layer1.c
Normal file
132
trial1/layer1.c
Normal file
@ -0,0 +1,132 @@
|
|||||||
|
#include "layer1.h"
|
||||||
|
|
||||||
|
|
||||||
|
//Layer 1 functions
|
||||||
|
|
||||||
|
#define UDSFILE "./out/RANDFILE"
|
||||||
|
#define UDSsize 8 //bytes
|
||||||
|
|
||||||
|
|
||||||
|
int createUDS()
|
||||||
|
{
|
||||||
|
int ret = RAND_write_file(UDSFILE);
|
||||||
|
if (ret == -1)
|
||||||
|
perror("rand write to file failed\n");
|
||||||
|
printf("\n %d bytes written to file\n", ret);
|
||||||
|
//readUDS();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int readUDS(uint8_t* UDS_M)
|
||||||
|
{
|
||||||
|
//1. Create RANDFILE if it does not exist. skip if it exists
|
||||||
|
//2. Load RANDFILE
|
||||||
|
//3. Read 64 bits to UDSbuf and compute hash into input arg.
|
||||||
|
//4. free UDSbuf
|
||||||
|
|
||||||
|
BIO *fp, *out;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
//uint8_t UDSbuf[UDSsize] = {0};
|
||||||
|
//uint8_t UDS_M[SHA256_dig_t] = {0};
|
||||||
|
|
||||||
|
uint8_t* UDSbuf = calloc(1,sizeof(uint8_t)*UDSsize);
|
||||||
|
//uint8_t* UDS_M = calloc(1,sizeof(uint8_t)*SHA256_dig_t);
|
||||||
|
|
||||||
|
|
||||||
|
out = BIO_new_fp(stdout, BIO_NOCLOSE);
|
||||||
|
|
||||||
|
int ret = RAND_load_file(UDSFILE, 8);
|
||||||
|
while (ret < 0)
|
||||||
|
{
|
||||||
|
perror("Could not load seed file\n");
|
||||||
|
BIO_printf(out,"Creating new seed file\n");
|
||||||
|
createUDS();
|
||||||
|
ret = RAND_load_file(UDSFILE, 8);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
fp = BIO_new_file(UDSFILE, "r");
|
||||||
|
if(!fp)
|
||||||
|
perror("Opening seed file to read failed\n");
|
||||||
|
|
||||||
|
if (BIO_read(fp,UDSbuf,UDSsize) < 0)
|
||||||
|
perror("BIO read failed\n");
|
||||||
|
|
||||||
|
//Compute hash of UDS
|
||||||
|
if(SHA256(UDSbuf, UDSsize, UDS_M) == NULL)
|
||||||
|
perror("UDS measurement failed\n");
|
||||||
|
|
||||||
|
//Print block. delete later
|
||||||
|
for(i = 0; i < UDSsize; i++)
|
||||||
|
BIO_printf(out,"%x",UDSbuf[i]);
|
||||||
|
BIO_printf(out, "\n");
|
||||||
|
|
||||||
|
BIO_printf(out,"UDS digest : ");
|
||||||
|
for(i = 0; i < SHA256_dig_t; i++)
|
||||||
|
BIO_printf(out,"%x",UDS_M[i]);
|
||||||
|
BIO_printf(out, "\n");
|
||||||
|
|
||||||
|
free(UDSbuf);
|
||||||
|
BIO_free(fp);
|
||||||
|
BIO_free(out);
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int readFWID(uint8_t * FW_M)
|
||||||
|
{
|
||||||
|
//1. Read layer1.c into memory
|
||||||
|
//2. Calcualte hash into arg
|
||||||
|
|
||||||
|
printf("in readFWID\n");
|
||||||
|
|
||||||
|
uint8_t * source = NULL;
|
||||||
|
BIO *fp, *out;
|
||||||
|
int filesize = 0;
|
||||||
|
int i;
|
||||||
|
|
||||||
|
out = BIO_new_fp(stdout, BIO_NOCLOSE);
|
||||||
|
fp = BIO_new_file("layer1.c", "r");
|
||||||
|
printf("in 2readFWID\n");
|
||||||
|
if(!fp)
|
||||||
|
perror("Opening FW to read failed\n");
|
||||||
|
|
||||||
|
if(BIO_eof(fp))
|
||||||
|
perror("File empty\n");
|
||||||
|
printf("in 3readFWID\n"); //SEG FAULT!
|
||||||
|
while(!BIO_eof(fp))
|
||||||
|
{
|
||||||
|
BIO_read(fp, source[filesize], 1);
|
||||||
|
filesize++; //filesize includes EOF
|
||||||
|
}
|
||||||
|
printf("in 4readFWID\n");
|
||||||
|
if(SHA256(source, filesize -1, FW_M) == NULL)
|
||||||
|
perror("FW measurement failed\n");
|
||||||
|
|
||||||
|
|
||||||
|
//print block
|
||||||
|
printf("in 5readFWID\n");
|
||||||
|
BIO_printf(out,"FW digest : ");
|
||||||
|
for(i = 0; i < SHA256_dig_t; i++)
|
||||||
|
BIO_printf(out,"%x",FW_M[i]);
|
||||||
|
BIO_printf(out, "\n");
|
||||||
|
|
||||||
|
//free(source);
|
||||||
|
BIO_free(fp);
|
||||||
|
BIO_free(out);
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// int calcCDID(uint8_t * UDS_M, uint8_t * FW_M, uint8_t * CDID)
|
||||||
|
// {
|
||||||
|
// //0. internally call readUDS and readFWID? abstraction of UDS against layer2
|
||||||
|
|
||||||
|
// //1. create sha256 context
|
||||||
|
// //2. add UDS hash
|
||||||
|
// //3. add FW hash
|
||||||
|
// //4. calc composite hash into CDID arg
|
||||||
|
// return 1;
|
||||||
|
// }
|
15
trial1/layer1.h
Normal file
15
trial1/layer1.h
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <openssl/rand.h>
|
||||||
|
#include <openssl/bio.h>
|
||||||
|
#include <openssl/err.h>
|
||||||
|
#include <openssl/sha.h>
|
||||||
|
|
||||||
|
#define SHA256_dig_t 32 //bytes
|
||||||
|
|
||||||
|
int readUDS(uint8_t* UDSdigest);
|
||||||
|
int createUDS();
|
||||||
|
int readFWID(uint8_t * FW_M);
|
||||||
|
int calcCDID(uint8_t * UDS_M, uint8_t * FW_M, uint8_t * CDID);
|
||||||
|
|
||||||
|
|
27
trial1/layer2.c
Normal file
27
trial1/layer2.c
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include "layer2.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void startProtocol()
|
||||||
|
{
|
||||||
|
|
||||||
|
//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);
|
||||||
|
|
||||||
|
|
||||||
|
// //setp 3 : Derive Composite Device ID
|
||||||
|
// uint8_t* CD_ID = calloc(1,sizeof(uint8_t)*SHA256_dig_t);
|
||||||
|
// calcCDID(CD_ID);
|
||||||
|
|
||||||
|
|
||||||
|
//End block
|
||||||
|
free(UDS_ID);
|
||||||
|
free(FW_ID);
|
||||||
|
// free(CD_ID);
|
||||||
|
}
|
||||||
|
|
13
trial1/layer2.h
Normal file
13
trial1/layer2.h
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
#include "layer1.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <openssl/rand.h>
|
||||||
|
#include <openssl/bio.h>
|
||||||
|
#include <openssl/err.h>
|
||||||
|
#include <openssl/sha.h>
|
||||||
|
|
||||||
|
|
||||||
|
#define SHA256_dig_t 32 //bytes
|
||||||
|
|
||||||
|
void startProtocol();
|
Loading…
Reference in New Issue
Block a user