getlogin_r: Work around bug in Mac OS X 10.12.
[gnulib.git] / tests / test-gc-arcfour.c
blob2f805a331460c2411a5ef6b6e04031873e0d020b
1 /*
2 * Copyright (C) 2005, 2010-2017 Free Software Foundation, Inc.
3 * Written by Simon Josefsson
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>. */
18 #include <config.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include "gc.h"
24 int
25 main (int argc, char *argv[])
27 gc_cipher_handle ctx;
28 /* Test vector from Cryptlib via Libgcrypt labeled there: "from the
29 State/Commerce Department". */
30 static char key_1[] = { 0x61, 0x8A, 0x63, 0xD2, 0xFB };
31 static char plaintext_1[] = { 0xDC, 0xEE, 0x4C, 0xF9, 0x2C };
32 static const char ciphertext_1[] = { 0xF1, 0x38, 0x29, 0xC9, 0xDE };
33 char scratch[16];
34 Gc_rc rc;
36 rc = gc_init ();
37 if (rc != GC_OK)
39 printf ("gc_init() failed\n");
40 return 1;
43 rc = gc_cipher_open (GC_ARCFOUR40, GC_STREAM, &ctx);
44 if (rc != GC_OK)
45 return 1;
47 rc = gc_cipher_setkey (ctx, sizeof (key_1), key_1);
48 if (rc != GC_OK)
49 return 1;
51 memcpy (scratch, plaintext_1, sizeof (plaintext_1));
52 rc = gc_cipher_encrypt_inline (ctx, sizeof (plaintext_1), scratch);
53 if (rc != GC_OK)
54 return 1;
56 if (memcmp (scratch, ciphertext_1, sizeof (ciphertext_1)))
58 size_t i;
59 printf ("expected:\n");
60 for (i = 0; i < 5; i++)
61 printf ("%02x ", scratch[i] & 0xFF);
62 printf ("\ncomputed:\n");
63 for (i = 0; i < 5; i++)
64 printf ("%02x ", ciphertext_1[i] & 0xFF);
65 printf ("\n");
66 return 1;
69 /* decrypt */
71 rc = gc_cipher_setkey (ctx, sizeof (key_1), key_1);
72 if (rc != GC_OK)
73 return 1;
75 rc = gc_cipher_decrypt_inline (ctx, sizeof (plaintext_1), scratch);
76 if (rc != GC_OK)
77 return 1;
79 if (memcmp (scratch, plaintext_1, sizeof (plaintext_1)))
81 size_t i;
82 printf ("expected:\n");
83 for (i = 0; i < 5; i++)
84 printf ("%02x ", plaintext_1[i] & 0xFF);
85 printf ("\ncomputed:\n");
86 for (i = 0; i < 5; i++)
87 printf ("%02x ", scratch[i] & 0xFF);
88 printf ("\n");
89 return 1;
92 gc_done ();
94 return 0;