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 */
David Horstmann71159f42023-01-03 12:51:59 +000011void des_check_weak(data_t *key, int ret)
Manuel Pégourié-Gonnard9ce7e842014-03-29 17:06:43 +010012{
David Horstmann71159f42023-01-03 12:51:59 +000013 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 */
David Horstmann71159f42023-01-03 12:51:59 +000018void 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);
David Horstmann71159f42023-01-03 12:51:59 +000024 mbedtls_des_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +000025
Paul Bakkere896fea2009-07-06 06:40:23 +000026
David Horstmann71159f42023-01-03 12:51:59 +000027 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
David Horstmann71159f42023-01-03 12:51:59 +000030 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:
David Horstmann71159f42023-01-03 12:51:59 +000033 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 */
David Horstmann71159f42023-01-03 12:51:59 +000038void 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);
David Horstmann71159f42023-01-03 12:51:59 +000044 mbedtls_des_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +000045
Paul Bakkere896fea2009-07-06 06:40:23 +000046
David Horstmann71159f42023-01-03 12:51:59 +000047 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
David Horstmann71159f42023-01-03 12:51:59 +000050 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:
David Horstmann71159f42023-01-03 12:51:59 +000053 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 */
David Horstmann71159f42023-01-03 12:51:59 +000058void 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);
David Horstmann71159f42023-01-03 12:51:59 +000065 mbedtls_des_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +000066
Paul Bakkere896fea2009-07-06 06:40:23 +000067
David Horstmann71159f42023-01-03 12:51:59 +000068 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
David Horstmann71159f42023-01-03 12:51:59 +000073 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:
David Horstmann71159f42023-01-03 12:51:59 +000078 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 */
David Horstmann71159f42023-01-03 12:51:59 +000083void 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);
David Horstmann71159f42023-01-03 12:51:59 +000091 mbedtls_des_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +000092
Paul Bakkere896fea2009-07-06 06:40:23 +000093
David Horstmann71159f42023-01-03 12:51:59 +000094 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
David Horstmann71159f42023-01-03 12:51:59 +000099 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:
David Horstmann71159f42023-01-03 12:51:59 +0000104 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 */
David Horstmann71159f42023-01-03 12:51:59 +0000109void 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);
David Horstmann71159f42023-01-03 12:51:59 +0000116 mbedtls_des3_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +0000117
Paul Bakkere896fea2009-07-06 06:40:23 +0000118
David Horstmann71159f42023-01-03 12:51:59 +0000119 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
David Horstmann71159f42023-01-03 12:51:59 +0000127 TEST_ASSERT(mbedtls_des3_crypt_ecb(&ctx, src_str->x, output) == 0);
Paul Bakkere896fea2009-07-06 06:40:23 +0000128
David Horstmann71159f42023-01-03 12:51:59 +0000129 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:
David Horstmann71159f42023-01-03 12:51:59 +0000132 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 */
David Horstmann71159f42023-01-03 12:51:59 +0000137void 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);
David Horstmann71159f42023-01-03 12:51:59 +0000144 mbedtls_des3_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +0000145
Paul Bakkere896fea2009-07-06 06:40:23 +0000146
David Horstmann71159f42023-01-03 12:51:59 +0000147 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
David Horstmann71159f42023-01-03 12:51:59 +0000155 TEST_ASSERT(mbedtls_des3_crypt_ecb(&ctx, src_str->x, output) == 0);
Paul Bakkere896fea2009-07-06 06:40:23 +0000156
David Horstmann71159f42023-01-03 12:51:59 +0000157 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:
David Horstmann71159f42023-01-03 12:51:59 +0000160 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 */
David Horstmann71159f42023-01-03 12:51:59 +0000165void 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);
David Horstmann71159f42023-01-03 12:51:59 +0000173 mbedtls_des3_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +0000174
Paul Bakkere896fea2009-07-06 06:40:23 +0000175
David Horstmann71159f42023-01-03 12:51:59 +0000176 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
David Horstmann71159f42023-01-03 12:51:59 +0000184 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
David Horstmann71159f42023-01-03 12:51:59 +0000187 if (cbc_result == 0) {
Paul Bakkere896fea2009-07-06 06:40:23 +0000188
David Horstmann71159f42023-01-03 12:51:59 +0000189 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:
David Horstmann71159f42023-01-03 12:51:59 +0000194 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 */
David Horstmann71159f42023-01-03 12:51:59 +0000199void 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);
David Horstmann71159f42023-01-03 12:51:59 +0000207 mbedtls_des3_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +0000208
Paul Bakkere896fea2009-07-06 06:40:23 +0000209
David Horstmann71159f42023-01-03 12:51:59 +0000210 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
David Horstmann71159f42023-01-03 12:51:59 +0000218 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
David Horstmann71159f42023-01-03 12:51:59 +0000221 if (cbc_result == 0) {
Paul Bakkere896fea2009-07-06 06:40:23 +0000222
David Horstmann71159f42023-01-03 12:51:59 +0000223 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:
David Horstmann71159f42023-01-03 12:51:59 +0000228 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 */
David Horstmann71159f42023-01-03 12:51:59 +0000233void 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
David Horstmann71159f42023-01-03 12:51:59 +0000239 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 //
David Horstmann71159f42023-01-03 12:51:59 +0000244 for (i = 0; i < 32; i++) {
245 for (j = 0; j < 8; j++) {
Paul Bakker1f87fb62011-01-15 17:32:24 +0000246 key[j] = cnt++;
David Horstmann71159f42023-01-03 12:51:59 +0000247 }
Paul Bakker1f87fb62011-01-15 17:32:24 +0000248
249 // Set the key parity according to the table
250 //
David Horstmann71159f42023-01-03 12:51:59 +0000251 mbedtls_des_key_set_parity(key);
Paul Bakker1f87fb62011-01-15 17:32:24 +0000252
253 // Check the parity with a function
254 //
David Horstmann71159f42023-01-03 12:51:59 +0000255 for (j = 0; j < 8; j++) {
256 parity = key[j] ^ (key[j] >> 4);
Paul Bakker1f87fb62011-01-15 17:32:24 +0000257 parity = parity ^
David Horstmann71159f42023-01-03 12:51:59 +0000258 (parity >> 1) ^
259 (parity >> 2) ^
260 (parity >> 3);
Paul Bakker1f87fb62011-01-15 17:32:24 +0000261 parity &= 1;
262
David Horstmann71159f42023-01-03 12:51:59 +0000263 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 //
David Horstmann71159f42023-01-03 12:51:59 +0000270 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 */
David Horstmann71159f42023-01-03 12:51:59 +0000276void des_selftest()
Paul Bakkere896fea2009-07-06 06:40:23 +0000277{
David Horstmann71159f42023-01-03 12:51:59 +0000278 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 */