random bit not in executable

This commit is contained in:
NIIBE Yutaka
2011-02-07 11:57:27 +09:00
parent 103484c44a
commit 21bcf76d36
8 changed files with 72 additions and 43 deletions

View File

@@ -204,19 +204,10 @@ endif
include $(CHIBIOS)/os/ports/GCC/ARM/rules.mk
OBJS += random-data.o
OUTFILES += random_bits
random_bits:
dd if=/dev/random bs=1 of=random_bits count=1024
random-data.o: random_bits
$(CP) -I binary $< -O elf32-littlearm -B arm \
--rename-section \
.data=.gnuk_random,alloc,load,readonly,data,contents \
$@
$(PROJECT).elf: random-data.o
distclean: clean
-rm -f Makefile gnuk.ld config.h

View File

@@ -600,10 +600,11 @@ flash_check_blank (const uint8_t *page, int size)
int
flash_erase_binary (uint8_t file_id)
{
const uint8_t *p = &ch_certificate_start;
const uint8_t *p;
if (file_id == FILEID_CH_CERTIFICATE)
{
p = &ch_certificate_start;
if (flash_check_blank (p, FLASH_CH_CERTIFICATE_SIZE) == 0)
{
flash_erase_page ((uint32_t)p);
@@ -612,6 +613,15 @@ flash_erase_binary (uint8_t file_id)
#endif
}
return 0;
}
else if (file_id == FILEID_RANDOM)
{
p = &random_bits_start;
if (flash_check_blank (p, FLASH_PAGE_SIZE) == 0)
flash_erase_page ((uint32_t)p);
return 0;
}
else
@@ -623,29 +633,39 @@ int
flash_write_binary (uint8_t file_id, const uint8_t *data,
uint16_t len, uint16_t offset)
{
uint16_t maxsize;
const uint8_t *p;
if (file_id == FILEID_CH_CERTIFICATE)
{
if (offset + len > FLASH_CH_CERTIFICATE_SIZE || (offset&1) || (len&1))
return -1;
else
{
const uint8_t *p = &ch_certificate_start;
uint16_t hw;
uint32_t addr;
int i;
addr = (uint32_t)p + offset;
for (i = 0; i < len/2; i++)
{
hw = data[i*2] | (data[i*2+1]<<8);
if (flash_program_halfword (addr, hw) != FLASH_COMPLETE)
flash_warning ("DO WRITE ERROR");
addr += 2;
}
return 0;
}
maxsize = FLASH_CH_CERTIFICATE_SIZE;
p = &ch_certificate_start;
}
else if (file_id == FILEID_RANDOM)
{
maxsize = FLASH_PAGE_SIZE;
p = &random_bits_start;
}
else
return -1;
if (offset + len > maxsize || (offset&1) || (len&1))
return -1;
else
{
uint16_t hw;
uint32_t addr;
int i;
addr = (uint32_t)p + offset;
for (i = 0; i < len/2; i++)
{
hw = data[i*2] | (data[i*2+1]<<8);
if (flash_program_halfword (addr, hw) != FLASH_COMPLETE)
flash_warning ("DO WRITE ERROR");
addr += 2;
}
return 0;
}
}

View File

@@ -36,7 +36,7 @@ extern void *memmove(void *dest, const void *src, size_t n);
extern int icc_data_size;
#define cmd_APDU_size icc_data_size
extern int res_APDU_size;
extern uint8_t *res_APDU_pointer;
extern const uint8_t *res_APDU_pointer;
/* USB buffer size of LL (Low-level): size of single Bulk transaction */
#define USB_LL_BUF_SIZE 64
@@ -123,6 +123,7 @@ extern int flash_write_binary (uint8_t file_id, const uint8_t *data, uint16_t le
/* Linker set this symbol */
extern uint8_t ch_certificate_start;
extern uint8_t random_bits_start;
#define KEY_MAGIC_LEN 8
#define KEY_CONTENT_LEN 256 /* p and q */

View File

@@ -117,7 +117,8 @@ SECTIONS
.gnuk_random :
{
. = ALIGN (@FLASH_PAGE_SIZE@);
*(.gnuk_random)
random_bits_start = .;
. += 1;
. = ALIGN (@FLASH_PAGE_SIZE@);
} > flash =0xffffffff

View File

@@ -25,24 +25,22 @@
#include "ch.h"
#include "gnuk.h"
extern void *_binary_random_bits_start;
const uint8_t *
random_bytes_get (void)
{
uint32_t addr, addr0;
addr = (uint32_t)&_binary_random_bits_start + ((hardclock () << 5) & 0x3e0);
addr = (uint32_t)&random_bits_start + ((hardclock () << 5) & 0x3e0);
addr0 = addr;
while (1)
{
if (*(uint32_t *)addr != 0)
if (*(uint32_t *)addr != 0 && *(uint32_t *)addr != 0xffffffff)
break;
addr += 32;
if (addr >= ((uint32_t)&_binary_random_bits_start) + 1024)
addr = ((uint32_t)&_binary_random_bits_start);
if (addr >= ((uint32_t)&random_bits_start) + 1024)
addr = ((uint32_t)&random_bits_start);
if (addr == addr0)
fatal (FATAL_RANDOM);

View File

@@ -386,7 +386,7 @@ icc_power_off (void)
}
int res_APDU_size;
uint8_t *res_APDU_pointer;
const uint8_t *res_APDU_pointer;
static void
icc_send_data_block (int len, uint8_t status, uint8_t chain)