예제 #1
0
파일: main.c 프로젝트: HorseMa/contiki
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void main(void)
{
  uint8_t arrayindex = 0;
  ErrorStatus cryptostatus = ERROR;

  /* Enable AES clock */
  CLK_PeripheralClockConfig(CLK_Peripheral_AES, ENABLE);

  /* Initialize LEDs mounted on STM8L1528-EVAL board */
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED3);

  /* Select the encryption mode */
  AES_OperationModeConfig(AES_Operation_Encryp);

  /* Enable the AES peripheral */
  AES_Cmd(ENABLE);

  /* write 32 times in the DINR register with encryption key and plain text */
  while (arrayindex < 16)
  {
    /* writing MSB first */
    AES_WriteSubKey(EncryptionKey[arrayindex]);
    AES_WriteSubData(PlainText[arrayindex]);
    /* Increment arrayindex */
    arrayindex++;
  }

  /* Wait for CCF flag to be set */
  while (AES_GetFlagStatus(AES_FLAG_CCF) == RESET)
  {}

  /* Clear CCF flag */
  AES_ClearFlag(AES_FLAG_CCF);

  /* Init arrayindex */
  arrayindex = 0;

  /* read 16 times the DOUTR register to get the cipher text */
  while (arrayindex < 16)
  {
    /* Reading MSB first */
    CypherText[arrayindex] = AES_ReadSubData();
    /* Increment arrayindex */
    arrayindex++;
  }

  /* Disable the AES peripheral to change the operation mode */
  AES_Cmd(DISABLE);

  /* Select the key derivation and decryption mode */
  AES_OperationModeConfig(AES_Operation_KeyDerivAndDecryp);

  /* Re-enable the AES peripheral */
  AES_Cmd(ENABLE);

  /* Init arrayindex */
  arrayindex = 0;

  /* write 32 times in the DINR register with encryption key and cypher text */
  while (arrayindex < 16)
  {
    /* writing MSB first */
    AES_WriteSubKey(EncryptionKey[arrayindex]);
    AES_WriteSubData(CypherText[arrayindex]);
    /* Increment arrayindex */
    arrayindex++;
  }

  /* Wait for CCF flag to be set */
  while (AES_GetFlagStatus(AES_FLAG_CCF) == RESET)
  {}

  /* Clear CCF flag */
  AES_ClearFlag(AES_FLAG_CCF);

  /* Init arrayindex */
  arrayindex = 0;

  /* read 16 times the DOUTR register to get the computed plain text */
  while (arrayindex < 16)
  {
    /* Reading MSB first */
    ComputedPlainText[arrayindex] = AES_ReadSubData();
    /* Increment arrayindex */
    arrayindex++;
  }

  /* Check if decrypted cypher text match the original plain text */
  cryptostatus = Buffercmp(PlainText, ComputedPlainText, 16);

  if (cryptostatus == SUCCESS)
  {
    /* Turn on LD1 */
    STM_EVAL_LEDOn(LED1);
  }
  else
  {
    /* Turn on LD3 */
    STM_EVAL_LEDOn(LED3);
  }
  /* Infinite loop */
  while (1)
  {}
}
/**
  * @brief  Encrypt using AES in ECB Mode
  * @param  Key: Key used for AES algorithm.
  * @param  Input: pointer to the Input buffer.
  * @param  Ilength: length of the Input buffer, must be a multiple of 16 bytes.
  * @param  Output: pointer to the returned buffer.
  * @retval An ErrorStatus enumeration value:
  *          - SUCCESS: Operation done
  *          - ERROR: Operation failed
  */
