OP-TEE / optee_os

Trusted side of the TEE
Other
1.59k stars 1.07k forks source link

import sm2 public key and encrypt by it #7011

Closed pengbaolv8 closed 1 month ago

pengbaolv8 commented 2 months ago

Hello, expert. i want to import the sm2 public key and encrypt data by this key,but TEE_SetOperationKey will show the error:“invalid obj_type 0xa0000047 or attri_len 0x2”,the object type TEE_TYPE_SM2_PKE_PUBLIC_KEY is wrong?or i miss the some Attribute to init? ==============================code===================================== char x[] = "174071B7E7CF2A12AE380E0C86B9DF469263A6C61FF45BC597AC82C348A3171E"; unsigned int xChar[32]; hex2array(x, xChar,32);

char y[] = "20F22DEA72F3643051DA5DE25D6E643581ED1FF07F5FDC71E271652102DFA13A";
unsigned int yChar[32];
hex2array(y, yChar,32);

TEE_ObjectHandle key_handle;
TEE_OperationHandle Operation = NULL;
TEE_Result res;
TEE_Attribute attrs[2];
res = TEE_AllocateTransientObject(TEE_TYPE_SM2_PKE_PUBLIC_KEY, 256, &key_handle); 
if (res != TEE_SUCCESS) {
    tloge("TEE_AllocateTransientObject() failed res=0x%X\n", (int)res);
}

TEE_InitRefAttribute(&attrs[0], TEE_ATTR_ECC_PUBLIC_VALUE_X, xChar, 32);
TEE_InitRefAttribute(&attrs[1], TEE_ATTR_ECC_PUBLIC_VALUE_Y, yChar, 32);

//add TEE_ATTR_ECC_CURVE will success but encrypt will wrong,response "set sm2 pub key error"
//TEE_InitValueAttribute(&attrs[2], TEE_ATTR_ECC_CURVE,TEE_ECC_CURVE_SM2,0);

res = TEE_PopulateTransientObject(key_handle, attrs, 2); 

res = TEE_AllocateOperation(Operation, TEE_ALG_SM2_PKE, TEE_MODE_ENCRYPT, 256);
if (ret != TEE_SUCCESS) {
    tloge("TEE_AllocateOperation failed.");
}

res = TEE_SetOperationKey(Operation, keyHandle);

res = TEE_AsymmetricEncrypt(Operation, NULL, 0, buf1->buffer, buf1->size, buf2->buffer, &outlen);

github-actions[bot] commented 1 month ago

This issue has been marked as a stale issue because it has been open (more than) 30 days with no activity. Remove the stale label or add a comment, otherwise this issue will automatically be closed in 5 days. Note, that you can always re-open a closed issue at any time.