Rate This Document
Findability
Accuracy
Completeness
Readability

Example Code for Using KAE

When you need to use the C language to invoke KAE, you can use the ENGINE_by_id function to obtain the KAE handle and then enable the corresponding algorithm. This section provides only sample code. Modify the code as required.

 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");
    /*Enable the KAE asynchronization function. This parameter is optional. The value 0 indicates disabled, and the value 1 indicates enabled. The asynchronization function is enabled by default. */
    ENGINE_ctrl_cmd_string(e, "KAE_CMD_ENABLE_ASYNC", "1", 0);
    ENGINE_init(e);
    /*Specify the KAE for RSA-based encryption and decryption. If ENGINE_set_default_RSA(ENGINE *e) is used during initialization, e does not need to be transferred.*/
    RSA *rsa = RSA_new_method(e);
    /*The user code*/
...
    
    ENGINE_free(e);
    
}

You can also specify the KAE for the crypto algorithm during initialization. Other algorithms do not need to use the KAE. In this way, the existing code modification workload is reduced. You only need to set the KAE in a certain initialization phase.

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);

For details about how to use APIs, visit the official OpenSSL website.