diff --git a/cr_enc_id.c b/cr_enc_id.c index 9ae9410..6a3891b 100644 --- a/cr_enc_id.c +++ b/cr_enc_id.c @@ -132,13 +132,17 @@ extern RSA *d2i_RSA_PUBKEY(RSA **a, const unsigned char **pp, long length); #endif // USE_HSM +#ifdef ENCRYPT_AES +static int crypto_aes_enc_dec( unsigned char *dst_buf,unsigned char *org_buf ); +#else // !ENCRYPT_AES +static int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf ); +#endif // ENCRYPT_AES + + static unsigned char local_buf_1[CR_ID_BUF_SIZE]; static unsigned char local_buf_2[CR_ID_BUF_SIZE]; -static int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf ); -static int crypto_rsa_enc_dec( unsigned char *dst_buf, unsigned char *org_buf ); - -// ビルドスイッチに応じて、IDバッファをAES/RSAで暗号化 +// ビルドスイチEに応じて、IDバッファをAES/RSAで暗号 int EncryptID( unsigned char *dst_buf, unsigned char *org_buf ) { #ifdef ENCRYPT_AES @@ -149,7 +153,7 @@ int EncryptID( unsigned char *dst_buf, unsigned char *org_buf ) } #ifdef ENCRYPT_AES -// AES暗号化 -> 復号化 -> ベリファイ +// AES暗号匁E-> 復号匁E-> ベリファイ #ifdef USE_HSM @@ -184,7 +188,7 @@ int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf ) } } - // バッファコピー + // バッファコピE memcpy( dst_buf, local_buf_1, CR_ID_BUF_SIZE ); return CR_GENID_SUCCESS; @@ -204,36 +208,36 @@ int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf ) memset( local_buf_1, 0, CR_ID_BUF_SIZE ); memset( local_buf_2, 0, CR_ID_BUF_SIZE ); - // AES 暗号化用鍵 作成 + // AES 暗号化用鍵 作E if ( AES_set_encrypt_key( AES_PASS_PHRASE, 128, &aesEncKey ) != 0 ) { printf( "error : AES_set_encrypt_key\n" ); return 0; } - // AES 復号化用鍵 作成 + // AES 復号化用鍵 作E if ( AES_set_decrypt_key( AES_PASS_PHRASE, 128, &aesDecKey ) != 0 ) { printf( "error : AES_set_decrypt_key\n" ); return 0; } - // iv 初期化 + // iv 初期 for ( i = 0; i < 16; i++ ) { temp_iv[i] = i; } - // AES 暗号化 + // AES 暗号 AES_cbc_encrypt ( org_buf, local_buf_1, CR_ID_BUF_SIZE, &aesEncKey, temp_iv, AES_ENCRYPT ); - // iv 初期化 + // iv 初期 for ( i = 0; i < 16; i++ ) { temp_iv[i] = i; } - // AES 復号化 + // AES 復号 AES_cbc_encrypt ( local_buf_1, local_buf_2, CR_ID_BUF_SIZE, &aesDecKey, temp_iv, AES_DECRYPT ); // ベリファイ @@ -246,7 +250,7 @@ int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf ) } } - // バッファコピー + // バッファコピE memcpy( dst_buf, local_buf_1, CR_ID_BUF_SIZE ); return CR_GENID_SUCCESS; @@ -256,7 +260,7 @@ int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf ) #else // !ENCRYPT_AES -// RSA暗号化->復号化->ベリファイ +// RSA暗号匁E>復号匁E>ベリファイ #ifdef USE_HSM int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf ) @@ -289,7 +293,7 @@ int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf ) } } - // バッファコピー + // バッファコピE memcpy( dst_buf, local_buf_1, CR_ID_BUF_SIZE ); return CR_GENID_SUCCESS; @@ -308,19 +312,19 @@ int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf) memset(local_buf_2, 0,CR_ID_BUF_SIZE); - // DERフォーマットのRSA鍵を読み込み + // DERフォーマットERSA鍵を読み込み { - const unsigned char *der_priv = cr_gen_id_rsa_key_priv_DER + 0x10; // ヘッダ部分を除外してKEY実体を指定 - const unsigned char *der_pub = cr_gen_id_rsa_key_pub_DER + 0x10; // 同上 - int priv_len = cr_gen_id_rsa_key_priv_DER[ 8 ] | cr_gen_id_rsa_key_priv_DER[ 9 ] << 8; // KEY長を取り出し - int pub_len = cr_gen_id_rsa_key_pub_DER [ 8 ] | cr_gen_id_rsa_key_pub_DER [ 9 ] << 8; // 同上 - // コマンドラインのopensslが出力する秘密鍵は、PKCS#1 RSAPublicKeyフォーマットなので、この関数を使う。 + const unsigned char *der_priv = cr_gen_id_rsa_key_priv_DER + 0x10; // ヘッダ部刁E除外してKEY実体を持E + const unsigned char *der_pub = cr_gen_id_rsa_key_pub_DER + 0x10; // 同 + int priv_len = cr_gen_id_rsa_key_priv_DER[ 8 ] | cr_gen_id_rsa_key_priv_DER[ 9 ] << 8; // KEY長を取りE + int pub_len = cr_gen_id_rsa_key_pub_DER [ 8 ] | cr_gen_id_rsa_key_pub_DER [ 9 ] << 8; // 同 + // コマンドラインのopensslがE力する秘寁Eは、PKCS#1 RSAPublicKeyフォーマットなので、この関数を使ぁE rsa_privkey = d2i_RSAPrivateKey( NULL, &der_priv, priv_len ); if( rsa_privkey == NULL ) { ret_code = CR_GENID_ERROR_RSA_READ_PRIVATE_KEY; goto end; } - // コマンドラインのopensslが出力する公開鍵は、SubjectPublicKeyInfo形式なので、この関数を使う。 + // コマンドラインのopensslがE力するE開鍵は、SubjectPublicKeyInfo形式なので、この関数を使ぁE rsa_pubkey = d2i_RSA_PUBKEY( NULL, &der_pub, pub_len ); if( rsa_pubkey == NULL ) { ret_code = CR_GENID_ERROR_RSA_READ_PUBLIC_KEY; diff --git a/cr_generate_id.c b/cr_generate_id.c index ff9d186..037d4ae 100644 --- a/cr_generate_id.c +++ b/cr_generate_id.c @@ -249,7 +249,7 @@ int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8 memset(id_buf, 0, CR_ID_BUF_SIZE); cr_id_buf = (CR_ID_BUFFER *)id_buf; - cr_id_buf->magic_number = 0xdeadb00f; /* 0xdeadbeefにするとRSAでコケる。 */ + cr_id_buf->magic_number = CR_GEN_ID_MAGICCODE; cr_id_buf->version = CR_GEN_ID_VERSION; //-------------------------------------------------------------- @@ -268,11 +268,9 @@ int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8 #if 1 if( cr_print_flag ) { printf("serialNo:\n"); - for( i = 0 ; i < CR_NUM_OF_SERIAL ; i++ ) { - if( i == 0 ) printf(" 0x%08x\n", (unsigned int)serial[i] ); - else if( i & 0x01 ) printf(" 0x%08x", (unsigned int)serial[i] ); - else printf("%08x\n", (unsigned int)serial[i] ); - } + printf(" 0x%08x\n", (unsigned int)serial[0] ); + printf(" 0x%08x%08x\n", (unsigned int)serial[2], (unsigned int)serial[1] ); + printf(" 0x%08x%08x\n", (unsigned int)serial[4], (unsigned int)serial[3] ); printf("\n"); } #endif /* DEBUG_PRINT */ @@ -286,7 +284,7 @@ int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8 &cr_id_buf->hour, &cr_id_buf->min, &cr_id_buf->sec, - &cr_id_buf->deviceCertExpiryDate ); + &cr_id_buf->expiryDate ); if ( ret_code != 0 ) { goto end; } diff --git a/cr_generate_id_private.h b/cr_generate_id_private.h index e499343..161f458 100644 --- a/cr_generate_id_private.h +++ b/cr_generate_id_private.h @@ -140,10 +140,15 @@ extern "C" { #define CR_GEN_ID_VERSION 1 +#ifdef USE_HSM +#define CR_GEN_ID_MAGICCODE 0xdeadb00f; /* ŏIIɂ͂œB0xdeadbeefɂRSAŃRPB */ +#else // !USE_HSM +#define CR_GEN_ID_MAGICCODE 0xabadf00d; +#endif // USE_HSM #define CR_RANDOM_LENGTH 0x50 #define EC_PRIVATE_KEY_LENGTH 0x20 -#define ECDSA_SIGN_LENGTH 0x44 -#define CR_RSV_LENGTH 0x08 +#define ECDSA_SIGN_LENGTH 0x3C +#define CR_RSV_LENGTH 0x10 #define EC_CURVE_NAME NID_sect233r1 typedef struct { @@ -164,12 +169,12 @@ typedef struct { u8 hour; /* 0x1D */ u8 min; /* 0x1E */ u8 sec; /* 0x1F */ + u32 expiryDate; /* 0x20 - 0x23 foCXؖ seconds from the Epoch (Jan 1, 1970 00:00) as a 32 bit */ u8 devicePrivKey[ EC_PRIVATE_KEY_LENGTH ]; - /* 0x20 - 0x3F ECC233 private key (big endian) j[Nۏ؂Ȃ */ + /* 0x24 - 0x43 ECC233 private key (big endian) j[Nۏ؂Ȃ */ u8 deviceCertSign[ ECDSA_SIGN_LENGTH ]; - /* 0x40 - 0x83 ECC233 ECDSA signature (big endian) */ - u32 deviceCertExpiryDate; /* 0x84 - 0x87 \ */ - u8 reserved[ CR_RSV_LENGTH ]; /* 0x88 - 0x8F \ */ + /* 0x44 - 0x7F ECC233 ECDSA signature (big endian) */ + u8 reserved[ CR_RSV_LENGTH ]; /* 0x80 - 0x8F \ */ u8 random[ CR_RANDOM_LENGTH ]; /* 0x90 - 0xDF */ u8 hash[ SHA256_DIGEST_LENGTH ]; /* 0xE0 - 0xFF "0x00-0xDF"̈SHA256nbV */ } CR_ID_BUFFER; /* v256bytes = 2048bit */ diff --git a/cr_hsm_util.c b/cr_hsm_util.c index 007f199..717507b 100644 --- a/cr_hsm_util.c +++ b/cr_hsm_util.c @@ -124,7 +124,7 @@ #include "cr_generate_id.h" #include "cr_generate_id_private.h" -// タイムスタンプの取得 +// タイムスタンプぅ取 int GetTimestamp( u8 *pYear, u8 *pMonth, u8 *pMday, u8 *pHour, u8 *pMin, u8 *pSec, time_t *pTime) { int ret_code = 0; @@ -137,8 +137,7 @@ int GetTimestamp( u8 *pYear, u8 *pMonth, u8 *pMday, u8 *pHour, u8 *pMin, u8 *pSe return ret_code; } #else // !USE_HSM - struct timezone tz; - gettimeofday(&tv,&tz); + gettimeofday( &tv, NULL ); #endif // USE_HSM tm_time = gmtime( &tv.tv_sec ); @@ -149,16 +148,35 @@ int GetTimestamp( u8 *pYear, u8 *pMonth, u8 *pMday, u8 *pHour, u8 *pMin, u8 *pSe *pHour = (u8)tm_time->tm_hour; *pMin = (u8)tm_time->tm_min; *pSec = (u8)tm_time->tm_sec; + *pTime = tv.tv_sec; + +#if 0 + { + struct tm tm2; + time_t t2; + memset( &tm2, 0, sizeof(tm2) ); + tm2.tm_year = *pYear; + tm2.tm_mon = *pMonth - 1; + tm2.tm_mday = *pMday; + tm2.tm_hour = *pHour; + tm2.tm_min = *pMin; + tm2.tm_sec = *pSec; + tm2.tm_isdst = 0; // 夏時 + t2 = gmt_mktime( &tm2 ); + printf( "time_t = %08x\n", (int)t ); + printf( "mktime = %08x\n", (int)t2 ); + } +#endif #ifdef DEBUG_PRINT if( cr_print_flag ) { printf("GMT:%d-%02d-%02d %02d:%02d:%02d\n", *pYear+1900, /* 年 */ - *pMonth, /* 月 */ + *pMonth, /* 朁E*/ *pMday, /* 日 */ - *pHour, /* 時 */ - *pMin, /* 分 */ - *pSec /* 秒 */ + *pHour, /* 晁E*/ + *pMin, /* 刁E*/ + *pSec /* 私E*/ ); } #endif /* DEBUG_PRINT */ @@ -166,7 +184,27 @@ int GetTimestamp( u8 *pYear, u8 *pMonth, u8 *pMday, u8 *pHour, u8 *pMin, u8 *pSe return ret_code; } -// ランダム値の生成 +#if 0 +static time_t gmt_mktime( struct tm *tm_time ) +{ + time_t ret; + char *tz; + + tz = getenv("TZ"); + setenv("TZ", "", 1); // setenv, unsetenvがcygwinでは使えなぁE + tzset(); + ret = mktime(tm_time); // mktimeはローカル時間での変換になるぅで、timezoneをケアしなぁEダメ + if (tz) + setenv("TZ", tz, 1); + else + unsetenv("TZ"); + tzset(); + return ret; +} +#endif + + +// ランダム値の生恅 int GenerateRandom( u8 *pDst, int length ) { int ret_code = 0;