aes_callback.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief SAM Advanced Encryption Standard driver.
  6. *
  7. * This file defines a useful set of functions for the AES on SAM devices.
  8. *
  9. * Copyright (c) 2014-2015 Atmel Corporation. All rights reserved.
  10. *
  11. * \asf_license_start
  12. *
  13. * \page License
  14. *
  15. * Redistribution and use in source and binary forms, with or without
  16. * modification, are permitted provided that the following conditions are met:
  17. *
  18. * 1. Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. *
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. *
  25. * 3. The name of Atmel may not be used to endorse or promote products derived
  26. * from this software without specific prior written permission.
  27. *
  28. * 4. This software may only be redistributed and used in connection with an
  29. * Atmel microcontroller product.
  30. *
  31. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  32. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  33. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  34. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  35. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  36. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  37. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  38. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  40. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  41. * POSSIBILITY OF SUCH DAMAGE.
  42. *
  43. * \asf_license_stop
  44. *
  45. */
  46. /*
  47. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  48. */
  49. #ifndef AES_CALLBACK_H_INCLUDED
  50. #define AES_CALLBACK_H_INCLUDED
  51. #ifdef __cplusplus
  52. extern "C" {
  53. #endif
  54. #include <compiler.h>
  55. #include <system_interrupt.h>
  56. /**
  57. * \addtogroup asfdoc_sam0_drivers_aes_group
  58. *
  59. * @{
  60. */
  61. /** AES interrupt callback function type. */
  62. typedef void (*aes_callback_t)(void);
  63. /** AES callback type. */
  64. enum aes_callback_type {
  65. /** Encryption complete callback */
  66. AES_CALLBACK_ENCRYPTION_COMPLETE = 0,
  67. /** GF Multiplication Complete callback */
  68. AES_CALLBACK_GF_MULTI_COMPLETE = 1,
  69. };
  70. /** \name Callback Configuration and Initialization
  71. * @{
  72. */
  73. enum status_code aes_register_callback(
  74. const aes_callback_t callback,
  75. const enum aes_callback_type type);
  76. enum status_code aes_unregister_callback(
  77. const aes_callback_t callback,
  78. const enum aes_callback_type type);
  79. /** @} */
  80. /** \name Callback Enabling and Disabling
  81. * @{
  82. */
  83. /**
  84. * \brief Enable an AES callback.
  85. *
  86. * \param[in,out] module Pointer to the software instance struct
  87. * \param[in] type Callback source type
  88. *
  89. * \return Status of the callback enable operation.
  90. * \retval STATUS_OK The callback was enabled successfully
  91. * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied
  92. */
  93. static inline enum status_code aes_enable_callback(struct aes_module *const module,
  94. const enum aes_callback_type type)
  95. {
  96. system_interrupt_enable(SYSTEM_INTERRUPT_MODULE_AES);
  97. if (type == AES_CALLBACK_ENCRYPTION_COMPLETE){
  98. module->hw->INTENSET.reg = AES_INTENSET_ENCCMP;
  99. } else if (type == AES_CALLBACK_GF_MULTI_COMPLETE){
  100. module->hw->INTENSET.reg = AES_INTENSET_GFMCMP;
  101. } else {
  102. Assert(false);
  103. return STATUS_ERR_INVALID_ARG;
  104. }
  105. return STATUS_OK;
  106. }
  107. /**
  108. * \brief Disable an AES callback.
  109. *
  110. * \param[in,out] module Pointer to the software instance struct
  111. * \param[in] type Callback source type
  112. *
  113. * \return Status of the callback enable operation.
  114. * \retval STATUS_OK The callback was enabled successfully
  115. * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied
  116. */
  117. static inline enum status_code aes_disable_callback(struct aes_module *const module,
  118. const enum aes_callback_type type)
  119. {
  120. system_interrupt_disable(SYSTEM_INTERRUPT_MODULE_AES);
  121. if (type == AES_CALLBACK_ENCRYPTION_COMPLETE){
  122. module->hw->INTENCLR.reg = AES_INTENCLR_ENCCMP;
  123. } else if (type == AES_CALLBACK_GF_MULTI_COMPLETE){
  124. module->hw->INTENCLR.reg = AES_INTENCLR_GFMCMP;
  125. } else {
  126. Assert(false);
  127. return STATUS_ERR_INVALID_ARG;
  128. }
  129. return STATUS_OK;
  130. }
  131. /** @} */
  132. /** @} */
  133. #ifdef __cplusplus
  134. }
  135. #endif
  136. #endif /* AES_CALLBACK_H_INCLUDED */