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.
40 // char tmp_key1[32] =
41 // {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
42 // 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
43 // 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
44 // 0x19, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
46 // char tmp_key2[32] =
47 // {0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
48 // 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02, 0x02,
49 // 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
50 // 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04};
59 char *c_keybuf = NULL;
60 char *s_keybuf = NULL;
61 int dh_err = 0, c_keylen = 0, s_keylen = 0;
63 /* TBD -- Generate random private keys */
65 /* Generate initial DH params on the client side */
66 if (NULL == (c_dh = tr_create_dh_params(NULL, 0))) {
67 printf("Error: Can't create client DH params, exiting.\n");
71 fprintf(stderr, "Client DH Parameters:\n");
72 DHparams_print_fp(stdout, c_dh);
73 fprintf(stderr, "\n");
75 /*** Would now send DH params and client's public key to the server ***/
77 /* Generate DH params on the server side */
78 if (NULL == (s_dh = tr_create_matching_dh(NULL, 0, c_dh))) {
79 printf("Error: Can't create server server DH params, exiting.\n");
83 fprintf(stdout, "Server DH Parameters:\n");
84 DHparams_print_fp(stdout, s_dh);
85 fprintf(stdout, "\n");
87 /*** Would now send server's pub key to client ***/
89 /* Compute key on client */
90 if (NULL == (c_keybuf = malloc(DH_size(c_dh)))) {
91 printf ("Error: Can't allocate client keybuf, exiting.\n");
94 if (0 > (c_keylen = tr_compute_dh_key(c_keybuf,
101 /* Compute key on server */
102 if (NULL == (s_keybuf = malloc(DH_size(c_dh)))) {
103 printf ("Error: Can't allocate server keybuf, exiting.\n");
106 if (0 > (s_keylen = tr_compute_dh_key(s_keybuf,
113 /* Compare the two keys to see if they match */
114 if ((c_keylen != s_keylen) ||
115 (0 != memcmp(c_keybuf, s_keybuf, c_keylen))) {
116 printf("Error: Different keys generated!\n");
120 printf("Success: Identical keys generated, key length = %d!\n", c_keylen);