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