Unconditionally include <config.h> in unit tests.
[pspp] / 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 #include <config.h>
21
22 #include <stdio.h>
23 #include <string.h>
24 #include "rijndael-api-fst.h"
25
26 int
27 main (int argc, char *argv[])
28 {
29   int rc;
30   rijndaelKeyInstance key;
31   rijndaelCipherInstance cipher;
32   char in[RIJNDAEL_BITSPERBLOCK / 8];
33   char out[RIJNDAEL_BITSPERBLOCK / 8];
34   char pt[] = "\x00\x00\x00\x00\x00\x00\x00\x00"
35     "\x00\x00\x00\x00\x00\x00\x00\x00";
36   char ct[] = "\xC3\x4C\x05\x2C\xC0\xDA\x8D\x73"
37     "\x45\x1A\xFE\x5F\x03\xBE\x29\x7F";
38   size_t i;
39
40   rc = rijndaelMakeKey (&key, RIJNDAEL_DIR_ENCRYPT,
41                         128, "00000000000000000000000000000000");
42   if (rc != 0)
43     printf ("makeKey failed %d\n", rc);
44
45   rc = rijndaelCipherInit (&cipher, RIJNDAEL_MODE_ECB, NULL);
46   if (rc != 0)
47     printf ("cipherInit failed %d\n", rc);
48
49   memset (in, 0, RIJNDAEL_BITSPERBLOCK / 8);
50
51   for (i = 0; i < 10000; i++)
52     {
53       rc = rijndaelBlockEncrypt (&cipher, &key, in, 128, out);
54       if (rc < 0)
55         printf ("blockEncrypt failed %d\n", rc);
56
57       memcpy (in, out, RIJNDAEL_BITSPERBLOCK / 8);
58     }
59
60   if (memcmp (out, ct, RIJNDAEL_BITSPERBLOCK / 8) != 0)
61     {
62       size_t i;
63       printf ("expected:\n");
64       for (i = 0; i < RIJNDAEL_BITSPERBLOCK / 8; i++)
65         printf ("%02x ", ct[i] & 0xFF);
66       printf ("\ncomputed:\n");
67       for (i = 0; i < RIJNDAEL_BITSPERBLOCK / 8; i++)
68         printf ("%02x ", out[i] & 0xFF);
69       printf ("\n");
70       return 1;
71     }
72
73   rc = rijndaelMakeKey (&key, RIJNDAEL_DIR_DECRYPT,
74                         128, "00000000000000000000000000000000");
75   if (rc != 0)
76     printf ("makeKey failed %d\n", rc);
77
78   rc = rijndaelCipherInit (&cipher, RIJNDAEL_MODE_ECB, NULL);
79   if (rc != 0)
80     printf ("cipherInit failed %d\n", rc);
81
82   for (i = 0; i < 10000; i++)
83     {
84       memcpy (in, out, RIJNDAEL_BITSPERBLOCK / 8);
85
86       rc = rijndaelBlockDecrypt (&cipher, &key, in, 128, out);
87       if (rc < 0)
88         printf ("blockEncrypt failed %d\n", rc);
89     }
90
91   if (memcmp (out, pt, RIJNDAEL_BITSPERBLOCK / 8) != 0)
92     {
93       size_t i;
94       printf ("expected:\n");
95       for (i = 0; i < RIJNDAEL_BITSPERBLOCK / 8; i++)
96         printf ("%02x ", pt[i] & 0xFF);
97       printf ("\ncomputed:\n");
98       for (i = 0; i < RIJNDAEL_BITSPERBLOCK / 8; i++)
99         printf ("%02x ", out[i] & 0xFF);
100       printf ("\n");
101       return 1;
102     }
103
104   return 0;
105 }