Fix community table sweep / removal. Trust router now stable.
[trust_router.git] / common / tests / 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 #include <string.h>
38
39 #include <trust_router/tr_dh.h>
40
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};
46 //
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};
52 //
53 // int tmp_len = 32;
54
55 int main (int argc, 
56           const char *argv[]) 
57 {
58   DH *c_dh = NULL;
59   DH *s_dh = NULL;
60   unsigned char *c_keybuf = NULL;
61   unsigned char *s_keybuf = NULL;
62   int c_keylen = 0, s_keylen = 0, i = 0;
63
64   /* TBD -- Generate random private keys */
65
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");
69     exit(1);
70   }
71
72   fprintf(stderr, "Client DH Parameters:\n");
73   DHparams_print_fp(stdout, c_dh);
74   fprintf(stderr, "\n");
75
76   /*** Would now send DH params and client's public key to the server ***/
77
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");
81     exit(1);
82   }
83
84   fprintf(stdout, "Server DH Parameters:\n");
85   DHparams_print_fp(stdout, s_dh);
86   fprintf(stdout, "\n");
87
88   /*** Would now send server's pub key to client ***/
89
90   /* Compute key on client */
91   if (0 > (c_keylen = tr_compute_dh_key(&c_keybuf, 
92                                       s_dh->pub_key, 
93                                       c_dh))) {
94     
95   }
96   
97   /* Compute key on server */
98   if (0 > (s_keylen = tr_compute_dh_key(&s_keybuf, 
99                                       c_dh->pub_key, 
100                                       s_dh))) {
101     printf("Error: Can't compute server key.\n");
102     exit(1);
103   }
104   
105   /* Print out the client key. */
106   printf("Client Key Generated (len = %d):\n", c_keylen);
107   for (i = 0; i < c_keylen; i++) {
108     printf("%2x", c_keybuf[i]);
109   }
110   printf("\n");
111
112   /* Print out the server key. */
113   printf("Server Key Generated (len = %d):\n", s_keylen);
114   for (i = 0; i < s_keylen; i++) {
115     printf("%2x", s_keybuf[i]);
116   }
117   printf("\n");
118
119   /* Compare the two keys to see if they match */
120   if ((c_keylen != s_keylen) ||
121       (0 != memcmp(c_keybuf, s_keybuf, c_keylen))) {
122     printf("Error: Different keys generated!\n");
123     exit(1);
124   }
125
126   printf("Success: Identical keys generated, key length = %d!\n", c_keylen);
127   exit(0);
128 }
129     
130
131