forked from NicolasFlamel1/Ed25519-NPM-Package
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·190 lines (132 loc) · 4.38 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
// Header files
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <vector>
// Check if using Emscripten
#ifdef __EMSCRIPTEN__
// Header files
#include <emscripten.h>
#include "./crypto_sign.h"
// Otherwise
#else
// Header files
extern "C" {
#include "./crypto_sign.h"
}
#endif
using namespace std;
// Definitions
// Check if using Emscripten
#ifdef __EMSCRIPTEN__
// Export
#define EXPORT extern "C"
// Otherwise
#else
// Export
#define EXPORT static
// Emscripten keepalive
#define EMSCRIPTEN_KEEPALIVE
#endif
// Function prototypes
// Public key size
EXPORT size_t EMSCRIPTEN_KEEPALIVE publicKeySize();
// Public key from secret key
EXPORT bool EMSCRIPTEN_KEEPALIVE publicKeyFromSecretKey(uint8_t *publicKey, const uint8_t *secretKey, size_t secretKeySize);
// Signature size
EXPORT size_t EMSCRIPTEN_KEEPALIVE signatureSize();
// Sign
EXPORT bool EMSCRIPTEN_KEEPALIVE sign(uint8_t *signature, const uint8_t *message, size_t messageSize, const uint8_t *secretKey, size_t secretKeySize);
// Verify
EXPORT bool EMSCRIPTEN_KEEPALIVE verify(const uint8_t *message, size_t messageSize, const uint8_t *signature, size_t signatureSize, const uint8_t *publicKey, size_t publicKeySize);
// Supporting function implementation
// Public key size
size_t publicKeySize() {
// Return public key size
return crypto_sign_PUBLICKEYBYTES;
}
// Public key from secret key
bool publicKeyFromSecretKey(uint8_t *publicKey, const uint8_t *secretKey, size_t secretKeySize) {
// Check if secret key is invalid
if(secretKeySize != crypto_sign_SECRETKEYBYTES - crypto_sign_PUBLICKEYBYTES) {
// Return false
return false;
}
// Check if getting the public key failed
uint8_t fullSecretKey[crypto_sign_SECRETKEYBYTES];
memcpy(fullSecretKey, secretKey, crypto_sign_SECRETKEYBYTES - crypto_sign_PUBLICKEYBYTES);
if(crypto_sign_keypair(publicKey, fullSecretKey)) {
// Clear memory
explicit_bzero(fullSecretKey, sizeof(fullSecretKey));
// Return false
return false;
}
// Clear memory
explicit_bzero(fullSecretKey, sizeof(fullSecretKey));
// Return true
return true;
}
// Signature size
size_t signatureSize() {
// Return signature size
return crypto_sign_BYTES;
}
// Sign
bool sign(uint8_t *signature, const uint8_t *message, size_t messageSize, const uint8_t *secretKey, size_t secretKeySize) {
// Check if secret key is invalid
if(secretKeySize != crypto_sign_SECRETKEYBYTES - crypto_sign_PUBLICKEYBYTES) {
// Return false
return false;
}
// Check if getting the full secret key failed
uint8_t publicKey[crypto_sign_PUBLICKEYBYTES];
uint8_t fullSecretKey[crypto_sign_SECRETKEYBYTES];
memcpy(fullSecretKey, secretKey, crypto_sign_SECRETKEYBYTES - crypto_sign_PUBLICKEYBYTES);
if(crypto_sign_keypair(publicKey, fullSecretKey)) {
// Clear memory
explicit_bzero(fullSecretKey, sizeof(fullSecretKey));
// Return false
return false;
}
// Check if signing the message failed
vector<uint8_t> fullSignature(crypto_sign_BYTES + messageSize);
long long unsigned int signatureLength;
if(crypto_sign(fullSignature.data(), &signatureLength, message, messageSize, fullSecretKey)) {
// Clear memory
explicit_bzero(fullSecretKey, sizeof(fullSecretKey));
// Return false
return false;
}
// Copy full signature to the signature
memcpy(signature, fullSignature.data(), crypto_sign_BYTES);
// Clear memory
explicit_bzero(fullSecretKey, sizeof(fullSecretKey));
// Return true
return true;
}
// Verify
bool verify(const uint8_t *message, size_t messageSize, const uint8_t *signature, size_t signatureSize, const uint8_t *publicKey, size_t publicKeySize) {
// Check if signature is invalid
if(signatureSize != crypto_sign_BYTES) {
// Return false
return false;
}
// Check if public key is invalid
if(publicKeySize != crypto_sign_PUBLICKEYBYTES) {
// Return false
return false;
}
// Get the full signature
vector<uint8_t> fullSignature;
fullSignature.insert(fullSignature.cend(), signature, signature + crypto_sign_BYTES);
fullSignature.insert(fullSignature.cend(), message, message + messageSize);
// Check if verifying the message failed
vector<uint8_t> verifiedMessage(fullSignature.size());
long long unsigned int verifiedMessageLength;
if(crypto_sign_open(verifiedMessage.data(), &verifiedMessageLength, fullSignature.data(), fullSignature.size(), publicKey)) {
// Return false
return false;
}
// Return true
return true;
}