ctr_eFuse/cr_enc_id.c
(no author) d323c591cd ソースコードの分割。
git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_eFuse@16 ff987cc8-cf2f-4642-8568-d52cce064691
2009-12-22 04:24:31 +00:00

470 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 "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>
#ifndef USE_HSM
// openssl
#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
static unsigned char local_buf_1[CR_ID_BUF_SIZE];
static unsigned char local_buf_2[CR_ID_BUF_SIZE];
// ビルドスイッチに応じて、IDバッファをAES/RSAで暗号化
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暗号化 -> 復号化 -> ベリファイ
#ifdef USE_HSM
int crypto_aes_enc_dec( unsigned char *dst_buf, unsigned char *org_buf )
{
int i, ret_code;
M_Command cmd;
M_Reply reply;
M_IV enc_iv, dec_iv;
ret_code = 0;
memset( &cmd, 0, sizeof( cmd ) );
memset( &reply, 0, sizeof( reply ) );
// iv
enc_iv.mech = dec_iv.mech = Mech_RijndaelmCBCpNONE;
for ( i = 0; i < 16; i++ )
enc_iv.iv.generic128.iv.bytes[i] = dec_iv.iv.generic128.iv.bytes[i] = i;
// encrypt
cmd.cmd = Cmd_Encrypt;
cmd.args.encrypt.key = hsmAeskeyid;
cmd.args.encrypt.mech = Mech_RijndaelmCBCpNONE;
cmd.args.encrypt.plain.type = PlainTextType_Bytes;
cmd.args.encrypt.plain.data.bytes.data.len = CR_ID_BUF_SIZE;
cmd.args.encrypt.plain.data.bytes.data.ptr = org_buf;
cmd.args.encrypt.flags = Cmd_Encrypt_Args_flags_given_iv_present;
cmd.args.encrypt.given_iv = &enc_iv;
ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL );
if ( ret_code != Status_OK )
{
printf( "error(%d) : Encryption by HSM\n", ret_code );
}
ret_code = reply.status;
if ( ret_code != Status_OK )
{
printf( "error(%d) : Encrypt Reply Status by HSM\n", ret_code );
}
memcpy( local_buf_1,
reply.reply.encrypt.cipher.data.generic128.cipher.ptr,
CR_ID_BUF_SIZE );
//NFastApp_Free_Command( hsmHandle, NULL, NULL, &cmd );
NFastApp_Free_Reply( hsmHandle, NULL, NULL, &reply );
memset( &cmd, 0, sizeof( cmd ) );
memset( &reply, 0, sizeof( reply ) );
// decyrpt
cmd.cmd = Cmd_Decrypt;
cmd.args.decrypt.flags = 0;
cmd.args.decrypt.key = hsmAeskeyid;
cmd.args.decrypt.mech = Mech_RijndaelmCBCpNONE;
cmd.args.decrypt.cipher.mech = Mech_RijndaelmCBCpNONE;
cmd.args.decrypt.cipher.data.generic128.cipher.len = CR_ID_BUF_SIZE;
cmd.args.decrypt.cipher.data.generic128.cipher.ptr = local_buf_1;
cmd.args.decrypt.cipher.iv = dec_iv.iv;
cmd.args.decrypt.reply_type = PlainTextType_Bytes;
ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL );
if ( ret_code != Status_OK )
{
printf( "error(%d) : Decryption by HSM\n", ret_code );
}
ret_code = reply.status;
if ( ret_code != Status_OK )
{
printf( "error(%d) : Decrypt Reply Status by HSM\n", ret_code );
}
memcpy( local_buf_2,
reply.reply.decrypt.plain.data.bytes.data.ptr,
CR_ID_BUF_SIZE );
//NFastApp_Free_Command( hsmHandle, NULL, NULL, &cmd );
NFastApp_Free_Reply( hsmHandle, NULL, NULL, &reply );
// ベリファイ
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 0;
} // 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] )
{
printf( "error : aes verify\n" );
return 0;
}
}
// バッファコピー
memcpy( dst_buf, local_buf_1, CR_ID_BUF_SIZE );
return 0;
} // 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 )
{
int i, ret_code;
M_Command cmd;
M_Reply reply;
M_IV enc_iv, dec_iv;
ret_code = 0;
memset( &cmd, 0, sizeof( cmd ) );
memset( &reply, 0, sizeof( reply ) );
// iv
enc_iv.mech = dec_iv.mech = Mech_RSApPKCS1;
for ( i = 0; i < sizeof( enc_iv.iv.generic256.iv.bytes ); i++ )
enc_iv.iv.generic256.iv.bytes[i] = dec_iv.iv.generic256.iv.bytes[i] = i;
// encrypt
cmd.cmd = Cmd_Encrypt;
cmd.args.encrypt.flags = Cmd_Encrypt_Args_flags_given_iv_present;
cmd.args.encrypt.key = hsmRsaPubkeyid;
cmd.args.encrypt.mech = Mech_RSApPKCS1;
cmd.args.encrypt.plain.type = PlainTextType_Bytes;
cmd.args.encrypt.plain.data.bytes.data.len = CR_ID_BUF_SIZE;
cmd.args.encrypt.plain.data.bytes.data.ptr = org_buf;
cmd.args.encrypt.given_iv = &enc_iv;
ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL );
if ( ret_code != Status_OK )
{
printf( "error(%d) : Encryption by HSM\n", ret_code );
}
ret_code = reply.status;
if ( ret_code != Status_OK )
{
printf( "error(%d) : Encrypt Reply Status by HSM\n", ret_code );
}
memcpy( local_buf_1,
reply.reply.encrypt.cipher.data.generic128.cipher.ptr,
CR_ID_BUF_SIZE );
//NFastApp_Free_Command( hsmHandle, NULL, NULL, &cmd );
NFastApp_Free_Reply( hsmHandle, NULL, NULL, &reply );
memset( &cmd, 0, sizeof( cmd ) );
memset( &reply, 0, sizeof( reply ) );
// decyrpt
cmd.cmd = Cmd_Decrypt;
cmd.args.decrypt.flags = 0;
cmd.args.decrypt.key = hsmAeskeyid;
cmd.args.decrypt.mech = Mech_RijndaelmCBCpNONE;
cmd.args.decrypt.cipher.mech = Mech_RijndaelmCBCpNONE;
cmd.args.decrypt.cipher.data.generic128.cipher.len = CR_ID_BUF_SIZE;
cmd.args.decrypt.cipher.data.generic128.cipher.ptr = local_buf_1;
cmd.args.decrypt.cipher.iv = dec_iv.iv;
cmd.args.decrypt.reply_type = PlainTextType_Bytes;
ret_code = NFastApp_Transact( hsmConnection, NULL, &cmd, &reply, NULL );
if ( ret_code != Status_OK )
{
printf( "error(%d) : Decryption by HSM\n", ret_code );
}
ret_code = reply.status;
if ( ret_code != Status_OK )
{
printf( "error(%d) : Decrypt Reply Status by HSM\n", ret_code );
}
memcpy( local_buf_2,
reply.reply.decrypt.plain.data.bytes.data.ptr,
CR_ID_BUF_SIZE );
//NFastApp_Free_Command( hsmHandle, NULL, NULL, &cmd );
NFastApp_Free_Reply( hsmHandle, NULL, NULL, &reply );
// ベリファイ
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 0;
} // 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フォーマットの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が出力する秘密鍵は、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が出力する公開鍵は、SubjectPublicKeyInfo形式なので、この関数を使う。
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