mirror of
https://github.com/rvtr/ctr_eFuse.git
synced 2025-11-02 00:11:04 -04:00
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:
parent
57e2336e2b
commit
8e3aaa9444
14
Makefile
14
Makefile
@ -7,12 +7,16 @@
|
|||||||
|
|
||||||
# 最終的なビルドスイッチの設定は、以下の通り。
|
# 最終的なビルドスイッチの設定は、以下の通り。
|
||||||
# DEV_CYGWIN = FALSE
|
# DEV_CYGWIN = FALSE
|
||||||
|
# DEBUG_PRINT = FALSE
|
||||||
|
# DSA_SHA256 = TRUE
|
||||||
# USE_HSM = TRUE
|
# USE_HSM = TRUE
|
||||||
# RESET_HSM = TRUE
|
# RESET_HSM = TRUE
|
||||||
|
|
||||||
DEV_CYGWIN = FALSE
|
DEV_CYGWIN = FALSE
|
||||||
USE_HSM = TRUE
|
DEBUG_PRINT = FALSE
|
||||||
RESET_HSM = TRUE
|
DSA_SHA256 = TRUE
|
||||||
|
USE_HSM = TRUE
|
||||||
|
RESET_HSM = TRUE
|
||||||
|
|
||||||
ifeq ($(USE_HSM),TRUE)
|
ifeq ($(USE_HSM),TRUE)
|
||||||
|
|
||||||
@ -119,6 +123,10 @@ ifeq ($(USE_DUMMY_KEY),TRUE)
|
|||||||
CFLAGS += -DUSE_DUMMY_KEY
|
CFLAGS += -DUSE_DUMMY_KEY
|
||||||
endif
|
endif
|
||||||
|
|
||||||
|
ifeq ($(DEBUG_PRINT),TRUE)
|
||||||
|
CFLAGS += -DDEBUG_PRINT
|
||||||
|
endif
|
||||||
|
|
||||||
ifeq ($(USE_HSM),TRUE)
|
ifeq ($(USE_HSM),TRUE)
|
||||||
CFLAGS += -DUSE_HSM
|
CFLAGS += -DUSE_HSM
|
||||||
CPPFLAGS+= $(NFAST_CPPFLAGS)
|
CPPFLAGS+= $(NFAST_CPPFLAGS)
|
||||||
|
|||||||
150
cr_device_cert.c
150
cr_device_cert.c
@ -185,11 +185,17 @@ int GenerateCTRDeviceCert( EC_KEY *pECkey, u32 device_id, u8 bonding_option, u8
|
|||||||
|
|
||||||
memset( &deviceCert, 0, sizeof(deviceCert) );
|
memset( &deviceCert, 0, sizeof(deviceCert) );
|
||||||
|
|
||||||
// sigType 0x00010005 ECDSA+SHA256
|
// sigType
|
||||||
|
// 0x00010005 ECDSA+SHA256
|
||||||
|
// 0x00010002 ECDSA+SHA1
|
||||||
deviceCert.sigType[0] = 0x00;
|
deviceCert.sigType[0] = 0x00;
|
||||||
deviceCert.sigType[1] = 0x01;
|
deviceCert.sigType[1] = 0x01;
|
||||||
deviceCert.sigType[2] = 0x00;
|
deviceCert.sigType[2] = 0x00;
|
||||||
|
#ifdef DSA_SHA256
|
||||||
deviceCert.sigType[3] = 0x05;
|
deviceCert.sigType[3] = 0x05;
|
||||||
|
#else
|
||||||
|
deviceCert.sigType[3] = 0x02;
|
||||||
|
#endif
|
||||||
|
|
||||||
// issuerName
|
// issuerName
|
||||||
for( i = 0; i < sizeof(issuerName); i++ ) {
|
for( i = 0; i < sizeof(issuerName); i++ ) {
|
||||||
@ -222,26 +228,28 @@ int GenerateCTRDeviceCert( EC_KEY *pECkey, u32 device_id, u8 bonding_option, u8
|
|||||||
// eccSignature
|
// eccSignature
|
||||||
#ifdef USE_HSM
|
#ifdef USE_HSM
|
||||||
|
|
||||||
// TODO: HSM使用時の処理を実装
|
|
||||||
|
|
||||||
// ECDSA署名付加
|
// ECDSA署名付加
|
||||||
|
#ifdef DSA_SHA256
|
||||||
u8 sha256Buf[ SHA256_DIGEST_LENGTH ];
|
u8 sha256Buf[ SHA256_DIGEST_LENGTH ];
|
||||||
|
|
||||||
// CR_DeviceCertのSHA256計算
|
// CR_DeviceCertのSHA256計算
|
||||||
SHA256( deviceCert.issuerName, (int)&deviceCert + sizeof(CR_DeviceCert) - (int)deviceCert.issuerName, sha256Buf );
|
SHA256( deviceCert.issuerName, (int)&deviceCert + sizeof(CR_DeviceCert) - (int)deviceCert.issuerName, sha256Buf );
|
||||||
DEBUG_PRINT_ARRAY( "sha256(HSM)", (const char *)sha256Buf, 32 );
|
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 );
|
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 )
|
if ( ret_code != CR_GENID_SUCCESS )
|
||||||
{
|
{
|
||||||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
SetErrorInfo( __FUNCTION__, __LINE__ );
|
||||||
return ret_code;
|
return ret_code;
|
||||||
}
|
}
|
||||||
|
|
||||||
#else // !USE_HSM
|
#else // !USE_HSM
|
||||||
// DERフォーマットのECC鍵を読み込み
|
// DERフォーマットのECC鍵を読み込み
|
||||||
{
|
{
|
||||||
@ -263,63 +271,73 @@ int GenerateCTRDeviceCert( EC_KEY *pECkey, u32 device_id, u8 bonding_option, u8
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
// ECDSA署名付加
|
// ECDSA署名付加
|
||||||
{
|
#ifdef DSA_SHA256
|
||||||
u8 sha256buf[ SHA256_DIGEST_LENGTH ];
|
u8 sha256Buf[ SHA256_DIGEST_LENGTH ];
|
||||||
u8 ecdsasig[ 0x80 ];
|
u8 ecdsasig[ 0x80 ];
|
||||||
const u8 *pECDSAsig = ecdsasig;
|
const u8 *pECDSAsig = ecdsasig;
|
||||||
ECDSA_SIG *sig = NULL;
|
ECDSA_SIG *sig = NULL;
|
||||||
int signLen = 0;
|
int signLen = 0;
|
||||||
int test_ret = 0;
|
int test_ret = 0;
|
||||||
|
|
||||||
// CR_DeviceCertのSHA256計算
|
// CR_DeviceCertのSHA256計算
|
||||||
SHA256( deviceCert.issuerName, (int)&deviceCert + sizeof(CR_DeviceCert) - (int)deviceCert.issuerName, sha256buf );
|
SHA256( deviceCert.issuerName, (int)&deviceCert + sizeof(CR_DeviceCert) - (int)deviceCert.issuerName, sha256Buf );
|
||||||
|
|
||||||
// 上位232bit分で署名
|
// 上位232bit分で署名
|
||||||
memset( ecdsasig, 0, sizeof(ecdsasig) );
|
memset( ecdsasig, 0, sizeof(ecdsasig) );
|
||||||
test_ret = ECDSA_sign( 0, sha256buf, 233/8, ecdsasig, &signLen, NCT2 );
|
test_ret = ECDSA_sign( 0, sha256Buf, 233/8, ecdsasig, &signLen, NCT2 );
|
||||||
if (test_ret == 0) {
|
#else // !DSA_SHA256
|
||||||
ret_code = CR_GENID_ERROR_ECDSA_SIGN;
|
u8 sha1Buf[ 20 ];
|
||||||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
u8 ecdsasig[ 0x80 ];
|
||||||
goto end;
|
const u8 *pECDSAsig = ecdsasig;
|
||||||
}
|
ECDSA_SIG *sig = NULL;
|
||||||
DEBUG_PRINT_ARRAY( "ECDSA:", (const char *)ecdsasig, signLen );
|
int signLen = 0;
|
||||||
|
int test_ret = 0;
|
||||||
|
|
||||||
#if 0
|
// CR_DeviceCertのSHA1計算
|
||||||
// 署名ベリファイ
|
SHA1( deviceCert.issuerName, (int)&deviceCert + sizeof(CR_DeviceCert) - (int)deviceCert.issuerName, sha1Buf );
|
||||||
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
|
|
||||||
|
|
||||||
// DERデコードして、r と s を eccSignature にセット
|
// 署名
|
||||||
sig = d2i_ECDSA_SIG( NULL, &pECDSAsig, signLen );
|
memset( ecdsasig, 0, sizeof(ecdsasig) );
|
||||||
if( sig == NULL ) {
|
test_ret = ECDSA_sign( 0, sha1Buf, 20, ecdsasig, &signLen, NCT2 );
|
||||||
ret_code = CR_GENID_ERROR_ECDSA_DEC;
|
#endif // DSA_SHA256
|
||||||
SetErrorInfo( __FUNCTION__, __LINE__ );
|
|
||||||
goto end;
|
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
|
#if 0
|
||||||
DEBUG_PRINT_ARRAY( "ECDSA.r:", (const char *)sig->r->d, sig->r->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 );
|
DEBUG_PRINT_ARRAY( "ECDSA.s:", (const char *)sig->s->d, sig->s->dmax * 4 );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
BN2BinWithPadding( sig->r, &deviceCert.eccSignature[ 0 ], 30 );
|
BN2BinWithPadding( sig->r, &deviceCert.eccSignature[ 0 ], 30 );
|
||||||
BN2BinWithPadding( sig->s, &deviceCert.eccSignature[ 30 ], 30 );
|
BN2BinWithPadding( sig->s, &deviceCert.eccSignature[ 30 ], 30 );
|
||||||
memcpy( pDevCertSign, &deviceCert.eccSignature, 60 );
|
memcpy( pDevCertSign, &deviceCert.eccSignature, 60 );
|
||||||
if( sig ) ECDSA_SIG_free( sig );
|
if( sig ) ECDSA_SIG_free( sig );
|
||||||
if( NCT2 ) EC_KEY_free( NCT2 );
|
if( NCT2 ) EC_KEY_free( NCT2 );
|
||||||
NCT2 = NULL;
|
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
|
|
||||||
}
|
|
||||||
#endif // USE_HSM
|
#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署名検証
|
// ECDSA署名検証
|
||||||
{
|
{
|
||||||
// bonding_option によって、鍵を差し替え
|
// 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;
|
ret_code = CR_GENID_ERROR_ECC_READ_PUBLIC_KEY;
|
||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
#if 0
|
#ifdef DSA_SHA256
|
||||||
// TODO: ハッシュ処理
|
// TODO: ハッシュ処理
|
||||||
int i;
|
int i;
|
||||||
u8 verifyHash[30];
|
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);
|
verifyHash[i] = (sha256Buf[i-1] << 1) | (sha256Buf[i] >> 7);
|
||||||
}
|
}
|
||||||
DEBUG_PRINT_ARRAY( (char*)"verifyHash(HSM)", (const char *)verifyHash, 30 );
|
DEBUG_PRINT_ARRAY( (char*)"verifyHash(HSM)", (const char *)verifyHash, 30 );
|
||||||
|
#else // !DSA_SHA256
|
||||||
|
// do nothing
|
||||||
|
#endif // DSA_SHA256
|
||||||
|
|
||||||
// TODO: ECDSA署名(DER)を再構築
|
// TODO: ECDSA署名(DER)を再構築
|
||||||
u8 signBuf[70];
|
u8 signBuf[70];
|
||||||
@ -369,7 +390,9 @@ int GenerateCTRDeviceCert( EC_KEY *pECkey, u32 device_id, u8 bonding_option, u8
|
|||||||
signBuf[0x23] = 0x1E;
|
signBuf[0x23] = 0x1E;
|
||||||
memcpy( &signBuf[0x24], &deviceCert.eccSignature[30], 0x1E );
|
memcpy( &signBuf[0x24], &deviceCert.eccSignature[30], 0x1E );
|
||||||
DEBUG_PRINT_ARRAY( (char*)"sign(HSM)", (const char *)signBuf, signLen );
|
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 );
|
ret_code = ECDSA_verify( 0, verifyHash, 30, signBuf, signLen, NCT2 );
|
||||||
if( ret_code != 1) {
|
if( ret_code != 1) {
|
||||||
@ -378,6 +401,15 @@ int GenerateCTRDeviceCert( EC_KEY *pECkey, u32 device_id, u8 bonding_option, u8
|
|||||||
goto end;
|
goto end;
|
||||||
}
|
}
|
||||||
#endif
|
#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;
|
ret_code = CR_GENID_SUCCESS;
|
||||||
|
|||||||
@ -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.flags = 0; // Cmd_Sign_Args_flags_given_iv_present;
|
||||||
cmd.args.sign.key = privKeyid;
|
cmd.args.sign.key = privKeyid;
|
||||||
cmd.args.sign.mech = HSM_SIGN_MECH;
|
cmd.args.sign.mech = HSM_SIGN_MECH;
|
||||||
#if 1
|
#ifdef DSA_SHA256
|
||||||
cmd.args.sign.plain.type = PlainTextType_Hash32;
|
cmd.args.sign.plain.type = PlainTextType_Hash32;
|
||||||
cmd.args.sign.plain.data.hash32.data = *(M_Hash32*)data_buf;
|
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.type = PlainTextType_Hash;
|
||||||
cmd.args.sign.plain.data.hash.data = *(M_Hash*)data_buf;
|
cmd.args.sign.plain.data.hash.data = *(M_Hash*)data_buf;
|
||||||
#endif
|
#endif // DSA_SHA256
|
||||||
|
|
||||||
// sign command issue
|
// sign command issue
|
||||||
ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL );
|
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 r(HSM)", (const char *)rPtr, rLen );
|
||||||
DEBUG_PRINT_ARRAY( (char*)"sig s(HSM)", (const char *)sPtr, sLen );
|
DEBUG_PRINT_ARRAY( (char*)"sig s(HSM)", (const char *)sPtr, sLen );
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
// verify
|
|
||||||
#if 1
|
#if 1
|
||||||
|
// verify
|
||||||
struct NFast_Bignum *rBn, *sBn;
|
struct NFast_Bignum *rBn, *sBn;
|
||||||
my_bignumCopy( &rBn, reply.reply.sign.sig.data.ecdsa.r, hsmHandle );
|
my_bignumCopy( &rBn, reply.reply.sign.sig.data.ecdsa.r, hsmHandle );
|
||||||
my_bignumCopy( &sBn, reply.reply.sign.sig.data.ecdsa.s, 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.flags = 0;
|
||||||
cmd.args.verify.key = pubKeyid;
|
cmd.args.verify.key = pubKeyid;
|
||||||
cmd.args.verify.mech = HSM_SIGN_MECH;
|
cmd.args.verify.mech = HSM_SIGN_MECH;
|
||||||
#if 1
|
#ifdef DSA_SHA256
|
||||||
cmd.args.verify.plain.type = PlainTextType_Hash32;
|
cmd.args.verify.plain.type = PlainTextType_Hash32;
|
||||||
cmd.args.verify.plain.data.hash32.data = *(M_Hash32*)data_buf;
|
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.type = PlainTextType_Hash;
|
||||||
cmd.args.verify.plain.data.hash.data = *(M_Hash*)data_buf;
|
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.mech = HSM_SIGN_MECH;
|
||||||
cmd.args.verify.sig.data.ecdsa.r = rBn;
|
cmd.args.verify.sig.data.ecdsa.r = rBn;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user