ctr_eFuse/cr_keyPair.c
(no author) 3a8205d8e4 レビュー結果のTODOリストを追加。
git-svn-id: file:///Volumes/Transfer/gigaleak_20231201/2020-09-30%20-%20paladin.7z/paladin/ctr_eFuse@45 ff987cc8-cf2f-4642-8568-d52cce064691
2009-12-23 03:11:23 +00:00

304 lines
11 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>
#include <openssl/err.h>
#include <openssl/ec.h>
#include <openssl/ecdsa.h>
#include "cr_generate_id.h"
#include "cr_generate_id_private.h"
static int generate_EC_private_key( EC_KEY *eckey, u8 *privKey );
static int generate_EC_public_key( EC_KEY *eckey );
// ECCキーペアの生成
int GenarateECCKeyPair( EC_KEY **ppECkey, u8 *pECPrivkey )
{
int openssl_result = 0;
// 楕円を選択 ( NID_X9_62_prime256v1 -> 32bytesまで、 NID_sect571r1 -> 71bytesまで 署名にデータを含められる )
*ppECkey = EC_KEY_new_by_curve_name( NID_sect233r1 );
if( *ppECkey == NULL )
{
return CR_GENID_ERROR_EC_KEY_NEW_1;
}
// 秘密鍵生成
openssl_result = generate_EC_private_key( *ppECkey, pECPrivkey );
if( openssl_result != 0 )
{
return CR_GENID_ERROR_EC_GENERATE_PRIVATE_KEY;
}
// 公開鍵生成
openssl_result = generate_EC_public_key( *ppECkey );
if ( openssl_result == 0 )
{
return CR_GENID_ERROR_EC_GENERATE_PUBLIC_KEY;
}
// ASN.1 形式指定フラグをセットする
// (これをセットしないと色々変なフィールドが入ってしまうため)
EC_KEY_set_asn1_flag( *ppECkey, 1 );
return CR_GENID_SUCCESS;
}
// EC秘密鍵を生成
static int generate_EC_private_key( EC_KEY *eckey, u8 *privKey )
{
int ret_code = CR_GENID_SUCCESS;
BIGNUM *bn_privkey = NULL;
// 乱数を取得して、秘密鍵にする。
ret_code = GenerateRandom( privKey, EC_PRIVATE_KEY_LENGTH );
if ( ret_code != CR_GENID_SUCCESS ) {
return ret_code;
}
// ECC233 で30バイトだけ利用するので、後ろ2バイトは0で埋める
// (DER(BER) が big endian なので、ここでは先頭2byte)
// 最後に3バイト目の7ビットをクリアする
privKey[ 0 ] = 0;
privKey[ 1 ] = 0;
privKey[ 2 ] &= 0x01;
// 生成した秘密鍵をBNに変換して、eckeyにセット
// ※bn_privkeyは、生成に成功した場合、ここではBN_freeされずにeckey要素の一つになって引き渡されます。
bn_privkey = BN_new();
if( bn_privkey == NULL )
{
return CR_GENID_ERROR_BN_NEW_0;
}
BN_init( bn_privkey ); /* memset(a,0,sizeof(BIGNUM)); */
(void)BN_bin2bn( privKey, EC_PRIVATE_KEY_LENGTH, bn_privkey );
eckey->priv_key = bn_privkey;
DEBUG_PRINT_ARRAY( "ec private key:", (const char *)privKey, EC_PRIVATE_KEY_LENGTH );
return ret_code;
} // generate_EC_private_key
// EC公開鍵を生成 ※opensslコードから抜粋し、一部改変
static int generate_EC_public_key( EC_KEY *eckey )
{
int ok = 0;
BN_CTX *ctx = NULL;
BIGNUM *priv_key = NULL, *order = NULL;
EC_POINT *pub_key = NULL;
if (!eckey || !eckey->group)
{
ECerr(EC_F_EC_KEY_GENERATE_KEY, ERR_R_PASSED_NULL_PARAMETER);
return 0;
}
if ((order = BN_new()) == NULL) goto err;
if ((ctx = BN_CTX_new()) == NULL) goto err;
if (eckey->priv_key == NULL)
{
priv_key = BN_new();
if (priv_key == NULL) {
goto err;
}
}
else {
priv_key = eckey->priv_key;
}
if (!EC_GROUP_get_order(eckey->group, order, ctx)) {
goto err;
}
#if 0 // 2009.09.25 これが実行されると、秘密鍵が乱数化されてしまい、指定した秘密鍵と変わってしまうためコメントアウトする。
do
if (!BN_rand_range(priv_key, order))
goto err;
while (BN_is_zero(priv_key));
#endif
if (eckey->pub_key == NULL)
{
pub_key = EC_POINT_new(eckey->group);
if (pub_key == NULL) {
goto err;
}
}
else
pub_key = eckey->pub_key;
if (!EC_POINT_mul(eckey->group, pub_key, priv_key, NULL, NULL, ctx)) {
goto err;
}
eckey->priv_key = priv_key;
eckey->pub_key = pub_key;
ok=1;
err:
if (order)
BN_free(order);
if (pub_key != NULL && eckey->pub_key == NULL)
EC_POINT_free(pub_key);
if (priv_key != NULL && eckey->priv_key == NULL)
BN_free(priv_key);
if (ctx != NULL)
BN_CTX_free(ctx);
return(ok);
} // generate_EC_public_key
// 鍵ペアをECDSAで検証
int TestECDSA( EC_KEY *pECkey )
{
#define CR_ECDSA_BUF_SIZE 29
#define CR_ECDSA_SIGN_BUF_SIZE 256
unsigned char ecdsa_test_buf[CR_ECDSA_BUF_SIZE];
unsigned char ecdsasig[CR_ECDSA_SIGN_BUF_SIZE];
unsigned int ecdsasiglen = 0;
int openssl_result = 0;
int i;
// ダミー署名データ作成
for( i = 0 ; i < CR_ECDSA_BUF_SIZE ; i++ ) {
ecdsa_test_buf[i] = (u8)(0xff & i );
}
memset( ecdsasig, 0, CR_ECDSA_SIGN_BUF_SIZE );
openssl_result = ECDSA_sign( 0, ecdsa_test_buf, CR_ECDSA_BUF_SIZE, ecdsasig,
&ecdsasiglen, pECkey );
if (openssl_result == 0) {
return CR_GENID_ERROR_ECDSA_SIGN;
}
openssl_result = ECDSA_verify( 0, ecdsa_test_buf, CR_ECDSA_BUF_SIZE, ecdsasig, ecdsasiglen, pECkey );
if( openssl_result != 1) {
return CR_GENID_ERROR_ECDSA_VERIFY;
}
return CR_GENID_SUCCESS;
}