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