・ビルドスイッチの整理。

HSM使用時は、ダミー鍵をリンクしないよう変更。
 HSM未使用時は、ダミー鍵を使用するよう変更。
 ENCRYPT_AESは、Makefileではなく、ヘッダで定義するよう変更。

・ダミー鍵処理を整備。
 AES鍵も外部データをリンクするよう変更。
 RSA鍵、AES鍵を共に開発鍵と製品鍵の両方を用意し、ボンディングオプションで切り替えるよう変更。
 ダミー鍵使用時は、gen_id.exeを停止せず、最初に警告メッセージを表示するよう変更。


git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_eFuse@32 ff987cc8-cf2f-4642-8568-d52cce064691
This commit is contained in:
(no author) 2009-12-22 08:35:27 +00:00
parent d2278af9ab
commit 1685d4ae68
16 changed files with 152 additions and 125 deletions

View File

@ -4,16 +4,21 @@
# nm generate_id.o | grep " [T|B|D] "
# nm ../rsa_keysrcgen/rsa1_key.o | grep " [T|B|D] "
# ダミーのRSA鍵ペアを使用する場合は、USE_DUMMY_KEYをTRUEにしてください。
# 正式な RSA鍵ペアを使用する場合は、USE_DUMMY_KEYをコメントアウトして、DER_KEY_DIRにRSA鍵ペアが入ったフォルダを指定してください。
# 最終的なビルドスイッチの設定は、以下の通り。
# DEV_CYGWIN = FALSE
# USE_HSM = TRUE
# USE_SFMT = XX
DEV_CYGWIN = TRUE
#ENCRYPT_AES = TRUE
USE_DUMMY_KEY = TRUE
USE_SFMT = TRUE
#USE_HSM = TRUE
USE_SFMT = TRUE
ifeq ($(USE_HSM),TRUE)
# HSM使用時は強制的にDUMMY_KEYは未使用にする。
USE_DUMMY_KEY = FALSE
# nFast Path
NFAST_PATH = /opt/nfast
@ -52,14 +57,20 @@ NFAST_OBJS_LIST = \
$(NFAST_OBJS_PATH)/simplebignum.o \
$(NFAST_OBJS_PATH)/nfutil.o \
else # !USE_HSM
# HSMが使用できない場合は、DUMMY_KEYを使ってテストする。
USE_DUMMY_KEY = TRUE
endif # USE_HSM
ifeq ($(USE_DUMMY_KEY),TRUE)
DER_KEY_DIR = ./rsakey_dummy
else
#DER_KEY_DIR = ./rsakey
DER_KEY_DIR = ./rsakey_dummy
endif
DEV_DER_KEY_DIR = ./dummyKey/dev
PROD_DER_KEY_DIR = ./dummyKey/prod
else # !USE_DUMMY_KEY
DEV_DER_KEY_DIR = /cygdrive/w/CTR/dev
PROD_DER_KEY_DIR = /cygdrive/w/CTR/prod
endif # USE_DUMMY_KEY
PACKAGE_DIR = ./package
# OPENSSL_DIR = ./openssl-1.0.0-beta2
@ -69,10 +80,12 @@ SFMT_DIR = ./SFMT-src-1.3.3
TARGET = gen_id
KEYS_C = cr_gen_id_rsa_key_priv.c cr_gen_id_rsa_key_pub.c
KEYS_H = $(KEYS_C:.c=.h)
KEYS_C = cr_eFuse_privKey_prod.c cr_eFuse_pubKey_prod.c \
cr_eFuse_privKey_dev.c cr_eFuse_pubKey_dev.c \
cr_eFuse_aesKey_prod.c cr_eFuse_iv_prod.c \
cr_eFuse_aesKey_dev.c cr_eFuse_iv_dev.c
SRCS = main.c cr_generate_id.c cr_hsm_util.c cr_keyPair.c \
cr_deviceCert.c cr_enc_id.c cr_alloc.c cr_hsm_code.c $(KEYS_C)
cr_deviceCert.c cr_enc_id.c cr_alloc.c cr_hsm_code.c
ifeq ($(USE_SFMT),TRUE)
SRCS += $(SFMT_DIR)/SFMT.c
@ -97,10 +110,6 @@ LDFLAGS += -Wl
LDLIBS += -ldl -lnsl
endif # DEV_CYGWIN
ifeq ($(ENCRYPT_AES),TRUE)
CFLAGS += -DENCRYPT_AES
endif
ifeq ($(USE_DUMMY_KEY),TRUE)
CFLAGS += -DUSE_DUMMY_KEY
endif
@ -146,11 +155,30 @@ $(OBJS): $(HEADS) Makefile
#%.c:$(DER_KEY_DIR)/%.der
# perl tools/bin2c.plx $<
cr_gen_id_rsa_key_priv.c : $(DER_KEY_DIR)/gen_id_rsa_key_priv.der
perl tools/bin2c.plx $<
cr_eFuse_privKey_prod.c : $(PROD_DER_KEY_DIR)/eFuse_privKey.der
perl tools/bin2c.plx $< prod
cr_gen_id_rsa_key_pub.c : $(DER_KEY_DIR)/gen_id_rsa_key_pub.der
perl tools/bin2c.plx $<
cr_eFuse_pubKey_prod.c : $(PROD_DER_KEY_DIR)/eFuse_pubKey.der
perl tools/bin2c.plx $< prod
cr_eFuse_privKey_dev.c : $(DEV_DER_KEY_DIR)/eFuse_privKey.der
perl tools/bin2c.plx $< dev
cr_eFuse_pubKey_dev.c : $(DEV_DER_KEY_DIR)/eFuse_pubKey.der
perl tools/bin2c.plx $< dev
cr_eFuse_aesKey_prod.c : $(PROD_DER_KEY_DIR)/eFuse_aesKey.bin
perl tools/bin2c.plx $< prod
cr_eFuse_iv_prod.c : $(PROD_DER_KEY_DIR)/eFuse_iv.bin
perl tools/bin2c.plx $< prod
cr_eFuse_aesKey_dev.c : $(DEV_DER_KEY_DIR)/eFuse_aesKey.bin
perl tools/bin2c.plx $< dev
cr_eFuse_iv_dev.c : $(DEV_DER_KEY_DIR)/eFuse_iv.bin
perl tools/bin2c.plx $< dev
.PHONY: clean clobber