ErrorStatus AES_ECB_Encrypt(uint8_t * Key, uint8_t * Input, uint32_t Ilength,
			    uint8_t * Output)
{
	AES_InitTypeDef AES_InitStructure;
	AES_KeyInitTypeDef AES_KeyInitStructure;
	ErrorStatus status = SUCCESS;
	uint32_t keyaddr = (uint32_t) Key;
	uint32_t inputaddr = (uint32_t) Input;
	uint32_t outputaddr = (uint32_t) Output;
	__IO uint32_t counter = 0;
	uint32_t ccstatus = 0;
	uint32_t i = 0;

	/* AES Key initialisation */
	AES_KeyInitStructure.AES_Key3 = __REV(*(uint32_t *) (keyaddr));
	keyaddr += 4;
	AES_KeyInitStructure.AES_Key2 = __REV(*(uint32_t *) (keyaddr));
	keyaddr += 4;
	AES_KeyInitStructure.AES_Key1 = __REV(*(uint32_t *) (keyaddr));
	keyaddr += 4;
	AES_KeyInitStructure.AES_Key0 = __REV(*(uint32_t *) (keyaddr));
	AES_KeyInit(&AES_KeyInitStructure);

	/* AES configuration */
	AES_InitStructure.AES_Operation = AES_Operation_Encryp;
	AES_InitStructure.AES_Chaining = AES_Chaining_ECB;
	AES_InitStructure.AES_DataType = AES_DataType_8b;
	AES_Init(&AES_InitStructure);

	/* Enable AES */
	AES_Cmd(ENABLE);

	for (i = 0; ((i < Ilength) && (status != ERROR)); i += 16) {
		AES_WriteSubData(*(uint32_t *) (inputaddr));
		inputaddr += 4;
		AES_WriteSubData(*(uint32_t *) (inputaddr));
		inputaddr += 4;
		AES_WriteSubData(*(uint32_t *) (inputaddr));
		inputaddr += 4;
		AES_WriteSubData(*(uint32_t *) (inputaddr));
		inputaddr += 4;

		/* Wait for CCF flag to be set */
		counter = 0;
		do {
			ccstatus = AES_GetFlagStatus(AES_FLAG_CCF);
			counter++;
		} while ((counter != AES_CC_TIMEOUT) && (ccstatus == RESET));

		if (ccstatus == RESET) {
			status = ERROR;
		} else {
			/* Clear CCF flag */
			AES_ClearFlag(AES_FLAG_CCF);
			/* Read cipher text */
			*(uint32_t *) (outputaddr) = AES_ReadSubData();
			outputaddr += 4;
			*(uint32_t *) (outputaddr) = AES_ReadSubData();
			outputaddr += 4;
			*(uint32_t *) (outputaddr) = AES_ReadSubData();
			outputaddr += 4;
			*(uint32_t *) (outputaddr) = AES_ReadSubData();
			outputaddr += 4;
		}
	}

	/* Disable AES before starting new processing */
	AES_Cmd(DISABLE);

	return status;
}
예제 #3
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
void main(void)
{
  uint8_t arrayindex = 0;
  uint8_t index1 = 0, index2 = 0;
  ErrorStatus cryptostatus = ERROR;

  /* Initialize LEDs mounted on STM8L1528-EVAL board */
  STM_EVAL_LEDInit(LED1);
  STM_EVAL_LEDInit(LED3);


  /****************************************************************************/
  /*                           Encryption phase                               */
  /****************************************************************************/

  /* Prepare the buffer to be transferred by DMA: Alternating key and plain text */
  while (arrayindex < PLAINTEXT_SIZE * 2)
  {
    SrcBuffer[arrayindex] = EncryptionKey[index1];
    arrayindex++;
    SrcBuffer[arrayindex] = PlainText[index2];
    arrayindex++;
    index1++;
    index2++;
    if (index1 == 16) index1 = 0;
  }

  /* DMA configuration to transfer data to/from AES --------------------------*/
  /* Enable DMA1 clock */
  CLK_PeripheralClockConfig(CLK_Peripheral_DMA1, ENABLE);
  /* DMA DeInit */
  DMA_GlobalDeInit();
  DMA_DeInit(DMA1_Channel0);
  DMA_DeInit(DMA1_Channel3);

  /* DMA1 channel 0 configuration
    Input phase: data transfer from memory "SrcBuffer" to AES_DINR register */
  DMA_Init(DMA1_Channel0, (uint16_t)SrcBuffer, AES_DINR_ADDRESS, PLAINTEXT_SIZE * 2,
           DMA_DIR_MemoryToPeripheral, DMA_Mode_Normal, DMA_MemoryIncMode_Inc,
           DMA_Priority_VeryHigh, DMA_MemoryDataSize_Byte);

  /* DMA1 channel 3 configuration
   Output phase: data transfer from AES_DOUTR register to memory "CypherText" */
  DMA_Init(DMA1_Channel3, (uint16_t)CypherText, AES_DOUTR_ADDRESS, PLAINTEXT_SIZE,
           DMA_DIR_PeripheralToMemory, DMA_Mode_Normal, DMA_MemoryIncMode_Inc,
           DMA_Priority_VeryHigh, DMA_MemoryDataSize_Byte);

  /* DMA1 Channel 0 and channel 3 enable */
  DMA_Cmd(DMA1_Channel0, ENABLE);
  DMA_Cmd(DMA1_Channel3, ENABLE);
  /* DMA1 global enable */
  DMA_GlobalCmd(ENABLE);

  /* AES configuration to encrypt data using DMA transfer --------------------*/
  /* Enable AES clock */
  CLK_PeripheralClockConfig(CLK_Peripheral_AES, ENABLE);
  /* Select the encryption mode */
  AES_OperationModeConfig(AES_Operation_Encryp);
  /* Enable using DMA for data transfer */
  AES_DMAConfig(AES_DMATransfer_InOut, ENABLE);
  /* Enable the AES peripheral: the AES initiates the DMA request */
  AES_Cmd(ENABLE);

  /* Wait for transfer from AES_DOUTR to memory to be completed */
  while (DMA_GetFlagStatus(DMA1_FLAG_TC3) == RESET);

  /****************************************************************************/
  /*                             Decryption phase                             */
  /****************************************************************************/

  /* Prepare the buffer to be transferred by DMA: Alternating key and cypher text */
  arrayindex = 0;
  index1 = 0;
  index2 = 0;
  while (arrayindex < PLAINTEXT_SIZE * 2)
  {
    SrcBuffer[arrayindex] = EncryptionKey[index1];
    arrayindex++;
    SrcBuffer[arrayindex] = CypherText[index2];
    arrayindex++;
    index1++;
    index2++;
    if (index1 == 16) index1 = 0;
  }

  /* Disable the AES peripheral to change AES operation mode */
  AES_Cmd(DISABLE);
  /*  DeInit DMA1 channel 3 */
  DMA_DeInit(DMA1_Channel3);
  /* DMA1 global disable */
  DMA_GlobalCmd(DISABLE);

  /* DMA1 channel 0 is already configured in Encryption phase
    Input phase: data transfer from memory "SrcBuffer" to AES_DINR register */
  /* Reconfigure channel 0 counter to start a new transfer */
	DMA_Cmd(DMA1_Channel0, DISABLE);
  DMA_SetCurrDataCounter(DMA1_Channel0, PLAINTEXT_SIZE * 2);

  /* DMA1 channel 3 configuration
    Output phase: data transfer from AES_DOUTR register to memory "ComputedPlainText" */
  DMA_Init(DMA1_Channel3, (uint16_t)ComputedPlainText, AES_DOUTR_ADDRESS, PLAINTEXT_SIZE,
           DMA_DIR_PeripheralToMemory, DMA_Mode_Normal, DMA_MemoryIncMode_Inc,
           DMA_Priority_VeryHigh, DMA_MemoryDataSize_Byte);

  /* DMA1 Channel 0 and Channel 3 enable */
  DMA_Cmd(DMA1_Channel3, ENABLE);
  DMA_Cmd(DMA1_Channel0, ENABLE);
  /* DMA1 global enable */
  DMA_GlobalCmd(ENABLE);

  /********** AES configuration to decrypt data using DMA transfer  ***********/
  /* Select the key derivation and decryption mode */
  AES_OperationModeConfig(AES_Operation_KeyDerivAndDecryp);
  /* Enable the AES peripheral */
  AES_Cmd(ENABLE);

  /* wait for transfer from AES_DOUTR register to memory to be completed */
  while (DMA_GetFlagStatus(DMA1_FLAG_TC3) == RESET);

  /****************************************************************************/
  /*                          Checking buffers                                */
  /****************************************************************************/

  /* Check if decrypted cypher text is equal to original plain text */
  cryptostatus = Buffercmp(PlainText, ComputedPlainText, PLAINTEXT_SIZE);

  if (cryptostatus == SUCCESS)
  {
    /* Turn on green led LD1 */
    STM_EVAL_LEDOn(LED1);
  }
  else
  {
    /* Turn on red led LD3 */
    STM_EVAL_LEDOn(LED3);
  }
  /* Infinite loop */
  while (1)
  {}
}