fsl_bee.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * Copyright 2017 NXP
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef _FSL_BEE_H_
  31. #define _FSL_BEE_H_
  32. #include "fsl_common.h"
  33. /*******************************************************************************
  34. * Definitions
  35. *******************************************************************************/
  36. /*! @name Driver version */
  37. /*@{*/
  38. /*! @brief BEE driver version. Version 2.0.0.
  39. *
  40. * Current version: 2.0.0
  41. *
  42. * Change log:
  43. * - Version 2.0.0
  44. * - Initial version
  45. */
  46. #define FSL_BEE_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
  47. /*@}*/
  48. typedef enum _bee_aes_mode
  49. {
  50. kBEE_AesEcbMode = 0U, /*!< AES ECB Mode */
  51. kBEE_AesCtrMode = 1U /*!< AES CTR Mode */
  52. } bee_aes_mode_t;
  53. typedef enum _bee_region
  54. {
  55. kBEE_Region0 = 0U, /*!< BEE region 0 */
  56. kBEE_Region1 = 1U /*!< BEE region 1 */
  57. } bee_region_t;
  58. typedef enum _bee_region_enable
  59. {
  60. kBEE_RegionDisabled = 0U, /*!< BEE region disabled */
  61. kBEE_RegionEnabled = 1U /*!< BEE region enabled */
  62. } bee_region_enable_t;
  63. typedef enum _bee_status_flags
  64. {
  65. kBEE_DisableAbortFlag = 1U, /*!< Disable abort flag. */
  66. kBEE_Reg0ReadSecViolation = 2U, /*!< Region-0 read channel security violation */
  67. kBEE_ReadIllegalAccess = 4U, /*!< Read channel illegal access detected */
  68. kBEE_Reg1ReadSecViolation = 8U, /*!< Region-1 read channel security violation */
  69. kBEE_Reg0AccessViolation = 16U, /*!< Protected region-0 access violation */
  70. kBEE_Reg1AccessViolation = 32U, /*!< Protected region-1 access violation */
  71. kBEE_IdleFlag = BEE_STATUS_BEE_IDLE_MASK /*!< Idle flag */
  72. } bee_status_flags_t;
  73. /*! @brief BEE region configuration structure. */
  74. typedef struct _bee_region_config
  75. {
  76. bee_aes_mode_t mode; /*!< AES mode used for encryption/decryption */
  77. uint32_t regionBot; /*!< Region bottom address */
  78. uint32_t regionTop; /*!< Region top address */
  79. uint32_t addrOffset; /*!< Region address offset */
  80. bee_region_enable_t regionEn; /*!< Region enable/disable */
  81. } bee_region_config_t;
  82. /*******************************************************************************
  83. * API
  84. ******************************************************************************/
  85. #if defined(__cplusplus)
  86. extern "C" {
  87. #endif
  88. /*!
  89. * @brief Resets BEE module to factory default values.
  90. *
  91. * This function performs hardware reset of BEE module. Attributes and keys from software for both regions are cleared.
  92. *
  93. * @param base BEE peripheral address.
  94. */
  95. void BEE_Init(BEE_Type *base);
  96. /*!
  97. * @brief Resets BEE module, clears keys for both regions and disables clock to the BEE.
  98. *
  99. * This function performs hardware reset of BEE module and disables clocks. Attributes and keys from software for both
  100. * regions are cleared.
  101. *
  102. * @param base BEE peripheral address.
  103. */
  104. void BEE_Deinit(BEE_Type *base);
  105. /*!
  106. * @brief Enables BEE decryption.
  107. *
  108. * This function enables decryption using BEE.
  109. *
  110. * @param base BEE peripheral address.
  111. */
  112. static inline void BEE_Enable(BEE_Type *base)
  113. {
  114. base->CTRL |= BEE_CTRL_BEE_ENABLE_MASK | BEE_CTRL_KEY_VALID_MASK;
  115. }
  116. /*!
  117. * @brief Disables BEE decryption.
  118. *
  119. * This function disables decryption using BEE.
  120. *
  121. * @param base BEE peripheral address.
  122. */
  123. static inline void BEE_Disable(BEE_Type *base)
  124. {
  125. base->CTRL &= ~BEE_CTRL_BEE_ENABLE_MASK | BEE_CTRL_KEY_VALID_MASK;
  126. }
  127. /*!
  128. * @brief Loads default values to the BEE region configuration structure.
  129. *
  130. * Loads default values to the BEE region configuration structure. The default values are as follows:
  131. * @code
  132. * config->mode = kBEE_AesCbcMode;
  133. * config->regionBot = 0U;
  134. * config->regionTop = 0U;
  135. * config->addrOffset = 0xF0000000U;
  136. * config->regionEn = kBEE_RegionDisabled;
  137. * @endcode
  138. *
  139. * @param config Configuration structure for BEE region.
  140. */
  141. void BEE_GetDefaultConfig(bee_region_config_t *config);
  142. /*!
  143. * @brief Sets BEE region configuration.
  144. *
  145. * This function sets BEE region settings accorging to given configuration structure.
  146. *
  147. * @param base BEE peripheral address.
  148. * @param region Selection of the BEE region to be configured.
  149. * @param config Configuration structure for BEE region.
  150. */
  151. status_t BEE_SetRegionConfig(BEE_Type *base, bee_region_t region, const bee_region_config_t *config);
  152. /*!
  153. * @brief Loads the AES key and nonce for selected region into BEE key registers.
  154. *
  155. * This function loads given AES key and nonce(only AES CTR mode) to BEE register for the given region.
  156. *
  157. * Please note, that eFuse BEE_KEYx_SEL must be set accordingly to be able to load and use key loaded in BEE registers.
  158. * Otherwise, key cannot loaded and BEE will use key from OTPMK or SW_GP2.
  159. *
  160. * @param base BEE peripheral address.
  161. * @param region Selection of the BEE region to be configured.
  162. * @param key AES key.
  163. * @param keySize Size of AES key.
  164. * @param nonce AES nonce.
  165. * @param nonceSize Size of AES nonce.
  166. */
  167. status_t BEE_SetRegionKey(
  168. BEE_Type *base, bee_region_t region, const uint8_t *key, size_t keySize, const uint8_t *nonce, size_t nonceSize);
  169. /*!
  170. * @brief Gets the BEE status flags.
  171. *
  172. * This function returns status of BEE peripheral.
  173. *
  174. * @param base BEE peripheral address.
  175. *
  176. * @return The status flags. This is the logical OR of members of the
  177. * enumeration ::bee_status_flags_t
  178. */
  179. uint32_t BEE_GetStatusFlags(BEE_Type *base);
  180. /*!
  181. * @brief Clears the BEE status flags.
  182. *
  183. * @param base BEE peripheral base address.
  184. * @param mask The status flags to clear. This is a logical OR of members of the
  185. * enumeration ::bee_status_flags_t
  186. */
  187. void BEE_ClearStatusFlags(BEE_Type *base, uint32_t mask);
  188. /*!
  189. * @brief Computes offset to be set for specifed memory location.
  190. *
  191. * This function calculates offset that must be set for BEE region to access physical memory location.
  192. *
  193. * @param addressMemory Address of physical memory location.
  194. */
  195. static inline uint32_t BEE_GetOffset(uint32_t addressMemory)
  196. {
  197. return (addressMemory >> 16);
  198. }
  199. #if defined(__cplusplus)
  200. }
  201. #endif
  202. #endif /* _FSL_BEE_H_ */