Unconditionally include <config.h> in unit tests.
[gnulib.git] / tests / test-gc-arcfour.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 #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_cipher_handle ctx;
30   /* Test vector from Cryptlib via Libgcrypt labeled there: "from the
31      State/Commerce Department". */
32   static char key_1[] = { 0x61, 0x8A, 0x63, 0xD2, 0xFB };
33   static char plaintext_1[] = { 0xDC, 0xEE, 0x4C, 0xF9, 0x2C };
34   static const char ciphertext_1[] = { 0xF1, 0x38, 0x29, 0xC9, 0xDE };
35   char scratch[16];
36   Gc_rc rc;
37
38   rc = gc_init ();
39   if (rc != GC_OK)
40     {
41       printf ("gc_init() failed\n");
42       return 1;
43     }
44
45   rc = gc_cipher_open (GC_ARCFOUR40, GC_STREAM, &ctx);
46   if (rc != GC_OK)
47     return 1;
48
49   rc = gc_cipher_setkey (ctx, sizeof (key_1), key_1);
50   if (rc != GC_OK)
51     return 1;
52
53   memcpy (scratch, plaintext_1, sizeof (plaintext_1));
54   rc = gc_cipher_encrypt_inline (ctx, sizeof (plaintext_1), scratch);
55   if (rc != GC_OK)
56     return 1;
57
58   if (memcmp (scratch, ciphertext_1, sizeof (ciphertext_1)))
59     {
60       size_t i;
61       printf ("expected:\n");
62       for (i = 0; i < 5; i++)
63         printf ("%02x ", scratch[i] & 0xFF);
64       printf ("\ncomputed:\n");
65       for (i = 0; i < 5; i++)
66         printf ("%02x ", ciphertext_1[i] & 0xFF);
67       printf ("\n");
68       return 1;
69     }
70
71   /* decrypt */
72
73   rc = gc_cipher_setkey (ctx, sizeof (key_1), key_1);
74   if (rc != GC_OK)
75     return 1;
76
77   rc = gc_cipher_decrypt_inline (ctx, sizeof (plaintext_1), scratch);
78   if (rc != GC_OK)
79     return 1;
80
81   if (memcmp (scratch, plaintext_1, sizeof (plaintext_1)))
82     {
83       size_t i;
84       printf ("expected:\n");
85       for (i = 0; i < 5; i++)
86         printf ("%02x ", plaintext_1[i] & 0xFF);
87       printf ("\ncomputed:\n");
88       for (i = 0; i < 5; i++)
89         printf ("%02x ", scratch[i] & 0xFF);
90       printf ("\n");
91       return 1;
92     }
93
94   gc_done ();
95
96   return 0;
97 }