1919/* Dynamic Binary Code 2 Modulo, which is 10^6 according to the spec. */
2020#define DBC2_MODULO 1000000
2121
22+ /* The size of a counter in bytes. */
23+ #define COUNTER_SIZE 8
24+
2225/*
2326 * Currently this only supports a single key, in the future this could be
2427 * updated to support multiple users, all with different unique keys (stored
2528 * using secure storage).
2629 */
27- static uint8_t K [MAX_KEY_SIZE ];
28- static uint32_t K_len ;
29-
30- /* The counter as defined by RFC4226. */
31- static uint8_t counter [] = { 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 , 0x0 };
30+ struct hotp_key
31+ {
32+ uint8_t K [MAX_KEY_SIZE ];
33+ uint32_t K_len ;
34+ /* The counter as defined by RFC4226. */
35+ uint8_t counter [COUNTER_SIZE ];
36+ };
3237
3338/*
3439 * HMAC a block of memory to produce the authentication tag
@@ -126,7 +131,8 @@ static void truncate(uint8_t *hmac_result, uint32_t *bin_code)
126131 * bin_code %= DBC2_MODULO ;
127132}
128133
129- static TEE_Result register_shared_key (uint32_t param_types , TEE_Param params [4 ])
134+ static TEE_Result register_shared_key (struct hotp_key * state ,
135+ uint32_t param_types , TEE_Param params [4 ])
130136{
131137 TEE_Result res = TEE_SUCCESS ;
132138
@@ -140,19 +146,20 @@ static TEE_Result register_shared_key(uint32_t param_types, TEE_Param params[4])
140146 return TEE_ERROR_BAD_PARAMETERS ;
141147 }
142148
143- if (params [0 ].memref .size > sizeof (K ))
149+ if (params [0 ].memref .size > sizeof (state -> K ))
144150 return TEE_ERROR_BAD_PARAMETERS ;
145151
146- memset (K , 0 , sizeof (K ));
147- memcpy (K , params [0 ].memref .buffer , params [0 ].memref .size );
152+ memset (state -> K , 0 , sizeof (state -> K ));
153+ memcpy (state -> K , params [0 ].memref .buffer , params [0 ].memref .size );
148154
149- K_len = params [0 ].memref .size ;
150- DMSG ("Got shared key %s (%u bytes)." , K , params [0 ].memref .size );
155+ state -> K_len = params [0 ].memref .size ;
156+ DMSG ("Got shared key %s (%u bytes)." , state -> K , params [0 ].memref .size );
151157
152158 return res ;
153159}
154160
155- static TEE_Result get_hotp (uint32_t param_types , TEE_Param params [4 ])
161+ static TEE_Result get_hotp (struct hotp_key * state ,
162+ uint32_t param_types , TEE_Param params [4 ])
156163{
157164 TEE_Result res = TEE_SUCCESS ;
158165 uint32_t hotp_val ;
@@ -170,11 +177,12 @@ static TEE_Result get_hotp(uint32_t param_types, TEE_Param params[4])
170177 return TEE_ERROR_BAD_PARAMETERS ;
171178 }
172179
173- res = hmac_sha1 (K , K_len , counter , sizeof (counter ), mac , & mac_len );
180+ res = hmac_sha1 (state -> K , state -> K_len , state -> counter ,
181+ sizeof (state -> counter ), mac , & mac_len );
174182
175183 /* Increment the counter. */
176- for (i = sizeof (counter ) - 1 ; i >= 0 ; i -- ) {
177- if (++ counter [i ])
184+ for (i = sizeof (state -> counter ) - 1 ; i >= 0 ; i -- ) {
185+ if (++ ( state -> counter ) [i ])
178186 break ;
179187 }
180188
@@ -199,32 +207,45 @@ void TA_DestroyEntryPoint(void)
199207
200208TEE_Result TA_OpenSessionEntryPoint (uint32_t param_types ,
201209 TEE_Param __unused params [4 ],
202- void __unused * * sess_ctx )
210+ void * * sess_ctx )
203211{
212+ struct hotp_key * state = NULL ;
213+
204214 uint32_t exp_param_types = TEE_PARAM_TYPES (TEE_PARAM_TYPE_NONE ,
205215 TEE_PARAM_TYPE_NONE ,
206216 TEE_PARAM_TYPE_NONE ,
207217 TEE_PARAM_TYPE_NONE );
208218 if (param_types != exp_param_types )
209219 return TEE_ERROR_BAD_PARAMETERS ;
210220
221+ /*
222+ * Allocate and init state for the session.
223+ */
224+ state = TEE_Malloc (sizeof (* state ), 0 );
225+ if (!state )
226+ return TEE_ERROR_OUT_OF_MEMORY ;
227+
228+ * sess_ctx = state ;
229+
211230 return TEE_SUCCESS ;
212231}
213232
214- void TA_CloseSessionEntryPoint (void __unused * sess_ctx )
233+ void TA_CloseSessionEntryPoint (void * sess_ctx )
215234{
235+ TEE_Free (sess_ctx );
236+ sess_ctx = NULL ;
216237}
217238
218- TEE_Result TA_InvokeCommandEntryPoint (void __unused * sess_ctx ,
239+ TEE_Result TA_InvokeCommandEntryPoint (void * sess_ctx ,
219240 uint32_t cmd_id ,
220241 uint32_t param_types , TEE_Param params [4 ])
221242{
222243 switch (cmd_id ) {
223244 case TA_HOTP_CMD_REGISTER_SHARED_KEY :
224- return register_shared_key (param_types , params );
245+ return register_shared_key (sess_ctx , param_types , params );
225246
226247 case TA_HOTP_CMD_GET_HOTP :
227- return get_hotp (param_types , params );
248+ return get_hotp (sess_ctx , param_types , params );
228249
229250 default :
230251 return TEE_ERROR_BAD_PARAMETERS ;
0 commit comments