XMEGA_AES_driver.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /* This file has been prepared for Doxygen automatic documentation generation.*/
  2. /*! \file *********************************************************************
  3. *
  4. * \brief XMEGA AES driver header file.
  5. *
  6. * This file contains the function prototypes and enumerator definitions
  7. * for various configuration parameters for the XMEGA AES driver.
  8. *
  9. * The driver is not intended for size and/or speed critical code, since
  10. * most functions are just a few lines of code, and the function call
  11. * overhead would decrease code performance. The driver is intended for
  12. * rapid prototyping and documentation purposes for getting started with
  13. * the XMEGA AES crypto instruction.
  14. *
  15. * For size and/or speed critical code, it is recommended to copy the
  16. * function contents directly into your application instead of making
  17. * a function call.
  18. *
  19. * \par Application note:
  20. * AVR1317 Using the XMEGA built in AES accelerator
  21. *
  22. * \par Documentation
  23. * For comprehensive code documentation, supported compilers, compiler
  24. * settings and supported devices see readme.html
  25. *
  26. * \author
  27. * Atmel Corporation: http://www.atmel.com \n
  28. * Support email: avr@atmel.com
  29. *
  30. * $Revision: 1569 $
  31. * $Date: 2008-04-22 13:03:43 +0200 (ti, 22 apr 2008) $ \n
  32. *
  33. * Copyright (c) 2008, Atmel Corporation All rights reserved.
  34. *
  35. * Redistribution and use in source and binary forms, with or without
  36. * modification, are permitted provided that the following conditions are met:
  37. *
  38. * 1. Redistributions of source code must retain the above copyright notice,
  39. * this list of conditions and the following disclaimer.
  40. *
  41. * 2. Redistributions in binary form must reproduce the above copyright notice,
  42. * this list of conditions and the following disclaimer in the documentation
  43. * and/or other materials provided with the distribution.
  44. *
  45. * 3. The name of ATMEL may not be used to endorse or promote products derived
  46. * from this software without specific prior written permission.
  47. *
  48. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  49. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  50. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND
  51. * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
  52. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  53. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  54. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  55. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  56. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  57. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  58. *****************************************************************************/
  59. #ifndef AES_DRIVER_H
  60. #define AES_DRIVER_H
  61. #include "avr_compiler.h"
  62. /* Length of one block. Always 128-bits (16 bytes). */
  63. #define AES_BLOCK_LENGTH 16
  64. /* \brief AES structure used by the AES interrupt driver.*/
  65. typedef struct AES_interrupt_driver
  66. {
  67. /*! \brief pointer to the AES input (plaintext or ciphertext)*/
  68. uint8_t * input_ptr;
  69. /*! \brief pointer to the key used by the AES*/
  70. uint8_t * key_ptr;
  71. /*! \brief pointer to the initialization vector needed in CBC*/
  72. uint8_t * init_ptr;
  73. /*! \brief pointer to the AES output (plaintext or ciphertext)*/
  74. uint8_t * output_ptr;
  75. /*! \brief variable that stores the number of blocks to encrypt/decrypt.*/
  76. uint8_t block_count;
  77. /*! \brief variable that stores the number of blocks left to encrypt/decrypt.*/
  78. uint8_t blocks_left;
  79. /*! \brief variable that tell if decryption or encryption shall be done*/
  80. bool decrypt;
  81. } AES_interrupt_driver_t;
  82. /* Definitions of macros */
  83. /*! \brief This macro enable AES module encryption mode. */
  84. #define AES_encryption_mode_set() ( AES.CTRL = AES.CTRL & (~AES_DECRYPT_bm) )
  85. /*! \brief This macro enable AES module decryption mode. */
  86. #define AES_decryption_mode_set() ( AES.CTRL |= AES_DECRYPT_bm )
  87. /*! \brief This macro enable the auto start feature in the AES module. */
  88. #define AES_auto_enable() ( AES.CTRL |= AES_AUTO_bm )
  89. /*! \brief This macro disable the auto start feature in the AES module. */
  90. #define AES_auto_disable() ( AES.CTRL = AES.CTRL & (~AES_AUTO_bm) )
  91. /*! \brief This macro enable the xor feature in the AES module. */
  92. #define AES_xor_enable() ( AES.CTRL |= AES_XOR_bm )
  93. /*! \brief This macro disable the xor feature in the AES module. */
  94. #define AES_xor_disable() ( AES.CTRL = AES.CTRL & (~AES_XOR_bm) )
  95. /*! \brief This macro resets all registers in AES module. */
  96. #define AES_software_reset() ( AES.CTRL = AES_RESET_bm )
  97. /*! \brief This macro starts a decryption/encryption. */
  98. #define AES_start() ( AES.CTRL |= AES_START_bm )
  99. /*! \brief This macro checks if AES state ready interrupt flag is set.
  100. *
  101. * \retval true if State Ready interrupt flag is set.
  102. * \retval false if State Ready interrupt flag is not set.
  103. */
  104. #define AES_state_ready_flag_check() ((AES.STATUS & AES_SRIF_bm) != 0)
  105. /*! \brief This macro checks if the error flag is set.
  106. *
  107. * \retval true if AES Error flag is set.
  108. * \retval false if AES Error flag not set.
  109. */
  110. #define AES_error_flag_check() ((AES.STATUS & AES_ERROR_bm) != 0)
  111. /* Prototyping of Interrupt driver functions */
  112. void AES_interrupt_driver_init(AES_interrupt_driver_t * interrupt_driver,
  113. uint8_t * input_ptr, uint8_t * output_ptr,
  114. uint8_t * AES_key, uint8_t * AES_init,
  115. uint8_t block_count, bool decrypt);
  116. bool AES_interrupt_driver_start(AES_interrupt_driver_t * interrupt_driver,
  117. AES_INTLVL_t int_lvl);
  118. void AES_interrupt_handler(AES_interrupt_driver_t * interrupt_driver);
  119. bool AES_interrupt_driver_finished(AES_interrupt_driver_t * interrupt_driver);
  120. void AES_interruptlevel_set(AES_INTLVL_t int_lvl);
  121. /* Prototyping of Polled driver functions */
  122. bool AES_encrypt(uint8_t * plaintext, uint8_t * ciphertext, uint8_t * key);
  123. bool AES_decrypt(uint8_t * ciphertext, uint8_t * plaintext, uint8_t * key);
  124. bool AES_encrypt_backtoback(uint8_t * plaintext, uint8_t * ciphertext);
  125. bool AES_decrypt_backtoback(uint8_t * ciphertext, uint8_t * plaintext);
  126. bool AES_lastsubkey_generate(uint8_t * key, uint8_t * decrypt_key);
  127. bool AES_CBC_encrypt(uint8_t * plaintext, uint8_t * ciphertext, uint8_t * keys,
  128. uint8_t * init, uint16_t block_count);
  129. bool AES_CBC_decrypt(uint8_t * ciphertext, uint8_t * plaintext, uint8_t * keys,
  130. uint8_t * init, uint16_t block_count);
  131. #endif