// /*
//  *  Copyright 2020-2023 Yuntu Microelectronics co.,ltd
//  *  All rights reserved.
//  *
//  *  YUNTU Confidential. This software is owned or controlled by YUNTU and may only be
//  *  used strictly in accordance with the applicable license terms. By expressly
//  *  accepting such terms or by downloading, installing, activating and/or otherwise
//  *  using the software, you are agreeing that you have read, and that you agree to
//  *  comply with and are bound by, such license terms. If you do not agree to be
//  *  bound by the applicable license terms, then you may not retain, install,
//  *  activate or otherwise use the software. The production use license in
//  *  Section 2.3 is expressly granted for this software.
//  *
//  * @file AES_example.c
//  * @brief This is an example of how to use the AES encryption and decryption functions with the PSA layer.
//  *
//  */

// #ifdef __cplusplus
// extern "C" {
// #endif

// /*==================================================================================================
//                                               INCLUDE FILES
// ==================================================================================================*/
// #include "AES_example.h"
// #include "psa/crypto.h"
// #include "common.h"
// #include "printf.h"

// /*==================================================================================================
//                                                 MACROS
// ==================================================================================================*/
// /**
//  * @brief Data length, using for AES encryption and decryption.
//  * @note The data length must be a multiple of 16 bytes.
//  */
// # define DATA_LENGTH    32

// /*==================================================================================================
//                                       LOCAL VARIABLES
// ==================================================================================================*/

// /* AES-128 key*/
// static const uint8_t aes_key[16] =
// {
//     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
//     0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
// };

// /**
//  * @brief Plaintext data, using for AES encryption and decryption.
//  */
// static const uint8_t plaintext[DATA_LENGTH] =
// {
//     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
//     0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
//     0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
//     0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
// };

// /**
//  * @brief Ciphertext data, using for AES encryption and decryption.
//  */
// static uint8_t ciphertext[DATA_LENGTH] = {0};

// /*==================================================================================================
//                                      LOCAL FUNCTION
// ==================================================================================================*/

// /*==================================================================================================
//                                          GLOBAL FUNCTIONS
// ==================================================================================================*/
// /**
//  * @brief AES ECB encryption and decryption test.
//  *
//  * @return  int : The result of the test.
//  * @retval  0: The test succeeded.
//  * @retval  other status: The test failed decode.
//  */
// int aes_ECB_test(void)
// {
//     psa_status_t status;
//     psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
//     psa_key_id_t key_id = 0;
//     size_t output_length;
//     uint8_t decrypted[DATA_LENGTH];

//     /* Set key attributes for AES-ECB*/
//     psa_set_key_usage_flags(&attributes, (PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT));
//     psa_set_key_algorithm(&attributes, PSA_ALG_ECB_NO_PADDING);
//     psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
//     psa_set_key_bits(&attributes, 128);

//     /* Import the key*/
//     status = psa_import_key(&attributes,
//                             aes_key,
//                             sizeof(aes_key),
//                             &key_id);
//     if (status != PSA_SUCCESS)
//     {
//         PRINTF("Key import failed! (status = %d)\n\r", status);
//         return status;
//     }

//     /* Performing encryption*/
//     status = psa_cipher_encrypt(key_id,
//                                 PSA_ALG_ECB_NO_PADDING,
//                                 plaintext,
//                                 sizeof(plaintext),
//                                 ciphertext,
//                                 sizeof(ciphertext),
//                                 &output_length);

//     if (status != PSA_SUCCESS)
//     {
//         PRINTF("Encryption failed! (status = %d)\n\r", status);
//         psa_destroy_key(key_id);
//         return status;
//     }

//     print_buf("Key", aes_key, DATA_LENGTH);
//     print_buf("Plaintext", plaintext, DATA_LENGTH);
//     print_buf("Ciphertext", ciphertext, DATA_LENGTH);

//     /*Performing decryption*/
//     status = psa_cipher_decrypt(key_id,
//                                 PSA_ALG_ECB_NO_PADDING,
//                                 ciphertext,
//                                 sizeof(ciphertext),
//                                 decrypted,
//                                 sizeof(decrypted),
//                                 &output_length);
//     if (status != PSA_SUCCESS)
//     {
//         PRINTF("Decryption failed! (status = %d)\n\r", status);
//         psa_destroy_key(key_id);
//         return status;
//     }

//     print_buf("Decrypted", decrypted, DATA_LENGTH);

//     if (buf_cmp(plaintext, decrypted, DATA_LENGTH) != 0)
//     {
//         PRINTF("Decryption failed! (status = %d)\n\r", status);
//         psa_destroy_key(key_id);
//         return -1;
//     }

//     /* Destroy the key*/
//     psa_destroy_key(key_id);
//     return 0;
// }

