xref: /freebsd/sys/contrib/libsodium/test/default/kx.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 
2 #define TEST_NAME "kx"
3 #include "cmptest.h"
4 
5 static const unsigned char small_order_p[crypto_scalarmult_BYTES] = {
6     0xe0, 0xeb, 0x7a, 0x7c, 0x3b, 0x41, 0xb8, 0xae, 0x16, 0x56, 0xe3,
7     0xfa, 0xf1, 0x9f, 0xc4, 0x6a, 0xda, 0x09, 0x8d, 0xeb, 0x9c, 0x32,
8     0xb1, 0xfd, 0x86, 0x62, 0x05, 0x16, 0x5f, 0x49, 0xb8, 0x00
9 };
10 
11 static void
12 tv_kx(void)
13 {
14     unsigned char *seed;
15     unsigned char *client_pk, *client_sk;
16     unsigned char *client_rx, *client_tx;
17     unsigned char *server_pk, *server_sk;
18     unsigned char *server_rx, *server_tx;
19     char           hex[65];
20     int            i;
21 
22     seed = (unsigned char *) sodium_malloc(crypto_kx_SEEDBYTES);
23     for (i = 0; i < crypto_kx_SEEDBYTES; i++) {
24         seed[i] = (unsigned char) i;
25     }
26     client_pk = (unsigned char *) sodium_malloc(crypto_kx_PUBLICKEYBYTES);
27     client_sk = (unsigned char *) sodium_malloc(crypto_kx_SECRETKEYBYTES);
28     crypto_kx_seed_keypair(client_pk, client_sk, seed);
29 
30     sodium_bin2hex(hex, sizeof hex, client_pk, crypto_kx_PUBLICKEYBYTES);
31     printf("client_pk: [%s]\n", hex);
32     sodium_bin2hex(hex, sizeof hex, client_sk, crypto_kx_SECRETKEYBYTES);
33     printf("client_sk: [%s]\n", hex);
34 
35     server_pk = (unsigned char *) sodium_malloc(crypto_kx_PUBLICKEYBYTES);
36     server_sk = (unsigned char *) sodium_malloc(crypto_kx_SECRETKEYBYTES);
37     crypto_kx_keypair(server_pk, server_sk);
38 
39     client_rx = (unsigned char *) sodium_malloc(crypto_kx_SESSIONKEYBYTES);
40     client_tx = (unsigned char *) sodium_malloc(crypto_kx_SESSIONKEYBYTES);
41 
42     assert(crypto_kx_client_session_keys(client_rx, client_tx,
43                                          client_pk, client_sk,
44                                          small_order_p) == -1);
45     if (crypto_kx_client_session_keys(client_rx, client_tx,
46                                       client_pk, client_sk, server_pk) != 0) {
47         printf("crypto_kx_client_session_keys() failed\n");
48     }
49 
50     server_rx = (unsigned char *) sodium_malloc(crypto_kx_SESSIONKEYBYTES);
51     server_tx = (unsigned char *) sodium_malloc(crypto_kx_SESSIONKEYBYTES);
52 
53     assert(crypto_kx_server_session_keys(server_rx, server_tx,
54                                          server_pk, server_sk,
55                                          small_order_p) == -1);
56     if (crypto_kx_server_session_keys(server_rx, server_tx,
57                                       server_pk, server_sk, client_pk) != 0) {
58         printf("crypto_kx_server_session_keys() failed\n");
59     }
60     if (memcmp(server_rx, client_tx, crypto_kx_SESSIONKEYBYTES) != 0 ||
61         memcmp(server_tx, client_rx, crypto_kx_SESSIONKEYBYTES) != 0) {
62         printf("client session keys != server session keys\n");
63     }
64 
65     sodium_increment(client_pk, crypto_kx_PUBLICKEYBYTES);
66     if (crypto_kx_server_session_keys(server_rx, server_tx,
67                                       server_pk, server_sk, client_pk) != 0) {
68         printf("crypto_kx_server_session_keys() failed\n");
69     }
70     if (memcmp(server_rx, client_tx, crypto_kx_SESSIONKEYBYTES) == 0 &&
71         memcmp(server_tx, client_rx, crypto_kx_SESSIONKEYBYTES) == 0) {
72         printf("peer's public key is ignored\n");
73     }
74 
75     crypto_kx_keypair(client_pk, client_sk);
76     if (crypto_kx_server_session_keys(server_rx, server_tx,
77                                       server_pk, server_sk, client_pk) != 0) {
78         printf("crypto_kx_server_session_keys() failed\n");
79     }
80     if (memcmp(server_rx, client_tx, crypto_kx_SESSIONKEYBYTES) == 0 ||
81         memcmp(server_tx, client_rx, crypto_kx_SESSIONKEYBYTES) == 0) {
82         printf("session keys are constant\n");
83     }
84 
85     crypto_kx_seed_keypair(client_pk, client_sk, seed);
86     sodium_increment(seed, crypto_kx_SEEDBYTES);
87     crypto_kx_seed_keypair(server_pk, server_sk, seed);
88     if (crypto_kx_server_session_keys(server_rx, server_tx,
89                                       server_pk, server_sk, client_pk) != 0) {
90         printf("crypto_kx_server_session_keys() failed\n");
91     }
92     sodium_bin2hex(hex, sizeof hex, server_rx, crypto_kx_SESSIONKEYBYTES);
93     printf("server_rx: [%s]\n", hex);
94     sodium_bin2hex(hex, sizeof hex, server_tx, crypto_kx_SESSIONKEYBYTES);
95     printf("server_tx: [%s]\n", hex);
96 
97     if (crypto_kx_client_session_keys(client_rx, client_tx,
98                                       client_pk, client_sk, server_pk) != 0) {
99         printf("crypto_kx_client_session_keys() failed\n");
100     }
101     sodium_bin2hex(hex, sizeof hex, client_rx, crypto_kx_SESSIONKEYBYTES);
102     printf("client_rx: [%s]\n", hex);
103     sodium_bin2hex(hex, sizeof hex, client_tx, crypto_kx_SESSIONKEYBYTES);
104     printf("client_tx: [%s]\n", hex);
105 
106     randombytes_buf(client_rx, crypto_kx_SESSIONKEYBYTES);
107     randombytes_buf(client_tx, crypto_kx_SESSIONKEYBYTES);
108     randombytes_buf(server_rx, crypto_kx_SESSIONKEYBYTES);
109     randombytes_buf(server_tx, crypto_kx_SESSIONKEYBYTES);
110     if (crypto_kx_client_session_keys(client_rx, NULL,
111                                       client_pk, client_sk, server_pk) != 0 ||
112         crypto_kx_client_session_keys(NULL, client_tx,
113                                       client_pk, client_sk, server_pk) != 0 ||
114         crypto_kx_server_session_keys(server_rx, NULL,
115                                       server_pk, server_sk, client_pk) != 0 ||
116         crypto_kx_server_session_keys(NULL, server_tx,
117                                       server_pk, server_sk, client_pk) != 0) {
118         printf("failure when one of the pointers happens to be NULL");
119     }
120     assert(memcmp(client_rx, client_tx, crypto_kx_SESSIONKEYBYTES) == 0);
121     assert(memcmp(client_tx, server_rx, crypto_kx_SESSIONKEYBYTES) == 0);
122     assert(memcmp(server_rx, server_tx, crypto_kx_SESSIONKEYBYTES) == 0);
123 
124     sodium_free(client_rx);
125     sodium_free(client_tx);
126     sodium_free(server_rx);
127     sodium_free(server_tx);
128     sodium_free(server_sk);
129     sodium_free(server_pk);
130     sodium_free(client_sk);
131     sodium_free(client_pk);
132     sodium_free(seed);
133 
134     assert(strcmp(crypto_kx_primitive(), crypto_kx_PRIMITIVE) == 0);
135     assert(crypto_kx_publickeybytes() == crypto_kx_PUBLICKEYBYTES);
136     assert(crypto_kx_secretkeybytes() == crypto_kx_SECRETKEYBYTES);
137     assert(crypto_kx_seedbytes() == crypto_kx_SEEDBYTES);
138     assert(crypto_kx_sessionkeybytes() == crypto_kx_SESSIONKEYBYTES);
139 
140     printf("tv_kx: ok\n");
141 }
142 
143 int
144 main(void)
145 {
146     tv_kx();
147 
148     return 0;
149 }
150