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