summaryrefslogtreecommitdiff
path: root/src/lib/tests/kex_test.c
blob: 58cf8b430e99802c01cabe57f5251c3a10b71542 (plain)
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
/*
 * Ouroboros - Copyright (C) 2016 - 2024
 *
 * Test of the key exchange functions
 *
 *    Dimitri Staessens <dimitri@ouroboros.rocks>
 *    Sander Vrijders   <sander@ouroboros.rocks>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., http://www.fsf.org/about/contact/.
 */

#define _POSIX_C_SOURCE 200809L

#include "config.h"

#include <test/test.h>
#include <ouroboros/utils.h>
#include <ouroboros/crypt.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#ifdef HAVE_OPENSSL
#include <openssl/evp.h>
#include <openssl/x509.h>
#endif

/* Test configuration strings */
#define KEX_CONFIG_CUSTOM \
        "kex=X25519\n"

#define KEX_CONFIG_NONE \
        "none\n"

#define KEX_CONFIG_WHITESPACE \
        "# Comment line\n" \
        "kex = X448" \
        "\n" \
        "# Another comment\n"

#define KEX_CONFIG_CIPHER \
        "kex=X25519\n" \
        "cipher=chacha20-poly1305\n"

#define KEX_CONFIG_DIGEST \
        "kex=X25519\n" \
        "digest=sha384\n"

/* Test key material for key loading tests */
#define X25519_PRIVKEY_PEM \
        "-----BEGIN PRIVATE KEY-----\n" \
        "MC4CAQAwBQYDK2VuBCIEIJDd3+/0k2IZlaH5sZ9Z2e5J8dV2U0nsXaSUm70ZaMhL\n" \
        "-----END PRIVATE KEY-----\n"

#define X25519_PUBKEY_PEM \
        "-----BEGIN PUBLIC KEY-----\n" \
        "MCowBQYDK2VuAyEAKYLIycSZtLFlwAX07YWWgBAYhEnRxHfgK1TVw9+mtBs=\n" \
        "-----END PUBLIC KEY-----\n"

/* Helper macro to open string constant as FILE stream */
#define FMEMOPEN_STR(str) fmemopen((void *) (str), strlen(str), "r")

extern const uint16_t kex_supported_nids[];

int parse_sec_config(struct sec_config * cfg,
                     FILE *              fp);

