blob: b846d777a0538314c3045b33888357f5d72aeef6 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/des.h"
Paul Bakker33b43f12013-08-20 11:48:36 +02003/* END_HEADER */
Paul Bakkere896fea2009-07-06 06:40:23 +00004
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006 * depends_on:MBEDTLS_DES_C
Paul Bakker33b43f12013-08-20 11:48:36 +02007 * END_DEPENDENCIES
8 */
Paul Bakker5690efc2011-05-26 13:16:06 +00009
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010011void des_check_weak(data_t *key, int ret)
Manuel Pégourié-Gonnard9ce7e842014-03-29 17:06:43 +010012{
Gilles Peskine449bd832023-01-11 14:50:10 +010013 TEST_ASSERT(mbedtls_des_key_check_weak(key->x) == ret);
Manuel Pégourié-Gonnard9ce7e842014-03-29 17:06:43 +010014}
15/* END_CASE */
16
17/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010018void des_encrypt_ecb(data_t *key_str, data_t *src_str, data_t *dst)
Paul Bakkere896fea2009-07-06 06:40:23 +000019{
Paul Bakkere896fea2009-07-06 06:40:23 +000020 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021 mbedtls_des_context ctx;
Paul Bakkere896fea2009-07-06 06:40:23 +000022
Paul Bakkere896fea2009-07-06 06:40:23 +000023 memset(output, 0x00, 100);
Gilles Peskine449bd832023-01-11 14:50:10 +010024 mbedtls_des_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +000025
Paul Bakkere896fea2009-07-06 06:40:23 +000026
Gilles Peskine449bd832023-01-11 14:50:10 +010027 TEST_ASSERT(mbedtls_des_setkey_enc(&ctx, key_str->x) == 0);
28 TEST_ASSERT(mbedtls_des_crypt_ecb(&ctx, src_str->x, output) == 0);
Paul Bakkere896fea2009-07-06 06:40:23 +000029
Gilles Peskine449bd832023-01-11 14:50:10 +010030 TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 8, dst->len) == 0);
Paul Bakker8cfd9d82014-06-18 11:16:11 +020031
Paul Bakkerbd51b262014-07-10 15:26:12 +020032exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010033 mbedtls_des_free(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +000034}
Paul Bakker33b43f12013-08-20 11:48:36 +020035/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000036
Paul Bakker33b43f12013-08-20 11:48:36 +020037/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010038void des_decrypt_ecb(data_t *key_str, data_t *src_str, data_t *dst)
Paul Bakkere896fea2009-07-06 06:40:23 +000039{
Paul Bakkere896fea2009-07-06 06:40:23 +000040 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020041 mbedtls_des_context ctx;
Paul Bakkere896fea2009-07-06 06:40:23 +000042
Paul Bakkere896fea2009-07-06 06:40:23 +000043 memset(output, 0x00, 100);
Gilles Peskine449bd832023-01-11 14:50:10 +010044 mbedtls_des_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +000045
Paul Bakkere896fea2009-07-06 06:40:23 +000046
Gilles Peskine449bd832023-01-11 14:50:10 +010047 TEST_ASSERT(mbedtls_des_setkey_dec(&ctx, key_str->x) == 0);
48 TEST_ASSERT(mbedtls_des_crypt_ecb(&ctx, src_str->x, output) == 0);
Paul Bakkere896fea2009-07-06 06:40:23 +000049
Gilles Peskine449bd832023-01-11 14:50:10 +010050 TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 8, dst->len) == 0);
Paul Bakker8cfd9d82014-06-18 11:16:11 +020051
Paul Bakkerbd51b262014-07-10 15:26:12 +020052exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010053 mbedtls_des_free(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +000054}
Paul Bakker33b43f12013-08-20 11:48:36 +020055/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000056
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020057/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Gilles Peskine449bd832023-01-11 14:50:10 +010058void des_encrypt_cbc(data_t *key_str, data_t *iv_str,
59 data_t *src_str, data_t *dst, int cbc_result)
Paul Bakkere896fea2009-07-06 06:40:23 +000060{
Paul Bakkere896fea2009-07-06 06:40:23 +000061 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062 mbedtls_des_context ctx;
Paul Bakkere896fea2009-07-06 06:40:23 +000063
Paul Bakkere896fea2009-07-06 06:40:23 +000064 memset(output, 0x00, 100);
Gilles Peskine449bd832023-01-11 14:50:10 +010065 mbedtls_des_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +000066
Paul Bakkere896fea2009-07-06 06:40:23 +000067
Gilles Peskine449bd832023-01-11 14:50:10 +010068 TEST_ASSERT(mbedtls_des_setkey_enc(&ctx, key_str->x) == 0);
69 TEST_ASSERT(mbedtls_des_crypt_cbc(&ctx, MBEDTLS_DES_ENCRYPT, src_str->len, iv_str->x,
70 src_str->x, output) == cbc_result);
71 if (cbc_result == 0) {
Paul Bakkere896fea2009-07-06 06:40:23 +000072
Gilles Peskine449bd832023-01-11 14:50:10 +010073 TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, src_str->len,
74 dst->len) == 0);
Paul Bakkerf3ccc682010-03-18 21:21:02 +000075 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020076
Paul Bakkerbd51b262014-07-10 15:26:12 +020077exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010078 mbedtls_des_free(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +000079}
Paul Bakker33b43f12013-08-20 11:48:36 +020080/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000081
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Gilles Peskine449bd832023-01-11 14:50:10 +010083void des_decrypt_cbc(data_t *key_str, data_t *iv_str,
84 data_t *src_str, data_t *dst,
85 int cbc_result)
Paul Bakkere896fea2009-07-06 06:40:23 +000086{
Paul Bakkere896fea2009-07-06 06:40:23 +000087 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_des_context ctx;
Paul Bakkere896fea2009-07-06 06:40:23 +000089
Paul Bakkere896fea2009-07-06 06:40:23 +000090 memset(output, 0x00, 100);
Gilles Peskine449bd832023-01-11 14:50:10 +010091 mbedtls_des_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +000092
Paul Bakkere896fea2009-07-06 06:40:23 +000093
Gilles Peskine449bd832023-01-11 14:50:10 +010094 TEST_ASSERT(mbedtls_des_setkey_dec(&ctx, key_str->x) == 0);
95 TEST_ASSERT(mbedtls_des_crypt_cbc(&ctx, MBEDTLS_DES_DECRYPT, src_str->len, iv_str->x,
96 src_str->x, output) == cbc_result);
97 if (cbc_result == 0) {
Paul Bakkere896fea2009-07-06 06:40:23 +000098
Gilles Peskine449bd832023-01-11 14:50:10 +010099 TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, src_str->len,
100 dst->len) == 0);
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000101 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200102
Paul Bakkerbd51b262014-07-10 15:26:12 +0200103exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 mbedtls_des_free(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +0000105}
Paul Bakker33b43f12013-08-20 11:48:36 +0200106/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000107
Paul Bakker33b43f12013-08-20 11:48:36 +0200108/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100109void des3_encrypt_ecb(int key_count, data_t *key_str,
110 data_t *src_str, data_t *dst)
Paul Bakkere896fea2009-07-06 06:40:23 +0000111{
Paul Bakkere896fea2009-07-06 06:40:23 +0000112 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200113 mbedtls_des3_context ctx;
Paul Bakkere896fea2009-07-06 06:40:23 +0000114
Paul Bakkere896fea2009-07-06 06:40:23 +0000115 memset(output, 0x00, 100);
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_des3_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +0000117
Paul Bakkere896fea2009-07-06 06:40:23 +0000118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if (key_count == 2) {
120 TEST_ASSERT(mbedtls_des3_set2key_enc(&ctx, key_str->x) == 0);
121 } else if (key_count == 3) {
122 TEST_ASSERT(mbedtls_des3_set3key_enc(&ctx, key_str->x) == 0);
123 } else {
124 TEST_ASSERT(0);
125 }
Paul Bakkere896fea2009-07-06 06:40:23 +0000126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 TEST_ASSERT(mbedtls_des3_crypt_ecb(&ctx, src_str->x, output) == 0);
Paul Bakkere896fea2009-07-06 06:40:23 +0000128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 8, dst->len) == 0);
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200130
Paul Bakkerbd51b262014-07-10 15:26:12 +0200131exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 mbedtls_des3_free(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +0000133}
Paul Bakker33b43f12013-08-20 11:48:36 +0200134/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000135
Paul Bakker33b43f12013-08-20 11:48:36 +0200136/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100137void des3_decrypt_ecb(int key_count, data_t *key_str,
138 data_t *src_str, data_t *dst)
Paul Bakkere896fea2009-07-06 06:40:23 +0000139{
Paul Bakkere896fea2009-07-06 06:40:23 +0000140 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_des3_context ctx;
Paul Bakkere896fea2009-07-06 06:40:23 +0000142
Paul Bakkere896fea2009-07-06 06:40:23 +0000143 memset(output, 0x00, 100);
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 mbedtls_des3_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +0000145
Paul Bakkere896fea2009-07-06 06:40:23 +0000146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 if (key_count == 2) {
148 TEST_ASSERT(mbedtls_des3_set2key_dec(&ctx, key_str->x) == 0);
149 } else if (key_count == 3) {
150 TEST_ASSERT(mbedtls_des3_set3key_dec(&ctx, key_str->x) == 0);
151 } else {
152 TEST_ASSERT(0);
153 }
Paul Bakkere896fea2009-07-06 06:40:23 +0000154
Gilles Peskine449bd832023-01-11 14:50:10 +0100155 TEST_ASSERT(mbedtls_des3_crypt_ecb(&ctx, src_str->x, output) == 0);
Paul Bakkere896fea2009-07-06 06:40:23 +0000156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 8, dst->len) == 0);
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200158
Paul Bakkerbd51b262014-07-10 15:26:12 +0200159exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 mbedtls_des3_free(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +0000161}
Paul Bakker33b43f12013-08-20 11:48:36 +0200162/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000163
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200164/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Gilles Peskine449bd832023-01-11 14:50:10 +0100165void des3_encrypt_cbc(int key_count, data_t *key_str,
166 data_t *iv_str, data_t *src_str,
167 data_t *dst, int cbc_result)
Paul Bakkere896fea2009-07-06 06:40:23 +0000168{
Paul Bakkere896fea2009-07-06 06:40:23 +0000169 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_des3_context ctx;
Paul Bakkere896fea2009-07-06 06:40:23 +0000171
Paul Bakkere896fea2009-07-06 06:40:23 +0000172 memset(output, 0x00, 100);
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 mbedtls_des3_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +0000174
Paul Bakkere896fea2009-07-06 06:40:23 +0000175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 if (key_count == 2) {
177 TEST_ASSERT(mbedtls_des3_set2key_enc(&ctx, key_str->x) == 0);
178 } else if (key_count == 3) {
179 TEST_ASSERT(mbedtls_des3_set3key_enc(&ctx, key_str->x) == 0);
180 } else {
181 TEST_ASSERT(0);
182 }
Paul Bakkere896fea2009-07-06 06:40:23 +0000183
Gilles Peskine449bd832023-01-11 14:50:10 +0100184 TEST_ASSERT(mbedtls_des3_crypt_cbc(&ctx, MBEDTLS_DES_ENCRYPT, src_str->len, iv_str->x,
185 src_str->x, output) == cbc_result);
Paul Bakker02722ea2011-05-25 11:34:44 +0000186
Gilles Peskine449bd832023-01-11 14:50:10 +0100187 if (cbc_result == 0) {
Paul Bakkere896fea2009-07-06 06:40:23 +0000188
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x,
190 src_str->len, dst->len) == 0);
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000191 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200192
Paul Bakkerbd51b262014-07-10 15:26:12 +0200193exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100194 mbedtls_des3_free(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +0000195}
Paul Bakker33b43f12013-08-20 11:48:36 +0200196/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Gilles Peskine449bd832023-01-11 14:50:10 +0100199void des3_decrypt_cbc(int key_count, data_t *key_str,
200 data_t *iv_str, data_t *src_str,
201 data_t *dst, int cbc_result)
Paul Bakkere896fea2009-07-06 06:40:23 +0000202{
Paul Bakkere896fea2009-07-06 06:40:23 +0000203 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200204 mbedtls_des3_context ctx;
Paul Bakkere896fea2009-07-06 06:40:23 +0000205
Paul Bakkere896fea2009-07-06 06:40:23 +0000206 memset(output, 0x00, 100);
Gilles Peskine449bd832023-01-11 14:50:10 +0100207 mbedtls_des3_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +0000208
Paul Bakkere896fea2009-07-06 06:40:23 +0000209
Gilles Peskine449bd832023-01-11 14:50:10 +0100210 if (key_count == 2) {
211 TEST_ASSERT(mbedtls_des3_set2key_dec(&ctx, key_str->x) == 0);
212 } else if (key_count == 3) {
213 TEST_ASSERT(mbedtls_des3_set3key_dec(&ctx, key_str->x) == 0);
214 } else {
215 TEST_ASSERT(0);
216 }
Paul Bakkere896fea2009-07-06 06:40:23 +0000217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 TEST_ASSERT(mbedtls_des3_crypt_cbc(&ctx, MBEDTLS_DES_DECRYPT, src_str->len, iv_str->x,
219 src_str->x, output) == cbc_result);
Paul Bakker02722ea2011-05-25 11:34:44 +0000220
Gilles Peskine449bd832023-01-11 14:50:10 +0100221 if (cbc_result == 0) {
Paul Bakkere896fea2009-07-06 06:40:23 +0000222
Gilles Peskine449bd832023-01-11 14:50:10 +0100223 TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, src_str->len,
224 dst->len) == 0);
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000225 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200226
Paul Bakkerbd51b262014-07-10 15:26:12 +0200227exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100228 mbedtls_des3_free(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +0000229}
Paul Bakker33b43f12013-08-20 11:48:36 +0200230/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000231
Paul Bakker33b43f12013-08-20 11:48:36 +0200232/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +0100233void des_key_parity_run()
Paul Bakker1f87fb62011-01-15 17:32:24 +0000234{
235 int i, j, cnt;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200236 unsigned char key[MBEDTLS_DES_KEY_SIZE];
Paul Bakker1f87fb62011-01-15 17:32:24 +0000237 unsigned int parity;
238
Gilles Peskine449bd832023-01-11 14:50:10 +0100239 memset(key, 0, MBEDTLS_DES_KEY_SIZE);
Paul Bakker1f87fb62011-01-15 17:32:24 +0000240 cnt = 0;
241
242 // Iterate through all possible byte values
243 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100244 for (i = 0; i < 32; i++) {
245 for (j = 0; j < 8; j++) {
Paul Bakker1f87fb62011-01-15 17:32:24 +0000246 key[j] = cnt++;
Gilles Peskine449bd832023-01-11 14:50:10 +0100247 }
Paul Bakker1f87fb62011-01-15 17:32:24 +0000248
249 // Set the key parity according to the table
250 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100251 mbedtls_des_key_set_parity(key);
Paul Bakker1f87fb62011-01-15 17:32:24 +0000252
253 // Check the parity with a function
254 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 for (j = 0; j < 8; j++) {
256 parity = key[j] ^ (key[j] >> 4);
Paul Bakker1f87fb62011-01-15 17:32:24 +0000257 parity = parity ^
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 (parity >> 1) ^
259 (parity >> 2) ^
260 (parity >> 3);
Paul Bakker1f87fb62011-01-15 17:32:24 +0000261 parity &= 1;
262
Gilles Peskine449bd832023-01-11 14:50:10 +0100263 if (parity != 1) {
264 TEST_ASSERT(0);
265 }
Paul Bakker1f87fb62011-01-15 17:32:24 +0000266 }
267
268 // Check the parity with the table
269 //
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 TEST_ASSERT(mbedtls_des_key_check_key_parity(key) == 0);
Paul Bakker1f87fb62011-01-15 17:32:24 +0000271 }
272}
Paul Bakker33b43f12013-08-20 11:48:36 +0200273/* END_CASE */
Paul Bakker1f87fb62011-01-15 17:32:24 +0000274
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200275/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +0100276void des_selftest()
Paul Bakkere896fea2009-07-06 06:40:23 +0000277{
Gilles Peskine449bd832023-01-11 14:50:10 +0100278 TEST_ASSERT(mbedtls_des_self_test(1) == 0);
Paul Bakkere896fea2009-07-06 06:40:23 +0000279}
Paul Bakker33b43f12013-08-20 11:48:36 +0200280/* END_CASE */