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