malloca: Silence a warning from clang's memory sanitizer.
[gnulib.git] / tests / test-rijndael.c
blobd5c47d7204a688186fc05bd8e78c01bcbcf21d48
1 /*
2 * Copyright (C) 2005, 2010-2017 Free Software Foundation, Inc.
3 * Written by Simon Josefsson
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.
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.
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/>. */
18 #include <config.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include "rijndael-api-fst.h"
24 int
25 main (int argc, char *argv[])
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;
38 rc = rijndaelMakeKey (&key, RIJNDAEL_DIR_ENCRYPT,
39 128, "00000000000000000000000000000000");
40 if (rc != 0)
41 printf ("makeKey failed %d\n", rc);
43 rc = rijndaelCipherInit (&cipher, RIJNDAEL_MODE_ECB, NULL);
44 if (rc != 0)
45 printf ("cipherInit failed %d\n", rc);
47 memset (in, 0, RIJNDAEL_BITSPERBLOCK / 8);
49 for (i = 0; i < 10000; i++)
51 rc = rijndaelBlockEncrypt (&cipher, &key, in, 128, out);
52 if (rc < 0)
53 printf ("blockEncrypt failed %d\n", rc);
55 memcpy (in, out, RIJNDAEL_BITSPERBLOCK / 8);
58 if (memcmp (out, ct, RIJNDAEL_BITSPERBLOCK / 8) != 0)
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;
71 rc = rijndaelMakeKey (&key, RIJNDAEL_DIR_DECRYPT,
72 128, "00000000000000000000000000000000");
73 if (rc != 0)
74 printf ("makeKey failed %d\n", rc);
76 rc = rijndaelCipherInit (&cipher, RIJNDAEL_MODE_ECB, NULL);
77 if (rc != 0)
78 printf ("cipherInit failed %d\n", rc);
80 for (i = 0; i < 10000; i++)
82 memcpy (in, out, RIJNDAEL_BITSPERBLOCK / 8);
84 rc = rijndaelBlockDecrypt (&cipher, &key, in, 128, out);
85 if (rc < 0)
86 printf ("blockEncrypt failed %d\n", rc);
89 if (memcmp (out, pt, RIJNDAEL_BITSPERBLOCK / 8) != 0)
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;
102 return 0;