-
Notifications
You must be signed in to change notification settings - Fork 30
/
crypto.c
100 lines (82 loc) · 2.22 KB
/
crypto.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
96
97
98
99
100
#include "config.h"
#include <stdlib.h>
#include <string.h>
#include "crypto.h"
#include "nsscrypto.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
*/
int crypto_encrypt_and_sign (
struct crypto_instance *instance,
const unsigned char *buf_in,
const ssize_t buf_in_len,
unsigned char *buf_out,
ssize_t *buf_out_len)
{
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 modules_cmds[instance->model].decrypt(instance->model_instance, buf, buf_len);
}
int crypto_init(
knet_handle_t knet_h,
struct knet_handle_crypto_cfg *knet_handle_crypto_cfg)
{
log_printf("Initizializing crypto module [%s/%s/%s]",
knet_handle_crypto_cfg->crypto_model,
knet_handle_crypto_cfg->crypto_cipher_type,
knet_handle_crypto_cfg->crypto_hash_type);
knet_h->crypto_instance = malloc(sizeof(struct crypto_instance));
if (!knet_h->crypto_instance) {
log_printf("no memory from crypto");
return -1;
}
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 0;
}
void crypto_fini(
knet_handle_t knet_h)
{
if (knet_h->crypto_instance) {
modules_cmds[knet_h->crypto_instance->model].fini(knet_h);
free(knet_h->crypto_instance);
knet_h->crypto_instance = NULL;
}
return;
}