blob: 9df6482a8db34a71ff67460c45fec9559c9b2acb [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 */
11void camellia_encrypt_ecb( char *hex_key_string, char *hex_src_string,
12 char *hex_dst_string, int setkey_result )
Paul Bakkere896fea2009-07-06 06:40:23 +000013{
14 unsigned char key_str[100];
15 unsigned char src_str[100];
16 unsigned char dst_str[100];
17 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020018 mbedtls_camellia_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000019 int key_len;
Paul Bakkere896fea2009-07-06 06:40:23 +000020
21 memset(key_str, 0x00, 100);
22 memset(src_str, 0x00, 100);
23 memset(dst_str, 0x00, 100);
24 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000026
Paul Bakker33b43f12013-08-20 11:48:36 +020027 key_len = unhexify( key_str, hex_key_string );
28 unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +000029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030 TEST_ASSERT( mbedtls_camellia_setkey_enc( &ctx, key_str, key_len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020031 if( setkey_result == 0 )
Paul Bakker2b222c82009-07-27 21:03:45 +000032 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033 TEST_ASSERT( mbedtls_camellia_crypt_ecb( &ctx, MBEDTLS_CAMELLIA_ENCRYPT, src_str, output ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000034 hexify( dst_str, output, 16 );
Paul Bakkere896fea2009-07-06 06:40:23 +000035
Paul Bakker33b43f12013-08-20 11:48:36 +020036 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000037 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020038
Paul Bakkerbd51b262014-07-10 15:26:12 +020039exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 mbedtls_camellia_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000041}
Paul Bakker33b43f12013-08-20 11:48:36 +020042/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000043
Paul Bakker33b43f12013-08-20 11:48:36 +020044/* BEGIN_CASE */
45void camellia_decrypt_ecb( char *hex_key_string, char *hex_src_string,
46 char *hex_dst_string, int setkey_result )
Paul Bakkere896fea2009-07-06 06:40:23 +000047{
48 unsigned char key_str[100];
49 unsigned char src_str[100];
50 unsigned char dst_str[100];
51 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 mbedtls_camellia_context ctx;
Paul Bakker69998dd2009-07-11 19:15:20 +000053 int key_len;
Paul Bakkere896fea2009-07-06 06:40:23 +000054
55 memset(key_str, 0x00, 100);
56 memset(src_str, 0x00, 100);
57 memset(dst_str, 0x00, 100);
58 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000060
Paul Bakker33b43f12013-08-20 11:48:36 +020061 key_len = unhexify( key_str, hex_key_string );
62 unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +000063
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 TEST_ASSERT( mbedtls_camellia_setkey_dec( &ctx, key_str, key_len * 8 ) == setkey_result );
Paul Bakker33b43f12013-08-20 11:48:36 +020065 if( setkey_result == 0 )
Paul Bakker2b222c82009-07-27 21:03:45 +000066 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067 TEST_ASSERT( mbedtls_camellia_crypt_ecb( &ctx, MBEDTLS_CAMELLIA_DECRYPT, src_str, output ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000068 hexify( dst_str, output, 16 );
Paul Bakkere896fea2009-07-06 06:40:23 +000069
Paul Bakker33b43f12013-08-20 11:48:36 +020070 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker2b222c82009-07-27 21:03:45 +000071 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +020072
Paul Bakkerbd51b262014-07-10 15:26:12 +020073exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 mbedtls_camellia_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000075}
Paul Bakker33b43f12013-08-20 11:48:36 +020076/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +000077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +020079void camellia_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
80 char *hex_src_string, char *hex_dst_string,
81 int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +000082{
83 unsigned char key_str[100];
84 unsigned char iv_str[100];
85 unsigned char src_str[100];
86 unsigned char dst_str[100];
87 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020088 mbedtls_camellia_context ctx;
Paul Bakkerf3ccc682010-03-18 21:21:02 +000089 int key_len, data_len;
Paul Bakkere896fea2009-07-06 06:40:23 +000090
91 memset(key_str, 0x00, 100);
92 memset(iv_str, 0x00, 100);
93 memset(src_str, 0x00, 100);
94 memset(dst_str, 0x00, 100);
95 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +000097
Paul Bakker33b43f12013-08-20 11:48:36 +020098 key_len = unhexify( key_str, hex_key_string );
99 unhexify( iv_str, hex_iv_string );
100 data_len = unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000101
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200102 mbedtls_camellia_setkey_enc( &ctx, key_str, key_len * 8 );
103 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 +0200104 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000105 {
106 hexify( dst_str, output, data_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000107
Paul Bakker33b43f12013-08-20 11:48:36 +0200108 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 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_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200117void camellia_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
118 char *hex_src_string, char *hex_dst_string,
119 int cbc_result )
Paul Bakkere896fea2009-07-06 06:40:23 +0000120{
121 unsigned char key_str[100];
122 unsigned char iv_str[100];
123 unsigned char src_str[100];
124 unsigned char dst_str[100];
125 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 mbedtls_camellia_context ctx;
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000127 int key_len, data_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000128
129 memset(key_str, 0x00, 100);
130 memset(iv_str, 0x00, 100);
131 memset(src_str, 0x00, 100);
132 memset(dst_str, 0x00, 100);
133 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000135
Paul Bakker33b43f12013-08-20 11:48:36 +0200136 key_len = unhexify( key_str, hex_key_string );
137 unhexify( iv_str, hex_iv_string );
138 data_len = unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 mbedtls_camellia_setkey_dec( &ctx, key_str, key_len * 8 );
141 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 +0200142 if( cbc_result == 0 )
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000143 {
144 hexify( dst_str, output, data_len );
Paul Bakkere896fea2009-07-06 06:40:23 +0000145
Paul Bakker33b43f12013-08-20 11:48:36 +0200146 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf3ccc682010-03-18 21:21:02 +0000147 }
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200148
Paul Bakkerbd51b262014-07-10 15:26:12 +0200149exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 mbedtls_camellia_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000151}
Paul Bakker33b43f12013-08-20 11:48:36 +0200152/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker33b43f12013-08-20 11:48:36 +0200155void camellia_encrypt_cfb128( char *hex_key_string, char *hex_iv_string,
156 char *hex_src_string, char *hex_dst_string )
Paul Bakkere896fea2009-07-06 06:40:23 +0000157{
158 unsigned char key_str[100];
159 unsigned char iv_str[100];
160 unsigned char src_str[100];
161 unsigned char dst_str[100];
162 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_camellia_context ctx;
Paul Bakker1ef71df2011-06-09 14:14:58 +0000164 size_t iv_offset = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000165 int key_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000166
167 memset(key_str, 0x00, 100);
168 memset(iv_str, 0x00, 100);
169 memset(src_str, 0x00, 100);
170 memset(dst_str, 0x00, 100);
171 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000173
Paul Bakker33b43f12013-08-20 11:48:36 +0200174 key_len = unhexify( key_str, hex_key_string );
175 unhexify( iv_str, hex_iv_string );
176 unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000177
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200178 mbedtls_camellia_setkey_enc( &ctx, key_str, key_len * 8 );
179 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 +0000180 hexify( dst_str, output, 16 );
181
Paul Bakker33b43f12013-08-20 11:48:36 +0200182 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200183
Paul Bakkerbd51b262014-07-10 15:26:12 +0200184exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 mbedtls_camellia_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000186}
Paul Bakker33b43f12013-08-20 11:48:36 +0200187/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000188
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
Paul Bakker33b43f12013-08-20 11:48:36 +0200190void camellia_decrypt_cfb128( char *hex_key_string, char *hex_iv_string,
191 char *hex_src_string, char *hex_dst_string )
Paul Bakkere896fea2009-07-06 06:40:23 +0000192{
193 unsigned char key_str[100];
194 unsigned char iv_str[100];
195 unsigned char src_str[100];
196 unsigned char dst_str[100];
197 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_camellia_context ctx;
Paul Bakker1ef71df2011-06-09 14:14:58 +0000199 size_t iv_offset = 0;
Paul Bakker69998dd2009-07-11 19:15:20 +0000200 int key_len;
Paul Bakkere896fea2009-07-06 06:40:23 +0000201
202 memset(key_str, 0x00, 100);
203 memset(iv_str, 0x00, 100);
204 memset(src_str, 0x00, 100);
205 memset(dst_str, 0x00, 100);
206 memset(output, 0x00, 100);
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 mbedtls_camellia_init( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000208
Paul Bakker33b43f12013-08-20 11:48:36 +0200209 key_len = unhexify( key_str, hex_key_string );
210 unhexify( iv_str, hex_iv_string );
211 unhexify( src_str, hex_src_string );
Paul Bakkere896fea2009-07-06 06:40:23 +0000212
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200213 mbedtls_camellia_setkey_enc( &ctx, key_str, key_len * 8 );
214 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 +0000215 hexify( dst_str, output, 16 );
216
Paul Bakker33b43f12013-08-20 11:48:36 +0200217 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakker8cfd9d82014-06-18 11:16:11 +0200218
Paul Bakkerbd51b262014-07-10 15:26:12 +0200219exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200220 mbedtls_camellia_free( &ctx );
Paul Bakkere896fea2009-07-06 06:40:23 +0000221}
Paul Bakker33b43f12013-08-20 11:48:36 +0200222/* END_CASE */
Paul Bakkere896fea2009-07-06 06:40:23 +0000223
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200225void camellia_selftest()
Paul Bakkere896fea2009-07-06 06:40:23 +0000226{
Andres AG93012e82016-09-09 09:10:28 +0100227 TEST_ASSERT( mbedtls_camellia_self_test( 1 ) == 0 );
Paul Bakkere896fea2009-07-06 06:40:23 +0000228}
Paul Bakker33b43f12013-08-20 11:48:36 +0200229/* END_CASE */