/* ==================================================================== * 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.] */ #ifndef _CR_GENERATE_ID_PRIVATE_H_ #define _CR_GENERATE_ID_PRIVATE_H_ #ifdef __cplusplus extern "C" { #endif #ifdef USE_HSM // nShield #include "nfastapp.h" #include "nfkm.h" #include "rqcard-applic.h" #include "rqcard-fips.h" #endif // USE_HSM #include #include #include #include "ec_lcl.h" // EC_KEY (=ec_key_st) 構造体の参照に必要 #ifdef DEBUG_PRINT #define DEBUG_PRINT_ARRAY DebugPrintArray #else // !DEBUG_PRINT #define DEBUG_PRINT_ARRAY( ... ) ((void)0) #endif // DEBUG_PRINT #define ENCRYPT_AES 1 // 定義を有効でFIX.(これが未定義の場合、IDの暗号化がRSAになる。) #define CR_GEN_ID_VERSION 5 // シャープへのリリースごとにUPする。 // 2010/02/02 Release ver.4 // 2010/03/15 Release ver.5 #ifdef USE_HSM #define CR_GEN_ID_MAGICCODE 0xdeadb00f; /* 最終的にはこちらで動作。0xdeadbeefにするとRSAでコケる。 */ #else // !USE_HSM #define CR_GEN_ID_MAGICCODE 0xabadf00d; #endif // USE_HSM #define CR_RANDOM_LENGTH 0x50 #define EC_PRIVATE_KEY_LENGTH 0x20 #define ECDSA_SIGN_LENGTH 0x3C #define CR_RSV_LENGTH 0x10 #define EC_CURVE_NAME NID_sect233r1 //--------------------------------------------------- // OpenSSLの処理結果によるエラーコード //--------------------------------------------------- #define CR_GENID_ERROR_BN_NEW ( -1) #define CR_GENID_ERROR_ECC_KEY_NEW ( -2) #define CR_GENID_ERROR_ECC_GENERATE_PRIVATE_KEY ( -3) #define CR_GENID_ERROR_ECC_GENERATE_PUBLIC_KEY ( -4) #define CR_GENID_ERROR_ECC_READ_PRIVATE_KEY ( -5) #define CR_GENID_ERROR_ECC_READ_PUBLIC_KEY ( -6) #define CR_GENID_ERROR_ECDSA_SIGN ( -7) #define CR_GENID_ERROR_ECDSA_DEC ( -8) #define CR_GENID_ERROR_ECDSA_VERIFY ( -9) #define CR_GENID_ERROR_AES_ENC (-10) #define CR_GENID_ERROR_AES_DEC (-11) #define CR_GENID_ERROR_AES_VERIFY (-12) #define CR_GENID_ERROR_RSA_READ_PRIVATE_KEY (-13) #define CR_GENID_ERROR_RSA_READ_PUBLIC_KEY (-14) #define CR_GENID_ERROR_RSA_ENC (-15) #define CR_GENID_ERROR_RSA_DEC (-16) #define CR_GENID_ERROR_RSA_VERIFY (-17) #define CR_GENID_ERROR_SET_MEM_FUNCTIONS (-18) typedef struct { u32 magic_number; /* 0x00 - 0x03 = 0xdeadb00f 確定!*/ u32 device_id[CR_NUM_OF_DEVICEID]; /* 0x04 - 0x07 32bit device ID (32bit。1固定カウントアップ。) (本ID+randomの先頭0x1C bytesを組み合わせて、デバイス秘密鍵とする。) 0x08 - 0x0F 64bit CTR番号 seed (34bitのみ使用。1〜4の乱数カウントアップ) 0x10 - 0x17 64bit 予備ID (64bitフルに使用。1〜0x100000000の乱数カウントアップ) */ u8 version; /* 0x18 = CR_GEN_ID_VERSION */ u8 bonding_option; /* 0x19 ボンディングオプション */ u8 year; /* 0x1A デバイス証明書発行時間 (HSMから取得) */ u8 month; /* 0x1B */ u8 mday; /* 0x1C */ u8 hour; /* 0x1D */ u8 min; /* 0x1E */ u8 sec; /* 0x1F */ u32 expiryDate; /* 0x20 - 0x23 デバイス証明書期限 seconds from the Epoch (Jan 1, 1970 00:00) as a 32 bit */ u8 devicePrivKey[ EC_PRIVATE_KEY_LENGTH ]; /* 0x24 - 0x43 ECC233 private key (big endian) ユニーク性保証なし */ u8 deviceCertSign[ ECDSA_SIGN_LENGTH ]; /* 0x44 - 0x7F ECC233 ECDSA signature (big endian) */ u8 reserved[ CR_RSV_LENGTH ]; /* 0x80 - 0x8F 予約 */ u8 random[ CR_RANDOM_LENGTH ]; /* 0x90 - 0xDF 乱数 */ u8 hash[ SHA256_DIGEST_LENGTH ]; /* 0xE0 - 0xFF "0x00-0xDF"領域のSHA256ハッシュ */ } CR_ID_BUFFER; /* 合計256bytes = 2048bit */ #define CALL_STACK_SIZE (234) typedef struct { u64 totalCount; /* 0x00 - 0x07 */ u32 magic_number; /* 0x08 - 0x0b 0x01234567 確定!*/ u32 device_id0; /* 0x0c - 0x0f */ s32 errorCode; /* 0x10 - 0x13 */ u8 bonding_option; u8 recordSize; // コールスタックの記録サイズ u8 callStack[ CALL_STACK_SIZE ]; } CR_ERR_BUFFER; extern int GetTimestamp( u8 *pYear, u8 *pMonth, u8 *pMday, u8 *pHour, u8 *pMin, u8 *pSec, time_t *pTime); extern int GenerateRandom( u8 *pDst, int length ); extern int GenarateECCKeyPair( EC_KEY **ppECkey, u8 *pECPrivkey ); extern int SetECCKeyPair( EC_KEY **ppECkey, u8 *pECPrivkey ); extern void InitErrorInfo( void ); extern void SetErrorInfo( const char *funcName, u32 line ); extern void GetErrorInfo( char *stack, u8 *size ); extern int TestECDSA( EC_KEY *pECkey ); extern int GenerateCTRDeviceCert( EC_KEY *pECkey, u32 device_id, u8 bonding_option, u8 *pDevCertSign, u32 *pExpiryDate ); extern int CheckCTRDeviceCert( EC_KEY *pECkey, u32 device_id, u8 bonding_option, u8 *pDevCertSign, u32 expiryDate, u8 eFuseVersion ); extern int EncryptID( unsigned char *dst_buf, unsigned char *org_buf, u8 bonding_option ); extern void DebugPrintArray( char *pStr, const u8 *pData, int length ); extern void DebugFileOutput( u32 device_id, char *pSuffix, const u8 *pSrc, int length ); #ifdef __cplusplus } #endif #endif /* _CR_GENERATE_ID_PRIVATE_H_ */