|
@@ -28,6 +28,7 @@
|
|
|
#include "hal_module_adapter.h"
|
|
|
#include "UartTask.h"
|
|
|
#include "MainTask.h"
|
|
|
+#include <stdlib.h>
|
|
|
#include "app.h"
|
|
|
#include "numeric.h"
|
|
|
#include "Fota.h"
|
|
@@ -65,6 +66,10 @@ void battErrorStateDisplay(void);
|
|
|
void battLockStateDisplay(UINT8 lockState);
|
|
|
void SP_BMS_Update_Service(void);
|
|
|
BOOL BattHeaterSwitch(UINT8* heaterSwitch);
|
|
|
+UINT16 encryptionAlgorithm (UINT16 plainText);
|
|
|
+UINT8 decryptionAlgorithm (UINT16 cipherText);
|
|
|
+UINT8 Uart_Encrypt_Send(void);
|
|
|
+
|
|
|
//BMS升级函数声明
|
|
|
UINT8 SP_BMS_Update_CheckSUM(UINT8* pSendData,UINT8 len);
|
|
|
void SP_BMS_Update_Service();
|
|
@@ -80,7 +85,7 @@ static void UartTask(void* arg)
|
|
|
ARM_USART_PARITY_NONE |
|
|
|
ARM_USART_STOP_BITS_1 |
|
|
|
ARM_USART_FLOW_CONTROL_NONE, 9600);
|
|
|
- PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
|
|
|
+ PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_ENCRYPT);
|
|
|
UINT16 Reg_Num = 0;
|
|
|
UINT16 Uart_Uds_LEN;
|
|
|
UINT16 Uart_Recv_LEN;
|
|
@@ -101,6 +106,21 @@ static void UartTask(void* arg)
|
|
|
{
|
|
|
switch (gProcess_Uart_Task)
|
|
|
{
|
|
|
+ case PROCESS_UART_STATE_ENCRYPT:
|
|
|
+ {
|
|
|
+ UINT8 EncryptFlag=0x00;
|
|
|
+ UINT8 EncryptCount=0;
|
|
|
+ while(EncryptFlag!=0x01&&EncryptCount<=5)
|
|
|
+ {
|
|
|
+ EncryptFlag = Uart_Encrypt_Send();
|
|
|
+ EncryptCount++;
|
|
|
+ }
|
|
|
+ PROC_UART_STATE_SWITCH(PROCESS_UART_STATE_IDLE);
|
|
|
+ #ifdef USING_PRINTF
|
|
|
+ printf("[%d]PROCESS_UART_STATE_ENCRYPT Done\n",__LINE__);
|
|
|
+ #endif
|
|
|
+ break;
|
|
|
+ }
|
|
|
case PROCESS_UART_STATE_IDLE:
|
|
|
{
|
|
|
osDelay(100);
|
|
@@ -819,24 +839,74 @@ UINT8 decryptionAlgorithm (UINT16 cipherText)
|
|
|
return (UINT8)plainText;
|
|
|
}
|
|
|
|
|
|
-UINT16 encryptionAlgorithm (UINT8 plainText)
|
|
|
+UINT16 encryptionAlgorithm (UINT16 plainText)
|
|
|
{
|
|
|
- UINT16 cipherText = 1;
|
|
|
+ UINT16 cipherText = 1;
|
|
|
UINT16 privateKeyE = 37507;
|
|
|
UINT16 privateKeyN = 10961;
|
|
|
plainText = plainText % privateKeyN;
|
|
|
while(privateKeyE >0)
|
|
|
{
|
|
|
- if(privateKeyE % 2 ==1)
|
|
|
- {
|
|
|
- cipherText = cipherText * plainText % privateKeyN;
|
|
|
- }
|
|
|
- privateKeyE = privateKeyE/2;
|
|
|
- cipherText = (plainText * plainText) % privateKeyN;
|
|
|
+ if(privateKeyE % 2 ==1)
|
|
|
+ {
|
|
|
+ cipherText = ( cipherText * plainText) % privateKeyN;
|
|
|
+ }
|
|
|
+ privateKeyE = privateKeyE/2;
|
|
|
+ plainText = (plainText * plainText) % privateKeyN;
|
|
|
}
|
|
|
return cipherText;
|
|
|
}
|
|
|
+UINT8 Uart_Encrypt_Send()
|
|
|
+{
|
|
|
+ UINT8 SeedNumberArrray[4]={0x38,0x56,0xfe,0xac};
|
|
|
+ UINT16 EncodeNumberArray[4];
|
|
|
+ UINT8 UartEncryptBuffer[17];
|
|
|
+ UINT8 UartDecryptBuffer[5];
|
|
|
+ UINT16 CRC_chk_buffer;
|
|
|
+ UINT8 timeCount = 0;
|
|
|
+ UartEncryptBuffer[0] = BMS_ADDRESS_CODE;
|
|
|
+ UartEncryptBuffer[1] = UART_ENCRYPT_CODE;
|
|
|
+ UartEncryptBuffer[2] = 0x0c;
|
|
|
+ for(int i=0;i<4;i++)
|
|
|
+ {
|
|
|
+ SeedNumberArrray[i]=rand();
|
|
|
+ EncodeNumberArray[i] = encryptionAlgorithm(SeedNumberArrray[i]);
|
|
|
+ UartEncryptBuffer[i+3] = SeedNumberArrray[i];
|
|
|
+ UartEncryptBuffer[i*2+7] = EncodeNumberArray[i]>>8;
|
|
|
+ UartEncryptBuffer[i*2+8] = EncodeNumberArray[i];
|
|
|
+ }
|
|
|
+ CRC_chk_buffer = crc_chk(UartEncryptBuffer,17-2);
|
|
|
+ UartEncryptBuffer[15] = CRC_chk_buffer;
|
|
|
+ UartEncryptBuffer[16] = CRC_chk_buffer>>8;
|
|
|
+ USARTdrv->Send(UartEncryptBuffer,17);
|
|
|
+ USARTdrv->Receive(UartDecryptBuffer,5);
|
|
|
+ while((isRecvTimeout == false) && (isRecvComplete == false))
|
|
|
+ {
|
|
|
+ timeCount++;
|
|
|
+ osDelay(100);
|
|
|
+ if (timeCount>=10)
|
|
|
+ {
|
|
|
+ timeCount =0;
|
|
|
+ isRecvTimeout = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (isRecvComplete == true)
|
|
|
+ {
|
|
|
+ isRecvComplete = false;
|
|
|
+ return UartDecryptBuffer[2];
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ isRecvTimeout = false;
|
|
|
+ return 0x03;
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
|
|
|
+/*-----------------------------------------------------------------------------*/
|
|
|
void SP_BMS_Update_Service() //超力源BMS升级服务
|
|
|
{
|
|
|
|