ctr_eFuse/cr_enc_id.c
kubodera_yuichi 5813af8471 エラー情報を関数名と行数に変更し、指定したサイズ分コールスタックとして記録する方式にした。
git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_eFuse@72 ff987cc8-cf2f-4642-8568-d52cce064691
2009-12-23 10:31:14 +00:00

377 lines
13 KiB
C
Raw Blame History

/* ====================================================================
* 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 "cr_generate_id_private.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/rsa.h>
#include <openssl/aes.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, u8 bondingOption );
#else // !ENCRYPT_AES
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];
// <20>r<EFBFBD><72><EFBFBD>h<EFBFBD>X<EFBFBD>C<EFBFBD>b<EFBFBD>`<60>ɏ]<5D><><EFBFBD><EFBFBD>AES or RSA <20>Ŏw<C58E><77><EFBFBD>o<EFBFBD>b<EFBFBD>t<EFBFBD>@<40><><EFBFBD>Í<EFBFBD><C38D><EFBFBD>
int EncryptID( unsigned char *dst_buf, unsigned char *org_buf, u8 bondingOption )
{
#ifdef ENCRYPT_AES
if( cr_print_flag ) printf( "[AES]\n");
return crypto_aes_enc_dec( dst_buf, org_buf, bondingOption ); // AES
#else // !ENCRYPT_AES
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
#ifdef USE_HSM
int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf, u8 bondingOption )
{
int i;
int ret_code = CR_GENID_SUCCESS;
// encrypt
ret_code = hsm_aes_encrypt( local_buf_1, org_buf, CR_ID_BUF_SIZE, bondingOption );
if ( ret_code != CR_GENID_SUCCESS )
{
SetErrorInfo( __FUNCTION__, __LINE__ );
return ret_code;
}
// decyrpt
ret_code = hsm_aes_decrypt( local_buf_2, local_buf_1, CR_ID_BUF_SIZE, bondingOption );
if ( ret_code != CR_GENID_SUCCESS )
{
SetErrorInfo( __FUNCTION__, __LINE__ );
return ret_code;
}
// <20>x<EFBFBD><78><EFBFBD>t<EFBFBD>@<40>C
for ( i = 0 ; i < CR_ID_BUF_SIZE ; i++ )
{
if( org_buf[i] != local_buf_2[i] )
{
ret_code = CR_GENID_ERROR_AES_VERIFY;
SetErrorInfo( __FUNCTION__, __LINE__ );
return ret_code;
}
}
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];
// <20><><EFBFBD>f<EFBFBD>[<5B>^<5E><><EFBFBD><EFBFBD><EFBFBD>o<EFBFBD><6F><EFBFBD>B<EFBFBD>i<EFBFBD>w<EFBFBD>b<EFBFBD>_<EFBFBD><5F><EFBFBD><EFBFBD>0x10<31><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>B<EFBFBD>j
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 )
{
SetErrorInfo( __FUNCTION__, __LINE__ );
return CR_GENID_ERROR_AES_ENC;
}
if ( AES_set_decrypt_key( pAesKey, 128, &aesDecKey ) != 0 )
{
SetErrorInfo( __FUNCTION__, __LINE__ );
return CR_GENID_ERROR_AES_DEC;
}
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 );
// <20>x<EFBFBD><78><EFBFBD>t<EFBFBD>@<40>C
for ( i = 0 ; i < CR_ID_BUF_SIZE ; i++ )
{
if( org_buf[i] != local_buf_2[i] )
{
SetErrorInfo( __FUNCTION__, __LINE__ );
return CR_GENID_ERROR_AES_VERIFY;
}
}
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
#ifdef USE_HSM
int crypto_rsa_enc_dec( unsigned char *dst_buf,unsigned char *org_buf, u8 bondingOption )
{
int i;
int ret_code = CR_GENID_SUCCESS;
// encrypt
ret_code = hsm_rsa_encrypt( local_buf_1, org_buf, CR_ID_BUF_SIZE, bondingOption );
if ( ret_code != CR_GENID_SUCCESS )
{
SetErrorInfo( __FUNCTION__, __LINE__ );
return ret_code;
}
// decyrpt
ret_code = hsm_rsa_decrypt( local_buf_2, local_buf_1, CR_ID_BUF_SIZE, bondingOption );
if ( ret_code != CR_GENID_SUCCESS )
{
SetErrorInfo( __FUNCTION__, __LINE__ );
return ret_code;
}
// <20>x<EFBFBD><78><EFBFBD>t<EFBFBD>@<40>C
for ( i = 0 ; i < CR_ID_BUF_SIZE ; i++ )
{
if( org_buf[i] != local_buf_2[i] )
{
ret_code = CR_GENID_ERROR_RSA_VERIFY;
SetErrorInfo( __FUNCTION__, __LINE__ );
return ret_code;
}
}
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, u8 bondingOption )
{
int ret_code = CR_GENID_SUCCESS;
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<45>t<EFBFBD>H<EFBFBD>[<5B>}<7D>b<EFBFBD>g<EFBFBD><67>RSA<53><41><EFBFBD><EFBFBD><EFBFBD>ǂݍ<C782><DD8D><EFBFBD>
{
// bondingOption<6F>ɂ<EFBFBD><C982><EFBFBD><EFBFBD>āA<C481><41><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ւ<EFBFBD>
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<45><59><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>o<EFBFBD><6F>
int pub_len = der_pub [ 8 ] | der_pub [ 9 ] << 8; // <20><><EFBFBD><EFBFBD>
der_priv += 0x10; // <20>w<EFBFBD>b<EFBFBD>_<EFBFBD><5F><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>O<EFBFBD><4F><EFBFBD><EFBFBD>KEY<45><59><EFBFBD>̂<EFBFBD><CC82>w<EFBFBD><77>
der_pub += 0x10; // <20><><EFBFBD><EFBFBD>
// <20>R<EFBFBD>}<7D><><EFBFBD>h<EFBFBD><68><EFBFBD>C<EFBFBD><43><EFBFBD><EFBFBD>openssl<73><6C><EFBFBD>o<EFBFBD>͂<EFBFBD><CD82><EFBFBD><EFBFBD><EFBFBD><E996A7><EFBFBD>́APKCS#1 RSAPublicKey<65>t<EFBFBD>H<EFBFBD>[<5B>}<7D>b<EFBFBD>g<EFBFBD>Ȃ̂ŁA<C581><41><EFBFBD>̊֐<CC8A><D690><EFBFBD><EFBFBD>g<EFBFBD><67><EFBFBD>B
rsa_privkey = d2i_RSAPrivateKey( NULL, &der_priv, priv_len );
if( rsa_privkey == NULL ) {
ret_code = CR_GENID_ERROR_RSA_READ_PRIVATE_KEY;
SetErrorInfo( __FUNCTION__, __LINE__ );
goto end;
}
// <20>R<EFBFBD>}<7D><><EFBFBD>h<EFBFBD><68><EFBFBD>C<EFBFBD><43><EFBFBD><EFBFBD>openssl<73><6C><EFBFBD>o<EFBFBD>͂<EFBFBD><CD82><EFBFBD><EFBFBD><EFBFBD><EFBFBD>J<EFBFBD><4A><EFBFBD>́ASubjectPublicKeyInfo<66>`<60><><EFBFBD>Ȃ̂ŁA<C581><41><EFBFBD>̊֐<CC8A><D690><EFBFBD><EFBFBD>g<EFBFBD><67><EFBFBD>B
rsa_pubkey = d2i_RSA_PUBKEY( NULL, &der_pub, pub_len );
if( rsa_pubkey == NULL ) {
ret_code = CR_GENID_ERROR_RSA_READ_PUBLIC_KEY;
SetErrorInfo( __FUNCTION__, __LINE__ );
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;
SetErrorInfo( __FUNCTION__, __LINE__ );
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;
SetErrorInfo( __FUNCTION__, __LINE__ );
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;
SetErrorInfo( __FUNCTION__, __LINE__ );
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