View File

@ -129,8 +129,6 @@
#include <openssl/rsa.h>
#include <openssl/aes.h>
#include "ec_lcl.h" // ec_key_st構造体の参照に必要
#include "cr_generate_id.h"
#include "cr_generate_id_private.h"
#include "cr_alloc.h"

View File

@ -110,6 +110,7 @@
*/
#include "cr_generate_id.h"
#include "cr_generate_id_private.h"
#include <assert.h>
#include <stdio.h>
@ -125,39 +126,49 @@
#include <openssl/err.h>
#include <openssl/rsa.h>
#include <openssl/aes.h>
#include "cr_gen_id_rsa_key_priv.h"
#include "cr_gen_id_rsa_key_pub.h"
#include "cr_eFuse_privKey_dev.c"
#include "cr_eFuse_pubKey_dev.c"
#include "cr_eFuse_privKey_prod.c"
#include "cr_eFuse_pubKey_prod.c"
#include "cr_eFuse_aesKey_dev.c"
#include "cr_eFuse_iv_dev.c"
#include "cr_eFuse_aesKey_prod.c"
#include "cr_eFuse_iv_prod.c"
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 );
static int crypto_aes_enc_dec( unsigned char *dst_buf,unsigned char *org_buf, u8 bondingOption );
#else // !ENCRYPT_AES
static int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf );
static int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf, u8 bondingOption );
#endif // ENCRYPT_AES
static unsigned char local_buf_1[CR_ID_BUF_SIZE];
static unsigned char local_buf_2[CR_ID_BUF_SIZE];
// 繝薙Ν繝峨せ繧、繝・メ縺ォ蠢懊§縺ヲ縲D繝舌ャ繝輔ぃ繧但ES/RSA縺ァ證怜捷蛹
int EncryptID( unsigned char *dst_buf, unsigned char *org_buf )
// ビルドスイッチに従ってAES or RSA で指定バッファを暗号化
int EncryptID( unsigned char *dst_buf, unsigned char *org_buf, u8 bondingOption )
{
#ifdef ENCRYPT_AES
return crypto_aes_enc_dec( dst_buf, org_buf ); // AES
if( cr_print_flag ) printf( "[AES]\n");
return crypto_aes_enc_dec( dst_buf, org_buf, bondingOption ); // AES
#else // !ENCRYPT_AES
return crypto_rsa_enc_dec( dst_buf, org_buf ); // RSA pubKey enc
if( cr_print_flag ) printf( "[RSA]\n");
return crypto_rsa_enc_dec( dst_buf, org_buf, bondingOption ); // RSA pubKey enc
#endif // ENCRYPT_AES
}
#ifdef ENCRYPT_AES
// AES證怜捷蛹・-> 蠕ゥ蜿キ蛹・-> 繝吶Μ繝輔ぃ繧、
// AES
#ifdef USE_HSM
int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf )
int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf, u8 bondingOption )
{
int i;
int ret_code = 0;
@ -178,69 +189,6 @@ int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf )
return ret_code;
}
// 繝吶Μ繝輔ぃ繧、
for ( i = 0 ; i < CR_ID_BUF_SIZE ; i++ )
{
if( org_buf[i] != local_buf_2[i] )
{
printf( "error : aes verify\n" );
return 0; // AES繧ィ繝ゥ繝シ繧ウ繝シ繝峨菴懊縺ェ縺上※縺ッ繝サ繝サ繝サ
}
}
// 繝舌ャ繝輔ぃ繧ウ繝斐・
memcpy( dst_buf, local_buf_1, CR_ID_BUF_SIZE );
return CR_GENID_SUCCESS;
} // hsm_crypto_aes_enc_dec
#else // !USE_HSM
int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf )
{
int i;
AES_KEY aesEncKey;
AES_KEY aesDecKey;
u8 temp_iv[16];
memset( local_buf_1, 0, CR_ID_BUF_SIZE );
memset( local_buf_2, 0, CR_ID_BUF_SIZE );
// AES 證怜捷蛹也畑骰オ 菴懈・
if ( AES_set_encrypt_key( AES_PASS_PHRASE, 128, &aesEncKey ) != 0 )
{
printf( "error : AES_set_encrypt_key\n" );
return 0;
}
// AES 蠕ゥ蜿キ蛹也畑骰オ 菴懈・
if ( AES_set_decrypt_key( AES_PASS_PHRASE, 128, &aesDecKey ) != 0 )
{
printf( "error : AES_set_decrypt_key\n" );
return 0;
}
// iv 蛻晄悄蛹
for ( i = 0; i < 16; i++ )
{
temp_iv[i] = i;
}
// AES 證怜捷蛹
AES_cbc_encrypt ( org_buf, local_buf_1, CR_ID_BUF_SIZE, &aesEncKey, temp_iv, AES_ENCRYPT );
// iv 蛻晄悄蛹
for ( i = 0; i < 16; i++ )
{
temp_iv[i] = i;
}
// AES 蠕ゥ蜿キ蛹
AES_cbc_encrypt ( local_buf_1, local_buf_2, CR_ID_BUF_SIZE, &aesDecKey, temp_iv, AES_DECRYPT );
// 繝吶Μ繝輔ぃ繧、
for ( i = 0 ; i < CR_ID_BUF_SIZE ; i++ )
{
if( org_buf[i] != local_buf_2[i] )
@ -250,7 +198,54 @@ int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf )
}
}
// 繝舌ャ繝輔ぃ繧ウ繝斐・
memcpy( dst_buf, local_buf_1, CR_ID_BUF_SIZE );
return CR_GENID_SUCCESS;
} // hsm_crypto_aes_enc_dec
#else // !USE_HSM
int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf, u8 bondingOption )
{
int i;
AES_KEY aesEncKey;
AES_KEY aesDecKey;
u8 temp_iv[16];
// 鍵データ取り出し。ヘッダ部分0x10を除去。
char *pAesKey = (char *)( bondingOption ? cr_eFuse_aesKey_dev : cr_eFuse_aesKey_prod ) + 0x10;
char *pIV = (char *)( bondingOption ? cr_eFuse_iv_dev : cr_eFuse_iv_prod ) + 0x10;
memset( local_buf_1, 0, CR_ID_BUF_SIZE );
memset( local_buf_2, 0, CR_ID_BUF_SIZE );
if ( AES_set_encrypt_key( pAesKey, 128, &aesEncKey ) != 0 )
{
printf( "error : AES_set_encrypt_key\n" );
return 0;
}
if ( AES_set_decrypt_key( pAesKey, 128, &aesDecKey ) != 0 )
{
printf( "error : AES_set_decrypt_key\n" );
return 0;
}
memcpy( temp_iv, pIV, 16 );
AES_cbc_encrypt ( org_buf, local_buf_1, CR_ID_BUF_SIZE, &aesEncKey, temp_iv, AES_ENCRYPT );
memcpy( temp_iv, pIV, 16 );
AES_cbc_encrypt ( local_buf_1, local_buf_2, CR_ID_BUF_SIZE, &aesDecKey, temp_iv, AES_DECRYPT );
// ベリファイ
for ( i = 0 ; i < CR_ID_BUF_SIZE ; i++ )
{
if( org_buf[i] != local_buf_2[i] )
{
printf( "error : aes verify\n" );
return 0;
}
}
memcpy( dst_buf, local_buf_1, CR_ID_BUF_SIZE );
return CR_GENID_SUCCESS;
@ -260,10 +255,10 @@ int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf )
#else // !ENCRYPT_AES
// RSA證怜捷蛹・>蠕ゥ蜿キ蛹・>繝吶Μ繝輔ぃ繧、
// RSA
#ifdef USE_HSM
int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf )
int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf, u8 bondingOption )
{
int i;
int ret_code = 0;
@ -284,7 +279,7 @@ int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf )
return ret_code;
}
// 繝吶Μ繝輔ぃ繧、
// ベリファイ
for ( i = 0 ; i < CR_ID_BUF_SIZE ; i++ )
{
if( org_buf[i] != local_buf_2[i] )
@ -293,7 +288,6 @@ int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf )
}
}
// 繝舌ャ繝輔ぃ繧ウ繝斐・
memcpy( dst_buf, local_buf_1, CR_ID_BUF_SIZE );
return CR_GENID_SUCCESS;
@ -301,7 +295,7 @@ int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf )
#else // USE_HSM
int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf)
int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf, u8 bondingOption )
{
int ret_code = 0;
int rsa_outlen = 0;
@ -312,19 +306,22 @@ 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フォーマットのRSA鍵を読み込み
{
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縺悟・蜉帙☆繧狗ァ伜ッ・嵯縺ッ縲 ̄KCS#1 RSAPublicKey繝輔か繝シ繝槭ャ繝医↑縺ョ縺ァ縲√縺ョ髢「謨ー繧剃スソ縺・€
// bondingOptionによって、鍵を差し替え
const unsigned char *der_priv = bondingOption ? cr_eFuse_privKey_dev : cr_eFuse_privKey_prod;
const unsigned char *der_pub = bondingOption ? cr_eFuse_pubKey_dev : cr_eFuse_pubKey_prod;
int priv_len = der_priv[ 8 ] | der_priv[ 9 ] << 8; // KEY長を取り出し
int pub_len = der_pub [ 8 ] | der_pub [ 9 ] << 8; // 同上
der_priv += 0x10; // ヘッダ部分を除外してKEY実体を指定
der_pub += 0x10; // 同上
// コマンドラインのopensslが出力する秘密鍵は、PKCS#1 RSAPublicKeyフォーマットなので、この関数を使う。
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縺悟・蜉帙☆繧句・髢矩嵯縺ッ縲ヾubjectPublicKeyInfo蠖「蠑上↑縺ョ縺ァ縲√縺ョ髢「謨ー繧剃スソ縺・€
// コマンドラインのopensslが出力する公開鍵は、SubjectPublicKeyInfo形式なので、この関数を使う。
rsa_pubkey = d2i_RSA_PUBKEY( NULL, &der_pub, pub_len );
if( rsa_pubkey == NULL ) {
ret_code = CR_GENID_ERROR_RSA_READ_PUBLIC_KEY;

View File

@ -336,7 +336,7 @@ int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8
// FuseIDバッファ全体をAES or RSAで暗号化
//--------------------------------------------------------------
DEBUG_PRINT_ARRAY( "ORG buf:", (const char *)id_buf, CR_ID_BUF_SIZE );
ret_code = EncryptID( id_buf, id_buf );
ret_code = EncryptID( id_buf, id_buf, bondingOption );
if( ret_code != CR_GENID_SUCCESS ) {
goto end;
}

View File

@ -128,6 +128,7 @@ extern "C" {
#include <openssl/sha.h>
#include <openssl/ec.h>
#include "ec_lcl.h" // EC_KEY (=ec_key_st) 構造体の参照に必要
#define DEBUG_PRINT 1
@ -137,6 +138,7 @@ extern "C" {
#define DEBUG_PRINT_ARRAY( ... ) ((void)0)
#endif // DEBUG_PRINT
#define ENCRYPT_AES 1 // 定義を有効でFIX.これが未定義の場合、IDの暗号化がRSAになる。
#define CR_GEN_ID_VERSION 1
#ifdef USE_HSM
@ -183,7 +185,7 @@ extern int GetTimestamp( u8 *pYear, u8 *pMonth, u8 *pMday, u8 *pHour, u8 *pMin,
extern int GenerateRandom( u8 *pDst, int length );
extern int GenarateECCKeyPair( EC_KEY *pECkey, u8 *pECPrivkey );
extern int GenerateCTRDeviceCert( EC_POINT *pubKey, u32 deviceId, u8 bondingOption );
extern int EncryptID( unsigned char *dst_buf, unsigned char *org_buf );
extern int EncryptID( unsigned char *dst_buf, unsigned char *org_buf, u8 bondingOption );
extern void DebugPrintArray( char *pStr, const u8 *pData, int length );
#ifdef __cplusplus

View File

@ -118,7 +118,6 @@
#include <string.h>
#include <openssl/err.h>
#include <openssl/ec.h>
#include "ec_lcl.h" // ec_key_st構造体の参照に必要
#include "cr_generate_id.h"
#include "cr_generate_id_private.h"

View File

@ -0,0 +1 @@
űćŁúp)v$ ëůFݸě

View File

@ -0,0 +1 @@
eMHë{ôéÜ<C3A9>:°˙<C2B0>D

View File

@ -0,0 +1 @@
<EFBFBD>拀惯耄}Wp~n煯<

View File

@ -0,0 +1 @@
<EFBFBD><EFBFBD><EFBFBD><1D><>L<EFBFBD>C<EFBFBD>k4m<34><6D><EFBFBD>

Binary file not shown.

Binary file not shown.

3
main.c
View File

@ -192,8 +192,7 @@ int main(int ac, char *argv[])
#endif
#ifdef USE_DUMMY_KEY
// fprintf(stderr,"RSA private key is dummy!\n");
// return -1; /* error */
printf( "[TEST MODE] Use dummy key.\n");
#endif
time(&tloc);

View File

@ -13,16 +13,16 @@ my $dstfname_h;
my $arrayname;
my $size;
if( !$ARGV[0] ) {
if( !$ARGV[0] || !$ARGV[1] ) {
die "parameter error.\n";
}
$_ = "cr_" . basename($srcfname, "");
s/(\..*)/\.c/;
s/(\..*)/_$ARGV[1]\.c/;
$dstfname_c = $_;
s/(\..*)/\.h/;
$dstfname_h = $_;
s/(\..*)/_DER/;
#s/(\..*)/\.h/;
#$dstfname_h = $_;
s/(\..*)//;
$arrayname = $_;
$size = -s $srcfname;
@ -33,18 +33,18 @@ open SRC, "< $srcfname"
open DST, "> $dstfname_c"
or die "Cannot open file temp : $!";
open DST2, "> $dstfname_h"
or die "Cannot open file temp : $!";
#open DST2, "> $dstfname_h"
# or die "Cannot open file temp : $!";
binmode( SRC );
# 配列サイズを + magic_code(8) + keyLen(2) + padding(6) し、16bytesでROUNDUP
# $sizeが16byte超(AES鍵でないなら、配列サイズを + magic_code(8) + keyLen(2) + padding(6) し、16bytesでROUNDUP
my $array_size = ( $size + 16 + 15) & 0xfffffff0;
# .c ファイルに変換して出力
printf DST "#include <stdio.h>\n\n";
printf DST "\/\/下記配列は、DERフォーマットのKEYデータの前に magic_code[8] + keyLen[2] + keyVer[1] + padding[5] のデータが挿入されています。\n\n";
printf DST "\/\/下記配列は、KEYデータの前に magic_code[8] + keyLen[2] + keyVer[1] + padding[5] のデータが挿入されています。\n\n";
printf DST "const unsigned char %s[ 0x%x ] = {\n\t", $arrayname, $array_size;
# magic code
@ -89,6 +89,6 @@ close SRC;
close DST;
# .h ファイルも出力
printf DST2 "extern const unsigned char %s[ 0x%x ];\n", $arrayname, $array_size;
close DST2;
#printf DST2 "extern const unsigned char %s[ 0x%x ];\n", $arrayname, $array_size;
#close DST2;