forked from open-quantum-safe/oqs-provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
static_oqsprovider.c
95 lines (79 loc) · 2.33 KB
/
static_oqsprovider.c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/**
* \file
* \brief Example of how to load oqsprovider when compiled as a static library
* `using OSSL_PROVIDER_add_builtin`.
*/
#include <stdio.h>
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <openssl/provider.h>
/** \brief The initialization function of oqsprovider. */
extern OSSL_provider_init_fn oqs_provider_init;
/** \brief Name of the oqsprovider. */
static const char *kOQSProviderName = "oqsprovider";
/** \brief Tries to load the oqsprovider named "oqsprovider".
*
* \param libctx Context of the OpenSSL library in which to load the
* oqsprovider.
*
* \returns 0 if success, else -1. */
static int load_oqs_provider(OSSL_LIB_CTX *libctx)
{
OSSL_PROVIDER *provider;
int ret;
ret = OSSL_PROVIDER_available(libctx, kOQSProviderName);
if (ret != 0) {
fprintf(stderr,
"`OSSL_PROVIDER_available` returned %i, but 0 was expected\n",
ret);
return -1;
}
ret = OSSL_PROVIDER_add_builtin(libctx, kOQSProviderName,
oqs_provider_init);
if (ret != 1) {
fprintf(stderr,
"`OSSL_PROVIDER_add_builtin` failed with returned code %i\n",
ret);
return -1;
}
provider = OSSL_PROVIDER_load(libctx, kOQSProviderName);
if (provider == NULL) {
fputs("`OSSL_PROVIDER_load` failed\n", stderr);
return -1;
}
ret = OSSL_PROVIDER_available(libctx, kOQSProviderName);
if (ret != 1) {
fprintf(stderr,
"`OSSL_PROVIDER_available` returned %i, but 0 was expected\n",
ret);
return -1;
}
ret = OSSL_PROVIDER_self_test(provider);
if (ret != 1) {
fprintf(stderr,
"`OSSL_PROVIDER_self_test` failed with returned code %i\n",
ret);
return -1;
}
return 0;
}
int main()
{
OSSL_LIB_CTX *libctx;
int ret;
libctx = OSSL_LIB_CTX_new();
if (libctx == NULL) {
fputs("`OSSL_LIB_CTX_new` failed. Cannot initialize OpenSSL.\n",
stderr);
return 1;
}
ret = load_oqs_provider(libctx);
if (ret != 0) {
fputs("`load_oqs_provider` failed. Dumping OpenSSL error queue.\n",
stderr);
ERR_print_errors_fp(stderr);
return 2;
}
OSSL_LIB_CTX_free(libctx);
return 0;
}