デバッグプリントの整理。

generate_id内ではSFMTを使用しないよう変更。(通常のrandを使用)

git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_eFuse@10 ff987cc8-cf2f-4642-8568-d52cce064691
This commit is contained in:
(no author) 2009-12-22 01:34:46 +00:00
parent 27a69561bb
commit 19d5af715d
5 changed files with 203 additions and 308 deletions

View File

@ -325,14 +325,6 @@ int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf )
for ( i = 0; i < sizeof( enc_iv.iv.generic256.iv.bytes ); i++ ) for ( i = 0; i < sizeof( enc_iv.iv.generic256.iv.bytes ); i++ )
enc_iv.iv.generic256.iv.bytes[i] = dec_iv.iv.generic256.iv.bytes[i] = i; enc_iv.iv.generic256.iv.bytes[i] = dec_iv.iv.generic256.iv.bytes[i] = i;
for ( i = 0; i < 256; i++ )
{
if ( i % 16 == 0 )
printf( "\n" );
printf( "0x%02X ", org_buf[i] );
}
printf( "\n" );
// encrypt // encrypt
cmd.cmd = Cmd_Encrypt; cmd.cmd = Cmd_Encrypt;
cmd.args.encrypt.flags = Cmd_Encrypt_Args_flags_given_iv_present; cmd.args.encrypt.flags = Cmd_Encrypt_Args_flags_given_iv_present;

View File

