1/* BEGIN_HEADER */
2#include "mbedtls/xtea.h"
3/* END_HEADER */
4
5/* BEGIN_DEPENDENCIES
6 * depends_on:MBEDTLS_XTEA_C
7 * END_DEPENDENCIES
8 */
9
10/* BEGIN_CASE */
11void xtea_encrypt_ecb( char *hex_key_string, char *hex_src_string,
12                       char *hex_dst_string )
13{
14    unsigned char key_str[100];
15    unsigned char src_str[100];
16    unsigned char dst_str[100];
17    unsigned char output[100];
18    mbedtls_xtea_context ctx;
19
20    memset(key_str, 0x00, 100);
21    memset(src_str, 0x00, 100);
22    memset(dst_str, 0x00, 100);
23    memset(output, 0x00, 100);
24
25    unhexify( key_str, hex_key_string );
26    unhexify( src_str, hex_src_string );
27
28    mbedtls_xtea_setup( &ctx, key_str );
29    TEST_ASSERT( mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_ENCRYPT, src_str, output ) == 0 );
30    hexify( dst_str, output, 8 );
31
32    TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
33}
34/* END_CASE */
35
36/* BEGIN_CASE */
37void xtea_decrypt_ecb( char *hex_key_string, char *hex_src_string,
38                       char *hex_dst_string )
39{
40    unsigned char key_str[100];
41    unsigned char src_str[100];
42    unsigned char dst_str[100];
43    unsigned char output[100];
44    mbedtls_xtea_context ctx;
45
46    memset(key_str, 0x00, 100);
47    memset(src_str, 0x00, 100);
48    memset(dst_str, 0x00, 100);
49    memset(output, 0x00, 100);
50
51    unhexify( key_str, hex_key_string );
52    unhexify( src_str, hex_src_string );
53
54    mbedtls_xtea_setup( &ctx, key_str );
55    TEST_ASSERT( mbedtls_xtea_crypt_ecb( &ctx, MBEDTLS_XTEA_DECRYPT, src_str, output ) == 0 );
56    hexify( dst_str, output, 8 );
57
58    TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
59}
60/* END_CASE */
61
62/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
63void 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;
72    mbedtls_xtea_context ctx;
73
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
84    mbedtls_xtea_setup( &ctx, key_str );
85    TEST_ASSERT( mbedtls_xtea_crypt_cbc( &ctx, MBEDTLS_XTEA_ENCRYPT, len, iv_str,
86                                 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
93/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
94void 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;
103    mbedtls_xtea_context ctx;
104
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
115    mbedtls_xtea_setup( &ctx, key_str );
116    TEST_ASSERT( mbedtls_xtea_crypt_cbc( &ctx, MBEDTLS_XTEA_DECRYPT, len, iv_str,
117                                 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
124/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
125void xtea_selftest()
126{
127    TEST_ASSERT( mbedtls_xtea_self_test( 1 ) == 0 );
128}
129/* END_CASE */
130