blob: cbc714a12fe64b856ce1524c1f80b542dc963df7 [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/xtea.h"
Paul Bakker33b43f12013-08-20 11:48:36 +02003/* END_HEADER */
Paul Bakkerf725a882009-07-08 06:43:10 +00004
Paul Bakker33b43f12013-08-20 11:48:36 +02005/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02006 * depends_on:MBEDTLS_XTEA_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 xtea_encrypt_ecb( char *hex_key_string, char *hex_src_string,
12 char *hex_dst_string )
Paul Bakkerf725a882009-07-08 06:43:10 +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_xtea_context ctx;
Paul Bakkerf725a882009-07-08 06:43:10 +000019
20 memset(key_str, 0x00, 100);
21 memset(src_str, 0x00, 100);
22 memset(dst_str, 0x00, 100);
23 memset(output, 0x00, 100);
24
Paul Bakker33b43f12013-08-20 11:48:36 +020025 unhexify( key_str, hex_key_string );
26 unhexify( src_str, hex_src_string );
Paul Bakkerf725a882009-07-08 06:43:10 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028 mbedtls_xtea_setup( &ctx, key_str );
29 TEST_ASSERT( mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_ENCRYPT, src_str, output ) == 0 );
Paul Bakkerf725a882009-07-08 06:43:10 +000030 hexify( dst_str, output, 8 );
31
Paul Bakker33b43f12013-08-20 11:48:36 +020032 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf725a882009-07-08 06:43:10 +000033}
Paul Bakker33b43f12013-08-20 11:48:36 +020034/* END_CASE */
Paul Bakkerf725a882009-07-08 06:43:10 +000035
Paul Bakker33b43f12013-08-20 11:48:36 +020036/* BEGIN_CASE */
37void xtea_decrypt_ecb( char *hex_key_string, char *hex_src_string,
38 char *hex_dst_string )
Paul Bakkerf725a882009-07-08 06:43:10 +000039{
40 unsigned char key_str[100];
41 unsigned char src_str[100];
42 unsigned char dst_str[100];
43 unsigned char output[100];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020044 mbedtls_xtea_context ctx;
Paul Bakkerf725a882009-07-08 06:43:10 +000045
46 memset(key_str, 0x00, 100);
47 memset(src_str, 0x00, 100);
48 memset(dst_str, 0x00, 100);
49 memset(output, 0x00, 100);
50
Paul Bakker33b43f12013-08-20 11:48:36 +020051 unhexify( key_str, hex_key_string );
52 unhexify( src_str, hex_src_string );
Paul Bakkerf725a882009-07-08 06:43:10 +000053
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020054 mbedtls_xtea_setup( &ctx, key_str );
55 TEST_ASSERT( mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_DECRYPT, src_str, output ) == 0 );
Paul Bakkerf725a882009-07-08 06:43:10 +000056 hexify( dst_str, output, 8 );
57
Paul Bakker33b43f12013-08-20 11:48:36 +020058 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
Paul Bakkerf725a882009-07-08 06:43:10 +000059}
Paul Bakker33b43f12013-08-20 11:48:36 +020060/* END_CASE */
Paul Bakkerf725a882009-07-08 06:43:10 +000061
Simon Butcher02c4a382016-06-23 02:41:31 +010062/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard7b4919c2014-05-29 18:26:53 +020063void xtea_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
64 char *hex_src_string, char *hex_dst_string )
65{
66 unsigned char key_str[100];
67 unsigned char src_str[100];
68 unsigned char dst_str[100];
69 unsigned char iv_str[100];
70 unsigned char output[100];
71 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 mbedtls_xtea_context ctx;
Manuel Pégourié-Gonnard7b4919c2014-05-29 18:26:53 +020073
74 memset(key_str, 0x00, 100);
75 memset(src_str, 0x00, 100);
76 memset(dst_str, 0x00, 100);
77 memset(iv_str, 0x00, 100);
78 memset(output, 0x00, 100);
79
80 unhexify( key_str, hex_key_string );
81 unhexify( iv_str, hex_iv_string );
82 len = unhexify( src_str, hex_src_string );
83
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020084 mbedtls_xtea_setup( &ctx, key_str );
85 TEST_ASSERT( mbedtls_xtea_crypt_cbc( &ctx, MBEDTLS_XTEA_ENCRYPT, len, iv_str,
Manuel Pégourié-Gonnard7b4919c2014-05-29 18:26:53 +020086 src_str, output ) == 0 );
87 hexify( dst_str, output, len );
88
89 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
90}
91/* END_CASE */
92
Simon Butcher02c4a382016-06-23 02:41:31 +010093/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Manuel Pégourié-Gonnard7b4919c2014-05-29 18:26:53 +020094void xtea_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
95 char *hex_src_string, char *hex_dst_string )
96{
97 unsigned char key_str[100];
98 unsigned char src_str[100];
99 unsigned char dst_str[100];
100 unsigned char iv_str[100];
101 unsigned char output[100];
102 size_t len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_xtea_context ctx;
Manuel Pégourié-Gonnard7b4919c2014-05-29 18:26:53 +0200104
105 memset(key_str, 0x00, 100);
106 memset(src_str, 0x00, 100);
107 memset(dst_str, 0x00, 100);
108 memset(iv_str, 0x00, 100);
109 memset(output, 0x00, 100);
110
111 unhexify( key_str, hex_key_string );
112 unhexify( iv_str, hex_iv_string );
113 len = unhexify( src_str, hex_src_string );
114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_xtea_setup( &ctx, key_str );
116 TEST_ASSERT( mbedtls_xtea_crypt_cbc( &ctx, MBEDTLS_XTEA_DECRYPT, len, iv_str,
Manuel Pégourié-Gonnard7b4919c2014-05-29 18:26:53 +0200117 src_str, output ) == 0 );
118 hexify( dst_str, output, len );
119
120 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
121}
122/* END_CASE */
123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
Paul Bakker33b43f12013-08-20 11:48:36 +0200125void xtea_selftest()
Paul Bakkerf725a882009-07-08 06:43:10 +0000126{
Andres AG93012e82016-09-09 09:10:28 +0100127 TEST_ASSERT( mbedtls_xtea_self_test( 1 ) == 0 );
Paul Bakkerf725a882009-07-08 06:43:10 +0000128}
Paul Bakker33b43f12013-08-20 11:48:36 +0200129/* END_CASE */