-
Notifications
You must be signed in to change notification settings - Fork 8
/
node_mceliece.cc
453 lines (384 loc) · 14.7 KB
/
node_mceliece.cc
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include <napi.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/evp.h>
#include <openssl/rand.h>
extern "C" {
#define NO_SHORT_NAMES_FOR_EXTERNALS
#include <mceliece.h>
#include <mceliece_externals.h>
#define CHECK(a, message) do { \
if ((a) != 1) { \
napi_fatal_error(__FILE__, NAPI_AUTO_LENGTH, \
message, NAPI_AUTO_LENGTH); \
} \
} while (0) \
int pqcrypto_mceliece_randombytes(unsigned char* x, size_t xlen) {
CHECK(RAND_bytes(x, xlen), "RAND_bytes failed");
return 0;
}
int pqcrypto_mceliece_SHAKE256(unsigned char *output, size_t outputByteLen,
const unsigned char *input, size_t inputByteLen) {
EVP_MD_CTX* ctx;
CHECK((ctx = EVP_MD_CTX_create()) != NULL,
"EVP_MD_CTX_create failed");
CHECK(EVP_DigestInit_ex(ctx, EVP_shake256(), NULL),
"EVP_DigestInit_ex failed");
CHECK(EVP_DigestUpdate(ctx, input, inputByteLen),
"EVP_DigestUpdate failed");
CHECK(EVP_DigestFinalXOF(ctx, output, outputByteLen),
"EVP_DigestFinalXOF failed");
EVP_MD_CTX_free(ctx);
return 0;
}
}
namespace {
typedef int (*keypair_fn_t)(unsigned char* public_key, unsigned char* private_key);
typedef int (*encrypt_fn_t)(unsigned char* ciphertext, unsigned char* key, const unsigned char* public_key);
typedef int (*decrypt_fn_t)(unsigned char* key, const unsigned char* ciphertext, const unsigned char* public_key);
typedef struct {
const char* name;
size_t public_key_size;
size_t private_key_size;
size_t key_size;
size_t ciphertext_size;
keypair_fn_t keypair;
encrypt_fn_t encrypt;
decrypt_fn_t decrypt;
} mceliece_t;
const mceliece_t kems[] = {
{
"mceliece348864",
crypto_kem_mceliece348864_ref_PUBLICKEYBYTES,
crypto_kem_mceliece348864_ref_SECRETKEYBYTES,
crypto_kem_mceliece348864_ref_BYTES,
crypto_kem_mceliece348864_ref_CIPHERTEXTBYTES,
crypto_kem_mceliece348864_ref_keypair,
crypto_kem_mceliece348864_ref_enc,
crypto_kem_mceliece348864_ref_dec
},
{
"mceliece348864f",
crypto_kem_mceliece348864f_ref_PUBLICKEYBYTES,
crypto_kem_mceliece348864f_ref_SECRETKEYBYTES,
crypto_kem_mceliece348864f_ref_BYTES,
crypto_kem_mceliece348864f_ref_CIPHERTEXTBYTES,
crypto_kem_mceliece348864f_ref_keypair,
crypto_kem_mceliece348864f_ref_enc,
crypto_kem_mceliece348864f_ref_dec
},
{
"mceliece460896",
crypto_kem_mceliece460896_ref_PUBLICKEYBYTES,
crypto_kem_mceliece460896_ref_SECRETKEYBYTES,
crypto_kem_mceliece460896_ref_BYTES,
crypto_kem_mceliece460896_ref_CIPHERTEXTBYTES,
crypto_kem_mceliece460896_ref_keypair,
crypto_kem_mceliece460896_ref_enc,
crypto_kem_mceliece460896_ref_dec
},
{
"mceliece460896f",
crypto_kem_mceliece460896f_ref_PUBLICKEYBYTES,
crypto_kem_mceliece460896f_ref_SECRETKEYBYTES,
crypto_kem_mceliece460896f_ref_BYTES,
crypto_kem_mceliece460896f_ref_CIPHERTEXTBYTES,
crypto_kem_mceliece460896f_ref_keypair,
crypto_kem_mceliece460896f_ref_enc,
crypto_kem_mceliece460896f_ref_dec
},
{
"mceliece6688128",
crypto_kem_mceliece6688128_ref_PUBLICKEYBYTES,
crypto_kem_mceliece6688128_ref_SECRETKEYBYTES,
crypto_kem_mceliece6688128_ref_BYTES,
crypto_kem_mceliece6688128_ref_CIPHERTEXTBYTES,
crypto_kem_mceliece6688128_ref_keypair,
crypto_kem_mceliece6688128_ref_enc,
crypto_kem_mceliece6688128_ref_dec
},
{
"mceliece6688128f",
crypto_kem_mceliece6688128f_ref_PUBLICKEYBYTES,
crypto_kem_mceliece6688128f_ref_SECRETKEYBYTES,
crypto_kem_mceliece6688128f_ref_BYTES,
crypto_kem_mceliece6688128f_ref_CIPHERTEXTBYTES,
crypto_kem_mceliece6688128f_ref_keypair,
crypto_kem_mceliece6688128f_ref_enc,
crypto_kem_mceliece6688128f_ref_dec
},
{
"mceliece6960119",
crypto_kem_mceliece6960119_ref_PUBLICKEYBYTES,
crypto_kem_mceliece6960119_ref_SECRETKEYBYTES,
crypto_kem_mceliece6960119_ref_BYTES,
crypto_kem_mceliece6960119_ref_CIPHERTEXTBYTES,
crypto_kem_mceliece6960119_ref_keypair,
crypto_kem_mceliece6960119_ref_enc,
crypto_kem_mceliece6960119_ref_dec
},
{
"mceliece6960119f",
crypto_kem_mceliece6960119f_ref_PUBLICKEYBYTES,
crypto_kem_mceliece6960119f_ref_SECRETKEYBYTES,
crypto_kem_mceliece6960119f_ref_BYTES,
crypto_kem_mceliece6960119f_ref_CIPHERTEXTBYTES,
crypto_kem_mceliece6960119f_ref_keypair,
crypto_kem_mceliece6960119f_ref_enc,
crypto_kem_mceliece6960119f_ref_dec
},
{
"mceliece8192128",
crypto_kem_mceliece8192128_ref_PUBLICKEYBYTES,
crypto_kem_mceliece8192128_ref_SECRETKEYBYTES,
crypto_kem_mceliece8192128_ref_BYTES,
crypto_kem_mceliece8192128_ref_CIPHERTEXTBYTES,
crypto_kem_mceliece8192128_ref_keypair,
crypto_kem_mceliece8192128_ref_enc,
crypto_kem_mceliece8192128_ref_dec
},
{
"mceliece8192128f",
crypto_kem_mceliece8192128f_ref_PUBLICKEYBYTES,
crypto_kem_mceliece8192128f_ref_SECRETKEYBYTES,
crypto_kem_mceliece8192128f_ref_BYTES,
crypto_kem_mceliece8192128f_ref_CIPHERTEXTBYTES,
crypto_kem_mceliece8192128f_ref_keypair,
crypto_kem_mceliece8192128f_ref_enc,
crypto_kem_mceliece8192128f_ref_dec
}
};
const mceliece_t* get_kem(const char* name) {
for (unsigned int i = 0; i < sizeof(kems) / sizeof(mceliece_t); i++) {
if (strcmp(kems[i].name, name) == 0)
return &kems[i];
}
return NULL;
}
template <typename T>
inline T* Malloc(size_t size) {
return reinterpret_cast<T*>(malloc(size));
}
void Free(Napi::Env env, void* p) {
free(p);
}
template <typename T>
inline T* Duplicate(const void* mem, size_t size) {
T* copy = Malloc<T>(size);
if (copy != nullptr)
memcpy(copy, mem, size);
return copy;
}
class KeygenWorker : public Napi::AsyncWorker {
public:
KeygenWorker(Napi::Function& callback, const mceliece_t* impl) : AsyncWorker(callback), impl(impl) {}
~KeygenWorker() {}
void Execute() override {
public_key = Malloc<unsigned char>(impl->public_key_size);
private_key = Malloc<unsigned char>(impl->private_key_size);
if (public_key == nullptr || private_key == nullptr) {
free(public_key);
free(private_key);
return SetError("Failed to allocate memory");
}
if (impl->keypair(public_key, private_key) != 0) {
free(public_key);
free(private_key);
return SetError("failed to generate keypair");
}
}
std::vector<napi_value> GetResult(Napi::Env env) override {
const auto public_key_buf = Napi::Buffer<unsigned char>::New(env, public_key, impl->public_key_size, Free);
const auto private_key_buf = Napi::Buffer<unsigned char>::New(env, private_key, impl->private_key_size, Free);
Napi::Object obj = Napi::Object::New(env);
obj.Set("publicKey", public_key_buf);
obj.Set("privateKey", private_key_buf);
return { env.Undefined(), obj };
}
private:
const mceliece_t* impl;
unsigned char* public_key;
unsigned char* private_key;
};
class DecryptWorker : public Napi::AsyncWorker {
public:
DecryptWorker(Napi::Function& callback, const mceliece_t* impl, const void* private_key, const void* ciphertext) : AsyncWorker(callback), impl(impl) {
this->private_key = Duplicate<unsigned char>(private_key, impl->private_key_size);
this->ciphertext = Duplicate<unsigned char>(ciphertext, impl->ciphertext_size);
}
void Execute() override {
if (private_key == nullptr || ciphertext == nullptr)
return SetError("Failed to allocate memory");
actual_key = Malloc<unsigned char>(impl->key_size);
if (actual_key == nullptr)
return SetError("Failed to allocate memory");
if (impl->decrypt(actual_key, ciphertext, private_key) != 0) {
free(actual_key);
return SetError("decryption failed");
}
}
std::vector<napi_value> GetResult(Napi::Env env) override {
const auto key = Napi::Buffer<unsigned char>::New(env, actual_key, impl->key_size, Free);
return { env.Undefined(), key };
}
~DecryptWorker() {
free(private_key);
free(ciphertext);
}
private:
const mceliece_t* impl;
unsigned char* private_key;
unsigned char* ciphertext;
unsigned char* actual_key;
};
class McEliece : public Napi::ObjectWrap<McEliece> {
public:
McEliece(const Napi::CallbackInfo& info) : Napi::ObjectWrap<McEliece>(info) {
Napi::Env env = info.Env();
if (info.Length() != 1) {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
return;
}
if (!info[0].IsString()) {
Napi::TypeError::New(env, "First argument must be a string")
.ThrowAsJavaScriptException();
return;
}
std::string name = info[0].As<Napi::String>();
this->impl = get_kem(name.c_str());
if (this->impl == nullptr) {
Napi::Error::New(env, "No such implementation")
.ThrowAsJavaScriptException();
}
}
Napi::Value Keypair(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info[0].IsFunction()) {
Napi::Function cb = info[0].As<Napi::Function>();
KeygenWorker* worker = new KeygenWorker(cb, impl);
worker->Queue();
return env.Undefined();
}
Napi::Buffer<unsigned char> public_key = Napi::Buffer<unsigned char>::New(env, impl->public_key_size);
Napi::Buffer<unsigned char> private_key = Napi::Buffer<unsigned char>::New(env, impl->private_key_size);
int r = impl->keypair(public_key.Data(), private_key.Data());
if (r != 0) {
Napi::Error::New(env, "failed to generate keypair").ThrowAsJavaScriptException();
return env.Undefined();
}
Napi::Object obj = Napi::Object::New(env);
obj.Set("publicKey", public_key);
obj.Set("privateKey", private_key);
return obj;
}
Napi::Value GenerateKey(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() != 1) {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
return env.Undefined();
}
if (!info[0].IsTypedArray()) {
Napi::TypeError::New(env, "First argument must be a TypedArray")
.ThrowAsJavaScriptException();
return env.Undefined();
}
Napi::Buffer<unsigned char> public_key = info[0].As<Napi::Buffer<unsigned char>>();
if (public_key.Length() != impl->public_key_size) {
Napi::TypeError::New(env, "Invalid public key size")
.ThrowAsJavaScriptException();
return env.Undefined();
}
Napi::Buffer<unsigned char> encrypted_key = Napi::Buffer<unsigned char>::New(env, impl->ciphertext_size);
Napi::Buffer<unsigned char> actual_key = Napi::Buffer<unsigned char>::New(env, impl->key_size);
int r = impl->encrypt(encrypted_key.Data(), actual_key.Data(), public_key.Data());
if (r != 0) {
Napi::Error::New(env, "encryption failed").ThrowAsJavaScriptException();
return env.Undefined();
}
Napi::Object obj = Napi::Object::New(env);
obj.Set("key", actual_key);
obj.Set("encryptedKey", encrypted_key);
return obj;
}
Napi::Value DecryptKey(const Napi::CallbackInfo& info) {
Napi::Env env = info.Env();
if (info.Length() != 2 && info.Length() != 3) {
Napi::TypeError::New(env, "Wrong number of arguments")
.ThrowAsJavaScriptException();
return env.Undefined();
}
if (!info[0].IsTypedArray()) {
Napi::TypeError::New(env, "First argument must be a TypedArray")
.ThrowAsJavaScriptException();
return env.Undefined();
}
if (!info[1].IsTypedArray()) {
Napi::TypeError::New(env, "Second argument must be a TypedArray")
.ThrowAsJavaScriptException();
return env.Undefined();
}
Napi::Buffer<unsigned char> private_key = info[0].As<Napi::Buffer<unsigned char>>();
if (private_key.Length() != impl->private_key_size) {
Napi::TypeError::New(env, "Invalid private key size")
.ThrowAsJavaScriptException();
return env.Undefined();
}
Napi::Buffer<unsigned char> encrypted_key = info[1].As<Napi::Buffer<unsigned char>>();
if (encrypted_key.Length() != impl->ciphertext_size) {
Napi::TypeError::New(env, "Invalid ciphertext size")
.ThrowAsJavaScriptException();
return env.Undefined();
}
if (info.Length() == 3 && info[2].IsFunction()) {
Napi::Function cb = info[2].As<Napi::Function>();
DecryptWorker* worker = new DecryptWorker(cb, impl, private_key.Data(), encrypted_key.Data());
worker->Queue();
return env.Undefined();
}
Napi::Buffer<unsigned char> actual_key = Napi::Buffer<unsigned char>::New(env, impl->key_size);
int r = impl->decrypt(actual_key.Data(), encrypted_key.Data(), private_key.Data());
if (r != 0) {
Napi::Error::New(env, "decryption failed").ThrowAsJavaScriptException();
return env.Undefined();
}
return actual_key;
}
Napi::Value GetKeySize(const Napi::CallbackInfo& info) {
return Napi::Value::From(info.Env(), impl->key_size);
}
Napi::Value GetEncryptedKeySize(const Napi::CallbackInfo& info) {
return Napi::Value::From(info.Env(), impl->ciphertext_size);
}
Napi::Value GetPublicKeySize(const Napi::CallbackInfo& info) {
return Napi::Value::From(info.Env(), impl->public_key_size);
}
Napi::Value GetPrivateKeySize(const Napi::CallbackInfo& info) {
return Napi::Value::From(info.Env(), impl->private_key_size);
}
private:
const mceliece_t* impl;
};
Napi::Object Init(Napi::Env env, Napi::Object exports) {
Napi::HandleScope scope(env);
Napi::Function func = McEliece::DefineClass(env, "McEliece", {
Napi::ObjectWrap<McEliece>::InstanceMethod("keypair", &McEliece::Keypair),
Napi::ObjectWrap<McEliece>::InstanceMethod("generateKey", &McEliece::GenerateKey),
Napi::ObjectWrap<McEliece>::InstanceMethod("decryptKey", &McEliece::DecryptKey),
Napi::ObjectWrap<McEliece>::InstanceAccessor("keySize", &McEliece::GetKeySize, nullptr),
Napi::ObjectWrap<McEliece>::InstanceAccessor("encryptedKeySize", &McEliece::GetEncryptedKeySize, nullptr),
Napi::ObjectWrap<McEliece>::InstanceAccessor("publicKeySize", &McEliece::GetPublicKeySize, nullptr),
Napi::ObjectWrap<McEliece>::InstanceAccessor("privateKeySize", &McEliece::GetPrivateKeySize, nullptr)
});
Napi::Array supported_algorithms = Napi::Array::New(env, sizeof(kems) / sizeof(*kems));
for (unsigned int i = 0; i < sizeof(kems) / sizeof(*kems); i++) {
supported_algorithms[i] = Napi::String::New(env, kems[i].name);
}
func.DefineProperty(Napi::PropertyDescriptor::Value("supportedAlgorithms", supported_algorithms));
exports.Set("McEliece", func);
return exports;
}
NODE_API_MODULE(NODE_GYP_MODULE_NAME, Init);
}