Skip to content

Commit

Permalink
libknet: move crypto to fully modular system
Browse files Browse the repository at this point in the history
Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
  • Loading branch information
fabbione committed Oct 12, 2012
1 parent 5492062 commit 2b75718
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
42 changes: 35 additions & 7 deletions libknet/crypto.c
@@ -1,17 +1,40 @@
#include "config.h"

#include <stdlib.h>
#include <string.h>

#include "crypto.h"
#include "nsscrypto.h"
#include "libknet-private.h"
#include "libknet.h"

#ifdef CRYPTO_DEBUG
#include <stdio.h>
#define log_printf(format, args...) fprintf(stderr, format "\n", ##args);
#else
#define log_printf(format, args...);
#endif

/*
* internal module switch data
*/

crypto_model_t modules_cmds[] = {
{ "nss", nsscrypto_init, nsscrypto_fini, nsscrypto_encrypt_and_sign, nsscrypto_authenticate_and_decrypt },
{ NULL, NULL, NULL, NULL, NULL },
};

static int get_model(const char *model)
{
int idx = 0;

while (modules_cmds[idx].model_name != NULL) {
if (!strcmp(modules_cmds[idx].model_name, model))
return idx;
idx++;
}
return -1;
}

/*
* exported API
*/
Expand All @@ -23,15 +46,15 @@ int crypto_encrypt_and_sign (
unsigned char *buf_out,
ssize_t *buf_out_len)
{
return nsscrypto_encrypt_and_sign(instance->model_instance,
return modules_cmds[instance->model].crypt(instance->model_instance,
buf_in, buf_in_len, buf_out, buf_out_len);
}

int crypto_authenticate_and_decrypt (struct crypto_instance *instance,
unsigned char *buf,
ssize_t *buf_len)
{
return nsscrypto_authenticate_and_decrypt(instance->model_instance, buf, buf_len);
return modules_cmds[instance->model].decrypt(instance->model_instance, buf, buf_len);
}

int crypto_init(
Expand All @@ -50,20 +73,25 @@ int crypto_init(
return -1;
}

/* do the model switch here */
if (nsscrypto_init(knet_h, knet_handle_crypto_cfg)) {
knet_h->crypto_instance->model = get_model(knet_handle_crypto_cfg->crypto_model);
if (knet_h->crypto_instance->model < 0) {
log_printf("model %s not supported", knet_handle_crypto_cfg->crypto_model);
return -1;
}

if (modules_cmds[knet_h->crypto_instance->model].init(knet_h, knet_handle_crypto_cfg)) {
free(knet_h->crypto_instance);
return -1;
}

return nsscrypto_init(knet_h, knet_handle_crypto_cfg);
return 0;
}

void crypto_fini(
knet_handle_t knet_h)
{
if (knet_h->crypto_instance) {
nsscrypto_fini(knet_h);
modules_cmds[knet_h->crypto_instance->model].fini(knet_h);
free(knet_h->crypto_instance);
knet_h->crypto_instance = NULL;
}
Expand Down
10 changes: 9 additions & 1 deletion libknet/crypto.h
Expand Up @@ -2,13 +2,21 @@
#define CRYPTO_H_DEFINED

#include <sys/types.h>
#include "libknet.h"
#include "libknet-private.h"

struct crypto_instance {
int model;
void *model_instance;
};

typedef struct {
const char *model_name;
int (*init) (knet_handle_t knet_h, struct knet_handle_crypto_cfg *knet_handle_crypto_cfg);
void (*fini) (knet_handle_t knet_h);
int (*crypt) (void *model_instance, const unsigned char *buf_in, const ssize_t buf_in_len, unsigned char *buf_out, ssize_t *buf_out_len);
int (*decrypt) (void *model_instance, unsigned char *buf, ssize_t *buf_len);
} crypto_model_t;

int crypto_authenticate_and_decrypt (
struct crypto_instance *instance,
unsigned char *buf,
Expand Down
10 changes: 8 additions & 2 deletions libknet/nsscrypto.c
Expand Up @@ -12,6 +12,7 @@
#include "libknet-private.h"

#ifdef CRYPTO_DEBUG
#include <stdio.h>
#define log_printf(format, args...) fprintf(stderr, format "\n", ##args);
#else
#define log_printf(format, args...);
Expand Down Expand Up @@ -455,12 +456,14 @@ static int init_nss(struct nsscrypto_instance *instance)
*/

int nsscrypto_encrypt_and_sign (
struct nsscrypto_instance *instance,
void *model_instance,
const unsigned char *buf_in,
const ssize_t buf_in_len,
unsigned char *buf_out,
ssize_t *buf_out_len)
{
struct nsscrypto_instance *instance = model_instance;

if (cipher_to_nss[instance->crypto_cipher_type]) {
if (encrypt_nss(instance, buf_in, buf_in_len, buf_out, buf_out_len) < 0) {
return -1;
Expand All @@ -480,10 +483,13 @@ int nsscrypto_encrypt_and_sign (
return 0;
}

int nsscrypto_authenticate_and_decrypt (struct nsscrypto_instance *instance,
int nsscrypto_authenticate_and_decrypt (
void *model_instance,
unsigned char *buf,
ssize_t *buf_len)
{
struct nsscrypto_instance *instance = model_instance;

if (hash_to_nss[instance->crypto_hash_type]) {
unsigned char tmp_hash[hash_len[instance->crypto_hash_type]];

Expand Down
4 changes: 2 additions & 2 deletions libknet/nsscrypto.h
Expand Up @@ -7,12 +7,12 @@
struct nsscrypto_instance;

int nsscrypto_authenticate_and_decrypt (
struct nsscrypto_instance *instance,
void *model_instance,
unsigned char *buf,
ssize_t *buf_len);

int nsscrypto_encrypt_and_sign (
struct nsscrypto_instance *instance,
void *model_instance,
const unsigned char *buf_in,
const ssize_t buf_in_len,
unsigned char *buf_out,
Expand Down

0 comments on commit 2b75718

Please sign in to comment.