|
|
@@ -8,7 +8,7 @@
|
|
|
#include "mbedtls/base64.h"
|
|
|
#include "sys.h"
|
|
|
#include <stdio.h>
|
|
|
-
|
|
|
+#include "AT.h"
|
|
|
#define KEY_IOPAD_SIZE 64
|
|
|
|
|
|
#define SHA1_DIGEST_SIZE 20
|
|
|
@@ -20,27 +20,27 @@
|
|
|
#define AES_BLOCK_SIZE 16
|
|
|
|
|
|
//unsigned char buffer[AES_LEN_SIZE*2]={0};
|
|
|
-char plaintextdata[AES_LEN_SIZE]= {0};
|
|
|
+static char plaintextdata[AES_LEN_SIZE]= {0};
|
|
|
/**
|
|
|
* 填充源码,返回填充后的数据长度
|
|
|
*/
|
|
|
-static int fillAESPKCS7Data(char* data)
|
|
|
-{
|
|
|
- int left= 0;
|
|
|
- int len = strlen(data);
|
|
|
- if(len%AES_BLOCK_SIZE != 0)
|
|
|
- {
|
|
|
- left = AES_BLOCK_SIZE-strlen(data)%AES_BLOCK_SIZE;
|
|
|
-
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- left = AES_BLOCK_SIZE;
|
|
|
- }
|
|
|
- memset(data+strlen(data),left,left);
|
|
|
- len+=left;
|
|
|
- return len;
|
|
|
-}
|
|
|
+//static int fillAESPKCS7Data(char* data)
|
|
|
+//{
|
|
|
+// int left= 0;
|
|
|
+// int len = strlen(data);
|
|
|
+// if(len%AES_BLOCK_SIZE != 0)
|
|
|
+// {
|
|
|
+// left = AES_BLOCK_SIZE-strlen(data)%AES_BLOCK_SIZE;
|
|
|
+//
|
|
|
+// }
|
|
|
+// else
|
|
|
+// {
|
|
|
+// left = AES_BLOCK_SIZE;
|
|
|
+// }
|
|
|
+// memset(data+strlen(data),left,left);
|
|
|
+// len+=left;
|
|
|
+// return len;
|
|
|
+//}
|
|
|
|
|
|
|
|
|
static int fillAESPKCS7DataWithLength(uint8_t * data, uint16_t data_length)
|
|
|
@@ -123,19 +123,23 @@ void utils_hmac_sha1_str(const char *msg, int msg_len, char *digest, const char
|
|
|
mbedtls_sha1_free(&context);
|
|
|
|
|
|
}
|
|
|
-
|
|
|
+//static char testhexdata[512] ={0};
|
|
|
void utils_sha256(const char *msg, int msg_len, char * digest)
|
|
|
{
|
|
|
|
|
|
//hmac sha1加密处理
|
|
|
mbedtls_sha256_context context;
|
|
|
-
|
|
|
+// memset(testhexdata, 0, sizeof(testhexdata));
|
|
|
/* perform inner MD5 */
|
|
|
mbedtls_sha256_init(&context); /* init context for 1st pass */
|
|
|
mbedtls_sha256_starts(&context,0); /* setup context for 1st pass */
|
|
|
mbedtls_sha256_update(&context, (unsigned char *) msg, msg_len); /* then text of datagram */
|
|
|
mbedtls_sha256_finish(&context, (unsigned char *) digest); /* finish up 1st pass */
|
|
|
mbedtls_sha256_free(&context);
|
|
|
+// byteToHexStr((const unsigned char *)digest,testhexdata, strlen((char * )digest));
|
|
|
+// Log_Printf_Debug("数据报文msg:%s,%d\r\n", msg,msg_len);
|
|
|
+// Log_Printf_Debug("数据报文digest:%s\r\n", testhexdata);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
//void utils_sha256_str(const char *msg, int msg_len, char *digest)
|
|
|
@@ -155,39 +159,58 @@ void utils_sha256(const char *msg, int msg_len, char * digest)
|
|
|
/**
|
|
|
*AES CBC加密 , plaintext:源数据,ciphertext:加密数据, 返回加密长度
|
|
|
*/
|
|
|
-int utils_aes128_cbc_enc(char *aes_key, char *iv, char *plaintext, char *ciphertext)
|
|
|
-{
|
|
|
+char iv_use[16] = {0};
|
|
|
+//int utils_aes128_cbc_enc(uint8_t * aes_key, char *iv, char *plaintext, char *ciphertext)
|
|
|
+//{
|
|
|
|
|
|
- char iv_use[16] = {0};
|
|
|
- memset(plaintextdata, '\0', sizeof(plaintextdata));
|
|
|
- mbedtls_aes_context aes_ctx;
|
|
|
- if(strlen(plaintext)+16 > AES_LEN_SIZE) return 0;
|
|
|
- memcpy(iv_use,iv,16);
|
|
|
- memcpy(plaintextdata,(const char *)plaintext,strlen(plaintext));
|
|
|
- int len = fillAESPKCS7Data(plaintextdata);
|
|
|
-
|
|
|
- mbedtls_aes_init(&aes_ctx);
|
|
|
- //setkey_dec
|
|
|
- mbedtls_aes_setkey_enc(&aes_ctx, (unsigned char * )aes_key, 128);
|
|
|
- mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_ENCRYPT, len, (unsigned char * )iv_use, (unsigned char * )plaintextdata, (unsigned char * )ciphertext);
|
|
|
-
|
|
|
- mbedtls_aes_free(&aes_ctx);
|
|
|
- return len; //OK
|
|
|
+//
|
|
|
+// memset(testhexdata, 0, sizeof(testhexdata));
|
|
|
+// byteToHexStr((const unsigned char *)aes_key,testhexdata, 16);
|
|
|
+// Log_Printf_Debug("数据报文aes_key:%s,iv:%s\r\n", testhexdata,iv);
|
|
|
+// memset(testhexdata, 0, sizeof(testhexdata));
|
|
|
+// memset(plaintextdata, 0, sizeof(plaintextdata));
|
|
|
+// mbedtls_aes_context aes_ctx;
|
|
|
+// if(strlen(plaintext)+16 > AES_LEN_SIZE) return 0;
|
|
|
+// memset(iv_use,0,sizeof(iv_use));
|
|
|
+// memcpy(iv_use,iv,16);
|
|
|
+// memcpy(plaintextdata,(const char *)plaintext,strlen(plaintext));
|
|
|
+// byteToHexStr((const unsigned char *)plaintextdata,testhexdata, strlen((char * )plaintextdata));
|
|
|
+// Log_Printf_Debug("数据报文加密数据plaintextdata:%s\r\n", testhexdata);
|
|
|
+//// int len = fillAESPKCS7Data(plaintextdata);
|
|
|
+// int len = fillAESPKCS7DataWithLength((uint8_t *)plaintextdata, strlen((char * )plaintextdata));
|
|
|
+// memset(testhexdata, 0, sizeof(testhexdata));
|
|
|
+// byteToHexStr((const unsigned char *)plaintextdata,testhexdata, len);
|
|
|
+// Log_Printf_Debug("数据报文加密数据填充后plaintextdata:%s\r\n", testhexdata);
|
|
|
+//
|
|
|
+// mbedtls_aes_init(&aes_ctx);
|
|
|
+// //setkey_dec
|
|
|
+// mbedtls_aes_setkey_enc(&aes_ctx, aes_key, 128);
|
|
|
+// mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_ENCRYPT, len, (unsigned char * )iv_use, (unsigned char * )plaintextdata, (unsigned char * )ciphertext);
|
|
|
+// byteToHexStr((const unsigned char *)ciphertext,testhexdata, len);
|
|
|
+// Log_Printf_Debug("数据报文加密数据后ciphertext:%s,ciphertext:%d,testhexdata:%d,len2:%d\r\n", testhexdata,strlen((char * )ciphertext),strlen(testhexdata),len);
|
|
|
+//
|
|
|
+//
|
|
|
+// mbedtls_aes_free(&aes_ctx);
|
|
|
+// return len; //OK
|
|
|
|
|
|
-}
|
|
|
+//}
|
|
|
|
|
|
|
|
|
-int utils_aes128_cbc_enc_with_length(char *aes_key, char *iv, uint8_t * plaintext, uint16_t plaintext_length, uint8_t * ciphertext)
|
|
|
+int utils_aes128_cbc_enc_with_length(uint8_t *aes_key, char *iv, uint8_t * plaintext, uint16_t plaintext_length, uint8_t * ciphertext)
|
|
|
{
|
|
|
|
|
|
- char iv_use[16] = {0};
|
|
|
- memset(plaintextdata, '\0', sizeof(plaintextdata));
|
|
|
+
|
|
|
+
|
|
|
+ memset(plaintextdata, 0, sizeof(plaintextdata));
|
|
|
mbedtls_aes_context aes_ctx;
|
|
|
if(plaintext_length + 16 > AES_LEN_SIZE) return -1;
|
|
|
+ memset(iv_use,0,sizeof(iv_use));
|
|
|
memcpy(iv_use,iv,16);
|
|
|
memcpy(plaintextdata, (const char *)plaintext, plaintext_length);
|
|
|
+
|
|
|
int len = fillAESPKCS7DataWithLength((uint8_t *)plaintextdata, plaintext_length);
|
|
|
-
|
|
|
+
|
|
|
+
|
|
|
mbedtls_aes_init(&aes_ctx);
|
|
|
//setkey_dec
|
|
|
mbedtls_aes_setkey_enc(&aes_ctx, (unsigned char * )aes_key, 128);
|
|
|
@@ -203,12 +226,12 @@ int utils_aes128_cbc_enc_with_length(char *aes_key, char *iv, uint8_t * plaintex
|
|
|
int utils_aes128_ECB_base64_enc_with_length(char *aes_key, uint8_t * plaintext)
|
|
|
{
|
|
|
int times=0;
|
|
|
- memset(plaintextdata, '\0', sizeof(plaintextdata));
|
|
|
+ memset(plaintextdata, 0, sizeof(plaintextdata));
|
|
|
mbedtls_aes_context aes_ctx;
|
|
|
if(strlen((const char *)plaintext) + 16 > AES_LEN_SIZE) return -1;
|
|
|
memcpy(plaintextdata, plaintext, strlen((const char *)plaintext));
|
|
|
|
|
|
- int len = fillAESPKCS7Data(plaintextdata);
|
|
|
+ int len = fillAESPKCS7DataWithLength((uint8_t *)plaintextdata, strlen((const char *)plaintext));
|
|
|
|
|
|
mbedtls_aes_init(&aes_ctx);
|
|
|
//setkey_dec
|
|
|
@@ -231,7 +254,7 @@ int utils_aes128_ECB_base64_enc_with_length(char *aes_key, uint8_t * plaintext)
|
|
|
int utils_aes128_ECB_base64_dec(char *aes_key, uint8_t * plaintext, uint16_t recvdata_length)
|
|
|
{
|
|
|
int times=0;
|
|
|
- memset(plaintextdata, '\0', sizeof(plaintextdata));
|
|
|
+ memset(plaintextdata, 0, sizeof(plaintextdata));
|
|
|
mbedtls_aes_context aes_ctx;
|
|
|
if(strlen((const char *)plaintext) + 16 > AES_LEN_SIZE) return -1;
|
|
|
memcpy(plaintextdata, plaintext, strlen((const char *)plaintext));
|
|
|
@@ -253,7 +276,7 @@ int utils_aes128_ECB_base64_dec(char *aes_key, uint8_t * plaintext, uint16_t rec
|
|
|
/**
|
|
|
*AES CBC解密 ciphertext:加密数据, len:加密数据长度,plaintext:解密到的数据
|
|
|
*/
|
|
|
-int utils_aes128_cbc_dec(char *aes_key, char *iv, char *ciphertext, int len, char *plaintext)
|
|
|
+int utils_aes128_cbc_dec(uint8_t *aes_key, char *iv, char *ciphertext, int len, char *plaintext)
|
|
|
{
|
|
|
|
|
|
char iv_use[16] = {0};
|
|
|
@@ -262,7 +285,7 @@ int utils_aes128_cbc_dec(char *aes_key, char *iv, char *ciphertext, int len, cha
|
|
|
memcpy(iv_use,iv,16);
|
|
|
mbedtls_aes_init(&aes_ctx);
|
|
|
//setkey_dec
|
|
|
- mbedtls_aes_setkey_dec(&aes_ctx, (unsigned char * )aes_key, 128);
|
|
|
+ mbedtls_aes_setkey_dec(&aes_ctx, aes_key, 128);
|
|
|
mbedtls_aes_crypt_cbc(&aes_ctx, MBEDTLS_AES_DECRYPT, len, (unsigned char * )iv_use, (unsigned char * )ciphertext, (unsigned char * )plaintext);
|
|
|
cutAESPKCS7Data((char *)plaintext);
|
|
|
mbedtls_aes_free(&aes_ctx);
|