forked from NicolasFlamel1/Ed25519-Node.js-Addon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
286 lines (205 loc) · 7.62 KB
/
main.cpp
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
// Header files
#include <cstring>
#include <node_api.h>
#include <tuple>
#include <vector>
using namespace std;
// Ed25519 namespace
namespace Ed25519 {
// Header files
#include "./Ed25519-NPM-Package-master/main.cpp"
}
// Constants
// Operation failed
static napi_value OPERATION_FAILED;
// Function prototypes
// Public key from secret key
static napi_value publicKeyFromSecretKey(napi_env environment, napi_callback_info arguments);
// Sign
static napi_value sign(napi_env environment, napi_callback_info arguments);
// Verify
static napi_value verify(napi_env environment, napi_callback_info arguments);
// Uint8 array to buffer
static tuple<uint8_t *, size_t, bool> uint8ArrayToBuffer(napi_env environment, napi_value uint8Array);
// Buffer to uint8 array
static napi_value bufferToUint8Array(napi_env environment, uint8_t *data, size_t size);
// C bool to bool
static napi_value cBoolToBool(napi_env environment, bool value);
// Main function
// Initialize module
NAPI_MODULE_INIT() {
// Check if initializing operation failed failed
if(napi_get_null(env, &OPERATION_FAILED) != napi_ok) {
// Return nothing
return nullptr;
}
// Check if creating public key from secret key property failed
napi_value temp;
if(napi_create_function(env, nullptr, 0, publicKeyFromSecretKey, nullptr, &temp) != napi_ok || napi_set_named_property(env, exports, "publicKeyFromSecretKey", temp) != napi_ok) {
// Return nothing
return nullptr;
}
// Check if creating sign property failed
if(napi_create_function(env, nullptr, 0, sign, nullptr, &temp) != napi_ok || napi_set_named_property(env, exports, "sign", temp) != napi_ok) {
// Return nothing
return nullptr;
}
// Check if creating verify property failed
if(napi_create_function(env, nullptr, 0, verify, nullptr, &temp) != napi_ok || napi_set_named_property(env, exports, "verify", temp) != napi_ok) {
// Return nothing
return nullptr;
}
// Check if creating operation failed property failed
if(napi_set_named_property(env, exports, "OPERATION_FAILED", OPERATION_FAILED) != napi_ok) {
// Return nothing
return nullptr;
}
// Return exports
return exports;
}
// Supporting function implementation
// Public key from secret key
napi_value publicKeyFromSecretKey(napi_env environment, napi_callback_info arguments) {
// Check if not enough arguments were provided
size_t argc = 1;
vector<napi_value> argv(argc);
if(napi_get_cb_info(environment, arguments, &argc, argv.data(), nullptr, nullptr) != napi_ok || argc != argv.size()) {
// Return operation failed
return OPERATION_FAILED;
}
// Check if getting secret key from arguments failed
const tuple<uint8_t *, size_t, bool> secretKey = uint8ArrayToBuffer(environment, argv[0]);
if(!get<2>(secretKey)) {
// Return operation failed
return OPERATION_FAILED;
}
// Check if getting public key from secret key failed
vector<uint8_t> publicKey(Ed25519::publicKeySize());
if(!Ed25519::publicKeyFromSecretKey(publicKey.data(), get<0>(secretKey), get<1>(secretKey))) {
// Return operation failed
return OPERATION_FAILED;
}
// Return public key as a uint8 array
return bufferToUint8Array(environment, publicKey.data(), publicKey.size());
}
// Sign
napi_value sign(napi_env environment, napi_callback_info arguments) {
// Check if not enough arguments were provided
size_t argc = 2;
vector<napi_value> argv(argc);
if(napi_get_cb_info(environment, arguments, &argc, argv.data(), nullptr, nullptr) != napi_ok || argc != argv.size()) {
// Return operation failed
return OPERATION_FAILED;
}
// Check if getting message from arguments failed
const tuple<uint8_t *, size_t, bool> message = uint8ArrayToBuffer(environment, argv[0]);
if(!get<2>(message)) {
// Return operation failed
return OPERATION_FAILED;
}
// Check if getting secret key from arguments failed
const tuple<uint8_t *, size_t, bool> secretKey = uint8ArrayToBuffer(environment, argv[1]);
if(!get<2>(secretKey)) {
// Return operation failed
return OPERATION_FAILED;
}
// Check if signing message failed
vector<uint8_t> signature(Ed25519::signatureSize());
if(!Ed25519::sign(signature.data(), get<0>(message), get<1>(message), get<0>(secretKey), get<1>(secretKey))) {
// Return operation failed
return OPERATION_FAILED;
}
// Return signature as a uint8 array
return bufferToUint8Array(environment, signature.data(), signature.size());
}
// Verify
napi_value verify(napi_env environment, napi_callback_info arguments) {
// Check if not enough arguments were provided
size_t argc = 3;
vector<napi_value> argv(argc);
if(napi_get_cb_info(environment, arguments, &argc, argv.data(), nullptr, nullptr) != napi_ok || argc != argv.size()) {
// Return false as a bool
return cBoolToBool(environment, false);
}
// Check if getting message from arguments failed
const tuple<uint8_t *, size_t, bool> message = uint8ArrayToBuffer(environment, argv[0]);
if(!get<2>(message)) {
// Return false as a bool
return cBoolToBool(environment, false);
}
// Check if getting signature from arguments failed
const tuple<uint8_t *, size_t, bool> signature = uint8ArrayToBuffer(environment, argv[1]);
if(!get<2>(signature)) {
// Return false as a bool
return cBoolToBool(environment, false);
}
// Check if getting public key from arguments failed
const tuple<uint8_t *, size_t, bool> publicKey = uint8ArrayToBuffer(environment, argv[2]);
if(!get<2>(publicKey)) {
// Return false as a bool
return cBoolToBool(environment, false);
}
// Check if signature failed to verify
if(!Ed25519::verify(get<0>(message), get<1>(message), get<0>(signature), get<1>(signature), get<0>(publicKey), get<1>(publicKey))) {
// Return false as a bool
return cBoolToBool(environment, false);
}
// Return true as a bool
return cBoolToBool(environment, true);
}
// Uint8 array to buffer
tuple<uint8_t *, size_t, bool> uint8ArrayToBuffer(napi_env environment, napi_value uint8Array) {
// Check if uint8 array isn't a typed array
bool isTypedArray;
if(napi_is_typedarray(environment, uint8Array, &isTypedArray) != napi_ok || !isTypedArray) {
// Return failure
return {nullptr, 0, false};
}
// Check if uint8 array isn't a uint8 array
napi_typedarray_type type;
size_t size;
uint8_t *data;
if(napi_get_typedarray_info(environment, uint8Array, &type, &size, reinterpret_cast<void **>(&data), nullptr, nullptr) != napi_ok || type != napi_uint8_array) {
// Return failure
return {nullptr, 0, false};
}
// Return data and size
return {data, size, true};
}
// Buffer to uint8 array
napi_value bufferToUint8Array(napi_env environment, uint8_t *data, size_t size) {
// Check if creating array buffer failed
uint8_t *arrayBufferData;
napi_value arrayBuffer;
if(napi_create_arraybuffer(environment, size, reinterpret_cast<void **>(&arrayBufferData), &arrayBuffer) != napi_ok) {
// Clear data
memset(data, 0, size);
// Return operation failed
return OPERATION_FAILED;
}
// Copy data to array buffer
memcpy(arrayBufferData, data, size);
// Clear data
memset(data, 0, size);
// Check if creating uint8 array from array buffer failed
napi_value uint8Array;
if(napi_create_typedarray(environment, napi_uint8_array, size, arrayBuffer, 0, &uint8Array) != napi_ok) {
// Clear array buffer
memset(arrayBufferData, 0, size);
// Return operation failed
return OPERATION_FAILED;
}
// Return uint8 array
return uint8Array;
}
// C bool to bool
napi_value cBoolToBool(napi_env environment, bool value) {
// Check if creating boolean from value failed
napi_value result;
if(napi_get_boolean(environment, value, &result) != napi_ok) {
// Return operation failed
return OPERATION_FAILED;
}
// Return result
return result;
}