blob: 1cef97a9fae172f0acb112891a6f3d1f45f66103 [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/camellia.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_CAMELLIA_C
Paul Bakker33b43f12013-08-20 11:48:36 +02007 * END_DEPENDENCIES
8 */
Paul Bakker5690efc2011-05-26 13:16:06 +00009
Tuvshinzaya Erdenekhuuc7d72202022-07-29 14:45:04 +010010/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010011void camellia_invalid_param()
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050012{
13 mbedtls_camellia_context ctx;
14 unsigned char buf[16] = { 0 };
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050015 const int invalid_mode = 42;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050016 size_t off;
17 ((void) off);
18
Gilles Peskine449bd832023-01-11 14:50:10 +010019 TEST_EQUAL(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
20 mbedtls_camellia_crypt_ecb(&ctx,
21 invalid_mode,
22 buf, buf));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050023
24#if defined(MBEDTLS_CIPHER_MODE_CBC)
Gilles Peskine449bd832023-01-11 14:50:10 +010025 TEST_EQUAL(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
26 mbedtls_camellia_crypt_cbc(&ctx,
27 invalid_mode,
28 sizeof(buf),
29 buf, buf, buf));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050030#endif /* MBEDTLS_CIPHER_MODE_CBC */
31
32#if defined(MBEDTLS_CIPHER_MODE_CFB)
Gilles Peskine449bd832023-01-11 14:50:10 +010033 TEST_EQUAL(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
34 mbedtls_camellia_crypt_cfb128(&ctx,
35 invalid_mode,
36 sizeof(buf),
37 &off, buf,
38 buf, buf));
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050039#endif /* MBEDTLS_CIPHER_MODE_CFB */
40
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050041exit:
42 return;
43}
44/* END_CASE */
45
46/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010047void camellia_encrypt_ecb(data_t *key_str, data_t *src_str,
48 data_t *dst, int setkey_result)
Paul Bakkere896fea2009-07-06 06:40:23 +000049{
Paul Bakkere896fea2009-07-06 06:40:23 +000050 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 mbedtls_camellia_context ctx;
Paul Bakkere896fea2009-07-06 06:40:23 +000052
Paul Bakkere896fea2009-07-06 06:40:23 +000053 memset(output, 0x00, 100);
Gilles Peskine449bd832023-01-11 14:50:10 +010054 mbedtls_camellia_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +000055
Paul Bakkere896fea2009-07-06 06:40:23 +000056
Gilles Peskine449bd832023-01-11 14:50:10 +010057 TEST_ASSERT(mbedtls_camellia_setkey_enc(&ctx, key_str->x, key_str->len * 8) == setkey_result);
58 if (setkey_result == 0) {
59 TEST_ASSERT(mbedtls_camellia_crypt_ecb(&ctx, MBEDTLS_CAMELLIA_ENCRYPT, src_str->x,
60 output) == 0);
Paul Bakkere896fea2009-07-06 06:40:23 +000061
Gilles Peskine449bd832023-01-11 14:50:10 +010062 TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0);
Paul Bakker2b222c82009-07-27 21:03:45 +000063 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020064
Paul Bakkerbd51b262014-07-10 15:26:12 +020065exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010066 mbedtls_camellia_free(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +000067}
Paul Bakker33b43f12013-08-20 11:48:36 +020068/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000069
Paul Bakker33b43f12013-08-20 11:48:36 +020070/* BEGIN_CASE */
Gilles Peskine449bd832023-01-11 14:50:10 +010071void camellia_decrypt_ecb(data_t *key_str, data_t *src_str,
72 data_t *dst, int setkey_result)
Paul Bakkere896fea2009-07-06 06:40:23 +000073{
Paul Bakkere896fea2009-07-06 06:40:23 +000074 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 mbedtls_camellia_context ctx;
Paul Bakkere896fea2009-07-06 06:40:23 +000076
Paul Bakkere896fea2009-07-06 06:40:23 +000077 memset(output, 0x00, 100);
Gilles Peskine449bd832023-01-11 14:50:10 +010078 mbedtls_camellia_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +000079
Paul Bakkere896fea2009-07-06 06:40:23 +000080
Gilles Peskine449bd832023-01-11 14:50:10 +010081 TEST_ASSERT(mbedtls_camellia_setkey_dec(&ctx, key_str->x, key_str->len * 8) == setkey_result);
82 if (setkey_result == 0) {
83 TEST_ASSERT(mbedtls_camellia_crypt_ecb(&ctx, MBEDTLS_CAMELLIA_DECRYPT, src_str->x,
84 output) == 0);
Paul Bakkere896fea2009-07-06 06:40:23 +000085
Gilles Peskine449bd832023-01-11 14:50:10 +010086 TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0);
Paul Bakker2b222c82009-07-27 21:03:45 +000087 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020088
Paul Bakkerbd51b262014-07-10 15:26:12 +020089exit:
Gilles Peskine449bd832023-01-11 14:50:10 +010090 mbedtls_camellia_free(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +000091}
Paul Bakker33b43f12013-08-20 11:48:36 +020092/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000093
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020094/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Gilles Peskine449bd832023-01-11 14:50:10 +010095void camellia_encrypt_cbc(data_t *key_str, data_t *iv_str,
96 data_t *src_str, data_t *dst, int cbc_result)
Paul Bakkere896fea2009-07-06 06:40:23 +000097{
Paul Bakkere896fea2009-07-06 06:40:23 +000098 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_camellia_context ctx;
Paul Bakkere896fea2009-07-06 06:40:23 +0000100
Paul Bakkere896fea2009-07-06 06:40:23 +0000101 memset(output, 0x00, 100);
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 mbedtls_camellia_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +0000103
Paul Bakkere896fea2009-07-06 06:40:23 +0000104
Gilles Peskine449bd832023-01-11 14:50:10 +0100105 mbedtls_camellia_setkey_enc(&ctx, key_str->x, key_str->len * 8);
106 TEST_ASSERT(mbedtls_camellia_crypt_cbc(&ctx, MBEDTLS_CAMELLIA_ENCRYPT, src_str->len, iv_str->x,
107 src_str->x, output) == cbc_result);
108 if (cbc_result == 0) {
Paul Bakkere896fea2009-07-06 06:40:23 +0000109
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, src_str->len,
111 dst->len) == 0);
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000112 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200113
Paul Bakkerbd51b262014-07-10 15:26:12 +0200114exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100115 mbedtls_camellia_free(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +0000116}
Paul Bakker33b43f12013-08-20 11:48:36 +0200117/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000118
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Gilles Peskine449bd832023-01-11 14:50:10 +0100120void camellia_decrypt_cbc(data_t *key_str, data_t *iv_str,
121 data_t *src_str, data_t *dst,
122 int cbc_result)
Paul Bakkere896fea2009-07-06 06:40:23 +0000123{
Paul Bakkere896fea2009-07-06 06:40:23 +0000124 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 mbedtls_camellia_context ctx;
Paul Bakkere896fea2009-07-06 06:40:23 +0000126
Paul Bakkere896fea2009-07-06 06:40:23 +0000127 memset(output, 0x00, 100);
Gilles Peskine449bd832023-01-11 14:50:10 +0100128 mbedtls_camellia_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +0000129
Paul Bakkere896fea2009-07-06 06:40:23 +0000130
Gilles Peskine449bd832023-01-11 14:50:10 +0100131 mbedtls_camellia_setkey_dec(&ctx, key_str->x, key_str->len * 8);
132 TEST_ASSERT(mbedtls_camellia_crypt_cbc(&ctx, MBEDTLS_CAMELLIA_DECRYPT, src_str->len, iv_str->x,
133 src_str->x, output) == cbc_result);
134 if (cbc_result == 0) {
Paul Bakkere896fea2009-07-06 06:40:23 +0000135
Gilles Peskine449bd832023-01-11 14:50:10 +0100136 TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, src_str->len,
137 dst->len) == 0);
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000138 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200139
Paul Bakkerbd51b262014-07-10 15:26:12 +0200140exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100141 mbedtls_camellia_free(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +0000142}
Paul Bakker33b43f12013-08-20 11:48:36 +0200143/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000144
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Gilles Peskine449bd832023-01-11 14:50:10 +0100146void camellia_encrypt_cfb128(data_t *key_str, data_t *iv_str,
147 data_t *src_str, data_t *dst)
Paul Bakkere896fea2009-07-06 06:40:23 +0000148{
Paul Bakkere896fea2009-07-06 06:40:23 +0000149 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_camellia_context ctx;
Paul Bakker1ef71df2011-06-09 14:14:58 +0000151 size_t iv_offset = 0;
Paul Bakkere896fea2009-07-06 06:40:23 +0000152
Paul Bakkere896fea2009-07-06 06:40:23 +0000153 memset(output, 0x00, 100);
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 mbedtls_camellia_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +0000155
Paul Bakkere896fea2009-07-06 06:40:23 +0000156
Gilles Peskine449bd832023-01-11 14:50:10 +0100157 mbedtls_camellia_setkey_enc(&ctx, key_str->x, key_str->len * 8);
158 TEST_ASSERT(mbedtls_camellia_crypt_cfb128(&ctx, MBEDTLS_CAMELLIA_ENCRYPT, 16, &iv_offset,
159 iv_str->x, src_str->x, output) == 0);
Paul Bakkere896fea2009-07-06 06:40:23 +0000160
Gilles Peskine449bd832023-01-11 14:50:10 +0100161 TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0);
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200162
Paul Bakkerbd51b262014-07-10 15:26:12 +0200163exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 mbedtls_camellia_free(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +0000165}
Paul Bakker33b43f12013-08-20 11:48:36 +0200166/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Gilles Peskine449bd832023-01-11 14:50:10 +0100169void camellia_decrypt_cfb128(data_t *key_str, data_t *iv_str,
170 data_t *src_str,
171 data_t *dst)
Paul Bakkere896fea2009-07-06 06:40:23 +0000172{
Paul Bakkere896fea2009-07-06 06:40:23 +0000173 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174 mbedtls_camellia_context ctx;
Paul Bakker1ef71df2011-06-09 14:14:58 +0000175 size_t iv_offset = 0;
Paul Bakkere896fea2009-07-06 06:40:23 +0000176
Paul Bakkere896fea2009-07-06 06:40:23 +0000177 memset(output, 0x00, 100);
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 mbedtls_camellia_init(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +0000179
Paul Bakkere896fea2009-07-06 06:40:23 +0000180
Gilles Peskine449bd832023-01-11 14:50:10 +0100181 mbedtls_camellia_setkey_enc(&ctx, key_str->x, key_str->len * 8);
182 TEST_ASSERT(mbedtls_camellia_crypt_cfb128(&ctx, MBEDTLS_CAMELLIA_DECRYPT, 16, &iv_offset,
183 iv_str->x, src_str->x, output) == 0);
Paul Bakkere896fea2009-07-06 06:40:23 +0000184
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 TEST_ASSERT(mbedtls_test_hexcmp(output, dst->x, 16, dst->len) == 0);
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200186
Paul Bakkerbd51b262014-07-10 15:26:12 +0200187exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 mbedtls_camellia_free(&ctx);
Paul Bakkere896fea2009-07-06 06:40:23 +0000189}
Paul Bakker33b43f12013-08-20 11:48:36 +0200190/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Gilles Peskine449bd832023-01-11 14:50:10 +0100193void camellia_selftest()
Paul Bakkere896fea2009-07-06 06:40:23 +0000194{
Gilles Peskine449bd832023-01-11 14:50:10 +0100195 TEST_ASSERT(mbedtls_camellia_self_test(1) == 0);
Paul Bakkere896fea2009-07-06 06:40:23 +0000196}
Paul Bakker33b43f12013-08-20 11:48:36 +0200197/* END_CASE */