blob: fb0a349177917e72839b8620dbcb0b73e2bf9a65 [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
Paul Bakker33b43f12013-08-20 11:48:36 +020010/* BEGIN_CASE */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050011void camellia_valid_param( )
12{
13 TEST_VALID_PARAM( mbedtls_camellia_free( NULL ) );
14}
15/* END_CASE */
16
17/* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */
18void camellia_invalid_param( )
19{
20 mbedtls_camellia_context ctx;
21 unsigned char buf[16] = { 0 };
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050022 const int invalid_mode = 42;
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050023 size_t off;
24 ((void) off);
25
Ronald Cron875b5fb2021-05-21 08:50:00 +020026 TEST_EQUAL( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050027 mbedtls_camellia_crypt_ecb( &ctx,
28 invalid_mode,
29 buf, buf ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050030
31#if defined(MBEDTLS_CIPHER_MODE_CBC)
Ronald Cron875b5fb2021-05-21 08:50:00 +020032 TEST_EQUAL( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050033 mbedtls_camellia_crypt_cbc( &ctx,
34 invalid_mode,
35 sizeof( buf ),
36 buf, buf, buf ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050037#endif /* MBEDTLS_CIPHER_MODE_CBC */
38
39#if defined(MBEDTLS_CIPHER_MODE_CFB)
Ronald Cron875b5fb2021-05-21 08:50:00 +020040 TEST_EQUAL( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050041 mbedtls_camellia_crypt_cfb128( &ctx,
42 invalid_mode,
43 sizeof( buf ),
44 &off, buf,
45 buf, buf ) );
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050046#endif /* MBEDTLS_CIPHER_MODE_CFB */
47
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050048exit:
49 return;
50}
51/* END_CASE */
52
53/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010054void camellia_encrypt_ecb( data_t * key_str, data_t * src_str,
Ronald Cronac6ae352020-06-26 14:33:03 +020055 data_t * dst, int setkey_result )
Paul Bakkere896fea2009-07-06 06:40:23 +000056{
Paul Bakkere896fea2009-07-06 06:40:23 +000057 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 mbedtls_camellia_context ctx;
Paul Bakkere896fea2009-07-06 06:40:23 +000059
Paul Bakkere896fea2009-07-06 06:40:23 +000060 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000062
Paul Bakkere896fea2009-07-06 06:40:23 +000063
Azim Khand30ca132017-06-09 04:32:58 +010064 TEST_ASSERT( mbedtls_camellia_setkey_enc( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020065 if( setkey_result == 0 )
Paul Bakker2b222c82009-07-27 21:03:45 +000066 {
Azim Khand30ca132017-06-09 04:32:58 +010067 TEST_ASSERT( mbedtls_camellia_crypt_ecb( &ctx, MBEDTLS_CAMELLIA_ENCRYPT, src_str->x, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000068
Ronald Cronac6ae352020-06-26 14:33:03 +020069 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 16, dst->len ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000070 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020071
Paul Bakkerbd51b262014-07-10 15:26:12 +020072exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_camellia_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000074}
Paul Bakker33b43f12013-08-20 11:48:36 +020075/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000076
Paul Bakker33b43f12013-08-20 11:48:36 +020077/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +010078void camellia_decrypt_ecb( data_t * key_str, data_t * src_str,
Ronald Cronac6ae352020-06-26 14:33:03 +020079 data_t * dst, int setkey_result )
Paul Bakkere896fea2009-07-06 06:40:23 +000080{
Paul Bakkere896fea2009-07-06 06:40:23 +000081 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 mbedtls_camellia_context ctx;
Paul Bakkere896fea2009-07-06 06:40:23 +000083
Paul Bakkere896fea2009-07-06 06:40:23 +000084 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000086
Paul Bakkere896fea2009-07-06 06:40:23 +000087
Azim Khand30ca132017-06-09 04:32:58 +010088 TEST_ASSERT( mbedtls_camellia_setkey_dec( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020089 if( setkey_result == 0 )
Paul Bakker2b222c82009-07-27 21:03:45 +000090 {
Azim Khand30ca132017-06-09 04:32:58 +010091 TEST_ASSERT( mbedtls_camellia_crypt_ecb( &ctx, MBEDTLS_CAMELLIA_DECRYPT, src_str->x, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000092
Ronald Cronac6ae352020-06-26 14:33:03 +020093 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 16, dst->len ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000094 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020095
Paul Bakkerbd51b262014-07-10 15:26:12 +020096exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_camellia_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000098}
Paul Bakker33b43f12013-08-20 11:48:36 +020099/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000100
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200101/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +0100102void camellia_encrypt_cbc( data_t * key_str, data_t * iv_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200103 data_t * src_str, data_t * dst, int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000104{
Paul Bakkere896fea2009-07-06 06:40:23 +0000105 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 mbedtls_camellia_context ctx;
Paul Bakkere896fea2009-07-06 06:40:23 +0000107
Paul Bakkere896fea2009-07-06 06:40:23 +0000108 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000110
Paul Bakkere896fea2009-07-06 06:40:23 +0000111
Azim Khand30ca132017-06-09 04:32:58 +0100112 mbedtls_camellia_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
113 TEST_ASSERT( mbedtls_camellia_crypt_cbc( &ctx, MBEDTLS_CAMELLIA_ENCRYPT, src_str->len, iv_str->x, src_str->x, output) == cbc_result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200114 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000115 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000116
Ronald Cronac6ae352020-06-26 14:33:03 +0200117 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, src_str->len,
118 dst->len ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000119 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200120
Paul Bakkerbd51b262014-07-10 15:26:12 +0200121exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_camellia_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000123}
Paul Bakker33b43f12013-08-20 11:48:36 +0200124/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000125
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +0100127void camellia_decrypt_cbc( data_t * key_str, data_t * iv_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200128 data_t * src_str, data_t * dst,
Azim Khand30ca132017-06-09 04:32:58 +0100129 int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000130{
Paul Bakkere896fea2009-07-06 06:40:23 +0000131 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200132 mbedtls_camellia_context ctx;
Paul Bakkere896fea2009-07-06 06:40:23 +0000133
Paul Bakkere896fea2009-07-06 06:40:23 +0000134 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000136
Paul Bakkere896fea2009-07-06 06:40:23 +0000137
Azim Khand30ca132017-06-09 04:32:58 +0100138 mbedtls_camellia_setkey_dec( &ctx, key_str->x, key_str->len * 8 );
139 TEST_ASSERT( mbedtls_camellia_crypt_cbc( &ctx, MBEDTLS_CAMELLIA_DECRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200140 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000141 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000142
Ronald Cronac6ae352020-06-26 14:33:03 +0200143 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, src_str->len,
144 dst->len ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000145 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200146
Paul Bakkerbd51b262014-07-10 15:26:12 +0200147exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_camellia_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000149}
Paul Bakker33b43f12013-08-20 11:48:36 +0200150/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000151
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khan5fcca462018-06-29 11:05:32 +0100153void camellia_encrypt_cfb128( data_t * key_str, data_t * iv_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200154 data_t * src_str, data_t * dst )
Paul Bakkere896fea2009-07-06 06:40:23 +0000155{
Paul Bakkere896fea2009-07-06 06:40:23 +0000156 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 mbedtls_camellia_context ctx;
Paul Bakker1ef71df2011-06-09 14:14:58 +0000158 size_t iv_offset = 0;
Paul Bakkere896fea2009-07-06 06:40:23 +0000159
Paul Bakkere896fea2009-07-06 06:40:23 +0000160 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200161 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000162
Paul Bakkere896fea2009-07-06 06:40:23 +0000163
Azim Khand30ca132017-06-09 04:32:58 +0100164 mbedtls_camellia_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
165 TEST_ASSERT( mbedtls_camellia_crypt_cfb128( &ctx, MBEDTLS_CAMELLIA_ENCRYPT, 16, &iv_offset, iv_str->x, src_str->x, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000166
Ronald Cronac6ae352020-06-26 14:33:03 +0200167 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 16, dst->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200168
Paul Bakkerbd51b262014-07-10 15:26:12 +0200169exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200170 mbedtls_camellia_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000171}
Paul Bakker33b43f12013-08-20 11:48:36 +0200172/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000173
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200174/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khan5fcca462018-06-29 11:05:32 +0100175void camellia_decrypt_cfb128( data_t * key_str, data_t * iv_str,
176 data_t * src_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200177 data_t * dst )
Paul Bakkere896fea2009-07-06 06:40:23 +0000178{
Paul Bakkere896fea2009-07-06 06:40:23 +0000179 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_camellia_context ctx;
Paul Bakker1ef71df2011-06-09 14:14:58 +0000181 size_t iv_offset = 0;
Paul Bakkere896fea2009-07-06 06:40:23 +0000182
Paul Bakkere896fea2009-07-06 06:40:23 +0000183 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200184 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000185
Paul Bakkere896fea2009-07-06 06:40:23 +0000186
Azim Khand30ca132017-06-09 04:32:58 +0100187 mbedtls_camellia_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
188 TEST_ASSERT( mbedtls_camellia_crypt_cfb128( &ctx, MBEDTLS_CAMELLIA_DECRYPT, 16, &iv_offset, iv_str->x, src_str->x, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000189
Ronald Cronac6ae352020-06-26 14:33:03 +0200190 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 16, dst->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200191
Paul Bakkerbd51b262014-07-10 15:26:12 +0200192exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_camellia_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000194}
Paul Bakker33b43f12013-08-20 11:48:36 +0200195/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000196
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200197/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100198void camellia_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +0000199{
Andres AG93012e82016-09-09 09:10:28 +0100200 TEST_ASSERT( mbedtls_camellia_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000201}
Paul Bakker33b43f12013-08-20 11:48:36 +0200202/* END_CASE */