[Feature][Modem]Update MTK MODEM V1.6 baseline version: MOLY.NR15.R3.MD700.IVT.MP1MR3.MP.V1.6
MTK modem version: MT2735_IVT_MOLY.NR15.R3.MD700.IVT.MP1MR3.MP.V1.6.tar.gz
RF modem version: NA
Change-Id: I45a4c2752fa9d1a618beacd5d40737fb39ab64fb
diff --git a/mcu/interface/driver/che/cert_verify.h b/mcu/interface/driver/che/cert_verify.h
new file mode 100644
index 0000000..8438a44
--- /dev/null
+++ b/mcu/interface/driver/che/cert_verify.h
@@ -0,0 +1,21 @@
+#ifndef __CERT_VERIFY_H__
+#define __CERT_VERIFY_H__
+
+#include "kal_public_defs.h"
+#include "custom_sec_key.h"
+#include "kal_public_api.h"
+#include "kal_general_types.h"
+#include "x509.h"
+#include "dhl_trace.h"
+
+extern const CUST_SEC_CERT_ST g_ROOT_CERT;
+
+kal_uint8 *CertChain_Get_RCert(TYPE_CUST_CHL_RCERT CERT_ID, kal_uint32 *root_cert_len);
+
+int Convert_Cert(kal_uint8 *BinaryCert, X509_CTX **FormatCert, int der_len, int *x509_len, kal_bool isPEM);
+
+int CertChain_Verify_SubCert(X509_CTX *ParentCert_X509, X509_CTX *SubCert_X509, kal_uint32 parent_len, kal_uint32 sub_len);
+
+int CertChain_Verify_Data(X509_CTX *SubCert_X509, kal_uint8 * data, kal_uint32 data_len, kal_uint8 * signature, kal_uint32 signature_len, kal_uint32 sig_type);
+
+#endif
diff --git a/mcu/interface/driver/che/che_api.h b/mcu/interface/driver/che/che_api.h
new file mode 100644
index 0000000..25eca2c
--- /dev/null
+++ b/mcu/interface/driver/che/che_api.h
@@ -0,0 +1,1615 @@
+/*****************************************************************************
+* Copyright Statement:
+* --------------------
+* This software is protected by Copyright and the information contained
+* herein is confidential. The software may not be copied and the information
+* contained herein may not be used or disclosed except with the written
+* permission of MediaTek Inc. (C) 2007
+*
+* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
+* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
+* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
+* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
+* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
+*
+* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
+* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
+* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+*
+* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
+* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
+* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
+* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
+* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
+*
+*****************************************************************************/
+
+/*****************************************************************************
+ *
+ * Filename:
+ * ---------
+ * che_api.h
+ *
+ * Project:
+ * --------
+ * Maui_Software
+ *
+ * Description:
+ * ------------
+ * Cipher/Hash Engine API.
+ *
+ * Author:
+ * -------
+ * -------
+ *
+ *============================================================================
+ * HISTORY
+ * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
+ *------------------------------------------------------------------------------
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ *
+ *
+ * removed!
+ * removed!
+ *
+ *
+ * removed!
+ * removed!
+ *
+ *
+ * removed!
+ * removed!
+ *
+ *
+ * removed!
+ * removed!
+ *
+ *
+ * removed!
+ * removed!
+ *
+ *
+ * removed!
+ * removed!
+ *
+ *
+ * removed!
+ * removed!
+ *
+ *
+ *------------------------------------------------------------------------------
+ * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
+ *============================================================================
+ ****************************************************************************/
+
+
+/*! \mainpage Cipher Hash Engine
+ * @author Julie Yang
+ * @version 1.0
+ * @date 2007/06/14
+ *
+ * \section CHE CHE API Introduction
+ *
+ * This document is an introduction of MTK Cipher and Hash Engine API. There is an interface
+ * between Application and Cipher/Hash engine. The interface layer includes four functions.
+ * User and application can just use these four functions to set
+ * cipher/hash condition, and trigger the process of the cipher/hash engine. Either the
+ * bottom layer is hardware or software driver.\n\n
+ *
+ * CHE API also release four enum structures to define the setting parameters for AP using.
+ * User can just use these structures to set cipher/hash conditions,
+ * and trigger the process of the cipher/hash engine.\n
+ *
+ * The header file is in \\mcu\\applib\\misc\\include\\che_api.h\n
+ *
+ * The API is supported after 07A branch. \n
+ *
+ *
+ */
+
+/** @defgroup CHE Cipher/Hash Engine Interface
+ * @{
+ */
+
+/** @defgroup CHE Cipher/Hash Engine Interface
+ * @{
+ */
+#ifndef _CHE_API_H
+#define _CHE_API_H
+
+#include "kal_public_api.h" //MSBB change #include "kal_release.h"
+#include "fsal.h"
+//#include "md5.h"
+//#include "sha1.h"
+//#include "md4.h"
+
+/* DOM-NOT_FOR_SDK-BEGIN */
+/* DNS context structure */
+typedef struct DES_CNXT_T
+{
+#if defined(__HW_CHE__)
+ kal_uint8 cheCKey[36]; /* des; 3des;convert_aes_128_key;convert_aes_192_key;convert_aes_256_key */
+ kal_uint8 cheDataAddr[120]; /* che_hw_des;che_hw_aes;che_hw_hash */
+#else /* defined(__HW_CHE__) */
+ kal_uint8 cheLastBlock[16]; /* Last block used in counter mode (store the previous cipher text) */
+ kal_uint8 cheLastReminder[16]; /* Last remainder block */
+ kal_uint8 cheKeySchedule[128]; /* key schedule */
+ kal_uint8 iv[64]; /* initial vector */
+#endif /* defined(__HW_CHE__) */
+}DES_CNXT;
+
+/* AES context structure */
+typedef struct AES_CNXT_T
+{
+#if defined(__HW_CHE__)
+ kal_uint8 cheCKey[36]; /* des; 3des;convert_aes_128_key;convert_aes_192_key;convert_aes_256_key */
+ kal_uint8 cheDataAddr[120]; /* che_hw_des;che_hw_aes;che_hw_hash */
+#else /* defined(__HW_CHE__) */
+ kal_uint8 cheLastReminder[16]; /* Last remaining segment */
+ kal_uint8 orgIV[64]; /* Initial vector */
+#endif /* defined(__HW_CHE__) */
+}AES_CNXT;
+
+/* MD5 and SH1 structure */
+typedef struct MD5_CNXT_T
+{
+#if defined(__HW_CHE__)
+ kal_uint8 cheDataAddr[120]; /* che_hw_des;che_hw_aes;che_hw_hash */
+#else
+ kal_uint8 *working_buffer; /* working buffer */
+#endif
+}MD5_CNXT, SHA1_CNXT;
+
+/* structure used for MD5,SHA1, MAC_MD5, MAC_SHA1, MD4, DHM */
+typedef struct CNXT_T
+{
+ kal_uint8 *working_buffer; /* working buffer */
+}SW_MISC_CNXT;
+
+/* RC4 structure */
+typedef struct RC4_CNXT_T
+{
+ kal_uint32 x, y; /* the swap index */
+ kal_uint8 keyStream[256]; /* key stream */
+} RC4_CNXT;
+
+/* used only for HW HASH ;che_hw_des;che_hw_aes;che_hw_hash */
+typedef struct HASH_CNXT_T
+{
+ kal_uint8 cheDataAddr[120]; /* Data address */
+}HASH_CNXT;
+
+/* RSA structure */
+#if 0
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+#endif
+
+/* #define __CHE_RSA_GEN_KEY__ */
+
+/** The structure STCHE is defined to record and maintain the context of CHE */
+typedef struct STCHE_T
+{
+ kal_uint32 cheLastLength; /* last block length */
+ kal_uint8 *cheLastDstAddr; /* last destination address */
+
+ kal_uint8 *cheWorkBuf; /* pointe to a space used by each algorithm */
+ kal_uint32 cheBufLen; /* the length of cheWorkBuf. It depends on the encrypt algorithm */
+ kal_uint32 cipher_type; /* cipher type */
+
+ kal_uint8 *cheKey; /* key stream */
+ kal_uint8 *cheIV; /* initial vector */
+ kal_uint32 cheKeyLength; /* key stream length */
+ kal_uint32 cheIVLength; /* initial vector length */
+
+#if defined(__HW_CHE__)
+ kal_bool cheResetKeyFlag; /* Reset key flag */
+ kal_bool cheResetIVFlag; /* Reset initial vector flag */
+#endif /* defined(__HW_CHE__) */
+
+ kal_bool cheFirstFlag; /* first block flag */
+ } STCHE;
+
+/* DOM-NOT_FOR_SDK-END */
+
+/* CHE error cause */
+typedef enum
+{
+ CHE_OK = 0, /* success */
+ CHE_ERROR = -1, /* error occurs */
+ CHE_KEY_CHK_FAIL = -2, /* public or private key check fail */
+ CHE_INVALID_LENGTH = -3, /* buffer length too small or too long */
+ CHE_OPERATION_FAIL = -4 /* che operation fails, such as cipher fail */
+} che_result_error_enum;
+
+/** CHE_TYPE define the cipher and hash function algorithm type */
+typedef enum
+{
+ CHE_MD5 = 1, /* MD5 hash algorithm. The output length is fixed 16 bytes */
+ CHE_DES, /* DES block cipher algorithm. The CHE will do auto padding in DES type.
+ The maximum padding length is 8 bytes */
+ CHE_3DES, /* 3DES block cipher algorithm. The CHE will do auto padding in 3DES type.
+ The maximum padding length is 8 bytes */
+ CHE_AES, /* AES block cipher algorithm. The CHE will do auto padding in AES(ECB, CBC) type.
+ The maximum padding length is 16 bytes */
+
+ CHE_MAC_MD5, /* HMAC_MD5 hash algorithm. The output length is fixed 16 bytes */
+ CHE_RC4, /* RC4 stream cipher algorithm. No padding */
+
+ CHE_DH, /* Diffie Hellman cipher algorithm used to generate the key. No padding */
+ CHE_MAC_SHA224, /* HMAC_SHA224 hash algorithm. The output length is fixed 28 bytes */
+ CHE_MAC_SHA256, /* HMAC_SHA256 hash algorithm. The output length is fixed 32 bytes */
+ CHE_SHA224, /* SHA 224 algorithm */
+ CHE_SHA256 /* SHA 256 algorithm */
+ //CHE_SHA1, /* SHA1 hash algorithm. The output length is fixed 20 bytes */
+ //CHE_MD4, /* MD4 hash algorithm. The output length is fixed 16 bytes */
+ //CHE_MAC_SHA1, /* HMAC_SHA1 hash algorithm. The output length is fixed 20 bytes */
+ //CHE_RSA, /* RSA asym cipher algorithm. */
+
+} CHE_TYPE;
+
+/* CHE_OPERATION_MODE define the cipher function mode. For hash function, it could be set CHE_MODE_NULL */
+typedef enum
+{
+ CHE_MODE_NULL = 0, /* null mode. This mode is used when there is no mode operation */
+ CHE_ECB, /* AES/DES ECB mode */
+ CHE_CBC, /* AES/DES CBC mode */
+ CHE_CTR, /* AES CTR(counter) mode */
+ CHE_F8, /* AES F8 mode */
+
+ CHE_SKIP, /* RC4 Skip mode */
+ CHE_KEY_WRAP, /* AES key wrap mode */
+ CHE_KEY_UNWRAP /* AES key unwrap mode */
+
+} CHE_OPERATION_MODE;
+
+#if 0
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+#endif
+
+/* CHE_ACTION define this CHE process should run hash, encipher or decipher */
+typedef enum
+{
+ CHE_HASH, /* generate hash value */
+ CHE_ENC, /* encryption data */
+ CHE_DEC, /* decryption data */
+ CHE_MAC_HASH, /* generate HMAC value */
+
+ DH_INIT, /* initialize the Diffie-Hellman */
+ DH_MAKE_PARAMS, /* Suppose A=G^a mod p. generate discrete logarithms(a) and A */
+ DH_MAKE_PUBLIC, /* Suppose A=G^a mod p. generate discrete logarithms(a) and A */
+ DH_CALC_SECRET, /* Suppose B=G^b mod p. Calculate the shared secret (G^b)^a mod P */
+ DH_FREE /* finialize the Diffie-Hellman */
+} CHE_ACTION;
+
+/* CHE_KEY_ACTION define the CHE Key process. The life cycle of input key buffer should
+ * be maintained until che_deinit except RSA.
+ */
+typedef enum
+{
+ CHE_GEN_KEY, /* generate the key exclude asynmetry and RC4 algirhtm */
+ CHE_SET_KEY, /* set the key exclude asynmetry and RC4 algirhtm */
+ CHE_GET_KEY, /* get the key exclude asynmetry and RC4 algirhtm*/
+ CHE_DEL_KEY, /* delete the key exclude asynmetry and RC4 algirhtm */
+
+ CHE_SET_RC4_KEY, /* set the RC4 key */
+
+#if defined(__CHE_RSA_GEN_KEY__)
+ CHE_GEN_RSA_KEY, /* generate RSA key for hardware */
+ CHE_DEL_RSA_KEY, /* delete the RSA key for hardward */
+#endif /* defined(__CHE_RSA_GEN_KEY__) */
+ CHE_SET_RSA_N, /* set the RSA modulus N. N=P*Q. N is prime number.
+ The input N is a binary data */
+ CHE_SET_RSA_E, /* set the RSA public exponent: E.
+ The input E is a binary data */
+ CHE_SET_RSA_D, /* set the RSA private exponent: D.
+ The input D is a binary data */
+ CHE_SET_RSA_P, /* set the RSA modulus P(CRT format).
+ The input P is a binary data */
+ CHE_SET_RSA_Q, /* set the RSA modulus Q(CRT format).
+ The input Q is the binary data */
+ CHE_SET_RSA_dModPm1, /* set the RSA private exponent(CRT format): DP.
+ The input DP is a binary data */
+ CHE_SET_RSA_dModQm1, /* set the RSA private exponent(CRT format): DQ.
+ The input DQ is a binary data */
+ CHE_SET_RSA_qInvModP, /* set the coefficient Integer(CRT format): inverse Q.
+ The input InvQ is a binary data */
+
+ CHE_SET_DH_P, /* set the DH modulus: P. The input P is a binary data. */
+ CHE_SET_DH_G, /* set the DH prime root: G. The input G is a binary data */
+ CHE_SET_DH_SELF_PUBLIC, /* set the self public value. G^X mod P. The input is a binary data. */
+ CHE_SET_DH_PEER_PUBLIC, /* set the peer public value. G^Y mod P. The input is a binary data. */
+ CHE_SET_DH_PRIVATE, /* set the discrete logarithms(private key): X.
+ The input X is a binary data. */
+
+ CHE_GET_DH_SELF_PUBLIC_LEN, /* get the self public length */
+ CHE_GET_DH_PRIVATE_LEN, /* get the discrete logarithm length */
+ CHE_GET_DH_SECRET_LEN, /* get the secret length */
+
+ CHE_GET_DH_SELF_PUBLIC, /* get self public value */
+ CHE_GET_DH_PRIVATE, /* get the discrete logarithm */
+ CHE_GET_DH_SECRET, /* get the shared key,i.e. key. key = GY^X mod P */
+
+ CHE_GET_RSA_N, /* get the RSA modulus N. N=P*Q. N is prime number. The output N is binary data */
+ CHE_GET_RSA_E, /* get the RSA public exponent: E. The output E is a binary data */
+ CHE_GET_RSA_D, /* get the RSA private exponent: D. The output D is a binary data */
+ CHE_GET_RSA_P, /* get the RSA modulus P(CRT format). The output P is a binary data */
+ CHE_GET_RSA_Q, /* get the RSA modulus Q(CRT format). The output Q is the binary data */
+ CHE_GET_RSA_dModPm1, /* get the RSA private exponent(CRT format): DP. The output DP is a binary data */
+ CHE_GET_RSA_dModQm1, /* get the RSA private exponent(CRT format): DQ. The output DQ is a binary data */
+ CHE_GET_RSA_qInvModP, /* get the coefficient Integer(CRT format): inverse Q. The output InvQ is a binary data */
+
+ CHE_GET_RSA_N_LEN, /* get the RSA modulus N. N=P*Q. N is prime number. The output N is binary data */
+ CHE_GET_RSA_E_LEN, /* get the RSA public exponent: E. The output E is a binary data */
+ CHE_GET_RSA_D_LEN, /* get the RSA private exponent: D. The output D is a binary data */
+ CHE_GET_RSA_P_LEN, /* get the RSA modulus P(CRT format). The output P is a binary data */
+ CHE_GET_RSA_Q_LEN, /* get the RSA modulus Q(CRT format). The output Q is the binary data */
+ CHE_GET_RSA_dModPm1_LEN, /* get the RSA private exponent(CRT format): DP. The output DP is a binary data */
+ CHE_GET_RSA_dModQm1_LEN, /* get the RSA private exponent(CRT format): DQ. The output DQ is a binary data */
+ CHE_GET_RSA_qInvModP_LEN /* get the coefficient Integer(CRT format): inverse Q. The output InvQ is a binary data */
+
+} CHE_KEY_ACTION;
+
+typedef enum
+{
+ CHE_NO_ERROR = 0, /* success */
+ CHE_NOT_SUPPORT_ALGO, /* not support error */
+ CHE_INVALID_NULL_POINTER, /* input NULL pointer */
+ CHE_INVALID_KEY_POINTER, /* key NULL pointer */
+ CHE_CALCULATE_FAIL /* calculate fail */
+
+} CHE_CALCULATE_ERROR;
+
+#include "che_trick.h"
+
+
+/*****************************************************************************
+ * FUNCTION
+ * che_init
+ * DESCRIPTION
+ * Initialize the CHE_CONTEXT data structure created from AP
+ * PARAMETERS
+ * che_context : [IN] CHE context
+ * type : [IN] CHE type (CHE_TYPE)
+ * RETURN VALUES
+ * void
+ * SEE ALSO
+ * che_deinit
+ * EXAMPLE
+ * <code>
+ * STCHE context;
+ * che_init(&context, CHE_SHA1);
+ * </code>
+ *****************************************************************************/
+extern void che_init(STCHE *che_context, kal_uint32 type);
+
+/*****************************************************************************
+ * FUNCTION
+ * che_deinit
+ * DESCRIPTION
+ * Deinitialize the CHE_CONTEXT
+ * PARAMETERS
+ * che_context : [IN] CHE context
+ * RETURN VALUES
+ * void
+ * SEE ALSO
+ * che_init
+ * EXAMPLE
+ * <code>
+ * STCHE context;
+ * che_init(&context, CHE_SHA1);
+ * ...
+ * che_deinit(&context);
+ * </code>
+ *****************************************************************************/
+extern void che_deinit(STCHE *che_context);
+
+/*****************************************************************************
+ * DESCRIPTION
+ * API used to set, get, delete, generate the key value.
+ * If user uses this API to get the key, CHE only stores the pointer of the key.
+ * Thus, the key has to be kept until the che_deinit is called. There is only
+ * One exception, in the RSA, we will allocate another space to store the key.
+ * Thus, user doesn't have to care if they can release the buffer of key.
+ * PARAMETERS
+ * che_context : [IN] che context
+ * key_act : [IN] key action (CHE_KEY_ACTION)
+ * key : [IN/OUT] key stream
+ * key_ley : [IN] key stream length
+ * EXAMPLE
+ * <code>
+ * STCHE che_context;
+ * che_init(&che_context, CHE_MAC_MD5);
+ * che_key_action(&che_context, CHE_SET_KEY, (kal_uint8 *) key, key_len);
+ * </code>
+ *****************************************************************************/
+#define che_key_action(che_context, key_act, key, key_len) \
+ \
+ ((void)TRICK_##key_act, run_##key_act##_process(che_context, key, key_len))
+
+/*****************************************************************************
+ * FUNCTION
+ * che_set_iv
+ * DESCRIPTION
+ * Set initial vector for CBC or CTR or AES_F8 mode. CHE only stores the pointer address.
+ * The iv buffer should be kept by the user until the first block is computed.
+ * To avoid confusion, user can keep the buffer until the che_deinit is called.
+ * PARAMETERS
+ * che_context : [IN] CHE context
+ * iv : [IN] initial vector.
+ * the input has to be 4-byte alignment on ARM platform
+ * iv_len : [IN] initial vector length
+ * RETURN VALUES
+ * void
+ * EXAMPLE
+ * <code>
+ * STCHE context;
+ * che_init(&context, CHE_AES);
+ * che_set_iv(&che_context, iv, 16);
+ * ...
+ * che_deinit(&context);
+ * </code>
+ *****************************************************************************/
+extern void che_set_iv(STCHE *che_context, kal_uint8 *iv, kal_uint32 iv_len);
+
+
+/*****************************************************************************
+ * DESCRIPTION
+ * API used to execute cipher and hash process function.
+ * Since using Marco to reduce linkage code size, please pass the "type" parameter
+ * as the constant announced in CHE_TYPE enum. It doesn't allow passing variable.
+ * The dest buffer size should be refer to the RFC standard specification.
+ * The DES/AES in CBC/ECB mode will do the auto padding if application uses the CHE's auto padding,
+ * the destination buffer address has to be at least source_length + 1 DES block size(8 bytes) .
+ * Otherwise, there will be a memory corruption. If application doesn't want to use auto padding,
+ * it cannot set the last_block to "TRUE". That is to say, as long as the last_block is not set to
+ * TRUE, there will no auto padding. The auto padding CHE uses is PKCS#1 v15. This padding is standard
+ * padding used in PCKS#5.
+ * P.S. The input and output buffer should be different buffer. Don't use the same buffer.
+ *
+ * PARAMETERS
+ * che_context : [IN] che context
+ * type : [IN] che type (CHE_TYPE)
+ * mode : [IN] che operation mode (CHE_OPERATION_MODE)
+ * act : [IN] che action (CHE_ACTION)
+ * source : [IN] source input data buffer (it has to be 4-byte alignment for DES and AES)
+ * dest : [OUT] destination buffer (it has to be 4-byte alignment for DES)
+ * source_length : [IN] source input buffer length
+ * final : [IN] KAL_TRUE: do the standard auto padding(PKCS#5).
+ * KAL_FLASE: don't do the standard auto padding(PKCS#5).
+ * If user wants the auto padding, s/he has to set the final to TRUE in its final block
+ * operation. On the contrary, if s/he doesn't want the auto padding, please do not set
+ * final to TRUE.
+ * RETURN VALUES
+ * KAL_TRUE : CHE process successful
+ * KAL_FLASE : CHE process fail
+ * SEE ALSO
+ * che_process_from_file
+ * EXAMPLE
+ * Symmetry Block Cipher Examples:
+ *
+ * 1. DES Encryption (CBC mode):
+ * The input, initial vector and destination address have to be in 4-byte alignment.
+ *
+ * With auto padding:
+ * <code>
+ * STCHE che_context;
+ * che_init(&che_context, CHE_DES);
+ * che_key_action(&che_context, CHE_SET_KEY, key, key_length);
+ * che_set_iv(&che_context, init_vector, vec_length);
+ * che_process(&che_context, CHE_DES, CHE_CBC , CHE_ENC, src_buffer, dst_buffer, src_len, KAL_FALSE);
+ * ...
+ * //if the last segmemnt in source buffer is intended to be processed and we want to use the auto padding,
+ * //we set final to TRUE
+ * che_process(&che_context, CHE_DES, CHE_CBC , CHE_ENC, src_buffer, dst_buffer, src_len, KAL_TRUE);
+ * // do other operation
+ * ...
+ * //Deinitialize when all cipher operation is finish
+ * che_deinit(&che_context);
+ * </code>
+ *
+ * Without auto padding:
+ * <code>
+ * STCHE che_context;
+ * che_init(&che_context, CHE_DES);
+ * che_key_action(&che_context, CHE_SET_KEY, key, key_length);
+ * che_set_iv(&che_context, init_vector, vec_length);
+ * che_process(&che_context, CHE_DES, CHE_CBC , CHE_ENC, src_buffer, dst_buffer, src_len, KAL_FALSE);
+ * ...
+ * //if the last segmemnt in source buffer is intended to be processed and we don't want the auto padding,
+ * //we set final to FALSE
+ * che_process(&che_context, CHE_DES, CHE_CBC , CHE_ENC, src_buffer, dst_buffer, src_len, KAL_FALSE);
+ * // do other operation
+ * ...
+ * //Deinitialize when all cipher operation is finish
+ * che_deinit(&che_context);
+ * </code>
+ * 2. DES Encryption (ECB mode):
+ * With auto padding:
+ * <code>
+ * STCHE che_context;
+ * che_init(&che_context, CHE_DES);
+ * che_key_action(&che_context, CHE_SET_KEY, key, key_length);
+ * che_process(&che_context, CHE_DES, CHE_ECB , CHE_ENC, src_buffer, dst_buffer, src_len, KAL_FALSE);
+ * ...
+ * //if the last segmemnt in source buffer is intended to be processed and we want to use the auto padding,
+ * //we set final to TRUE
+ * che_process(&che_context, CHE_DES, CHE_ECB , CHE_ENC, src_buffer, dst_buffer, src_len, KAL_TRUE);
+ * // do other operation
+ * ...
+ * //Deinitialize when all cipher operation is finish
+ * che_deinit(&che_context);
+ * </code>
+ *
+ * Without auto padding:
+ * <code>
+ * STCHE che_context;
+ * che_init(&che_context, CHE_DES);
+ * che_key_action(&che_context, CHE_SET_KEY, key, key_length);
+ * che_process(&che_context, CHE_DES, CHE_ECB , CHE_ENC, src_buffer, dst_buffer, src_len, KAL_FALSE);
+ * ...
+ * //if the last segmemnt in source buffer is intended to be processed and we don't want to use the
+ * //auto padding, we set final to TRUE
+ * che_process(&che_context, CHE_DES, CHE_ECB , CHE_ENC, src_buffer, dst_buffer, src_len, KAL_FALSE);
+ * // do other operation
+ * ...
+ * //Deinitialize when all cipher operation is finish
+ * che_deinit(&che_context);
+ * </code>
+ * 3. AES Encryption (Counter mode):
+ * Because this mode doesn't need padding, we don't care the "final" parameter in this mode.
+ * Its input data can not in block boundary size.
+ * <code>
+ * STCHE che_cntx;
+ * che_init(&che_cntx, CHE_AES);
+ * che_key_action(&che_cntx, CHE_SET_KEY, key, 16);
+ * che_set_iv(&che_cntx, iv, 16);
+ * che_process(&che_cntx, CHE_AES, CHE_CTR, CHE_ENC, src_data, dst_data, src_len, KAL_TRUE);
+ * // do other operation
+ * ...
+ * //Deinitialize when all cipher operation is finish
+ * che_deinit(&che_cntx);
+ * </code>
+ *
+ * 4. AES Encryption (Key WRAP mode):
+ * The input data has to be in multiple of 64 bits block size.
+ * Because this mode doesn't need padding, we don't care the "final" parameter in this mode.
+ * Because one more block is generated for checking in KEY WRAP mode,
+ * the input and output buffer should be different one in KEY_WRAP and KEY_UNWRAP mode.
+ * <code>
+ * STCHE che_cntx;
+ * che_init(&che_cntx, CHE_AES);
+ * che_key_action(&che_cntx, CHE_SET_KEY, key, 16);
+ * che_set_iv(&che_cntx, iv, 16);
+ * che_process(&che_cntx, CHE_AES, CHE_KEY_WRAP, CHE_ENC, src_data, dst_data, src_len, KAL_TRUE);
+ * // do other operation
+ * ...
+ * //Deinitialize when all cipher operation is finish
+ * che_deinit(&che_cntx);
+ * </code>
+ *
+ * 5. AES Encryption (CBC mode):
+ * The input and initial vector address have to be in 4-byte alignment.
+ *
+ * With auto padding:
+ * <code>
+ * STCHE che_cntx;
+ * che_init(&che_cntx, CHE_AES);
+ * che_key_action(&che_cntx, CHE_SET_KEY, key, 16);
+ * che_set_iv(&che_cntx, iv, 16);
+ * che_process(&che_cntx, CHE_AES, CHE_CBC, CHE_ENC, src_data, dst_data, src_len, KAL_FALSE);
+ * //final block
+ * che_process(&che_cntx, CHE_AES, CHE_CBC, CHE_ENC, src_data, dst_data, src_len, KAL_TRUE);
+ * // do other operation
+ * ...
+ * //Deinitialize when all cipher operation is finish
+ * che_deinit(&che_cntx);
+ * </code>
+ *
+ * Without auto padding:
+ * <code>
+ * STCHE che_cntx;
+ * che_init(&che_cntx, CHE_AES);
+ * che_key_action(&che_cntx, CHE_SET_KEY, key, 16);
+ * che_set_iv(&che_cntx, iv, 16);
+ * che_process(&che_cntx, CHE_AES, CHE_CBC, CHE_ENC, src_data, dst_data, src_len, KAL_FALSE);
+ * // final block
+ * che_process(&che_cntx, CHE_AES, CHE_CBC, CHE_ENC, src_data, dst_data, src_len, KAL_FALSE);
+ * // do other operation
+ * ...
+ * //Deinitialize when all cipher operation is finish
+ * che_deinit(&che_cntx);
+ * </code>
+ * 6. AES Encryption (ECB mode):
+ * The input address have to be in 4-byte alignment.
+ *
+ * With auto padding:
+ * <code>
+ * STCHE che_cntx;
+ * che_init(&che_cntx, CHE_AES);
+ * che_key_action(&che_cntx, CHE_SET_KEY, key, 16);
+ * che_process(&che_cntx, CHE_AES, CHE_ECB, CHE_ENC, src_data, dst_data, src_len, KAL_FALSE);
+ * //final block
+ * che_process(&che_cntx, CHE_AES, CHE_ECB, CHE_ENC, src_data, dst_data, src_len, KAL_TRUE);
+ * // do other operation
+ * ...
+ * //Deinitialize when all cipher operation is finish
+ * che_deinit(&che_cntx);
+ * </code>
+ *
+ * Without auto padding:
+ * <code>
+ * STCHE che_cntx;
+ * che_init(&che_cntx, CHE_AES);
+ * che_key_action(&che_cntx, CHE_SET_KEY, key, 16);
+ * che_process(&che_cntx, CHE_AES, CHE_ECB, CHE_ENC, src_data, dst_data, src_len, KAL_FALSE);
+ * //final block
+ * che_process(&che_cntx, CHE_AES, CHE_ECB, CHE_ENC, src_data, dst_data, src_len, KAL_FALSE);
+ * // do other operation
+ * ...
+ * //Deinitialize when all cipher operation is finish
+ * che_deinit(&che_cntx);
+ * </code>
+ *
+ * Symmetry Stream Cipher Example:
+ *
+ * 1. RC4 Encryption:
+ * Because this mode doesn't need padding, we don't care the "final" parameter in this mode.
+ *
+ * Use default RC4 mode:
+ * <code>
+ * STCHE che_ctx;
+ * che_init(&che_ctx, CHE_RC4);
+ * che_key_action(&che_ctx, CHE_SET_KEY, (kal_uint8 *)key, key_len);
+ * che_process(&che_ctx, CHE_RC4, CHE_MODE_NULL, CHE_ENC, (kal_uint8 *)input,
+ * (kal_uint8 *)output, (kal_int32)input_size, KAL_TRUE);
+ * // do other operation
+ * ...
+ * //Deinitialize when all cipher operation is finish
+ * che_deinit(&che_ctx);
+ * </code>
+ *
+ * Use CHE_SKIP mode:
+ * skip the first 256 byts key data because first 256 bytes are not random enough.
+ * <code>
+ * STCHE che_ctx;
+ * che_init(&che_ctx, CHE_RC4);
+ * che_key_action(&che_ctx, CHE_SET_KEY, (kal_uint8 *)key, key_len);
+ * che_process(&che_ctx, CHE_RC4, CHE_SKIP, CHE_ENC, (kal_uint8 *)input,
+ * (kal_uint8 *)output, (kal_int32)input_size, KAL_TRUE);
+ * // do other operation
+ * ...
+ * //Deinitialize when all cipher operation is finish
+ * che_deinit(&che_ctx);
+ * </code>
+ *
+ * Hash Operation:
+ *
+ * 1. MD5 Hash generation:
+ * we have to set the final block to TRUE for hash operation. The mode field is useless for hash algorithm.
+ * HASH doesn't have any mode options. Thus, we set it to CHE_MODE_NULL.
+ *
+ * <code>
+ * STCHE che_context;
+ * kal_uint8 digest[16];
+ * che_init(&che_context, CHE_MD5);
+ * che_process(&che_context,CHE_MD5,CHE_MODE_NULL,CHE_HASH,(kal_uint8*)input,digest,input_len, KAL_FALSE);
+ * ...
+ * che_process(&che_context,CHE_MD5,CHE_MODE_NULL,CHE_HASH,(kal_uint8*)input,digest,input_len, KAL_TRUE);
+ * // do other operation
+ * ...
+ * //Deinitialize when all hash operation is finish
+ * che_deinit(&che_context);
+ * </code>
+ *
+ * 2. SHA1 Hash generation:
+ * we have to set the final block to TRUE for hash operation. The mode field is useless for hash algorithm.
+ * HASH doesn't have any mode options. Thus, we set it to CHE_MODE_NULL.
+ *
+ * <code>
+ * STCHE che_context;
+ * kal_uint8 digest[20];
+ * che_init(&che_context, CHE_SHA1);
+ * che_process(&che_context,CHE_SHA1,CHE_MODE_NULL,CHE_HASH,(kal_uint8*)input,digest,input_len, KAL_FALSE);
+ * ...
+ * che_process(&che_context,CHE_SHA1,CHE_MODE_NULL,CHE_HASH,(kal_uint8*)input,digest,input_len, KAL_TRUE);
+ * che_deinit(&che_context);
+ * // do other operation
+ * ...
+ * //Deinitialize when all hash operation is finish
+ * </code>
+ *
+ * 3. HMAC MD5 generation:
+ * we have to set the final block to TRUE for HMAC operation because HMAC doesn't have padding consideartion for last block.
+ * The mode field is useless for HMAC algorithm. HMAC doesn't have any mode options. Thus, we set it to CHE_MODE_NULL.
+ *
+ * <code>
+ * STCHE che_context;
+ * kal_uint8 digest[16];
+ * che_init(&che_context, CHE_MAC_MD5);
+ * che_key_action(&che_context, CHE_SET_KEY, (kal_uint8 *) key, key_len);
+ * che_process(&che_context, CHE_MAC_MD5, CHE_MODE_NULL, CHE_HASH, (kal_uint8 *) input, digest, input_len, KAL_FALSE);
+ * ...
+ * che_process(&che_context, CHE_MAC_MD5, CHE_MODE_NULL, CHE_HASH, (kal_uint8 *) input, digest, input_len, KAL_TRUE);
+ * // do other operation
+ * ...
+ * //Deinitialize when all hash operation is finish
+ * che_deinit(&che_context);
+ * </code>
+ *
+ * 4. SHA2 Hash generation:
+ * we have to set the final block to TRUE for hash operation. The mode field is useless for hash algorithm.
+ * HASH doesn't have any mode options. Thus, we set it to CHE_MODE_NULL.
+ *
+ * <code>
+ * STCHE che_context;
+ * kal_uint8 digest[32];
+ * che_init(&che_context, CHE_SHA256);
+ * che_process(&che_context, CHE_SHA256, CHE_MODE_NULL, CHE_HASH, (kal_uint8 *) input, digest, input_len, KAL_FALSE);
+ * ...
+ * che_process(&che_context, CHE_SHA256, CHE_MODE_NULL, CHE_HASH, (kal_uint8 *) input, digest, input_len, KAL_TRUE);
+ * // do other operation
+ * ...
+ * //Deinitialize when all hash operation is finish
+ * che_deinit(&che_context);
+ * </code>
+ *
+ * Public Key or Key Agreement:
+ *
+ * 1. Diffie-Hellman:
+ * The source and dest parameter is useless for DH.
+ * The output data is get through che_key_action.
+ * <code>
+ * STCHE che_ctx_dh;
+ * kal_uint32 dh_secret_len;
+ * kal_uint8 *dh_secret_key = NULL;
+ *
+ * che_init(&che_ctx_dh, CHE_DH);
+ * che_process(&che_ctx_dh, CHE_DH, CHE_MODE_NULL, DH_INIT, NULL, NULL, 0, KAL_TRUE);
+ * // set the p and g. p and g must in binary format
+ * che_key_action(&che_ctx_dh, CHE_SET_DH_P, (kal_uint8*) P, P_Len);
+ * che_key_action(&che_ctx_dh, CHE_SET_DH_G, G, G_Len);
+ * // Compute self public B = (g^b mod p) and private (b)
+ * che_process(&che_ctx_dh, CHE_DH, CHE_MODE_NULL, DH_MAKE_PARAMS, NULL, NULL, 0, KAL_TRUE);
+ * // Set the peer public key (A) in binary format
+ * che_key_action(&che_ctx_dh, CHE_SET_DH_PEER_PUBLIC, (kal_uint8*)peer_public, peer_public_len);
+ * // calculate the shared secret: K
+ * che_process(&che_ctx_dh, CHE_DH, CHE_MODE_NULL, DH_CALC_SECRET, NULL, NULL, 0, KAL_TRUE);
+ * //get the shared secret key
+ * che_key_action(&che_ctx_dh, CHE_GET_DH_SECRET_LEN, (void*)&dh_secret_len, sizeof(int));
+ * dh_secret_key = malloc(dh_secret_len);
+ * che_key_action(&che_ctx_dh, CHE_GET_DH_SECRET, (kal_uint8*) dh_secret_key, dh_secret_len);
+ * //call deinit when all operations are finish
+ * che_process(&che_ctx_dh, CHE_DH, CHE_MODE_NULL, DH_FREE, NULL, NULL, 0, KAL_TRUE);
+ * che_deinit(&che_ctx_dh);
+ * </code>
+ *****************************************************************************/
+#define che_process(che_context, type, mode, act, source, dest, source_length, final) \
+ \
+ ((void)TRICK_##type, run_##type##_process(che_context, type, mode, act, source, dest, source_length, final, 0))
+
+
+#define che_AES_process(che_context, type, mode, act, source, dest, source_length, final, aes_param) \
+ \
+ ((void)TRICK_##type, run_##type##_process(che_context, type, mode, act, source, dest, source_length, final, 0, aes_param))
+/*****************************************************************************
+ * DESCRIPTION
+ * API used to do the CHE computation. The input message is from the "file".
+ * The usage is the same as che_process except the source of input data.
+ * For detail algorithm usage, please also take the reference of "che_process"
+ * PARAMETERS
+ * che_context : [IN] che context
+ * type : [IN] che type (CHE_TYPE)
+ * mode : [IN] che operation mode (CHE_OPERATION_MODE)
+ * act : [IN] che action (CHE_ACTION)
+ * pstFSAL : [IN] source file handler
+ * dest : [OUT] destination buffer (it has to be 4-byte alignment for DES)
+ * RETURN VALUES
+ * KAL_TRUE : CHE process successful
+ * KAL_FLASE : CHE process fail
+ * SEE ALSO
+ * che_process
+ * EXAMPLE
+ * 1. DES Encryption (CBC mode):
+ * The initial vector and destination address have to be in 4-byte alignment.
+ *
+ * <code>
+ * STFSAL pstFSAL;
+ * kal_uint8 pbBuf[512];
+ * STCHE che_str;
+ * che_init(&che_str, CHE_DES);
+ * che_key_action(&che_str, CHE_SET_KEY, key, key_length);
+ * che_set_iv(&che_str, init_vector, vec_length);
+ * FSAL_Open(&pstFSAL, body_data_filepath, FSAL_READ);
+ * FSAL_SetBuffer(&pstFSAL, 512, pbBuf);
+ * if (che_process_from_file(&che_str, CHE_DES, CHE_CBC, CHE_ENC,
+ * &pstFSAL, dst_buf) == KAL_FALSE)
+ * {
+ * FSAL_Close(&pstFSAL);
+ * che_deinit(&che_str);
+ * return ERROR;
+ * }
+ * ...
+ * FSAL_Close(&pstFSAL);
+ * // do other operations
+ * ...
+ * //Deinitialize when all operations are finish
+ * che_deinit(&che_str);
+ * </code>
+ *
+ * 2. DES Encryption (ECB mode):
+ * <code>
+ * STFSAL pstFSAL;
+ * kal_uint8 pbBuf[512];
+ * STCHE che_str;
+ * che_init(&che_str, CHE_DES);
+ * che_key_action(&che_str, CHE_SET_KEY, key, key_length);
+ * FSAL_Open(&pstFSAL, body_data_filepath, FSAL_READ);
+ * FSAL_SetBuffer(&pstFSAL, 512, pbBuf);
+ * if (che_process_from_file(&che_str, CHE_DES, CHE_ECB, CHE_ENC,
+ * &pstFSAL, dst_buf) == KAL_FALSE)
+ * {
+ * FSAL_Close(&pstFSAL);
+ * che_deinit(&che_str);
+ * return ERROR;
+ * }
+ * ...
+ * FSAL_Close(&pstFSAL);
+ * // do other operations
+ * ...
+ * //Deinitialize when all operations are finish
+ * che_deinit(&che_str);
+ * </code>
+ *
+ * 3. AES Encryption (CBC mode):
+ * <code>
+ * STFSAL pstFSAL;
+ * kal_uint8 pbBuf[512];
+ * STCHE che_str;
+ * che_init(&che_str, CHE_AES);
+ * che_key_action(&che_str, CHE_SET_KEY, key, key_length);
+ * che_set_iv(&che_str, init_vector, vec_length);
+ * FSAL_Open(&pstFSAL, body_data_filepath, FSAL_READ);
+ * FSAL_SetBuffer(&pstFSAL, 512, pbBuf);
+ * if (che_process_from_file(&che_str, CHE_AES, CHE_CBC, CHE_ENC,
+ * &pstFSAL, dst_buf) == KAL_FALSE)
+ * {
+ * FSAL_Close(&pstFSAL);
+ * che_deinit(&che_str);
+ * return ERROR;
+ * }
+ * ...
+ * FSAL_Close(&pstFSAL);
+ * // do other operations
+ * ...
+ * //Deinitialize when all operations are finish
+ * che_deinit(&che_str);
+ * </code>
+ *
+ * 4. MD5 Hash generation:
+ * <code>
+ * STFSAL pstFSAL;
+ * kal_uint8 pbBuf[512];
+ * STCHE che_str;
+ * kal_uint8 digest[16];
+ * che_init(&che_str, CHE_MD5);
+ * FSAL_Open(&pstFSAL, body_data_filepath, FSAL_READ);
+ * FSAL_SetBuffer(&pstFSAL, 512, pbBuf);
+ * if (che_process_from_file(&che_str, CHE_MD5, CHE_MODE_NULL, CHE_HASH,
+ * &pstFSAL, digest) == KAL_FALSE)
+ * {
+ * FSAL_Close(&pstFSAL);
+ * che_deinit(&che_str);
+ * return ERROR;
+ * }
+ * ...
+ * FSAL_Close(&pstFSAL);
+ * // do other operations
+ * ...
+ * //Deinitialize when all operations are finish
+ * che_deinit(&che_str);
+ * </code>
+ *
+ * 5. HMAC MD5 generation:
+ * <code>
+ * STFSAL pstFSAL;
+ * kal_uint8 pbBuf[512];
+ * STCHE che_str;
+ * kal_uint8 digest[16];
+ * che_init(&che_str, CHE_MAC_MD5);
+ * che_key_action(&che_context, CHE_SET_KEY, (kal_uint8 *) key, key_len);
+ * FSAL_Open(&pstFSAL, body_data_filepath, FSAL_READ);
+ * FSAL_SetBuffer(&pstFSAL, 512, pbBuf);
+ * if (che_process_from_file(&che_str, CHE_MAC_MD5, CHE_MODE_NULL, CHE_HASH,
+ * &pstFSAL, digest) == KAL_FALSE)
+ * {
+ * FSAL_Close(&pstFSAL);
+ * che_deinit(&che_str);
+ * return ERROR;
+ * }
+ * ...
+ * FSAL_Close(&pstFSAL);
+ * // do other operations
+ * ...
+ * //Deinitialize when all operations are finish
+ * che_deinit(&che_str);
+ * </code>
+ *
+ * 6. Diffie-Hellman: (key = (G^Y)^X mod P)
+ * get the discrete logarithms (X) and compute self public value (G^X mod P)
+ * <code>
+ * STFSAL pstFSAL;
+ * kal_uint8 pbBuf[512];
+ * STCHE che_str;
+ * che_init(&che_str, CHE_DH);
+ * che_process(&che_ctx_dh, CHE_DH, CHE_MODE_NULL, DH_INIT, NULL, NULL, 0, KAL_TRUE);
+ * // set the P and G that P and G must in binary format
+ * che_key_action(&che_ctx_dh, CHE_SET_DH_P, (kal_uint8*) P, P_Len);
+ * che_key_action(&che_ctx_dh, CHE_SET_DH_G, G, G_Len);
+ * // make self public (G^X mod P) and private (X)
+ * che_process(&che_ctx_dh, CHE_DH, CHE_MODE_NULL, DH_MAKE_PARAMS, NULL, NULL, 0, KAL_TRUE);
+ * // get the self public
+ * che_key_action(&che_ctx_dh, CHE_GET_DH_SELF_PUBLIC_LEN, (void*)&self_public_len, sizeof(int));
+ * // get the self public
+ * *self_private = malloc(self_private_len);
+ * che_key_action(&che_ctx_dh, CHE_GET_DH_PRIVATE, (kal_uint8*) (*priv)->v, dh_private_len);
+ * FSAL_Open(&pstFSAL, body_data_filepath, FSAL_READ);
+ * FSAL_SetBuffer(&pstFSAL, 512, pbBuf);
+ * if (che_process_from_file(&che_str, CHE_MD5, CHE_MODE_NULL, CHE_ENC,
+ * &pstFSAL, digest) == KAL_FALSE)
+ * {
+ * FSAL_Close(&pstFSAL);
+ * che_deinit(&che_str);
+ * return ERROR;
+ * }
+ * ...
+ * FSAL_Close(&pstFSAL);
+ * // do other operations
+ * ...
+ * //Deinitialize when all operations are finish
+ * che_deinit(&che_str);
+ * </code>
+ *****************************************************************************/
+#define che_process_from_file(che_context, type, mode, act, pstFSAL, dest) \
+ \
+ ((void)TRICK_##type, run_##type##_process(che_context, type, mode, act, 0, dest, 0, 0, pstFSAL))
+
+/*****************************************************************************
+ * FUNCTION
+ * che_duplicate
+ * DESCRIPTION
+ * Duplicate the CHE structure and current content.
+ * This API only supports CHE_MD5, CHE_SHA1, CHE_MD4, CHE_MAC_MD5, CHE_MAC_SHA1.
+ * The situation application may encounter and wish to use this API:
+ * packet1
+ * Step 1: A -------------------------> B (B receives packet1 and generates the hash value)
+ * packet2
+ * Step 2: A -------------------------> B (B receives another packet2 and generates the hash value again)
+ *
+ * In this case, application may wish the result of step 2 is the continue of step 1.
+ * To fulfill user's need, we provide this API for user to store the temporary CHE cotext for
+ * later use.
+ * PARAMETERS
+ * src_che_context : [IN] source CHE context
+ * dst_che_context : [IN] destination CHE context
+ *
+ * RETURN VALUES
+ * KAL_TRUE : success
+ * KAL_FALSE : fail
+ * EXAMPLE
+ * <code>
+ * STCHE dst_context;
+ * che_duplicate(&dst_context, &src_context);
+ * </code>
+ *****************************************************************************/
+extern kal_bool che_duplicate(STCHE *src_che_context, STCHE *dst_che_context);
+
+/*****************************************************************************
+ * FUNCTION
+ * che_exc_support_algo
+ * DESCRIPTION
+ * Support which algorithm for exception flow (used by che_calculate_exc_ext)
+ *
+ * PARAMETERS
+ * type [IN]
+ * RETURNS
+ * kal_bool - KAL_TRUE for support, KAL_FALSE - not support
+ *****************************************************************************/
+extern kal_bool che_exc_support_algo(kal_uint32 type);
+
+/*****************************************************************************
+ * FUNCTION
+ * che_calculate_exc_ext
+ * DESCRIPTION
+ * Execute algorithm function for exception flow & normal flow (only support MD5 & HMAC SHA256)
+ *
+ * PARAMETERS
+ * type [IN]
+ * mode [IN]
+ * act [IN]
+ * key [IN]
+ * key_len [IN]
+ * source [IN]
+ * source_length [IN]
+ * dest [OUT]
+ * dest_length [OUT]
+ * RETURNS
+ * CHE_CALCULATE_ERROR - error code
+ *****************************************************************************/
+extern CHE_CALCULATE_ERROR che_calculate_exc_ext(
+ CHE_TYPE type,
+ CHE_OPERATION_MODE mode,
+ CHE_ACTION act,
+ kal_uint8 *key,
+ kal_uint32 key_len,
+ kal_uint8 *source,
+ kal_uint32 source_length,
+ kal_uint8 *dest,
+ kal_uint32 *dest_length);
+
+#if 0
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+#endif
+
+extern void SST_SSF_Init(void);
+
+extern void SST_SSF_Deinit(void);
+
+extern int Base64encode(char *encoded, const char *string, int len);
+
+extern int Base64decode(char *bufplain, const char *bufcoded);
+
+#endif /* _CHE_API_H */
+
+/*
+ * ! \page page3 Reference
+ *
+ * 1) FEDERAL INFORMATION PROCESSING STANDARDS PUBLICATION U.S. DEPARTMENT OF COMMERCE/national Institute of Standards and Technology 46-3 : DATA ENCRYPTION STANDARD (DES)\n
+ * 2) FEDERAL INFORMATION PROCESSING STANDARDS PUBLICATION 197 : ADVANCED ENCRYPTION STANDARD (AES)\n
+ * 3) FEDERAL INFORMATION PROCESSING STANDARDS PUBLICATION 180-2 SECURE HASH STANDARD (SHA)\n
+ * 4) MIT Laboratory for Computer Science and RSA Data Security, Inc. : The MD5 Message-Digest Algorithm\n
+ *
+ */
+
diff --git a/mcu/interface/driver/che/che_ossl_api.h b/mcu/interface/driver/che/che_ossl_api.h
new file mode 100644
index 0000000..8f6f47f
--- /dev/null
+++ b/mcu/interface/driver/che/che_ossl_api.h
@@ -0,0 +1,76 @@
+/*****************************************************************************
+* Copyright Statement:
+* --------------------
+* This software is protected by Copyright and the information contained
+* herein is confidential. The software may not be copied and the information
+* contained herein may not be used or disclosed except with the written
+* permission of MediaTek Inc. (C) 2007
+*
+* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
+* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
+* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
+* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
+* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
+*
+* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
+* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
+* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+*
+* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
+* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
+* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
+* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
+* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
+*
+*****************************************************************************/
+
+/*****************************************************************************
+ *
+ * Filename:
+ * ---------
+ * che_ossl_api.h
+ *
+ * Project:
+ * --------
+ * Maui_Software
+ *
+ * Description:
+ * ------------
+ * Cipher/Hash Engine API.
+ *
+ * Author:
+ * -------
+ * -------
+ *
+ *============================================================================
+ * HISTORY
+ * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
+ *------------------------------------------------------------------------------
+ * removed!
+ *
+ *
+ *------------------------------------------------------------------------------
+ * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
+ *============================================================================
+ ****************************************************************************/
+
+#ifndef __CHE_OSSL_API_H__
+#define __CHE_OSSL_API_H__
+
+#include "kal_public_api.h"
+
+extern kal_int32 PKCS5_PBKDF2_HMAC_SHA256(const kal_char *pass, kal_int32 passlen,
+ const kal_uint8 *salt, kal_int32 saltlen, kal_int32 iter,
+ kal_int32 keylen, kal_uint8 *out);
+
+#endif /* __CHE_OSSL_API_H__ */
+
diff --git a/mcu/interface/driver/che/che_sw_driver.h b/mcu/interface/driver/che/che_sw_driver.h
new file mode 100644
index 0000000..cc10cc2
--- /dev/null
+++ b/mcu/interface/driver/che/che_sw_driver.h
@@ -0,0 +1,253 @@
+/*****************************************************************************
+* Copyright Statement:
+* --------------------
+* This software is protected by Copyright and the information contained
+* herein is confidential. The software may not be copied and the information
+* contained herein may not be used or disclosed except with the written
+* permission of MediaTek Inc. (C) 2007
+*
+* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
+* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
+* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
+* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
+* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
+*
+* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
+* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
+* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+*
+* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
+* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
+* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
+* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
+* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
+*
+*****************************************************************************/
+
+/*****************************************************************************
+ *
+ * Filename:
+ * ---------
+ * che_sw_driver.h
+ *
+ * Project:
+ * --------
+ * Maui_Software
+ *
+ * Description:
+ * ------------
+ * Cipher/Hash Engine software driver.
+ *
+ * Author:
+ * -------
+ * -------
+ *
+ *============================================================================
+ * HISTORY
+ * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
+ *------------------------------------------------------------------------------
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ * removed!
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ *------------------------------------------------------------------------------
+ * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
+ *============================================================================
+ ****************************************************************************/
+
+#ifndef _CHE_SW_Driver_H
+#define _CHE_SW_Driver_H
+
+#include "che_api.h"
+
+
+#if !defined(__CHE_WIN32_STANDALONE__)
+
+#define malloc get_ctrl_buffer
+#define free free_ctrl_buffer
+#endif /* !defined(__CHE_WIN32_STANDALONE__) */
+
+#include "kal_public_api.h"
+#include "kal_general_types.h"
+
+extern kal_bool che_sw_des(
+ STCHE *che_context,
+ CHE_TYPE type,
+ CHE_OPERATION_MODE mode,
+ CHE_ACTION act,
+ kal_uint8 *data_src,
+ kal_uint8 *data_dst,
+ kal_int32 length,
+ kal_bool last_block);
+
+extern kal_bool che_sw_aes(
+ STCHE *che_context,
+ CHE_OPERATION_MODE mode,
+ CHE_ACTION act,
+ kal_uint8 *data_src,
+ kal_uint8 *encryptedData,
+ kal_int32 length,
+ kal_bool last_block,
+ void *aes_param);
+
+extern kal_bool che_sw_md5(
+ STCHE *che_context,
+ kal_uint8 *data_src,
+ kal_uint8 *data_dst,
+ kal_int32 length,
+ kal_bool last_block);
+
+extern kal_bool che_sw_sha_1(
+ STCHE *che_context,
+ kal_uint8 *data_src,
+ kal_uint8 *data_dst,
+ kal_int32 length,
+ kal_bool last_block);
+
+extern kal_bool che_sw_rc4(
+ STCHE *che_context,
+ CHE_OPERATION_MODE mode,
+ kal_uint8 *data_src,
+ kal_uint8 *data_dst,
+ kal_uint32 length,
+ kal_bool last_block);
+
+extern kal_bool che_sw_md4(
+ STCHE *che_context,
+ kal_uint8 *data_src,
+ kal_uint8 *data_dst,
+ kal_int32 length,
+ kal_bool last_block);
+
+extern kal_bool che_sw_mac(
+ CHE_TYPE type,
+ STCHE *che_context,
+ kal_uint8 *data_src,
+ kal_uint8 *data_dst,
+ kal_int32 length,
+ kal_bool last_block);
+
+extern kal_bool che_sw_rsa_key_from_string(
+ STCHE *che_context,
+ CHE_ACTION act,
+ kal_uint8 *data_src,
+ kal_uint8 *data_dst,
+ kal_int32 length,
+ kal_bool last_block);
+
+extern kal_bool che_sw_dh(
+ STCHE *che_context,
+ CHE_ACTION act,
+ kal_uint8 *data_src,
+ kal_uint8 *data_dst,
+ kal_int32 length,
+ kal_bool last_block);
+
+#if 0
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+#endif
+
+#if !defined(__CHE_NO_SHA256__)
+
+extern kal_bool che_sw_sha_256(
+ STCHE *che_context,
+ kal_uint8 *data_src,
+ kal_uint8 *data_dst,
+ kal_int32 length,
+ kal_bool last_block);
+
+extern kal_bool che_sw_sha_224(
+ STCHE *che_context,
+ kal_uint8 *data_src,
+ kal_uint8 *data_dst,
+ kal_int32 length,
+ kal_bool last_block);
+
+#endif
+#endif /* _CHE_SW_Driver_H */
+
diff --git a/mcu/interface/driver/che/che_trick.h b/mcu/interface/driver/che/che_trick.h
new file mode 100644
index 0000000..843fe73
--- /dev/null
+++ b/mcu/interface/driver/che/che_trick.h
@@ -0,0 +1,446 @@
+/*****************************************************************************
+* Copyright Statement:
+* --------------------
+* This software is protected by Copyright and the information contained
+* herein is confidential. The software may not be copied and the information
+* contained herein may not be used or disclosed except with the written
+* permission of MediaTek Inc. (C) 2007
+*
+* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
+* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
+* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
+* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
+* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
+*
+* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
+* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
+* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+*
+* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
+* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
+* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
+* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
+* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
+*
+*****************************************************************************/
+
+/*****************************************************************************
+ *
+ * Filename:
+ * ---------
+ * che_trick.h
+ *
+ * Project:
+ * --------
+ * Maui_Software
+ *
+ * Description:
+ * ------------
+ * CHE Trick define.
+ *
+ * Author:
+ * -------
+ * -------
+ *
+ *============================================================================
+ * HISTORY
+ * Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
+ *------------------------------------------------------------------------------
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ * removed!
+ * removed!
+ * removed!
+ *
+ *------------------------------------------------------------------------------
+ * Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
+ *============================================================================
+ ****************************************************************************/
+
+#ifndef __CHE_TRICK_H__
+#define __CHE_TRICK_H__
+
+#define TRICK_CHE_MD5 1
+#define TRICK_CHE_SHA224 1
+#define TRICK_CHE_SHA256 1
+//#define TRICK_CHE_SHA1 1
+#define TRICK_CHE_DES 1
+#define TRICK_CHE_3DES 1
+#define TRICK_CHE_AES 1
+//#define TRICK_CHE_MD4 1
+#define TRICK_CHE_MAC_MD5 1
+//#define TRICK_CHE_MAC_SHA1 1
+#define TRICK_CHE_MAC_SHA224 1
+#define TRICK_CHE_MAC_SHA256 1
+#define TRICK_CHE_RC4 1
+//#define TRICK_CHE_RSA 1
+#define TRICK_CHE_DH 1
+#define TRICK_CHE_GEN_KEY 1
+#define TRICK_CHE_SET_KEY 1
+#define TRICK_CHE_SET_RC4_KEY 1
+#define TRICK_CHE_GET_KEY 1
+#define TRICK_CHE_DEL_KEY 1
+/*#define TRICK_CHE_GEN_RSA_KEY 1
+#define TRICK_CHE_DEL_RSA_KEY 1
+#define TRICK_CHE_SET_RSA_N 1
+#define TRICK_CHE_SET_RSA_E 1
+#define TRICK_CHE_SET_RSA_D 1
+#define TRICK_CHE_SET_RSA_P 1
+#define TRICK_CHE_SET_RSA_Q 1
+#define TRICK_CHE_SET_RSA_dModPm1 1
+#define TRICK_CHE_SET_RSA_dModQm1 1
+#define TRICK_CHE_SET_RSA_qInvModP 1 */
+#define TRICK_CHE_SET_DH_P 1
+#define TRICK_CHE_SET_DH_G 1
+#define TRICK_CHE_SET_DH_SELF_PUBLIC 1
+#define TRICK_CHE_SET_DH_PEER_PUBLIC 1
+#define TRICK_CHE_SET_DH_PRIVATE 1
+#define TRICK_CHE_GET_DH_SELF_PUBLIC_LEN 1
+#define TRICK_CHE_GET_DH_PRIVATE_LEN 1
+#define TRICK_CHE_GET_DH_SECRET_LEN 1
+#define TRICK_CHE_GET_DH_SELF_PUBLIC 1
+#define TRICK_CHE_GET_DH_PRIVATE 1
+#define TRICK_CHE_GET_DH_SECRET 1
+/*#define TRICK_CHE_GET_RSA_N 1
+#define TRICK_CHE_GET_RSA_N_LEN 1
+#define TRICK_CHE_GET_RSA_E 1
+#define TRICK_CHE_GET_RSA_E_LEN 1
+#define TRICK_CHE_GET_RSA_D 1
+#define TRICK_CHE_GET_RSA_D_LEN 1
+#define TRICK_CHE_GET_RSA_P 1
+#define TRICK_CHE_GET_RSA_P_LEN 1
+#define TRICK_CHE_GET_RSA_Q 1
+#define TRICK_CHE_GET_RSA_Q_LEN 1
+#define TRICK_CHE_GET_RSA_dModPm1 1
+#define TRICK_CHE_GET_RSA_dModPm1_LEN 1
+#define TRICK_CHE_GET_RSA_dModQm1 1
+#define TRICK_CHE_GET_RSA_dModQm1_LEN 1
+#define TRICK_CHE_GET_RSA_qInvModP 1
+#define TRICK_CHE_GET_RSA_qInvModP_LEN 1*/
+
+/* check it before execute key action functions */
+extern void checkCHEKeyActionPerFix(STCHE *che_context, kal_uint8 *key);
+
+/* execute GEN_KEY action */
+extern void run_CHE_GEN_KEY_process(STCHE *che_context, kal_uint8 *key, kal_int32 key_len);
+
+/* execute SET_KEY action */
+extern void run_CHE_SET_KEY_process(STCHE *che_context, kal_uint8 *key, kal_int32 key_len);
+
+/* execute SET_RC4_KEY action */
+extern void run_CHE_SET_RC4_KEY_process(STCHE *che_context, kal_uint8 *key, kal_int32 key_len);
+
+/* execute GET_KEY action */
+extern void run_CHE_GET_KEY_process(STCHE *che_context, kal_uint8 *key, kal_int32 key_len);
+
+/* execute DEL_KEY action */
+extern void run_CHE_DEL_KEY_process(STCHE *che_context, kal_uint8 *key, kal_int32 key_len);
+
+#if 0
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+#endif
+
+/* execute SET_DH_P key action */
+extern void run_CHE_SET_DH_P_process(STCHE *che_context, kal_uint8 *key, kal_int32 key_len);
+
+/* execute SET_DH_G key action */
+extern void run_CHE_SET_DH_G_process(STCHE *che_context, kal_uint8 *key, kal_int32 key_len);
+
+/* execute SET_DH_SELF key action */
+extern void run_CHE_SET_DH_SELF_PUBLIC_process(STCHE *che_context, kal_uint8 *key, kal_int32 key_len);
+
+/* execute SET_DH_PEER_PUBLIC key action */
+extern void run_CHE_SET_DH_PEER_PUBLIC_process(STCHE *che_context, kal_uint8 *key, kal_int32 key_len);
+
+/* execute SET_DH_PRIVATE key action */
+extern void run_CHE_SET_DH_PRIVATE_process(STCHE *che_context, kal_uint8 *key, kal_int32 key_len);
+
+/* execute GET_DH_SELF_PUBLIC_LEN key action */
+extern void run_CHE_GET_DH_SELF_PUBLIC_LEN_process(STCHE *che_context, kal_uint8 *key, kal_int32 key_len);
+
+/* execute GET_DH_SELF_PUBLIC key action */
+extern void run_CHE_GET_DH_SELF_PUBLIC_process(STCHE *che_context, kal_uint8 *key, kal_int32 key_len);
+
+/* execute GET_DH_PRIVATE_LEN action */
+extern void run_CHE_GET_DH_PRIVATE_LEN_process(STCHE *che_context, kal_uint8 *key, kal_int32 key_len);
+
+/* execute CHE_GET_DH_PRIVATE key action */
+extern void run_CHE_GET_DH_PRIVATE_process(STCHE *che_context, kal_uint8 *key, kal_int32 key_len);
+
+/* execute CHE_GET_DH_SECRET_LEN action */
+extern void run_CHE_GET_DH_SECRET_LEN_process(STCHE *che_context, kal_uint8 *key, kal_int32 key_len);
+
+/* execute CHE_GET_DH_SECRET key action */
+extern void run_CHE_GET_DH_SECRET_process(STCHE *che_context, kal_uint8 *key, kal_int32 key_len);
+
+/* check it before execute cipher/hash functions */
+extern void checkCHEProcessPerFix(STCHE *che_context);
+
+/* check it after execute cipher/hash functions */
+extern void checkCHEProcessPostfix(STCHE *che_context, kal_bool last_block);
+
+/* execute DES cipher function */
+extern kal_bool run_CHE_DES_process(
+ STCHE *che_context,
+ CHE_TYPE type,
+ CHE_OPERATION_MODE mode,
+ CHE_ACTION act,
+ kal_uint8 *source,
+ kal_uint8 *dest,
+ kal_uint32 source_length,
+ kal_bool last_block,
+ STFSAL *pstFSAL);
+
+/* execute 3DES cipher function */
+extern kal_bool run_CHE_3DES_process(
+ STCHE *che_context,
+ CHE_TYPE type,
+ CHE_OPERATION_MODE mode,
+ CHE_ACTION act,
+ kal_uint8 *source,
+ kal_uint8 *dest,
+ kal_uint32 source_length,
+ kal_bool last_block,
+ STFSAL *pstFSAL);
+
+/* execute AES cipher function */
+extern kal_bool run_CHE_AES_process(
+ STCHE *che_context,
+ CHE_TYPE type,
+ CHE_OPERATION_MODE mode,
+ CHE_ACTION act,
+ kal_uint8 *source,
+ kal_uint8 *dest,
+ kal_uint32 source_length,
+ kal_bool last_block,
+ STFSAL *pstFSAL,
+ void *aes_param);
+
+/* execute MD5 hash function */
+extern kal_bool run_CHE_MD5_process(
+ STCHE *che_context,
+ CHE_TYPE type,
+ CHE_OPERATION_MODE mode,
+ CHE_ACTION act,
+ kal_uint8 *source,
+ kal_uint8 *dest,
+ kal_uint32 source_length,
+ kal_bool last_block,
+ STFSAL *pstFSAL);
+
+/* execute SHA1 hash function */
+/*extern kal_bool run_CHE_SHA1_process(
+ STCHE *che_context,
+ CHE_TYPE type,
+ CHE_OPERATION_MODE mode,
+ CHE_ACTION act,
+ kal_uint8 *source,
+ kal_uint8 *dest,
+ kal_uint32 source_length,
+ kal_bool last_block,
+ STFSAL *pstFSAL);*/
+
+extern kal_bool run_CHE_SHA224_process(
+ STCHE *che_context,
+ CHE_TYPE type,
+ CHE_OPERATION_MODE mode,
+ CHE_ACTION act,
+ kal_uint8 *source,
+ kal_uint8 *dest,
+ kal_uint32 source_length,
+ kal_bool last_block,
+ STFSAL *pstFSAL);
+
+extern kal_bool run_CHE_SHA256_process(
+ STCHE *che_context,
+ CHE_TYPE type,
+ CHE_OPERATION_MODE mode,
+ CHE_ACTION act,
+ kal_uint8 *source,
+ kal_uint8 *dest,
+ kal_uint32 source_length,
+ kal_bool last_block,
+ STFSAL *pstFSAL);
+
+/* / execute RC4 cipher function */
+extern kal_bool run_CHE_RC4_process(
+ STCHE *che_context,
+ CHE_TYPE type,
+ CHE_OPERATION_MODE mode,
+ CHE_ACTION act,
+ kal_uint8 *source,
+ kal_uint8 *dest,
+ kal_uint32 source_length,
+ kal_bool last_block,
+ STFSAL *pstFSAL);
+
+/* execute MD4 hash function */
+/*extern kal_bool run_CHE_MD4_process(
+ STCHE *che_context,
+ CHE_TYPE type,
+ CHE_OPERATION_MODE mode,
+ CHE_ACTION act,
+ kal_uint8 *source,
+ kal_uint8 *dest,
+ kal_uint32 source_length,
+ kal_bool last_block,
+ STFSAL *pstFSAL);*/
+
+/* / execute MAC_MD5 hash function */
+extern kal_bool run_CHE_MAC_MD5_process(
+ STCHE *che_context,
+ CHE_TYPE type,
+ CHE_OPERATION_MODE mode,
+ CHE_ACTION act,
+ kal_uint8 *source,
+ kal_uint8 *dest,
+ kal_uint32 source_length,
+ kal_bool last_block,
+ STFSAL *pstFSAL);
+
+/* execute MAC_SHA1 hash function */
+/*extern kal_bool run_CHE_MAC_SHA1_process(
+ STCHE *che_context,
+ CHE_TYPE type,
+ CHE_OPERATION_MODE mode,
+ CHE_ACTION act,
+ kal_uint8 *source,
+ kal_uint8 *dest,
+ kal_uint32 source_length,
+ kal_bool last_block,
+ STFSAL *pstFSAL);*/
+
+extern kal_bool run_CHE_MAC_SHA256_process(
+ STCHE *che_context,
+ CHE_TYPE type,
+ CHE_OPERATION_MODE mode,
+ CHE_ACTION act,
+ kal_uint8 *source,
+ kal_uint8 *dest,
+ kal_uint32 source_length,
+ kal_bool last_block,
+ STFSAL *pstFSAL);
+
+extern kal_bool run_CHE_MAC_SHA224_process(
+ STCHE *che_context,
+ CHE_TYPE type,
+ CHE_OPERATION_MODE mode,
+ CHE_ACTION act,
+ kal_uint8 *source,
+ kal_uint8 *dest,
+ kal_uint32 source_length,
+ kal_bool last_block,
+ STFSAL *pstFSAL);
+
+/* execute DH cipher function */
+extern kal_bool run_CHE_DH_process(
+ STCHE *che_context,
+ CHE_TYPE type,
+ CHE_OPERATION_MODE mode,
+ CHE_ACTION act,
+ kal_uint8 *source,
+ kal_uint8 *dest,
+ kal_uint32 source_length,
+ kal_bool last_block,
+ STFSAL *pstFSAL);
+
+#endif /* __CHE_TRICK_H__ */
+
diff --git a/mcu/interface/driver/che/ossl_bn.h b/mcu/interface/driver/che/ossl_bn.h
new file mode 100644
index 0000000..5f43a90
--- /dev/null
+++ b/mcu/interface/driver/che/ossl_bn.h
@@ -0,0 +1,595 @@
+/* crypto/bn/bn.h */
+/* Copyright (C) 1995-1997 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.]
+ */
+/* ====================================================================
+ * Copyright (c) 1998-2006 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 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ *
+ * Portions of the attached software ("Contribution") are developed by
+ * SUN MICROSYSTEMS, INC., and are contributed to the OpenSSL project.
+ *
+ * The Contribution is licensed pursuant to the Eric Young open source
+ * license provided above.
+ *
+ * The binary polynomial arithmetic software is originally written by
+ * Sheueling Chang Shantz and Douglas Stebila of Sun Microsystems Laboratories.
+ *
+ */
+
+#ifndef HEADER_BN_H
+#define HEADER_BN_H
+
+#include "ossl_typ.h"
+#include "kal_public_defs.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/* These preprocessor symbols control various aspects of the bignum headers and
+ * library code. They're not defined by any "normal" configuration, as they are
+ * intended for development and testing purposes. NB: defining all three can be
+ * useful for debugging application code as well as openssl itself.
+ *
+ * BN_DEBUG - turn on various debugging alterations to the bignum code
+ * BN_DEBUG_RAND - uses random poisoning of unused words to trip up
+ * mismanagement of bignum internals. You must also define BN_DEBUG.
+ */
+/* #define BN_DEBUG */
+/* #define BN_DEBUG_RAND */
+
+#if 0
+#ifndef OPENSSL_SMALL_FOOTPRINT
+/* under construction !*/
+/* under construction !*/
+/* under construction !*/
+#endif
+#endif
+
+
+//#ifdef THIRTY_TWO_BIT
+#define BN_ULLONG unsigned long long
+#define BN_MASK (0xffffffffffffffffLL)
+
+#define BN_ULONG kal_uint32 // Raymomd
+#define BN_BITS 64
+#define BN_BYTES 4
+#define BN_BITS2 32
+#define BN_BITS4 16
+#define BN_MASK2 (0xffffffffL)
+#define BN_MASK2l (0xffff)
+#define BN_MASK2h1 (0xffff8000L)
+#define BN_MASK2h (0xffff0000L)
+#define BN_TBIT (0x80000000L)
+
+#define BN_FLG_MALLOCED 0x01
+#define BN_FLG_STATIC_DATA 0x02
+#define BN_FLG_CONSTTIME 0x04 /* avoid leaking exponent information through timing,
+ * BN_mod_exp_mont() will call BN_mod_exp_mont_consttime,
+ * BN_div() will call BN_div_no_branch,
+ * BN_mod_inverse() will call BN_mod_inverse_no_branch.
+ */
+#define BN_FLG_SECURE 0x08
+
+//#endif
+
+
+#define PTR_SIZE_INT size_t
+
+
+#ifndef OPENSSL_NO_DEPRECATED
+#define BN_FLG_FREE 0x8000 /* used for debuging */
+#endif
+#define BN_set_flags(b,n) ((b)->flags|=(n))
+#define BN_get_flags(b,n) ((b)->flags&(n))
+
+/* get a clone of a BIGNUM with changed flags, for *temporary* use only
+ * (the two BIGNUMs cannot not be used in parallel!) */
+#define BN_with_flags(dest,b,n) ((dest)->d=(b)->d, \
+ (dest)->top=(b)->top, \
+ (dest)->dmax=(b)->dmax, \
+ (dest)->neg=(b)->neg, \
+ (dest)->flags=(((dest)->flags & BN_FLG_MALLOCED) \
+ | ((b)->flags & ~BN_FLG_MALLOCED) \
+ | BN_FLG_STATIC_DATA \
+ | (n)))
+
+struct bignum_st
+ {
+ BN_ULONG *d; /* Pointer to an array of 'BN_BITS2' bit chunks. */
+ int top; /* Index of last used d +1. */
+ /* The next are internal book keeping for bn_expand. */
+ int dmax; /* Size of the d array. */
+ int neg; /* one if the number is negative */
+ int flags;
+ };
+
+/* Used for montgomery multiplication */
+struct bn_mont_ctx_st
+ {
+ int ri; /* number of bits in R */
+ BIGNUM RR; /* used to convert to montgomery form */
+ BIGNUM N; /* The modulus */
+ BIGNUM Ni; /* R*(1/R mod N) - N*Ni = 1
+ * (Ni is only stored for bignum algorithm) */
+ BN_ULONG n0[2];/* least significant word(s) of Ni;
+ (type changed with 0.9.9, was "BN_ULONG n0;" before) */
+ int flags;
+ };
+
+/*
+ * Used for reciprocal division/mod functions It cannot be shared between
+ * threads
+ */
+struct bn_recp_ctx_st {
+ BIGNUM N; /* the divisor */
+ BIGNUM Nr; /* the reciprocal */
+ int num_bits;
+ int shift;
+ int flags;
+};
+
+/* Used for slow "generation" functions. */
+struct bn_gencb_st
+ {
+ unsigned int ver; /* To handle binary (in)compatibility */
+ void *arg; /* callback-specific data */
+ union
+ {
+ /* if(ver==1) - handles old style callbacks */
+ void (*cb_1)(int, int, void *);
+ /* if(ver==2) - new callback style */
+ int (*cb_2)(int, int, BN_GENCB *);
+ } cb;
+ };
+
+
+/* number of Miller-Rabin iterations for an error rate of less than 2^-80
+ * for random 'b'-bit input, b >= 100 (taken from table 4.4 in the Handbook
+ * of Applied Cryptography [Menezes, van Oorschot, Vanstone; CRC Press 1996];
+ * original paper: Damgaard, Landrock, Pomerance: Average case error estimates
+ * for the strong probable prime test. -- Math. Comp. 61 (1993) 177-194) */
+#define BN_prime_checks_for_size(b) ((b) >= 1300 ? 2 : \
+ (b) >= 850 ? 3 : \
+ (b) >= 650 ? 4 : \
+ (b) >= 550 ? 5 : \
+ (b) >= 450 ? 6 : \
+ (b) >= 400 ? 7 : \
+ (b) >= 350 ? 8 : \
+ (b) >= 300 ? 9 : \
+ (b) >= 250 ? 12 : \
+ (b) >= 200 ? 15 : \
+ (b) >= 150 ? 18 : \
+ /* b >= 100 */ 27)
+
+#define BN_num_bytes(a) ((BN_num_bits(a)+7)/8)
+
+/* Note that BN_abs_is_word didn't work reliably for w == 0 until 0.9.8 */
+#define BN_abs_is_word(a,w) ((((a)->top == 1) && ((a)->d[0] == (BN_ULONG)(w))) || \
+ (((w) == 0) && ((a)->top == 0)))
+#define BN_is_zero(a) ((a)->top == 0)
+#define BN_is_one(a) (BN_abs_is_word((a),1) && !(a)->neg)
+#define BN_is_word(a,w) (BN_abs_is_word((a),(w)) && (!(w) || !(a)->neg))
+#define BN_is_odd(a) (((a)->top > 0) && ((a)->d[0] & 1))
+
+#define BN_one(a) (BN_set_word((a),1))
+#define BN_zero_ex(a) \
+ do { \
+ BIGNUM *_tmp_bn = (a); \
+ _tmp_bn->top = 0; \
+ _tmp_bn->neg = 0; \
+ } while(0)
+
+#ifdef OPENSSL_NO_DEPRECATED
+#define BN_zero(a) BN_zero_ex(a)
+#else
+#define BN_zero(a) (BN_set_word((a),0))
+#endif
+#define BN_prime_checks 0 /* default: select number of iterations
+ based on the size of the number */
+
+const BIGNUM *BN_value_one(void);
+BN_CTX *BN_CTX_new(void);
+BN_CTX *BN_CTX_secure_new(void);
+#ifndef OPENSSL_NO_DEPRECATED
+void BN_CTX_init(BN_CTX *c);
+#endif
+void BN_CTX_free(BN_CTX *c);
+void BN_CTX_start(BN_CTX *ctx);
+BIGNUM *BN_CTX_get(BN_CTX *ctx);
+void BN_CTX_end(BN_CTX *ctx);
+int BN_num_bits(const BIGNUM *a);
+int BN_num_bits_word(BN_ULONG);
+BIGNUM *BN_new(void);
+void BN_init(BIGNUM *);
+void BN_clear_free(BIGNUM *a);
+BIGNUM *BN_copy(BIGNUM *a, const BIGNUM *b);
+void BN_swap(BIGNUM *a, BIGNUM *b);
+BIGNUM *BN_bin2bn(const unsigned char *s,int len,BIGNUM *ret);
+int BN_bn2bin(const BIGNUM *a, unsigned char *to);
+int BN_bn2binpad(const BIGNUM *a, unsigned char *to, int tolen);
+int BN_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
+int BN_usub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
+int BN_uadd(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
+int BN_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b);
+int BN_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
+int BN_sqr(BIGNUM *r, const BIGNUM *a,BN_CTX *ctx);
+int BN_rand_range(BIGNUM *r, const BIGNUM *range);
+BIGNUM *BN_secure_new(void);
+
+/** BN_set_negative sets sign of a BIGNUM
+ * \param b pointer to the BIGNUM object
+ * \param n 0 if the BIGNUM b should be positive and a value != 0 otherwise
+ */
+void BN_set_negative(BIGNUM *b, int n);
+/** BN_is_negative returns 1 if the BIGNUM is negative
+ * \param a pointer to the BIGNUM object
+ * \return 1 if a < 0 and 0 otherwise
+ */
+#define BN_is_negative(a) ((a)->neg != 0)
+
+int BN_div(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m, const BIGNUM *d,
+ BN_CTX *ctx);
+#define BN_mod(rem,m,d,ctx) BN_div(NULL,(rem),(m),(d),(ctx))
+int BN_nnmod(BIGNUM *r, const BIGNUM *m, const BIGNUM *d, BN_CTX *ctx);
+int BN_mod_add(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx);
+int BN_mod_add_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m);
+int BN_mod_sub(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m, BN_CTX *ctx);
+int BN_mod_sub_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *b, const BIGNUM *m);
+int BN_mod_mul(BIGNUM *r, const BIGNUM *a, const BIGNUM *b,
+ const BIGNUM *m, BN_CTX *ctx);
+int BN_mod_sqr(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
+int BN_mod_lshift1(BIGNUM *r, const BIGNUM *a, const BIGNUM *m, BN_CTX *ctx);
+int BN_mod_lshift1_quick(BIGNUM *r, const BIGNUM *a, const BIGNUM *m);
+int BN_mod_lshift(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m, BN_CTX *ctx);
+int BN_mod_lshift_quick(BIGNUM *r, const BIGNUM *a, int n, const BIGNUM *m);
+
+BN_ULONG BN_mod_word(const BIGNUM *a, BN_ULONG w);
+BN_ULONG BN_div_word(BIGNUM *a, BN_ULONG w);
+int BN_mul_word(BIGNUM *a, BN_ULONG w);
+int BN_add_word(BIGNUM *a, BN_ULONG w);
+int BN_sub_word(BIGNUM *a, BN_ULONG w);
+int BN_set_word(BIGNUM *a, BN_ULONG w);
+BN_ULONG BN_get_word(const BIGNUM *a);
+
+int BN_cmp(const BIGNUM *a, const BIGNUM *b);
+void BN_free(BIGNUM *a);
+int BN_is_bit_set(const BIGNUM *a, int n);
+int BN_lshift(BIGNUM *r, const BIGNUM *a, int n);
+int BN_lshift1(BIGNUM *r, const BIGNUM *a);
+int BN_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,BN_CTX *ctx);
+
+int BN_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
+ const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
+
+int BN_mask_bits(BIGNUM *a,int n);
+
+int BN_rshift(BIGNUM *r, const BIGNUM *a, int n);
+int BN_rshift1(BIGNUM *r, const BIGNUM *a);
+void BN_clear(BIGNUM *a);
+BIGNUM *BN_dup(const BIGNUM *a);
+int BN_ucmp(const BIGNUM *a, const BIGNUM *b);
+int BN_set_bit(BIGNUM *a, int n);
+int BN_clear_bit(BIGNUM *a, int n);
+int BN_gcd(BIGNUM *r,const BIGNUM *a,const BIGNUM *b,BN_CTX *ctx);
+BIGNUM *BN_mod_inverse(BIGNUM *ret,
+ const BIGNUM *a, const BIGNUM *n,BN_CTX *ctx);
+
+void BN_consttime_swap(BN_ULONG swap, BIGNUM *a, BIGNUM *b, int nwords);
+
+/* Deprecated versions */
+/*#ifndef OPENSSL_NO_DEPRECATED
+BIGNUM *BN_generate_prime(BIGNUM *ret,int bits,int safe,
+ const BIGNUM *add, const BIGNUM *rem,
+ void (*callback)(int,int,void *),void *cb_arg);
+int BN_is_prime(const BIGNUM *p,int nchecks,
+ void (*callback)(int,int,void *),
+ BN_CTX *ctx,void *cb_arg);
+int BN_is_prime_fasttest(const BIGNUM *p,int nchecks,
+ void (*callback)(int,int,void *),BN_CTX *ctx,void *cb_arg,
+ int do_trial_division);
+#endif*/ /* !defined(OPENSSL_NO_DEPRECATED) */
+
+/* Newer versions */
+/*int BN_generate_prime_ex(BIGNUM *ret,int bits,int safe, const BIGNUM *add,
+ const BIGNUM *rem, BN_GENCB *cb);
+int BN_is_prime_ex(const BIGNUM *p,int nchecks, BN_CTX *ctx, BN_GENCB *cb);
+int BN_is_prime_fasttest_ex(const BIGNUM *p,int nchecks, BN_CTX *ctx,
+ int do_trial_division, BN_GENCB *cb);
+
+int BN_X931_generate_Xpq(BIGNUM *Xp, BIGNUM *Xq, int nbits, BN_CTX *ctx);
+
+int BN_X931_derive_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
+ const BIGNUM *Xp, const BIGNUM *Xp1, const BIGNUM *Xp2,
+ const BIGNUM *e, BN_CTX *ctx, BN_GENCB *cb);
+int BN_X931_generate_prime_ex(BIGNUM *p, BIGNUM *p1, BIGNUM *p2,
+ BIGNUM *Xp1, BIGNUM *Xp2,
+ const BIGNUM *Xp,
+ const BIGNUM *e, BN_CTX *ctx,
+ BN_GENCB *cb);
+*/
+BN_MONT_CTX *BN_MONT_CTX_new(void );
+void BN_MONT_CTX_init(BN_MONT_CTX *ctx);
+int BN_mod_mul_montgomery(BIGNUM *r,const BIGNUM *a,const BIGNUM *b,
+ BN_MONT_CTX *mont, BN_CTX *ctx);
+#define BN_to_montgomery(r,a,mont,ctx) BN_mod_mul_montgomery(\
+ (r),(a),&((mont)->RR),(mont),(ctx))
+int BN_from_montgomery(BIGNUM *r,const BIGNUM *a,
+ BN_MONT_CTX *mont, BN_CTX *ctx);
+void BN_MONT_CTX_free(BN_MONT_CTX *mont);
+int BN_MONT_CTX_set(BN_MONT_CTX *mont,const BIGNUM *mod,BN_CTX *ctx);
+BN_MONT_CTX *BN_MONT_CTX_copy(BN_MONT_CTX *to,BN_MONT_CTX *from);
+BN_MONT_CTX *BN_MONT_CTX_set_locked(BN_MONT_CTX **pmont, kal_mutexid mutexid,
+ const BIGNUM *mod, BN_CTX *ctx);
+
+void BN_RECP_CTX_init(BN_RECP_CTX *recp);
+BN_RECP_CTX *BN_RECP_CTX_new(void);
+void BN_RECP_CTX_free(BN_RECP_CTX *recp);
+int BN_RECP_CTX_set(BN_RECP_CTX *recp, const BIGNUM *d, BN_CTX *ctx);
+int BN_mod_mul_reciprocal(BIGNUM *r, const BIGNUM *x, const BIGNUM *y,
+BN_RECP_CTX *recp, BN_CTX *ctx);
+int BN_div_recp(BIGNUM *dv, BIGNUM *rem, const BIGNUM *m,
+BN_RECP_CTX *recp, BN_CTX *ctx);
+int BN_reciprocal(BIGNUM *r, const BIGNUM *m, int len, BN_CTX *ctx);
+int BN_mod_exp_mont(BIGNUM *rr, const BIGNUM *a, const BIGNUM *p,
+const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont);
+BIGNUM *BN_mod_sqrt(BIGNUM *in, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
+int BN_kronecker(const BIGNUM *a, const BIGNUM *b, BN_CTX *ctx);
+int BN_mod_exp_mont_word(BIGNUM *rr, BN_ULONG a, const BIGNUM *p,
+const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *in_mont);
+int BN_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx);
+int BN_pseudo_rand(BIGNUM *rnd, int bits, int top, int bottom);
+
+/* library internal functions */
+
+#define bn_expand(a,bits) ((((((bits+BN_BITS2-1))/BN_BITS2)) <= (a)->dmax)?\
+ (a):bn_expand2((a),(bits+BN_BITS2-1)/BN_BITS2))
+#define bn_wexpand(a,words) (((words) <= (a)->dmax)?(a):bn_expand2((a),(words)))
+BIGNUM *bn_expand2(BIGNUM *a, int words);
+
+#ifdef BN_DEBUG
+
+/* We only need assert() when debugging */
+#include <assert.h>
+
+#ifdef BN_DEBUG_RAND
+/* To avoid "make update" cvs wars due to BN_DEBUG, use some tricks */
+#ifndef RAND_pseudo_bytes
+int RAND_pseudo_bytes(unsigned char *buf,int num);
+#define BN_DEBUG_TRIX
+#endif
+#define bn_pollute(a) \
+ do { \
+ const BIGNUM *_bnum1 = (a); \
+ if(_bnum1->top < _bnum1->dmax) { \
+ unsigned char _tmp_char; \
+ /* We cast away const without the compiler knowing, any \
+ * *genuinely* constant variables that aren't mutable \
+ * wouldn't be constructed with top!=dmax. */ \
+ BN_ULONG *_not_const; \
+ memcpy(&_not_const, &_bnum1->d, sizeof(BN_ULONG*)); \
+ RAND_pseudo_bytes(&_tmp_char, 1); \
+ memset((unsigned char *)(_not_const + _bnum1->top), _tmp_char, \
+ (_bnum1->dmax - _bnum1->top) * sizeof(BN_ULONG)); \
+ } \
+ } while(0)
+#ifdef BN_DEBUG_TRIX
+#undef RAND_pseudo_bytes
+#endif
+#else
+#define bn_pollute(a)
+#endif
+#define bn_check_top(a) \
+ do { \
+ const BIGNUM *_bnum2 = (a); \
+ if (_bnum2 != NULL) { \
+ assert((_bnum2->top == 0) || \
+ (_bnum2->d[_bnum2->top - 1] != 0)); \
+ bn_pollute(_bnum2); \
+ } \
+ } while(0)
+
+#define bn_fix_top(a) bn_check_top(a)
+
+#define bn_check_size(bn, bits) bn_wcheck_size(bn, ((bits+BN_BITS2-1))/BN_BITS2)
+#define bn_wcheck_size(bn, words) \
+ do { \
+ const BIGNUM *_bnum2 = (bn); \
+ assert(words <= (_bnum2)->dmax && words >= (_bnum2)->top); \
+ } while(0)
+
+#else /* !BN_DEBUG */
+
+#define bn_pollute(a)
+#define bn_check_top(a)
+#define bn_fix_top(a) bn_correct_top(a)
+#define bn_check_size(bn, bits)
+#define bn_wcheck_size(bn, words)
+
+#endif
+
+
+#define bn_correct_top(a) \
+ { \
+ BN_ULONG *ftl; \
+ int tmp_top = (a)->top; \
+ if (tmp_top > 0) \
+ { \
+ for (ftl= &((a)->d[tmp_top-1]); tmp_top > 0; tmp_top--) \
+ if (*(ftl--)) break; \
+ (a)->top = tmp_top; \
+ } \
+ bn_pollute(a); \
+ }
+
+
+BN_ULONG bn_mul_add_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
+BN_ULONG bn_mul_words(BN_ULONG *rp, const BN_ULONG *ap, int num, BN_ULONG w);
+void bn_sqr_words(BN_ULONG *rp, const BN_ULONG *ap, int num);
+BN_ULONG bn_div_words(BN_ULONG h, BN_ULONG l, BN_ULONG d);
+BN_ULONG bn_add_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,int num);
+BN_ULONG bn_sub_words(BN_ULONG *rp, const BN_ULONG *ap, const BN_ULONG *bp,int num);
+
+signed char *bn_compute_wNAF(const BIGNUM *scalar, int w, size_t *ret_len);
+int bn_get_top(const BIGNUM *a);
+BN_ULONG *bn_get_words(const BIGNUM *a);
+int bn_copy_words(BN_ULONG *out, const BIGNUM *in, int size);
+int bn_set_words(BIGNUM *a, const BN_ULONG *words, int num_words);
+int (*BN_nist_mod_func(const BIGNUM *p)) (BIGNUM *r, const BIGNUM *a,
+ const BIGNUM *field, BN_CTX *ctx);
+
+int BN_nist_mod_256(BIGNUM *r, const BIGNUM *a, const BIGNUM *p, BN_CTX *ctx);
+const BIGNUM *BN_get0_nist_prime_256(void);
+
+
+/* Error codes for the BN functions. */
+
+/* Function codes. */
+#define BN_F_BNRAND 127
+#define BN_F_BN_CTX_GET 116
+#define BN_F_BN_CTX_NEW 106
+#define BN_F_BN_CTX_START 129
+#define BN_F_BN_DIV 107
+#define BN_F_BN_EXPAND_INTERNAL 120
+#define BN_F_BN_MOD_EXP_MONT 109
+#define BN_F_BN_MOD_INVERSE 110
+#define BN_F_BN_MOD_INVERSE_NO_BRANCH 139
+#define BN_F_BN_MOD_LSHIFT_QUICK 119
+#define BN_F_BN_NEW 113
+#define BN_F_BN_USUB 115
+#define BN_F_BN_RAND_RANGE 122
+#define BN_F_BN_SET_WORDS 144
+#define BN_F_BN_COMPUTE_WNAF 142
+#define BN_F_BN_MOD_SQRT 121
+#define BN_F_BN_MOD_EXP_MONT_WORD 117
+#define BN_F_BN_MOD_EXP_SIMPLE 126
+#define BN_F_BN_MOD_EXP_RECP 125
+#define BN_F_BN_DIV_RECP 130
+
+/* Reason codes. */
+#define BN_R_ARG2_LT_ARG3 100
+#define BN_R_BIGNUM_TOO_LONG 114
+#define BN_R_CALLED_WITH_EVEN_MODULUS 102
+#define BN_R_DIV_BY_ZERO 103
+#define BN_R_EXPAND_ON_STATIC_BIGNUM_DATA 105
+#define BN_R_INPUT_NOT_REDUCED 110
+#define BN_R_NOT_INITIALIZED 107
+#define BN_R_NO_INVERSE 108
+#define BN_R_TOO_MANY_TEMPORARY_VARIABLES 109
+#define BN_R_TOO_MANY_ITERATIONS 113
+#define BN_R_INVALID_RANGE 115
+#define BN_R_P_IS_NOT_PRIME 112
+#define BN_R_NOT_A_SQUARE 111
+#define BN_R_BAD_RECIPROCAL 101
+
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/mcu/interface/driver/che/ossl_crypto.h b/mcu/interface/driver/che/ossl_crypto.h
new file mode 100644
index 0000000..df5fbf0
--- /dev/null
+++ b/mcu/interface/driver/che/ossl_crypto.h
@@ -0,0 +1,194 @@
+/* crypto/crypto.h */
+/* ====================================================================
+ * Copyright (c) 1998-2006 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.]
+ */
+/* ====================================================================
+ * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
+ * ECDH support in OpenSSL originally developed by
+ * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
+ */
+
+#ifndef HEADER_CRYPTO_H
+#define HEADER_CRYPTO_H
+
+#include "ossl_portingfunc.h"
+#include "che_api.h"
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+/* When changing the CRYPTO_LOCK_* list, be sure to maintin the text lock
+ * names in cryptlib.c
+ */
+
+#define CRYPTO_LOCK_RSA 9
+#define CRYPTO_LOCK_RSA_BLINDING 25
+
+#define CRYPTO_LOCK 1
+#define CRYPTO_UNLOCK 2
+#define CRYPTO_READ 4
+#define CRYPTO_WRITE 8
+#define CRYPTO_LOCK_RAND 18
+#define CRYPTO_LOCK_RAND2 19
+
+#ifndef OPENSSL_NO_LOCKING
+#ifndef CRYPTO_w_lock
+#define CRYPTO_w_lock(type) \
+ CRYPTO_lock(CRYPTO_LOCK|CRYPTO_WRITE,type,__FILE__,__LINE__)
+#define CRYPTO_w_unlock(type) \
+ CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_WRITE,type,__FILE__,__LINE__)
+#define CRYPTO_r_lock(type) \
+ CRYPTO_lock(CRYPTO_LOCK|CRYPTO_READ,type,__FILE__,__LINE__)
+#define CRYPTO_r_unlock(type) \
+ CRYPTO_lock(CRYPTO_UNLOCK|CRYPTO_READ,type,__FILE__,__LINE__)
+#define CRYPTO_add(addr,amount,type) \
+ CRYPTO_add_lock(addr,amount,type,__FILE__,__LINE__)
+#endif
+#else
+#define CRYPTO_w_lock(a)
+#define CRYPTO_w_unlock(a)
+#define CRYPTO_r_lock(a)
+#define CRYPTO_r_unlock(a)
+#define CRYPTO_add(a,b,c) ((*(a))+=(b))
+#endif
+
+void CRYPTO_lock(int mode,kal_mutexid mutexid ,const char *file,int line);
+void CRYPTO_set_locking_callback(void (*func)(int mode,kal_mutexid mutexid,
+ const char *file,int line));
+
+#define OPENSSL_malloc(num) CHE_malloc(num)
+#define OPENSSL_zalloc(num) CHE_zalloc(num)
+#define OPENSSL_clear_free(addr, num) CHE_clear_free(addr, num)
+#define OPENSSL_free(num) CHE_free(num)
+#define OPENSSL_cleanse(ptr, len) CHE_cleanse(ptr, len)
+#define OPENSSL_MALLOC_MAX_NELEMS(type) (((1U<<(sizeof(int)*8-1))-1)/sizeof(type))
+
+#define fips_md_init_ctx(alg, cx) \
+ int alg##_Init(cx##_CTX *c)
+#define fips_cipher_abort(alg) while(0)
+
+/* Don't use this structure directly. */
+typedef struct crypto_threadid_st
+ {
+ void *ptr;
+ unsigned long val;
+ } CRYPTO_THREADID;
+void CRYPTO_THREADID_current(CRYPTO_THREADID *id);
+int CRYPTO_THREADID_cmp(const CRYPTO_THREADID *a, const CRYPTO_THREADID *b);
+void CRYPTO_THREADID_cpy(CRYPTO_THREADID *dest, const CRYPTO_THREADID *src);
+
+int CRYPTO_memcmp(const volatile void * volatile in_a,
+ const volatile void * volatile in_b,
+ size_t len);
+
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/mcu/interface/driver/che/ossl_ecies.h b/mcu/interface/driver/che/ossl_ecies.h
new file mode 100644
index 0000000..6b01857
--- /dev/null
+++ b/mcu/interface/driver/che/ossl_ecies.h
@@ -0,0 +1,67 @@
+#ifndef __OSSL_ECIES_H__
+#define __OSSL_ECIES_H__
+
+#include "kal_general_types.h"
+
+/* support scheme */
+typedef enum {
+ECIES_PROFILE_A = 1,
+ECIES_PROFILE_B,
+} sim_suci_scheme_enum;
+
+/* ECIES error code */
+typedef enum {
+ECIES_ERROR_NONE,
+ECIES_ERROR_SCHEME_NOT_SUPPORT,
+ECIES_ERROR_PUBKEY_NOT_EXIST,
+ECIES_ERROR_PUBKEY_LENGTH_WRONG,
+ECIES_ERROR_PUBKEY_FORMAT_WRONG,
+ECIES_ERROR_SUPI_NOT_EXIST,
+ECIES_ERROR_OUTPUT_NOT_EXIST,
+ECIES_ERROR_ECC_GENKEY_FAILED,
+ECIES_ERROR_ECC_EXPORT_PUBKEY_FAILED,
+ECIES_ERROR_ECC_GENSECRET_FAILED,
+ECIES_ERROR_KDF,
+ECIES_ERROR_AES_ENC,
+ECIES_ERROR_CALCULATE_MAC
+} ECIES_ERROR;
+
+extern kal_uint8 g_ecies_dump_log;
+
+/* ECIES encryption */
+/*****************************************************************************
+* FUNCTION
+* sim_supi_enc
+* DESCRIPTION
+* This function is to get the scheme ouput (None null scheme) with specified key and plaintext.
+* PARAMETERS
+* scheme [IN]
+* peer_pub_key_ptr [IN]
+* peer_pub_keylen [IN]
+* supi [IN]
+* supi_len [IN]
+* enc_out [OUT]
+* enc_out_len [OUT]
+* exported_public_key [OUT]
+* exported_public_key_len [OUT]
+* RETURNS
+* 0 : successfully
+* otherwise : failure (reference to enum ECIES_ERROR for error code)
+*****************************************************************************/
+kal_uint32 sim_supi_enc(sim_suci_scheme_enum scheme, const kal_uint8 *peer_pub_key_ptr, kal_uint16 peer_pub_keylen, const kal_uint8 *supi, kal_uint16 supi_len, kal_uint8 *enc_out, kal_uint16 *enc_out_len, kal_uint8 *exported_public_key, kal_uint8 *exported_public_key_len);
+
+/*****************************************************************************
+* FUNCTION
+* sim_check_suci_enc_support
+* DESCRIPTION
+* This function is to check the ME support SUCI protection scheme.
+* PARAMETERS
+* scheme [IN] The scheme ID to be check, refer to 33.501 Annex C.1
+* RETURNS
+* TRUE - ME support the scheme
+*****************************************************************************/
+kal_bool sim_check_suci_enc_support(sim_suci_scheme_enum scheme);
+
+void enable_ecies_dump_log();
+
+#endif /* __OSSL_ECIES_H__ */
diff --git a/mcu/interface/driver/che/ossl_evp.h b/mcu/interface/driver/che/ossl_evp.h
new file mode 100644
index 0000000..87676a5
--- /dev/null
+++ b/mcu/interface/driver/che/ossl_evp.h
@@ -0,0 +1,242 @@
+/* crypto/evp/evp.h */
+/* 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 HEADER_ENVELOPE_H
+#define HEADER_ENVELOPE_H
+
+#include "ossl_typ.h"
+#include "kal_public_defs.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#define ASN1_PKEY_ALIAS 0x1
+
+#define EVP_PKEY_EC 408
+#define EVP_PKEY_NONE 0
+#define EVP_PKEY_ALG_CTRL 0x1000
+
+#define EVP_MAX_MD_SIZE 64 /* longest known is SHA512 */
+
+
+struct env_md_st
+ {
+ //int type;
+ //int pkey_type;
+ int md_size;
+ //unsigned long flags;
+ int (*init)(EVP_MD_CTX *ctx);
+ int (*update)(EVP_MD_CTX *ctx,const void *data,size_t count);
+ int (*final)(EVP_MD_CTX *ctx,unsigned char *md);
+ //int (*copy)(EVP_MD_CTX *to,const EVP_MD_CTX *from);
+ //int (*cleanup)(EVP_MD_CTX *ctx);
+
+ /* FIXME: prototype these some day */
+ //int (*sign)(int type, const unsigned char *m, unsigned int m_length,
+ // unsigned char *sigret, unsigned int *siglen, void *key);
+ //int (*verify)(int type, const unsigned char *m, unsigned int m_length,
+ // const unsigned char *sigbuf, unsigned int siglen,
+ // void *key);
+ //int required_pkey_type[5]; /*EVP_PKEY_xxx */
+ int block_size;
+ int ctx_size; /* how big does the ctx->md_data need to be */
+ /* control function */
+ //int (*md_ctrl)(EVP_MD_CTX *ctx, int cmd, int p1, void *p2);
+ } /* EVP_MD */;
+
+
+struct env_md_ctx_st
+ {
+ const EVP_MD *digest;
+ //ENGINE *engine; /* functional reference if 'digest' is ENGINE-provided */
+ unsigned long flags;
+ void *md_data;
+ /* Public key context for sign/verify */
+ //EVP_PKEY_CTX *pctx;
+ /* Update function: usually copied from EVP_MD */
+ int (*update)(EVP_MD_CTX *ctx,const void *data,size_t count);
+ } /* EVP_MD_CTX */;
+
+/* values for EVP_MD_CTX flags */
+
+#define EVP_MD_CTX_FLAG_ONESHOT 0x0001 /* digest update will be called
+ * once only */
+//#define EVP_MD_CTX_FLAG_CLEANED 0x0002 /* context has already been
+// * cleaned */
+#define EVP_MD_CTX_FLAG_REUSE 0x0004 /* Don't free up ctx->md_data
+ * in EVP_MD_CTX_cleanup */
+
+//#define EVP_MD_CTX_FLAG_NO_INIT 0x0100 /* Don't initialize md_data */
+
+
+int EVP_MD_size(const EVP_MD *md);
+int EVP_MD_block_size(const EVP_MD *md);
+EVP_MD_CTX *EVP_MD_CTX_new(void);
+int EVP_MD_CTX_reset(EVP_MD_CTX *ctx);
+void EVP_MD_CTX_free(EVP_MD_CTX *ctx);
+void EVP_MD_CTX_init(EVP_MD_CTX *ctx);
+int EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx);
+void EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags);
+int EVP_MD_CTX_copy_ex(EVP_MD_CTX *out,const EVP_MD_CTX *in);
+int EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx,int flags);
+int EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *type);//, ENGINE *impl);
+int EVP_DigestUpdate(EVP_MD_CTX *ctx,const void *d,
+ size_t cnt);
+int EVP_DigestFinal_ex(EVP_MD_CTX *ctx,unsigned char *md,unsigned int *s);
+int EVP_DigestFinal(EVP_MD_CTX *ctx,unsigned char *md,unsigned int *s);
+int EVP_MD_CTX_copy(EVP_MD_CTX *out,const EVP_MD_CTX *in);
+int EVP_DigestInit(EVP_MD_CTX *ctx, const EVP_MD *type);
+int EVP_Digest(const void *data, kal_uint32 data_len,
+ unsigned char *digest, unsigned int *digest_len, const EVP_MD *md);
+int EVP_DigestFinal(EVP_MD_CTX *ctx,unsigned char *md,unsigned int *s);
+
+
+const EVP_MD *EVP_sha256(void);
+const EVP_MD *EVP_sha1(void);
+const EVP_MD *EVP_SHA384(void);
+
+/* values for EVP_MD_CTX flags */
+
+EVP_PKEY_CTX *EVP_PKEY_CTX_new_id(int id);
+EVP_PKEY_CTX *EVP_PKEY_CTX_new(EVP_PKEY *pkey);
+int EVP_PKEY_paramgen_init(EVP_PKEY_CTX *ctx);
+int EVP_PKEY_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
+int EVP_PKEY_keygen_init(EVP_PKEY_CTX *ctx);
+int EVP_PKEY_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY **ppkey);
+int EVP_PKEY_CTX_ctrl(EVP_PKEY_CTX *ctx, int keytype, int optype,
+ int cmd, int p1, void *p2);
+int EVP_PKEY_derive_init(EVP_PKEY_CTX *ctx);
+int EVP_PKEY_derive_set_peer(EVP_PKEY_CTX *ctx, EVP_PKEY *peer);
+int EVP_PKEY_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen);
+int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey);
+int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b);
+int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key);
+int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from);
+const EVP_PKEY_ASN1_METHOD *EVP_PKEY_asn1_find(int type); //ENGINE **pe, int type);
+
+
+EVP_PKEY *EVP_PKEY_new(void);
+int EVP_PKEY_up_ref(EVP_PKEY *pkey);
+void EVP_PKEY_free(EVP_PKEY *pkey);
+void EVP_PKEY_CTX_free(EVP_PKEY_CTX *ctx);
+int EVP_PKEY_set_type(EVP_PKEY *pkey, int type);
+
+
+#define EVP_PKEY_CTRL_PEER_KEY 2
+
+#define EVP_PKEY_assign_EC_KEY(pkey,eckey) EVP_PKEY_assign((pkey),EVP_PKEY_EC, (char *)(eckey))
+
+/* Operation codes. */
+
+#define EVP_PKEY_OP_UNDEFINED 0
+#define EVP_PKEY_OP_PARAMGEN (1<<1)
+#define EVP_PKEY_OP_KEYGEN (1<<2)
+#define EVP_PKEY_OP_SIGN (1<<3)
+#define EVP_PKEY_OP_VERIFY (1<<4)
+#define EVP_PKEY_OP_VERIFYRECOVER (1<<5)
+#define EVP_PKEY_OP_SIGNCTX (1<<6)
+#define EVP_PKEY_OP_VERIFYCTX (1<<7)
+#define EVP_PKEY_OP_ENCRYPT (1<<8)
+#define EVP_PKEY_OP_DECRYPT (1<<9)
+#define EVP_PKEY_OP_DERIVE (1<<10)
+
+
+/* Error codes for the EVP functions. */
+
+/* Function codes. */
+#define EVP_F_EVP_DIGESTINIT_EX 128
+#define EVP_F_EVP_MD_CTX_COPY_EX 110
+#define EVP_F_EVP_MD_SIZE 162
+#define EVP_F_INT_CTX_NEW 157
+#define EVP_F_EVP_PKEY_KEYGEN 146
+#define EVP_F_EVP_PKEY_KEYGEN_INIT 147
+#define EVP_F_EVP_PKEY_PARAMGEN 148
+#define EVP_F_EVP_PKEY_PARAMGEN_INIT 149
+#define EVP_F_EVP_PKEY_CTX_CTRL 137
+#define EVP_F_EVP_PKEY_DERIVE 153
+#define EVP_F_EVP_PKEY_DERIVE_INIT 154
+#define EVP_F_EVP_PKEY_DERIVE_SET_PEER 155
+#define EVP_F_EVP_PKEY_NEW 106
+#define EVP_F_PKEY_SET_TYPE 158
+#define EVP_F_EVP_PKEY_COPY_PARAMETERS 103
+
+
+/* Reason codes. */
+#define EVP_R_INPUT_NOT_INITIALIZED 111
+#define EVP_R_MESSAGE_DIGEST_IS_NULL 159
+#define EVP_R_UNSUPPORTED_ALGORITHM 156
+#define EVP_R_COMMAND_NOT_SUPPORTED 147
+#define EVP_R_INVALID_OPERATION 148
+#define EVP_R_NO_OPERATION_SET 149
+#define EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE 150
+#define EVP_R_OPERATON_NOT_INITIALIZED 151
+#define EVP_R_DIFFERENT_PARAMETERS 153
+#define EVP_R_NO_KEY_SET 154
+#define EVP_R_DIFFERENT_KEY_TYPES 101
+#define EVP_R_MISSING_PARAMETERS 103
+
+
+
+
+
+
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/mcu/interface/driver/che/ossl_portingfunc.h b/mcu/interface/driver/che/ossl_portingfunc.h
new file mode 100644
index 0000000..cbcbd18
--- /dev/null
+++ b/mcu/interface/driver/che/ossl_portingfunc.h
@@ -0,0 +1,22 @@
+#ifndef __OSSL_PORTINGFUNC_H__
+#define __OSSL_PORTINGFUNC_H__
+
+#include "kal_general_types.h"
+#include "kal_public_api.h"
+
+
+void *CHE_malloc(kal_uint32 size);
+
+void *CHE_zalloc(size_t num);
+
+void CHE_free(void * ptr);
+
+void CHE_cleanse(void *ptr, size_t len);
+
+void CHE_clear_free(void *str, size_t num);
+
+int CHE_rand_bytes(unsigned char *buf, int num);
+
+
+#endif
+
diff --git a/mcu/interface/driver/che/ossl_rsa.h b/mcu/interface/driver/che/ossl_rsa.h
new file mode 100644
index 0000000..3943796
--- /dev/null
+++ b/mcu/interface/driver/che/ossl_rsa.h
@@ -0,0 +1,371 @@
+/* crypto/rsa/rsa.h */
+/* 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 __OSSL_RSA_H__
+#define __OSSL_RSA_H__
+
+#include "ossl_typ.h"
+#include "ossl_bn.h"
+#include "ossl_crypto.h"
+#include "ossl_err.h"
+#include "ossl_constant_time_locl.h"
+#include "ossl_rand.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Declared already in ossl_typ.h */
+/* typedef struct rsa_st RSA; */
+/* typedef struct rsa_meth_st RSA_METHOD; */
+
+struct rsa_meth_st
+ {
+ const char *name;
+ int (*rsa_pub_enc)(int flen,const unsigned char *from,
+ unsigned char *to,
+ RSA *rsa,int padding);
+ int (*rsa_pub_dec)(int flen,const unsigned char *from,
+ unsigned char *to,
+ RSA *rsa,int padding);
+ int (*rsa_priv_enc)(int flen,const unsigned char *from,
+ unsigned char *to,
+ RSA *rsa,int padding);
+ int (*rsa_priv_dec)(int flen,const unsigned char *from,
+ unsigned char *to,
+ RSA *rsa,int padding);
+ int (*rsa_mod_exp)(BIGNUM *r0,const BIGNUM *I,RSA *rsa,BN_CTX *ctx); /* Can be null */
+ int (*bn_mod_exp)(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
+ const BIGNUM *m, BN_CTX *ctx,
+ BN_MONT_CTX *m_ctx); /* Can be null */
+ int (*init)(RSA *rsa); /* called at new */
+ int (*finish)(RSA *rsa); /* called at free */
+ int flags; /* RSA_METHOD_FLAG_* things */
+ char *app_data; /* may be needed! */
+/* New sign and verify functions: some libraries don't allow arbitrary data
+ * to be signed/verified: this allows them to be used. Note: for this to work
+ * the RSA_public_decrypt() and RSA_private_encrypt() should *NOT* be used
+ * RSA_sign(), RSA_verify() should be used instead. Note: for backwards
+ * compatibility this functionality is only enabled if the RSA_FLAG_SIGN_VER
+ * option is set in 'flags'.
+ */
+ int (*rsa_sign)(int type,
+ const unsigned char *m, unsigned int m_length,
+ unsigned char *sigret, unsigned int *siglen, const RSA *rsa);
+ int (*rsa_verify)(int dtype,
+ const unsigned char *m, unsigned int m_length,
+ const unsigned char *sigbuf, unsigned int siglen,
+ const RSA *rsa);
+/* If this callback is NULL, the builtin software RSA key-gen will be used. This
+ * is for behavioural compatibility whilst the code gets rewired, but one day
+ * it would be nice to assume there are no such things as "builtin software"
+ * implementations. */
+ int (*rsa_keygen)(RSA *rsa, int bits, BIGNUM *e, BN_GENCB *cb);
+ };
+
+struct rsa_st
+ {
+ /* The first parameter is used to pickup errors where
+ * this is passed instead of aEVP_PKEY, it is set to 0 */
+ int pad;
+ long version;
+ const RSA_METHOD *meth;
+ /* functional reference if 'meth' is ENGINE-provided */
+ // ENGINE *engine;
+ BIGNUM *n;
+ BIGNUM *e;
+ BIGNUM *d;
+ BIGNUM *p;
+ BIGNUM *q;
+ BIGNUM *dmp1;
+ BIGNUM *dmq1;
+ BIGNUM *iqmp;
+ /* be careful using this if the RSA structure is shared */
+ //CRYPTO_EX_DATA ex_data;
+ int references;
+ int flags;
+
+ /* Used to cache montgomery values */
+ BN_MONT_CTX *_method_mod_n;
+ BN_MONT_CTX *_method_mod_p;
+ BN_MONT_CTX *_method_mod_q;
+
+ /* all BIGNUM values are actually in the following data, if it is not
+ * NULL */
+ char *bignum_data;
+ //BN_BLINDING *blinding; // TODO : Support blinding
+ //BN_BLINDING *mt_blinding;
+ kal_mutexid lock;
+ };
+
+#ifndef OPENSSL_RSA_MAX_MODULUS_BITS
+# define OPENSSL_RSA_MAX_MODULUS_BITS 16384
+#endif
+
+#ifndef OPENSSL_RSA_SMALL_MODULUS_BITS
+# define OPENSSL_RSA_SMALL_MODULUS_BITS 3072
+#endif
+#ifndef OPENSSL_RSA_MAX_PUBEXP_BITS
+# define OPENSSL_RSA_MAX_PUBEXP_BITS 64 /* exponent limit enforced for "large" modulus only */
+#endif
+
+//#define RSA_METHOD_FLAG_NO_CHECK 0x0001 /* don't check pub/private match */
+
+#define RSA_FLAG_CACHE_PUBLIC 0x0002
+#define RSA_FLAG_CACHE_PRIVATE 0x0004
+//#define RSA_FLAG_BLINDING 0x0008
+//#define RSA_FLAG_THREAD_SAFE 0x0010
+/* This flag means the private key operations will be handled by rsa_mod_exp
+ * and that they do not depend on the private key components being present:
+ * for example a key stored in external hardware. Without this flag bn_mod_exp
+ * gets called when private key components are absent.
+ */
+#define RSA_FLAG_EXT_PKEY 0x0020
+
+/* This flag in the RSA_METHOD enables the new rsa_sign, rsa_verify functions.
+ */
+//#define RSA_FLAG_SIGN_VER 0x0040
+
+#define RSA_FLAG_NO_BLINDING 0x0080 /* new with 0.9.6j and 0.9.7b; the built-in
+ * RSA implementation now uses blinding by
+ * default (ignoring RSA_FLAG_BLINDING),
+ * but other engines might not need it
+ */
+#define RSA_FLAG_NO_CONSTTIME 0x0100 /* new with 0.9.8f; the built-in RSA
+ * implementation now uses constant time
+ * operations by default in private key operations,
+ * e.g., constant time modular exponentiation,
+ * modular inverse without leaking branches,
+ * division without leaking branches. This
+ * flag disables these constant time
+ * operations and results in faster RSA
+ * private key operations.
+ */
+//#ifndef OPENSSL_NO_DEPRECATED
+//#define RSA_FLAG_NO_EXP_CONSTTIME RSA_FLAG_NO_CONSTTIME /* deprecated name for the flag*/
+ /* new with 0.9.7h; the built-in RSA
+ * implementation now uses constant time
+ * modular exponentiation for secret exponents
+ * by default. This flag causes the
+ * faster variable sliding window method to
+ * be used for all exponents.
+ */
+//#endif
+
+
+#define RSA_PKCS1_PADDING 1
+//#define RSA_SSLV23_PADDING 2
+#define RSA_NO_PADDING 3
+#define RSA_PKCS1_OAEP_PADDING_SHA1 4
+#define RSA_PKCS1_OAEP_PADDING_SHA256 7
+//#define RSA_X931_PADDING 5
+/* EVP_PKEY_ only */
+//#define RSA_PKCS1_PSS_PADDING 6
+
+#define RSA_PKCS1_PADDING_SIZE 11
+#define RSA_3 0x3L
+#define RSA_F4 0x10001L
+#define DEFBITS 2048
+
+RSA * RSA_new(void);
+RSA * RSA_new_method();//(ENGINE *engine);
+int RSA_size(const RSA *rsa);
+
+ /* next 4 return -1 on error */
+int RSA_public_encrypt(int flen, const unsigned char *from,
+ unsigned char *to, RSA *rsa,int padding);
+int RSA_private_encrypt(int flen, const unsigned char *from,
+ unsigned char *to, RSA *rsa,int padding);
+int RSA_public_decrypt(int flen, const unsigned char *from,
+ unsigned char *to, RSA *rsa,int padding);
+int RSA_private_decrypt(int flen, const unsigned char *from,
+ unsigned char *to, RSA *rsa,int padding);
+void RSA_free (RSA *r);
+
+
+/* these are the actual SSLeay RSA functions */
+const RSA_METHOD *RSA_PKCS1_SSLeay(void);
+
+/* The following 2 functions sign and verify a X509_SIG ASN1 object
+ * inside PKCS#1 padded RSA encryption */
+/*int RSA_sign(int type, const unsigned char *m, unsigned int m_length,
+ unsigned char *sigret, unsigned int *siglen, RSA *rsa);
+int RSA_verify(int type, const unsigned char *m, unsigned int m_length,
+ const unsigned char *sigbuf, unsigned int siglen, RSA *rsa);*/
+
+int RSA_padding_add_PKCS1_type_1(unsigned char *to,int tlen,
+ const unsigned char *f,int fl);
+int RSA_padding_check_PKCS1_type_1(unsigned char *to,int tlen,
+ const unsigned char *f,int fl,int rsa_len);
+int RSA_padding_add_PKCS1_type_2(unsigned char *to,int tlen,
+ const unsigned char *f,int fl);
+int RSA_padding_check_PKCS1_type_2(unsigned char *to,int tlen,
+ const unsigned char *f,int fl,int rsa_len);
+int PKCS1_MGF1(unsigned char *mask, long len,
+ const unsigned char *seed, long seedlen, const EVP_MD *dgst);
+int RSA_padding_add_none(unsigned char *to,int tlen,
+ const unsigned char *f,int fl);
+int RSA_padding_check_none(unsigned char *to,int tlen,
+ const unsigned char *f,int fl,int rsa_len);
+int RSA_verify_PKCS1_PSS(RSA *rsa, const unsigned char *mHash,
+ const EVP_MD *Hash, const unsigned char *EM, int sLen);
+int RSA_padding_add_PKCS1_PSS(RSA *rsa, unsigned char *EM,
+ const unsigned char *mHash,
+ const EVP_MD *Hash, int sLen);
+
+int RSA_verify_PKCS1_PSS_mgf1(RSA *rsa, const unsigned char *mHash,
+ const EVP_MD *Hash, const EVP_MD *mgf1Hash,
+ const unsigned char *EM, int sLen);
+
+int RSA_padding_add_PKCS1_PSS_mgf1(RSA *rsa, unsigned char *EM,
+ const unsigned char *mHash,
+ const EVP_MD *Hash, const EVP_MD *mgf1Hash, int sLen);
+
+int RSA_padding_add_PKCS1_OAEP(unsigned char *to, int tlen,
+ const unsigned char *f, int fl,
+ const unsigned char *p, int pl);
+int RSA_padding_check_PKCS1_OAEP(unsigned char *to, int tlen,
+ const unsigned char *f, int fl, int rsa_len,
+ const unsigned char *p, int pl);
+int RSA_padding_add_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
+ const unsigned char *from, int flen,
+ const unsigned char *param, int plen,
+ const EVP_MD *md, const EVP_MD *mgf1md);
+int RSA_padding_check_PKCS1_OAEP_mgf1(unsigned char *to, int tlen,
+ const unsigned char *from, int flen,
+ int num, const unsigned char *param,
+ int plen, const EVP_MD *md,
+ const EVP_MD *mgf1md);
+
+/* If this flag is set the RSA method is FIPS compliant and can be used
+ * in FIPS mode. This is set in the validated module method. If an
+ * application sets this flag in its own methods it is its responsibility
+ * to ensure the result is compliant.
+ */
+
+//#define RSA_FLAG_FIPS_METHOD 0x0400
+
+/* If this flag is set the operations normally disabled in FIPS mode are
+ * permitted it is then the applications responsibility to ensure that the
+ * usage is compliant.
+ */
+
+#define RSA_FLAG_NON_FIPS_ALLOW 0x0400
+/* Application has decided PRNG is good enough to generate a key: don't
+ * check.
+ */
+#define RSA_FLAG_CHECKED 0x0800
+
+/* BEGIN ERROR CODES */
+/* The following lines are auto generated by the script mkerr.pl. Any changes
+ * made after this point may be overwritten when the script is next run.
+ */
+void ERR_load_RSA_strings(void);
+
+/* Error codes for the RSA functions. */
+
+/* Function codes. */
+#define RSA_F_INT_RSA_VERIFY 145
+#define RSA_F_RSA_EAY_PRIVATE_DECRYPT 101
+#define RSA_F_RSA_EAY_PRIVATE_ENCRYPT 102
+#define RSA_F_RSA_EAY_PUBLIC_DECRYPT 103
+#define RSA_F_RSA_EAY_PUBLIC_ENCRYPT 104
+#define RSA_F_RSA_NEW_METHOD 106
+#define RSA_F_RSA_PADDING_ADD_NONE 107
+#define RSA_F_RSA_PADDING_ADD_PKCS1_PSS_MGF1 148
+#define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_1 108
+#define RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2 109
+#define RSA_F_RSA_PADDING_CHECK_NONE 111
+#define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1 112
+#define RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2 113
+#define RSA_F_RSA_PADDING_ADD_PKCS1_OAEP 121
+#define RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP 122
+#define RSA_F_RSA_BUILTIN_KEYGEN 129
+#define RSA_F_RSA_VERIFY_PKCS1_PSS_MGF1 149
+#define RSA_F_RSA_PADDING_CHECK_PKCS1_OAEP_MGF1 153
+#define RSA_F_RSA_PADDING_ADD_PKCS1_OAEP_MGF1 154
+
+/* Reason codes. */
+#define RSA_R_BAD_E_VALUE 101
+#define RSA_R_BAD_FIXED_HEADER_DECRYPT 102
+#define RSA_R_BAD_PAD_BYTE_COUNT 103
+#define RSA_R_BAD_SIGNATURE 104
+#define RSA_R_INVALID_PADDING 105
+#define RSA_R_BLOCK_TYPE_IS_NOT_01 106
+#define RSA_R_DATA_GREATER_THAN_MOD_LEN 108
+#define RSA_R_DATA_TOO_LARGE 109
+#define RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE 110
+#define RSA_R_KEY_SIZE_TOO_SMALL 120
+#define RSA_R_DATA_TOO_LARGE_FOR_MODULUS 132
+#define RSA_R_DATA_TOO_SMALL_FOR_KEY_SIZE 122
+#define RSA_R_FIRST_OCTET_INVALID 133
+#define RSA_R_LAST_OCTET_INVALID 134
+#define RSA_R_MODULUS_TOO_LARGE 105
+#define RSA_R_NULL_BEFORE_BLOCK_MISSING 113
+#define RSA_R_PADDING_CHECK_FAILED 114
+#define RSA_R_OAEP_DECODING_ERROR 121
+#define RSA_R_PKCS_DECODING_ERROR 159
+#define RSA_R_SLEN_CHECK_FAILED 136
+#define RSA_R_SLEN_RECOVERY_FAILED 135
+#define RSA_R_UNKNOWN_PADDING_TYPE 118
+#define RSA_R_GEN_KEY_EPOF_ABORT 500
+
+#ifdef __cplusplus
+}
+#endif
+#endif
diff --git a/mcu/interface/driver/che/ossl_typ.h b/mcu/interface/driver/che/ossl_typ.h
new file mode 100644
index 0000000..c641fd6
--- /dev/null
+++ b/mcu/interface/driver/che/ossl_typ.h
@@ -0,0 +1,92 @@
+/* ====================================================================
+ * Copyright (c) 1998-2001 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).
+ *
+ */
+
+#ifndef HEADER_OPENSSL_TYPES_H
+#define HEADER_OPENSSL_TYPES_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef BIGNUM
+#undef BIGNUM
+#endif
+typedef struct bignum_st BIGNUM;
+typedef struct bignum_ctx BN_CTX;
+typedef struct bn_mont_ctx_st BN_MONT_CTX;
+typedef struct bn_gencb_st BN_GENCB;
+typedef struct bn_recp_ctx_st BN_RECP_CTX;
+
+typedef struct env_md_st EVP_MD;
+typedef struct env_md_ctx_st EVP_MD_CTX;
+
+typedef struct hmac_ctx_st HMAC_CTX;
+typedef struct rsa_st RSA;
+typedef struct rsa_meth_st RSA_METHOD;
+
+typedef struct rand_meth_st RAND_METHOD;
+
+typedef struct ec_key_st EC_KEY;
+typedef struct ec_key_method_st EC_KEY_METHOD;
+
+typedef struct evp_pkey_ctx_st EVP_PKEY_CTX;
+typedef struct evp_pkey_st EVP_PKEY;
+typedef struct evp_pkey_method_st EVP_PKEY_METHOD;
+typedef struct evp_pkey_asn1_method_st EVP_PKEY_ASN1_METHOD;
+
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* def HEADER_OPENSSL_TYPES_H */
diff --git a/mcu/interface/driver/che/x509.h b/mcu/interface/driver/che/x509.h
new file mode 100644
index 0000000..d7df121
--- /dev/null
+++ b/mcu/interface/driver/che/x509.h
@@ -0,0 +1,103 @@
+/*
+ * Copyright (c) 2007, Cameron Rich
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright notice,
+ * this list of conditions and the following disclaimer.
+ * * 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.
+ * * Neither the name of the axTLS project nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "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 COPYRIGHT OWNER 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.
+*/
+
+#ifndef __HEADER_X509_H__
+#define __HEADER_X509_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <time.h>
+#include "cust_chl_interface.h"
+#include "kal_public_api.h"
+#include "kal_public_defs.h"
+#include "ps_public_struct.h"
+
+#define malloc get_ctrl_buffer
+#define free free_ctrl_buffer
+
+/**************************************************************************
+ * X509 declarations
+ **************************************************************************/
+#define X509_OK 0
+#define X509_NOT_OK -1
+#define X509_VFY_ERROR_NO_TRUSTED_CERT -2
+#define X509_VFY_ERROR_BAD_SIGNATURE -3
+#define X509_VFY_ERROR_NOT_YET_VALID -4
+#define X509_VFY_ERROR_EXPIRED -5
+#define X509_VFY_ERROR_SELF_SIGNED -6
+#define X509_VFY_ERROR_INVALID_CHAIN -7
+#define X509_VFY_ERROR_UNSUPPORTED_DIGEST -8
+#define X509_UNRECOGNISED_SIG_TYPE -9
+#define X509_VFY_ERROR_NO_UNTRUSTED_CERT -10
+#define X509_VFY_ERROR_GET_CA_DN -11
+#define X509_VFY_ERROR_GET_CERT_DN -12
+#define X509_VFY_ERROR_GET_TIME -13
+#define X509_VFY_ERROR_GET_PUB_KEY -14
+#define X509_VFY_ERROR_INOUT_NULL_PTR -15
+/*
+ * The Distinguished Name
+ */
+#define X509_NUM_DN_TYPES 3
+
+struct _x509_ctx
+{
+ char *ca_cert_dn[X509_NUM_DN_TYPES];
+ char *cert_dn[X509_NUM_DN_TYPES];
+ kal_uint32 not_before;
+ kal_uint32 not_after;
+ kal_uint8 *tbs;
+ kal_uint32 tbs_len;
+ t_cust_chl_asym_key key;
+ kal_uint8 *signature;
+ kal_uint16 sig_len;
+ kal_uint8 sig_type;
+ struct _x509_ctx *next;
+};
+
+typedef struct _x509_ctx X509_CTX;
+
+#define CONFIG_X509_MAX_CA_CERTS 150
+
+typedef struct
+{
+ X509_CTX *cert[CONFIG_X509_MAX_CA_CERTS];
+} CA_CERT_CTX;
+
+int x509_new(const kal_uint8 *cert, int *len, X509_CTX **ctx);
+void x509_free(X509_CTX *x509_ctx);
+int x509_verify(const CA_CERT_CTX *ca_cert_ctx, const X509_CTX *cert);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif