KAE加速引擎使用示例代码

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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加速引擎,这样对已有的代码修改量将更小,只需要在初始化的某个阶段设置一下即可。

1
2
3
4
5
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