Encryption
>w## Notice
> * please include the header file "bwsdk_api.h" when you need to input encryption and decryption data
`#include "bwsdk_api.h"`
>i## data structure
```
/**
* @brief Key information
* @details Key information for under key write in
*/
typedef struct {
unsigned char ucSrcKeyType; ///< Upper key type: PED_TLK,PED_TMK,PED_TPK,PED_TAK,PED_TDK, should not lower then ucDstKeyType
unsigned char ucDstKeyType; ///< Under key type: PED_TLK,PED_TMK,PED_TPK,PED_TAK,PED_TDK
unsigned char ucSrcKeyIdx; ///< Upper key index. Generally start from 1. Index=0 means that no upper key used and plaintext to write in.
unsigned char ucDstKeyIdx; ///< Under key index.
unsigned char ucDstKeyLen; ///< Length of under key. Only permit: 8, 16, 24, 32
unsigned char Algorithm; ///< Algorithm: PED_TDEA, PED_AES, PED_SM4
unsigned char RFU[10]; ///< RFU
unsigned char ucDstKeyValue[32]; ///< Under key
} ST_KEY_INFO;
/**
* @brief KCV information
*/
typedef struct {
/**
* Check mode:
* -# 0x00 - Do not check KCV;
* -# 0x01 - Mode 1: KCV is the result of using key to TDES encrypt all zero;
* -# 0x02 - Key odd check and then as mode 1;
* -# 0x03 - Key even check and then as mode 1;
* -# 0x04 - ICBC mode, KCV is the X9.19 MAC result of data([under key + ICBC-index] padding to multiple of 8)using Upper key;
* -# 0x05 - CMAC mode;
* -# 0x80 - TR-31 mode.
*/
int iCheckMode;
int iDataLen; ///< Length of check data
unsigned char szCheckBuf[128]; ///< Check data
} ST_KCV_INFO;
/**
* @brief RSA key
* @details RSA key for wirtting
*/
typedef struct {
int iModulusLen; ///< Length of modulus
unsigned char aucModulus[512]; ///< Modulus, padding 00 on the right
int iExponentLen; ///< Length of exponent
unsigned char aucExponent[512]; ///< Exponent, padding 00 on the right
unsigned char aucKeyInfo[128]; ///< Key information
} ST_RSA_KEY;
```
>i## PedWriteKeyRev
### Prototype
`int PedWriteKeyRev(ST_KEY_INFO *pstKeyInfoIn, ST_KCV_INFO *pstKcvInfoIn);`
### Function
* write encryption and decryption key
### Parameter
|Name|Type|description|
|-|-|-|
|pstKeyInfoIn|ST_KEY_INFO *|Key info|
|pstKcvInfoIn|ST_KCV_INFO *|KCV info|
### Retval
|Value|Type|Description|
|-|-|-|
|=0|int|success|
|<0|int|fail|
>i## PedCalcDESRev
### Prototype
`int PedCalcDESRev(unsigned char ucKeyIdx, unsigned char ucMode, unsigned char *pucDataIn, unsigned short usDataInLen, unsigned char *pucDataOut);`
### Function
*Enctypt or Decrypt data
### Parameter
|Name|Type|description|
|-|-|-|
|ucKeyIdx|unsigned char|TDK index|
|ucMode| unsigned char|mode:PED_ECB_DEC/PED_CBC_DEC/PED_ECB_ENC/PED_CBC_ENC|
|pucDataIn|unsigned char *|input data to calc|
|usDataInLen|unsigned short|Length of data|
|pucDataOut|unsigned char *|Enctypt/Decrypt result|
### Retval
|Value|Type|Description|
|-|-|-|
|=0|int|success|
|<0|int|fail|
>i## PedCalcDesDPARev
### Prototype
`int PedCalcDesDPARev(unsigned char *pucKey, unsigned char ucKeyLen, unsigned char *pucDataIn, unsigned char ucDataInLen,
unsigned char *pucDataOut, unsigned int uiSize, unsigned char ucMode, unsigned char ucEnc);`
### Function
*Enctypt or Decrypt data
### Parameter
|Name|Type|description|
|-|-|-|
|pucKey|unsigned char *|DES/TDES key|
|ucKeyLen| unsigned char|Length of key in Bytes: 8/16/24|
|pucDataIn|unsigned char *|input data to calc|
|usDataInLen|unsigned char|length of input data, hava to mutiple of 8|
|pucDataOut|unsigned char *|Output data after calc|
|uiSize|unsigned int| Max size of dataOut|
|ucMode| unsigned char|Refer to ped_symm_mode_t|
|ucEnc| unsigned char| Refer to ped_func_t|
### Retval
|Value|Type|Description|
|-|-|-|
|=0|int|success|
|<0|int|fail|
>i## PedCalcAesDPARev
### Prototype
`PedCalcAesDPARev(unsigned char *pucKey, unsigned char ucKeyLen, unsigned char *pucDataIn, unsigned char ucDataInLen,
unsigned char *pucDataOut, unsigned int uiSize, unsigned char ucMode, unsigned char ucEnc)`
### Function
*Enctypt or Decrypt data
### Parameter
|Name|Type|description|
|-|-|-|
|pucKey|unsigned char *|DES/TDES key|
|ucKeyLen| unsigned char|Length of key in Bytes: 8/16/24|
|pucDataIn|unsigned char *|input data to calc|
|usDataInLen|unsigned char|length of input data, hava to mutiple of 8|
|pucDataOut|unsigned char *|Output data after calc|
|uiSize|unsigned int| Max size of dataOut|
|ucMode| unsigned char|Refer to ped_symm_mode_t|
|ucEnc| unsigned char| Refer to ped_func_t|
### Retval
|Value|Type|Description|
|-|-|-|
|=0|int|success|
|<0|int|fail|
>i## PedWriteRsaKeyRev
### Prototype
`int PedWriteRsaKeyRev(unsigned char ucRsaKeyIdx, ST_RSA_KEY* pstRsakeyIn);`
### Function
* write encryption and decryption key
### Parameter
|Name|Type|description|
|-|-|-|
|ucRsaKeyIdx|unsigned char|RSA key index|
|pstRsakeyIn| ST_RSA_KEY*| RSA key|
### Retval
|Value|Type|Description|
|-|-|-|
|=0|int|success|
|<0|int|fail|
>i## PedRsaRecoverRev
### Prototype
`int PedRsaRecoverRev(unsigned char ucRsaKeyIdx, unsigned char *pucDataIn, unsigned int uiDataInLen, unsigned char *pucDataOut);`
### Function
* write encryption and decryption key
### Parameter
|Name|Type|description|
|-|-|-|
|ucRsaKeyIdx|unsigned char|RSA key index|
|pucDataIn| unsigned char *|input data to calc|
|uiDataInLen| unsigned int| Length of data|
|pucDataOut|unsigned char *|Length of result|
### Retval
|Value|Type|Description|
|-|-|-|
|=0|int|success|
|<0|int|fail|
>s## Example
```
///< write key function
if (!bNoDecrypt) {
keyInfo.ucSrcKeyType = PED_TMK;
keyInfo.ucSrcKeyIdx = ucKeyIndex;
}
keyInfo.ucDstKeyType = ucSaveKeyType;
keyInfo.ucDstKeyIdx = ucSaveKeyIndex;
keyInfo.Algorithm = PED_TDES;
keyInfo.ucDstKeyLen = len;
memcpy(keyInfo.ucDstKeyValue, pKeyValue, len);
if (bCheckFlag == 1) {
kcvInfo.iCheckMode = 0x01;
kcvInfo.iDataLen = iCheckLen;
memcpy(kcvInfo.szCheckBuf, p, iCheckLen);
}
iRet = PedWriteKeyRev(&keyInfo, &kcvInfo);
///< PedCalcDESRev function
iRet = PedCalcDESRev(KEY_INDEX_TAK, PED_ECB_ENC, szGetBuffer, 8, szTmpBuf);
///< PedCalcDesDPARev function
iRet = PedCalcDesDPARev(mBuf, 16, mTemp, 16, mCheckValue, 16, PED_SYMM_MODE_ECB, PED_FUNC_ENCRYPT)
///< PedCalcAesDPARev function
ret = PedCalcAesDPARev(key,keylen,plainText,len,cipherText,sizeof(cipherText),PED_SYMM_MODE_ECB,PED_FUNC_ENCRYPT);
///< rsa function
int RsaDemo(void)
{
ST_RSA_KEY key;
unsigned int buffsize = 0;
int ret = 0;
int iLine;
int fail_cnt = 0;
char acDisp[64] = {0};
unsigned char plainText[257] = {0};
unsigned char cipherText[257] = {0};
unsigned char DecplainText[257] = {0};
buffsize = sizeof(key.aucExponent);
ret = HexStringToBytes(rsaKey_E, strlen(rsaKey_E), key.aucExponent, &buffsize);
if (ret)
{
sprintf(acDisp, "HexToBytes fail");
goto ERR;
}
else
{
logger_serial_dump_buff("E:", key.aucExponent, buffsize);
}
key.iExponentLen = buffsize;
buffsize = sizeof(key.aucModulus);
ret = HexStringToBytes(rsaKey_N, strlen(rsaKey_N), key.aucModulus, &buffsize);
if (ret)
{
sprintf(acDisp, "HexToBytes fail");
goto ERR;
}
else
{
logger_serial_dump_buff("N:", key.aucModulus, buffsize);
}
// pub key
key.iModulusLen = buffsize;
ret = PedWriteRsaKeyRev(1, &key);
if (ret)
{
sprintf(acDisp, "Pubkey write fail:%d", ret);
goto ERR;
}
// priv key
buffsize = sizeof(key.aucExponent);
ret = HexStringToBytes(rsaKey_D, strlen(rsaKey_D), key.aucExponent, &buffsize);
if (ret)
{
sprintf(acDisp, "HexToBytes fail");
goto ERR;
}
else
{
logger_serial_dump_buff("D:", key.aucExponent, buffsize);
}
key.iExponentLen = buffsize;
ret = PedWriteRsaKeyRev(2, &key);
if (ret)
{
sprintf(acDisp, "Privkey write fail:%d", ret);
goto ERR;
}
// Public Encrypt - Private Decrypt
memset(plainText, 0, sizeof(plainText));
plainText[0] = 0; // The first byte of the original text is 0, making sure it is less than n
plainText[1] = 0x02; // The second byte of the original text is not 0, ensuring the length of decryption
fail_cnt = 0;
do
{
ret = PedGetRandomRev(&plainText[2], key.iModulusLen - 2);
if (ret == (key.iModulusLen - 2))
{
fail_cnt = 0;
break;
}
else
{
fail_cnt++;
Sleep(100);
}
} while (fail_cnt < 10);
if (fail_cnt >= 10)
{
strcpy(acDisp, "get plainText fail too many times");
goto ERR;
}
// print plaintext
logger_serial_dump_buff("plainText:", plainText, key.iModulusLen);
// Public Encrypt
LOGD("[Public Encrypt]:");
memset(cipherText, 0, sizeof(cipherText));
ret = PedRsaRecoverRev(1, plainText, key.iModulusLen, cipherText);
if (ret > 0)
{
logger_serial_dump_buff("cipherText:", cipherText, ret);
}
else
{
strcpy(acDisp, "Public key encrypt fail");
goto ERR;
}
// Private Decrypt
LOGD("[Private Decrypt]:");
memset(DecplainText, 0, sizeof(DecplainText));
ret = PedRsaRecoverRev(2, cipherText, ret, DecplainText);
if (ret > 0)
{
logger_serial_dump_buff("DecplainText:", DecplainText, ret);
}
else
{
strcpy(acDisp, "Privat key decrypt fail");
goto ERR;
}
if (memcmp(DecplainText, &plainText[key.iModulusLen - ret], ret) != 0)
{
sprintf(acDisp, "Publickey Enc Dec not matched");
goto ERR;
}
// Private Encrypt - Public Decrypt
// Private Encrypt
LOGD("[Private Encrypt]:");
memset(cipherText, 0, sizeof(cipherText));
ret = PedRsaRecoverRev(2, plainText, key.iModulusLen, cipherText);
if (ret > 0)
{
logger_serial_dump_buff("cipherText:", cipherText, ret);
}
else
{
strcpy(acDisp, "Privat key encrypt fail");
goto ERR;
}
// Public Decrypt
LOGD("[Public Decrypt]:");
memset(DecplainText, 0, sizeof(DecplainText));
ret = PedRsaRecoverRev(1, cipherText, ret, DecplainText);
if (ret > 0)
{
logger_serial_dump_buff("DecplainText:", DecplainText, ret);
}
else
{
strcpy(acDisp, "Public key decrypt fail");
goto ERR;
}
if (memcmp(DecplainText, &plainText[key.iModulusLen - ret], ret) != 0)
{
sprintf(acDisp, "Privatekey Enc Dec not matched");
goto ERR;
}
mmi_clearLine(DISP_ClearALL);
iLine = mmi_display(DISP_Line2, DISP_Left, "Rsa Enc Dec success");
mmi_display((DISP_VAlign)iLine, DISP_Left, "result see log");
mmi_inputWaitKeypress(3);
ERR:
if (strlen(acDisp) > 0)
{
mmi_clearLine(DISP_ClearALL);
mmi_display(DISP_VCenter, DISP_HCenter, acDisp);
mmi_inputWaitKeypress(3);
}
return ret;
}
```