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