2 * Copyright (c) 2004 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
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.
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.
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
49 "\x00\x00\x00\x00\x00\x00\x00\x00"
50 "\x00\x00\x00\x00\x00\x00\x00\x00",
53 "\x00\x00\x00\x00\x00\x00\x00\x00",
54 "\x1C\x19\x8A\x83\x8D\xF0\x28\xB7"
57 "\x00\x00\x00\x00\x00\x00\x00\x00"
58 "\x00\x00\x00\x00\x00\x00\x00\x01",
61 "\x00\x00\x00\x00\x00\x00\x00\x00",
62 "\x21\x82\x9C\x78\xA9\xF9\xC0\x74"
65 "\x00\x00\x00\x00\x00\x00\x00\x00"
66 "\x00\x00\x00\x00\x00\x00\x00\x00",
69 "\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
70 "\x13\xDB\x35\x17\xD3\x21\x86\x9E"
73 "\x00\x01\x02\x03\x04\x05\x06\x07"
74 "\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F",
77 "\x00\x00\x00\x00\x00\x00\x00\x00",
78 "\x50\xDC\x01\x62\xBD\x75\x7F\x31"
81 "\x00\x00\x00\x00\x00\x00\x00\x00",
84 "\x00\x00\x00\x00\x00\x00\x00\x00",
85 "\xeb\xb7\x73\xf9\x93\x27\x8e\xff"
88 "\xff\xff\xff\xff\xff\xff\xff\xff",
91 "\xff\xff\xff\xff\xff\xff\xff\xff",
92 "\x27\x8b\x27\xe4\x2e\x2f\x0d\x49"
98 "\x00\x00\x00\x00\x00\x00\x00\x00",
99 "\x61\xa8\xa2\x44\xad\xac\xcc\xf0"
103 const unsigned char cbc_key
[16] =
104 "\x00\x00\x00\x00\x00\x00\x00\x00"
105 "\x00\x00\x00\x00\x00\x00\x00\x00";
106 const char cbc_iv
[8] =
107 "\x01\x01\x01\x01\x01\x01\x01\x01";
108 const 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";
114 const char out_iv
[8] = "\x00\x78\x1b\x6\xff\xb9\xfa\xe";
116 const 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";
123 main(int argc
, char **argv
)
127 unsigned char out
[40];
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
);
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
);
139 RC2_decryptc(t
, t
, &key
);
140 if (memcmp(t
, tests
[i
].plain
, 8) != 0) {
141 printf("decrypt: %d\n", i
);
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);
152 if (memcmp(out_iv
, t
, 8) != 0)
155 if (memcmp(out
, cbc_out_data
, 32) != 0) {
156 printf("cbc test encrypt\n");
160 memcpy(t
, cbc_iv
, 8);
161 RC2_cbc_encrypt(out
, out
, 32, &key
, t
, 0);
163 if (memcmp(cbc_in_data
, out
, 32) != 0) {
164 printf("cbc test decrypt \n");