mirror of
https://github.com/rvtr/ctr_eFuse.git
synced 2025-11-02 00:11:04 -04:00
CR_ID_BUFFER構造体のdeviceCertExpiryDateをexpiryDateに変更して、位置を調整。また、deviceCertSignのサイズを0x3Cに戻す。 serialNo.プリントデバッグ部分の修正。 git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_eFuse@30 ff987cc8-cf2f-4642-8568-d52cce064691
373 lines
12 KiB
C
373 lines
12 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 "cr_generate_id.h"
|
||
|
||
#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"
|
||
#else // !USE_HSM
|
||
#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"
|
||
|
||
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 );
|
||
#else // !ENCRYPT_AES
|
||
static int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf );
|
||
#endif // ENCRYPT_AES
|
||
|
||
|
||
static unsigned char local_buf_1[CR_ID_BUF_SIZE];
|
||
static unsigned char local_buf_2[CR_ID_BUF_SIZE];
|
||
|
||
// ビルドスイチE<E38381><45>に応じて、IDバッファをAES/RSAで暗号<E69A97><E58FB7>
|
||
int EncryptID( unsigned char *dst_buf, unsigned char *org_buf )
|
||
{
|
||
#ifdef ENCRYPT_AES
|
||
return crypto_aes_enc_dec( dst_buf, org_buf ); // AES
|
||
#else // !ENCRYPT_AES
|
||
return crypto_rsa_enc_dec( dst_buf, org_buf ); // RSA pubKey enc
|
||
#endif // ENCRYPT_AES
|
||
}
|
||
|
||
#ifdef ENCRYPT_AES
|
||
// AES暗号匁E-> 復号匁E-> ベリファイ
|
||
|
||
#ifdef USE_HSM
|
||
|
||
int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf )
|
||
{
|
||
int i;
|
||
int ret_code = 0;
|
||
|
||
// encrypt
|
||
ret_code = hsm_aes_encrypt( local_buf_1, org_buf, CR_ID_BUF_SIZE );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
printf( "error(%d) : hsm_aes_encyrpt\n", ret_code );
|
||
return ret_code;
|
||
}
|
||
|
||
// decyrpt
|
||
ret_code = hsm_aes_decrypt( local_buf_2, local_buf_1, CR_ID_BUF_SIZE );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
printf( "error(%d) : hsm_aes_decrypt\n", ret_code );
|
||
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エラーコードを作らなくては・・・
|
||
}
|
||
}
|
||
|
||
// バッファコピ<E382B3><E38394>E
|
||
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 暗号化用鍵 作<><E4BD9C>E
|
||
if ( AES_set_encrypt_key( AES_PASS_PHRASE, 128, &aesEncKey ) != 0 )
|
||
{
|
||
printf( "error : AES_set_encrypt_key\n" );
|
||
return 0;
|
||
}
|
||
|
||
// AES 復号化用鍵 作<><E4BD9C>E
|
||
if ( AES_set_decrypt_key( AES_PASS_PHRASE, 128, &aesDecKey ) != 0 )
|
||
{
|
||
printf( "error : AES_set_decrypt_key\n" );
|
||
return 0;
|
||
}
|
||
|
||
// iv 初期<E5889D><E69C9F>
|
||
for ( i = 0; i < 16; i++ )
|
||
{
|
||
temp_iv[i] = i;
|
||
}
|
||
|
||
// AES 暗号<E69A97><E58FB7>
|
||
AES_cbc_encrypt ( org_buf, local_buf_1, CR_ID_BUF_SIZE, &aesEncKey, temp_iv, AES_ENCRYPT );
|
||
|
||
// iv 初期<E5889D><E69C9F>
|
||
for ( i = 0; i < 16; i++ )
|
||
{
|
||
temp_iv[i] = i;
|
||
}
|
||
|
||
// AES 復号<E5BEA9><E58FB7>
|
||
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;
|
||
}
|
||
}
|
||
|
||
// バッファコピ<E382B3><E38394>E
|
||
memcpy( dst_buf, local_buf_1, CR_ID_BUF_SIZE );
|
||
|
||
return CR_GENID_SUCCESS;
|
||
} // crypto_aes_enc_dec
|
||
|
||
#endif // USE_HSM
|
||
|
||
#else // !ENCRYPT_AES
|
||
|
||
// RSA暗号匁E>復号匁E>ベリファイ
|
||
#ifdef USE_HSM
|
||
|
||
int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf )
|
||
{
|
||
int i;
|
||
int ret_code = 0;
|
||
|
||
// encrypt
|
||
ret_code = hsm_rsa_encrypt( local_buf_1, org_buf, CR_ID_BUF_SIZE );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
printf( "error(%d) : hsm_rsa_encyrpt\n", ret_code );
|
||
return ret_code;
|
||
}
|
||
|
||
// decyrpt
|
||
ret_code = hsm_rsa_decrypt( local_buf_2, local_buf_1, CR_ID_BUF_SIZE );
|
||
if ( ret_code != CR_GENID_SUCCESS )
|
||
{
|
||
printf( "error(%d) : hsm_rsa_decrypt\n", ret_code );
|
||
return ret_code;
|
||
}
|
||
|
||
// ベリファイ
|
||
for ( i = 0 ; i < CR_ID_BUF_SIZE ; i++ )
|
||
{
|
||
if( org_buf[i] != local_buf_2[i] )
|
||
{
|
||
return CR_GENID_ERROR_AES_VERIFY;
|
||
}
|
||
}
|
||
|
||
// バッファコピ<E382B3><E38394>E
|
||
memcpy( dst_buf, local_buf_1, CR_ID_BUF_SIZE );
|
||
|
||
return CR_GENID_SUCCESS;
|
||
} // hsm_crypto_rsa_enc_dec
|
||
|
||
#else // USE_HSM
|
||
|
||
int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf)
|
||
{
|
||
int ret_code = 0;
|
||
int rsa_outlen = 0;
|
||
RSA *rsa_privkey = NULL;
|
||
RSA *rsa_pubkey = NULL;
|
||
|
||
memset(local_buf_1, 0,CR_ID_BUF_SIZE);
|
||
memset(local_buf_2, 0,CR_ID_BUF_SIZE);
|
||
|
||
|
||
// DERフォーマット<E38383><E38388>ERSA鍵を読み込み
|
||
{
|
||
const unsigned char *der_priv = cr_gen_id_rsa_key_priv_DER + 0x10; // ヘッダ部刁E<E58881><45>除外してKEY実体を持E<E68C81>
|
||
const unsigned char *der_pub = cr_gen_id_rsa_key_pub_DER + 0x10; // 同<><E5908C>
|
||
int priv_len = cr_gen_id_rsa_key_priv_DER[ 8 ] | cr_gen_id_rsa_key_priv_DER[ 9 ] << 8; // KEY長を取り<E58F96><E3828A>E<EFBFBD><45>
|
||
int pub_len = cr_gen_id_rsa_key_pub_DER [ 8 ] | cr_gen_id_rsa_key_pub_DER [ 9 ] << 8; // 同<><E5908C>
|
||
// コマンドラインのopensslが<6C><E3818C>E力する秘寁E<E5AF81><45>は、PKCS#1 RSAPublicKeyフォーマットなので、この関数を使ぁE<E38181>
|
||
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が<6C><E3818C>E力する<E38199><E3828B>E開鍵は、SubjectPublicKeyInfo形式なので、この関数を使ぁE<E38181>
|
||
rsa_pubkey = d2i_RSA_PUBKEY( NULL, &der_pub, pub_len );
|
||
if( rsa_pubkey == NULL ) {
|
||
ret_code = CR_GENID_ERROR_RSA_READ_PUBLIC_KEY;
|
||
goto end;
|
||
}
|
||
}
|
||
|
||
if( (rsa_outlen = RSA_private_encrypt(CR_ID_BUF_SIZE, org_buf, local_buf_1,
|
||
rsa_privkey, RSA_NO_PADDING)) == -1) {
|
||
ret_code = CR_GENID_ERROR_RSA_ENC;
|
||
goto end;
|
||
}
|
||
else {
|
||
if((rsa_outlen = RSA_public_decrypt(rsa_outlen, local_buf_1, local_buf_2,
|
||
rsa_pubkey, RSA_NO_PADDING)) == -1) {
|
||
ret_code = CR_GENID_ERROR_RSA_DEC;
|
||
goto end;
|
||
}
|
||
else {
|
||
int i;
|
||
int error_flag = 0;
|
||
for( i = 0 ; i < CR_ID_BUF_SIZE ; i++ ) {
|
||
if( org_buf[i] != local_buf_2[i] ) {
|
||
error_flag++;
|
||
}
|
||
}
|
||
if( error_flag ) {
|
||
ret_code = CR_GENID_ERROR_RSA_VERIFY;
|
||
goto end;
|
||
}
|
||
}
|
||
}
|
||
|
||
memcpy(dst_buf,local_buf_1,CR_ID_BUF_SIZE);
|
||
|
||
end:
|
||
if ( rsa_privkey ) RSA_free( rsa_privkey );
|
||
if ( rsa_pubkey ) RSA_free( rsa_pubkey );
|
||
|
||
return ret_code;
|
||
}
|
||
|
||
#endif // !USE_HSM
|
||
|
||
#endif // ENCRYPT_AES
|