Example code of AES CTR mode

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Scaber2001
Level 1
Level 1
First reply posted First like given First question asked

Hi there,

After downloading Cryptography_AES_Demostration example codes from Github, I have tried all AES ECB mode, CBC mode, and CFB mode, all of them works fine.

However, I got some trouble in calling Cy_Crypto_Core_Aes_Ctr. Specifically speaking, I have no idea how to provide the parameters of this API function.

Do you have any example codes to show me how to encrypt/decrypt by AES CTR mode?

Hardware : CY8KIT-062-BLE

ModusToolbox version: 2.4.0

 

Thanks,

 

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.
Sharath_V
Moderator
Moderator
Moderator
First comment on blog 250 sign-ins 100 replies posted

Hi @Scaber2001 :

Please find a crude example of encrypting "Hello World" and decrypting the same using AES counter mode. We only encrypt 1 block here as we have hardcoded the sizes to 16 bytes (128-bits). 

In the attached main.c file example, we have done the following:

  1. srcOffset : This is set to 0. This is initially don't care or can be 0. This is updated by the AES counter API when the data stream (or plaintext) you want to encrypt is not a multiple of 16 bytes. For instance, if you send a plaintext of 50 bytes, this is set to 2 (50 % 16) by the API. 
  2. streamBlock : This is currently unused in the crypto implementation, so it can just be a uint8_t variable and its address can be passed to the API. You can even pass NULL to this. This parameter is actually used in tandem with  srcOffset to XOR the next stream of plaintext data. For instance, this buffer will contain the already calculated stream key, and it starts XOR from srcOffset of key stream (2) on next batch of plaintext.

View solution in original post

3 Replies
Shreya_S
Moderator
Moderator
Moderator
First comment on blog 100 replies posted 25 solutions authored

Hello,

Currently we do not have any examples related to this, but you can refer to the link.

I hope it clarifies the doubts you have regarding the parameters.
In case things aren't clear, feel free to reach us out.

Hi,

Could you please guide me how to give parameters "srcOffset" and "streamBlock"?

Does "srcOffset" is kind of variable like "count of block"?

What I should declare for "streamBlock"? Is it an unit8_t array and what size it should? What content I should give as default values?

Thanks

0 Likes
lock attach
Attachments are accessible only for community members.
Sharath_V
Moderator
Moderator
Moderator
First comment on blog 250 sign-ins 100 replies posted

Hi @Scaber2001 :

Please find a crude example of encrypting "Hello World" and decrypting the same using AES counter mode. We only encrypt 1 block here as we have hardcoded the sizes to 16 bytes (128-bits). 

In the attached main.c file example, we have done the following:

  1. srcOffset : This is set to 0. This is initially don't care or can be 0. This is updated by the AES counter API when the data stream (or plaintext) you want to encrypt is not a multiple of 16 bytes. For instance, if you send a plaintext of 50 bytes, this is set to 2 (50 % 16) by the API. 
  2. streamBlock : This is currently unused in the crypto implementation, so it can just be a uint8_t variable and its address can be passed to the API. You can even pass NULL to this. This parameter is actually used in tandem with  srcOffset to XOR the next stream of plaintext data. For instance, this buffer will contain the already calculated stream key, and it starts XOR from srcOffset of key stream (2) on next batch of plaintext.