use KM_CopyEcdhPrivateAttributes() to copy ECDSA to ECDH private key 14/297214/1
authorDongsun Lee <ds73.lee@samsung.com>
Mon, 14 Aug 2023 03:00:49 +0000 (12:00 +0900)
committerDongsun Lee <ds73.lee@samsung.com>
Mon, 14 Aug 2023 03:00:49 +0000 (12:00 +0900)
Change-Id: I582511a5390537c5effb5349842c392a53f0d285

ta/src/cmd_exec.c

index 31a8edfeb8324eaa6bd1331786aac3a293000a2e..843aec839c2a87e99c7a756729bd824adb982f94 100644 (file)
@@ -922,6 +922,71 @@ clean:
        return ret;
 }
 
+
+static TEE_Result KM_CopyEcdhPrivateAttributes(TEE_ObjectHandle dest_key,
+                                                                                               TEE_ObjectHandle src_key)
+{
+       TEE_Result ret = TEE_SUCCESS;
+       uint32_t curve_a = 0, curve_b = 0;
+       KM_BinaryData data_priv = {0, NULL};
+       KM_BinaryData data_pub_x = {0, NULL};
+       KM_BinaryData data_pub_y = {0, NULL};
+       TEE_Attribute attrs[4];
+       TEE_ObjectInfo info_src, info_dest;
+
+       TEE_GetObjectInfo(src_key, &info_src);
+       TEE_GetObjectInfo(dest_key, &info_dest);
+
+       // check compatibility of source & destination
+       if (info_dest.objectType != TEE_TYPE_ECDH_KEYPAIR
+               || !(
+                       (info_src.objectType == TEE_TYPE_ECDSA_KEYPAIR) || (info_src.objectType == TEE_TYPE_ECDH_KEYPAIR)
+               )) {
+               ret = TEE_ERROR_GENERIC;
+               LOG("Invalid Object Type. source object type=%x, dest object type=%x",
+                       info_src.objectType, info_dest.objectType);
+               goto  clean;
+       }
+
+       ret = TEE_GetObjectValueAttribute(src_key, TEE_ATTR_ECC_CURVE, &curve_a, &curve_b);
+       if (ret != TEE_SUCCESS) {
+               LOG("Failed to get TEE_ATTR_ECC_CURVE attribute info, ret = %x", ret);
+               goto  clean;
+       }
+       ret = KM_GetBufferAttribute(src_key, TEE_ATTR_ECC_PRIVATE_VALUE, &data_priv);
+       if (ret != TEE_SUCCESS) {
+               LOG("Failed to get TEE_ATTR_ECC_PRIVATE_VALUE attribute, ret = %x", ret);
+               goto  clean;
+       }
+       ret = KM_GetBufferAttribute(src_key, TEE_ATTR_ECC_PUBLIC_VALUE_X, &data_pub_x);
+       if (ret != TEE_SUCCESS) {
+               LOG("Failed to get TEE_ATTR_ECC_PUBLIC_VALUE_X attribute, ret = %x", ret);
+               goto  clean;
+       }
+       ret = KM_GetBufferAttribute(src_key, TEE_ATTR_ECC_PUBLIC_VALUE_Y, &data_pub_y);
+       if (ret != TEE_SUCCESS) {
+               LOG("Failed to get TEE_ATTR_ECC_PUBLIC_VALUE_Y attribute, ret = %x", ret);
+               goto  clean;
+       }
+
+       KM_InitValueAttribute(&attrs[0], TEE_ATTR_ECC_CURVE, curve_a, curve_b);
+       KM_InitRefAttribute(&attrs[1], TEE_ATTR_ECC_PRIVATE_VALUE, data_priv.data, data_priv.data_size);
+       KM_InitRefAttribute(&attrs[2], TEE_ATTR_ECC_PUBLIC_VALUE_X, data_pub_x.data, data_pub_x.data_size);
+       KM_InitRefAttribute(&attrs[3], TEE_ATTR_ECC_PUBLIC_VALUE_Y, data_pub_y.data, data_pub_y.data_size);
+
+       ret = TEE_PopulateTransientObject(dest_key, attrs, sizeof(attrs) / sizeof(attrs[0]));
+       if(ret != TEE_SUCCESS) {
+               LOG("Failed to populate transient object. ret=%x.", ret);
+               goto clean;
+       }
+
+clean:
+       free(data_priv.data);
+       free(data_pub_x.data);
+       free(data_pub_y.data);
+       return ret;
+}
+
 static TEE_Result KM_DeriveEcdhSecret(const TEE_ObjectHandle prv_key,
                uint32_t curve, const KM_BinaryData *pub_x, const KM_BinaryData *pub_y,
                TEE_ObjectHandle *secret_hndl)
@@ -971,7 +1036,11 @@ static TEE_Result KM_DeriveEcdhSecret(const TEE_ObjectHandle prv_key,
                                "key_bits_size=%d.", ret, TEE_TYPE_ECDH_KEYPAIR, info.objectSize);
                        goto clean;
                }
-               TEE_CopyObjectAttributes(ecdh_key, prv_key);
+               ret = KM_CopyEcdhPrivateAttributes(ecdh_key, prv_key);
+               if (TEE_SUCCESS != ret) {
+                       LOG("Failed to copy ecdh private attribute=%x.", ret);
+                       goto clean;
+               }
        } else {
                ecdh_key = prv_key;
        }