1 /*
2 * Copyright 2020-2021, NXP
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8 #include "fsl_key_manager.h"
9
10 /*******************************************************************************
11 * Definitions
12 ******************************************************************************/
13
14 /* Component ID definition, used by tools. */
15 #ifndef FSL_COMPONENT_ID
16 #define FSL_COMPONENT_ID "platform.drivers.key_manager"
17 #endif
18
19 /*******************************************************************************
20 * Prototypes
21 ******************************************************************************/
22
23 /*******************************************************************************
24 * Code
25 ******************************************************************************/
26
27 /*!
28 * brief Configures Master key settings.
29 *
30 * This function configures Key Manager's setting for Master key.
31 *
32 * param base Key Manager peripheral address.
33 * param select select source for Master key.
34 * param lock setting for lock Master key.
35 * return status of Master key control operation
36 */
KEYMGR_MasterKeyControll(KEY_MANAGER_Type * base,keymgr_select_t select,keymgr_lock_t lock)37 status_t KEYMGR_MasterKeyControll(KEY_MANAGER_Type *base, keymgr_select_t select, keymgr_lock_t lock)
38 {
39 if ((select != (uint8_t)(KEYMGR_SEL_UDF)) && (select != (uint8_t)(KEYMGR_SEL_PUF)))
40 {
41 return kStatus_InvalidArgument;
42 }
43
44 /* Clear KEY_CTRL_SELECT bitfield */
45 base->MASTER_KEY_CTRL &= ~KEY_MANAGER_MASTER_KEY_CTRL_SELECT_MASK;
46
47 /* Write new setting in MASTER_KEY_CTRL register */
48 base->MASTER_KEY_CTRL |= KEY_MANAGER_MASTER_KEY_CTRL_SELECT(select) | KEY_MANAGER_MASTER_KEY_CTRL_LOCK(lock);
49
50 return kStatus_Success;
51 }
52
53 /*!
54 * brief Configures OTFAD1 key settings.
55 *
56 * This function configures Key Manager's setting for OTFAD1 key.
57 *
58 * param base Key Manager peripheral address.
59 * param select select source for OTFAD1 key.
60 * param lock setting for lock OTFAD1 key.
61 * return status of OTFAD1 key control operation
62 */
KEYMGR_OTFAD1KeyControll(KEY_MANAGER_Type * base,keymgr_select_t select,keymgr_lock_t lock)63 status_t KEYMGR_OTFAD1KeyControll(KEY_MANAGER_Type *base, keymgr_select_t select, keymgr_lock_t lock)
64 {
65 if ((select != (uint8_t)(KEYMGR_SEL_OCOTP)) && (select != (uint8_t)(KEYMGR_SEL_PUF)))
66 {
67 return kStatus_InvalidArgument;
68 }
69
70 /* Clear KEY_CTRL_SELECT bitfield */
71 base->OTFAD1_KEY_CTRL &= ~KEY_MANAGER_OTFAD1_KEY_CTRL_SELECT_MASK;
72
73 /* Write new setting in OTFAD1_KEY_CTRL register */
74 base->OTFAD1_KEY_CTRL |= KEY_MANAGER_OTFAD1_KEY_CTRL_SELECT(select) | KEY_MANAGER_OTFAD1_KEY_CTRL_LOCK(lock);
75
76 return kStatus_Success;
77 }
78
79 /*!
80 * brief Configures OTFAD2 key settings.
81 *
82 * This function configures Key Manager's setting for OTFAD2 key.
83 *
84 * param base Key Manager peripheral address.
85 * param select select source for OTFAD2 key.
86 * param lock setting for lock OTFAD2 key.
87 * return status of OTFAD2 key control operation
88 */
KEYMGR_OTFAD2KeyControll(KEY_MANAGER_Type * base,keymgr_select_t select,keymgr_lock_t lock)89 status_t KEYMGR_OTFAD2KeyControll(KEY_MANAGER_Type *base, keymgr_select_t select, keymgr_lock_t lock)
90 {
91 if ((select != (uint8_t)(KEYMGR_SEL_OCOTP)) && (select != (uint8_t)(KEYMGR_SEL_PUF)))
92 {
93 return kStatus_InvalidArgument;
94 }
95
96 /* Clear KEY_CTRL_SELECT bitfield */
97 base->OTFAD2_KEY_CTRL &= ~KEY_MANAGER_OTFAD2_KEY_CTRL_SELECT_MASK;
98
99 /* Write new setting in OTFAD2_KEY_CTRL register */
100 base->OTFAD2_KEY_CTRL |= KEY_MANAGER_OTFAD2_KEY_CTRL_SELECT(select) | KEY_MANAGER_OTFAD2_KEY_CTRL_LOCK(lock);
101
102 return kStatus_Success;
103 }
104
105 /*!
106 * brief Restart load key signal for IEE.
107 *
108 * This function genrates Key Manager's restart signal for IEE key.
109 *
110 * param base Key Manager peripheral address.
111 */
KEYMGR_IEEKeyReload(KEY_MANAGER_Type * base)112 void KEYMGR_IEEKeyReload(KEY_MANAGER_Type *base)
113 {
114 base->IEE_KEY_CTRL |= KEYMGR_IEE_RELOAD;
115 }
116
117 /*!
118 * brief Lock the key select from PUF.
119 *
120 * This function locks selection of key for PUF.
121 *
122 * param base Key Manager peripheral address.
123 */
KEYMGR_PUFKeyLock(KEY_MANAGER_Type * base,keymgr_lock_t lock)124 void KEYMGR_PUFKeyLock(KEY_MANAGER_Type *base, keymgr_lock_t lock)
125 {
126 base->PUF_KEY_CTRL |= KEY_MANAGER_PUF_KEY_CTRL_LOCK(lock);
127 }
128
129 /*!
130 * brief Sets the default configuration of Key manager slot.
131 *
132 * This function initialize Key Manager slot config structure to default values.
133 *
134 * param config Pointer to slot configuration structure.
135 */
KEYMGR_GetDefaultConfig(domain_slot_config_t * config)136 status_t KEYMGR_GetDefaultConfig(domain_slot_config_t *config)
137 {
138 if (config == NULL)
139 {
140 return kStatus_InvalidArgument;
141 }
142
143 config->lockControl = kKEYMGR_Unlock;
144 config->allowUser = kKEYMGR_Allow;
145 config->allowNonSecure = kKEYMGR_Allow;
146 config->lockList = kKEYMGR_Unlock;
147 config->whiteList = 0u;
148
149 return kStatus_Success;
150 }
151
152 /*!
153 * brief Configures Slot Domain control.
154 *
155 * This function configures domain slot control which locks and allows writes.
156 *
157 * param base Key Manager peripheral address.
158 * param config Pointer to slot configuration structure.
159 * param slot Select slot to be configured.
160 */
KEYMGR_SlotControl(KEY_MANAGER_Type * base,domain_slot_config_t * config,keymgr_slot_t slot)161 status_t KEYMGR_SlotControl(KEY_MANAGER_Type *base, domain_slot_config_t *config, keymgr_slot_t slot)
162 {
163 if (slot == kKEYMGR_Slot0)
164 {
165 base->SLOT0_CTRL |=
166 KEY_MANAGER_SLOT0_CTRL_WHITE_LIST(config->whiteList) | KEY_MANAGER_SLOT0_CTRL_LOCK_LIST(config->lockList) |
167 KEY_MANAGER_SLOT0_CTRL_TZ_NS(config->allowNonSecure) | KEY_MANAGER_SLOT0_CTRL_TZ_USER(config->allowUser) |
168 KEY_MANAGER_SLOT0_CTRL_LOCK_CONTROL(config->lockControl);
169 }
170 else if (slot == kKEYMGR_Slot1)
171 {
172 base->SLOT1_CTRL |=
173 KEY_MANAGER_SLOT0_CTRL_WHITE_LIST(config->whiteList) | KEY_MANAGER_SLOT0_CTRL_LOCK_LIST(config->lockList) |
174 KEY_MANAGER_SLOT0_CTRL_TZ_NS(config->allowNonSecure) | KEY_MANAGER_SLOT0_CTRL_TZ_USER(config->allowUser) |
175 KEY_MANAGER_SLOT1_CTRL_LOCK_CONTROL(config->lockControl);
176 }
177 else if (slot == kKEYMGR_Slot2)
178 {
179 base->SLOT2_CTRL |=
180 KEY_MANAGER_SLOT0_CTRL_WHITE_LIST(config->whiteList) | KEY_MANAGER_SLOT0_CTRL_LOCK_LIST(config->lockList) |
181 KEY_MANAGER_SLOT0_CTRL_TZ_NS(config->allowNonSecure) | KEY_MANAGER_SLOT0_CTRL_TZ_USER(config->allowUser) |
182 KEY_MANAGER_SLOT2_CTRL_LOCK_CONTROL(config->lockControl);
183 }
184 else if (slot == kKEYMGR_Slot3)
185 {
186 base->SLOT3_CTRL |=
187 KEY_MANAGER_SLOT0_CTRL_WHITE_LIST(config->whiteList) | KEY_MANAGER_SLOT0_CTRL_LOCK_LIST(config->lockList) |
188 KEY_MANAGER_SLOT0_CTRL_TZ_NS(config->allowNonSecure) | KEY_MANAGER_SLOT0_CTRL_TZ_USER(config->allowUser) |
189 KEY_MANAGER_SLOT3_CTRL_LOCK_CONTROL(config->lockControl);
190 }
191 else if (slot == kKEYMGR_Slot4)
192 {
193 base->SLOT4_CTRL |=
194 KEY_MANAGER_SLOT0_CTRL_WHITE_LIST(config->whiteList) | KEY_MANAGER_SLOT0_CTRL_LOCK_LIST(config->lockList) |
195 KEY_MANAGER_SLOT0_CTRL_TZ_NS(config->allowNonSecure) | KEY_MANAGER_SLOT0_CTRL_TZ_USER(config->allowUser) |
196 KEY_MANAGER_SLOT4_CTRL_LOCK_CONTROL(config->lockControl);
197 }
198 else
199 {
200 return kStatus_InvalidArgument;
201 }
202
203 return kStatus_Success;
204 }
205
206 /*!
207 * brief Enables clock for Key Manager module.
208 *
209 * This function enables clocks for Key Manager module.
210 *
211 * param base Key Manager peripheral address.
212 */
KEYMGR_Init(KEY_MANAGER_Type * base)213 void KEYMGR_Init(KEY_MANAGER_Type *base)
214 {
215 #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
216 CLOCK_EnableClock(kCLOCK_Key_Manager);
217 #endif
218 }
219