xref: /netbsd-src/crypto/external/bsd/heimdal/dist/lib/hcrypto/rc2test.c (revision d3273b5b76f5afaafe308cead5511dbb8df8c5e9)
1*d3273b5bSchristos /*	$NetBSD: rc2test.c,v 1.2 2017/01/28 21:31:47 christos Exp $	*/
2ca1c9b0cSelric 
3ca1c9b0cSelric /*
4ca1c9b0cSelric  * Copyright (c) 2004 Kungliga Tekniska Högskolan
5ca1c9b0cSelric  * (Royal Institute of Technology, Stockholm, Sweden).
6ca1c9b0cSelric  * All rights reserved.
7ca1c9b0cSelric  *
8ca1c9b0cSelric  * Redistribution and use in source and binary forms, with or without
9ca1c9b0cSelric  * modification, are permitted provided that the following conditions
10ca1c9b0cSelric  * are met:
11ca1c9b0cSelric  *
12ca1c9b0cSelric  * 1. Redistributions of source code must retain the above copyright
13ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer.
14ca1c9b0cSelric  *
15ca1c9b0cSelric  * 2. Redistributions in binary form must reproduce the above copyright
16ca1c9b0cSelric  *    notice, this list of conditions and the following disclaimer in the
17ca1c9b0cSelric  *    documentation and/or other materials provided with the distribution.
18ca1c9b0cSelric  *
19ca1c9b0cSelric  * 3. Neither the name of the Institute nor the names of its contributors
20ca1c9b0cSelric  *    may be used to endorse or promote products derived from this software
21ca1c9b0cSelric  *    without specific prior written permission.
22ca1c9b0cSelric  *
23ca1c9b0cSelric  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24ca1c9b0cSelric  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25ca1c9b0cSelric  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ca1c9b0cSelric  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27ca1c9b0cSelric  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28ca1c9b0cSelric  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29ca1c9b0cSelric  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30ca1c9b0cSelric  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31ca1c9b0cSelric  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32ca1c9b0cSelric  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33ca1c9b0cSelric  * SUCH DAMAGE.
34ca1c9b0cSelric  */
35ca1c9b0cSelric 
36ca1c9b0cSelric #include <config.h>
37b9d004c6Schristos #include <krb5/roken.h>
38ca1c9b0cSelric 
39ca1c9b0cSelric #include <rc2.h>
40ca1c9b0cSelric 
41ca1c9b0cSelric struct {
42ca1c9b0cSelric     const void *key;
43ca1c9b0cSelric     const int keylen;
44ca1c9b0cSelric     const int bitsize;
45ca1c9b0cSelric     const void *plain;
46ca1c9b0cSelric     const void *cipher;
47ca1c9b0cSelric } tests[] = {
48ca1c9b0cSelric     {
49ca1c9b0cSelric 	"\x00\x00\x00\x00\x00\x00\x00\x00"
50ca1c9b0cSelric 	"\x00\x00\x00\x00\x00\x00\x00\x00",
51ca1c9b0cSelric 	16,
52ca1c9b0cSelric 	0,
53ca1c9b0cSelric 	"\x00\x00\x00\x00\x00\x00\x00\x00",
54ca1c9b0cSelric 	"\x1C\x19\x8A\x83\x8D\xF0\x28\xB7"
55ca1c9b0cSelric     },
56ca1c9b0cSelric     {
57ca1c9b0cSelric 	"\x00\x00\x00\x00\x00\x00\x00\x00"
58ca1c9b0cSelric 	"\x00\x00\x00\x00\x00\x00\x00\x01",
59ca1c9b0cSelric 	16,
60ca1c9b0cSelric 	0,
61ca1c9b0cSelric 	"\x00\x00\x00\x00\x00\x00\x00\x00",
62ca1c9b0cSelric 	"\x21\x82\x9C\x78\xA9\xF9\xC0\x74"
63ca1c9b0cSelric     },
64ca1c9b0cSelric     {
65ca1c9b0cSelric 	"\x00\x00\x00\x00\x00\x00\x00\x00"
66ca1c9b0cSelric 	"\x00\x00\x00\x00\x00\x00\x00\x00",
67ca1c9b0cSelric 	16,
68ca1c9b0cSelric 	0,
69ca1c9b0cSelric 	"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
70ca1c9b0cSelric 	"\x13\xDB\x35\x17\xD3\x21\x86\x9E"
71ca1c9b0cSelric     },
72ca1c9b0cSelric     {
73ca1c9b0cSelric 	"\x00\x01\x02\x03\x04\x05\x06\x07"
74ca1c9b0cSelric 	"\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F",
75ca1c9b0cSelric 	16,
76ca1c9b0cSelric 	0,
77ca1c9b0cSelric 	"\x00\x00\x00\x00\x00\x00\x00\x00",
78ca1c9b0cSelric 	"\x50\xDC\x01\x62\xBD\x75\x7F\x31"
79ca1c9b0cSelric     },
80ca1c9b0cSelric     {
81ca1c9b0cSelric 	"\x00\x00\x00\x00\x00\x00\x00\x00",
82ca1c9b0cSelric 	8,
83ca1c9b0cSelric 	63,
84ca1c9b0cSelric 	"\x00\x00\x00\x00\x00\x00\x00\x00",
85ca1c9b0cSelric 	"\xeb\xb7\x73\xf9\x93\x27\x8e\xff"
86ca1c9b0cSelric     },
87ca1c9b0cSelric     {
88ca1c9b0cSelric 	"\xff\xff\xff\xff\xff\xff\xff\xff",
89ca1c9b0cSelric 	8,
90ca1c9b0cSelric 	64,
91ca1c9b0cSelric 	"\xff\xff\xff\xff\xff\xff\xff\xff",
92ca1c9b0cSelric 	"\x27\x8b\x27\xe4\x2e\x2f\x0d\x49"
93ca1c9b0cSelric     },
94ca1c9b0cSelric     {
95ca1c9b0cSelric 	"\x88",
96ca1c9b0cSelric 	1,
97ca1c9b0cSelric 	64,
98ca1c9b0cSelric 	"\x00\x00\x00\x00\x00\x00\x00\x00",
99ca1c9b0cSelric 	"\x61\xa8\xa2\x44\xad\xac\xcc\xf0"
100ca1c9b0cSelric     }
101ca1c9b0cSelric };
102ca1c9b0cSelric 
103ca1c9b0cSelric const unsigned char cbc_key[16] =
104ca1c9b0cSelric "\x00\x00\x00\x00\x00\x00\x00\x00"
105ca1c9b0cSelric "\x00\x00\x00\x00\x00\x00\x00\x00";
106ca1c9b0cSelric const char cbc_iv[8] =
107ca1c9b0cSelric "\x01\x01\x01\x01\x01\x01\x01\x01";
108ca1c9b0cSelric const unsigned char cbc_in_data[32] =
109ca1c9b0cSelric "\x20\x20\x20\x20\x20\x20\x20\x20"
110ca1c9b0cSelric "\x20\x20\x20\x20\x20\x20\x20\x20"
111ca1c9b0cSelric "\x20\x20\x20\x20\x20\x20\x20\x20"
112ca1c9b0cSelric "\x20\x20\x20\x20\x20\x20\x20\x20";
113ca1c9b0cSelric 
114ca1c9b0cSelric const char out_iv[8] = "\x00\x78\x1b\x6\xff\xb9\xfa\xe";
115ca1c9b0cSelric 
116ca1c9b0cSelric const char cbc_out_data[32] =
117ca1c9b0cSelric "\xb4\x3f\x89\x15\x69\x68\xda\x79"
118ca1c9b0cSelric "\x29\xab\x5f\x78\xc5\xba\x15\x82"
119ca1c9b0cSelric "\x80\x89\x57\x1b\xbe\x57\x2f\xdc"
120ca1c9b0cSelric "\x00\x78\x1b\x06\xff\xb9\xfa\x0e";
121ca1c9b0cSelric 
122ca1c9b0cSelric int
main(int argc,char ** argv)123ca1c9b0cSelric main(int argc, char **argv)
124ca1c9b0cSelric {
125ca1c9b0cSelric     RC2_KEY key;
126ca1c9b0cSelric     unsigned char t[8];
127ca1c9b0cSelric     unsigned char out[40];
128ca1c9b0cSelric     int i;
129ca1c9b0cSelric 
130ca1c9b0cSelric     for (i = 0; i < sizeof(tests)/sizeof(tests[0]); i++) {
131ca1c9b0cSelric 	RC2_set_key(&key, tests[i].keylen, tests[i].key, tests[i].bitsize);
132ca1c9b0cSelric 
133ca1c9b0cSelric 	memcpy(t, tests[i].plain, 8);
134ca1c9b0cSelric 	RC2_encryptc(t, t, &key);
135ca1c9b0cSelric 	if (memcmp(t, tests[i].cipher, 8) != 0) {
136ca1c9b0cSelric 	    printf("encrypt %d\n", i);
137ca1c9b0cSelric 	    exit(1);
138ca1c9b0cSelric 	}
139ca1c9b0cSelric 	RC2_decryptc(t, t, &key);
140ca1c9b0cSelric 	if (memcmp(t, tests[i].plain, 8) != 0) {
141ca1c9b0cSelric 	    printf("decrypt: %d\n", i);
142ca1c9b0cSelric 	    exit(1);
143ca1c9b0cSelric 	}
144ca1c9b0cSelric     }
145ca1c9b0cSelric 
146ca1c9b0cSelric     /* cbc test */
147ca1c9b0cSelric 
148ca1c9b0cSelric     RC2_set_key(&key, 16, cbc_key, 0);
149ca1c9b0cSelric     memcpy(t, cbc_iv, 8);
150ca1c9b0cSelric     RC2_cbc_encrypt(cbc_in_data, out, 32, &key, t, 1);
151ca1c9b0cSelric 
152ca1c9b0cSelric     if (memcmp(out_iv, t, 8) != 0)
153ca1c9b0cSelric 	abort();
154ca1c9b0cSelric 
155ca1c9b0cSelric     if (memcmp(out, cbc_out_data, 32) != 0) {
156ca1c9b0cSelric 	printf("cbc test encrypt\n");
157ca1c9b0cSelric 	exit(1);
158ca1c9b0cSelric     }
159ca1c9b0cSelric 
160ca1c9b0cSelric     memcpy(t, cbc_iv, 8);
161ca1c9b0cSelric     RC2_cbc_encrypt(out, out, 32, &key, t, 0);
162ca1c9b0cSelric 
163ca1c9b0cSelric     if (memcmp(cbc_in_data, out, 32) != 0) {
164ca1c9b0cSelric 	printf("cbc test decrypt \n");
165ca1c9b0cSelric 	exit(1);
166ca1c9b0cSelric     }
167ca1c9b0cSelric 
168ca1c9b0cSelric     return 0;
169ca1c9b0cSelric }
170