rxkad: Tidy header includes
[openafs.git] / src / rxkad / fc_test.c
1 /*
2  * Copyright (c) 1995 - 2000, 2002 Kungliga Tekniska Högskolan
3  * (Royal Institute of Technology, Stockholm, Sweden).
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of the Institute nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include <afsconfig.h>
35 #include <afs/param.h>
36
37 #include <roken.h>
38
39 #include "rxkad.h"
40 #include <rx/rx.h>
41 #include "private_data.h"
42
43 #define ROUNDS 16
44 #define ENCRYPT 1
45 #define DECRYPT 0
46
47 typedef afs_int32 int32;
48 typedef afs_uint32 u_int32;
49
50 const char the_quick[] = "The quick brown fox jumps over the lazy dogs.\0\0";
51
52 const unsigned char key1[8] =
53     { 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87 };
54 const char ciph1[] = {
55     0x00, 0xf0, 0xe, 0x11, 0x75, 0xe6, 0x23, 0x82, 0xee, 0xac, 0x98, 0x62,
56     0x44, 0x51, 0xe4, 0x84, 0xc3, 0x59, 0xd8, 0xaa, 0x64, 0x60, 0xae, 0xf7,
57     0xd2, 0xd9, 0x13, 0x79, 0x72, 0xa3, 0x45, 0x03, 0x23, 0xb5, 0x62, 0xd7,
58     0xc, 0xf5, 0x27, 0xd1, 0xf8, 0x91, 0x3c, 0xac, 0x44, 0x22, 0x92, 0xef
59 };
60
61 const unsigned char key2[8] =
62     { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
63 const char ciph2[] = {
64     0xca, 0x90, 0xf5, 0x9d, 0xcb, 0xd4, 0xd2, 0x3c, 0x01, 0x88, 0x7f, 0x3e,
65     0x31, 0x6e, 0x62, 0x9d, 0xd8, 0xe0, 0x57, 0xa3, 0x06, 0x3a, 0x42, 0x58,
66     0x2a, 0x28, 0xfe, 0x72, 0x52, 0x2f, 0xdd, 0xe0, 0x19, 0x89, 0x09, 0x1c,
67     0x2a, 0x8e, 0x8c, 0x94, 0xfc, 0xc7, 0x68, 0xe4, 0x88, 0xaa, 0xde, 0x0f
68 };
69
70 #ifdef TEST_KERNEL
71 #define fc_keysched    _afs_QTKrFdpoFL
72 #define fc_ecb_encrypt _afs_sDLThwNLok
73 #define fc_cbc_encrypt _afs_fkyCWTvfRS
74 #define rxkad_DecryptPacket _afs_SRWEeqTXrS
75 #define rxkad_EncryptPacket _afs_bpwQbdoghO
76 #endif
77
78 int
79 main(void)
80 {
81     int32 sched[ROUNDS];
82     char ciph[100], clear[100];
83     u_int32 data[2];
84     u_int32 iv[2];
85     struct rx_connection conn;
86     struct rx_securityClass obj;
87     struct rxkad_cprivate cpriv;
88     struct rx_packet packet;
89     int fail = 0;
90
91     conn.securityObject = &obj;
92     obj.privateData = (void *)&cpriv;
93     cpriv.type = 0;
94
95     if (sizeof(int32) != 4) {
96         fprintf(stderr, "error: sizeof(int32) != 4\n");
97         fail++;
98     }
99     if (sizeof(u_int32) != 4) {
100         fprintf(stderr, "error: sizeof(u_int32) != 4\n");
101         fail++;
102     }
103
104     /*
105      * Use key1 and key2 as iv */
106     fc_keysched((struct ktc_encryptionKey *)key1, sched);
107     memcpy(iv, key2, sizeof(iv));
108     fc_cbc_encrypt(the_quick, ciph, sizeof(the_quick), sched, iv, ENCRYPT);
109     if (memcmp(ciph1, ciph, sizeof(ciph1)) != 0) {
110         fprintf(stderr, "encrypt FAILED\n");
111         fail++;
112     }
113     memcpy(iv, key2, sizeof(iv));
114     fc_cbc_encrypt(ciph, clear, sizeof(the_quick), sched, iv, DECRYPT);
115     if (strcmp(the_quick, clear) != 0) {
116         fprintf(stderr, "crypt decrypt FAILED\n");
117         fail++;
118     }
119
120     /*
121      * Use key2 and key1 as iv
122      */
123     fc_keysched((struct ktc_encryptionKey *)key2, sched);
124     memcpy(iv, key1, sizeof(iv));
125     fc_cbc_encrypt(the_quick, ciph, sizeof(the_quick), sched, iv, ENCRYPT);
126     if (memcmp(ciph2, ciph, sizeof(ciph2)) != 0) {
127         fprintf(stderr, "encrypt FAILED\n");
128         fail++;
129     }
130     memcpy(iv, key1, sizeof(iv));
131     fc_cbc_encrypt(ciph, clear, sizeof(the_quick), sched, iv, DECRYPT);
132     if (strcmp(the_quick, clear) != 0) {
133         fprintf(stderr, "crypt decrypt FAILED\n");
134         fail++;
135     }
136
137     /*
138      * Test Encrypt- and Decrypt-Packet, use key1 and key2 as iv
139      */
140     fc_keysched((struct ktc_encryptionKey *)key1, sched);
141     memcpy(iv, key2, sizeof(iv));
142     strcpy(clear, the_quick);
143     packet.wirevec[1].iov_base = clear;
144     packet.wirevec[1].iov_len = sizeof(the_quick);
145     packet.wirevec[2].iov_len = 0;
146
147     /* For unknown reasons bytes 4-7 are zeroed in rxkad_EncryptPacket */
148     rxkad_EncryptPacket(&conn, sched, iv, sizeof(the_quick), &packet);
149     rxkad_DecryptPacket(&conn, sched, iv, sizeof(the_quick), &packet);
150     clear[4] ^= 'q';
151     clear[5] ^= 'u';
152     clear[6] ^= 'i';
153     clear[7] ^= 'c';
154     if (strcmp(the_quick, clear) != 0)
155         fprintf(stderr, "rxkad_EncryptPacket/rxkad_DecryptPacket FAILED\n");
156
157     {
158         struct timeval start, stop;
159         int i;
160
161         fc_keysched((struct ktc_encryptionKey *)key1, sched);
162         gettimeofday(&start, 0);
163         for (i = 0; i < 1000000; i++)
164             fc_keysched((struct ktc_encryptionKey *)key1, sched);
165         gettimeofday(&stop, 0);
166         printf("fc_keysched    = %2.2f us\n",
167                (stop.tv_sec - start.tv_sec +
168                 (stop.tv_usec - start.tv_usec) / 1e6) * 1);
169
170         fc_ecb_encrypt(data, data, sched, ENCRYPT);
171         gettimeofday(&start, 0);
172         for (i = 0; i < 1000000; i++)
173             fc_ecb_encrypt(data, data, sched, ENCRYPT);
174         gettimeofday(&stop, 0);
175         printf("fc_ecb_encrypt = %2.2f us\n",
176                (stop.tv_sec - start.tv_sec +
177                 (stop.tv_usec - start.tv_usec) / 1e6) * 1);
178
179         fc_cbc_encrypt(the_quick, ciph, sizeof(the_quick), sched, iv,
180                        ENCRYPT);
181         gettimeofday(&start, 0);
182         for (i = 0; i < 100000; i++)
183             fc_cbc_encrypt(the_quick, ciph, sizeof(the_quick), sched, iv,
184                            ENCRYPT);
185         gettimeofday(&stop, 0);
186         printf("fc_cbc_encrypt = %2.2f us\n",
187                (stop.tv_sec - start.tv_sec +
188                 (stop.tv_usec - start.tv_usec) / 1e6) * 10);
189
190     }
191
192     exit(fail);
193 }