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