2 * Copyright (c) 2012, JANET(UK)
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of JANET(UK) nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
41 // char tmp_key1[32] =
42 // {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
43 // 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
44 // 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
45 // 0x19, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
47 // char tmp_key2[32] =
48 // {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
49 // 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
50 // 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
51 // 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04};
60 unsigned char *c_keybuf = NULL;
61 unsigned char *s_keybuf = NULL;
62 int c_keylen = 0, s_keylen = 0, i = 0;
64 /* TBD -- Generate random private keys */
66 /* Generate initial DH params on the client side */
67 if (NULL == (c_dh = tr_create_dh_params(NULL, 0))) {
68 printf("Error: Can't create client DH params, exiting.\n");
72 fprintf(stderr, "Client DH Parameters:\n");
73 DHparams_print_fp(stdout, c_dh);
74 fprintf(stderr, "\n");
76 /*** Would now send DH params and client's public key to the server ***/
78 /* Generate DH params on the server side */
79 if (NULL == (s_dh = tr_create_matching_dh(NULL, 0, c_dh))) {
80 printf("Error: Can't create server server DH params, exiting.\n");
84 fprintf(stdout, "Server DH Parameters:\n");
85 DHparams_print_fp(stdout, s_dh);
86 fprintf(stdout, "\n");
88 /*** Would now send server's pub key to client ***/
90 /* Compute key on client */
91 if (NULL == (c_keybuf = malloc(DH_size(c_dh)))) {
92 printf ("Error: Can't allocate client keybuf, exiting.\n");
95 if (0 > (c_keylen = tr_compute_dh_key(c_keybuf,
102 /* Compute key on server */
103 if (NULL == (s_keybuf = malloc(DH_size(s_dh)))) {
104 printf ("Error: Can't allocate server keybuf, exiting.\n");
107 if (0 > (s_keylen = tr_compute_dh_key(s_keybuf,
111 printf("Error: Can't compute server key.\n");
115 /* Print out the client key. */
116 printf("Client Key Generated (len = %d):\n", c_keylen);
117 for (i = 0; i < c_keylen; i++) {
118 printf("%2x", c_keybuf[i]);
122 /* Print out the server key. */
123 printf("Server Key Generated (len = %d):\n", s_keylen);
124 for (i = 0; i < s_keylen; i++) {
125 printf("%2x", s_keybuf[i]);
129 /* Compare the two keys to see if they match */
130 if ((c_keylen != s_keylen) ||
131 (0 != memcmp(c_keybuf, s_keybuf, c_keylen))) {
132 printf("Error: Different keys generated!\n");
136 printf("Success: Identical keys generated, key length = %d!\n", c_keylen);