cancel
Showing results for 
Search instead for 
Did you mean: 

Usage of CRYPTO LIBRARY

ABatt.1
Senior

I am trying to use the cryptographic library on a STM32F103, but I cannot find any documentation that is in line with the library and functions.

In UM1924 rev.8 we still speak of version 3, but I cannot find the mnual consideration of rev. 4.0.1 (there is no user manual on the cdocumentation page).

Where can I find the documentation for the correct management of the library functions?

Thank you all for your support

4 REPLIES 4

https://wiki.st.com/stm32mcu/wiki/Security:Getting_started_with_the_Cryptographic_Library

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
ABatt.1
Senior

Hi the wiki was useful, but i'm an implementation issue and I can't figure out where is the problem.

I started from example in ...\STM32CubeExpansion_Crypto_V4.0.1\Projects\NUCLEO-G071RB\Applications\Cipher\AES_CBC_EncryptDecrypt\Src

In order to keep main clear, I wrote an Crypto_Inizializzazione() function like this:

cmox_cbc_handle_t Cbc_Ctx;
cmox_cipher_retval_t retval;
 
const uint8_t Key[] = {'1','2','3','4','5','6','7','8','9','1','2','3','4','5','6','7'};
const uint8_t IV[] = {'1','2','3','4','5','6','7','8','9','1','2','3','4','5','6','7'};
 
 
uint8_t Crypto_Inizializzazione(cmox_cipher_handle_t * cipher_ctx ){
 
	/* Initialize cryptographic library */
	if (cmox_initialize(NULL) != CMOX_INIT_SUCCESS){
		return 1;
	}
 
	/* Cleanup the context */
	//retval = cmox_cipher_cleanup(cipher_ctx);
	//if (retval != CMOX_CIPHER_SUCCESS){
	//	return 2;
	//}
 
	cipher_ctx = cmox_cbc_construct(&Cbc_Ctx, CMOX_AESSMALL_CBC_ENC);
	if (cipher_ctx == NULL){
		return 3;
	}
 
	/* Initialize the cipher context */
	retval = cmox_cipher_init(cipher_ctx);
	if (retval != CMOX_CIPHER_SUCCESS){
		return 4;
	}
 
	/* Setup of the encryption key into the context */
	retval = cmox_cipher_setKey(cipher_ctx, Key, sizeof(Key));  /* AES key to use */
	if (retval != CMOX_CIPHER_SUCCESS){
		return 5;
	}
 
	/* Setup of the Initialization Vector (IV) into the context */
	retval = cmox_cipher_setIV(cipher_ctx, IV, sizeof(IV));     /* Initialization vector */
	if (retval != CMOX_CIPHER_SUCCESS){
		return 6;
	}
 
	return 0;
}

In main I read 16 bytes from SD, encrypt it, and I write encrypted 16 bytes on SD (in another file).

int main(void)
{
  /* USER CODE BEGIN 1 */
	FATFS StrutturaCtrFS;
	FIL File_RD, File_WD; /* File objects */
	FRESULT Risultato;
	uint8_t Risultato_tmp;
	FILINFO fno;
	cmox_cipher_handle_t * cryptoHandler;
	cmox_cipher_retval_t CRYRisultato;
	size_t computed_size;
	char NOME_FILE[13], buffer[13];
	uint8_t BufferTest_1[16], BufferTest_2[16];
 
  /* USER CODE END 1 */
 
  /* MCU Configuration--------------------------------------------------------*/
 
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();
 
  /* USER CODE BEGIN Init */
 
  /* USER CODE END Init */
 
  /* Configure the system clock */
  SystemClock_Config();
 
  /* USER CODE BEGIN SysInit */
 
  /* USER CODE END SysInit */
 
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_SPI2_Init();
  MX_ADC1_Init();
  MX_USART1_UART_Init();
  MX_USART3_UART_Init();
  MX_CRC_Init();
  MX_FATFS_Init();
  /* USER CODE BEGIN 2 */
 
  //Accendo le periferiche
  BACKLIGHT_ON();
Risultato_tmp = Crypto_Inizializzazione(cryptoHandler);
  if(0 != Risultato_tmp){
	while(1);
  }
 
  Risultato = f_mount(&StrutturaCtrFS,"",1);
  if(FR_OK != Risultato){
	  while(1);
  }
 
  memcpy(NOME_FILE,"Upgrade.bin",12);
  Risultato = f_open(&File_RD,NOME_FILE, FA_READ);
  if(FR_OK != Risultato){
	  while(1);
  }
 
  memcpy(NOME_FILE,"Upgrade.out",12);
  Risultato = f_open(&File_WD,NOME_FILE, FA_CREATE_ALWAYS | FA_WRITE);
  if(FR_OK != Risultato){
	  f_close(&File_RD);
	  while(1);
  }
 
  numBytes = 16;
  while(numBytes > 15){
	  f_read(&File_RD,BufferTest_1, 16, &numBytes);
	  if(numBytes < 16){
		  memset(&BufferTest_1[numBytes],0xFF,(16-numBytes));
	  }
	  memset(BufferTest_2,0,16);
	  CRYRisultato = cmox_cipher_append(cryptoHandler,(const uint8_t *)BufferTest_1, (size_t)16,BufferTest_2, &computed_size);
	  f_write(&File_WD,BufferTest_2, computed_size, &numBytes);
  }
 
  f_close(&File_RD);
  f_close(&File_WD);
  BACKLIGHT_OFF();
  while(1);

Init of cypher is ok (return 0), but cmox_cipher_append return code 0x10003 (I thinks is CMOX_CIPHER_ERR_BAD_PARAMETER) and computed_size is 0.

I checked every input parameter (adding also a cast) but I don't fined the issue.

The only questionable point is the cmox_cipher_handle_t * cryptoHandler pointer: I do not make any memory allocation for this pointer. it's correct ? it is the task of the cmox_cbc_construct function ?

Where I can find futher information ?

ABatt.1
Senior

Has anyone ever had the same problem as me?

I'd wager money on it..

Problem solving typically revolves around finding what the problem is and fixing it, and doesn't necessarily require someone to have previously come up with a solution. Apply reason and logic.

You aren't going to get the assignment back into main() the way you've constructed this. You'd presumably need a pointer to a pointer

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..