OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 3893|回复: 2

stm32F103 aes ctr到底是怎么回事?

[复制链接]

1

主题

3

帖子

0

精华

新手上路

积分
34
金钱
34
注册时间
2016-8-16
在线时间
5 小时
发表于 2016-9-18 18:58:56 | 显示全部楼层 |阅读模式
5金钱
各位大侠好,我有个关于STM32 密码库的问题问大家,我是从ST的官网上下载的AES加密库,其中用到AES_CTR 方式用于加密从USART收发的指令,芯片用STM32F103VET6,加密库是STM32 Cryptographic library package V2.0.6,自己定义了KEY和IV,我的明文长度超过15个字节加密还原就出错了,请问是什么原因?另外可以提供或者提供链接关于在安卓平台或者windows平台的AES库吗?在网上找的ECB方式可以正确加密还原,CTR方式就不对。

void Encryption_CTR(const uint8_t* plaintext,\
          uint8_t* ciphertext,\
          const uint8_t* key,\
          const uint8_t* iv,\
          const uint16_t len)
{
  // outSize is for output size, retval is for return value
int32_t outSize, retval;   
  AESCTRctx_stt AESctx_st; // The AES context
   // Initialize Context Flag with default value
  AESctx_st.mFlags = E_SK_DEFAULT;
   // Set Iv size to 16
  AESctx_st.mIvSize=16;
   // Set key size to 16
  AESctx_st.mKeySize=CRL_AES128_KEY;
   // call init function
  retval = AES_CTR_Encrypt_Init(&AESctx_st, key, iv);
  if (retval != AES_SUCCESS)
  {  }

  retval = AES_CTR_Encrypt_Append(&AESctx_st, plaintext, len, ciphertext, &outSize);
  if (retval != AES_SUCCESS)
  {   }

  //Do the finalization call (in CTR it will not return any output)
  retval = AES_CTR_Encrypt_Finish(&AESctx_st, ciphertext+outSize, &outSize );
  if (retval != AES_SUCCESS)
  {   }   
}
/**
  * @brief  
  * @param  
  * @retval None
  */
void Decryption_CTR(uint8_t* plaintext,\
          const uint8_t* ciphertext,\
          const uint8_t* key,\
          const uint8_t* iv,\
          const uint16_t len)
{
  // outSize is for output size, retval is for return value
int32_t outSize, retval;   
  AESCTRctx_stt AESctx_st; // The AES context
   // Initialize Context Flag with default value
  AESctx_st.mFlags = E_SK_DEFAULT;
   // Set Iv size to 16
  AESctx_st.mIvSize=16;
   // Set key size to 16
  AESctx_st.mKeySize=CRL_AES128_KEY;
   // call init function
  retval = AES_CTR_Decrypt_Init(&AESctx_st, key, iv);
  if (retval != AES_SUCCESS)
  {  }

  retval = AES_CTR_Decrypt_Append(&AESctx_st, ciphertext, len, plaintext, &outSize);
  if (retval != AES_SUCCESS)
  {   }

  //Do the finalization call (in CTR it will not return any output)
  retval = AES_CTR_Decrypt_Finish(&AESctx_st, plaintext+outSize, &outSize );
  if (retval != AES_SUCCESS)
  {   }   
}           

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

11

主题

1044

帖子

0

精华

论坛元老

Rank: 8Rank: 8

积分
3720
金钱
3720
注册时间
2011-5-23
在线时间
2012 小时
发表于 2016-9-18 19:16:17 | 显示全部楼层
AES一般是按16字节对齐填充。
RT-Thread RTOS 音频,WIFI,蓝牙
回复

使用道具 举报

1

主题

3

帖子

0

精华

新手上路

积分
34
金钱
34
注册时间
2016-8-16
在线时间
5 小时
 楼主| 发表于 2016-9-19 13:55:16 | 显示全部楼层
aozima 发表于 2016-9-18 19:16
AES一般是按16字节对齐填充。

AES的CTR方式不是的,可以流加密,就是任意长度的意思
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-25 08:54

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表