---
title: KAE加速引擎使用示例代码
description: "当用户需要使用C语言调用KAE时，可以通过ENGINE_by_id函数来获取KAE句柄后再使能相应的算法。本节提供的仅是使用示例代码，使用过程中请根据实际业务需求进行配置修改。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/251RC1/accel/kunpengaccel_06_0027.html
sourcePath: /source/zh/kunpengboostkithistory/251RC1/accel/kunpengaccel_06_0027.html
indexId: df13418743cd4bfabe00468042112e18886dc630f2a49519c5724e9f8461b94872
---
# KAE加速引擎使用示例代码

当用户需要使用C语言调用KAE时，可以通过ENGINE_by_id函数来获取KAE句柄后再使能相应的算法。本节提供的仅是使用示例代码，使用过程中请根据实际业务需求进行配置修改。

```
#include <stdio.h>
#include <stdlib.h>

/* OpenSSL headers */
#include <openssl/bio.h>
#include <openssl/ssl.h>
#include <openssl/err.h>
#include <openssl/engine.h>
 
int main(int argc, char **argv)
{
    /* Initializing OpenSSL */
    SSL_load_error_strings();
    ERR_load_BIO_strings();
    OpenSSL_add_all_algorithms();
    
    /*You can use ENGINE_by_id Function to get the handle of the Huawei Accelerator Engine*/
    ENGINE *e = ENGINE_by_id("kae");
    /*使能KAE加速引擎异步功能，可选配置，设置为“0”表示不使能，设置为“1”表示使能，默认使能异步功能*/
    ENGINE_ctrl_cmd_string(e, "KAE_CMD_ENABLE_ASYNC", "1", 0);
    ENGINE_init(e);
    /*指定KAE加速引擎用于RSA加解密，如果初始时使用ENGINE_set_default_RSA(ENGINE *e);则无需传入e*/
    RSA *rsa = RSA_new_method(e);
    /*The user code*/
    ……
    
    ENGINE_free(e);
    
}
```

用户还可以在初始化阶段指定crypto相应算法使用KAE加速引擎，其他算法不需要使用KAE加速引擎，这样对已有的代码修改量将更小，只需要在初始化的某个阶段设置一下即可。

```
int ENGINE_set_default_RSA(ENGINE *e); 
int ENGINE_set_default_DH(ENGINE *e);
int ENGINE_set_default_ciphers(ENGINE *e);
int ENGINE_set_default_digests(ENGINE *e);
int ENGINE_set_default(ENGINE *e, unsigned int flags);
```

更多使用API方法请访问OpenSSL官网(https://www.openssl.org/docs/manpages.html)。
