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 code for reference only. Modify the code as required.
#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 (default) indicates enabled. */
ENGINE_ctrl_cmd_string(e, "KAE_CMD_ENABLE_ASYNC", "1", 0);
ENGINE_init(e);
/*Specify 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 KAE for the crypto algorithm during initialization (other algorithms do not require KAE). In this way, the code modification workload is reduced.
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.
Parent topic: Using KAE Encryption and Decryption