text: /*
   * Copyright (c) 2015-2021 University of Antwerp, Aloxy NV.
   *
   * This file is part of Sub-IoT.
   * See https://github.com/Sub-IoT/Sub-IoT-Stack for further info.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   *
   *  \author philippe.nunes@cortus.com
   */
  
  #include <stdio.h>
  #include <string.h>
  #include "aes.h"
  
  /*
   * This unit-test application is used to confirm that our implementation has
   * correctly implemented AES-ECB, AES-CBC, AES-CTR and AES-CCM mode. All the
   * test vectors use AES with a 128 bit key;
   *
   * The AES-ECB, AES-CBC, AES-CBC-MAC modes are implicitly tested through the
   * testing of the AES-CTR and AES-CCM mode
   */
  
  #define DPRINT(...) printf(__VA_ARGS__)
  #define DPRINT_DATA(...) print_array(__VA_ARGS__)
  
  void print_array(uint8_t* buffer, uint8_t length)
  {
      int i;
  
      for (i = 0; i < length; i++)
      {
          printf("%02X ", buffer[i]);
      }
      printf("\n");
  }
  
  /*
   * AES-CCM test vectors from:
   *
   * http://www.faqs.org/rfcs/rfc3610.html
   */
  
  #define CCM_TEST_VECTORS_NB 6
  
  /* The authentication len is set to 8 bytes for all the CCM test vectors */
  #define CCM_AUTH_LEN 8
  
  /*
   * The plain text is the same for all the ccm test vectors, only the used length
   * changes
   */
  static const unsigned char ccm_pt[] =
  {
      0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
      0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
      0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F,
      0x20
  };
  
  /* The key is the same for all the ccm test vectors */
  static const uint8_t ccm_key[] = {
      0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7,
  	0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF
  };
  
  static const uint8_t ccm_iv[CCM_TEST_VECTORS_NB][AES_BLOCK_SIZE] = {
      { 0x59, 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00,
        0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x00, 0x17 },
      { 0x59, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01,
        0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x00, 0x18 },
      { 0x59, 0x00, 0x00, 0x00, 0x05, 0x04, 0x03, 0x02,
        0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x00, 0x19 },
      { 0x59, 0x00, 0x00, 0x00, 0x06, 0x05, 0x04, 0x03,
        0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x00, 0x13 },
      { 0x59, 0x00, 0x00, 0x00, 0x07, 0x06, 0x05, 0x04,
        0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x00, 0x14 },
      { 0x59, 0x00, 0x00, 0x00, 0x08, 0x07, 0x06, 0x05,
        0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x00, 0x15 },
  };
  
  static const uint8_t ccm_ctr[CCM_TEST_VECTORS_NB][AES_BLOCK_SIZE] = {
      { 0x01, 0x00, 0x00, 0x00, 0x03, 0x02, 0x01, 0x00,
        0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x00, 0x01 },
      { 0x01, 0x00, 0x00, 0x00, 0x04, 0x03, 0x02, 0x01,
        0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x00, 0x01 },
      { 0x01, 0x00, 0x00, 0x00, 0x05, 0x04, 0x03, 0x02,
        0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x00, 0x01 },
      { 0x01, 0x00, 0x00, 0x00, 0x06, 0x05, 0x04, 0x03,
        0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x00, 0x01 },
      { 0x01, 0x00, 0x00, 0x00, 0x07, 0x06, 0x05, 0x04,
        0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x00, 0x01 },
      { 0x01, 0x00, 0x00, 0x00, 0x08, 0x07, 0x06, 0x05,
        0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0x00, 0x01 },
  };
  
  
  /*
   * The cleartext header length is set to 8 octets for the 3 first vectors and
   * to 12 octets for the next ones.
   */
  static const uint8_t ad[] = {
      0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
      0x08, 0x09, 0x0A, 0x0B
  };
  
  static const uint8_t ccm_ct[CCM_TEST_VECTORS_NB][33] = {
      { 0x58, 0x8C, 0x97, 0x9A, 0x61, 0xC6, 0x63, 0xD2,
        0xF0, 0x66, 0xD0, 0xC2, 0xC0, 0xF9, 0x89, 0x80,
        0x53, 0x6C, 0xDA, 0x1B, 0x83, 0x83, 0xA4, 0x58,
        0xF9, 0x29, 0x4E, 0xE1, 0x43, 0x01, 0x5E },
      { 0x72, 0xC9, 0x1A, 0x36, 0xE1, 0x35, 0xF8, 0xCF,
        0x29, 0x1C, 0xA8, 0x94, 0x08, 0x5C, 0x87, 0xE3,
        0xCE, 0x48, 0x55, 0x16, 0x6D, 0xE3, 0x54, 0xA2,
        0x9E, 0x63, 0x7F, 0x49, 0x18, 0x93, 0x13, 0x22 },
     {  0x51, 0xB1, 0xE5, 0xF4, 0x4A, 0x19, 0x7D, 0x1D,
        0xA4, 0x6B, 0x0F, 0x8E, 0x2D, 0x28, 0x2A, 0xE8,
        0xCA, 0x30, 0xC0, 0xC2, 0x1A, 0x92, 0x0F, 0x13,
        0xCF, 0xD7, 0x90, 0xE3, 0x6C, 0xF9, 0xBF, 0x91,
        0x6A },
      { 0xA2, 0x8C, 0x68, 0x65, 0x93, 0x9A, 0x9A, 0x79,
        0xFA, 0xAA, 0x5C, 0x4C, 0x2A, 0x9D, 0x4A, 0x91,
        0xD3, 0xF9, 0x51, 0x1D, 0xF0, 0xAE, 0x8F, 0xFF,
        0xFC, 0xCC, 0x1B },
      { 0xDC, 0xF1, 0xFB, 0x7B, 0x5D, 0x9E, 0x23, 0xFB,
        0x9D, 0x4E, 0x13, 0x12, 0x53, 0x65, 0x8A, 0xD8,
        0x2C, 0x48, 0x08, 0x48, 0xFB, 0xB6, 0x29, 0x62,
        0xEC, 0x32, 0x46, 0x27 },
      { 0x6F, 0xC1, 0xB0, 0x11, 0xF0, 0x06, 0x56, 0x8B,
        0x51, 0x71, 0xA4, 0x2D, 0x95, 0x3D, 0x46, 0x9B,
        0x48, 0x65, 0xDD, 0x0E, 0x51, 0x00, 0xD2, 0xD4,
        0x2A, 0x5C, 0xC0, 0x9A, 0xC3 }
  };
  
  static const size_t add_len[CCM_TEST_VECTORS_NB] = { 8, 8, 8, 12, 12, 12 };
  static const size_t ccm_len[CCM_TEST_VECTORS_NB] = { 23, 24, 25, 19, 20, 21 };
  static const size_t ccm_offset[CCM_TEST_VECTORS_NB] = { 0, 0, 0, 4, 4, 4 };
  
  /*
   * AES-CTR test vectors from:
   *
   * http://www.faqs.org/rfcs/rfc3686.html
   */
  
  #define CTR_TEST_VECTORS_NB 3
  
  static const unsigned char ctr_key[CTR_TEST_VECTORS_NB][AES_BLOCK_SIZE] =
  {
      { 0xAE, 0x68, 0x52, 0xF8, 0x12, 0x10, 0x67, 0xCC,
        0x4B, 0xF7, 0xA5, 0x76, 0x55, 0x77, 0xF3, 0x9E },
      { 0x7E, 0x24, 0x06, 0x78, 0x17, 0xFA, 0xE0, 0xD7,
        0x43, 0xD6, 0xCE, 0x1F, 0x32, 0x53, 0x91, 0x63 },
      { 0x76, 0x91, 0xBE, 0x03, 0x5E, 0x50, 0x20, 0xA8,
        0xAC, 0x6E, 0x61, 0x85, 0x29, 0xF9, 0xA0, 0xDC }
  };
  
  static const unsigned char ctr_blk[CTR_TEST_VECTORS_NB][AES_BLOCK_SIZE] =
  {
      { 0x01, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00, 0x00,
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 },
      { 0x01, 0x00, 0x6C, 0xB6, 0xDB, 0xC0, 0x54, 0x3B,
        0x59, 0xDA, 0x48, 0xD9, 0x0B, 0x00, 0x00, 0x00 },
      { 0x01, 0x00, 0xE0, 0x01, 0x7B, 0x27, 0x77, 0x7F,
        0x3F, 0x4A, 0x17, 0x86, 0xF0, 0x00, 0x00, 0x00 }
  };
  
  static const unsigned char ctr_pt[CTR_TEST_VECTORS_NB][36] =
  {
      { 0x53, 0x69, 0x6E, 0x67, 0x6C, 0x65, 0x20, 0x62,
        0x6C, 0x6F, 0x63, 0x6B, 0x20, 0x6D, 0x73, 0x67 },
  
      { 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 },
  
      { 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,
        0x20, 0x21, 0x22, 0x23 }
  };
  
  static const unsigned char ctr_ct[CTR_TEST_VECTORS_NB][36] =
  {
      { 0x86, 0xBA, 0x59, 0xEC, 0x02, 0xBC, 0xF6, 0xC6,
        0x0A, 0x05, 0xDE, 0x99, 0x5D, 0xFD, 0x8C, 0x6B },
      { 0x50, 0xD1, 0x8C, 0xE6, 0x3D, 0x90, 0x2C, 0x6D,
        0x95, 0x3D, 0x44, 0x91, 0x29, 0x0C, 0xB9, 0x31,
        0xE3, 0x3B, 0x14, 0xEC, 0x7E, 0x47, 0xC3, 0x6B,
        0xC3, 0x4A, 0x30, 0x89, 0x99, 0x81, 0xA8, 0xBA },
      { 0xCF, 0x6D, 0xDD, 0x50, 0x96, 0xCE, 0x18, 0xA6,
        0x9D, 0xBF, 0x34, 0xF6, 0x1C, 0x09, 0x3C, 0x8F,
        0x7D, 0x39, 0x28, 0x42, 0xE7, 0x9C, 0x4F, 0x40,
        0x43, 0x87, 0x55, 0x73, 0x0C, 0x68, 0x55, 0xAB,
        0x2A, 0x8E, 0x22, 0x4F }
  };
  
  static const int ctr_len[CTR_TEST_VECTORS_NB] = { 16, 32, 36 };
  
  int main(int argc, char *argv[])
  {
      int i;
      error_t ret;
      uint8_t ctr[AES_BLOCK_SIZE];
      uint8_t payload[AES_BLOCK_SIZE * 3];
  
      DPRINT("Unit-tests for AES-CTR / AES-CCM mode \n");
  
  #if (defined PLATFORM_EFM32GG_STK3700 || defined PLATFORM_EFM32HG_STK3400 || defined PLATFORM_EZR32LG_WSTK6200A)
      // TODO set a minimal platform configuration to enable the AES hardware module
  #endif
  
      /* test AES-CTR mode*/
      for (i = 0; i < CTR_TEST_VECTORS_NB; i++)
      {
          AES128_init(ctr_key[i]);
  
          memcpy(payload, ctr_pt[i], ctr_len[i]);
          memcpy(ctr, ctr_blk[i], AES_BLOCK_SIZE);
  
          AES128_CTR_encrypt(payload, payload, ctr_len[i], ctr);
          if (memcmp(payload, ctr_ct[i], ctr_len[i] ) != 0)
          {
              DPRINT("AES-CTR encryption output \n");
              DPRINT_DATA(payload, ctr_len[i]);
              DPRINT("AES-CTR encryption #%d failed", i + 1 );
              return -1;
          }
  
          /* ctr has been incremented by the previous encryption, recover the original ctr block */
          memcpy(ctr, ctr_blk[i], AES_BLOCK_SIZE);
  
          AES128_CTR_encrypt(payload, payload, ctr_len[i], ctr);
          if (memcmp(payload, ctr_pt[i], ctr_len[i]) != 0)
          {
              DPRINT("AES-CTR encryption output \n");
              DPRINT_DATA(payload, ctr_len[i]);
              DPRINT("AES-CTR decryption #%d failed", i + 1);
              return -1;
          }
  
          DPRINT("AES-CTR test vector #%d passed\n", i + 1);
      }
  
      // The key is the same for all the ccm test vectors */
      AES128_init(ccm_key);
  
      /* test AES-CCM mode*/
      for (i = 0; i < CCM_TEST_VECTORS_NB; i++)
      {
          memcpy(payload, ccm_pt + ccm_offset[i], ccm_len[i]);
          memcpy(ctr, ccm_ctr[i], AES_BLOCK_SIZE);
  
          // the auth_len is always set to 32 bits
          ret = AES128_CCM_encrypt(payload, ccm_len[i], ccm_iv[i], ad, add_len[i],
                                   ctr, CCM_AUTH_LEN);
          if (ret != 0 || memcmp(payload, ccm_ct[i], ccm_len[i] + CCM_AUTH_LEN) != 0)
          {
              DPRINT("AES-CCM encryption output \n");
              DPRINT_DATA(payload, ccm_len[i] + CCM_AUTH_LEN);
              DPRINT("AES-CCM encryption #%d failed\n", i + 1);
              return -1;
          }
  
          /* ctr has been incremented by the previous encryption, recover the original ctr block */
          memcpy(ctr, ccm_ctr[i], AES_BLOCK_SIZE);
  
          ret = AES128_CCM_decrypt(payload, ccm_len[i], ccm_iv[i], ad, add_len[i],
                                   ctr, payload + ccm_len[i], CCM_AUTH_LEN);
          if (ret != 0 || memcmp(payload, ccm_pt + ccm_offset[i], ccm_len[i]) != 0)
          {
              DPRINT("AES-CCM encryption output \n");
              DPRINT_DATA(payload, ccm_len[i]);
              DPRINT("AES-CCM decryption #%d failed\n", i + 1);
              return -1;
          }
  
          DPRINT("AES-CCM test vector #%d passed\n", i + 1);
      }
  
      DPRINT("AES all unit tests OK !\n");
      return 0;
  }
  