DSA-SHA1,SHA256のビルドスイッチ対応、

デバッグプリントのビルドスイッチ対応

git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_eFuse@169 ff987cc8-cf2f-4642-8568-d52cce064691
This commit is contained in:
kubodera_yuichi 2010-01-12 02:05:38 +00:00
parent 57e2336e2b
commit 8e3aaa9444
3 changed files with 109 additions and 70 deletions

View File

@ -7,12 +7,16 @@
# 最終的なビルドスイッチの設定は、以下の通り。
# DEV_CYGWIN = FALSE
# DEBUG_PRINT = FALSE
# DSA_SHA256 = TRUE
# USE_HSM = TRUE
# RESET_HSM = TRUE
DEV_CYGWIN = FALSE
USE_HSM = TRUE
RESET_HSM = TRUE
DEV_CYGWIN = FALSE
DEBUG_PRINT = FALSE
DSA_SHA256 = TRUE
USE_HSM = TRUE
RESET_HSM = TRUE
ifeq ($(USE_HSM),TRUE)
@ -119,6 +123,10 @@ ifeq ($(USE_DUMMY_KEY),TRUE)
CFLAGS += -DUSE_DUMMY_KEY
endif
ifeq ($(DEBUG_PRINT),TRUE)
CFLAGS += -DDEBUG_PRINT
endif
ifeq ($(USE_HSM),TRUE)
CFLAGS += -DUSE_HSM
CPPFLAGS+= $(NFAST_CPPFLAGS)

View File

