Add (forgotten before).
[gnulib.git] / tests / test-rijndael.c
1 /*
2  * Copyright (C) 2005 Free Software Foundation
3  * Written by Simon Josefsson
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2, or (at your option)
8  * any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA.  */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <string.h>
26 #include "rijndael-api-fst.h"
27
28 int
29 main (int argc, char *argv[])
30 {
31   int rc;
32   rijndaelKeyInstance key;
33   rijndaelCipherInstance cipher;
34   char in[RIJNDAEL_BITSPERBLOCK / 8];
35   char out[RIJNDAEL_BITSPERBLOCK / 8];
36   char pt[] = "\x00\x00\x00\x00\x00\x00\x00\x00"
37     "\x00\x00\x00\x00\x00\x00\x00\x00";
38   char ct[] = "\xC3\x4C\x05\x2C\xC0\xDA\x8D\x73"
39     "\x45\x1A\xFE\x5F\x03\xBE\x29\x7F";
40   size_t i;
41
42   rc = rijndaelMakeKey (&key, RIJNDAEL_DIR_ENCRYPT,
43                         128, "00000000000000000000000000000000");
44   if (rc != 0)
45     printf ("makeKey failed %d\n", rc);
46
47   rc = rijndaelCipherInit (&cipher, RIJNDAEL_MODE_ECB, NULL);
48   if (rc != 0)
49     printf ("cipherInit failed %d\n", rc);
50
51   memset (in, 0, RIJNDAEL_BITSPERBLOCK / 8);
52
53   for (i = 0; i < 10000; i++)
54     {
55       rc = rijndaelBlockEncrypt (&cipher, &key, in, 128, out);
56       if (rc < 0)
57         printf ("blockEncrypt failed %d\n", rc);
58
59       memcpy (in, out, RIJNDAEL_BITSPERBLOCK / 8);
60     }
61
62   if (memcmp (out, ct, RIJNDAEL_BITSPERBLOCK / 8) != 0)
63     {
64       size_t i;
65       printf ("expected:\n");
66       for (i = 0; i < RIJNDAEL_BITSPERBLOCK / 8; i++)
67         printf ("%02x ", ct[i] & 0xFF);
68       printf ("\ncomputed:\n");
69       for (i = 0; i < RIJNDAEL_BITSPERBLOCK / 8; i++)
70         printf ("%02x ", out[i] & 0xFF);
71       printf ("\n");
72       return 1;
73     }
74
75   rc = rijndaelMakeKey (&key, RIJNDAEL_DIR_DECRYPT,
76                         128, "00000000000000000000000000000000");
77   if (rc != 0)
78     printf ("makeKey failed %d\n", rc);
79
80   rc = rijndaelCipherInit (&cipher, RIJNDAEL_MODE_ECB, NULL);
81   if (rc != 0)
82     printf ("cipherInit failed %d\n", rc);
83
84   for (i = 0; i < 10000; i++)
85     {
86       memcpy (in, out, RIJNDAEL_BITSPERBLOCK / 8);
87
88       rc = rijndaelBlockDecrypt (&cipher, &key, in, 128, out);
89       if (rc < 0)
90         printf ("blockEncrypt failed %d\n", rc);
91     }
92
93   if (memcmp (out, pt, RIJNDAEL_BITSPERBLOCK / 8) != 0)
94     {
95       size_t i;
96       printf ("expected:\n");
97       for (i = 0; i < RIJNDAEL_BITSPERBLOCK / 8; i++)
98         printf ("%02x ", pt[i] & 0xFF);
99       printf ("\ncomputed:\n");
100       for (i = 0; i < RIJNDAEL_BITSPERBLOCK / 8; i++)
101         printf ("%02x ", out[i] & 0xFF);
102       printf ("\n");
103       return 1;
104     }
105
106   return 0;
107 }