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