@ -144,15 +144,12 @@
#include "cr_generate_id_private.h" #include "cr_generate_id_private.h"
#include "cr_alloc.h" #include "cr_alloc.h"
// #define DEBUG_PRINT 1
#define CR_GEN_ID_VERSION 1 #define CR_GEN_ID_VERSION 1
#define CR_NUM_OF_SERIAL 5 #define CR_NUM_OF_SERIAL 5
#define CR_RSV_LENGTH 0x0C #define CR_RSV_LENGTH 0x0C
#define CR_RANDOM_LENGTH 0x50 #define CR_RANDOM_LENGTH 0x50
#define EC_PRIVATE_KEY_LENGTH 0x20 #define EC_PRIVATE_KEY_LENGTH 0x20
#define ECDSA_SIGN_LENGTH 0x44 #define ECDSA_SIGN_LENGTH 0x44
//#define SHA256_DIGEST_LENGTH 0x20 // OpenSSL側で定義済み
#define EC_CURVE_NAME NID_sect233r1 #define EC_CURVE_NAME NID_sect233r1
#define CA_FILE "dummyCA/NintendoCTR2_dummy.crt" #define CA_FILE "dummyCA/NintendoCTR2_dummy.crt"
@ -295,7 +292,7 @@ static int generate_EC_private_key( EC_KEY *eckey, u8 *privKey )
int i; int i;
for ( i = 0; i < EC_PRIVATE_KEY_LENGTH; i++ ) for ( i = 0; i < EC_PRIVATE_KEY_LENGTH; i++ )
{ {
privKey[i] = (u8)gen_rand32(); privKey[i] = (u8)rand();
} }
} }
#endif // USE_HSM #endif // USE_HSM
@ -318,19 +315,7 @@ static int generate_EC_private_key( EC_KEY *eckey, u8 *privKey )
(void)BN_bin2bn( privKey, EC_PRIVATE_KEY_LENGTH, bn_privkey ); (void)BN_bin2bn( privKey, EC_PRIVATE_KEY_LENGTH, bn_privkey );
eckey->priv_key = bn_privkey; eckey->priv_key = bn_privkey;
#ifdef DEBUG_PRINT DEBUG_PRINT_ARRAY( "ec private key:", (const char *)privKey, EC_PRIVATE_KEY_LENGTH );
if( cr_print_flag ) {
int i;
printf("ec private key:");
for( i = 0 ; i < EC_PRIVATE_KEY_LENGTH ; i++ ) {
if( (i%8) == 0 ) {
printf("\n ");
}
printf("0x%02x ", privKey[i] );
}
printf("\n");
}
#endif /* DEBUG_PRINT */
return ret_code; return ret_code;
} // generate_EC_private_key } // generate_EC_private_key
@ -594,17 +579,7 @@ static int generate_X509_cert( EVP_PKEY *evp_pkey, X509_REQ *req, X509 *cert, u3
#endif #endif
#if 0 #if 0
if ( cr_print_flag ) DEBUG_PRINT_ARRAY( "eccSignature:", (const char *)cert->signature->data, cert->signature->length );
{
int i;
for ( i = 0; i < cert->signature->length; i++ )
{
if ( i % 8 == 0 )
printf( "\n" );
printf( "0x%02X ", cert->signature->data[i] );
}
printf( "\n" );
}
#endif #endif
#if 0 #if 0
@ -670,48 +645,18 @@ static int generate_CTRCustom_cert( CTR_Device_Cert *cert, u32 deviceId, u8 bond
int i; int i;
printf( "CTR custom cert\n" ); printf( "CTR custom cert\n" );
printf( "sigType : 0x%08X\n", *(unsigned int*)cert->sigType ); printf( "sigType : 0x%08X\n", *(unsigned int*)cert->sigType );
printf( "eccSignature :" ); DEBUG_PRINT_ARRAY( "eccSignature:", (const char *)cert->eccSignature, sizeof(cert->eccSignature) );
for ( i = 0; i < 60; i++ ) DEBUG_PRINT_ARRAY( "padding0:", (const char *)cert->padding0, sizeof(cert->padding0) );
{ printf( "issuerName : " );
if ( i % 16 == 0 ) for ( i = 0; i < sizeof(cert->issuerName); i++ ) printf( "%c", cert->issuerName[i] );
printf( "\n" );
printf( "0x%02X ", cert->eccSignature[i] );
}
printf( "\n" );
printf( "padding :" );
for ( i = 0; i < 60; i++ )
{
if ( i % 16 == 0 )
printf( "\n" );
printf( "0x%02X ", cert->padding0[i] );
}
printf( "\n" );
printf( "subject : " );
for ( i = 0; i < 64; i++ )
printf( "%c", cert->issuerName[i] );
printf( "\n" ); printf( "\n" );
printf( "keyType : 0x%08X\n", *(unsigned int*)cert->keyType ); printf( "keyType : 0x%08X\n", *(unsigned int*)cert->keyType );
printf( "subject : " ); printf( "subject : " );
for ( i = 0; i < 64; i++ ) for ( i = 0; i < sizeof(cert->subject); i++ ) printf( "%c", cert->subject[i] );
printf( "%c", cert->subject[i] );
printf( "\n" ); printf( "\n" );
printf( "expiryDate : 0x%08X\n", (unsigned int)cert->expiryDate ); printf( "expiryDate : 0x%08X\n", (unsigned int)cert->expiryDate );
printf( "eccPubKey :" ); DEBUG_PRINT_ARRAY( "eccPubKey :", (const char *)cert->eccPubKey, sizeof(cert->eccPubKey) );
for ( i = 0; i < 60; i++ ) DEBUG_PRINT_ARRAY( "padding1:", (const char *)cert->padding1, sizeof(cert->padding1) );
{
if ( i % 16 == 0 )
printf( "\n" );
printf( "0x%02X ", cert->eccPubKey[i] );
}
printf( "\n" );
printf( "padding :" );
for ( i = 0; i < 60; i++ )
{
if ( i % 16 == 0 )
printf( "\n" );
printf( "0x%02X ", cert->padding1[i] );
}
printf( "\n" );
// 証明書の書き込みテスト // 証明書の書き込みテスト
FILE *fp; FILE *fp;
@ -1199,10 +1144,6 @@ int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8
X509_REQ *req = NULL; X509_REQ *req = NULL;
X509 *cert = NULL; X509 *cert = NULL;
struct tm *tm_time;
struct timeval tv;
struct timezone tz;
CR_ID_BUFFER *cr_id_buf; CR_ID_BUFFER *cr_id_buf;
u8 hash[SHA256_DIGEST_LENGTH]; /* SHA256 check */ u8 hash[SHA256_DIGEST_LENGTH]; /* SHA256 check */
CTR_Device_Cert ctr_dev_cert; CTR_Device_Cert ctr_dev_cert;
@ -1224,11 +1165,14 @@ int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8
} }
ret_code = CR_GENID_ERROR_NON; /* CR_GENID_ERROR_NON = 0 */ ret_code = CR_GENID_ERROR_NON; /* CR_GENID_ERROR_NON = 0 */
// printf("sizeof(CR_ID_BUFFER) = %d bytes\n",sizeof(CR_ID_BUFFER) );
// printf("offset(factory, CR_ID_BUFFER) = 0x%02x bytes\n", offsetof(CR_ID_BUFFER,factory ) ); #if 0
// printf("sizeof(CR_ERR_BUFFER) = 0x%02x bytes\n",sizeof(CR_ERR_BUFFER) ); printf("sizeof(CR_ID_BUFFER) = %d bytes\n",sizeof(CR_ID_BUFFER) );
// printf("offset(err_buffer, CR_ERR_BUFFER) = 0x%02x bytes\n", offsetof(CR_ERR_BUFFER,err_buffer ) ); printf("offset(factory, CR_ID_BUFFER) = 0x%02x bytes\n", offsetof(CR_ID_BUFFER,factory ) );
// printf("offset(err_line, CR_ERR_BUFFER) = 0x%02x bytes\n", offsetof(CR_ERR_BUFFER,err_line ) ); printf("sizeof(CR_ERR_BUFFER) = 0x%02x bytes\n",sizeof(CR_ERR_BUFFER) );
printf("offset(err_buffer, CR_ERR_BUFFER) = 0x%02x bytes\n", offsetof(CR_ERR_BUFFER,err_buffer ) );
printf("offset(err_line, CR_ERR_BUFFER) = 0x%02x bytes\n", offsetof(CR_ERR_BUFFER,err_line ) );
#endif
// ダイジェストアルゴリズムを追加する // ダイジェストアルゴリズムを追加する
#if 0 #if 0
@ -1287,6 +1231,11 @@ int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8
//-------------------------------------------------------------- //--------------------------------------------------------------
// タイムスタンプセット // タイムスタンプセット
//-------------------------------------------------------------- //--------------------------------------------------------------
{
struct tm *tm_time;
struct timeval tv;
struct timezone tz;
#ifdef USE_HSM #ifdef USE_HSM
hsm_get_rtc( &tv.tv_sec ); hsm_get_rtc( &tv.tv_sec );
#else // !USE_HSM #else // !USE_HSM
@ -1295,9 +1244,15 @@ int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8
tm_time = gmtime( &tv.tv_sec ); tm_time = gmtime( &tv.tv_sec );
cr_id_buf->year = (u8)tm_time->tm_year;
cr_id_buf->month = (u8)tm_time->tm_mon+1;
cr_id_buf->mday = (u8)tm_time->tm_mday;
cr_id_buf->hour = (u8)tm_time->tm_hour;
cr_id_buf->min = (u8)tm_time->tm_min;
cr_id_buf->sec = (u8)tm_time->tm_sec;
#ifdef DEBUG_PRINT #ifdef DEBUG_PRINT
if( cr_print_flag ) if( cr_print_flag ) {
{
printf("GMT:%d-%02d-%02d %02d:%02d:%02d\n", printf("GMT:%d-%02d-%02d %02d:%02d:%02d\n",
tm_time->tm_year+1900, /* 年 */ tm_time->tm_year+1900, /* 年 */
tm_time->tm_mon+1, /* 月 */ tm_time->tm_mon+1, /* 月 */
@ -1308,14 +1263,7 @@ int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8
); );
} }
#endif /* DEBUG_PRINT */ #endif /* DEBUG_PRINT */
}
cr_id_buf->year = (u8)tm_time->tm_year;
cr_id_buf->month = (u8)tm_time->tm_mon+1;
cr_id_buf->mday = (u8)tm_time->tm_mday;
cr_id_buf->hour = (u8)tm_time->tm_hour;
cr_id_buf->min = (u8)tm_time->tm_min;
cr_id_buf->sec = (u8)tm_time->tm_sec;
//-------------------------------------------------------------- //--------------------------------------------------------------
// 乱数を生成してセット // 乱数を生成してセット
//-------------------------------------------------------------- //--------------------------------------------------------------
@ -1327,31 +1275,16 @@ int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8
return ret_code; return ret_code;
} }
#else // !USE_HSM #else // !USE_HSM
#ifdef USE_SFMT // メルセンヌ・ツイスタ
for ( i = 0; i < CR_RANDOM_LENGTH; i += 4 )
{ {
*(u32*)&(cr_id_buf->random[i]) = gen_rand32(); u16 buffer[ CR_RANDOM_LENGTH / sizeof(u16) ];
for( i = 0 ; i < CR_RANDOM_LENGTH / sizeof(u16); i++ ) {
buffer[i] = (u16)rand();
} }
#else /* USE_SFMT */ memcpy( cr_id_buf->random, buffer, CR_RANDOM_LENGTH );
for( i = 0 ; i < CR_NUM_OF_RANDOM ; i++ ) {
cr_id_buf->random[i] = (u16)rand(); /* 範囲は0から32767 */
} }
#endif /* USE_SFMT */
#endif // USE_HSM #endif // USE_HSM
#ifdef DEBUG_PRINT DEBUG_PRINT_ARRAY( "rand:", (const char *)cr_id_buf->random, CR_RANDOM_LENGTH );
if( cr_print_flag ) {
unsigned char *p = (unsigned char *)cr_id_buf->random;
printf("rand:" );
for( i = 0 ; i < CR_RANDOM_LENGTH; i++ ) {
if( (i % 16) == 0 ) {
printf("\n ");
}
printf("0x%02x ", p[ i ] );
}
printf("\n");
}
#endif /* DEBUG_PRINT */
//-------------------------------------------------------------- //--------------------------------------------------------------
// 楕円曲線鍵ペアを生成 // 楕円曲線鍵ペアを生成
@ -1452,7 +1385,6 @@ int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8
for ( i = 0; i < cert->signature->length; i++ ) for ( i = 0; i < cert->signature->length; i++ )
cr_id_buf->deviceCertSign[ i + padding ] = cert->signature->data[i]; cr_id_buf->deviceCertSign[ i + padding ] = cert->signature->data[i];
// mistake!! // mistake!!
#endif
// TORIAEZU : create Custom cert // TORIAEZU : create Custom cert
memset( &ctr_dev_cert, 0, sizeof( ctr_dev_cert ) ); memset( &ctr_dev_cert, 0, sizeof( ctr_dev_cert ) );
@ -1467,6 +1399,7 @@ int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8
len = BN_bn2bin( &my_eckey->pub_key->Y, buf ); len = BN_bn2bin( &my_eckey->pub_key->Y, buf );
memcpy( &ctr_dev_cert.eccPubKey[30], buf, len ); memcpy( &ctr_dev_cert.eccPubKey[30], buf, len );
free( buf ); free( buf );
#endif
// copy sign to Custom cert // copy sign to Custom cert
//#define SHOW_SIG //#define SHOW_SIG
@ -1481,8 +1414,7 @@ int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8
printf( "r :" ); printf( "r :" );
for ( i = 0; i < len; i++ ) for ( i = 0; i < len; i++ )
{ {
if ( i % 8 == 0 ) if ( i % 8 == 0 ) printf( "\n" );
printf( "\n" );
printf( "0x%02X ", *(ptr++) ); printf( "0x%02X ", *(ptr++) );
} }
printf( "\n" ); printf( "\n" );
@ -1492,8 +1424,7 @@ int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8
printf( "s :" ); printf( "s :" );
for ( i = 0; i < len; i++ ) for ( i = 0; i < len; i++ )
{ {
if ( i % 8 == 0 ) if ( i % 8 == 0 ) printf( "\n" );
printf( "\n" );
printf( "0x%02X ", *(ptr++) ); printf( "0x%02X ", *(ptr++) );
} }
printf( "\n" ); printf( "\n" );
@ -1511,83 +1442,34 @@ int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8
ret_code = generate_CTRCustom_cert( &ctr_dev_cert, ret_code = generate_CTRCustom_cert( &ctr_dev_cert,
cr_id_buf->serial[0], cr_id_buf->bondingOption ); cr_id_buf->serial[0], cr_id_buf->bondingOption );
#endif #endif
printf("3\n");
#if 0 #if 0
if( cr_print_flag ) DEBUG_PRINT_ARRAY( "deviceCertSign:", (const char *)cr_id_buf->deviceCertSign, ECDSA_SIGN_LENGTH );
{ #endif
for( i = 0 ; i < ECDSA_SIGN_LENGTH; i++ )
{
if( (i%8) == 0 )
{
printf( "\n ");
}
printf("0x%02x ", cr_id_buf->deviceCertSign[i] );
}
printf("\n");
}
#endif /* DEBUG_PRINT */
//-------------------------------------------------------------- //--------------------------------------------------------------
// FuseIDバッファ全体のSHA256ハッシュを算出してセット // FuseIDバッファ全体のSHA256ハッシュを算出してセット
//-------------------------------------------------------------- //--------------------------------------------------------------
SHA256(id_buf, CR_ID_BUF_SIZE - SHA256_DIGEST_LENGTH, cr_id_buf->hash); SHA256(id_buf, CR_ID_BUF_SIZE - SHA256_DIGEST_LENGTH, cr_id_buf->hash);
memcpy( hash, cr_id_buf->hash, SHA256_DIGEST_LENGTH ); memcpy( hash, cr_id_buf->hash, SHA256_DIGEST_LENGTH );
DEBUG_PRINT_ARRAY( "SHA256 Digest:", (const char *)cr_id_buf->hash, SHA256_DIGEST_LENGTH );
#ifdef DEBUG_PRINT
if( cr_print_flag ) {
printf("SHA256 Digest:");
for( i = 0 ; i < SHA256_DIGEST_LENGTH ; i++ ) {
if( (i%8) == 0 ) {
printf("\n ");
}
printf("0x%02x ", cr_id_buf->hash[i] );
}
printf("\n");
}
#endif /* DEBUG_PRINT */
//-------------------------------------------------------------- //--------------------------------------------------------------
// FuseIDバッファ全体を暗号化 // FuseIDバッファ全体を暗号化
//-------------------------------------------------------------- //--------------------------------------------------------------
#ifdef DEBUG_PRINT DEBUG_PRINT_ARRAY( "ORG buf:", (const char *)id_buf, CR_ID_BUF_SIZE );
if( cr_print_flag ) {
printf("ORG buf:");
for( i = 0 ; i < CR_ID_BUF_SIZE ; i++ ) {
if( (i%16) == 0 ) {
printf("\n ");
}
printf("0x%02x ", id_buf[i] );
}
printf("\n");
}
#endif /* DEBUG_PRINT */
#ifdef ENCRYPT_AES #ifdef ENCRYPT_AES
// AES暗号化 -> 復号化 -> ベリファイ ret_code = crypto_aes_enc_dec( id_buf, id_buf ); // AES
ret_code = crypto_aes_enc_dec( id_buf, id_buf );
#else // !ENCRYPT_AES #else // !ENCRYPT_AES
ret_code = crypto_rsa_enc_dec( id_buf, id_buf); ret_code = crypto_rsa_enc_dec( id_buf, id_buf ); // RSA pubKey enc
#endif // ENCRYPT_AES #endif // ENCRYPT_AES
if( ret_code != 0 ) if( ret_code != 0 ) {
{
goto end; goto end;
} }
#ifdef DEBUG_PRINT DEBUG_PRINT_ARRAY( "encrypted:", (const char *)id_buf, CR_ID_BUF_SIZE );
if( cr_print_flag ) {
printf("encrypted:");
for( i = 0 ; i < CR_ID_BUF_SIZE ; i++ ) {
if( (i%16) == 0 ) {
printf("\n ");
}
printf("0x%02x ", id_buf[i] );
}
printf("\n");
}
#endif /* DEBUG_PRINT */
//-------------------------------------------------------------- //--------------------------------------------------------------
// 終了処理 // 終了処理
@ -1625,3 +1507,19 @@ printf("3\n");
return ret_code; /* success */ return ret_code; /* success */
} }
#ifdef DEBUG_PRINT
void DebugPrintArray( char *pStr, const u8 *pData, int length )
{
int i;
if( cr_print_flag ) {
printf( "%s", pStr );
for( i = 0 ; i < length; i++ ) {
if( (i % 16) == 0 ) printf("\n ");
printf("%02X ", pData[ i ] );
}
printf("\n");
}
}
#endif

