New module 'time', so that apps can include <time.h> as per
[gnulib.git] / tests / test-gc-des.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_cipher_handle ctx;
32   Gc_rc rc;
33
34   rc = gc_init ();
35   if (rc != GC_OK)
36     {
37       printf ("gc_init() failed\n");
38       return 1;
39     }
40
41   /*
42    * DES Maintenance Test
43    */
44   {
45     int i;
46     char key[8] = { 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55 };
47     char input[8] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
48     char result[8] = { 0x24, 0x6e, 0x9d, 0xb9, 0xc5, 0x50, 0x38, 0x1a };
49     char temp1[8], temp2[8], temp3[8];
50
51     for (i = 0; i < 64; ++i)
52       {
53
54         rc = gc_cipher_open (GC_DES, GC_ECB, &ctx);
55         if (rc != GC_OK)
56           return 1;
57
58         rc = gc_cipher_setkey (ctx, 8, key);
59         if (rc != GC_OK)
60           return 1;
61
62         memcpy (temp1, input, 8);
63         rc = gc_cipher_encrypt_inline (ctx, 8, temp1);
64         if (rc != GC_OK)
65           return 1;
66
67         memcpy (temp2, temp1, 8);
68         rc = gc_cipher_encrypt_inline (ctx, 8, temp2);
69         if (rc != GC_OK)
70           return 1;
71
72         rc = gc_cipher_setkey (ctx, 8, temp2);
73         if (rc != GC_OK)
74           return 1;
75
76         memcpy (temp3, temp1, 8);
77         rc = gc_cipher_decrypt_inline (ctx, 8, temp3);
78         if (rc != GC_OK)
79           return 1;
80
81         memcpy (key, temp3, 8);
82         memcpy (input, temp1, 8);
83       }
84     if (memcmp (temp3, result, 8))
85       return 1;
86   }
87
88   gc_done ();
89
90   return 0;
91 }