cfb8: Fix decrypt path
[gd/nettle] / testsuite / des-test.c
1 #include "testutils.h"
2 #include "nettle-internal.h"
3 #include "des.h"
4
5 static void
6 test_des(const struct tstring *key, int expected_parity,
7          const struct tstring *cleartext,
8          const struct tstring *ciphertext)
9 {
10   struct des_ctx ctx;
11   uint8_t *data;
12   size_t length;
13
14   ASSERT (cleartext->length == ciphertext->length);
15   length = cleartext->length;
16
17   ASSERT (key->length == DES_KEY_SIZE);
18   
19   data = xalloc(length);
20
21   ASSERT (des_check_parity(8, key->data) == expected_parity);
22
23   ASSERT (des_set_key(&ctx, key->data));
24
25   des_encrypt(&ctx, length, data, cleartext->data);
26
27   if (!MEMEQ(length, data, ciphertext->data))
28     {
29       fprintf(stderr, "Encrypt failed:\nInput:");
30       tstring_print_hex(cleartext);
31       fprintf(stderr, "\nOutput: ");
32       print_hex(length, data);
33       fprintf(stderr, "\nExpected:");
34       tstring_print_hex(ciphertext);
35       fprintf(stderr, "\n");
36       FAIL();
37     }
38
39   des_decrypt(&ctx, length, data, data);
40
41   if (!MEMEQ(length, data, cleartext->data))
42     {
43       fprintf(stderr, "Decrypt failed:\nInput:");
44       tstring_print_hex(ciphertext);
45       fprintf(stderr, "\nOutput: ");
46       print_hex(length, data);
47       fprintf(stderr, "\nExpected:");
48       tstring_print_hex(cleartext);
49       fprintf(stderr, "\n");
50       FAIL();
51     }
52
53   free(data);
54 }
55
56 static void
57 test_weak(const struct tstring *key)
58 {
59   struct des_ctx ctx;
60
61   ASSERT (key->length == DES_KEY_SIZE);
62   ASSERT (des_set_key(&ctx, key->data) == 0);
63 }
64
65 void
66 test_main(void)
67 {
68   /* From Applied Cryptography */
69   test_des(SHEX("01234567 89ABCDEF"), 1,
70            SHEX("01234567 89ABCDE7"),
71            SHEX("C9574425 6A5ED31D"));
72
73   test_des(SHEX("01 01 01 01 01 01 01 80"), 1,
74            SHEX("00 00 00 00 00 00 00 00"),
75            SHEX("9C C6 2D F4 3B 6E ED 74"));
76
77   test_des(SHEX("80 01 01 01 01 01 01 01"), 1,
78            SHEX("00 00 00 00 00 00 00 40"),
79            SHEX("A3 80 E0 2A 6B E5 46 96"));
80
81   test_des(SHEX("08 19 2A 3B 4C 5D 6E 7F"), 1,
82            SHEX("00 00 00 00 00 00 00 00"),
83            SHEX("25 DD AC 3E 96 17 64 67"));
84
85   test_des(SHEX("01 23 45 67 89 AB CD EF"), 1,
86            SDATA("Now is t"),
87            SHEX("3F A4 0E 8A 98 4D 48 15"));
88
89   /* Same key, but with one bad parity bit, */
90   test_des(SHEX("01 23 45 66 89 AB CD EF"), 0,
91            SDATA("Now is t"),
92            SHEX("3F A4 0E 8A 98 4D 48 15"));
93
94   /* Parity check */
95   {
96     const struct tstring *s = SHEX("01 01 01 01 01 01 01 00");
97     ASSERT (des_check_parity(s->length, s->data) == 0);
98   }
99
100   /* The four weak keys */
101   test_weak(SHEX("01 01 01 01 01 01 01 01"));  
102   test_weak(SHEX("FE FE FE FE FE FE FE FE"));
103   test_weak(SHEX("1F 1F 1F 1F 0E 0E 0E 0E"));
104   test_weak(SHEX("E0 E0 E0 E0 F1 F1 F1 F1"));
105
106   /* Same weak key, but different parity. */
107   test_weak(SHEX("E0 E0 E0 E0 F0 F1 F1 F1"));
108
109   /* The six pairs of semiweak keys */
110   test_weak(SHEX("01 FE 01 FE 01 FE 01 FE"));
111   test_weak(SHEX("FE 01 FE 01 FE 01 FE 01"));
112
113   test_weak(SHEX("1F E0 1F E0 0E F1 0E F1"));
114   test_weak(SHEX("E0 1F E0 1F F1 0E F1 0E"));
115
116   test_weak(SHEX("01 E0 01 E0 01 F1 01 F1"));
117   test_weak(SHEX("E0 01 E0 01 F1 01 F1 01"));
118
119   test_weak(SHEX("1F FE 1F FE 0E FE 0E FE"));
120   test_weak(SHEX("FE 1F FE 1F FE 0E FE 0E"));
121
122   test_weak(SHEX("01 1F 01 1F 01 0E 01 0E"));
123   test_weak(SHEX("1F 01 1F 01 0E 01 0E 01"));
124
125   test_weak(SHEX("E0 FE E0 FE F1 FE F1 FE"));
126   test_weak(SHEX("FE E0 FE E0 FE F1 FE F1"));
127 }