fsl_bee.h 7.6 KB

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