@ -185,11 +185,17 @@ int GenerateCTRDeviceCert( EC_KEY *pECkey, u32 device_id, u8 bonding_option, u8
memset( &deviceCert, 0, sizeof(deviceCert) );
// sigType 0x00010005 ECDSA+SHA256
// sigType
// 0x00010005 ECDSA+SHA256
// 0x00010002 ECDSA+SHA1
deviceCert.sigType[0] = 0x00;
deviceCert.sigType[1] = 0x01;
deviceCert.sigType[2] = 0x00;
#ifdef DSA_SHA256
deviceCert.sigType[3] = 0x05;
#else
deviceCert.sigType[3] = 0x02;
#endif
// issuerName
for( i = 0; i < sizeof(issuerName); i++ ) {
@ -222,26 +228,28 @@ int GenerateCTRDeviceCert( EC_KEY *pECkey, u32 device_id, u8 bonding_option, u8
// eccSignature
#ifdef USE_HSM
// TODO: HSM使用時の処理を実装
// ECDSA署名付加
#ifdef DSA_SHA256
u8 sha256Buf[ SHA256_DIGEST_LENGTH ];
// CR_DeviceCertのSHA256計算
SHA256( deviceCert.issuerName, (int)&deviceCert + sizeof(CR_DeviceCert) - (int)deviceCert.issuerName, sha256Buf );
DEBUG_PRINT_ARRAY( "sha256(HSM)", (const char *)sha256Buf, 32 );
//u8 sha1Buf[ 20 ];
//SHA1( deviceCert.issuerName, (int)&deviceCert + sizeof(CR_DeviceCert) - (int)deviceCert.issuerName, sha1Buf );
//DEBUG_PRINT_ARRAY( "sha1(HSM)", (const char *)sha1buf, 20 );
ret_code = hsm_ecdsa_sign( deviceCert.eccSignature, sha256Buf, bonding_option );
#else // !DSA_SHA256
u8 sha1Buf[ 20 ];
SHA1( deviceCert.issuerName, (int)&deviceCert + sizeof(CR_DeviceCert) - (int)deviceCert.issuerName, sha1Buf );
DEBUG_PRINT_ARRAY( "sha1(HSM)", (const char *)sha1buf, 20 );
ret_code = hsm_ecdsa_sign( deviceCert.eccSignature, sha1Buf, bonding_option );
#endif // DSA_SHA256
if ( ret_code != CR_GENID_SUCCESS )
{
SetErrorInfo( __FUNCTION__, __LINE__ );
return ret_code;
}
#else // !USE_HSM
// DERフォーマットのECC鍵を読み込み
{
@ -263,63 +271,73 @@ int GenerateCTRDeviceCert( EC_KEY *pECkey, u32 device_id, u8 bonding_option, u8
#endif
}
// ECDSA署名付加
{
u8 sha256buf[ SHA256_DIGEST_LENGTH ];
u8 ecdsasig[ 0x80 ];
const u8 *pECDSAsig = ecdsasig;
ECDSA_SIG *sig = NULL;
int signLen = 0;
int test_ret = 0;
#ifdef DSA_SHA256
u8 sha256Buf[ SHA256_DIGEST_LENGTH ];
u8 ecdsasig[ 0x80 ];
const u8 *pECDSAsig = ecdsasig;
ECDSA_SIG *sig = NULL;
int signLen = 0;
int test_ret = 0;
// CR_DeviceCertのSHA256計算
SHA256( deviceCert.issuerName, (int)&deviceCert + sizeof(CR_DeviceCert) - (int)deviceCert.issuerName, sha256buf );
// CR_DeviceCertのSHA256計算
SHA256( deviceCert.issuerName, (int)&deviceCert + sizeof(CR_DeviceCert) - (int)deviceCert.issuerName, sha256Buf );
// 上位232bit分で署名
memset( ecdsasig, 0, sizeof(ecdsasig) );
test_ret = ECDSA_sign( 0, sha256buf, 233/8, ecdsasig, &signLen, NCT2 );
if (test_ret == 0) {
ret_code = CR_GENID_ERROR_ECDSA_SIGN;
SetErrorInfo( __FUNCTION__, __LINE__ );
goto end;
}
DEBUG_PRINT_ARRAY( "ECDSA:", (const char *)ecdsasig, signLen );
// 上位232bit分で署名
memset( ecdsasig, 0, sizeof(ecdsasig) );
test_ret = ECDSA_sign( 0, sha256Buf, 233/8, ecdsasig, &signLen, NCT2 );
#else // !DSA_SHA256
u8 sha1Buf[ 20 ];
u8 ecdsasig[ 0x80 ];
const u8 *pECDSAsig = ecdsasig;
ECDSA_SIG *sig = NULL;
int signLen = 0;
int test_ret = 0;
#if 0
// 署名ベリファイ
ret_code = ECDSA_verify( 0, sha1buf, 20, tempSign, 66, NCT2 );
if( ret_code != 1) {
ret_code = CR_GENID_ERROR_ECDSA_VERIFY;
SetErrorInfo( __FUNCTION__, __LINE__ );
goto end;
}
#endif
// CR_DeviceCertのSHA1計算
SHA1( deviceCert.issuerName, (int)&deviceCert + sizeof(CR_DeviceCert) - (int)deviceCert.issuerName, sha1Buf );
// DERデコードして、r と s を eccSignature にセット
sig = d2i_ECDSA_SIG( NULL, &pECDSAsig, signLen );
if( sig == NULL ) {
ret_code = CR_GENID_ERROR_ECDSA_DEC;
SetErrorInfo( __FUNCTION__, __LINE__ );
goto end;
}
// 署名
memset( ecdsasig, 0, sizeof(ecdsasig) );
test_ret = ECDSA_sign( 0, sha1Buf, 20, ecdsasig, &signLen, NCT2 );
#endif // DSA_SHA256
if (test_ret == 0) {
ret_code = CR_GENID_ERROR_ECDSA_SIGN;
SetErrorInfo( __FUNCTION__, __LINE__ );
goto end;
}
DEBUG_PRINT_ARRAY( "ECDSA:", (const char *)ecdsasig, signLen );
// DERデコードして、r と s を eccSignature にセット
sig = d2i_ECDSA_SIG( NULL, &pECDSAsig, signLen );
if( sig == NULL ) {
ret_code = CR_GENID_ERROR_ECDSA_DEC;
SetErrorInfo( __FUNCTION__, __LINE__ );
goto end;
}
#if 0
DEBUG_PRINT_ARRAY( "ECDSA.r:", (const char *)sig->r->d, sig->r->dmax * 4);
DEBUG_PRINT_ARRAY( "ECDSA.s:", (const char *)sig->s->d, sig->s->dmax * 4 );
DEBUG_PRINT_ARRAY( "ECDSA.r:", (const char *)sig->r->d, sig->r->dmax * 4);
DEBUG_PRINT_ARRAY( "ECDSA.s:", (const char *)sig->s->d, sig->s->dmax * 4 );
#endif
BN2BinWithPadding( sig->r, &deviceCert.eccSignature[ 0 ], 30 );
BN2BinWithPadding( sig->s, &deviceCert.eccSignature[ 30 ], 30 );
memcpy( pDevCertSign, &deviceCert.eccSignature, 60 );
if( sig ) ECDSA_SIG_free( sig );
if( NCT2 ) EC_KEY_free( NCT2 );
NCT2 = NULL;
#ifdef DEBUG_DEVICE_CERT_OUTPUT_FILE
DebugFileOutput( device_id, "dgst", sha256buf, 233/8 );
DebugFileOutput( device_id, "sign", ecdsasig, signLen );
#endif // DEBUG_DEVICE_CERT_OUTPUT_FILE
}
BN2BinWithPadding( sig->r, &deviceCert.eccSignature[ 0 ], 30 );
BN2BinWithPadding( sig->s, &deviceCert.eccSignature[ 30 ], 30 );
memcpy( pDevCertSign, &deviceCert.eccSignature, 60 );
if( sig ) ECDSA_SIG_free( sig );
if( NCT2 ) EC_KEY_free( NCT2 );
NCT2 = NULL;
#endif // USE_HSM
#ifdef DEBUG_DEVICE_CERT_OUTPUT_FILE
#ifdef DSA_SHA256
DebugFileOutput( device_id, "dgst", sha256Buf, 233/8 );
#else // !DSA_SHA256
DebugFileOutput( device_id, "dgst", sha1Buf, 20 );
#endif // DSA_SHA256
DebugFileOutput( device_id, "sign", ecdsasig, signLen );
#endif // DEBUG_DEVICE_CERT_OUTPUT_FILE
// ECDSA署名検証
{
// bonding_option によって、鍵を差し替え
@ -343,7 +361,7 @@ int GenerateCTRDeviceCert( EC_KEY *pECkey, u32 device_id, u8 bonding_option, u8
ret_code = CR_GENID_ERROR_ECC_READ_PUBLIC_KEY;
goto end;
}
#if 0
#ifdef DSA_SHA256
// TODO: ハッシュ処理
int i;
u8 verifyHash[30];
@ -355,6 +373,9 @@ int GenerateCTRDeviceCert( EC_KEY *pECkey, u32 device_id, u8 bonding_option, u8
verifyHash[i] = (sha256Buf[i-1] << 1) | (sha256Buf[i] >> 7);
}
DEBUG_PRINT_ARRAY( (char*)"verifyHash(HSM)", (const char *)verifyHash, 30 );
#else // !DSA_SHA256
// do nothing
#endif // DSA_SHA256
// TODO: ECDSA署名DERを再構築
u8 signBuf[70];
@ -369,7 +390,9 @@ int GenerateCTRDeviceCert( EC_KEY *pECkey, u32 device_id, u8 bonding_option, u8
signBuf[0x23] = 0x1E;
memcpy( &signBuf[0x24], &deviceCert.eccSignature[30], 0x1E );
DEBUG_PRINT_ARRAY( (char*)"sign(HSM)", (const char *)signBuf, signLen );
#ifdef DSA_SHA256
#if 0
// 署名ベリファイ
ret_code = ECDSA_verify( 0, verifyHash, 30, signBuf, signLen, NCT2 );
if( ret_code != 1) {
@ -378,6 +401,15 @@ int GenerateCTRDeviceCert( EC_KEY *pECkey, u32 device_id, u8 bonding_option, u8
goto end;
}
#endif
#else // !DSA_SHA256
// 署名ベリファイ
ret_code = ECDSA_verify( 0, sha1Buf, 20, signBuf, signLen, NCT2 );
if( ret_code != 1) {
ret_code = CR_GENID_ERROR_ECDSA_VERIFY;
SetErrorInfo( __FUNCTION__, __LINE__ );
goto end;
}
#endif // DSA_SHA256
}
ret_code = CR_GENID_SUCCESS;

View File

@ -899,13 +899,13 @@ int hsm_ecdsa_sign( unsigned char *sign_buf, unsigned char *data_buf, unsigned c
cmd.args.sign.flags = 0; // Cmd_Sign_Args_flags_given_iv_present;
cmd.args.sign.key = privKeyid;
cmd.args.sign.mech = HSM_SIGN_MECH;
#if 1
#ifdef DSA_SHA256
cmd.args.sign.plain.type = PlainTextType_Hash32;
cmd.args.sign.plain.data.hash32.data = *(M_Hash32*)data_buf;
#else
#else // !DSA_SHA256
cmd.args.sign.plain.type = PlainTextType_Hash;
cmd.args.sign.plain.data.hash.data = *(M_Hash*)data_buf;
#endif
#endif // DSA_SHA256
// sign command issue
ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL );
@ -935,10 +935,9 @@ int hsm_ecdsa_sign( unsigned char *sign_buf, unsigned char *data_buf, unsigned c
DEBUG_PRINT_ARRAY( (char*)"sig r(HSM)", (const char *)rPtr, rLen );
DEBUG_PRINT_ARRAY( (char*)"sig s(HSM)", (const char *)sPtr, sLen );
#endif
// verify
#if 1
// verify
struct NFast_Bignum *rBn, *sBn;
my_bignumCopy( &rBn, reply.reply.sign.sig.data.ecdsa.r, hsmHandle );
my_bignumCopy( &sBn, reply.reply.sign.sig.data.ecdsa.s, hsmHandle );
@ -952,13 +951,13 @@ int hsm_ecdsa_sign( unsigned char *sign_buf, unsigned char *data_buf, unsigned c
cmd.args.verify.flags = 0;
cmd.args.verify.key = pubKeyid;
cmd.args.verify.mech = HSM_SIGN_MECH;
#if 1
#ifdef DSA_SHA256
cmd.args.verify.plain.type = PlainTextType_Hash32;
cmd.args.verify.plain.data.hash32.data = *(M_Hash32*)data_buf;
#else
#else // !DSA_SHA256
cmd.args.verify.plain.type = PlainTextType_Hash;
cmd.args.verify.plain.data.hash.data = *(M_Hash*)data_buf;
#endif
#endif // DSA_SHA256
cmd.args.verify.sig.mech = HSM_SIGN_MECH;
cmd.args.verify.sig.data.ecdsa.r = rBn;