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