forked from Novators/libsqrl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSqrlCrypt.cpp
More file actions
314 lines (272 loc) · 9.7 KB
/
SqrlCrypt.cpp
File metadata and controls
314 lines (272 loc) · 9.7 KB
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
/** \file SqrlCrypt.cpp
*
* \author Adam Comley
*
* This file is part of libsqrl. It is released under the MIT license.
* For more details, see the LICENSE file included with this package.
**/
#include "sqrl_internal.h"
#include "SqrlCrypt.h"
#include "SqrlEnScrypt.h"
#include "SqrlEntropy.h"
#include "aes.h"
#include "gcm.h"
#ifdef ARDUINO
#include <Crypto.h>
#include <SHA256.h>
#include <Ed25519.h>
#include <Curve25519.h>
#endif
namespace libsqrl
{
#define ENSCRYPT_R 256
#define ENSCRYPT_P 1
#define SODIUM_SCRYPT crypto_pwhash_scryptsalsa208sha256_ll
SqrlCrypt::SqrlCrypt() :
plain_text(NULL),
cipher_text(NULL),
text_len(0),
add(NULL),
add_len(0),
tag(NULL),
salt(NULL),
iv(NULL),
count(0),
nFactor(9),
flags(SQRL_ENCRYPT | SQRL_ITERATIONS),
key(NULL),
enscrypt(NULL)
{
}
SqrlCrypt::~SqrlCrypt() {
if( this->enscrypt ) {
delete this->enscrypt;
}
}
int SqrlCrypt::enHash( uint64_t *out, const uint64_t *in ) {
uint64_t trans[4];
uint64_t tmp[4];
memset( out, 0, 32 );
memcpy( tmp, in, 32 );
int i;
#ifdef ARDUINO
SHA256 sha = SHA256();
for( i = 0; i < 16; i++ ) {
sha.update( tmp, 32 );
sha.finalize( trans, 32 );
sha.reset();
out[0] ^= trans[0];
out[1] ^= trans[1];
out[2] ^= trans[2];
out[3] ^= trans[3];
memcpy( tmp, trans, 32 );
}
#else
sqrl_mlock( trans, 32 );
sqrl_mlock( tmp, 32 );
for( i = 0; i < 16; i++ ) {
crypto_hash_sha256( (unsigned char*)trans, (unsigned char*)tmp, 32 );
out[0] ^= trans[0];
out[1] ^= trans[1];
out[2] ^= trans[2];
out[3] ^= trans[3];
memcpy( tmp, trans, 32 );
}
sqrl_munlock( trans, 32 );
sqrl_munlock( tmp, 32 );
#endif
return 0;
}
int SqrlCrypt::encrypt( uint8_t *cipherText, const uint8_t *plainText, size_t textLength,
const uint8_t *key, const uint8_t *iv, const uint8_t *add, size_t add_len, uint8_t *tag ) {
gcm_context ctx;
uint8_t miv[12] = {0};
size_t iv_len = 12;
size_t tag_len = 0;
int retVal;
if( iv ) memcpy( miv, iv, iv_len );
if( tag ) tag_len = 16;
if( !add ) add_len = 0;
gcm_setkey( &ctx, (unsigned char*)key, 32 );
retVal = gcm_crypt_and_tag(
&ctx, ENCRYPT,
miv, iv_len,
add, add_len,
plainText, cipherText, textLength,
tag, tag_len );
gcm_zero_ctx( &ctx );
return retVal;
}
int SqrlCrypt::decrypt( uint8_t *plainText, const uint8_t *cipherText, size_t textLength,
const uint8_t *key, const uint8_t *iv, const uint8_t *add, size_t add_len, const uint8_t *tag ) {
gcm_context ctx;
size_t iv_len = 0;
size_t tag_len = 0;
int retVal;
if( iv ) iv_len = 12;
if( tag ) tag_len = 16;
if( !add ) add_len = 0;
gcm_setkey( &ctx, (unsigned char*)key, 32 );
retVal = gcm_auth_decrypt(
&ctx, iv, iv_len,
add, add_len,
cipherText, plainText, textLength,
tag, tag_len );
gcm_zero_ctx( &ctx );
return retVal;
}
bool SqrlCrypt::genKey_init( SqrlAction *action, const SqrlString *password ) {
if( !action || !password || this->enscrypt ) {
return false;
}
if( !this->key || this->count == 0 ) return false;
size_t salt_len = this->salt ? 16 : 0;
SqrlString salt( this->salt, salt_len );
if( (this->flags & SQRL_MILLIS) == SQRL_MILLIS ) {
this->enscrypt = new SqrlEnScrypt( action, password, &salt, this->count, false, this->nFactor );
} else {
this->enscrypt = new SqrlEnScrypt( action, password, &salt, this->count, true, this->nFactor );
}
this->lastProgress = 0;
action->onProgress( 0 );
return true;
}
bool SqrlCrypt::genKey_step( SqrlAction *action ) {
if( !action || !this->enscrypt ) return false;
int p;
if( !this->enscrypt->isFinished() ) {
this->enscrypt->update();
p = this->enscrypt->getCurrentProgress();
if( p > this->lastProgress ) {
this->lastProgress = p;
action->onProgress( p );
}
return true;
}
return false;
}
bool SqrlCrypt::genKey_finalize( SqrlAction *action ) {
if( !action || !this->enscrypt ) return false;
if( !this->enscrypt->isFinished() || !this->enscrypt->isSuccessful() ) return false;
SqrlString *key = this->enscrypt->getResult();
memcpy( this->key, key->cdata(), SQRL_KEY_SIZE );
this->count = this->enscrypt->getIterations();
this->flags &= ~SQRL_MILLIS;
this->flags |= SQRL_ITERATIONS;
delete this->enscrypt;
this->enscrypt = NULL;
return true;
}
bool SqrlCrypt::genKey( SqrlAction *action, const SqrlString *password ) {
if( this->genKey_init( action, password ) ) {
while( this->genKey_step( action ) );
return this->genKey_finalize( action );
}
return false;
}
bool SqrlCrypt::doCrypt() {
if( !this->cipher_text || !this->plain_text || this->text_len == 0 ||
!this->key || !this->tag ) return false;
if( this->flags & SQRL_ENCRYPT ) {
SqrlCrypt::encrypt( this->cipher_text, this->plain_text, this->text_len,
key, this->iv, this->add, this->add_len, this->tag );
} else {
if( SqrlCrypt::decrypt( this->plain_text, this->cipher_text, this->text_len,
key, this->iv, this->add, this->add_len, this->tag ) ) {
return false;
}
}
return true;
}
void SqrlCrypt::generateIdentityLockKey( uint8_t ilk[SQRL_KEY_SIZE], const uint8_t iuk[SQRL_KEY_SIZE] ) {
uint8_t tmp[SQRL_KEY_SIZE];
sqrl_mlock( tmp, SQRL_KEY_SIZE );
memcpy( tmp, iuk, SQRL_KEY_SIZE );
SqrlCrypt::generateCurvePrivateKey( tmp );
SqrlCrypt::generateCurvePublicKey( ilk, tmp );
sqrl_munlock( tmp, SQRL_KEY_SIZE );
}
void SqrlCrypt::generateLocalKey( uint8_t local[SQRL_KEY_SIZE], const uint8_t mk[SQRL_KEY_SIZE] ) {
SqrlCrypt::enHash( (uint64_t*)local, (uint64_t*)mk );
}
void SqrlCrypt::generateMasterKey( uint8_t mk[SQRL_KEY_SIZE], const uint8_t iuk[SQRL_KEY_SIZE] ) {
SqrlCrypt::enHash( (uint64_t*)mk, (uint64_t*)iuk );
}
void SqrlCrypt::generateRandomLockKey( uint8_t rlk[SQRL_KEY_SIZE] ) {
SqrlEntropy::bytes( rlk, SQRL_KEY_SIZE );
SqrlCrypt::generateCurvePrivateKey( rlk );
}
void SqrlCrypt::generateServerUnlockKey( uint8_t suk[SQRL_KEY_SIZE], const uint8_t rlk[SQRL_KEY_SIZE] ) {
SqrlCrypt::generateCurvePublicKey( suk, rlk );
}
void SqrlCrypt::generateVerifyUnlockKey( uint8_t vuk[SQRL_KEY_SIZE], const uint8_t ilk[SQRL_KEY_SIZE], const uint8_t rlk[SQRL_KEY_SIZE] ) {
uint8_t tmp[SQRL_KEY_SIZE];
sqrl_mlock( tmp, SQRL_KEY_SIZE );
SqrlCrypt::generateSharedSecret( tmp, ilk, rlk );
SqrlCrypt::generatePublicKey( vuk, tmp );
sqrl_munlock( tmp, SQRL_KEY_SIZE );
}
void SqrlCrypt::generateUnlockRequestSigningKey( uint8_t ursk[SQRL_KEY_SIZE], const uint8_t suk[SQRL_KEY_SIZE], const uint8_t iuk[SQRL_KEY_SIZE] ) {
uint8_t tmp[SQRL_KEY_SIZE];
sqrl_mlock( tmp, SQRL_KEY_SIZE );
memcpy( tmp, iuk, SQRL_KEY_SIZE );
SqrlCrypt::generateCurvePrivateKey( tmp );
SqrlCrypt::generateSharedSecret( ursk, suk, tmp );
sqrl_munlock( tmp, SQRL_KEY_SIZE );
}
void SqrlCrypt::generatePublicKey( uint8_t *puk, const uint8_t *prk ) {
#ifdef ARDUINO
Ed25519::derivePublicKey( puk, prk );
#else
uint8_t sk[crypto_sign_SECRETKEYBYTES];
sqrl_mlock( sk, crypto_sign_SECRETKEYBYTES );
crypto_sign_seed_keypair( puk, sk, prk );
sqrl_munlock( sk, crypto_sign_SECRETKEYBYTES );
#endif
}
void SqrlCrypt::sign( const SqrlString *msg, const uint8_t sk[32], const uint8_t pk[32], uint8_t sig[64] ) {
#ifdef ARDUINO
Ed25519::sign( sig, sk, pk, msg->cstring(), msg->length() );
#else
uint8_t secret[crypto_sign_SECRETKEYBYTES];
sqrl_mlock( secret, crypto_sign_SECRETKEYBYTES );
memcpy( secret, sk, 32 );
memcpy( secret + 32, pk, 32 );
crypto_sign_detached(
sig, NULL,
(const unsigned char*)msg->cdata(), msg->length(),
secret );
sqrl_munlock( secret, crypto_sign_SECRETKEYBYTES );
#endif
}
bool SqrlCrypt::verifySignature( const SqrlString *msg, const uint8_t *sig, const uint8_t *pub ) {
#ifdef ARDUINO
return Ed25519::verify( sig, pub, msg->cstring(), msg->length() );
#else
if( crypto_sign_verify_detached( sig, (const unsigned char *)msg->cdata(), msg->length(), pub ) == 0 ) {
return true;
}
return false;
#endif
}
void SqrlCrypt::generateCurvePrivateKey( uint8_t *key ) {
key[0] &= 248;
key[31] &= 127;
key[31] |= 64;
}
void SqrlCrypt::generateCurvePublicKey( uint8_t *puk, const uint8_t *prk ) {
#ifdef ARDUINO
Curve25519::eval( puk, prk, NULL );
#else
crypto_scalarmult_base( puk, prk );
#endif
}
int SqrlCrypt::generateSharedSecret( uint8_t *shared, const uint8_t *puk, const uint8_t *prk ) {
#ifdef ARDUINO
Curve25519::eval( shared, puk, prk );
return 0;
#else
return crypto_scalarmult( shared, prk, puk );
#endif
}
}