-
Notifications
You must be signed in to change notification settings - Fork 0
/
crypto_mbedtls.c
736 lines (601 loc) · 15 KB
/
crypto_mbedtls.c
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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
#include <rain_common.h>
#ifdef MBEDTLS_CONF
char * time_string (time_t t, int usec, bool show_usec)
{
struct timeval tv;
char *out = malloc(64);
memset(out,0x00,64);
if (t)
{
tv.tv_sec = t;
tv.tv_usec = usec;
}
else
{
gettimeofday (&tv, NULL);
}
t = tv.tv_sec;
snprintf(out,strlen(ctime(&t))-1,"%s",ctime(&t));
if (show_usec && tv.tv_usec){
sprintf(out,"%s us=%d",out,(int)tv.tv_usec);
}
return out;
}
void crypto_init_lib_engine (const char *engine_name)
{
if(engine_name){}
MM("Note: PolarSSL hardware crypto engine functionality is not " "available \n");
}
void crypto_init_lib (void)
{
}
void crypto_uninit_lib (void)
{
}
void crypto_clear_error (void)
{
}
#if 1
bool mbedtls_log_err(unsigned int flags, int errval, const char *prefix)
{
if(flags){}
if (0 != errval)
{
char errstr[256];
mbedtls_strerror(errval, errstr, sizeof(errstr));
if (NULL == prefix){
prefix = "PolarSSL error";
}
MM("%s: %s \n", prefix, errstr);
}
return 0 == errval;
}
bool mbedtls_log_func_line(unsigned int flags, int errval, const char *func, int line)
{
char prefix[256];
if(flags){}
if (!snprintf(prefix, sizeof(prefix), "%s:%d", func, line)){
return mbedtls_log_err(flags, errval, func);
}
return mbedtls_log_err(flags, errval, prefix);
}
bool mbedtls_log_func_line_lite(unsigned int flags, int errval, const char *func, int line)
{
if(flags){}
if(errval){}
if(func){}
if(line){}
#if 0
if (errval) {
return polar_log_func_line (flags, errval, func, line);
}
return true;
#else
return true;
#endif
}
#endif
typedef struct {
const char * openvpn_name;
const char * polarssl_name;
}cipher_name_pair;
cipher_name_pair cipher_name_translation_table[] = {
{ "BF-CBC", "BLOWFISH-CBC" },
{ "BF-CFB", "BLOWFISH-CFB64" },
{ "CAMELLIA-128-CFB", "CAMELLIA-128-CFB128" },
{ "CAMELLIA-192-CFB", "CAMELLIA-192-CFB128" },
{ "CAMELLIA-256-CFB", "CAMELLIA-256-CFB128" }
};
const size_t cipher_name_translation_table_count = sizeof(cipher_name_translation_table) / sizeof(*cipher_name_translation_table);
static void print_cipher(const cipher_kt_t *info)
{
if (info && (cipher_kt_mode_cbc(info)
#ifdef HAVE_AEAD_CIPHER_MODES
|| cipher_kt_mode_aead(info)
#endif
))
{
const char *ssl_only = cipher_kt_mode_cbc(info) ? "" : ", TLS client/server mode only";
const char *var_key_size = info->flags & MBEDTLS_CIPHER_VARIABLE_KEY_LEN ? " by default" : "";
printf("%s (%d bit key%s, %d bit block%s)\n", cipher_kt_name(info), cipher_kt_key_size(info) * 8, var_key_size,cipher_kt_block_size(info) * 8, ssl_only);
}
}
void show_available_ciphers ()
{
const int *ciphers = mbedtls_cipher_list();
while (*ciphers != 0)
{
const cipher_kt_t *info = mbedtls_cipher_info_from_type(*ciphers);
if (info && cipher_kt_block_size(info) >= 128/8)
{
print_cipher(info);
}
ciphers++;
}
printf("\nThe following ciphers have a block size of less than 128 bits, \n"
"and are therefore deprecated. Do not use unless you have to.\n\n");
ciphers = mbedtls_cipher_list();
while (*ciphers != 0)
{
const cipher_kt_t *info = mbedtls_cipher_info_from_type(*ciphers);
if (info && cipher_kt_block_size(info) < 128/8)
{
print_cipher(info);
}
ciphers++;
}
printf("\n");
#if 0
const int *ciphers = mbedtls_cipher_list();
while (*ciphers != 0)
{
const cipher_kt_t *info = mbedtls_cipher_info_from_type(*ciphers);
if (info && cipher_kt_block_size(info) >= 128/8)
{
print_cipher(info);
}
ciphers++;
}
printf ("\nThe following ciphers have a block size of less than 128 bits, \n"
"and are therefore deprecated. Do not use unless you have to.\n\n");
ciphers = mbedtls_cipher_list();
while (*ciphers != 0)
{
const cipher_kt_t *info = mbedtls_cipher_info_from_type(*ciphers);
if (info && cipher_kt_block_size(info) < 128/8)
{
print_cipher(info);
}
ciphers++;
}
printf ("\n");
#endif
}
const cipher_name_pair * get_cipher_name_pair(const char *cipher_name) {
cipher_name_pair *pair;
size_t i = 0;
for (; i < sizeof (cipher_name_translation_table) / sizeof (*cipher_name_translation_table); i++)
{
pair = &cipher_name_translation_table[i];
if (0 == strcmp (cipher_name, pair->openvpn_name) || 0 == strcmp (cipher_name, pair->polarssl_name)){
return pair;
}
}
return NULL;
}
void show_available_digests ()
{
const int *digests = mbedtls_md_list();
while (*digests != 0)
{
const mbedtls_md_info_t *info = mbedtls_md_info_from_type(*digests);
if (info){
printf("%s %d bit default key\n", mbedtls_md_get_name(info),mbedtls_md_get_size(info) * 8);
}
digests++;
}
printf ("\n");
}
void show_available_engines ()
{
printf ("Sorry, PolarSSL hardware crypto engine functionality is not " "available\n");
}
const char * translate_cipher_name_from_openvpn (const char *cipher_name) {
const cipher_name_pair *pair = get_cipher_name_pair(cipher_name);
if (NULL == pair){
return cipher_name;
}
return pair->polarssl_name;
}
const char * translate_cipher_name_to_openvpn (const char *cipher_name) {
const cipher_name_pair *pair = get_cipher_name_pair(cipher_name);
if (NULL == pair){
return cipher_name;
}
return pair->openvpn_name;
}
mbedtls_ctr_drbg_context * rand_ctx_get() //rainroot
{
static mbedtls_entropy_context ec = {0};
static mbedtls_ctr_drbg_context cd_ctx = {0};
static bool rand_initialised = false;
if (!rand_initialised)
{
char *pers_string = malloc(100);
memset(pers_string,0x00,100);
char *time_str = time_string(0, 0, 0);
sprintf(pers_string,"OpenVPN %0u %p %s",getpid(), &cd_ctx, time_str);
mbedtls_entropy_init(&ec);
mbedtls_ctr_drbg_init(&cd_ctx);
if (mbedtls_ctr_drbg_seed(&cd_ctx, mbedtls_entropy_func, &ec,pers_string, strlen(pers_string)) < 0)
{
MM("Failed to initialize random generator\n");
}
free(pers_string);
free(time_str);
rand_initialised = true;
}
return &cd_ctx;
}
int rand_bytes(uint8_t *output, int len)
{
mbedtls_ctr_drbg_context *rng_ctx = rand_ctx_get();
while (len > 0)
{
const size_t blen = min_int (len, MBEDTLS_CTR_DRBG_MAX_REQUEST);
if (0 != mbedtls_ctr_drbg_random(rng_ctx, output, blen)){
return 0;
}
output += blen;
len -= blen;
}
return 1;
}
int key_des_num_cblocks (const mbedtls_cipher_info_t *kt)
{
int ret = 0;
if (kt->type == MBEDTLS_CIPHER_DES_CBC){
ret = 1;
}
if (kt->type == MBEDTLS_CIPHER_DES_EDE_CBC){
ret = 2;
}
if (kt->type == MBEDTLS_CIPHER_DES_EDE3_CBC){
ret = 3;
}
MM("CRYPTO INFO: n_DES_cblocks=%d \n", ret);
return ret;
}
bool key_des_check (uint8_t *key, int key_len, int ndc)
{
int i;
if(key_len){}
for (i = 0; i < ndc; ++i)
{
if(i > 0){
key += MBEDTLS_DES_KEY_SIZE;
}
if (!key)
{
MM("ERR: CRYPTO INFO: check_key_DES: insufficient key material \n");
goto err;
}
if (0 != mbedtls_des_key_check_weak(key))
{
MM("ERR: CRYPTO INFO: check_key_DES: weak key detected \n");
goto err;
}
if (0 != mbedtls_des_key_check_key_parity(key))
{
MM("ERR: CRYPTO INFO: check_key_DES: bad parity detected \n");
goto err;
}
}
return true;
err:
return false;
}
void key_des_fixup (uint8_t *key, int key_len, int ndc)
{
int i;
if(key_len){}
for (i = 0; i < ndc; ++i)
{
if( i > 0){
key += MBEDTLS_DES_KEY_SIZE;
}
if (!key)
{
MM("ERR: CRYPTO INFO: fixup_key_DES: insufficient key material \n");
return;
}
mbedtls_des_key_set_parity(key);
}
}
mbedtls_cipher_info_t * cipher_kt_get (const char *ciphername)
{
mbedtls_cipher_info_t *cipher = NULL;
if(ciphername != NULL){
cipher = (mbedtls_cipher_info_t *)mbedtls_cipher_info_from_string(ciphername);
if (NULL == cipher){
MM("Cipher algorithm '%s' not found \n", ciphername);
exit(0);
}
if (cipher->key_bitlen/8 > MAX_CIPHER_KEY_LENGTH){
MM("Cipher algorithm '%s' uses a default key size (%d bytes) which is larger than current maximum key size (%d bytes) \n",
ciphername,
cipher->key_bitlen/8,
MAX_CIPHER_KEY_LENGTH);
return NULL;
}
}
return cipher;
}
const char * cipher_kt_name (const mbedtls_cipher_info_t *cipher_kt)
{
if (NULL == cipher_kt){
return "[null-cipher]";
}
return translate_cipher_name_to_openvpn(cipher_kt->name);
}
int cipher_kt_key_size (const mbedtls_cipher_info_t *cipher_kt)
{
if (NULL == cipher_kt){
return 0;
}
return cipher_kt->key_bitlen/8;
}
int cipher_kt_iv_size (const mbedtls_cipher_info_t *cipher_kt)
{
if (NULL == cipher_kt){
return 0;
}
return cipher_kt->iv_size;
}
int cipher_kt_block_size (const mbedtls_cipher_info_t *cipher_kt)
{
if (NULL == cipher_kt){
return 0;
}
return cipher_kt->block_size;
}
int cipher_kt_tag_size(const mbedtls_cipher_info_t *cipher_kt)
{
#ifdef HAVE_AEAD_CIPHER_MODES
if (cipher_kt && cipher_kt_mode_aead(cipher_kt))
{
return OPENVPN_AEAD_TAG_LENGTH;
}
#endif
return 0;
}
int cipher_kt_mode (const mbedtls_cipher_info_t *cipher_kt)
{
assert(NULL != cipher_kt);
return cipher_kt->mode;
}
bool cipher_kt_mode_cbc(const cipher_kt_t *cipher)
{
return cipher && cipher_kt_mode(cipher) == OPENVPN_MODE_CBC;
}
bool cipher_kt_mode_ofb_cfb(const cipher_kt_t *cipher)
{
return cipher && (cipher_kt_mode(cipher) == OPENVPN_MODE_OFB || cipher_kt_mode(cipher) == OPENVPN_MODE_CFB);
}
#ifdef HAVE_AEAD_CIPHER_MODES
bool cipher_kt_mode_aead(const cipher_kt_t *cipher)
{
return cipher && cipher_kt_mode(cipher) == OPENVPN_MODE_GCM;
}
#endif
void cipher_ctx_init (mbedtls_cipher_context_t *ctx, uint8_t *key, int key_len, const mbedtls_cipher_info_t *kt, const mbedtls_operation_t operation)
{
assert(NULL != kt && NULL != ctx);
if (mbedtls_cipher_setup(ctx, kt)){
MM("PolarSSL cipher context init #1\n");
}
if (mbedtls_cipher_setkey(ctx, key, key_len*8, operation)){
MM("PolarSSL cipher set key\n");
}
assert (ctx->key_bitlen <= key_len * 8);
}
void cipher_ctx_cleanup (mbedtls_cipher_context_t *ctx)
{
mbedtls_cipher_free(ctx);
}
int cipher_ctx_iv_length (const mbedtls_cipher_context_t *ctx)
{
return mbedtls_cipher_get_iv_size(ctx);
}
int cipher_ctx_get_tag(cipher_ctx_t *ctx, uint8_t *tag, int tag_len)
{
#ifdef HAVE_AEAD_CIPHER_MODES
if (tag_len > SIZE_MAX)
{
return 0;
}
if (!mbed_ok(mbedtls_cipher_write_tag(ctx, (unsigned char *) tag, tag_len)))
{
return 0;
}
return 1;
#else /* ifdef HAVE_AEAD_CIPHER_MODES */
return 0;
#endif /* HAVE_AEAD_CIPHER_MODES */
}
int cipher_ctx_block_size(const mbedtls_cipher_context_t *ctx)
{
return mbedtls_cipher_get_block_size(ctx);
}
int cipher_ctx_mode (const mbedtls_cipher_context_t *ctx)
{
return cipher_kt_mode(ctx->cipher_info);
}
const cipher_kt_t * cipher_ctx_get_cipher_kt (const cipher_ctx_t *ctx)
{
return ctx ? ctx->cipher_info : NULL;
}
int cipher_ctx_reset (mbedtls_cipher_context_t *ctx, uint8_t *iv_buf)
{
if (mbedtls_cipher_reset(ctx)){
MM("##ERR: %s %d ##\n",__func__,__LINE__);
return 0;
}
if (mbedtls_cipher_set_iv(ctx, iv_buf, ctx->cipher_info->iv_size)){
MM("##ERR: %s %d ##\n",__func__,__LINE__);
return 0;
}
return 1;
}
int cipher_ctx_update_ad(cipher_ctx_t *ctx, const uint8_t *src, int src_len)
{
#ifdef HAVE_AEAD_CIPHER_MODES
if (src_len > SIZE_MAX)
{
return 0;
}
if (mbedtls_cipher_update_ad(ctx, src, src_len) < 0)
{
return 0;
}
return 1;
#else /* ifdef HAVE_AEAD_CIPHER_MODES */
return 0;
#endif /* HAVE_AEAD_CIPHER_MODES */
}
int cipher_ctx_update (mbedtls_cipher_context_t *ctx, uint8_t *dst, int *dst_len, uint8_t *src, int src_len)
{
size_t s_dst_len = *dst_len;
if (mbedtls_cipher_update(ctx, src, (size_t)src_len, dst, &s_dst_len) < 0){
MM("##ERR: %s %d ##\n",__func__,__LINE__);
return 0;
}
*dst_len = s_dst_len;
return 1;
}
int cipher_ctx_final (mbedtls_cipher_context_t *ctx, uint8_t *dst, int *dst_len)
{
size_t s_dst_len = *dst_len;
if (mbedtls_cipher_finish(ctx, dst, &s_dst_len) < 0){
MM("##ERR: %s %d ##\n",__func__,__LINE__);
return 0;
}
*dst_len = s_dst_len;
return 1;
}
int cipher_ctx_final_check_tag(mbedtls_cipher_context_t *ctx, uint8_t *dst, int *dst_len, uint8_t *tag, size_t tag_len)
{
#ifdef HAVE_AEAD_CIPHER_MODES
size_t olen = 0;
if (MBEDTLS_DECRYPT != ctx->operation)
{
return 0;
}
if (tag_len > SIZE_MAX)
{
return 0;
}
if (!mbed_ok(mbedtls_cipher_finish(ctx, dst, &olen)))
{
msg(D_CRYPT_ERRORS, "%s: cipher_ctx_final() failed", __func__);
return 0;
}
if (olen > INT_MAX)
{
return 0;
}
*dst_len = olen;
if (!mbed_ok(mbedtls_cipher_check_tag(ctx, (const unsigned char *) tag,
tag_len)))
{
return 0;
}
return 1;
#else /* ifdef HAVE_AEAD_CIPHER_MODES */
return 0;
#endif /* HAVE_AEAD_CIPHER_MODES */
}
void cipher_des_encrypt_ecb (const unsigned char key[DES_KEY_LENGTH], unsigned char *src,unsigned char *dst)
{
mbedtls_des_context ctx;
if(mbedtls_des_setkey_enc(&ctx, key) < 0){
MM("##ERR: %s %d ##\n",__func__,__LINE__);
exit(0);
}
if(mbedtls_des_crypt_ecb(&ctx, src, dst) < 0){
MM("##ERR: %s %d ##\n",__func__,__LINE__);
exit(0);
}
}
const mbedtls_md_info_t * md_kt_get (const char *digest)
{
const mbedtls_md_info_t *md = NULL;
assert (digest);
md = mbedtls_md_info_from_string(digest);
if (md == NULL){
MM("## EXIT : Message hash algorithm '%s' not found ##\n", digest);
exit(0);
}
if (mbedtls_md_get_size(md) > MAX_HMAC_KEY_LENGTH)
{
MM("Message hash algorithm '%s' uses a default hash size (%d bytes) which is larger than Drizzle current maximum hash size (%d bytes) \n",
digest,
mbedtls_md_get_size(md),
MAX_HMAC_KEY_LENGTH);
}
return md;
}
const char * md_kt_name (const mbedtls_md_info_t *kt)
{
if (NULL == kt){
return "[null-digest]";
}
return mbedtls_md_get_name(kt);
}
int md_kt_size (const mbedtls_md_info_t *kt)
{
if (NULL == kt){
return 0;
}
return mbedtls_md_get_size(kt);
}
int md_full (const md_kt_t *kt, const uint8_t *src, int src_len, uint8_t *dst)
{
return 0 == mbedtls_md(kt, src, src_len, dst);
}
void md_ctx_init (mbedtls_md_context_t *ctx, const mbedtls_md_info_t *kt)
{
mbedtls_md_init_ctx(ctx, kt);
mbedtls_md_setup(ctx, kt, 0);
mbedtls_md_starts(ctx);
}
void md_ctx_cleanup(mbedtls_md_context_t *ctx)
{
if(ctx){}
}
int md_ctx_size (const mbedtls_md_context_t *ctx)
{
if (NULL == ctx){
return 0;
}
return mbedtls_md_get_size(ctx->md_info);
}
void md_ctx_update (mbedtls_md_context_t *ctx, const char *src, int src_len)
{
assert(0 == mbedtls_md_update(ctx, (const unsigned char *)src, src_len));
}
void md_ctx_final (mbedtls_md_context_t *ctx, char *dst)
{
assert(0 == mbedtls_md_finish(ctx, (unsigned char *)dst));
mbedtls_md_free(ctx);
}
void hmac_ctx_init (mbedtls_md_context_t *ctx, const uint8_t *key, int key_len, const mbedtls_md_info_t *kt)
{
mbedtls_md_init(ctx);
mbedtls_md_setup(ctx, kt, 1);
mbedtls_md_hmac_starts(ctx, key, key_len);
}
void hmac_ctx_cleanup(mbedtls_md_context_t *ctx)
{
mbedtls_md_free(ctx);
}
int hmac_ctx_size (const mbedtls_md_context_t *ctx)
{
if (NULL == ctx){
return 0;
}
return mbedtls_md_get_size(ctx->md_info);
}
void hmac_ctx_reset (mbedtls_md_context_t *ctx)
{
assert(0 == mbedtls_md_hmac_reset(ctx));
}
void hmac_ctx_update (mbedtls_md_context_t *ctx, const uint8_t *src, int src_len)
{
assert(0 == mbedtls_md_hmac_update(ctx, src, src_len));
}
void hmac_ctx_final (mbedtls_md_context_t *ctx, uint8_t *dst)
{
assert(0 == mbedtls_md_hmac_finish(ctx, dst));
}
#endif