Fix logic error in comparison.
[trust_router.git] / common / dh_test / dh_test.c
1 /*
2  * Copyright (c) 2012, JANET(UK)
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
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.
15  *
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.
19  *
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.
32  *
33  */
34
35 #include <stdlib.h>
36 #include <stdio.h>
37
38 #include <tr_dh.h>
39
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};
45 //
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};
51 //
52 // int tmp_len = 32;
53
54 int main (int argc, 
55           const char *argv[]) 
56 {
57   DH *c_dh = NULL;
58   DH *s_dh = NULL;
59   char *c_keybuf = NULL;
60   char *s_keybuf = NULL;
61   int dh_err = 0, c_keylen = 0, s_keylen = 0;
62
63   /* TBD -- Generate random private keys */
64
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");
68     exit(1);
69   }
70
71   fprintf(stderr, "Client DH Parameters:\n");
72   DHparams_print_fp(stdout, c_dh);
73   fprintf(stderr, "\n");
74
75   /*** Would now send DH params and client's public key to the server ***/
76
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");
80     exit(1);
81   }
82
83   fprintf(stdout, "Server DH Parameters:\n");
84   DHparams_print_fp(stdout, s_dh);
85   fprintf(stdout, "\n");
86
87   /*** Would now send server's pub key to client ***/
88
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");
92     exit(1);
93   }
94   if (0 > (c_keylen = tr_compute_dh_key(c_keybuf, 
95                                       DH_size(c_dh), 
96                                       s_dh->pub_key, 
97                                       c_dh))) {
98     
99   }
100   
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");
104     exit(1);
105   }
106   if (0 > (s_keylen = tr_compute_dh_key(s_keybuf, 
107                                       DH_size(s_dh), 
108                                       c_dh->pub_key, 
109                                       s_dh))) {
110     
111   }
112   
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");
117     exit(1);
118   }
119
120   printf("Success: Identical keys generated, key length = %d!\n", c_keylen);
121   exit(0);
122 }
123     
124
125