blob: 96d25a2514fc7fb9caa816fb5c20b627a7ada2f4 [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 */
Azim Khanf1aaec92017-05-30 14:23:15 +010011void camellia_encrypt_ecb( uint8_t * key_str, uint32_t key_len,
12 uint8_t * src_str, uint32_t src_str_len,
13 uint8_t * hex_dst_string,
14 uint32_t hex_dst_string_len, int setkey_result )
Paul Bakkere896fea2009-07-06 06:40:23 +000015{
Paul Bakkere896fea2009-07-06 06:40:23 +000016 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017 mbedtls_camellia_context ctx;
Paul Bakkere896fea2009-07-06 06:40:23 +000018
Paul Bakkere896fea2009-07-06 06:40:23 +000019 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000021
Paul Bakkere896fea2009-07-06 06:40:23 +000022
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023 TEST_ASSERT( mbedtls_camellia_setkey_enc( &ctx, key_str, key_len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020024 if( setkey_result == 0 )
Paul Bakker2b222c82009-07-27 21:03:45 +000025 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026 TEST_ASSERT( mbedtls_camellia_crypt_ecb( &ctx, MBEDTLS_CAMELLIA_ENCRYPT, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000027
Azim Khanf1aaec92017-05-30 14:23:15 +010028 TEST_ASSERT( hexcmp( output, hex_dst_string, 16, hex_dst_string_len ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000029 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020030
Paul Bakkerbd51b262014-07-10 15:26:12 +020031exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032 mbedtls_camellia_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000033}
Paul Bakker33b43f12013-08-20 11:48:36 +020034/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000035
Paul Bakker33b43f12013-08-20 11:48:36 +020036/* BEGIN_CASE */
Azim Khanf1aaec92017-05-30 14:23:15 +010037void camellia_decrypt_ecb( uint8_t * key_str, uint32_t key_len,
38 uint8_t * src_str, uint32_t src_str_len,
39 uint8_t * hex_dst_string,
40 uint32_t hex_dst_string_len, int setkey_result )
Paul Bakkere896fea2009-07-06 06:40:23 +000041{
Paul Bakkere896fea2009-07-06 06:40:23 +000042 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 mbedtls_camellia_context ctx;
Paul Bakkere896fea2009-07-06 06:40:23 +000044
Paul Bakkere896fea2009-07-06 06:40:23 +000045 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000047
Paul Bakkere896fea2009-07-06 06:40:23 +000048
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 TEST_ASSERT( mbedtls_camellia_setkey_dec( &ctx, key_str, key_len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020050 if( setkey_result == 0 )
Paul Bakker2b222c82009-07-27 21:03:45 +000051 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 TEST_ASSERT( mbedtls_camellia_crypt_ecb( &ctx, MBEDTLS_CAMELLIA_DECRYPT, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +000053
Azim Khanf1aaec92017-05-30 14:23:15 +010054 TEST_ASSERT( hexcmp( output, hex_dst_string, 16, hex_dst_string_len ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000055 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020056
Paul Bakkerbd51b262014-07-10 15:26:12 +020057exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020058 mbedtls_camellia_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000059}
Paul Bakker33b43f12013-08-20 11:48:36 +020060/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000061
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020062/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khanf1aaec92017-05-30 14:23:15 +010063void camellia_encrypt_cbc( uint8_t * key_str, uint32_t key_len,
64 uint8_t * iv_str, uint32_t iv_str_len,
65 uint8_t * src_str, uint32_t data_len,
66 uint8_t * hex_dst_string,
67 uint32_t hex_dst_string_len, int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +000068{
Paul Bakkere896fea2009-07-06 06:40:23 +000069 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_camellia_context ctx;
Paul Bakkere896fea2009-07-06 06:40:23 +000071
Paul Bakkere896fea2009-07-06 06:40:23 +000072 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020073 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000074
Paul Bakkere896fea2009-07-06 06:40:23 +000075
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020076 mbedtls_camellia_setkey_enc( &ctx, key_str, key_len * 8 );
77 TEST_ASSERT( mbedtls_camellia_crypt_cbc( &ctx, MBEDTLS_CAMELLIA_ENCRYPT, data_len, iv_str, src_str, output) == cbc_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020078 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +000079 {
Paul Bakkere896fea2009-07-06 06:40:23 +000080
Azim Khanf1aaec92017-05-30 14:23:15 +010081 TEST_ASSERT( hexcmp( output, hex_dst_string, data_len, hex_dst_string_len ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +000082 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020083
Paul Bakkerbd51b262014-07-10 15:26:12 +020084exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020085 mbedtls_camellia_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000086}
Paul Bakker33b43f12013-08-20 11:48:36 +020087/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000088
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khanf1aaec92017-05-30 14:23:15 +010090void camellia_decrypt_cbc( uint8_t * key_str, uint32_t key_len,
91 uint8_t * iv_str, uint32_t iv_str_len,
92 uint8_t * src_str, uint32_t data_len,
93 uint8_t * hex_dst_string,
94 uint32_t hex_dst_string_len, int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +000095{
Paul Bakkere896fea2009-07-06 06:40:23 +000096 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_camellia_context ctx;
Paul Bakkere896fea2009-07-06 06:40:23 +000098
Paul Bakkere896fea2009-07-06 06:40:23 +000099 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000101
Paul Bakkere896fea2009-07-06 06:40:23 +0000102
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_camellia_setkey_dec( &ctx, key_str, key_len * 8 );
104 TEST_ASSERT( mbedtls_camellia_crypt_cbc( &ctx, MBEDTLS_CAMELLIA_DECRYPT, data_len, iv_str, src_str, output ) == cbc_result );
Paul Bakker33b43f12013-08-20 11:48:36 +0200105 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000106 {
Paul Bakkere896fea2009-07-06 06:40:23 +0000107
Azim Khanf1aaec92017-05-30 14:23:15 +0100108 TEST_ASSERT( hexcmp( output, hex_dst_string, data_len, hex_dst_string_len ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000109 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200110
Paul Bakkerbd51b262014-07-10 15:26:12 +0200111exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_camellia_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000113}
Paul Bakker33b43f12013-08-20 11:48:36 +0200114/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000115
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khanf1aaec92017-05-30 14:23:15 +0100117void camellia_encrypt_cfb128( uint8_t * key_str, uint32_t key_len,
118 uint8_t * iv_str, uint32_t iv_str_len,
119 uint8_t * src_str, uint32_t src_str_len,
120 uint8_t * hex_dst_string,
121 uint32_t hex_dst_string_len )
Paul Bakkere896fea2009-07-06 06:40:23 +0000122{
Paul Bakkere896fea2009-07-06 06:40:23 +0000123 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_camellia_context ctx;
Paul Bakker1ef71df2011-06-09 14:14:58 +0000125 size_t iv_offset = 0;
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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131 mbedtls_camellia_setkey_enc( &ctx, key_str, key_len * 8 );
132 TEST_ASSERT( mbedtls_camellia_crypt_cfb128( &ctx, MBEDTLS_CAMELLIA_ENCRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000133
Azim Khanf1aaec92017-05-30 14:23:15 +0100134 TEST_ASSERT( hexcmp( output, hex_dst_string, 16, hex_dst_string_len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200135
Paul Bakkerbd51b262014-07-10 15:26:12 +0200136exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_camellia_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000138}
Paul Bakker33b43f12013-08-20 11:48:36 +0200139/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Azim Khanf1aaec92017-05-30 14:23:15 +0100142void camellia_decrypt_cfb128( uint8_t * key_str, uint32_t key_len,
143 uint8_t * iv_str, uint32_t iv_str_len,
144 uint8_t * src_str, uint32_t src_str_len,
145 uint8_t * hex_dst_string,
146 uint32_t hex_dst_string_len )
Paul Bakkere896fea2009-07-06 06:40:23 +0000147{
Paul Bakkere896fea2009-07-06 06:40:23 +0000148 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200149 mbedtls_camellia_context ctx;
Paul Bakker1ef71df2011-06-09 14:14:58 +0000150 size_t iv_offset = 0;
Paul Bakkere896fea2009-07-06 06:40:23 +0000151
Paul Bakkere896fea2009-07-06 06:40:23 +0000152 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200153 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000154
Paul Bakkere896fea2009-07-06 06:40:23 +0000155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 mbedtls_camellia_setkey_enc( &ctx, key_str, key_len * 8 );
157 TEST_ASSERT( mbedtls_camellia_crypt_cfb128( &ctx, MBEDTLS_CAMELLIA_DECRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000158
Azim Khanf1aaec92017-05-30 14:23:15 +0100159 TEST_ASSERT( hexcmp( output, hex_dst_string, 16, hex_dst_string_len ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200160
Paul Bakkerbd51b262014-07-10 15:26:12 +0200161exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_camellia_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000163}
Paul Bakker33b43f12013-08-20 11:48:36 +0200164/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000165
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Azim Khanf1aaec92017-05-30 14:23:15 +0100167void camellia_selftest( )
Paul Bakkere896fea2009-07-06 06:40:23 +0000168{
Andres AG93012e82016-09-09 09:10:28 +0100169 TEST_ASSERT( mbedtls_camellia_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000170}
Paul Bakker33b43f12013-08-20 11:48:36 +0200171/* END_CASE */