HEIMDAL: move code from source4/heimdal* to third_party/heimdal*
[samba.git] / third_party / heimdal / lib / hcrypto / rc2test.c
1 /*
2  * Copyright (c) 2004 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 <config.h>
35 #include <roken.h>
36
37 #include <rc2.h>
38
39 struct {
40     const void *key;
41     const int keylen;
42     const int bitsize;
43     const void *plain;
44     const void *cipher;
45 } tests[] = {
46     {
47         "\x00\x00\x00\x00\x00\x00\x00\x00"
48         "\x00\x00\x00\x00\x00\x00\x00\x00",
49         16,
50         0,
51         "\x00\x00\x00\x00\x00\x00\x00\x00",
52         "\x1C\x19\x8A\x83\x8D\xF0\x28\xB7"
53     },
54     {
55         "\x00\x00\x00\x00\x00\x00\x00\x00"
56         "\x00\x00\x00\x00\x00\x00\x00\x01",
57         16,
58         0,
59         "\x00\x00\x00\x00\x00\x00\x00\x00",
60         "\x21\x82\x9C\x78\xA9\xF9\xC0\x74"
61     },
62     {
63         "\x00\x00\x00\x00\x00\x00\x00\x00"
64         "\x00\x00\x00\x00\x00\x00\x00\x00",
65         16,
66         0,
67         "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
68         "\x13\xDB\x35\x17\xD3\x21\x86\x9E"
69     },
70     {
71         "\x00\x01\x02\x03\x04\x05\x06\x07"
72         "\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F",
73         16,
74         0,
75         "\x00\x00\x00\x00\x00\x00\x00\x00",
76         "\x50\xDC\x01\x62\xBD\x75\x7F\x31"
77     },
78     {
79         "\x00\x00\x00\x00\x00\x00\x00\x00",
80         8,
81         63,
82         "\x00\x00\x00\x00\x00\x00\x00\x00",
83         "\xeb\xb7\x73\xf9\x93\x27\x8e\xff"
84     },
85     {
86         "\xff\xff\xff\xff\xff\xff\xff\xff",
87         8,
88         64,
89         "\xff\xff\xff\xff\xff\xff\xff\xff",
90         "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49"
91     },
92     {
93         "\x88",
94         1,
95         64,
96         "\x00\x00\x00\x00\x00\x00\x00\x00",
97         "\x61\xa8\xa2\x44\xad\xac\xcc\xf0"
98     }
99 };
100
101 const unsigned char cbc_key[16] =
102 "\x00\x00\x00\x00\x00\x00\x00\x00"
103 "\x00\x00\x00\x00\x00\x00\x00\x00";
104 const char cbc_iv[8] =
105 "\x01\x01\x01\x01\x01\x01\x01\x01";
106 const unsigned char cbc_in_data[32] =
107 "\x20\x20\x20\x20\x20\x20\x20\x20"
108 "\x20\x20\x20\x20\x20\x20\x20\x20"
109 "\x20\x20\x20\x20\x20\x20\x20\x20"
110 "\x20\x20\x20\x20\x20\x20\x20\x20";
111
112 const char out_iv[8] = "\x00\x78\x1b\x6\xff\xb9\xfa\xe";
113
114 const char cbc_out_data[32] =
115 "\xb4\x3f\x89\x15\x69\x68\xda\x79"
116 "\x29\xab\x5f\x78\xc5\xba\x15\x82"
117 "\x80\x89\x57\x1b\xbe\x57\x2f\xdc"
118 "\x00\x78\x1b\x06\xff\xb9\xfa\x0e";
119
120 int
121 main(int argc, char **argv)
122 {
123     RC2_KEY key;
124     unsigned char t[8];
125     unsigned char out[40];
126     int i;
127
128     for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
129         RC2_set_key(&key, tests[i].keylen, tests[i].key, tests[i].bitsize);
130
131         memcpy(t, tests[i].plain, 8);
132         RC2_encryptc(t, t, &key);
133         if (memcmp(t, tests[i].cipher, 8) != 0) {
134             printf("encrypt %d\n", i);
135             exit(1);
136         }
137         RC2_decryptc(t, t, &key);
138         if (memcmp(t, tests[i].plain, 8) != 0) {
139             printf("decrypt: %d\n", i);
140             exit(1);
141         }
142     }
143
144     /* cbc test */
145
146     RC2_set_key(&key, 16, cbc_key, 0);
147     memcpy(t, cbc_iv, 8);
148     RC2_cbc_encrypt(cbc_in_data, out, 32, &key, t, 1);
149
150     if (memcmp(out_iv, t, 8) != 0)
151         abort();
152
153     if (memcmp(out, cbc_out_data, 32) != 0) {
154         printf("cbc test encrypt\n");
155         exit(1);
156     }
157
158     memcpy(t, cbc_iv, 8);
159     RC2_cbc_encrypt(out, out, 32, &key, t, 0);
160
161     if (memcmp(cbc_in_data, out, 32) != 0) {
162         printf("cbc test decrypt \n");
163         exit(1);
164     }
165
166     return 0;
167 }