main.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. ChibiOS - Copyright (C) 2016..2018 Theodore Ateba
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include "ch.h"
  14. #include "hal.h"
  15. #include "chprintf.h"
  16. BaseSequentialStream *chp = (BaseSequentialStream *) &SD1;
  17. // Key to use during the Encryption.
  18. uint8_t key[AES_BLOCK_SIZE] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
  19. 0x08, 0x09, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15};
  20. // Data to encrypt.
  21. uint8_t data[AES_BLOCK_SIZE] = { 0x08, 0x04, 0x02, 0x01, 0x08, 0x04, 0x02, 0x01,
  22. 0x08, 0x04, 0x12, 0x11, 0x18, 0x14, 0x12, 0x11};
  23. uint8_t data_encrypted[AES_BLOCK_SIZE] = {0};
  24. uint8_t data_decrypted[AES_BLOCK_SIZE] = {0};
  25. cryerror_t res;
  26. uint8_t tkey = 0; // Transient key
  27. static const CRYConfig cryConfig = {
  28. FALSE, /* No Auto start feature. */
  29. FALSE /* No XOR feature. */
  30. };
  31. /**
  32. * @brief Blink thread.
  33. */
  34. static THD_WORKING_AREA(waThread1, 32);
  35. static THD_FUNCTION(Thread1, arg) {
  36. (void)arg;
  37. chRegSetThreadName("Blinker");
  38. while (true) {
  39. palClearPad(IOPORT5, PORTE_LED);
  40. chThdSleepMilliseconds(1000);
  41. palSetPad(IOPORT5, PORTE_LED);
  42. chThdSleepMilliseconds(100);
  43. }
  44. }
  45. /*
  46. * Application entry point.
  47. */
  48. int main(void) {
  49. uint8_t i;
  50. /*
  51. * System initializations.
  52. * - HAL initialization, this also initializes the configured device drivers
  53. * and performs the board-specific initializations.
  54. * - Kernel initialization, the main() function becomes a thread and the
  55. * RTOS is active.
  56. */
  57. halInit();
  58. chSysInit();
  59. palClearPad(IOPORT5, PORTE_LED);
  60. /*
  61. * Configure TX (PINC3) and RX (PIN2) for the USART1.
  62. */
  63. palSetPadMode(IOPORT3, PIN3, PAL_MODE_OUTPUT_PUSHPULL);
  64. palSetPadMode(IOPORT3, PIN2, PAL_MODE_INPUT_PULLUP);
  65. /*
  66. * Start the Serial driver 1 by using the default configuration.
  67. */
  68. sdStart(&SD1, NULL);
  69. chprintf(chp, "\r\nChibiOS on XMEGA 128U4A.\r\n");
  70. chprintf(chp, "This program is using trunk branch of the svn repo.\r\n");
  71. chprintf(chp, "AES Encryption/Decryption testhal demo.\r\n");
  72. chprintf(chp, "Example started...\r\n");
  73. cryStart(&CRYD1, &cryConfig);
  74. res = cryLoadTransientKey(&CRYD1, (cryalgorithm_t) cry_algo_aes,16, key);
  75. res = cryEncryptAES(&CRYD1, tkey, data, data_encrypted);
  76. if (res != CRY_NOERROR) {
  77. chprintf(chp, "Encryption of data failed.\r\n");
  78. while (true) {
  79. chThdSleepMilliseconds(100);
  80. }
  81. }
  82. else {
  83. chprintf(chp, "Encryption done.\r\n");
  84. }
  85. res = cryDecryptAES(&CRYD1, tkey, data_encrypted, data_decrypted);
  86. if (res != CRY_NOERROR) {
  87. chprintf(chp, "Decryption of data failed.\r\n");
  88. while (true) {
  89. chThdSleepMilliseconds(100);
  90. }
  91. }
  92. else {
  93. chprintf(chp, "Decryption done.\r\n");
  94. }
  95. // Check if decrypted answer is equal to plaintext.
  96. for (i = 0; i < AES_BLOCK_SIZE ; i++ ) {
  97. if (data[i] != data_decrypted[i]) {
  98. chprintf(chp, "Decrypted data is not equal to data.\r\n");
  99. while (true) {
  100. chThdSleepMilliseconds(100);
  101. }
  102. }
  103. }
  104. chprintf(chp, "Example ends up every thing is ok.\r\n");
  105. /*
  106. * Starts the LED blinker thread.
  107. */
  108. chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);
  109. while (true) {
  110. chThdSleepMilliseconds(100);
  111. }
  112. return 0;
  113. }