rand関数の出力がcygwin(16bit)とlinux(32bit)と差異があるのをマスクで吸収する

git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_eFuse@158 ff987cc8-cf2f-4642-8568-d52cce064691
This commit is contained in:
kubodera_yuichi 2010-01-08 00:20:10 +00:00
parent 2c9d0d869f
commit f937bfed91

724
main.c
View File

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