#include "main.h"
#include "communicate.h"
#include "cmox_crypto.h"
int lv_usart1_init(void);
int lv_delay_init(void);
/* Global variables ----------------------------------------------------------*/
/* CBC context handle */
cmox_cbc_handle_t Cbc_Ctx;
//__IO TestStatus glob_status = FAILED;
/* Private typedef -----------------------------------------------------------*/
/* Private defines -----------------------------------------------------------*/
#define CHUNK_SIZE 48u /* Chunk size (in bytes) when data to encrypt or decrypt are processed by chunk */
/* Private macros ------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
//密钥
const uint8_t Key[] =
{
0x2b, 0x7e, 0x15, 0x16, 0x28, 0xae, 0xd2, 0xa6, 0xab, 0xf7, 0x15, 0x88, 0x09, 0xcf, 0x4f, 0x3c
};
//偏移量
const uint8_t IV[] =
{
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
};
const uint8_t Plaintext[] =
{
0x6b, 0xc1, 0xbe, 0xe2, 0x2e, 0x40, 0x9f, 0x96, 0xe9, 0x3d, 0x7e, 0x11, 0x73, 0x93, 0x17, 0x2a,
0xae, 0x2d, 0x8a, 0x57, 0x1e, 0x03, 0xac, 0x9c, 0x9e, 0xb7, 0x6f, 0xac, 0x45, 0xaf, 0x8e, 0x51,
0x30, 0xc8, 0x1c, 0x46, 0xa3, 0x5c, 0xe4, 0x11, 0xe5, 0xfb, 0xc1, 0x19, 0x1a, 0x0a, 0x52, 0xef,
0xf6, 0x9f, 0x24, 0x45, 0xdf, 0x4f, 0x9b, 0x17, 0xad, 0x2b, 0x41, 0x7b, 0xe6, 0x6c, 0x37, 0x10
};
const uint8_t Expected_Ciphertext[] =
{
0x76, 0x49, 0xab, 0xac, 0x81, 0x19, 0xb2, 0x46, 0xce, 0xe9, 0x8e, 0x9b, 0x12, 0xe9, 0x19, 0x7d,
0x50, 0x86, 0xcb, 0x9b, 0x50, 0x72, 0x19, 0xee, 0x95, 0xdb, 0x11, 0x3a, 0x91, 0x76, 0x78, 0xb2,
0x73, 0xbe, 0xd6, 0xb8, 0xe3, 0xc1, 0x74, 0x3b, 0x71, 0x16, 0xe6, 0x9e, 0x22, 0x22, 0x95, 0x16,
0x3f, 0xf1, 0xca, 0xa1, 0x68, 0x1f, 0xac, 0x09, 0x12, 0x0e, 0xca, 0x30, 0x75, 0x86, 0xe1, 0xa7
};
/* Computed data buffer */
uint8_t Computed_Ciphertext[sizeof(Expected_Ciphertext)];
uint8_t Computed_Plaintext[sizeof(Plaintext)];
RCC_ClocksTypeDef get_rcc_clock;
int main(void)
{
cmox_cipher_retval_t retval;
size_t computed_size;
/* General cipher context */
cmox_cipher_handle_t *cipher_ctx;
/* Index for piecemeal processing */
uint32_t index;
lv_usart1_init();
lv_delay_init();
/*开启CRC,使用ST加密库需要开启CRC*/
if (cmox_initialize(NULL) != CMOX_INIT_SUCCESS)
{
printf("init error\r\n");
}
/***************************************************************************************************
* CRC-None加密
***************************************************************************************************/
retval = cmox_cipher_encrypt(CMOX_AESSMALL_CBC_ENC_ALGO, /* Use AES CBC algorithm */
Plaintext, sizeof(Plaintext)-1, /* Plaintext to encrypt */
Key, sizeof(Key), /* AES key to use */
IV, sizeof(IV), /* Initialization vector */
Computed_Ciphertext, &computed_size); /* Data buffer to receive generated ciphertext */
/* Verify API returned value */
if (retval != CMOX_CIPHER_SUCCESS)
{
printf("CBC ERROR\r\n");
}
printf("CBC encrypt data:\r\n");
for(int i=0; i<computed_size; i++)
{
printf("%x",Computed_Ciphertext[i]);
}
printf("\r\n");
/* Compute directly the plaintext passing all the needed parameters */
/* Note: CMOX_AES_CBC_DEC_ALGO refer to the default AES implementation
* selected in cmox_default_config.h. To use a specific implementation, user can
* directly choose:
* - CMOX_AESFAST_CBC_DEC_ALGO to select the AES fast implementation
* - CMOX_AESSMALL_CBC_DEC_ALGO to select the AES small implementation
*/
/********************************************************************************
*CRC-None解密
********************************************************************************/
retval = cmox_cipher_decrypt(CMOX_AES_CBC_DEC_ALGO, /* Use AES CBC algorithm */
Computed_Ciphertext, computed_size, /* Ciphertext to decrypt */
Key, sizeof(Key), /* AES key to use */
IV, sizeof(IV), /* Initialization vector */
Computed_Plaintext, &computed_size); /* Data buffer to receive generated plaintext */
/* Verify API returned value */
if (retval != CMOX_CIPHER_SUCCESS)
{
printf("CBC decrypt ERROR\r\n");
}
printf("CBC decrypt data:\r\n");
{
for(int i=0; i<computed_size; i++)
{
printf("%x",Computed_Plaintext[i]);
}
}
printf("\r\ncomputed_size:%d\r\n",computed_size);
/*******************************************************************************************
*
*SHA256演示
********************************************************************************************/
const uint8_t Message[] =
{
0x6b, 0x91, 0x8f, 0xb1, 0xa5, 0xad, 0x1f, 0x9c, 0x5e, 0x5d, 0xbd, 0xf1, 0x0a, 0x93, 0xa9, 0xc8,
0xf6, 0xbc, 0xa8, 0x9f, 0x37, 0xe7, 0x9c, 0x9f, 0xe1, 0x2a, 0x57, 0x22, 0x79, 0x41, 0xb1, 0x73,
0xac, 0x79, 0xd8, 0xd4, 0x40, 0xcd, 0xe8, 0xc6, 0x4c, 0x4e, 0xbc, 0x84, 0xa4, 0xc8, 0x03, 0xd1,
0x98, 0xa2, 0x96, 0xf3, 0xde, 0x06, 0x09, 0x00, 0xcc, 0x42, 0x7f, 0x58, 0xca, 0x6e, 0xc3, 0x73,
0x08, 0x4f, 0x95, 0xdd, 0x6c, 0x7c, 0x42, 0x7e, 0xcf, 0xbf, 0x78, 0x1f, 0x68, 0xbe, 0x57, 0x2a,
0x88, 0xdb, 0xcb, 0xb1, 0x88, 0x58, 0x1a, 0xb2, 0x00, 0xbf, 0xb9, 0x9a, 0x3a, 0x81, 0x64, 0x07,
0xe7, 0xdd, 0x6d, 0xd2, 0x10, 0x03, 0x55, 0x4d, 0x4f, 0x7a, 0x99, 0xc9, 0x3e, 0xbf, 0xce, 0x5c,
0x30, 0x2f, 0xf0, 0xe1, 0x1f, 0x26, 0xf8, 0x3f, 0xe6, 0x69, 0xac, 0xef, 0xb0, 0xc1, 0xbb, 0xb8,
0xb1, 0xe9, 0x09, 0xbd, 0x14, 0xaa, 0x48, 0xba, 0x34, 0x45, 0xc8, 0x8b, 0x0e, 0x11, 0x90, 0xee,
0xf7, 0x65, 0xad, 0x89, 0x8a, 0xb8, 0xca, 0x2f, 0xe5, 0x07, 0x01, 0x5f, 0x15, 0x78, 0xf1, 0x0d,
0xce, 0x3c, 0x11, 0xa5, 0x5f, 0xb9, 0x43, 0x4e, 0xe6, 0xe9, 0xad, 0x6c, 0xc0, 0xfd, 0xc4, 0x68,
0x44, 0x47, 0xa9, 0xb3, 0xb1, 0x56, 0xb9, 0x08, 0x64, 0x63, 0x60, 0xf2, 0x4f, 0xec, 0x2d, 0x8f,
0xa6, 0x9e, 0x2c, 0x93, 0xdb, 0x78, 0x70, 0x8f, 0xcd, 0x2e, 0xef, 0x74, 0x3d, 0xcb, 0x93, 0x53,
0x81, 0x9b, 0x8d, 0x66, 0x7c, 0x48, 0xed, 0x54, 0xcd, 0x43, 0x6f, 0xb1, 0x47, 0x65, 0x98, 0xc4,
0xa1, 0xd7, 0x02, 0x8e, 0x6f, 0x2f, 0xf5, 0x07, 0x51, 0xdb, 0x36, 0xab, 0x6b, 0xc3, 0x24, 0x35,
0x15, 0x2a, 0x00, 0xab, 0xd3, 0xd5, 0x8d, 0x9a, 0x87, 0x70, 0xd9, 0xa3, 0xe5, 0x2d, 0x5a, 0x36,
0x28, 0xae, 0x3c, 0x9e, 0x03, 0x25
};
uint8_t computed_hash[CMOX_SHA256_SIZE];
retval = cmox_hash_compute(CMOX_SHA256_ALGO, /* Use SHA256 algorithm */
Message, sizeof(Message), /* Message to digest */
computed_hash, /* Data buffer to receive digest data */
CMOX_SHA256_SIZE, /* Expected digest size */
&computed_size); /* Size of computed digest */
/* Verify API returned value */
if (retval != CMOX_HASH_SUCCESS)
{
printf("HASH ERROR\r\n");
}
printf("HASH data:\r\n");
for(int i=0; i<computed_size; i++)
{
printf("%x ",computed_hash[i]);
}
while(1)
{
}
}