View File

@ -239,12 +239,6 @@ typedef signed long s32;
typedef unsigned long u32; typedef unsigned long u32;
typedef unsigned long long u64; typedef unsigned long long u64;
//#define DEBUG_PRINT 1
#ifdef USE_SFMT
#include "SFMT.h"
#endif /* USE_SFMT */
#define CR_ID_BUF_SIZE (2048/8) #define CR_ID_BUF_SIZE (2048/8)
#define CR_NUM_OF_SERIAL 5 #define CR_NUM_OF_SERIAL 5
@ -263,10 +257,9 @@ typedef unsigned long long u64;
#define CR_ID1_MASK 0x00000003FFFFFFFFll // 34bit #define CR_ID1_MASK 0x00000003FFFFFFFFll // 34bit
#define CR_ID2_MASK 0xFFFFFFFFFFFFFFFFll // 64bit #define CR_ID2_MASK 0xFFFFFFFFFFFFFFFFll // 64bit
int cr_generate_id_initialize( void ); extern int cr_generate_id_initialize( void );
int cr_generate_id_finalize( void ); extern int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id[CR_ID_BUF_SIZE], u8 bondingOption );
extern int cr_generate_id_finalize( void );
int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id[CR_ID_BUF_SIZE], u8 bondingOption );
extern int cr_print_flag; extern int cr_print_flag;

View File

@ -116,9 +116,17 @@
extern "C" { extern "C" {
#endif #endif
//#define DEBUG_PRINT 1
int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf ); #ifdef DEBUG_PRINT
int crypto_rsa_enc_dec( unsigned char *dst_buf, unsigned char *org_buf ); #define DEBUG_PRINT_ARRAY DebugPrintArray
#else // !DEBUG_PRINT
#define DEBUG_PRINT_ARRAY( ... ) ((void)0)
#endif // DEBUG_PRINT
extern int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf );
extern int crypto_rsa_enc_dec( unsigned char *dst_buf, unsigned char *org_buf );
extern void DebugPrintArray( char *pStr, const u8 *pData, int length );
#ifdef __cplusplus #ifdef __cplusplus
} }

4
main.c
View File

@ -14,6 +14,10 @@
#include <unistd.h> #include <unistd.h>
#endif // Linux #endif // Linux
#ifdef USE_SFMT
#include "SFMT.h"
#endif /* USE_SFMT */
#include "cr_generate_id.h" #include "cr_generate_id.h"
#define T_BONDING_OPTION 0 // TORIAEZU bondingOption = 0 #define T_BONDING_OPTION 0 // TORIAEZU bondingOption = 0