-
Notifications
You must be signed in to change notification settings - Fork 4
/
wolfcrypt.c
203 lines (163 loc) · 4.58 KB
/
wolfcrypt.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "wolfcrypt.h"
#ifdef HAS_WOLFSSL_OPTIONS
# include <wolfssl/options.h>
#endif
#include <wolfssl/wolfcrypt/aes.h>
#include <wolfssl/wolfcrypt/chacha20_poly1305.h>
#include <wolfssl/wolfcrypt/random.h>
typedef struct WolfCryptParam {
byte key[KEY_SIZE];
byte iv[IV_SIZE];
byte tag[TAG_SIZE];
WC_RNG rng;
Aes aes;
const char *current_cipher;
} WolfCryptParam;
const char *wolfcrypt_name() {
return "wolfCrypt";
}
const char **wolfcrypt_ciphers() {
static const char *names[] = {
CIPHER_AES_256_GCM,
CIPHER_CHACHA20_POLY1305,
NULL
};
return names;
}
bool wolfcrypt_init(void **param) {
if (!param) {
return false;
}
int ret = wolfCrypt_Init();
if (ret != 0) {
printf("wolfcrypt_init(): wolfCrypt_Init() failed with error %d\n", ret);
return false;
}
WolfCryptParam *wcp = malloc(sizeof(WolfCryptParam));
ret = wc_InitRng(&wcp->rng);
if (ret != 0) {
printf("wolfcrypt_init(): wc_InitRng() failed with error %d\n", ret);
free(wcp);
return false;
}
*param = wcp;
return true;
}
bool wolfcrypt_free(void *param) {
if (!param) {
return false;
}
bool ok = true;
int ret = wc_FreeRng(&((WolfCryptParam *)param)->rng);
if (ret != 0) {
printf("wolfcrypt_cleanup(): wc_FreeRng() failed with error %d\n", ret);
ok = false;
}
ret = wolfCrypt_Cleanup();
if (ret != 0) {
printf("wolfcrypt_cleanup(): wolfCrypt_Cleanup() failed with error %d!\n", ret);
ok = false;
}
free(param);
return ok;
}
bool wolfcrypt_random(void *param, const size_t size, void *dst) {
if (!param || !dst) {
return false;
}
const int ret = wc_RNG_GenerateBlock(&((WolfCryptParam *)param)->rng, dst, size);
if (ret != 0) {
printf("wolfcrypt_random(): wc_RNG_GenerateBlock() failed with error %d\n", ret);
return false;
}
return true;
}
bool wolfcrypt_set_cipher(void *param, const char *cipher) {
if (!param || !cipher) {
return false;
}
WolfCryptParam *wcp = param;
if (cipher == CIPHER_AES_256_GCM) {
const int ret = wc_AesGcmSetKey(&wcp->aes, wcp->key, sizeof(wcp->key));
if (ret != 0) {
printf("wolfcrypt_set_cipher(): wc_AesGcmSetKey() failed with error %d\n", ret);
return false;
}
wcp->current_cipher = CIPHER_AES_256_GCM;
} else if (cipher == CIPHER_CHACHA20_POLY1305) {
wcp->current_cipher = CIPHER_CHACHA20_POLY1305;
} else {
printf("wolfcrypt_set_cipher(): \"%s\" is not a recognized cipher!\n", cipher);
return false;
}
if (!wolfcrypt_random(param, sizeof(wcp->key), wcp->key)) {
printf("wolfcrypt_set_cipher(): wolfcrypt_random() failed to generate the key!\n");
return false;
}
if (!wolfcrypt_random(param, sizeof(wcp->iv), wcp->iv)) {
printf("wolfcrypt_set_cipher(): wolfcrypt_random() failed to generate the IV!\n");
return false;
}
return true;
}
size_t wolfcrypt_buffer_size(size_t size) {
return size;
}
size_t wolfcrypt_encrypt(void *param, const size_t size, void *dst, const void *src) {
if (!param || !dst || !src) {
return 0;
}
WolfCryptParam *wcp = param;
if (wcp->current_cipher == CIPHER_AES_256_GCM) {
const int ret = wc_AesGcmEncrypt(&wcp->aes, dst, src, size, wcp->iv, sizeof(wcp->iv), wcp->tag, sizeof(wcp->tag), NULL, 0);
if (ret != 0) {
printf("wolfcrypt_encrypt(): wc_AesGcmEncrypt() failed with error %d\n", ret);
return 0;
}
} else if (wcp->current_cipher == CIPHER_CHACHA20_POLY1305) {
const int ret = wc_ChaCha20Poly1305_Encrypt(wcp->key, wcp->iv, NULL, 0, src, size, dst, wcp->tag);
if (ret != 0) {
printf("wolfcrypt_encrypt(): wc_ChaCha20Poly1305_Encrypt() failed with error %d\n", ret);
return 0;
}
} else {
return 0;
}
return size;
}
size_t wolfcrypt_decrypt(void *param, const size_t size, void *dst, const void *src) {
if (!param || !dst || !src) {
return 0;
}
WolfCryptParam *wcp = param;
if (wcp->current_cipher == CIPHER_AES_256_GCM) {
const int ret = wc_AesGcmDecrypt(&wcp->aes, dst, src, size, wcp->iv, sizeof(wcp->iv), wcp->tag, sizeof(wcp->tag), NULL, 0);
if (ret != 0) {
printf("wolfcrypt_decrypt(): wc_AesGcmDecrypt() failed with error %d\n", ret);
return 0;
}
} else if (wcp->current_cipher == CIPHER_CHACHA20_POLY1305) {
const int ret = wc_ChaCha20Poly1305_Decrypt(wcp->key, wcp->iv, NULL, 0, src, size, wcp->tag, dst);
if (ret != 0) {
printf("wolfcrypt_decrypt(): wc_ChaCha20Poly1305_Decrypt() failed with error %d\n", ret);
return 0;
}
} else {
return 0;
}
return size;
}
const Crypto *wolfcrypt_get() {
static const Crypto crypto = {
wolfcrypt_name,
wolfcrypt_ciphers,
wolfcrypt_init,
wolfcrypt_free,
wolfcrypt_random,
wolfcrypt_set_cipher,
wolfcrypt_buffer_size,
wolfcrypt_encrypt,
wolfcrypt_decrypt
};
return &crypto;
}