/**
  * @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;
}
示例#2
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)
  {}
}