#include <string.h>
#include <errno.h>
+#include <assert.h>
#include <gcrypt.h>
#include "crypto_backend.h"
#define GCRYPT_REQ_VERSION "1.1.42"
+static int crypto_backend_initialised = 0;
+
struct crypt_hash {
gcry_md_hd_t hd;
int hash_id;
int crypt_backend_init(void)
{
+ if (crypto_backend_initialised)
+ return 0;
+
log_dbg("Initialising gcrypt crypto backend.");
if (!gcry_control (GCRYCTL_INITIALIZATION_FINISHED_P)) {
if (!gcry_check_version (GCRYPT_REQ_VERSION)) {
gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
}
+ crypto_backend_initialised = 1;
return 0;
}
/* HASH */
int crypt_hash_size(const char *name)
{
- int hash_id = gcry_md_map_name(name);
+ int hash_id;
+ assert(crypto_backend_initialised);
+
+ hash_id = gcry_md_map_name(name);
if (!hash_id)
return -EINVAL;
{
struct crypt_hash *h;
+ assert(crypto_backend_initialised);
+
h = malloc(sizeof(*h));
if (!h)
return -ENOMEM;
{
struct crypt_hmac *h;
+ assert(crypto_backend_initialised);
+
h = malloc(sizeof(*h));
if (!h)
return -ENOMEM;
#define SOL_ALG 279
#endif
+static int crypto_backend_initialised = 0;
+
struct hash_alg {
const char *name;
const char *kernel_name;
};
int tfmfd = -1, opfd = -1;
+ if (crypto_backend_initialised)
+ return 0;
+
log_dbg("Initialising kernel crypto API backend.");
if (uname(&uts) == -1 || strcmp(uts.sysname, "Linux"))
close(tfmfd);
close(opfd);
+
+ crypto_backend_initialised = 1;
return 0;
}
#include <nss/pk11pub.h>
#include "crypto_backend.h"
+static int crypto_backend_initialised = 0;
+
struct hash_alg {
const char *name;
SECOidTag oid;
int crypt_backend_init(void)
{
+ if (crypto_backend_initialised)
+ return 0;
+
log_dbg("Initialising NSS crypto backend.");
if (NSS_NoDB_Init(".") != SECSuccess)
return -EINVAL;
+ crypto_backend_initialised = 1;
return 0;
}
#include <openssl/hmac.h>
#include "crypto_backend.h"
+static int crypto_backend_initialised = 0;
+
struct crypt_hash {
EVP_MD_CTX md;
const EVP_MD *hash_id;
int crypt_backend_init(void)
{
+ if (crypto_backend_initialised)
+ return 0;
+
OpenSSL_add_all_digests();
log_dbg("OpenSSL crypto backend initialized.");
+
+ crypto_backend_initialised = 1;
return 0;
}
/* If crypto backend is not implemented in userspace,
* but uses some kernel part, we must measure also time
* spent in kernel. */
- if (crypt_backend_flags() & CRYPT_BACKEND_KERNEL)
+ if (crypt_backend_flags() & CRYPT_BACKEND_KERNEL) {
timer_type = ITIMER_PROF;
- else
+ signal(SIGPROF,sigvtalarm);
+ } else {
timer_type = ITIMER_VIRTUAL;
+ signal(SIGVTALRM,sigvtalarm);
+ }
- signal(SIGVTALRM,sigvtalarm);
- signal(SIGPROF,sigvtalarm);
it.it_interval.tv_usec = 0;
it.it_interval.tv_sec = 0;
it.it_value.tv_usec = 0;
/* Initialisation of both RNG file descriptors is mandatory */
int crypt_random_init(struct crypt_device *ctx)
{
+ if (random_initialised)
+ return 0;
+
/* Used for CRYPT_RND_NORMAL */
if(urandom_fd == -1)
urandom_fd = open(URANDOM_DEVICE, O_RDONLY);