blob: 3d318c861f1eed28d3b5f5b812f3026535479a64 [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 */
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050011void camellia_invalid_param( )
12{
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
Ronald Cron875b5fb2021-05-21 08:50:00 +020019 TEST_EQUAL( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050020 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)
Ronald Cron875b5fb2021-05-21 08:50:00 +020025 TEST_EQUAL( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050026 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)
Ronald Cron875b5fb2021-05-21 08:50:00 +020033 TEST_EQUAL( MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA,
Andrzej Kurekc470b6b2019-01-31 08:20:20 -050034 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 */
Azim Khan5fcca462018-06-29 11:05:32 +010047void camellia_encrypt_ecb( data_t * key_str, data_t * src_str,
Ronald Cronac6ae352020-06-26 14:33:03 +020048 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);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000055
Paul Bakkere896fea2009-07-06 06:40:23 +000056
Azim Khand30ca132017-06-09 04:32:58 +010057 TEST_ASSERT( mbedtls_camellia_setkey_enc( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020058 if( setkey_result == 0 )
Paul Bakker2b222c82009-07-27 21:03:45 +000059 {
Azim Khand30ca132017-06-09 04:32:58 +010060 TEST_ASSERT( mbedtls_camellia_crypt_ecb( &ctx, MBEDTLS_CAMELLIA_ENCRYPT, src_str->x, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000061
Ronald Cronac6ae352020-06-26 14:33:03 +020062 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:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 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 */
Azim Khan5fcca462018-06-29 11:05:32 +010071void camellia_decrypt_ecb( data_t * key_str, data_t * src_str,
Ronald Cronac6ae352020-06-26 14:33:03 +020072 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);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000079
Paul Bakkere896fea2009-07-06 06:40:23 +000080
Azim Khand30ca132017-06-09 04:32:58 +010081 TEST_ASSERT( mbedtls_camellia_setkey_dec( &ctx, key_str->x, key_str->len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020082 if( setkey_result == 0 )
Paul Bakker2b222c82009-07-27 21:03:45 +000083 {
Azim Khand30ca132017-06-09 04:32:58 +010084 TEST_ASSERT( mbedtls_camellia_crypt_ecb( &ctx, MBEDTLS_CAMELLIA_DECRYPT, src_str->x, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000085
Ronald Cronac6ae352020-06-26 14:33:03 +020086 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:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 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 */
Azim Khan5fcca462018-06-29 11:05:32 +010095void camellia_encrypt_cbc( data_t * key_str, data_t * iv_str,
Ronald Cronac6ae352020-06-26 14:33:03 +020096 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);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000103
Paul Bakkere896fea2009-07-06 06:40:23 +0000104
Azim Khand30ca132017-06-09 04:32:58 +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, src_str->x, output) == cbc_result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200107 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000108 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000109
Ronald Cronac6ae352020-06-26 14:33:03 +0200110 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:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 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 */
Azim Khan5fcca462018-06-29 11:05:32 +0100120void camellia_decrypt_cbc( data_t * key_str, data_t * iv_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200121 data_t * src_str, data_t * dst,
Azim Khand30ca132017-06-09 04:32:58 +0100122 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);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000129
Paul Bakkere896fea2009-07-06 06:40:23 +0000130
Azim Khand30ca132017-06-09 04:32:58 +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, src_str->x, output ) == cbc_result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200133 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000134 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000135
Ronald Cronac6ae352020-06-26 14:33:03 +0200136 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:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 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 */
Azim Khan5fcca462018-06-29 11:05:32 +0100146void camellia_encrypt_cfb128( data_t * key_str, data_t * iv_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200147 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);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000155
Paul Bakkere896fea2009-07-06 06:40:23 +0000156
Azim Khand30ca132017-06-09 04:32:58 +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, iv_str->x, src_str->x, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000159
Ronald Cronac6ae352020-06-26 14:33:03 +0200160 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 16, dst->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200161
Paul Bakkerbd51b262014-07-10 15:26:12 +0200162exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_camellia_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000164}
Paul Bakker33b43f12013-08-20 11:48:36 +0200165/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000166
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200167/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khan5fcca462018-06-29 11:05:32 +0100168void camellia_decrypt_cfb128( data_t * key_str, data_t * iv_str,
169 data_t * src_str,
Ronald Cronac6ae352020-06-26 14:33:03 +0200170 data_t * dst )
Paul Bakkere896fea2009-07-06 06:40:23 +0000171{
Paul Bakkere896fea2009-07-06 06:40:23 +0000172 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_camellia_context ctx;
Paul Bakker1ef71df2011-06-09 14:14:58 +0000174 size_t iv_offset = 0;
Paul Bakkere896fea2009-07-06 06:40:23 +0000175
Paul Bakkere896fea2009-07-06 06:40:23 +0000176 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000178
Paul Bakkere896fea2009-07-06 06:40:23 +0000179
Azim Khand30ca132017-06-09 04:32:58 +0100180 mbedtls_camellia_setkey_enc( &ctx, key_str->x, key_str->len * 8 );
181 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 +0000182
Ronald Cronac6ae352020-06-26 14:33:03 +0200183 TEST_ASSERT( mbedtls_test_hexcmp( output, dst->x, 16, dst->len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200184
Paul Bakkerbd51b262014-07-10 15:26:12 +0200185exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200186 mbedtls_camellia_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000187}
Paul Bakker33b43f12013-08-20 11:48:36 +0200188/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000189
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100191void camellia_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +0000192{
Andres AG93012e82016-09-09 09:10:28 +0100193 TEST_ASSERT( mbedtls_camellia_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000194}
Paul Bakker33b43f12013-08-20 11:48:36 +0200195/* END_CASE */