mirror of
https://github.com/rvtr/ctr_eFuse.git
synced 2025-11-02 00:11:04 -04:00
git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_eFuse@20 ff987cc8-cf2f-4642-8568-d52cce064691
459 lines
16 KiB
C
459 lines
16 KiB
C
/* ====================================================================
|
||
* Copyright (c) 1998-2008 The OpenSSL Project. All rights reserved.
|
||
*
|
||
* Redistribution and use in source and binary forms, with or without
|
||
* modification, are permitted provided that the following conditions
|
||
* are met:
|
||
*
|
||
* 1. Redistributions of source code must retain the above copyright
|
||
* notice, this list of conditions and the following disclaimer.
|
||
*
|
||
* 2. Redistributions in binary form must reproduce the above copyright
|
||
* notice, this list of conditions and the following disclaimer in
|
||
* the documentation and/or other materials provided with the
|
||
* distribution.
|
||
*
|
||
* 3. All advertising materials mentioning features or use of this
|
||
* software must display the following acknowledgment:
|
||
* "This product includes software developed by the OpenSSL Project
|
||
* for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
|
||
*
|
||
* 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
|
||
* endorse or promote products derived from this software without
|
||
* prior written permission. For written permission, please contact
|
||
* openssl-core@openssl.org.
|
||
*
|
||
* 5. Products derived from this software may not be called "OpenSSL"
|
||
* nor may "OpenSSL" appear in their names without prior written
|
||
* permission of the OpenSSL Project.
|
||
*
|
||
* 6. Redistributions of any form whatsoever must retain the following
|
||
* acknowledgment:
|
||
* "This product includes software developed by the OpenSSL Project
|
||
* for use in the OpenSSL Toolkit (http://www.openssl.org/)"
|
||
*
|
||
* THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
|
||
* EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
|
||
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
||
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
||
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
* ====================================================================
|
||
*
|
||
* This product includes cryptographic software written by Eric Young
|
||
* (eay@cryptsoft.com). This product includes software written by Tim
|
||
* Hudson (tjh@cryptsoft.com).
|
||
*
|
||
*/
|
||
|
||
/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
|
||
* All rights reserved.
|
||
*
|
||
* This package is an SSL implementation written
|
||
* by Eric Young (eay@cryptsoft.com).
|
||
* The implementation was written so as to conform with Netscapes SSL.
|
||
*
|
||
* This library is free for commercial and non-commercial use as long as
|
||
* the following conditions are aheared to. The following conditions
|
||
* apply to all code found in this distribution, be it the RC4, RSA,
|
||
* lhash, DES, etc., code; not just the SSL code. The SSL documentation
|
||
* included with this distribution is covered by the same copyright terms
|
||
* except that the holder is Tim Hudson (tjh@cryptsoft.com).
|
||
*
|
||
* Copyright remains Eric Young's, and as such any Copyright notices in
|
||
* the code are not to be removed.
|
||
* If this package is used in a product, Eric Young should be given attribution
|
||
* as the author of the parts of the library used.
|
||
* This can be in the form of a textual message at program startup or
|
||
* in documentation (online or textual) provided with the package.
|
||
*
|
||
* Redistribution and use in source and binary forms, with or without
|
||
* modification, are permitted provided that the following conditions
|
||
* are met:
|
||
* 1. Redistributions of source code must retain the copyright
|
||
* notice, this list of conditions and the following disclaimer.
|
||
* 2. Redistributions in binary form must reproduce the above copyright
|
||
* notice, this list of conditions and the following disclaimer in the
|
||
* documentation and/or other materials provided with the distribution.
|
||
* 3. All advertising materials mentioning features or use of this software
|
||
* must display the following acknowledgement:
|
||
* "This product includes cryptographic software written by
|
||
* Eric Young (eay@cryptsoft.com)"
|
||
* The word 'cryptographic' can be left out if the rouines from the library
|
||
* being used are not cryptographic related :-).
|
||
* 4. If you include any Windows specific code (or a derivative thereof) from
|
||
* the apps directory (application code) you must include an acknowledgement:
|
||
* "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
|
||
*
|
||
* THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
|
||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||
* SUCH DAMAGE.
|
||
*
|
||
* The licence and distribution terms for any publically available version or
|
||
* derivative of this code cannot be changed. i.e. this code cannot simply be
|
||
* copied and put under another distribution licence
|
||
* [including the GNU Public Licence.]
|
||
*/
|
||
|
||
#include <assert.h>
|
||
#include <stdio.h>
|
||
#include <stdlib.h>
|
||
#include <limits.h>
|
||
#include <time.h>
|
||
#include <sys/time.h>
|
||
#include <string.h>
|
||
|
||
#ifdef USE_HSM
|
||
#include "cr_hsm_code.h"
|
||
#endif // USE_HSM
|
||
// openssl
|
||
#include <openssl/err.h>
|
||
#include <openssl/sha.h>
|
||
#include <openssl/ec.h>
|
||
#include <openssl/rsa.h>
|
||
#include <openssl/aes.h>
|
||
#include <openssl/x509.h>
|
||
|
||
#include "cr_generate_id.h"
|
||
#include "cr_generate_id_private.h"
|
||
#include "cr_alloc.h"
|
||
|
||
typedef struct {
|
||
u32 magic_number; /* 0x00 - 0x03 0x01234567 確定!*/
|
||
u32 serial0; /* 0x04 - 0x07 */
|
||
u32 openssl_err_code; /* 0x08 - 0x0b */
|
||
s32 top; /* 0x0c - 0x0f */
|
||
s32 bottom; /* 0x10 - 0x13 */
|
||
|
||
/* #define ERR_NUM_ERRORS 16 */
|
||
u32 err_buffer[ERR_NUM_ERRORS]; /* 0x14 - 0x17 .... 0x50 - 0x53 */
|
||
//#define ERR_GET_LIB(l) (int)((((unsigned long)l)>>24L)&0xffL)
|
||
//#define ERR_GET_FUNC(l) (int)((((unsigned long)l)>>12L)&0xfffL)
|
||
//#define ERR_GET_REASON(l) (int)((l)&0xfffL)
|
||
//#define ERR_FATAL_ERROR(l) (int)((l)&ERR_R_FATAL)
|
||
s32 err_line[ERR_NUM_ERRORS]; /* 0x54 - 0x57 .... 0x90 - 0x93 */
|
||
} CR_ERR_BUFFER;
|
||
|
||
|
||
int cr_print_flag = 0;
|
||
|
||
|
||
int cr_generate_id_initialize( void )
|
||
{
|
||
int ret_code = 0;
|
||
|
||
#ifdef USE_HSM
|
||
ret_code = hsm_initialize();
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
printf( "error(%d) : NFastApp_InitEx\n", ret_code );
|
||
return ret_code;
|
||
}
|
||
#endif
|
||
|
||
ret_code = 1;
|
||
|
||
return ( ret_code );
|
||
} // cr_generate_id_initialize
|
||
|
||
// generate_id関数のファイナライズ
|
||
int cr_generate_id_finalize( void )
|
||
{
|
||
int ok = 0;
|
||
|
||
// openssl
|
||
ERR_remove_state(0);
|
||
EVP_cleanup();
|
||
CRYPTO_cleanup_all_ex_data();
|
||
|
||
// HSM
|
||
#ifdef USE_HSM
|
||
hsm_finalize();
|
||
#endif // USE_HSM
|
||
|
||
ok = 1;
|
||
|
||
return ( ok );
|
||
} // cr_generate_id_finalize
|
||
|
||
/*
|
||
unsigned long ERR_get_error(void);
|
||
エラーが起こったとき id_buf にエラーログを返すか?
|
||
*/
|
||
|
||
|
||
// generate_id 関数
|
||
int cr_generate_id( u32 serial[CR_NUM_OF_SERIAL], u8 id_buf[CR_ID_BUF_SIZE], u8 bondingOption )
|
||
{
|
||
int i;
|
||
int ret_code;
|
||
CR_ID_BUFFER *cr_id_buf;
|
||
EC_KEY *my_eckey = NULL;
|
||
|
||
if( sizeof(CR_ID_BUFFER) != 256 ) {
|
||
ret_code = CR_GENID_ERROR_ID_BUF_SIZE;
|
||
goto end;
|
||
}
|
||
|
||
if( sizeof(CR_ERR_BUFFER) > 256 ) {
|
||
ret_code = CR_GENID_ERROR_ERR_BUF_SIZE;
|
||
goto end;
|
||
}
|
||
|
||
ret_code = CR_GENID_ERROR_NON; /* CR_GENID_ERROR_NON = 0 */
|
||
|
||
#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
|
||
OpenSSL_add_all_digests();
|
||
#else
|
||
EVP_add_digest( EVP_ecdsa() );
|
||
#endif
|
||
|
||
//--------------------------------------------------------------
|
||
// 暗号処理初期化
|
||
//--------------------------------------------------------------
|
||
|
||
// メモリリーク防止のため、オリジナルのアロケータを使用。
|
||
CRYPTO_set_mem_functions(cr_mem_malloc, cr_mem_realloc, cr_mem_free);
|
||
cr_mem_bufmgr_initialize();
|
||
|
||
#ifdef MY_CRYPTO_DEBUG
|
||
ERR_load_crypto_strings();
|
||
#endif /* MY_CRYPTO_DEBUG */
|
||
|
||
//--------------------------------------------------------------
|
||
// FuseIDバッファに固定データセット
|
||
//--------------------------------------------------------------
|
||
memset(id_buf, 0, CR_ID_BUF_SIZE);
|
||
|
||
cr_id_buf = (CR_ID_BUFFER *)id_buf;
|
||
cr_id_buf->magic_number = 0xdeadb00f; /* 0xdeadbeefにするとRSAでコケる。 */
|
||
cr_id_buf->version = CR_GEN_ID_VERSION;
|
||
|
||
//--------------------------------------------------------------
|
||
// 引数のボンディングオプションをセット
|
||
//--------------------------------------------------------------
|
||
cr_id_buf->bondingOption = bondingOption;
|
||
|
||
//--------------------------------------------------------------
|
||
// serialNo.セット
|
||
//--------------------------------------------------------------
|
||
for( i = 0 ; i < CR_NUM_OF_SERIAL ; i++ ) {
|
||
cr_id_buf->serial[i] = serial[i]; /* serial[0] => ec priv key */
|
||
}
|
||
|
||
//#ifdef DEBUG_PRINT
|
||
#if 1
|
||
if( cr_print_flag ) {
|
||
printf("serialNo:\n");
|
||
for( i = 0 ; i < CR_NUM_OF_SERIAL ; i++ ) {
|
||
if( i == 0 ) printf(" 0x%08x\n", (unsigned int)serial[i] );
|
||
else if( i & 0x01 ) printf(" 0x%08x", (unsigned int)serial[i] );
|
||
else printf("%08x\n", (unsigned int)serial[i] );
|
||
}
|
||
printf("\n");
|
||
}
|
||
#endif /* DEBUG_PRINT */
|
||
|
||
//--------------------------------------------------------------
|
||
// タイムスタンプセット
|
||
//--------------------------------------------------------------
|
||
ret_code = GetTimestamp( &cr_id_buf->year,
|
||
&cr_id_buf->month,
|
||
&cr_id_buf->mday,
|
||
&cr_id_buf->hour,
|
||
&cr_id_buf->min,
|
||
&cr_id_buf->sec,
|
||
&cr_id_buf->deviceCertExpiryDate );
|
||
if ( ret_code != 0 ) {
|
||
goto end;
|
||
}
|
||
|
||
//--------------------------------------------------------------
|
||
// 乱数を生成してセット
|
||
//--------------------------------------------------------------
|
||
ret_code = GenerateRandom( cr_id_buf->random, CR_RANDOM_LENGTH );
|
||
if ( ret_code != CR_GENID_SUCCESS ) {
|
||
goto end;
|
||
}
|
||
DEBUG_PRINT_ARRAY( "rand:", (const char *)cr_id_buf->random, CR_RANDOM_LENGTH );
|
||
|
||
//--------------------------------------------------------------
|
||
// 楕円曲線鍵ペアを生成
|
||
//--------------------------------------------------------------
|
||
ret_code = GenarateECCKeyPair( my_eckey, cr_id_buf->devicePrivKey );
|
||
if ( ret_code != CR_GENID_SUCCESS ) {
|
||
goto end;
|
||
}
|
||
|
||
//--------------------------------------------------------------
|
||
// 生成した鍵ペアをECDSAで動作確認
|
||
//--------------------------------------------------------------
|
||
#if 0
|
||
ret_code = TestECDSA( hoge hoge );
|
||
// 当然やるべき
|
||
#endif
|
||
|
||
//--------------------------------------------------------------
|
||
// デバイス証明書生成&署名+証明書期限セット
|
||
//--------------------------------------------------------------
|
||
#if 0
|
||
int padding = ECDSA_SIGN_LENGTH % cert->signature->length;
|
||
printf( "padding = %d\n", padding );
|
||
for ( i = 0; i < padding; i++ )
|
||
cr_id_buf->deviceCertSign[i] = 0;
|
||
for ( i = 0; i < cert->signature->length; i++ )
|
||
cr_id_buf->deviceCertSign[ i + padding ] = cert->signature->data[i];
|
||
// mistake!!
|
||
|
||
// TORIAEZU : create Custom cert
|
||
memset( &ctr_dev_cert, 0, sizeof( ctr_dev_cert ) );
|
||
// copy ecpubkey to Custom cert
|
||
int len = BN_num_bytes( &my_eckey->pub_key->X );
|
||
u8 *buf = (u8*)malloc( len );
|
||
len = BN_bn2bin( &my_eckey->pub_key->X, buf );
|
||
memcpy( ctr_dev_cert.eccPubKey, buf, len );
|
||
free( buf );
|
||
len = BN_num_bytes( &my_eckey->pub_key->Y );
|
||
buf = (u8*)malloc( len );
|
||
len = BN_bn2bin( &my_eckey->pub_key->Y, buf );
|
||
memcpy( &ctr_dev_cert.eccPubKey[30], buf, len );
|
||
free( buf );
|
||
|
||
// copy sign to Custom cert
|
||
//#define SHOW_SIG
|
||
u8 *ptr = cert->signature->data;
|
||
#ifdef SHOW_SIG
|
||
printf( "signature\n" );
|
||
printf( "SEQUENCE : 0x%02X\n", *(ptr++) );
|
||
printf( "ALL_LEN : 0x%02X\n", *(ptr++) );
|
||
printf( "INTEGER : 0x%02X\n", *(ptr++) );
|
||
len = *(ptr++);
|
||
printf( "r LEN : 0x%02X\n", len );
|
||
printf( "r :" );
|
||
for ( i = 0; i < len; i++ )
|
||
{
|
||
if ( i % 8 == 0 ) printf( "\n" );
|
||
printf( "0x%02X ", *(ptr++) );
|
||
}
|
||
printf( "\n" );
|
||
printf( "INTEGER : 0x%02X\n", *(ptr++) );
|
||
len = *(ptr++);
|
||
printf( "s LEN : 0x%02X\n", len );
|
||
printf( "s :" );
|
||
for ( i = 0; i < len; i++ )
|
||
{
|
||
if ( i % 8 == 0 ) printf( "\n" );
|
||
printf( "0x%02X ", *(ptr++) );
|
||
}
|
||
printf( "\n" );
|
||
#else
|
||
ptr += 3;
|
||
int r_len = *(ptr++);
|
||
for ( i = 0; i < r_len; i++ )
|
||
ctr_dev_cert.eccSignature[i] = *(ptr++);
|
||
ptr++;
|
||
int s_len = *(ptr++);
|
||
for ( i = 0; i < s_len; i++ )
|
||
ctr_dev_cert.eccSignature[i+30] = *(ptr++);
|
||
|
||
// create CTR Custom cert
|
||
ret_code = generate_CTRCustom_cert( &ctr_dev_cert,
|
||
cr_id_buf->serial[0], cr_id_buf->bondingOption );
|
||
#endif
|
||
|
||
#if 0
|
||
DEBUG_PRINT_ARRAY( "deviceCertSign:", (const char *)cr_id_buf->deviceCertSign, ECDSA_SIGN_LENGTH );
|
||
#endif
|
||
|
||
#endif
|
||
|
||
//--------------------------------------------------------------
|
||
// FuseIDバッファ全体のSHA256ハッシュを算出してセット
|
||
//--------------------------------------------------------------
|
||
SHA256(id_buf, CR_ID_BUF_SIZE - SHA256_DIGEST_LENGTH, cr_id_buf->hash);
|
||
DEBUG_PRINT_ARRAY( "SHA256 Digest:", (const char *)cr_id_buf->hash, SHA256_DIGEST_LENGTH );
|
||
|
||
//--------------------------------------------------------------
|
||
// FuseIDバッファ全体をAES or RSAで暗号化
|
||
//--------------------------------------------------------------
|
||
DEBUG_PRINT_ARRAY( "ORG buf:", (const char *)id_buf, CR_ID_BUF_SIZE );
|
||
ret_code = EncryptID( id_buf, id_buf );
|
||
if( ret_code != CR_GENID_SUCCESS ) {
|
||
goto end;
|
||
}
|
||
DEBUG_PRINT_ARRAY( "encrypted:", (const char *)id_buf, CR_ID_BUF_SIZE );
|
||
|
||
//--------------------------------------------------------------
|
||
// 終了処理
|
||
//--------------------------------------------------------------
|
||
end:
|
||
/* id_buf[]にエラーログを書き込む。 */
|
||
if( ret_code != CR_GENID_SUCCESS ) {
|
||
ERR_STATE *es = NULL;
|
||
CR_ERR_BUFFER *cr_err_buf = (CR_ERR_BUFFER *)id_buf;
|
||
memset( cr_err_buf, 0, sizeof(CR_ERR_BUFFER) );
|
||
cr_err_buf->magic_number = 0x01234567;
|
||
cr_err_buf->serial0 = serial[0];
|
||
cr_err_buf->openssl_err_code = ERR_get_error();
|
||
es=ERR_get_state();
|
||
cr_err_buf->top = es->top;
|
||
cr_err_buf->bottom = es->bottom;
|
||
for( i = 0 ; i < ERR_NUM_ERRORS ; i++ ) {
|
||
cr_err_buf->err_buffer[i] = es->err_buffer[i];
|
||
cr_err_buf->err_line[i] = es->err_line[i];
|
||
}
|
||
}
|
||
|
||
// リソースの解放
|
||
if ( my_eckey ) EC_KEY_free( my_eckey );
|
||
|
||
ERR_remove_state(0);
|
||
EVP_cleanup();
|
||
CRYPTO_cleanup_all_ex_data();
|
||
|
||
#ifdef MY_CRYPTO_DEBUG
|
||
ERR_free_strings();
|
||
#endif /* MY_CRYPTO_DEBUG */
|
||
|
||
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
|