解密在STM32L082xx上没有按预期工作。
这是我的尝试:我使用全零作为iv和key,所有0x42用于纯文本。
如果我在python中做同样的事情,我得到一个不同的结果: 我得到了
{0x7c,0xA5,0xDA,为0xBF,0x97,0x18 ....}
注意我正在使用不受字节顺序影响的键和IV,因此参数CRYP_DATATYPE_8B不应受影响。我已经使用CRYP_DATATYPE_16B,CRYP_DATATYPE_1B,CRYP_DATATYPE_32B进行了测试但没有成功。有什么想法吗?
我也尝试过使用EBC,但没有成功。
void TEST_decryption(){
uint8_t encrypted[128]={0x00};
uint8_t decrypted[128]={0x00};
for(int i=0;i<128;i++){
decrypted[i]=0x42;
}
HAL_CRYP_AESCBC_Encrypt(&hcryp, decrypted, 128, encrypted, 10000);
if(encrypted[0]==0xbf){
while(1); //pass
}else{
while(1); //fail
}
}
uint8_t iv[16]={0x00};
uint8_t userKey[16]={0x00};
/* AES init function */
void MX_AES_Init(void)
{
hcryp.Instance = AES;
hcryp.Init.DataType = CRYP_DATATYPE_8B;
hcryp.Init.pKey = &userKey[0];
hcryp.Init.pInitVect = &iv[0];
if (HAL_CRYP_Init(&hcryp) != HAL_OK)
{
Error_Handler();
}
__HAL_RCC_AES_CLK_ENABLE();
}
在python中也是如此:
from Crypto.Cipher import AES
key = binascii.unhexlify('0'*32)
IV = binascii.unhexlify('0'*32)
encryptor = AES.new(key, AES.MODE_CBC, IV=IV)
print(binascii.hexlify(encryptor.encrypt(binascii.unhexlify('42'*128))))
输出:
b'bfdaa57cb812189713a950ad99478879ec40e0761ed6475fca829d311af9ab3c72099b8b728c5145dc58f99d4fd9f0466ea50ca1a42a98560407c8e716e32bab1db3b30baa48939e253343b3a20f519767bdbb0f9083540b0ba14d289673c8129ae4c31855bf8a35d8ee1a22ce26337c2987e46fde5b448d1021682f5999ab49'
查看即时演示:https://repl.it/repls/ImpracticalWhitesmokeCodec
编辑:
建议将密钥大小设置为128位,这在此库中是不可能的:
/**
* @brief Writes the Key in Key registers.
* @param hcryp: pointer to a CRYP_HandleTypeDef structure that contains
* the configuration information for CRYP module
* @param Key: Pointer to Key buffer
* @note Key must be written as little endian.
* If Key pointer points at address n,
* n[15:0] contains key[96:127],
* (n+4)[15:0] contains key[64:95],
* (n+8)[15:0] contains key[32:63] and
* (n+12)[15:0] contains key[0:31]
* @retval None
*/
static void CRYP_SetKey(CRYP_HandleTypeDef *hcryp, uint8_t *Key)
{
uint32_t keyaddr = (uint32_t)Key;
hcryp->Instance->KEYR3 = __REV(*(uint32_t*)(keyaddr));
keyaddr+=4U;
hcryp->Instance->KEYR2 = __REV(*(uint32_t*)(keyaddr));
keyaddr+=4U;
hcryp->Instance->KEYR1 = __REV(*(uint32_t*)(keyaddr));
keyaddr+=4U;
hcryp->Instance->KEYR0 = __REV(*(uint32_t*)(keyaddr));
}