// /**
//  * @brief AES CBC encryption and decryption test.
//  *
//  * @return  int : The result of the test.
//  * @retval  0: The test succeeded.
//  * @retval  other status: The test failed decode.
//  */
// int aes_CBC_test(void)
// {
//     psa_status_t status;
//     psa_key_attributes_t attributes = PSA_KEY_ATTRIBUTES_INIT;
//     psa_key_id_t key_id = 0;
//     size_t output_length;
//     psa_cipher_operation_t operation = PSA_CIPHER_OPERATION_INIT;
//     uint8_t iv[16] =
//     {
//         0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
//         0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f
//     }; // Initialization vector for CBC mode

//     /* Set key attributes for AES-CBC*/
//     psa_set_key_usage_flags(&attributes, (PSA_KEY_USAGE_ENCRYPT | PSA_KEY_USAGE_DECRYPT));
//     psa_set_key_algorithm(&attributes, PSA_ALG_CBC_NO_PADDING);
//     psa_set_key_type(&attributes, PSA_KEY_TYPE_AES);
//     psa_set_key_bits(&attributes, 128);

//     /* Import the key*/
//     status = psa_import_key(&attributes,
//                             aes_key,
//                             sizeof(aes_key),
//                             &key_id);
//     if (status != PSA_SUCCESS)
//     {
//         PRINTF("Key import failed! (status = %d)\n\r", status);
//         return status;
//     }

//     /* Performing encryption*/
//     status = psa_cipher_encrypt_setup(&operation, key_id, PSA_ALG_CBC_NO_PADDING);
//     if (status != PSA_SUCCESS)
//     {
//         PRINTF("Encryption setup failed! (status = %d)\n\r", status);
//         psa_destroy_key(key_id);
//         return status;
//     }

//     status = psa_cipher_set_iv(&operation, iv, sizeof(iv));
//     if (status != PSA_SUCCESS)
//     {
//         PRINTF("Setting IV failed! (status = %d)\n\r", status);
//         psa_cipher_abort(&operation);
//         psa_destroy_key(key_id);
//         return status;
//     }

//     status = psa_cipher_update(&operation, plaintext, sizeof(plaintext),
//                                ciphertext, sizeof(ciphertext), &output_length);
//     if (status != PSA_SUCCESS)
//     {
//         PRINTF("Encryption update failed! (status = %d)\n\r", status);
//         psa_cipher_abort(&operation);
//         psa_destroy_key(key_id);
//         return status;
//     }

//     status = psa_cipher_finish(&operation, ciphertext + output_length,
//                                sizeof(ciphertext) - output_length, &output_length);
//     if (status != PSA_SUCCESS)
//     {
//         PRINTF("Encryption finish failed! (status = %d)\n\r", status);
//         psa_cipher_abort(&operation);
//         psa_destroy_key(key_id);
//         return status;
//     }

//     print_buf("Key", aes_key, DATA_LENGTH);
//     print_buf("IV", iv, DATA_LENGTH);
//     print_buf("Plaintext", plaintext, DATA_LENGTH);
//     print_buf("Ciphertext", ciphertext, DATA_LENGTH);

//     /*Performing decryption*/
//     uint8_t decrypted[DATA_LENGTH];
//     status = psa_cipher_decrypt_setup(&operation, key_id, PSA_ALG_CBC_NO_PADDING);
//     if (status != PSA_SUCCESS)
//     {
//         PRINTF("Decryption setup failed! (status = %d)\n\r", status);
//         psa_destroy_key(key_id);
//         return status;
//     }

//     status = psa_cipher_set_iv(&operation, iv, sizeof(iv));
//     if (status != PSA_SUCCESS)
//     {
//         PRINTF("Setting IV failed! (status = %d)\n\r", status);
//         psa_cipher_abort(&operation);
//         psa_destroy_key(key_id);
//         return status;
//     }

//     status = psa_cipher_update(&operation, ciphertext, sizeof(ciphertext),
//                                decrypted, sizeof(decrypted), &output_length);
//     if (status != PSA_SUCCESS)
//     {
//         PRINTF("Decryption update failed! (status = %d)\n\r", status);
//         psa_cipher_abort(&operation);
//         psa_destroy_key(key_id);
//         return status;
//     }

//     status = psa_cipher_finish(&operation, decrypted + output_length,
//                                sizeof(decrypted) - output_length, &output_length);
//     if (status != PSA_SUCCESS)
//     {
//         PRINTF("Decryption finish failed! (status = %d)\n\r", status);
//         psa_cipher_abort(&operation);
//         psa_destroy_key(key_id);
//         return status;
//     }

//     print_buf("Decrypted", decrypted, DATA_LENGTH);

//     if (buf_cmp(plaintext, decrypted, DATA_LENGTH) != 0)
//     {
//         PRINTF("Decryption failed! (status = %d)\n\r", status);
//         psa_destroy_key(key_id);
//         return -1;
//     }

//     /* Destroy the key*/
//     psa_destroy_key(key_id);
//     return 0;
// }

// #ifdef __cplusplus
// }
// #endif