static int test_kex_create_destroy(void)
{
        struct sec_config cfg;

        TEST_START();

        memset(&cfg, 0, sizeof(cfg));
        cfg.x.nid = NID_X9_62_prime256v1;
        cfg.x.str = kex_nid_to_str(cfg.x.nid);
        cfg.c.nid = NID_aes_256_gcm;
        cfg.c.str = crypt_nid_to_str(cfg.c.nid);

        if (cfg.x.nid == NID_undef || cfg.c.nid == NID_undef) {
                printf("Failed to initialize kex config.\n");
                goto fail;
        }

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_kex_dh_pkp_create_destroy(void)
{
        struct sec_config kex;
        void *         pkp;
        uint8_t        buf[MSGBUFSZ];

        TEST_START();

        memset(&kex, 0, sizeof(kex));
        SET_KEX_ALGO(&kex, "prime256v1");

        if (kex_pkp_create(&kex, &pkp, buf) < 0) {
                printf("Failed to create DH PKP.\n");
                goto fail;
        }

        kex_pkp_destroy(pkp);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_kex_get_algo_from_pk(const char * algo)
{
        struct sec_config kex;
        void *            pkp;
        buffer_t          pk;
        ssize_t           len;
        uint8_t           buf[MSGBUFSZ];
        char              extracted_algo[256];

        TEST_START("(%s)", algo);

        memset(&kex, 0, sizeof(kex));
        SET_KEX_ALGO(&kex, algo);

        len = kex_pkp_create(&kex, &pkp, buf);
        if (len < 0) {
                printf("Failed to create key pair.\n");
                goto fail;
        }

        pk.len  = (size_t) len;
        pk.data = buf;

        /* Use raw decode for hybrid KEMs, DER for others */
        if (IS_HYBRID_KEM(algo)) {
                if (kex_get_algo_from_pk_raw(pk, extracted_algo) < 0) {
                        printf("Failed to extract algo from pk.\n");
                        goto fail_pkp;
                }
        } else {
                if (kex_get_algo_from_pk_der(pk, extracted_algo) < 0) {
                        printf("Failed to extract algo from pk.\n");
                        goto fail_pkp;
                }
        }

        /* All algorithms should now return the specific group name */
        if (strcmp(extracted_algo, algo) != 0) {
                printf("Algo mismatch: expected %s, got %s.\n",
                       algo, extracted_algo);
                goto fail_pkp;
        }

        kex_pkp_destroy(pkp);

        TEST_SUCCESS("(%s)", algo);

        return TEST_RC_SUCCESS;
 fail_pkp:
        kex_pkp_destroy(pkp);
 fail:
        TEST_FAIL("(%s)", algo);
        return TEST_RC_FAIL;
}

static int test_kex_get_algo_from_pk_all(void)
{
        int ret = 0;
        int i;

        for (i = 0; kex_supported_nids[i] != NID_undef; i++) {
                const char * algo = kex_nid_to_str(kex_supported_nids[i]);
                ret |= test_kex_get_algo_from_pk(algo);
        }

        return ret;
}

static int test_kex_dhe_derive(const char * algo)
{
        struct sec_config kex;
        void *            pkp1;
        void *            pkp2;
        buffer_t          pk1;
        buffer_t          pk2;
        ssize_t           len;
        uint8_t           buf1[MSGBUFSZ];
        uint8_t           buf2[MSGBUFSZ];
        uint8_t           s1[SYMMKEYSZ];
        uint8_t           s2[SYMMKEYSZ];

        TEST_START("(%s)", algo);

        memset(&kex, 0, sizeof(kex));
        SET_KEX_ALGO(&kex, algo);

        len = kex_pkp_create(&kex, &pkp1, buf1);
        if (len < 0) {
                printf("Failed to create first key pair for %s.\n", algo);
                goto fail;
        }

        pk1.len  = (size_t) len;
        pk1.data = buf1;

        len = kex_pkp_create(&kex, &pkp2, buf2);
        if (len < 0) {
                printf("Failed to create second key pair for %s.\n", algo);
                goto fail_pkp1;
        }

        pk2.len  = (size_t) len;
        pk2.data = buf2;

        if (kex_dhe_derive(&kex, pkp1, pk2, s1) < 0) {
                printf("Failed to derive first key for %s.\n", algo);
                goto fail_pkp2;
        }

        if (kex_dhe_derive(&kex, pkp2, pk1, s2) < 0) {
                printf("Failed to derive second key for %s.\n", algo);
                goto fail_pkp2;
        }

        if (memcmp(s1, s2, SYMMKEYSZ) != 0) {
                printf("Derived keys do not match for %s.\n", algo);
                goto fail_pkp2;
        }

        kex_pkp_destroy(pkp2);
        kex_pkp_destroy(pkp1);

        TEST_SUCCESS("(%s)", algo);

        return TEST_RC_SUCCESS;
 fail_pkp2:
        kex_pkp_destroy(pkp2);
 fail_pkp1:
        kex_pkp_destroy(pkp1);
 fail:
        TEST_FAIL("(%s)", algo);
        return TEST_RC_FAIL;
}

static int test_kex_validate_algo(void)
{
        TEST_START();

        if (kex_validate_algo("prime256v1") != 0) {
                printf("prime256v1 should be valid.\n");
                goto fail;
        }

        if (kex_validate_algo("X25519") != 0) {
                printf("X25519 should be valid.\n");
                goto fail;
        }

#ifdef HAVE_OPENSSL_PQC
        if (kex_validate_algo("ML-KEM-768") != 0) {
                printf("ML-KEM-768 should be valid.\n");
                goto fail;
        }
#endif

        if (kex_validate_algo("ffdhe2048") != 0) {
                printf("ffdhe2048 should be valid.\n");
                goto fail;
        }

        if (kex_validate_algo("invalid_algo") == 0) {
                printf("invalid_algo should be rejected.\n");
                goto fail;
        }

        if (kex_validate_algo("rsa2048") == 0) {
                printf("rsa2048 should be rejected.\n");
                goto fail;
        }

        if (kex_validate_algo(NULL) == 0) {
                printf("NULL should be rejected.\n");
                goto fail;
        }

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_kex_dhe_corrupted_pubkey(const char * algo)
{
        struct sec_config kex;
        void *            pkp;
        buffer_t          pk;
        ssize_t           len;
        uint8_t           buf[MSGBUFSZ];
        uint8_t           s[SYMMKEYSZ];

        TEST_START("(%s)", algo);

        memset(&kex, 0, sizeof(kex));
        SET_KEX_ALGO(&kex, algo);

        len = kex_pkp_create(&kex, &pkp, buf);
        if (len < 0) {
                printf("Failed to create key pair.\n");
                goto fail;
        }

        pk.len  = (size_t) len;
        pk.data = buf;

        /* Corrupt the public key */
        buf[0] ^= 0xFF;
        buf[len - 1] ^= 0xFF;

        if (kex_dhe_derive(&kex, pkp, pk, s) == 0) {
                printf("Should fail with corrupted public key.\n");
                goto fail_pkp;
        }

        kex_pkp_destroy(pkp);

        TEST_SUCCESS("(%s)", algo);

        return TEST_RC_SUCCESS;
 fail_pkp:
        kex_pkp_destroy(pkp);
 fail:
        TEST_FAIL("(%s)", algo);
        return TEST_RC_FAIL;
}

static int test_kex_dhe_wrong_algo(void)
{
        struct sec_config kex1;
        struct sec_config kex2;
        void *            pkp1;
        void *            pkp2;
        buffer_t          pk2;
        ssize_t           len;
        uint8_t           buf1[MSGBUFSZ];
        uint8_t           buf2[MSGBUFSZ];
        uint8_t           s[SYMMKEYSZ];
        const char *      algo1 = "X25519";
        const char *      algo2 = "X448";

        TEST_START("(%s vs %s)", algo1, algo2);

        memset(&kex1, 0, sizeof(kex1));
        memset(&kex2, 0, sizeof(kex2));
        SET_KEX_ALGO(&kex1, algo1);
        SET_KEX_ALGO(&kex2, algo2);

        if (kex_pkp_create(&kex1, &pkp1, buf1) < 0) {
                printf("Failed to create first key pair.\n");
                goto fail;
        }

        len = kex_pkp_create(&kex2, &pkp2, buf2);
        if (len < 0) {
                printf("Failed to create second key pair.\n");
                goto fail_pkp1;
        }

        pk2.len  = (size_t) len;
        pk2.data = buf2;

        /* Try to derive with mismatched algorithms */
        if (kex_dhe_derive(&kex1, pkp1, pk2, s) == 0) {
                printf("Should fail with mismatched algorithms.\n");
                goto fail_pkp2;
        }

        kex_pkp_destroy(pkp2);
        kex_pkp_destroy(pkp1);

        TEST_SUCCESS("(%s vs %s)", algo1, algo2);

        return TEST_RC_SUCCESS;
 fail_pkp2:
        kex_pkp_destroy(pkp2);
 fail_pkp1:
        kex_pkp_destroy(pkp1);
 fail:
        TEST_FAIL("(%s vs %s)", algo1, algo2);
        return TEST_RC_FAIL;
}

static int test_kex_load_dhe_privkey(void)
{
        void * key;

        TEST_START();

        if (crypt_load_privkey_str(X25519_PRIVKEY_PEM, &key) < 0) {
                printf("Failed to load X25519 private key.\n");
                goto fail;
        }

        crypt_free_key(key);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_kex_load_dhe_pubkey(void)
{
        void * key;

        TEST_START();

        if (crypt_load_pubkey_str(X25519_PUBKEY_PEM, &key) < 0) {
                printf("Failed to load X25519 public key.\n");
                goto fail;
        }

        crypt_free_key(key);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

#ifdef HAVE_OPENSSL
#include <openssl/obj_mac.h>

static int test_kex_nid_values(void)
{
        int i;

        TEST_START();

        /* Verify all KEX algorithm NIDs match OpenSSL's */
        for (i = 0; kex_supported_nids[i] != NID_undef; i++) {
                uint16_t our_nid = kex_supported_nids[i];
                const char * kex_name;
                int openssl_nid;

                kex_name = kex_nid_to_str(our_nid);
                if (kex_name == NULL) {
                        printf("kex_nid_to_str failed for NID %u\n", our_nid);
                        goto fail;
                }

                /* Test reverse conversion */
                if (kex_str_to_nid(kex_name) != our_nid) {
                        printf("kex_str_to_nid failed for '%s'\n", kex_name);
                        goto fail;
                }

                /* Get OpenSSL's NID for this name */
                openssl_nid = OBJ_txt2nid(kex_name);
                if (openssl_nid != NID_undef) {
                        /* OpenSSL recognizes this algorithm */
                        if (our_nid != openssl_nid) {
                                printf("NID mismatch for '%s': "
                                       "ours=%d, OpenSSL=%d\n",
                                       kex_name, our_nid, openssl_nid);
                                goto fail;
                        }
                } else {
                        /* Verify no NID collision with different algorithm */
                        const char * ossl_name = OBJ_nid2sn(our_nid);
                        if (ossl_name != NULL &&
                            strcmp(ossl_name, kex_name) != 0) {
                                printf("NID collision for '%d': "
                                       "ours=%s, OpenSSL=%s\n",
                                       our_nid, kex_name, ossl_name);
                                goto fail;
                        }
                }
        }

        /* Test error cases */
        if (kex_str_to_nid("invalid") != NID_undef) {
                printf("kex_str_to_nid should return NID_undef for invalid\n");
                goto fail;
        }

        if (kex_nid_to_str(9999) != NULL) {
                printf("kex_nid_to_str should return NULL for invalid NID\n");
                goto fail;
        }

        if (kex_str_to_nid(NULL) != NID_undef) {
                printf("kex_str_to_nid should return NID_undef for NULL\n");
                goto fail;
        }

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}
#endif

static int test_kex_all(void)
{
        int ret = 0;
        int i;

        for (i = 0; kex_supported_nids[i] != NID_undef; i++) {
                const char * algo = kex_nid_to_str(kex_supported_nids[i]);

                /* KEM tests are in kex_test_pqc.c */
                if (IS_KEM_ALGORITHM(algo))
                        continue;

                ret |= test_kex_dhe_derive(algo);
        }

        return ret;
}

static int test_kex_dhe_corrupted_pubkey_all(void)
{
        int ret = 0;
        int i;

        /* Test corruption for all DHE algorithms */
        /* KEM error injection tests are in kex_test_pqc.c */
        for (i = 0; kex_supported_nids[i] != NID_undef; i++) {
                const char * algo = kex_nid_to_str(kex_supported_nids[i]);

                if (IS_KEM_ALGORITHM(algo))
                        continue;

                ret |= test_kex_dhe_corrupted_pubkey(algo);
        }

        return ret;
}

static int test_kex_parse_config_empty(void)
{
        struct sec_config kex;
        FILE *         fp;

        TEST_START();

        memset(&kex, 0, sizeof(kex));

        fp = FMEMOPEN_STR("");
        if (fp == NULL) {
                printf("Failed to open memory stream.\n");
                goto fail;
        }

        if (parse_sec_config(&kex, fp) < 0) {
                printf("Failed to parse empty config.\n");
                fclose(fp);
                goto fail;
        }

        if (strcmp(kex.x.str, "prime256v1") != 0) {
                printf("Empty config should use prime256v1.\n");
                fclose(fp);
                goto fail;
        }

        fclose(fp);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_kex_parse_config_custom(void)
{
        struct sec_config kex;
        FILE *         fp;

        TEST_START();

        memset(&kex, 0, sizeof(kex));

        fp = FMEMOPEN_STR(KEX_CONFIG_CUSTOM);
        if (fp == NULL) {
                printf("Failed to open memory stream.\n");
                goto fail;
        }

        if (parse_sec_config(&kex, fp) < 0) {
                printf("Failed to parse custom config.\n");
                fclose(fp);
                goto fail;
        }

        if (strcmp(kex.x.str, "X25519") != 0) {
                printf("Algorithm not set correctly.\n");
                fclose(fp);
                goto fail;
        }

        fclose(fp);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_kex_parse_config_none(void)
{
        struct sec_config kex;
        FILE *         fp;

        TEST_START();

        memset(&kex, 0, sizeof(kex));

        fp = FMEMOPEN_STR(KEX_CONFIG_NONE);
        if (fp == NULL) {
                printf("Failed to open memory stream.\n");
                goto fail;
        }

        if (parse_sec_config(&kex, fp) < 0) {
                printf("Failed to parse 'none' config.\n");
                fclose(fp);
                goto fail;
        }

        if (kex.x.nid != NID_undef) {
                printf("'none' keyword should disable encryption.\n");
                fclose(fp);
                goto fail;
        }

        fclose(fp);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_kex_parse_config_whitespace(void)
{
        struct sec_config kex;
        FILE *         fp;

        TEST_START();

        memset(&kex, 0, sizeof(kex));

        fp = FMEMOPEN_STR(KEX_CONFIG_WHITESPACE);
        if (fp == NULL) {
                printf("Failed to open memory stream.\n");
                goto fail;
        }

        if (parse_sec_config(&kex, fp) < 0) {
                printf("Failed to parse config with comments.\n");
                fclose(fp);
                goto fail;
        }

        if (strcmp(kex.x.str, "X448") != 0) {
                printf("Algorithm with whitespace not parsed correctly.\n");
                fclose(fp);
                goto fail;
        }

        fclose(fp);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_kex_parse_config_cipher(void)
{
        struct sec_config kex;
        FILE *         fp;

        TEST_START();

        memset(&kex, 0, sizeof(kex));

        fp = FMEMOPEN_STR(KEX_CONFIG_CIPHER);
        if (fp == NULL) {
                printf("Failed to open memory stream.\n");
                goto fail;
        }

        if (parse_sec_config(&kex, fp) < 0) {
                printf("Failed to parse cipher config.\n");
                fclose(fp);
                goto fail;
        }

        if (strcmp(kex.x.str, "X25519") != 0) {
                printf("Algorithm not set correctly.\n");
                fclose(fp);
                goto fail;
        }

        if (kex.c.nid != NID_chacha20_poly1305) {
                printf("Cipher not set correctly.\n");
                fclose(fp);
                goto fail;
        }

        fclose(fp);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

static int test_kex_parse_config_digest(void)
{
        struct sec_config kex;
        FILE *         fp;

        TEST_START();

        memset(&kex, 0, sizeof(kex));

        fp = FMEMOPEN_STR(KEX_CONFIG_DIGEST);
        if (fp == NULL) {
                printf("Failed to open memory stream.\n");
                goto fail;
        }

        if (parse_sec_config(&kex, fp) < 0) {
                printf("Failed to parse digest config.\n");
                fclose(fp);
                goto fail;
        }

        if (strcmp(kex.x.str, "X25519") != 0) {
                printf("Algorithm not set correctly.\n");
                fclose(fp);
                goto fail;
        }

        if (kex.d.nid != NID_sha384) {
                printf("Digest not set correctly.\n");
                fclose(fp);
                goto fail;
        }

        fclose(fp);

        TEST_SUCCESS();

        return TEST_RC_SUCCESS;
 fail:
        TEST_FAIL();
        return TEST_RC_FAIL;
}

int kex_test(int     argc,
             char ** argv)
{
        int ret = 0;

        (void) argc;
        (void) argv;

        ret |= test_kex_create_destroy();
        ret |= test_kex_parse_config_empty();
        ret |= test_kex_parse_config_none();
#ifdef HAVE_OPENSSL
        ret |= test_kex_parse_config_custom();
        ret |= test_kex_parse_config_whitespace();
        ret |= test_kex_parse_config_cipher();
        ret |= test_kex_parse_config_digest();
        ret |= test_kex_nid_values();
        ret |= test_kex_dh_pkp_create_destroy();
        ret |= test_kex_all();
        ret |= test_kex_validate_algo();
        ret |= test_kex_get_algo_from_pk_all();
        ret |= test_kex_dhe_wrong_algo();
        ret |= test_kex_dhe_corrupted_pubkey_all();
        ret |= test_kex_load_dhe_privkey();
        ret |= test_kex_load_dhe_pubkey();
#else
        (void) test_kex_parse_config_custom;
        (void) test_kex_parse_config_whitespace;
        (void) test_kex_parse_config_cipher;
        (void) test_kex_parse_config_digest;
        (void) test_kex_dh_pkp_create_destroy;
        (void) test_kex_all;
        (void) test_kex_validate_algo;
        (void) test_kex_get_algo_from_pk_all;
        (void) test_kex_dhe_wrong_algo();
        (void) test_kex_dhe_corrupted_pubkey_all;
        (void) test_kex_load_dhe_privkey;
        (void) test_kex_load_dhe_pubkey;

        ret = TEST_RC_SKIP;
#endif
        return ret;
}