@@ -2119,17 +2119,17 @@ tee_svc_obj_generate_key_x25519(struct tee_obj *o,
21192119 uint32_t param_count )
21202120{
21212121 TEE_Result res = TEE_ERROR_GENERIC ;
2122- struct x25519_keypair * tee_x25519_key = NULL ;
2122+ struct x25519_keypair * key = NULL ;
21232123
21242124 /* Copy the present attributes into the obj before starting */
21252125 res = tee_svc_cryp_obj_populate_type (o , type_props , params ,
21262126 param_count );
21272127 if (res != TEE_SUCCESS )
21282128 return res ;
21292129
2130- tee_x25519_key = ( struct x25519_keypair * ) o -> attr ;
2130+ key = o -> attr ;
21312131
2132- res = crypto_acipher_gen_x25519_key (tee_x25519_key , key_size );
2132+ res = crypto_acipher_gen_x25519_key (key , key_size );
21332133 if (res != TEE_SUCCESS )
21342134 return res ;
21352135
@@ -2174,6 +2174,7 @@ tee_svc_obj_ed25519_sign(struct ed25519_keypair *key,
21742174 uint8_t * sig , size_t * sig_len ,
21752175 const TEE_Attribute * params , size_t num_params )
21762176{
2177+ TEE_Result err ;
21772178 size_t n ;
21782179 size_t ctx_len = 0 ;
21792180 uint8_t * ctx = NULL ;
@@ -2187,26 +2188,33 @@ tee_svc_obj_ed25519_sign(struct ed25519_keypair *key,
21872188 break ;
21882189
21892190 case TEE_ATTR_EDDSA_CTX :
2190- cx_flag = true;
2191+ /* only first provided context if effective */
2192+ if (cx_flag )
2193+ break ;
21912194 ctx_len = params [n ].content .ref .length ;
21922195 if (ctx_len > 255 )
21932196 return TEE_ERROR_BAD_PARAMETERS ;
2194- ctx = mempool_calloc (mempool_default , 1 , 256 );
2197+ ctx = mempool_calloc (mempool_default , 1 , ctx_len + 1 );
21952198 if (!ctx )
21962199 return TEE_ERROR_OUT_OF_MEMORY ;
21972200 memcpy (ctx , params [n ].content .ref .buffer , ctx_len );
21982201 ctx [ctx_len ] = 0 ;
2202+ cx_flag = true;
21992203 break ;
22002204
22012205 default :
22022206 return TEE_ERROR_BAD_PARAMETERS ;
22032207 }
22042208 }
22052209
2206- if (ph_flag || cx_flag )
2207- return crypto_acipher_ed25519ctx_sign (key , msg , msg_len , sig ,
2210+ if (ph_flag || cx_flag ) {
2211+ err = crypto_acipher_ed25519ctx_sign (key , msg , msg_len , sig ,
22082212 sig_len , ph_flag ,
22092213 ctx , ctx_len );
2214+ if (ctx )
2215+ mempool_free (mempool_default , ctx );
2216+ return err ;
2217+ }
22102218
22112219 return crypto_acipher_ed25519_sign (key , msg , msg_len , sig , sig_len );
22122220}
@@ -2217,36 +2225,44 @@ tee_svc_obj_ed25519_verify(struct ed25519_keypair *key,
22172225 const uint8_t * sig , size_t sig_len ,
22182226 const TEE_Attribute * params , size_t num_params )
22192227{
2228+ TEE_Result err ;
22202229 size_t n ;
22212230 size_t ctx_len = 0 ;
2222- uint8_t ctx [ 256 ] = { 0 } ;
2223- uint8_t ph_flag = 0 ;
2224- uint8_t cx_flag = 0 ;
2231+ uint8_t * ctx = NULL ;
2232+ bool ph_flag = false ;
2233+ bool cx_flag = false ;
22252234
2226- for (n = 0u ; n < num_params ; n ++ ) {
2235+ for (n = 0 ; n < num_params ; n ++ ) {
22272236 switch (params [n ].attributeID ) {
22282237 case TEE_ATTR_EDDSA_PREHASH :
2229- ph_flag = 1 ;
2238+ ph_flag = true ;
22302239 break ;
22312240
22322241 case TEE_ATTR_EDDSA_CTX :
2233- cx_flag = 1 ;
2242+ /* only first provided context if effective */
2243+ if (cx_flag )
2244+ break ;
22342245 ctx_len = params [n ].content .ref .length ;
22352246 if (ctx_len > 255 )
22362247 return TEE_ERROR_BAD_PARAMETERS ;
2237-
2248+ ctx = mempool_calloc ( mempool_default , 1 , ctx_len + 1 );
22382249 memcpy (ctx , params [n ].content .ref .buffer , ctx_len );
22392250 ctx [ctx_len ] = 0 ;
2251+ cx_flag = true;
22402252 break ;
22412253
22422254 default :
22432255 return TEE_ERROR_BAD_PARAMETERS ;
22442256 }
22452257 }
2246- if (ph_flag || cx_flag )
2247- return crypto_acipher_ed25519ctx_verify (key , msg , msg_len , sig ,
2258+ if (ph_flag || cx_flag ) {
2259+ err = crypto_acipher_ed25519ctx_verify (key , msg , msg_len , sig ,
22482260 sig_len , ph_flag ,
22492261 ctx , ctx_len );
2262+ if (ctx )
2263+ mempool_free (mempool_default , ctx );
2264+ return err ;
2265+ }
22502266
22512267 return crypto_acipher_ed25519_verify (key , msg , msg_len , sig , sig_len );
22522268}
0 commit comments