-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcrypto.c
329 lines (264 loc) · 7.55 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/*
* DESFire-Shell: Modify MIFARE DESFire Cards
*
* Copyright (C) 2015-2023 Mario Haustein
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see https://www.gnu.org/licenses/.
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <lua.h>
#include <lauxlib.h>
#include <openssl/opensslv.h>
#include <openssl/err.h>
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
#include <openssl/evp.h>
#include <openssl/params.h>
#else
#include <openssl/cmac.h>
#include <openssl/hmac.h>
#endif
#include "buffer.h"
#include "desflua.h"
#include "fn.h"
static int crypto_cmac(lua_State *l);
static int crypto_hmac(lua_State *l);
FN_ALIAS(crypto_cmac) = { "cmac", NULL };
FN_PARAM(crypto_cmac) =
{
FNPARAM("cipher", "Block Chipher Identifier", 0),
FNPARAM("input", "Input Buffer", 0),
FNPARAM("key", "Secret Key", 0),
FNPARAMEND
};
FN_RET(crypto_cmac) =
{
FNPARAM("mac", "MAC", 0),
FNPARAMEND
};
FN("crypto", crypto_cmac, "Calculate CMAC",
"Calculates the CMAC of the input buffer <input> using the specified\n" \
"symmetric cipher. The size of <key> and <mac> depends on the choosen\n" \
"cipher.\n");
static int crypto_cmac(lua_State *l)
{
int result;
const char *cipherstr;
const EVP_CIPHER *cipher;
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
OSSL_PARAM macparams[2];
#endif
unsigned int klen;
uint8_t *input, *key, *mac;
unsigned int inputlen, keylen;
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
EVP_MAC *macalg;
EVP_MAC_CTX *ctx;
#else
CMAC_CTX *ctx;
#endif
size_t maclen, buflen;
luaL_argcheck(l, lua_isstring(l, 1), 1, "cipher identifier expected");
cipherstr = lua_tostring(l, 1);
cipher = EVP_get_cipherbyname(cipherstr);
if(cipher == NULL)
return luaL_error(l, "cipher '%s' unknown", cipherstr);
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
macparams[0] = OSSL_PARAM_construct_utf8_string("cipher", (char*)cipherstr, 0);
macparams[1] = OSSL_PARAM_construct_end();
#endif
klen = EVP_CIPHER_key_length(cipher);
result = buffer_get(l, 2, &input, &inputlen);
if(result)
desflua_argerror(l, 2, "input");
result = buffer_get(l, 3, &key, &keylen);
if(result)
{
free(input);
desflua_argerror(l, 2, "key");
}
if(klen != keylen)
{
free(input);
memset(key, 0, keylen);
free(key);
lua_settop(l, 0);
lua_checkstack(l, 1);
lua_pushfstring(l, "key length %d invalid, expected %d bytes", keylen, klen);
return luaL_argerror(l, 3, lua_tostring(l, -1));
}
buflen = EVP_MAX_BLOCK_LENGTH * sizeof(uint8_t);
mac = (uint8_t*)malloc(buflen);
if(mac == NULL)
{
free(input);
memset(key, 0, keylen);
free(key);
return luaL_error(l, "internal error (%s:%d): out of memory", __FILE__, __LINE__);
}
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
macalg = EVP_MAC_fetch(NULL, "CMAC", NULL);
if(macalg == NULL)
goto fail;
ctx = EVP_MAC_CTX_new(macalg);
if(!EVP_MAC_init(ctx, key, keylen, macparams)) { goto fail; }
if(!EVP_MAC_update(ctx, input, inputlen)) { goto fail; }
if(!EVP_MAC_final(ctx, mac, &maclen, buflen)) { goto fail; }
EVP_MAC_CTX_free(ctx);
EVP_MAC_free(macalg);
#else
ctx = CMAC_CTX_new();
if(!CMAC_Init(ctx, key, keylen, cipher, NULL)) { goto fail; }
if(!CMAC_Update(ctx, input, inputlen)) { goto fail; }
if(!CMAC_Final(ctx, mac, &maclen)) { goto fail; }
CMAC_CTX_free(ctx);
#endif
lua_settop(l, 0);
buffer_push(l, mac, maclen);
free(input);
memset(key, 0, keylen);
free(key);
memset(mac, 0, EVP_MAX_BLOCK_LENGTH);
free(mac);
return lua_gettop(l);
fail:;
unsigned long err;
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
EVP_MAC_free(macalg);
#endif
lua_settop(l, 0);
free(input);
memset(key, 0, keylen);
free(key);
memset(mac, 0, EVP_MAX_BLOCK_LENGTH);
free(mac);
lua_checkstack(l, 2);
lua_pushstring(l, "Crypto error:\n");
while((err = ERR_get_error()))
{
lua_pushfstring(l, "%s\n", ERR_error_string(err, NULL));
lua_concat(l, 2);
}
return lua_error(l);
}
FN_ALIAS(crypto_hmac) = { "hmac", NULL };
FN_PARAM(crypto_hmac) =
{
FNPARAM("hash", "Hash Function", 0),
FNPARAM("input", "Input Buffer", 0),
FNPARAM("key", "Secret Key", 0),
FNPARAMEND
};
FN_RET(crypto_hmac) =
{
FNPARAM("mac", "MAC", 0),
FNPARAMEND
};
FN("crypto", crypto_hmac, "Calculate HMAC",
"Calculates the HMAC of the input buffer <input> using the specified\n" \
"message digest. The size of <key> and <mac> depends on the choosen\n" \
"digest.\n");
static int crypto_hmac(lua_State *l)
{
int result;
const char *digeststr;
const EVP_MD *digest;
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
OSSL_PARAM macparams[2];
#endif
uint8_t *input, *key, *mac;
unsigned int inputlen, keylen;
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
EVP_MAC *macalg;
EVP_MAC_CTX *ctx;
size_t maclen;
#else
HMAC_CTX *ctx;
unsigned int maclen;
#endif
size_t buflen;
luaL_argcheck(l, lua_isstring(l, 1), 1, "message digest identifier expected");
digeststr = lua_tostring(l, 1);
digest = EVP_get_digestbyname(digeststr);
if(digest == NULL)
return luaL_error(l, "message digest '%s' unknown", digeststr);
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
macparams[0] = OSSL_PARAM_construct_utf8_string("digest", (char*)digeststr, 0);
macparams[1] = OSSL_PARAM_construct_end();
#endif
result = buffer_get(l, 2, &input, &inputlen);
if(result)
desflua_argerror(l, 2, "input");
result = buffer_get(l, 3, &key, &keylen);
if(result)
{
free(input);
desflua_argerror(l, 2, "key");
}
buflen = EVP_MAX_MD_SIZE * sizeof(uint8_t);
mac = (uint8_t*)malloc(buflen);
if(mac == NULL)
{
free(input);
memset(key, 0, keylen);
free(key);
return luaL_error(l, "internal error (%s:%d): out of memory", __FILE__, __LINE__);
}
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
macalg = EVP_MAC_fetch(NULL, "HMAC", NULL);
if(macalg == NULL)
goto fail;
ctx = EVP_MAC_CTX_new(macalg);
if(!EVP_MAC_init(ctx, key, keylen, macparams)) { goto fail; }
if(!EVP_MAC_update(ctx, input, inputlen)) { goto fail; }
if(!EVP_MAC_final(ctx, mac, &maclen, buflen)) { goto fail; }
EVP_MAC_CTX_free(ctx);
EVP_MAC_free(macalg);
#else
ctx = HMAC_CTX_new();
if(!HMAC_Init_ex(ctx, key, keylen, digest, NULL)) { goto fail; }
if(!HMAC_Update(ctx, input, inputlen)) { goto fail; }
if(!HMAC_Final(ctx, mac, &maclen)) { goto fail; }
HMAC_CTX_free(ctx);
#endif
lua_settop(l, 0);
buffer_push(l, mac, maclen);
free(input);
memset(key, 0, keylen);
free(key);
memset(mac, 0, EVP_MAX_MD_SIZE);
free(mac);
return lua_gettop(l);
fail:;
unsigned long err;
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
EVP_MAC_free(macalg);
#endif
lua_settop(l, 0);
free(input);
memset(key, 0, keylen);
free(key);
memset(mac, 0, EVP_MAX_MD_SIZE);
free(mac);
lua_checkstack(l, 2);
lua_pushstring(l, "");
while((err = ERR_get_error()))
{
lua_pushfstring(l, "%s\n", ERR_error_string(err, NULL));
lua_concat(l, 2);
}
return luaL_error(l, "Crypto error:\n%s", lua_tostring(l, 1));
}