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