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