endian-fixes-20060802
[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 <afs/param.h>
35 #include "rxkad.h"
36 #include <rx/rx.h>
37 #include "private_data.h"
38
39 #define ROUNDS 16
40 #define ENCRYPT 1
41 #define DECRYPT 0
42
43 typedef afs_int32 int32;
44 typedef afs_uint32 u_int32;
45
46 #include <stdio.h>
47 #include <string.h>
48
49 #include <time.h>
50
51 const char the_quick[] = "The quick brown fox jumps over the lazy dogs.\0\0";
52
53 const unsigned char key1[8] =
54     { 0xf0, 0xe1, 0xd2, 0xc3, 0xb4, 0xa5, 0x96, 0x87 };
55 const char ciph1[] = {
56     0x00, 0xf0, 0xe, 0x11, 0x75, 0xe6, 0x23, 0x82, 0xee, 0xac, 0x98, 0x62,
57     0x44, 0x51, 0xe4, 0x84, 0xc3, 0x59, 0xd8, 0xaa, 0x64, 0x60, 0xae, 0xf7,
58     0xd2, 0xd9, 0x13, 0x79, 0x72, 0xa3, 0x45, 0x03, 0x23, 0xb5, 0x62, 0xd7,
59     0xc, 0xf5, 0x27, 0xd1, 0xf8, 0x91, 0x3c, 0xac, 0x44, 0x22, 0x92, 0xef
60 };
61
62 const unsigned char key2[8] =
63     { 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 };
64 const char ciph2[] = {
65     0xca, 0x90, 0xf5, 0x9d, 0xcb, 0xd4, 0xd2, 0x3c, 0x01, 0x88, 0x7f, 0x3e,
66     0x31, 0x6e, 0x62, 0x9d, 0xd8, 0xe0, 0x57, 0xa3, 0x06, 0x3a, 0x42, 0x58,
67     0x2a, 0x28, 0xfe, 0x72, 0x52, 0x2f, 0xdd, 0xe0, 0x19, 0x89, 0x09, 0x1c,
68     0x2a, 0x8e, 0x8c, 0x94, 0xfc, 0xc7, 0x68, 0xe4, 0x88, 0xaa, 0xde, 0x0f
69 };
70
71 #ifdef TEST_KERNEL
72 #define fc_keysched    _afs_QTKrFdpoFL
73 #define fc_ecb_encrypt _afs_sDLThwNLok
74 #define fc_cbc_encrypt _afs_fkyCWTvfRS
75 #define rxkad_DecryptPacket _afs_SRWEeqTXrS
76 #define rxkad_EncryptPacket _afs_bpwQbdoghO
77 #endif
78
79 int
80 main(void)
81 {
82     int32 sched[ROUNDS];
83     char ciph[100], clear[100];
84     u_int32 data[2];
85     u_int32 iv[2];
86     struct rx_connection conn;
87     struct rx_securityClass obj;
88     struct rxkad_cprivate cpriv;
89     struct rx_packet packet;
90     int fail = 0;
91
92     conn.securityObject = &obj;
93     obj.privateData = (void *)&cpriv;
94     cpriv.type = 0;
95
96     if (sizeof(int32) != 4) {
97         fprintf(stderr, "error: sizeof(int32) != 4\n");
98         fail++;
99     }
100     if (sizeof(u_int32) != 4) {
101         fprintf(stderr, "error: sizeof(u_int32) != 4\n");
102         fail++;
103     }
104
105     /*
106      * Use key1 and key2 as iv */
107     fc_keysched(key1, sched);
108     memcpy(iv, key2, sizeof(iv));
109     fc_cbc_encrypt(the_quick, ciph, sizeof(the_quick), sched, iv, ENCRYPT);
110     if (memcmp(ciph1, ciph, sizeof(ciph1)) != 0) {
111         fprintf(stderr, "encrypt FAILED\n");
112         fail++;
113     }
114     memcpy(iv, key2, sizeof(iv));
115     fc_cbc_encrypt(ciph, clear, sizeof(the_quick), sched, iv, DECRYPT);
116     if (strcmp(the_quick, clear) != 0) {
117         fprintf(stderr, "crypt decrypt FAILED\n");
118         fail++;
119     }
120
121     /*
122      * Use key2 and key1 as iv
123      */
124     fc_keysched(key2, sched);
125     memcpy(iv, key1, sizeof(iv));
126     fc_cbc_encrypt(the_quick, ciph, sizeof(the_quick), sched, iv, ENCRYPT);
127     if (memcmp(ciph2, ciph, sizeof(ciph2)) != 0) {
128         fprintf(stderr, "encrypt FAILED\n");
129         fail++;
130     }
131     memcpy(iv, key1, sizeof(iv));
132     fc_cbc_encrypt(ciph, clear, sizeof(the_quick), sched, iv, DECRYPT);
133     if (strcmp(the_quick, clear) != 0) {
134         fprintf(stderr, "crypt decrypt FAILED\n");
135         fail++;
136     }
137
138     /*
139      * Test Encrypt- and Decrypt-Packet, use key1 and key2 as iv
140      */
141     fc_keysched(key1, sched);
142     memcpy(iv, key2, sizeof(iv));
143     strcpy(clear, the_quick);
144     packet.wirevec[1].iov_base = clear;
145     packet.wirevec[1].iov_len = sizeof(the_quick);
146     packet.wirevec[2].iov_len = 0;
147
148     /* For unknown reasons bytes 4-7 are zeroed in rxkad_EncryptPacket */
149     rxkad_EncryptPacket(&conn, sched, iv, sizeof(the_quick), &packet);
150     rxkad_DecryptPacket(&conn, sched, iv, sizeof(the_quick), &packet);
151     clear[4] ^= 'q';
152     clear[5] ^= 'u';
153     clear[6] ^= 'i';
154     clear[7] ^= 'c';
155     if (strcmp(the_quick, clear) != 0)
156         fprintf(stderr, "rxkad_EncryptPacket/rxkad_DecryptPacket FAILED\n");
157
158     {
159         struct timeval start, stop;
160         int i;
161
162         fc_keysched(key1, sched);
163         gettimeofday(&start, 0);
164         for (i = 0; i < 1000000; i++)
165             fc_keysched(key1, sched);
166         gettimeofday(&stop, 0);
167         printf("fc_keysched    = %2.2f us\n",
168                (stop.tv_sec - start.tv_sec +
169                 (stop.tv_usec - start.tv_usec) / 1e6) * 1);
170
171         fc_ecb_encrypt(data, data, sched, ENCRYPT);
172         gettimeofday(&start, 0);
173         for (i = 0; i < 1000000; i++)
174             fc_ecb_encrypt(data, data, sched, ENCRYPT);
175         gettimeofday(&stop, 0);
176         printf("fc_ecb_encrypt = %2.2f us\n",
177                (stop.tv_sec - start.tv_sec +
178                 (stop.tv_usec - start.tv_usec) / 1e6) * 1);
179
180         fc_cbc_encrypt(the_quick, ciph, sizeof(the_quick), sched, iv,
181                        ENCRYPT);
182         gettimeofday(&start, 0);
183         for (i = 0; i < 100000; i++)
184             fc_cbc_encrypt(the_quick, ciph, sizeof(the_quick), sched, iv,
185                            ENCRYPT);
186         gettimeofday(&stop, 0);
187         printf("fc_cbc_encrypt = %2.2f us\n",
188                (stop.tv_sec - start.tv_sec +
189                 (stop.tv_usec - start.tv_usec) / 1e6) * 10);
190
191     }
192
193     exit(fail);
194 }