revert unwanted commit
[gnulib.git] / tests / test-gc-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 "gc.h"
27
28 int
29 main (int argc, char *argv[])
30 {
31   Gc_rc rc;
32
33   rc = gc_init ();
34   if (rc != GC_OK)
35     {
36       printf ("gc_init() failed\n");
37       return 1;
38     }
39
40   {
41     char buf[16];
42     char key[] = "\x00\x00\x00\x00\x00\x00\x00\x00"
43       "\x00\x00\x00\x00\x00\x00\x00\x00";
44     char pt[] = "\x00\x00\x00\x00\x00\x00\x00\x00"
45       "\x00\x00\x00\x00\x00\x00\x00\x00";
46     char ct[] = "\xC3\x4C\x05\x2C\xC0\xDA\x8D\x73"
47       "\x45\x1A\xFE\x5F\x03\xBE\x29\x7F";
48     gc_cipher_handle ctx;
49     size_t i;
50
51     rc = gc_cipher_open (GC_AES128, GC_ECB, &ctx);
52     if (rc != GC_OK)
53       return 1;
54
55     rc = gc_cipher_setkey (ctx, 16, key);
56     if (rc != GC_OK)
57       return 1;
58
59     memcpy (buf, pt, 16);
60
61     for (i = 0; i < 10000; i++)
62       {
63         rc = gc_cipher_encrypt_inline (ctx, 16, buf);
64         if (rc != GC_OK)
65           {
66             printf ("encrypt failed %d\n", rc);
67             return 1;
68           }
69       }
70
71     if (memcmp (buf, ct, 16) != 0)
72       {
73         size_t i;
74         printf ("expected:\n");
75         for (i = 0; i < 16; i++)
76           printf ("%02x ", ct[i] & 0xFF);
77         printf ("\ncomputed:\n");
78         for (i = 0; i < 16; i++)
79           printf ("%02x ", buf[i] & 0xFF);
80         printf ("\n");
81         return 1;
82       }
83
84     for (i = 0; i < 10000; i++)
85       {
86         rc = gc_cipher_decrypt_inline (ctx, 16, buf);
87         if (rc != GC_OK)
88           {
89             printf ("decrypt failed %d\n", rc);
90             return 1;
91           }
92       }
93
94     if (memcmp (buf, pt, 16) != 0)
95       {
96         size_t i;
97         printf ("expected:\n");
98         for (i = 0; i < 16; i++)
99           printf ("%02x ", pt[i] & 0xFF);
100         printf ("\ncomputed:\n");
101         for (i = 0; i < 16; i++)
102           printf ("%02x ", buf[i] & 0xFF);
103         printf ("\n");
104         return 1;
105       }
106
107     gc_cipher_close (ctx);
108   }
109
110
111   {
112     char buf[16];
113     char iv[] = "\x00\x00\x00\x00\x00\x00\x00\x00"
114       "\x00\x00\x00\x00\x00\x00\x00\x00";
115     char key[] = "\x00\x00\x00\x00\x00\x00\x00\x00"
116       "\x00\x00\x00\x00\x00\x00\x00\x00";
117     char pt[] = "\x00\x00\x00\x00\x00\x00\x00\x00"
118       "\x00\x00\x00\x00\x00\x00\x00\x00";
119     char ct[] = "\x66\xe9\x4b\xd4\xef\x8a\x2c\x3b"
120       "\x88\x4c\xfa\x59\xca\x34\x2b\x2e";
121     gc_cipher_handle ctx;
122     size_t i;
123
124     rc = gc_cipher_open (GC_AES128, GC_CBC, &ctx);
125     if (rc != GC_OK)
126       return 1;
127
128     rc = gc_cipher_setkey (ctx, 16, key);
129     if (rc != GC_OK)
130       return 1;
131
132     rc = gc_cipher_setiv (ctx, 16, iv);
133     if (rc != GC_OK)
134       return 1;
135
136     memcpy (buf, pt, 16);
137
138     for (i = 0; i < 10000; i++)
139       {
140         rc = gc_cipher_encrypt_inline (ctx, 16, buf);
141         if (rc != GC_OK)
142           {
143             printf ("encrypt failed %d\n", rc);
144             return 1;
145           }
146       }
147
148     if (memcmp (buf, ct, 16) != 0)
149       {
150         size_t i;
151         printf ("expected:\n");
152         for (i = 0; i < 16; i++)
153           printf ("%02x ", ct[i] & 0xFF);
154         printf ("\ncomputed:\n");
155         for (i = 0; i < 16; i++)
156           printf ("%02x ", buf[i] & 0xFF);
157         printf ("\n");
158         return 1;
159       }
160
161     gc_cipher_close (ctx);
162   }
163
164   gc_done ();
165
166   return 0;
167 }