1 /*
2  * COPYRIGHT (c) International Business Machines Corp. 2011-2017
3  *
4  * This program is provided under the terms of the Common Public License,
5  * version 1.0 (CPL-1.0). Any use, reproduction or distribution for this
6  * software constitutes recipient's acceptance of CPL-1.0 terms which can be
7  * found in the file LICENSE file or at
8  * https://opensource.org/licenses/cpl1.0.php
9  */
10 
11 #include "pkcs11types.h"
12 
13 #define DES_KEY_SIZE 8
14 #define DES_IV_SIZE 8
15 #define MAX_TEXT_SIZE 8
16 #define DES_BLOCK_SIZE 8
17 #define MAX_CHUNKS 8
18 
19 unsigned char des_cbc_iv[] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef};
20 
21 struct CK_MECHANISM des_keygen = {
22     .mechanism = CKM_DES_KEY_GEN,
23     .ulParameterLen = 0,
24     .pParameter = NULL,
25 };
26 
27 struct des_test_vector {
28     unsigned char key[DES_KEY_SIZE];
29     unsigned char klen;
30     unsigned char iv[DES_IV_SIZE];
31     unsigned char ivlen;
32     unsigned char plaintext[MAX_TEXT_SIZE];
33     unsigned char plen;
34     unsigned char ciphertext[MAX_TEXT_SIZE];
35     unsigned char clen;
36     int chunks[MAX_CHUNKS];
37     int num_chunks;
38 };
39 
40 struct published_test_suite_info {
41     const char *name;
42     unsigned int tvcount;
43     struct des_test_vector *tv;
44     unsigned long mechanism;
45 };
46 
47 struct generated_test_suite_info {
48     const char *name;
49     CK_MECHANISM mech;
50 };
51 
52 /** FIPS PUB 81 - DES MODES OF OPERATION
53     http://www.itl.nist.gov/fipspubs/fip81.htm
54     Table B1 - AN EXAMPLE OF THE ELECTRONIC CODEBOOK (ECB) MODE
55 **/
56 static struct des_test_vector des_ecb_tv[] = {
57     {                           // 1
58         .key = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
59         .klen = 8,
60         .iv = {0},
61         .ivlen = 0,
62         .plaintext = {0x4e, 0x6f, 0x77, 0x20, 0x69, 0x73, 0x20, 0x74},
63         .plen = 8,
64         .ciphertext = {0x3f, 0xa4, 0x0e, 0x8a, 0x98, 0x4d, 0x48, 0x15},
65         .clen = 8
66     }, {              // 2
67         .key = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
68         .klen = 8,
69         .iv = {0},
70         .ivlen = 0,
71         .plaintext = {0x68, 0x65, 0x20, 0x74, 0x69, 0x6d, 0x65, 0x20},
72         .plen = 8,
73         .ciphertext = {0x6a, 0x27, 0x17, 0x87, 0xab, 0x88, 0x83, 0xf9},
74         .clen = 8,
75         .num_chunks = 3,
76         .chunks = {3, 0, 5},
77     }, {          // 3
78         .key = {0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef},
79         .klen = 8,
80         .iv = {0},
81         .ivlen = 0,
82         .plaintext = {0x66, 0x6f, 0x72, 0x20, 0x61, 0x6c, 0x6c, 0x20},
83         .plen = 8,
84         .ciphertext = {0x89, 0x3d, 0x51, 0xec, 0x4b, 0x56, 0x3b, 0x53},
85         .clen = 8,
86         .num_chunks = 3,
87         .chunks = {4, -1, 4},
88     },
89 };
90 
91 /** NIST Special Publication 800-17
92     http://csrc.nist.gov/publications/nistpubs/800-17/800-17.pdf
93     Appendix B - Variable Key Known Answer Test
94 **/
95 static struct des_test_vector des_cbc_tv[] = {
96     {                           // round 0
97         .key = {0x80, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
98         .klen = 8,
99         .iv = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
100         .ivlen = 8,
101         .plaintext = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
102         .plen = 8,
103         .ciphertext = {0x95, 0xA8, 0xD7, 0x28, 0x13, 0xDA, 0xA9, 0x4D},
104         .clen = 8,
105     }, {                       // round 1
106         .key = {0x40, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
107         .klen = 8,
108         .iv = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
109         .ivlen = 8,
110         .plaintext = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
111         .plen = 8,
112         .ciphertext = {0x0E, 0xEC, 0x14, 0x87, 0xDD, 0x8C, 0x26, 0xD5},
113         .clen = 8,
114         .num_chunks = 3,
115         .chunks = {3, 0, 5},
116     }, {                   // round 2
117         .key = {0x20, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01},
118         .klen = 8,
119         .iv = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
120         .ivlen = 8,
121         .plaintext = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
122         .plen = 8,
123         .ciphertext = {0x7A, 0xD1, 0x6F, 0xFB, 0x79, 0xC4, 0x59, 0x26},
124         .clen = 8,
125         .num_chunks = 3,
126         .chunks = {4, -1, 4},
127     },
128 };
129 
130 
131 #define NUM_OF_PUBLISHED_TESTSUITES	2
132 
133 struct published_test_suite_info published_test_suites[] = {
134     {
135         .name = "DES_ECB",
136         .tvcount = 3,
137         .tv = des_ecb_tv,
138         .mechanism = CKM_DES_ECB,
139     }, {
140         .name = "DES_CBC",
141         .tvcount = 3,
142         .tv = des_cbc_tv,
143         .mechanism = CKM_DES_CBC,
144     }
145 };
146 
147 #define NUM_OF_GENERATED_TESTSUITES 3
148 
149 static struct generated_test_suite_info generated_test_suites[] = {
150     {
151         .name = "DES_ECB",
152         .mech = {CKM_DES_ECB, 0, 0},
153     }, {
154         .name = "DES_CBC",
155         .mech = {CKM_DES_CBC, &des_cbc_iv, DES_IV_SIZE},
156     }, {
157         .name = "DES_CBC_PAD",
158         .mech = {CKM_DES_CBC_PAD, &des_cbc_iv, DES_IV_SIZE},
159     }
160 };
161