outputSharpID:testSharpID に食わせられるファイルを出力するプログラム(途中)

git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_eFuse@208 ff987cc8-cf2f-4642-8568-d52cce064691
This commit is contained in:
n2460 2013-06-05 05:13:33 +00:00
parent 31cda8f0d5
commit 619800d315
2 changed files with 625 additions and 0 deletions

View File

@ -0,0 +1,224 @@
# nm ntd_mem_allocator.o | grep " [T|B|D] "
# nm ntd_crypto_ecdsa.o | grep " [T|B|D] "
# nm ntd_crypto_rsa.o | grep " [T|B|D] "
# nm generate_id.o | grep " [T|B|D] "
# nm ../rsa_keysrcgen/rsa1_key.o | grep " [T|B|D] "
# 最終的なビルドスイッチの設定は、以下の通り。
# DEV_CYGWIN = FALSE
# DEBUG_PRINT = FALSE
# DEBUG_OUTPUT = FALSE
# ECDSA_SHA256 = TRUE
# USE_HSM = TRUE
# RESET_HSM = TRUE
DEV_CYGWIN = FALSE
DEBUG_PRINT = TRUE
DEBUG_OUTPUT = FALSE
ECDSA_SHA256 = TRUE
USE_HSM = TRUE
RESET_HSM = TRUE
ifeq ($(USE_HSM),TRUE)
# HSM使用時は強制的にDUMMY_KEYは未使用にする。
USE_DUMMY_KEY = FALSE
# nFast Path
NFAST_PATH = /opt/nfast
# nFast Developer tools installation
NFAST_DEV_PATH = $(NFAST_PATH)/c/ctd/gcc
NFAST_EXAMPLES = $(NFAST_PATH)/c/ctd/examples
# nFast Developer tools library
NFAST_LIBPATH = $(NFAST_DEV_PATH)/lib
# nFast Developer tools include
NFAST_INC = $(NFAST_DEV_PATH)/include
# nFast CPPFLAGS
NFAST_CPPFLAGS = \
-I$(NFAST_INC)/sworld \
-I$(NFAST_INC)/hilibs \
-I$(NFAST_INC)/nflog \
-I$(NFAST_INC)/cutils \
-I$(NFAST_EXAMPLES)/sworld \
-I$(NFAST_EXAMPLES)/hilibs \
-I$(NFAST_EXAMPLES)/nflog \
-I$(NFAST_EXAMPLES)/cutils \
# nFast LDLIBS
NFAST_LDLIBS = \
$(NFAST_LIBPATH)/libnfkm.a \
$(NFAST_LIBPATH)/libnfstub.a \
$(NFAST_LIBPATH)/libnflog.a \
$(NFAST_LIBPATH)/libcutils.a \
else # !USE_HSM
# HSMが使用できない場合は、DUMMY_KEYを使ってテストする。
USE_DUMMY_KEY = TRUE
endif # USE_HSM
ifeq ($(USE_DUMMY_KEY),TRUE)
DEV_DER_KEY_DIR = ./dummyKey/dev
PROD_DER_KEY_DIR = ./dummyKey/prod
else # !USE_DUMMY_KEY
DEV_DER_KEY_DIR = ./realKey/dev
PROD_DER_KEY_DIR = ./realKey/prod
endif # USE_DUMMY_KEY
PACKAGE_DIR = ./package
OPENSSL_DIR = ./openssl-1.0.0-beta5
# OPENSSL_DIR = ./openssl-0.9.8k
TARGET_LIB = libgenid.a
TARGET = outputSharpID
KEYS_C = cr_eFuse_iv_prod.c \
cr_eFuse_iv_dev.c \
cr_NCT2_pub_prod.c \
cr_NCT2_pub_dev.c
ifeq ($(USE_DUMMY_KEY),TRUE)
KEYS_C += cr_eFuse_privKey_prod.c cr_eFuse_pubKey_prod.c \
cr_eFuse_privKey_dev.c cr_eFuse_pubKey_dev.c \
cr_eFuse_aesKey_prod.c \
cr_eFuse_aesKey_dev.c \
cr_NCT2_priv_prod.c \
cr_NCT2_priv_dev.c
endif # USE_DUMMY_KEY
SRCS = main3.c
OBJS = $(notdir $(SRCS:.c=.o))
LIB_SRCS = cr_generate_id.c cr_id_util.c cr_keyPair.c \
cr_device_cert.c cr_enc_id.c cr_alloc.c \
cr_hsm_code.c cr_hsm_alloc.c cr_hsm_bignum.c
LIB_OBJS = $(notdir $(LIB_SRCS:.c=.o))
CFLAGS = -Wall -DMEXP=216091 -msse2 -DHAVE_SSE2
CPPFLAGS= -I. -I$(OPENSSL_DIR)/include -I$(OPENSSL_DIR)/crypto/ec
LDFLAGS = -mwindows -L$(OPENSSL_DIR)
LDLIBS = -lcrypto -lssl
MERGE_PROG = merge_lib_objs.plx
ifeq ($(DEV_CYGWIN),TRUE)
CC := C:/Cygwin/bin/gcc-3
LD = C:/Cygwin/bin/gcc-3
CFLAGS += -mno-cygwin -DDEV_CYGWIN
LDFLAGS += -Wl,--subsystem,console -mno-cygwin
TARGET_DEL = $(TARGET).exe
else # DEV_CYGWIN
CC := /usr/bin/gcc
LD = /usr/bin/gcc
LDFLAGS += -Wl
LDLIBS += -ldl -lnsl
TARGET_DEL = $(TARGET)
endif # DEV_CYGWIN
ifeq ($(USE_DUMMY_KEY),TRUE)
CFLAGS += -DUSE_DUMMY_KEY
endif
ifeq ($(DEBUG_PRINT),TRUE)
CFLAGS += -DDEBUG_PRINT
endif
ifeq ($(DEBUG_OUTPUT),TRUE)
CFLAGS += -DDEBUG_OUTPUT_FILE
endif
ifeq ($(ECDSA_SHA256),TRUE)
CFLAGS += -DECDSA_SHA256
endif
ifeq ($(USE_HSM),TRUE)
CFLAGS += -DUSE_HSM
CPPFLAGS+= $(NFAST_CPPFLAGS)
LDLIBS += $(NFAST_LDLIBS)
MERGE_PROG = merge_lib_objs_hsm.plx
endif
ifeq ($(RESET_HSM),TRUE)
CFLAGS += -DRESET_HSM
endif
.SUFFIXES:
all: package_build $(KEYS_C) $(TARGET_LIB) $(TARGET)
# install: $(TARGET)
# install -c -m 777 $(TARGET) ../bin
ifeq ($(DEV_CYGWIN),TRUE)
package_build :
cd $(PACKAGE_DIR);make DEV_CYGWIN=TRUE
else
package_build :
cd $(PACKAGE_DIR);make
endif
$(TARGET): $(OBJS) $(TARGET_LIB)
$(LD) $(LDFLAGS) $(OBJS) -o $@ $(TARGET_LIB)
$(TARGET_LIB): $(LIB_OBJS)
ar rcs $@ $(LIB_OBJS)
perl tools/$(MERGE_PROG)
%.o:%.c
$(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $@
#%.c:$(DER_KEY_DIR)/%.der
# perl tools/bin2c.plx $<
cr_eFuse_privKey_prod.c : $(PROD_DER_KEY_DIR)/eFuse_privKey.der
perl tools/bin2c.plx $< prod
cr_eFuse_pubKey_prod.c : $(PROD_DER_KEY_DIR)/eFuse_pubKey.der
perl tools/bin2c.plx $< prod
cr_eFuse_aesKey_prod.c : $(PROD_DER_KEY_DIR)/eFuse_aesKey.bin
perl tools/bin2c.plx $< prod
cr_eFuse_iv_prod.c : $(PROD_DER_KEY_DIR)/eFuse_iv.bin
perl tools/bin2c.plx $< prod
cr_NCT2_priv_prod.c : $(PROD_DER_KEY_DIR)/NCT2_priv.der
perl tools/bin2c.plx $< prod
cr_NCT2_pub_prod.c : $(PROD_DER_KEY_DIR)/NCT2_pub.der
perl tools/bin2c.plx $< prod
cr_eFuse_privKey_dev.c : $(DEV_DER_KEY_DIR)/eFuse_privKey.der
perl tools/bin2c.plx $< dev
cr_eFuse_pubKey_dev.c : $(DEV_DER_KEY_DIR)/eFuse_pubKey.der
perl tools/bin2c.plx $< dev
cr_eFuse_aesKey_dev.c : $(DEV_DER_KEY_DIR)/eFuse_aesKey.bin
perl tools/bin2c.plx $< dev
cr_eFuse_iv_dev.c : $(DEV_DER_KEY_DIR)/eFuse_iv.bin
perl tools/bin2c.plx $< dev
cr_NCT2_priv_dev.c : $(DEV_DER_KEY_DIR)/NCT2_priv.der
perl tools/bin2c.plx $< dev
cr_NCT2_pub_dev.c : $(DEV_DER_KEY_DIR)/NCT2_pub.der
perl tools/bin2c.plx $< dev
.PHONY: clean clobber
clean:
$(RM) $(LIB_OBJS) $(OBJS) $(TARGET_DEL) $(TARGET_LIB) $(KEYS_C) $(KEYS_H)
clobber:
$(RM) $(LIB_OBJS) $(OBJS) $(TARGET_DEL) $(TARGET_LIB) $(KEYS_C) $(KEYS_H)
cd $(PACKAGE_DIR);make clobber

401
trunk/main3.c Normal file
View File

@ -0,0 +1,401 @@
#define RAND_MAX 0xffffffff
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#ifdef DEV_CYGWIN
#include <conio.h>
#else // Cygwin
#include <termios.h>
#include <unistd.h>
#endif // Linux
#include "cr_generate_id.h"
#define BONDING_OPTION_PROD 0 // 製品用ID
#define BONDING_OPTION_DEV 1 // 開発用ID
// extern const int isDummyPrivateKey;
/*
gen_id.exe 0x01 0x02
gen_id.exe 0x01 0x02 ctrid090716.dat
gen_id.exe 0x01 0x03 ctrid090728.dat
*/
#ifndef DEV_CYGWIN
static struct termios initial_setting, new_setting;
static int peek_character = -1;
void keyboard_initialize( void )
{
tcgetattr( 0, &initial_setting );
new_setting = initial_setting;
new_setting.c_lflag &= ~ICANON;
new_setting.c_lflag &= ~ECHO;
new_setting.c_lflag &= ~ISIG;
new_setting.c_cc[VMIN] = 0;
new_setting.c_cc[VTIME] = 0;
tcsetattr( 0, TCSANOW, &initial_setting );
} // keyboard_initialize
void keyboard_finalize( void )
{
tcsetattr( 0, TCSANOW, &initial_setting );
} // keyboard_finalize
int kbhit( void )
{
char ch;
int nread;
if ( peek_character != -1 )
return 1;
new_setting.c_cc[VMIN] = 0;
tcsetattr( 0, TCSANOW, &new_setting );
nread = read( 0, &ch, 1 );
new_setting.c_cc[VMIN] = 1;
tcsetattr( 0, TCSANOW, &new_setting );
if ( nread == 1 )
{
peek_character = ch;
return 1;
}
return 0;
} // kbhit
int getch( void )
{
char ch;
if ( peek_character != -1 )
{
ch = peek_character;
peek_character = -1;
return ch;
}
read( 0, &ch, 1 );
return ch;
} // readch
#endif // DEV_CYGWIN
// char *str = "0x11111111";
static int str_to_u32(u32 *num, const char *str)
{
u32 c;
int shift = 0;
char *s;
int hex_mode = 0;
*num = 0;
if( *str == '0' && *(str+1) == 'x' ) {
hex_mode = 1;
s = (char *)(str + 2);
}
else {
s = (char *)str;
}
while( *s != '\0' ) {
if( shift > 8 ) {
return -1; /* error */
}
if( hex_mode ) {
if( '0' <= *s && *s <= '9' ) {
c = (u32)(*s - '0');
}
else if( 'a' <= *s && *s <= 'f' ) {
c = (u32)(*s - 'a') + 10;
}
else if( 'A' <= *s && *s <= 'F' ) {
c = (u32)(*s - 'A') + 10;
}
else {
return -1; /* error */
}
*num <<= 4;
*num |= c;
}
else {
if( '0' <= *s && *s <= '9' ) {
c = (u32)(*s - '0');
}
else {
return -1; /* error */
}
*num *= 10;
*num += c;
}
shift++;
s++;
}
return 0;
}
static double gettimeofday_sec(void)
{
struct timeval tv;
#if 0
struct timeval {
time_t tv_sec; /* 秒 */
suseconds_t tv_usec; /* マイクロ秒 */
};
struct timezone {
int tz_minuteswest; /* グリニッジ標準時との差 (西方に分単位) */
int tz_dsttime; /* 夏時間調整の型 */
};
int gettimeofday(struct timeval *tv, struct timezone *tz);
#endif
gettimeofday(&tv, NULL);
return tv.tv_sec + (double)tv.tv_usec*1e-6;
}
int main(int ac, char *argv[])
{
u8 bonding_option = BONDING_OPTION_PROD;
u32 device_id[CR_NUM_OF_DEVICEID];
u8 id[CR_ID_BUF_SIZE]; /* 256byte(2048bit) */
int ret_code;
int c;
FILE *fp;
char path[512];
double time_start,time_end;
long double time_total = 0;
int time_count = 0;
int myseed;
time_t tloc;
u32 counter0, counter0_bak;
u64 counter1, counter1_bak;
u64 counter2, counter2_bak;
u32 i;
#ifndef DEV_CYGWIN
keyboard_initialize();
#endif
// コマンドライン引数チェック
if ( ac == 1 )
{
printf( "Usage : %s BO [FILE]\n", argv[0] );
return 0;
}
if ( ac >= 2 )
{
u32 temp;
str_to_u32( &temp, argv[1] );
switch (temp)
{
case BONDING_OPTION_DEV :
bonding_option = BONDING_OPTION_DEV;
break;
case BONDING_OPTION_PROD :
bonding_option = BONDING_OPTION_PROD;
break;
default :
printf( "Invalid BondingOption %s\n", argv[1] );
return 0;
}
}
if ( ac == 3 )
{
sprintf( path, "%s", argv[2] );
}
else
{
sprintf( path, "output/outputSharpID.txt" );
}
// ファイルを開く
printf("path=%s\n", path);
fp = fopen( path, "w" );
if( fp == NULL )
{
fprintf( stderr, "failed to fopen %s\n", argv[2] );
return 0;
}
// ヘッダを書き込む
fprintf( fp, "SerialNo, Crypto Key1, Crypto Key2, ID\n" );
fprintf( fp, "--------------------------------------\n" );
#ifdef USE_DUMMY_KEY
printf( "[TEST MODE] Use dummy key.\n");
#endif
time(&tloc);
myseed = tloc;
srand(myseed);
// ID生成前にカウンタ加算をするなら、初期値は 0 で OK
counter0 = 0x00000000;
counter1 = 0x0000000000000000ll;
counter2 = 0x0000000000000000ll;
// cr_generate_id を使用する前に呼び出す
ret_code = cr_generate_id_initialize( id );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error : cr_generate_id_initialize\n" );
goto end;
}
for( i = 1 ; i < 0xffffffff; i++ )
{
u64 unit;
counter0_bak = counter0;
counter1_bak = counter1;
counter2_bak = counter2;
// counter0 は、1 ずつ加算
counter0 = i;
if( counter0 == 0 )
{
counter0 = 1;
}
// counter1 は、"14 の乱数値" を加算
unit = (u64)( ( rand() & 0x03 ) + 1 );
counter1 += unit;
// counter2 は、"0 以外の 32bit 乱数値" を加算
do {
unit = ((u64)rand() & 0xffff) | ( ((u64)rand() & 0xffff) << 16 );
} while( unit == 0 );
counter2 += unit;
// カウンタオーバーフローチェック
if( counter0 < counter0_bak )
{
fprintf(stderr,"counter0 overflow : %08x\n", (unsigned int)counter0 );
}
if( counter1 < counter1_bak )
{
fprintf(stderr,"counter1 overflow : %08x%08x\n", (unsigned int)( counter1 >> 32 ), (unsigned int)counter2 );
}
if( counter2 < counter2_bak )
{
fprintf(stderr,"counter2 overflow : %08x%08x\n", (unsigned int)( counter2 >> 32 ), (unsigned int)counter2 );
}
device_id[0] = counter0;
device_id[1] = (u32)(counter1 & 0xffffffff);
device_id[2] = (u32)((counter1 >> 32) & 0xffffffff);
device_id[3] = (u32)(counter2 & 0xffffffff);
device_id[4] = (u32)((counter2 >> 32) & 0xffffffff);
time_start = gettimeofday_sec();
ret_code = cr_generate_id( device_id, id, bonding_option );
if( ret_code != 0 )
{
fprintf(stderr,"generate_id failed\n");
goto end;
}
else
{
time_end = gettimeofday_sec();
time_total += (long double)(time_end - time_start);
time_count++;
/* printf("generate_id success\n"); */
}
// 書き込み
fprintf( fp, "%08X, %08X %08X, %08X %08X, ",
(unsigned int)device_id[0], (unsigned int)device_id[2], (unsigned int)device_id[1], (unsigned int)device_id[4], (unsigned int)device_id[3] );
if (kbhit())
{
c = getch();
if( 'p' == c )
{
printf("ID[0] = 0x%08x\n", (unsigned int)device_id[0]);
printf("ID[1] = 0x%08x%08x\n", (unsigned int)device_id[2], (unsigned int)device_id[1] );
printf("ID[2] = 0x%08x%08x\n", (unsigned int)device_id[4], (unsigned int)device_id[3] );
printf("time av. = %8.8f sec\n", (double)(time_total/(long double)time_count));
cr_print_flag = 1;
}
else if( c == 'q' )
{
fclose(fp);
goto end;
}
}
else
{
cr_print_flag = 0;
}
}
if( ac == 4 ) {
if( 0 == str_to_u32(&device_id[0], argv[1]) && 0 == str_to_u32(&device_id[1], argv[2]) ) {
printf("ID[0] = 0x%08x\n", (unsigned int)device_id[0]);
printf("ID[1] = 0x%08x%08x\n", (unsigned int)device_id[2], (unsigned int)device_id[1] );
printf("ID[2] = 0x%08x%08x\n", (unsigned int)device_id[4], (unsigned int)device_id[3] );
fp = fopen( argv[3], "wb" );
if( fp == NULL ) {
fprintf(stderr, "failed to fopen %s\n",argv[3]);
}
else {
time_start = gettimeofday_sec();
if( 0 != cr_generate_id( device_id, id, bonding_option ) )
{
fprintf(stderr,"cr_generate_id failed s1=0x%08x s2_lo=0x%08x s2_hi=0x%08x\n",
(int)device_id[0], (int)device_id[1], (int)device_id[2]);
}
else {
time_end = gettimeofday_sec();
fwrite(id, CR_ID_BUF_SIZE, 1, fp);
}
fclose(fp);
}
}
else {
goto err_print;
}
}
else {
err_print:
fprintf(stderr,"Invalid argument!\n");
fprintf(stderr,"Usage: %s\n", argv[0]);
fprintf(stderr,"Usage: %s device_id(32bit) filename.dat\n", argv[0]);
}
end:
// ファイルのクローズ
if (fp)
{
fclose(fp);
}
// cr_generate_id を使用した後に呼び出す
ret_code = cr_generate_id_finalize( id );
if ( ret_code != CR_GENID_SUCCESS )
{
printf( "error : cr_generate_id_finalize\n" );
return 0; // error
}
#ifndef DEV_CYGWIN
keyboard_finalize();
#endif
printf("end of main\n");
return 0;
}