/*
   infosec_api.h
   
   The header file of ICBC demanded API writen in ANSI C
   Written by Yang Hongxuan (yanghx <yanghx@infosec.com.cn>)
  
   Copyright (C) 2002, Infosec Century, Inc. Created 2002. 
   All rights reserved.
*/

#ifndef _INFOSEC_API_
#define _INFOSEC_API_

#if defined(_WIN32)	
	#if defined( _DLLEXPORT_ )
		#define DLLENTRY	__declspec(dllexport)
	#else
		#define DLLENTRY	//__declspec(dllimport)
	#endif
#else
	#define DLLENTRY
#endif

#ifdef          __cplusplus
extern "C" {
#endif


#ifndef  _RETURN_VALUE_
#define  _RETURN_VALUE_
typedef enum RETURN_VALUE
{
	RET_SUCCESS = 0,
	ERR_BAD_PARAMETERS = -1,
	ERR_MALLOC = -2,
	ERR_PUBLICKEY_ENCODE = -3,
	ERR_PUBLICKEY_DECODE = -4,
	ERR_PRIVATEKEY_ENCODE = -5,
	ERR_PRIVATEKEY_DECODE = -6,
	ERR_PUBLICKEY_ENCRYPT = -7,
	ERR_PUBLICKEY_DECRYPT = -8,
	ERR_PRIVATEKEY_ENCRYPT = -9,
	ERR_PRIVATEKEY_DECRYPT = -10,
	ERR_CEA_ENCRYPT = -11,
	ERR_CEA_DECRYPT = -12,
	ERR_GET_CERT_INFO = -13,
	ERR_GEN_KEY = -14,
	ERR_MPI_CONVERT = -15,
	ERR_FILE = -16,
	ERR_MD	= -17,
	ERR_DIGEST = -18,
	ERR_UNEQUAL = -19,
	ERR_BASE64_ENCODE = -20,
	ERR_BASE64_DECODE = -21,
	ERR_OTHER = -22,
} RETURN_VALUE;
#endif

/*
Description:
  sign a string

parameter:
      src           (in)--data to be sign
      srcLen        (in)--src 's length
      privateKey    (in)--private key
      keyLen        (in)--private key len
      keyPass       (in)--private key password,must be '\0' end string
      signBuf       (out)-signature data buffer
      signBufLen    (out)-signature buffer' length

return:
      0  for successful
      -1 src error
      -2 private key error  
      -3 decrypt error
remark:
     the caller has obligation to free the  'signBuf'
*/
DLLENTRY int sign
( 
  unsigned char *src,
  int srcLen,
  unsigned char *privateKey,
  int keyLen,
  char *keyPass,
  unsigned char **signBuf,
  int *signBufLen
);
/*
Description:
  verify a signature

parameter:
      src           (in)--data to be verify
      srcLen        (in)--src 's length
      cert          (in)--certificate
      certLen       (in)--certificate len
      signBuf       (in)--signature buffer
      signBufLen    (in)--signature buffer' length

return:
      0  signature is good
      -1 decode cert failure
*/
DLLENTRY int verifySign
(
   unsigned char *src,
   int srcLen,
   unsigned char *cert,
   int certLen,
   unsigned char *signBuf,
   int signBufLen
);
/*
Description:
  base64 encode

parameter:
      src           (in)--src to be encoded
      srcLen        (in)--src len
      dst           (out)-base64 encode result
      dstLen        (out)-dst len
return:
      0  get ok
      -1 failure
remark:
     caller must free "dst"
*/
DLLENTRY int base64enc(unsigned char *src,int srcLen,unsigned char **dst,int *dstLen);

/*
Description:
  base64 decode

parameter:
      src           (in)--src to be decode
      srcLen        (in)--src len
      dst           (out)-base64 decode result
      dstLen        (out)-dst len
return:
      0  get ok
      -1 failure
remark:
     caller must free "dst"
*/
DLLENTRY int base64dec(unsigned char *src,int srcLen,unsigned char **dst,int *dstLen);
/*
Description:
  get certificate ID

parameter:
      mertCert           (in)--certificate
      mertCertLen        (in)--cert len
      id                 (out)-id
      idLen              (out)-id len
return:
      0  get ok
      -1 failure
remark:
     caller must free "id"
*/
DLLENTRY int getCertID(unsigned char *mertCert,int mertCertLen,char **id,int *idLen);

//DLLENTRY void infosec_free(void *dst);

/*******************************************************/
/*                  part 1 : RSA的操作                 */
/*******************************************************/

/* 说明：产生公钥和有口令保护的私钥 
   返回值：成功 0  失败 非0 
   注意：存放公钥和私钥的缓冲区由接口分配，调用者来释放
         inKeyLen <= 4096 bits, 最好inKeyLen被32整除.
		 初始口令为12345678
*/
DLLENTRY int genRSAKeyPwd
(
   int inKeyLen,             /*所需RSA模数n的比特数，输入*/
   unsigned char *seed,      /*随机数种子，输入*/
   unsigned char **pubKeyBuf,/*保存公钥的缓冲区，输出*/
   int  *publicKeyLen,        /*ASN.1编码的公钥的长度*/
   unsigned char **priKeyBuf,/*保存私钥的缓冲区，输出*/
   int *privateKeyLen,        /*ASN.1编码的私钥的长度*/
   char **initialKeyPass     /*保护私钥的初始口令，输出*/
);

/* 说明：从证书中获得公钥 
   注意：pubKeyBuf缓冲区由接口分配，调用者来释放
   返回值：成功 0  失败 非0 
*/
DLLENTRY int getPublicKey 
(
   unsigned char *cert,      /*证书数据，输入*/
   int certLen,              /*证书数据的长度，输入*/
   unsigned char **publicKey,/*获得的公钥的缓冲区，输出*/
   int *publicKeyLen         /*获得公钥的长度，输出*/
);

/*1。2 用RSA的私钥进行的操作 */ 

/* 说明：用口令保护的RSA私钥来加密数据 */
/* 注意：encBuf缓冲区由接口分配，调用者来释放
   返回值：成功 0  失败 非0
 */
DLLENTRY int privateEncryptPwd
(
  unsigned char *src,       /*要加密的数据*/
  int srcLen,               /*要加密的数据的字节数*/
  unsigned char *privateKey,/*私钥*/
  int keyLen,               /*私钥的字节数*/
  char *keyPass,            /*口令*/
  unsigned char **encBuf,   /*加密以后数据存放的缓冲区指针*/
  int *encBufLen            /*加密以后数据的长度*/
);

/* 说明：用口令保护的RSA私钥来解密数据 */
/* 注意：decBuf缓冲区由接口分配，调用者来释放
   返回值：成功 0  失败 非0 */
DLLENTRY int privateDecryptPwd
(
   unsigned char *src,       /*要解密的数据*/
   int srcLen,               /*要解密的数据的字节数*/
   unsigned char *privateKey,/*私钥*/
   int keyLen,               /*私钥的字节数*/
   char *pass,               /*口令*/
   unsigned char **decBuf,   /*解密以后的数据存放的缓冲区指针*/
   int *decBufLen            /*解密以后的数据的长度*/
);


/* 说明：用证书中的RSA公钥来加密数据 */
/* 注意：encBuf缓冲区由接口分配，调用者来释放
   返回值：成功 0  失败 非0 */
DLLENTRY int publicEncryptCert
(
   unsigned char *src,    /*要加密的数据*/
   int srcLen,            /*要加密的数据的长度*/
   unsigned char *cert,   /*证书*/
   int certLen,           /*证书长度*/
   unsigned char **encBuf,/*加密后的数据缓冲区指针*/
   int *encBufLen         /*加密后的数据的长度*/
);

/* 说明：用证书中的RSA公钥来解密数据 */
/* 注意：decBuf缓冲区由接口分配，调用者来释放
   返回值：成功 0  失败 非0 */
/* 这个是为了使用者方便新加的接口*/
DLLENTRY int publicDecryptCert
(
   unsigned char *src,    /*要解密的数据*/
   int srcLen,            /*要解密的数据的长度*/
   unsigned char *cert,   /*证书*/
   int certLen,           /*证书长度*/
   unsigned char **decBuf,/*加密后的数据缓冲区指针*/
   int *decBufLen         /*加密后的数据的长度*/
);

/*********************************************************/
/*              part 2: Des密码操作                      */
/*********************************************************/

/* 说明：用DES加密接口 */
/* 返回值：成功 0  失败 非0 */
/* 注意：encodedBuf缓冲区由接口分配，调用者来释放
         明文长度不被8整除时，自动添加了若干个值为0x00的字节，
		 以满足明文长度被8整除的条件
*/
DLLENTRY int desEncrypt
(
	char *src, 
	int srcLen, 
	unsigned char *Key,
	int keyLen,
	unsigned char **encBuf,
	int *encBufLen
);

/* 说明：用DES解密接口 */
/* 返回值：成功 0  失败 非0 */
/* 注意：encodedBuf缓冲区由接口分配，调用者来释放
         明文长度不被8整除时，自动添加了若干个值为0x00的字节，
		 以满足明文长度被8整除的条件
*/
DLLENTRY int desDecrypt
(
	unsigned char *src, 
	int srcLen, 
	unsigned char *Key, 
	int keyLen, 
	unsigned char **decBuf,
	int *decBufLen
);


/* 说明：产生一个用于tripple DES加/解密的密钥 */
/* 注意：keyBuf缓冲区由接口分配，调用者来释放
   返回值：成功 0  失败 非0 */
DLLENTRY int genDESKey
(
   int length,            /*输入要产生的密钥的长度 >= 24 */
   unsigned char *seed,   /*输入的随机数种子*/
   unsigned char **keyBuf,/*产生的密钥*/
   int *keyLen            /*产生的密钥的长度*/
);

//int getErrorInfo(char *err);
//char err[128];

#ifdef          __cplusplus
}
#endif

#endif
