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