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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
/*
* wee-crypto.c - cryptographic functions
*
* Copyright (C) 2018-2023 Sébastien Helleu <flashcode@flashtux.org>
*
* This file is part of WeeChat, the extensible chat client.
*
* WeeChat 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.
*
* WeeChat 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 WeeChat. If not, see <https://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <sys/stat.h>
#include <time.h>
#include <math.h>
#include <gcrypt.h>
#include "weechat.h"
#include "wee-crypto.h"
#include "wee-config-file.h"
#include "wee-hashtable.h"
#include "wee-string.h"
#include "../plugins/plugin.h"
char *weecrypto_hash_algo_string[] = {
"crc32",
"md5",
"sha1",
"sha224", "sha256", "sha384", "sha512",
#if GCRYPT_VERSION_NUMBER >= 0x010700
"sha3-224", "sha3-256", "sha3-384", "sha3-512",
#endif
NULL,
};
int weecrypto_hash_algo[] = {
GCRY_MD_CRC32,
GCRY_MD_MD5,
GCRY_MD_SHA1,
GCRY_MD_SHA224, GCRY_MD_SHA256, GCRY_MD_SHA384, GCRY_MD_SHA512,
#if GCRYPT_VERSION_NUMBER >= 0x010700
GCRY_MD_SHA3_224, GCRY_MD_SHA3_256, GCRY_MD_SHA3_384, GCRY_MD_SHA3_512,
#endif
};
/*
* Returns the hash algorithm with the name, or GCRY_MD_NONE if not found.
*/
int
weecrypto_get_hash_algo (const char *hash_algo)
{
int i;
if (!hash_algo)
return GCRY_MD_NONE;
for (i = 0; weecrypto_hash_algo_string[i]; i++)
{
if (strcmp (weecrypto_hash_algo_string[i], hash_algo) == 0)
return weecrypto_hash_algo[i];
}
return GCRY_MD_NONE;
}
/*
* Computes hash of data using the given hash algorithm.
*
* The hash size depends on the algorithm, common ones are:
*
* GCRY_MD_CRC32 32 bits == 4 bytes
* GCRY_MD_MD5 128 bits == 16 bytes
* GCRY_MD_SHA1 160 bits == 20 bytes
* GCRY_MD_SHA224 224 bits == 28 bytes
* GCRY_MD_SHA256 256 bits == 32 bytes
* GCRY_MD_SHA384 384 bits == 48 bytes
* GCRY_MD_SHA512 512 bits == 64 bytes
* GCRY_MD_SHA3_224 224 bits == 28 bytes (libgcrypt ≥ 1.7.0)
* GCRY_MD_SHA3_256 256 bits == 32 bytes (libgcrypt ≥ 1.7.0)
* GCRY_MD_SHA3_384 384 bits == 48 bytes (libgcrypt ≥ 1.7.0)
* GCRY_MD_SHA3_512 512 bits == 64 bytes (libgcrypt ≥ 1.7.0)
*
* The result hash is stored in "hash" (the buffer must be large enough).
*
* If hash_size is not NULL, the length of hash is stored in *hash_size
* (in bytes).
*
* Returns:
* 1: OK
* 0: error
*/
int
weecrypto_hash (const void *data, int data_size, int hash_algo,
void *hash, int *hash_size)
{
gcry_md_hd_t *hd_md;
int rc, hd_md_opened, algo_size;
unsigned char *ptr_hash;
rc = 0;
hd_md = NULL;
hd_md_opened = 0;
if (!hash)
goto hash_end;
if (hash_size)
*hash_size = 0;
if (!data || (data_size < 1))
goto hash_end;
hd_md = malloc (sizeof (gcry_md_hd_t));
if (!hd_md)
goto hash_end;
if (gcry_md_open (hd_md, hash_algo, 0) != 0)
goto hash_end;
hd_md_opened = 1;
gcry_md_write (*hd_md, data, data_size);
ptr_hash = gcry_md_read (*hd_md, hash_algo);
if (!ptr_hash)
goto hash_end;
algo_size = gcry_md_get_algo_dlen (hash_algo);
memcpy (hash, ptr_hash, algo_size);
if (hash_size)
*hash_size = algo_size;
rc = 1;
hash_end:
if (hd_md)
{
if (hd_md_opened)
gcry_md_close (*hd_md);
free (hd_md);
}
return rc;
}
/*
* Computes hash of file using the given hash algorithm.
*
* The hash size depends on the algorithm, common ones are:
*
* GCRY_MD_CRC32 32 bits == 4 bytes
* GCRY_MD_MD5 128 bits == 16 bytes
* GCRY_MD_SHA1 160 bits == 20 bytes
* GCRY_MD_SHA224 224 bits == 28 bytes
* GCRY_MD_SHA256 256 bits == 32 bytes
* GCRY_MD_SHA384 384 bits == 48 bytes
* GCRY_MD_SHA512 512 bits == 64 bytes
* GCRY_MD_SHA3_224 224 bits == 28 bytes (libgcrypt ≥ 1.7.0)
* GCRY_MD_SHA3_256 256 bits == 32 bytes (libgcrypt ≥ 1.7.0)
* GCRY_MD_SHA3_384 384 bits == 48 bytes (libgcrypt ≥ 1.7.0)
* GCRY_MD_SHA3_512 512 bits == 64 bytes (libgcrypt ≥ 1.7.0)
*
* The result hash is stored in "hash" (the buffer must be large enough).
*
* If hash_size is not NULL, the length of hash is stored in *hash_size
* (in bytes).
*
* Returns:
* 1: OK
* 0: error
*/
int
weecrypto_hash_file (const char *filename, int hash_algo,
void *hash, int *hash_size)
{
gcry_md_hd_t *hd_md;
struct stat st;
FILE *file;
size_t num_read;
int rc, hd_md_opened, algo_size;
unsigned char *ptr_hash;
char buffer[4096];
rc = 0;
hd_md = NULL;
hd_md_opened = 0;
file = NULL;
if (!hash)
goto hash_end;
if (hash_size)
*hash_size = 0;
if (!filename || !filename[0] || !hash)
goto hash_end;
if (stat (filename, &st) == -1)
goto hash_end;
file = fopen (filename, "r");
if (!file)
goto hash_end;
hd_md = malloc (sizeof (gcry_md_hd_t));
if (!hd_md)
goto hash_end;
if (gcry_md_open (hd_md, hash_algo, 0) != 0)
goto hash_end;
hd_md_opened = 1;
while (!feof (file))
{
num_read = fread (buffer, 1, sizeof (buffer), file);
if (num_read == 0)
break;
gcry_md_write (*hd_md, buffer, num_read);
}
ptr_hash = gcry_md_read (*hd_md, hash_algo);
if (!ptr_hash)
goto hash_end;
algo_size = gcry_md_get_algo_dlen (hash_algo);
memcpy (hash, ptr_hash, algo_size);
if (hash_size)
*hash_size = algo_size;
rc = 1;
hash_end:
if (hd_md)
{
if (hd_md_opened)
gcry_md_close (*hd_md);
free (hd_md);
}
if (file)
fclose (file);
return rc;
}
/*
* Computes PKCS#5 Passphrase Based Key Derivation Function number 2 (PBKDF2)
* hash of data.
*
* The hash size depends on the algorithm, common ones are:
*
* GCRY_MD_SHA1 160 bits == 20 bytes
* GCRY_MD_SHA256 256 bits == 32 bytes
* GCRY_MD_SHA512 512 bits == 64 bytes
*
* The result hash is stored in "hash" (the buffer must be large enough).
*
* If hash_size is not NULL, the length of hash is stored in *hash_size
* (in bytes).
*
* Returns:
* 1: OK
* 0: error
*/
int
weecrypto_hash_pbkdf2 (const void *data, int data_size, int hash_algo,
const void *salt, int salt_size, int iterations,
void *hash, int *hash_size)
{
int rc, algo_size;
rc = 0;
if (!hash)
goto hash_pbkdf2_end;
if (hash_size)
*hash_size = 0;
if (!data || (data_size < 1) || !salt || (salt_size < 1)
|| (iterations < 1))
{
goto hash_pbkdf2_end;
}
algo_size = gcry_md_get_algo_dlen (hash_algo);
if (gcry_kdf_derive (data, data_size, GCRY_KDF_PBKDF2, hash_algo,
salt, salt_size, iterations,
algo_size, hash) != 0)
{
goto hash_pbkdf2_end;
}
if (hash_size)
*hash_size = algo_size;
rc = 1;
hash_pbkdf2_end:
return rc;
}
/*
* Computes keyed-hash message authentication code (HMAC).
*
* The hash size depends on the algorithm, common ones are:
*
* GCRY_MD_CRC32 32 bits == 4 bytes
* GCRY_MD_MD5 128 bits == 16 bytes
* GCRY_MD_SHA1 160 bits == 20 bytes
* GCRY_MD_SHA224 224 bits == 28 bytes
* GCRY_MD_SHA256 256 bits == 32 bytes
* GCRY_MD_SHA384 384 bits == 48 bytes
* GCRY_MD_SHA512 512 bits == 64 bytes
* GCRY_MD_SHA3_224 224 bits == 28 bytes (libgcrypt ≥ 1.7.0)
* GCRY_MD_SHA3_256 256 bits == 32 bytes (libgcrypt ≥ 1.7.0)
* GCRY_MD_SHA3_384 384 bits == 48 bytes (libgcrypt ≥ 1.7.0)
* GCRY_MD_SHA3_512 512 bits == 64 bytes (libgcrypt ≥ 1.7.0)
*
* The result hash is stored in "hash" (the buffer must be large enough).
*
* If hash_size is not NULL, the length of hash is stored in *hash_size
* (in bytes).
*
* Returns:
* 1: OK
* 0: error
*/
int
weecrypto_hmac (const void *key, int key_size,
const void *message, int message_size,
int hash_algo,
void *hash, int *hash_size)
{
gcry_md_hd_t *hd_md;
int rc, hd_md_opened, algo_size;
unsigned char *ptr_hash;
rc = 0;
hd_md = NULL;
hd_md_opened = 0;
if (!hash)
goto hmac_end;
if (hash_size)
*hash_size = 0;
if (!key || (key_size < 1) || !message || (message_size < 1))
goto hmac_end;
hd_md = malloc (sizeof (gcry_md_hd_t));
if (!hd_md)
goto hmac_end;
if (gcry_md_open (hd_md, hash_algo, GCRY_MD_FLAG_HMAC) != 0)
goto hmac_end;
hd_md_opened = 1;
if (gcry_md_setkey (*hd_md, key, key_size) != 0)
goto hmac_end;
gcry_md_write (*hd_md, message, message_size);
ptr_hash = gcry_md_read (*hd_md, hash_algo);
if (!ptr_hash)
goto hmac_end;
algo_size = gcry_md_get_algo_dlen (hash_algo);
memcpy (hash, ptr_hash, algo_size);
if (hash_size)
*hash_size = algo_size;
rc = 1;
hmac_end:
if (hd_md)
{
if (hd_md_opened)
gcry_md_close (*hd_md);
free (hd_md);
}
return rc;
}
/*
* Generates a Time-based One-Time Password (TOTP), as described
* in the RFC 6238.
*
* Returns:
* 1: OK
* 0: error
*/
int
weecrypto_totp_generate_internal (const char *secret, int length_secret,
uint64_t moving_factor, int digits,
char *result)
{
uint64_t moving_factor_swapped;
char hash[20];
int rc, offset, length;
unsigned long bin_code;
moving_factor_swapped = (moving_factor >> 56)
| ((moving_factor << 40) & 0x00FF000000000000)
| ((moving_factor << 24) & 0x0000FF0000000000)
| ((moving_factor << 8) & 0x000000FF00000000)
| ((moving_factor >> 8) & 0x00000000FF000000)
| ((moving_factor >> 24) & 0x0000000000FF0000)
| ((moving_factor >> 40) & 0x000000000000FF00)
| (moving_factor << 56);
rc = weecrypto_hmac (secret, length_secret,
&moving_factor_swapped, sizeof (moving_factor_swapped),
GCRY_MD_SHA1,
hash, NULL);
if (!rc)
return 0;
offset = hash[19] & 0xf;
bin_code = (hash[offset] & 0x7f) << 24
| (hash[offset+1] & 0xff) << 16
| (hash[offset+2] & 0xff) << 8
| (hash[offset+3] & 0xff);
bin_code %= (unsigned long)(pow (10, digits));
length = snprintf (result, digits + 1, "%.*lu", digits, bin_code);
if (length != digits)
return 0;
return 1;
}
/*
* Generates a Time-based One-Time Password (TOTP), as described
* in the RFC 6238.
*
* Returns the password as string, NULL if error.
*
* Note: result must be freed after use.
*/
char *
weecrypto_totp_generate (const char *secret_base32, time_t totp_time,
int digits)
{
char *result, *secret;
int length_secret, rc;
uint64_t moving_factor;
secret = NULL;
result = NULL;
if (!secret_base32 || !secret_base32[0]
|| (digits < WEECRYPTO_TOTP_MIN_DIGITS)
|| (digits > WEECRYPTO_TOTP_MAX_DIGITS))
{
goto error;
}
secret = malloc ((strlen (secret_base32) * 4) + 16 + 1);
if (!secret)
goto error;
length_secret = string_base32_decode (secret_base32, secret);
if (length_secret < 0)
goto error;
result = malloc (digits + 1);
if (!result)
goto error;
if (totp_time == 0)
totp_time = time (NULL);
moving_factor = totp_time / 30;
rc = weecrypto_totp_generate_internal (secret, length_secret,
moving_factor, digits, result);
if (!rc)
goto error;
free (secret);
return result;
error:
if (secret)
free (secret);
if (result)
free (result);
return NULL;
}
/*
* Validates a Time-based One-Time Password (TOTP).
*
* Returns:
* 1: OTP is OK
* 0: OTP is invalid
*/
int
weecrypto_totp_validate (const char *secret_base32, time_t totp_time,
int window, const char *otp)
{
char *secret, str_otp[16];
int length_secret, digits, rc, otp_ok;
uint64_t i, moving_factor;
secret = NULL;
if (!secret_base32 || !secret_base32[0] || (window < 0) || !otp || !otp[0])
goto error;
digits = strlen (otp);
if ((digits < WEECRYPTO_TOTP_MIN_DIGITS)
|| (digits > WEECRYPTO_TOTP_MAX_DIGITS))
{
goto error;
}
secret = malloc (strlen (secret_base32) + 1);
if (!secret)
goto error;
length_secret = string_base32_decode (secret_base32, secret);
if (length_secret < 0)
goto error;
if (totp_time == 0)
totp_time = time (NULL);
moving_factor = totp_time / 30;
otp_ok = 0;
for (i = moving_factor - window; i <= moving_factor + window; i++)
{
rc = weecrypto_totp_generate_internal (secret, length_secret,
i, digits, str_otp);
if (rc && (strcmp (str_otp, otp) == 0))
{
otp_ok = 1;
break;
}
}
free (secret);
return otp_ok;
error:
if (secret)
free (secret);
return 0;
}
|