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