Skip to main content
jgroff
Associate
August 16, 2025
Question

STM32H7S7 Encrypt/DeCrypt Issue with Shared Key

  • August 16, 2025
  • 0 replies
  • 1163 views

I'm trying to understand an issue I am having with chaining decryption, using the shared key out of the SAES and have distilled the problem to the following example:

I am attempting to encrypt and decrypt large blocks of data (65536 bytes). I can encrypt and decrypt the entire block in one fell swoop with one call to Encrypt and one call to Decrypt. I can encrypt and decrypt the entire block in small chunks with repeated calls to Encrypt and Decrypt back to back (En, De, En, De, ...). However if I encrypt the entire block in chunks then decrypt the entire block in chunks (En, En, En, ... De, De De, ...), I get bad data as my decrypted text.

I'm trying to understand what I am setting up incorrectly for the decrypted data to be bad. I'm using a STM32H7S78-DK board, although any STM32H7Sxxx could be used in this example. I am using a clean CRYP_SAES_SharedKey_Appli project. I have added the following code to the project. Any help on this problem would be greatly appreciated:

uint8_t plaintext[65536];
uint8_t crypttext[65536];
uint8_t decryptedtext[65536];
 
 /* Initialise CRYP */
 if (HAL_CRYP_Init(&hcryp) != HAL_OK)
 {
 /* Initialization Error */
 Error_Handler();
 }

 // ADDED

 for (int i = 0; i < 65536; i++) {
	 plaintext[i] = i % 256;
	 crypttext[i] = 0;
	 decryptedtext[i] = 0;
 }

 for (int i = 0; i < 64; i++) {
	 HAL_CRYP_Encrypt(&hcryp, ((uint32_t*)plaintext) + (128 * i), 128, ((uint32_t*)crypttext) + (128 * i), 0xfff);
	 HAL_CRYP_Decrypt(&hcryp, ((uint32_t*)crypttext) + (128 * i), 128, ((uint32_t*)decryptedtext) + (128 * i), 0xfff);
 }

 volatile int cmpret = 0;
 cmpret = memcmp(plaintext, decryptedtext, 2048); //<--- THIS WORKS OK

 for (int i = 0; i < 65536; i++) {
	 crypttext[i] = 0;
	 decryptedtext[i] = 0;
 }

 for (int i = 0; i < 64; i++) {
	 HAL_CRYP_Encrypt(&hcryp, ((uint32_t*)plaintext) + (128 * i), 128, ((uint32_t*)crypttext) + (128 * i), 0xfff);
 }

 for (int i = 0; i < 64; i++) {
	 HAL_CRYP_Decrypt(&hcryp, ((uint32_t*)crypttext) + (128 * i), 128, ((uint32_t*)decryptedtext) + (128 * i), 0xfff);
 }

 cmpret = memcmp(plaintext, decryptedtext, 2048); // <--- THIS DOES NOT EQUAL!
 HAL_Delay(1);

 // END ADD

// /* AES ECB Encryption */
// if (HAL_CRYP_Encrypt(&hcryp, Plaintext, 16, EncryptedText, TIMEOUT_VALUE) != HAL_OK)
// {