---
title: 通过OpenSSL配置文件openssl.cnf使用KAE
description: "当需要使用OpenSSL配置文件调用KAE时，需要在配置文件openssl.cnf中添加KAE相关配置参数。通过配置文件方式使用KAE，可以使用户的APP在非常小的修改量情况下使用加速器功能。"
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/2300/accel/kunpengaccel_16_0021.html
sourcePath: /source/zh/kunpengboostkithistory/2300/accel/kunpengaccel_16_0021.html
indexId: ae2351e0b7f745c9dd5e645bcad3ba098e84d036c5370cba48682c34098cd86e70
---
# 通过OpenSSL配置文件openssl.cnf使用KAE

当需要使用OpenSSL配置文件调用KAE时，需要在配置文件openssl.cnf中添加KAE相关配置参数。通过配置文件方式使用KAE，可以使用户的APP在非常小的修改量情况下使用加速器功能。

如下所示，仅需调用一次此初始化API即可完成相应的配置工作：

```
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
```

如果用到openssl req -new -x509命令生成证书功能，请参见使用openssl req -new -x509命令生成证书失败(https://www.hikunpeng.com/document/detail/zh/kunpengaccel/kae/kae/docs/zh/faq.md#使用openssl-req--new--x509命令生成证书失败)中的方法二完成openssl.cnf文件的配置。


新建openssl.cnf需要添加如下配置信息。

```
openssl_conf=openssl_def
[openssl_def]
engines=engine_section
[engine_section]
kae=kae_section
[kae_section]
engine_id=kae
dynamic_path=/usr/local/lib/engines-1.1/kae.so
KAE_CMD_ENABLE_ASYNC=1
KAE_CMD_ENABLE_SM3=1
KAE_CMD_ENABLE_SM4=1
default_algorithms=ALL
init=1
```

- KAE_CMD_ENABLE_ASYNC为可选配置，0表示不使能异步功能，1表示使能异步功能，默认使能。
- KAE_CMD_ENABLE_SM3为可选配置，0表示不使能SM3加速功能，1表示使能SM3加速功能，默认使能。
- KAE_CMD_ENABLE_SM4为可选配置，0表示不使能SM4加速功能，1表示使能SM4加速功能，默认使能。
- default_algorithms=ALL表示所有算法优先查找KAE加速引擎，若引擎不支持，则切换OpenSSL进行计算。


设置OPENSSL_CONF环境变量：

```
export OPENSSL_CONF=/home/app/openssl.cnf  #该路径为openssl.cnf存放路径
```

使用OpenSSL配置文件示例如下：

```
#include <stdio.h>
#include <stdlib.h>
/* OpenSSL headers */
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/engine.h>
int main(int argc, char **argv)
{
/* Initializing OpenSSL */
ERR_load_BIO_strings();
/* Load openssl configure */
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CONFIG, NULL);
/*The user code*/
……
```
