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

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++ )
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
cmd.cmd = Cmd_Encrypt;
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_alloc.h"
// #define DEBUG_PRINT 1
#define CR_GEN_ID_VERSION 1
#define CR_NUM_OF_SERIAL 5
#define CR_RSV_LENGTH 0x0C
#define CR_RANDOM_LENGTH 0x50
#define EC_PRIVATE_KEY_LENGTH 0x20
#define ECDSA_SIGN_LENGTH 0x44
//#define SHA256_DIGEST_LENGTH 0x20 // OpenSSL側で定義済み
#define EC_CURVE_NAME NID_sect233r1
#define CA_FILE "dummyCA/NintendoCTR2_dummy.crt"
@ -220,15 +217,15 @@ certEntry certEntries[ENTRY_COUNT] =
// TWL device cert base
typedef struct CTR_Device_Cert
{
u8 sigType[4]; // 0x000 - 0x003 : 0x00010002, signature type is ECDSA
u8 eccSignature[60]; // 0x004 - 0x03F : ECDSA using SHA-1 and CA key
u8 padding0[64]; // 0x040 - 0x07F : zero-filled
u8 issuerName[64]; // 0x080 - 0x0BF : issuer name, "Root-CA00000002-MS00000008"
u8 keyType[4]; // 0x0C0 - 0x0C3 : 0x00000002, cert public key type is ECC233
u8 subject[64]; // 0x0C4 - 0x103 : subject field, "CTxxxxxxxx-yyyyyyyyyyyyyyyy"
u8 sigType[ 4 ]; // 0x000 - 0x003 : 0x00010002, signature type is ECDSA
u8 eccSignature[ 60 ]; // 0x004 - 0x03F : ECDSA using SHA-1 and CA key
u8 padding0[ 64 ]; // 0x040 - 0x07F : zero-filled
u8 issuerName[ 64 ]; // 0x080 - 0x0BF : issuer name, "Root-CA00000002-MS00000008"
u8 keyType[ 4 ]; // 0x0C0 - 0x0C3 : 0x00000002, cert public key type is ECC233
u8 subject[ 64 ]; // 0x0C4 - 0x103 : subject field, "CTxxxxxxxx-yyyyyyyyyyyyyyyy"
u32 expiryDate; // 0x104 - 0x107 : second from Epoch (Jan 1, 1970 00:00)
u8 eccPubKey[60]; // 0x108 - 0x143 : cert public key (openssl sect233r1)
u8 padding1[60]; // 0x144 - 0x17F : zero-filled
u8 eccPubKey[ 60 ]; // 0x108 - 0x143 : cert public key (openssl sect233r1)
u8 padding1[ 60 ]; // 0x144 - 0x17F : zero-filled
} CTR_Device_Cert;
@ -295,7 +292,7 @@ static int generate_EC_private_key( EC_KEY *eckey, u8 *privKey )
int i;
for ( i = 0; i < EC_PRIVATE_KEY_LENGTH; i++ )
{
privKey[i] = (u8)gen_rand32();
privKey[i] = (u8)rand();
}
}
#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 );
eckey->priv_key = bn_privkey;
#ifdef DEBUG_PRINT
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 */
DEBUG_PRINT_ARRAY( "ec private key:", (const char *)privKey, EC_PRIVATE_KEY_LENGTH );
return ret_code;
} // generate_EC_private_key
@ -594,17 +579,7 @@ static int generate_X509_cert( EVP_PKEY *evp_pkey, X509_REQ *req, X509 *cert, u3
#endif
#if 0
if ( cr_print_flag )
{
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" );
}
DEBUG_PRINT_ARRAY( "eccSignature:", (const char *)cert->signature->data, cert->signature->length );
#endif
#if 0
@ -670,48 +645,18 @@ static int generate_CTRCustom_cert( CTR_Device_Cert *cert, u32 deviceId, u8 bond
int i;
printf( "CTR custom cert\n" );
printf( "sigType : 0x%08X\n", *(unsigned int*)cert->sigType );
printf( "eccSignature :" );
for ( i = 0; i < 60; i++ )
{
if ( i % 16 == 0 )
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] );
DEBUG_PRINT_ARRAY( "eccSignature:", (const char *)cert->eccSignature, sizeof(cert->eccSignature) );
DEBUG_PRINT_ARRAY( "padding0:", (const char *)cert->padding0, sizeof(cert->padding0) );
printf( "issuerName : " );
for ( i = 0; i < sizeof(cert->issuerName); i++ ) printf( "%c", cert->issuerName[i] );
printf( "\n" );
printf( "keyType : 0x%08X\n", *(unsigned int*)cert->keyType );
printf( "subject : " );
for ( i = 0; i < 64; i++ )
printf( "%c", cert->subject[i] );
for ( i = 0; i < sizeof(cert->subject); i++ ) printf( "%c", cert->subject[i] );
printf( "\n" );
printf( "expiryDate : 0x%08X\n", (unsigned int)cert->expiryDate );
printf( "eccPubKey :" );
for ( i = 0; i < 60; i++ )
{
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" );
DEBUG_PRINT_ARRAY( "eccPubKey :", (const char *)cert->eccPubKey, sizeof(cert->eccPubKey) );
DEBUG_PRINT_ARRAY( "padding1:", (const char *)cert->padding1, sizeof(cert->padding1) );
// 証明書の書き込みテスト
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 *cert = NULL;
struct tm *tm_time;
struct timeval tv;
struct timezone tz;
CR_ID_BUFFER *cr_id_buf;
u8 hash[SHA256_DIGEST_LENGTH]; /* SHA256 check */
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 */
// 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 ) );
// 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 ) );
#if 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 ) );
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
@ -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
hsm_get_rtc( &tv.tv_sec );
#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 );
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
if( cr_print_flag )
{
if( cr_print_flag ) {
printf("GMT:%d-%02d-%02d %02d:%02d:%02d\n",
tm_time->tm_year+1900, /* 年 */
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 */
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;
}
#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 */
for( i = 0 ; i < CR_NUM_OF_RANDOM ; i++ ) {
cr_id_buf->random[i] = (u16)rand(); /* 範囲は0から32767 */
memcpy( cr_id_buf->random, buffer, CR_RANDOM_LENGTH );
}
#endif /* USE_SFMT */
#endif // USE_HSM
#ifdef DEBUG_PRINT
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 */
DEBUG_PRINT_ARRAY( "rand:", (const char *)cr_id_buf->random, CR_RANDOM_LENGTH );
//--------------------------------------------------------------
// 楕円曲線鍵ペアを生成
@ -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++ )
cr_id_buf->deviceCertSign[ i + padding ] = cert->signature->data[i];
// mistake!!
#endif
// TORIAEZU : create Custom 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 );
memcpy( &ctr_dev_cert.eccPubKey[30], buf, len );
free( buf );
#endif
// copy sign to Custom cert
//#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 :" );
for ( i = 0; i < len; i++ )
{
if ( i % 8 == 0 )
printf( "\n" );
if ( i % 8 == 0 ) printf( "\n" );
printf( "0x%02X ", *(ptr++) );
}
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 :" );
for ( i = 0; i < len; i++ )
{
if ( i % 8 == 0 )
printf( "\n" );
if ( i % 8 == 0 ) printf( "\n" );
printf( "0x%02X ", *(ptr++) );
}
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,
cr_id_buf->serial[0], cr_id_buf->bondingOption );
#endif
printf("3\n");
#if 0
if( cr_print_flag )
{
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 */
DEBUG_PRINT_ARRAY( "deviceCertSign:", (const char *)cr_id_buf->deviceCertSign, ECDSA_SIGN_LENGTH );
#endif
//--------------------------------------------------------------
// FuseIDバッファ全体のSHA256ハッシュを算出してセット
//--------------------------------------------------------------
SHA256(id_buf, CR_ID_BUF_SIZE - SHA256_DIGEST_LENGTH, cr_id_buf->hash);
memcpy( hash, 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 */
DEBUG_PRINT_ARRAY( "SHA256 Digest:", (const char *)cr_id_buf->hash, SHA256_DIGEST_LENGTH );
//--------------------------------------------------------------
// FuseIDバッファ全体を暗号化
//--------------------------------------------------------------
#ifdef DEBUG_PRINT
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 */
DEBUG_PRINT_ARRAY( "ORG buf:", (const char *)id_buf, CR_ID_BUF_SIZE );
#ifdef ENCRYPT_AES
// AES暗号化 -> 復号化 -> ベリファイ
ret_code = crypto_aes_enc_dec( id_buf, id_buf );
ret_code = crypto_aes_enc_dec( id_buf, id_buf ); // 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
if( ret_code != 0 )
{
if( ret_code != 0 ) {
goto end;
}
#ifdef DEBUG_PRINT
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 */
DEBUG_PRINT_ARRAY( "encrypted:", (const char *)id_buf, CR_ID_BUF_SIZE );
//--------------------------------------------------------------
// 終了処理
@ -1625,3 +1507,19 @@ printf("3\n");
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 long u64;
//#define DEBUG_PRINT 1
#ifdef USE_SFMT
#include "SFMT.h"
#endif /* USE_SFMT */
#define CR_ID_BUF_SIZE (2048/8)
#define CR_NUM_OF_SERIAL 5
@ -263,10 +257,9 @@ typedef unsigned long long u64;
#define CR_ID1_MASK 0x00000003FFFFFFFFll // 34bit
#define CR_ID2_MASK 0xFFFFFFFFFFFFFFFFll // 64bit
int cr_generate_id_initialize( void );
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_generate_id_initialize( 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 );
extern int cr_print_flag;

View File

@ -116,9 +116,17 @@
extern "C" {
#endif
//#define DEBUG_PRINT 1
int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf );
int crypto_rsa_enc_dec( unsigned char *dst_buf, unsigned char *org_buf );
#ifdef DEBUG_PRINT
#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
}

4
main.c
View File

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