1 /*********************************************************************
2 * Filename: des_test.c
3 * Author: Brad Conte (brad AT bradconte.com)
4 * Copyright:
5 * Disclaimer: This code is presented "as is" without any guarantees.
6 * Details: Performs known-answer tests on the corresponding DES
7 implementation. These tests do not encompass the full
8 range of available test vectors, however, if the tests
9 pass it is very, very likely that the code is correct
10 and was compiled properly. This code also serves as
11 example usage of the functions.
12 *********************************************************************/
13
14 /*************************** HEADER FILES ***************************/
15 #include <stdio.h>
16 #include <memory.h>
17 #include "des.h"
18
19 /*********************** FUNCTION DEFINITIONS ***********************/
des_test()20 int des_test()
21 {
22 BYTE pt1[DES_BLOCK_SIZE] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xE7};
23 BYTE pt2[DES_BLOCK_SIZE] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF};
24 BYTE pt3[DES_BLOCK_SIZE] = {0x54,0x68,0x65,0x20,0x71,0x75,0x66,0x63};
25 BYTE ct1[DES_BLOCK_SIZE] = {0xc9,0x57,0x44,0x25,0x6a,0x5e,0xd3,0x1d};
26 BYTE ct2[DES_BLOCK_SIZE] = {0x85,0xe8,0x13,0x54,0x0f,0x0a,0xb4,0x05};
27 BYTE ct3[DES_BLOCK_SIZE] = {0xc9,0x57,0x44,0x25,0x6a,0x5e,0xd3,0x1d};
28 BYTE ct4[DES_BLOCK_SIZE] = {0xA8,0x26,0xFD,0x8C,0xE5,0x3B,0x85,0x5F};
29 BYTE key1[DES_BLOCK_SIZE] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF};
30 BYTE key2[DES_BLOCK_SIZE] = {0x13,0x34,0x57,0x79,0x9B,0xBC,0xDF,0xF1};
31 BYTE three_key1[DES_BLOCK_SIZE * 3] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,
32 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,
33 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF};
34 BYTE three_key2[DES_BLOCK_SIZE * 3] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,
35 0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,0x01,
36 0x45,0x67,0x89,0xAB,0xCD,0xEF,0x01,0x23};
37
38 BYTE schedule[16][6];
39 BYTE three_schedule[3][16][6];
40 BYTE buf[DES_BLOCK_SIZE];
41 int pass = 1;
42
43 des_key_setup(key1, schedule, DES_ENCRYPT);
44 des_crypt(pt1, buf, schedule);
45 pass = pass && !memcmp(ct1, buf, DES_BLOCK_SIZE);
46
47 des_key_setup(key1, schedule, DES_DECRYPT);
48 des_crypt(ct1, buf, schedule);
49 pass = pass && !memcmp(pt1, buf, DES_BLOCK_SIZE);
50
51 des_key_setup(key2, schedule, DES_ENCRYPT);
52 des_crypt(pt2, buf, schedule);
53 pass = pass && !memcmp(ct2, buf, DES_BLOCK_SIZE);
54
55 des_key_setup(key2, schedule, DES_DECRYPT);
56 des_crypt(ct2, buf, schedule);
57 pass = pass && !memcmp(pt2, buf, DES_BLOCK_SIZE);
58
59 three_des_key_setup(three_key1, three_schedule, DES_ENCRYPT);
60 three_des_crypt(pt1, buf, three_schedule);
61 pass = pass && !memcmp(ct3, buf, DES_BLOCK_SIZE);
62
63 three_des_key_setup(three_key1, three_schedule, DES_DECRYPT);
64 three_des_crypt(ct3, buf, three_schedule);
65 pass = pass && !memcmp(pt1, buf, DES_BLOCK_SIZE);
66
67 three_des_key_setup(three_key2, three_schedule, DES_ENCRYPT);
68 three_des_crypt(pt3, buf, three_schedule);
69 pass = pass && !memcmp(ct4, buf, DES_BLOCK_SIZE);
70
71 three_des_key_setup(three_key2, three_schedule, DES_DECRYPT);
72 three_des_crypt(ct4, buf, three_schedule);
73 pass = pass && !memcmp(pt3, buf, DES_BLOCK_SIZE);
74
75 return(pass);
76 }
77
main()78 int main()
79 {
80 printf("DES test: %s\n", des_test() ? "SUCCEEDED" : "FAILED");
81
82 return(0);
83 }
84