forked from SZV10X_Software/SZV103_FM33A0xxEV_SiZhu

wujiazhi
2024-06-07 d0cca79a4aa7efce979c6aed275fe1bb75af4cc4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "main.h"
#include "gprs_sundry_api.h"
#include "AES128.h"
#include "gprs.h"
#include "cyclic_storage_cfg_api.h"
 
GPRS_PROTOCOL_ENCRYPT_PARA gprs_protocol_encrypt_para_g = {0};
 
GPRS_SAVE_SEND_PARA_T __attribute__ ((aligned (4))) gprs_save_send_para_g = {0};
 
 
 
/*
desc:ÏàËÆPKCS5PaddingµÄ²¹ÆëËã·¨
pLength£ºÐè²¹ÆëµÄ³¤¶È/²¹ÆëµÄÖµ
plen£ºÃ÷Îij¤¶È
return£º²¹ÆëºóµÄ³¤¶È
*/
uint16_t PKCS5Padding_like(uint8_t *pLength, uint16_t plen)
{
  uint8_t remain;
  if ((plen & 15) == 0) //µÈ¼ÛÓÚplen % 16 == 0
  {
    *pLength = 0;
    return plen;
  }else
    {
        remain = plen & 15;
    *pLength = 16 - remain;
    }
  return *pLength + plen;
}
 
/*
GPRS½ÓÊÕÊý¾ÝAES128½âÃÜ
pDecryptData : ½âÃܵÄÊý¾Ý
decrypt_data_length £º½âÃÜÊý¾Ý³¤¶È
*/
void Gprs_Recv_Data_Aes128_Ecb_Decrypt(uint8_t *pDecryptData, uint16_t decrypt_data_length)
{
  uint8_t cnt = 0;
#if GPRS_PROTOCOL_SELECT
    uint16_t decrypt_len = decrypt_data_length - PROTOCOL_HEAD_LENGTH - PROTOCOL_TAIL_LENGTH;
#else
    uint16_t decrypt_len = decrypt_data_length - PROTOCOL_HEAD_LENGTH - PROTOCOL_DATA_FIELD_HEAD_LENGTH - PROTOCOL_TAIL_LENGTH;
#endif
  if ((decrypt_len & 15) == 0)//µÈ¼ÛÓÚplen % 16 == 0
  {
    cnt = decrypt_len >> 4;
    for (uint8_t i = 0; i < cnt; i++)
      AES128_ECB_decrypt(&pDecryptData[16 * i], KVer_AES_128, &pDecryptData[16 * i]);
  }
}
 
/*
Ô¶´«·¢ËÍÊý¾ÝAES128Ìî³äºÍ¼ÓÃÜ
pEncryptData £º ¼ÓÃÜÊý¾Ý
*/
void Gprs_Send_Data_Aes128_Ecb_Encrypt_Padding(uint8_t *pEncryptData, GPRS_PROTOCOL_ENCRYPT_PARA *gprs_protocol_encrypt_para_p)
{
  uint8_t cnt = gprs_protocol_encrypt_para_p->padding_total_length >> 4; // ×ܳ¤±ØÐëΪ16µÄ±¶Êý X >> 4 µÈ¼ÛÓÚ X / 16
  /*¼ÓÃÜÊý¾ÝÌî³ä²¹ÆëÖÁ16µÄ±¶Êý*/
  if (gprs_protocol_encrypt_para_p->padding_length != 0)
  {
        /*²¹ÆëµÄÊý¾ÝΪȱÉÙµÄÖµ*/
#if GPRS_PROTOCOL_SELECT
        for (uint8_t i = 0;i < gprs_protocol_encrypt_para_p->padding_length; i++)
            pEncryptData[gprs_protocol_encrypt_para_p->plaintext_length + i] = gprs_protocol_encrypt_para_p->padding_length;
#else
        pEncryptData[gprs_protocol_encrypt_para_p->plaintext_length] = 0x80;
    for (uint8_t i = 0; i < gprs_protocol_encrypt_para_p->padding_length - 1; i++)
      pEncryptData[gprs_protocol_encrypt_para_p->plaintext_length + 1 + i] = 0;
#endif        
  }
  /*¼ÓÃÜÊý¾Ý*/
  for (uint8_t i = 0; i < cnt; i++)
    AES128_ECB_encrypt(&pEncryptData[16 * i], KVer_AES_128, &pEncryptData[16 * i]);
}
 
 
uint8_t Gprs_Protocol_GetSendDataNum_FrameSum(GPRS_SAVE_SEND_PARA_T * send_para_p)
{
    /*ʼÖÕΪ1*/
    gprs_protocol_head_tail_g.gprs_protocol_head.frame_num = 1;
    /*¼ÆËãÐèÒª·¢Ë͵ÄÌõÊý*/
#if    GPRS_HISTORY_OR_REAL_DATA    
    send_para_p->now_send_data_num = send_para_p->last_remain_data_num + send_para_p->send_data_log_save_cnt; // ±¾´Î·¢Ë͵ÄÌõÊý = ÉÏ´ÎÁôϵÄÌõÊý + Éϴε½±¾´Î·¢ËÍÖ®¼äµÄ¼ä¸ô´¢´æÌõÊý
#else
    send_para_p->now_send_data_num = 1; // ÔÝʱĬÈÏΪһÌõ
#endif
    if(send_para_p->now_send_data_num > 72)
        send_para_p->now_send_data_num = 24;
    send_para_p->last_remain_data_num = send_para_p->now_send_data_num;
    send_para_p->send_data_log_save_cnt = 0;
#if    GPRS_HISTORY_OR_REAL_DATA        
    send_para_p->start_addr = Sys_Log_E2pCyclic_ReverseGetStartAddr(&sys_log_cyclic_table[CYCL_SEND_FLOW_DATA], send_para_p->now_send_data_num);
#endif
    /*¼ÆËã·¢Ë͵Ä×ÜÖ¡Êý*/
    if ((send_para_p->now_send_data_num % SEND_DATA_PACK_MAX) == 0) 
        return send_para_p->now_send_data_num / SEND_DATA_PACK_MAX; 
    else
        return (send_para_p->now_send_data_num / SEND_DATA_PACK_MAX) + 1;            
}