TEE AES对称加解密开发实例

/**
 * 
 *
 */
TEE_Result lge_utils_make_aes_keyobj(TEE_ObjectHandle * aes_key_obj, uint8_t * key, uint32_t max_key_size)
{
        TEE_Result ret;
        TEE_Attribute                attrs = {};
        uint32_t                attr_count = 1;
        ret = TEE_AllocateTransientObject(TEE_TYPE_AES, max_key_size * 8, aes_key_obj);
        if (ret != TEE_SUCCESS) {
                EMSG("TEE_AllocateTransientObject failed 0x%x", ret);
                return ret;
        }
        TEE_InitRefAttribute(&attrs, TEE_ATTR_SECRET_VALUE, key, max_key_size);
        ret = TEE_PopulateTransientObject(*aes_key_obj, &attrs, attr_count);
        if (ret != TEE_SUCCESS) {
                EMSG("TEE_PopulateTransientObject failed 0x%x", ret);
                goto exit;
        }
        return TEE_SUCCESS;
exit:
        TEE_FreeTransientObject(*aes_key_obj);
        return ret;
}
/**
 * 
 *
 */
TEE_Result lge_utils_aes_encrypt(TEE_ObjectHandle aes_key_obj, uint32_t max_key_size, uint8_t *msg_buffer, size_t msg_len, uint8_t *encrypted_buffer, size_t *enc_len)
{
    TEE_Result ret;
    TEE_OperationHandle oper_enc = NULL;
    ret = TEE_AllocateOperation(&oper_enc, TEE_ALG_AES_ECB_NOPAD, TEE_MODE_ENCRYPT , max_key_size * 8);
    if (ret != TEE_SUCCESS) {
            EMSG("TEE_AllocateOperation failed 0x%x", ret);
            return ret;
    }
    ret = TEE_SetOperationKey(oper_enc, aes_key_obj);
    if (ret != TEE_SUCCESS) {
        EMSG("Fail to set rsa encrypt key, ret 0x%xn", ret);
        TEE_FreeOperation(oper_enc);
        return ret;
    }
    EMSG("TEE_SetOperationKey success");
    TEE_CipherInit(oper_enc, NULL, 0);
    ret = TEE_CipherDoFinal(oper_enc, msg_buffer, msg_len, encrypted_buffer, enc_len);
    if (ret != TEE_SUCCESS) {
            EMSG("TEE_CipherDoFinal failed 0x%x", ret);
            goto exit;
    }
    EMSG("AES enc len:%d", *enc_len);
exit:
        TEE_FreeTransientObject(aes_key_obj);
        TEE_FreeOperation(oper_enc);
        return ret;
}
/**
 * 
 *
 */
TEE_Result lge_utils_aes_decrypt(TEE_ObjectHandle aes_key_obj, uint32_t max_key_size, uint8_t *msg_buffer, size_t msg_len, uint8_t *encrypted_buffer, size_t *enc_len)
{
    TEE_Result ret;
    TEE_OperationHandle oper_enc = NULL;
    ret = TEE_AllocateOperation(&oper_enc, TEE_ALG_AES_ECB_NOPAD, TEE_MODE_DECRYPT , max_key_size * 8);
    if (ret != TEE_SUCCESS) {
            EMSG("TEE_AllocateOperation failed 0x%x", ret);
            return ret;
    }
    ret = TEE_SetOperationKey(oper_enc, aes_key_obj);
    if (ret != TEE_SUCCESS) {
        EMSG("Fail to set rsa encrypt key, ret 0x%xn", ret);
        TEE_FreeOperation(oper_enc);
        return ret;
    }
    EMSG("TEE_SetOperationKey success");
    TEE_CipherInit(oper_enc, NULL, 0);
    ret = TEE_CipherDoFinal(oper_enc, msg_buffer, msg_len, encrypted_buffer, enc_len);
    if (ret != TEE_SUCCESS) {
            EMSG("TEE_CipherDoFinal failed 0x%x", ret);
            goto exit;
    }
    EMSG("AES enc len:%d", *enc_len);
exit:
        TEE_FreeTransientObject(aes_key_obj);
        TEE_FreeOperation(oper_enc);
        return ret;
}

  

posted @ 2023-01-06 17:15  颜小雀  阅读(287)  评论(0编辑  收藏  举报