mbed TLS Build

Dependents:   Encrypt_Decrypt1 mbed_blink_tls encrypt encrypt

Committer:
markrad
Date:
Thu Jan 05 00:18:44 2017 +0000
Revision:
0:cdf462088d13
Initial commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /* BEGIN_HEADER */
markrad 0:cdf462088d13 2 #include "mbedtls/camellia.h"
markrad 0:cdf462088d13 3 /* END_HEADER */
markrad 0:cdf462088d13 4
markrad 0:cdf462088d13 5 /* BEGIN_DEPENDENCIES
markrad 0:cdf462088d13 6 * depends_on:MBEDTLS_CAMELLIA_C
markrad 0:cdf462088d13 7 * END_DEPENDENCIES
markrad 0:cdf462088d13 8 */
markrad 0:cdf462088d13 9
markrad 0:cdf462088d13 10 /* BEGIN_CASE */
markrad 0:cdf462088d13 11 void camellia_encrypt_ecb( char *hex_key_string, char *hex_src_string,
markrad 0:cdf462088d13 12 char *hex_dst_string, int setkey_result )
markrad 0:cdf462088d13 13 {
markrad 0:cdf462088d13 14 unsigned char key_str[100];
markrad 0:cdf462088d13 15 unsigned char src_str[100];
markrad 0:cdf462088d13 16 unsigned char dst_str[100];
markrad 0:cdf462088d13 17 unsigned char output[100];
markrad 0:cdf462088d13 18 mbedtls_camellia_context ctx;
markrad 0:cdf462088d13 19 int key_len;
markrad 0:cdf462088d13 20
markrad 0:cdf462088d13 21 memset(key_str, 0x00, 100);
markrad 0:cdf462088d13 22 memset(src_str, 0x00, 100);
markrad 0:cdf462088d13 23 memset(dst_str, 0x00, 100);
markrad 0:cdf462088d13 24 memset(output, 0x00, 100);
markrad 0:cdf462088d13 25 mbedtls_camellia_init( &ctx );
markrad 0:cdf462088d13 26
markrad 0:cdf462088d13 27 key_len = unhexify( key_str, hex_key_string );
markrad 0:cdf462088d13 28 unhexify( src_str, hex_src_string );
markrad 0:cdf462088d13 29
markrad 0:cdf462088d13 30 TEST_ASSERT( mbedtls_camellia_setkey_enc( &ctx, key_str, key_len * 8 ) == setkey_result );
markrad 0:cdf462088d13 31 if( setkey_result == 0 )
markrad 0:cdf462088d13 32 {
markrad 0:cdf462088d13 33 TEST_ASSERT( mbedtls_camellia_crypt_ecb( &ctx, MBEDTLS_CAMELLIA_ENCRYPT, src_str, output ) == 0 );
markrad 0:cdf462088d13 34 hexify( dst_str, output, 16 );
markrad 0:cdf462088d13 35
markrad 0:cdf462088d13 36 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
markrad 0:cdf462088d13 37 }
markrad 0:cdf462088d13 38
markrad 0:cdf462088d13 39 exit:
markrad 0:cdf462088d13 40 mbedtls_camellia_free( &ctx );
markrad 0:cdf462088d13 41 }
markrad 0:cdf462088d13 42 /* END_CASE */
markrad 0:cdf462088d13 43
markrad 0:cdf462088d13 44 /* BEGIN_CASE */
markrad 0:cdf462088d13 45 void camellia_decrypt_ecb( char *hex_key_string, char *hex_src_string,
markrad 0:cdf462088d13 46 char *hex_dst_string, int setkey_result )
markrad 0:cdf462088d13 47 {
markrad 0:cdf462088d13 48 unsigned char key_str[100];
markrad 0:cdf462088d13 49 unsigned char src_str[100];
markrad 0:cdf462088d13 50 unsigned char dst_str[100];
markrad 0:cdf462088d13 51 unsigned char output[100];
markrad 0:cdf462088d13 52 mbedtls_camellia_context ctx;
markrad 0:cdf462088d13 53 int key_len;
markrad 0:cdf462088d13 54
markrad 0:cdf462088d13 55 memset(key_str, 0x00, 100);
markrad 0:cdf462088d13 56 memset(src_str, 0x00, 100);
markrad 0:cdf462088d13 57 memset(dst_str, 0x00, 100);
markrad 0:cdf462088d13 58 memset(output, 0x00, 100);
markrad 0:cdf462088d13 59 mbedtls_camellia_init( &ctx );
markrad 0:cdf462088d13 60
markrad 0:cdf462088d13 61 key_len = unhexify( key_str, hex_key_string );
markrad 0:cdf462088d13 62 unhexify( src_str, hex_src_string );
markrad 0:cdf462088d13 63
markrad 0:cdf462088d13 64 TEST_ASSERT( mbedtls_camellia_setkey_dec( &ctx, key_str, key_len * 8 ) == setkey_result );
markrad 0:cdf462088d13 65 if( setkey_result == 0 )
markrad 0:cdf462088d13 66 {
markrad 0:cdf462088d13 67 TEST_ASSERT( mbedtls_camellia_crypt_ecb( &ctx, MBEDTLS_CAMELLIA_DECRYPT, src_str, output ) == 0 );
markrad 0:cdf462088d13 68 hexify( dst_str, output, 16 );
markrad 0:cdf462088d13 69
markrad 0:cdf462088d13 70 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
markrad 0:cdf462088d13 71 }
markrad 0:cdf462088d13 72
markrad 0:cdf462088d13 73 exit:
markrad 0:cdf462088d13 74 mbedtls_camellia_free( &ctx );
markrad 0:cdf462088d13 75 }
markrad 0:cdf462088d13 76 /* END_CASE */
markrad 0:cdf462088d13 77
markrad 0:cdf462088d13 78 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
markrad 0:cdf462088d13 79 void camellia_encrypt_cbc( char *hex_key_string, char *hex_iv_string,
markrad 0:cdf462088d13 80 char *hex_src_string, char *hex_dst_string,
markrad 0:cdf462088d13 81 int cbc_result )
markrad 0:cdf462088d13 82 {
markrad 0:cdf462088d13 83 unsigned char key_str[100];
markrad 0:cdf462088d13 84 unsigned char iv_str[100];
markrad 0:cdf462088d13 85 unsigned char src_str[100];
markrad 0:cdf462088d13 86 unsigned char dst_str[100];
markrad 0:cdf462088d13 87 unsigned char output[100];
markrad 0:cdf462088d13 88 mbedtls_camellia_context ctx;
markrad 0:cdf462088d13 89 int key_len, data_len;
markrad 0:cdf462088d13 90
markrad 0:cdf462088d13 91 memset(key_str, 0x00, 100);
markrad 0:cdf462088d13 92 memset(iv_str, 0x00, 100);
markrad 0:cdf462088d13 93 memset(src_str, 0x00, 100);
markrad 0:cdf462088d13 94 memset(dst_str, 0x00, 100);
markrad 0:cdf462088d13 95 memset(output, 0x00, 100);
markrad 0:cdf462088d13 96 mbedtls_camellia_init( &ctx );
markrad 0:cdf462088d13 97
markrad 0:cdf462088d13 98 key_len = unhexify( key_str, hex_key_string );
markrad 0:cdf462088d13 99 unhexify( iv_str, hex_iv_string );
markrad 0:cdf462088d13 100 data_len = unhexify( src_str, hex_src_string );
markrad 0:cdf462088d13 101
markrad 0:cdf462088d13 102 mbedtls_camellia_setkey_enc( &ctx, key_str, key_len * 8 );
markrad 0:cdf462088d13 103 TEST_ASSERT( mbedtls_camellia_crypt_cbc( &ctx, MBEDTLS_CAMELLIA_ENCRYPT, data_len, iv_str, src_str, output) == cbc_result );
markrad 0:cdf462088d13 104 if( cbc_result == 0 )
markrad 0:cdf462088d13 105 {
markrad 0:cdf462088d13 106 hexify( dst_str, output, data_len );
markrad 0:cdf462088d13 107
markrad 0:cdf462088d13 108 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
markrad 0:cdf462088d13 109 }
markrad 0:cdf462088d13 110
markrad 0:cdf462088d13 111 exit:
markrad 0:cdf462088d13 112 mbedtls_camellia_free( &ctx );
markrad 0:cdf462088d13 113 }
markrad 0:cdf462088d13 114 /* END_CASE */
markrad 0:cdf462088d13 115
markrad 0:cdf462088d13 116 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
markrad 0:cdf462088d13 117 void camellia_decrypt_cbc( char *hex_key_string, char *hex_iv_string,
markrad 0:cdf462088d13 118 char *hex_src_string, char *hex_dst_string,
markrad 0:cdf462088d13 119 int cbc_result )
markrad 0:cdf462088d13 120 {
markrad 0:cdf462088d13 121 unsigned char key_str[100];
markrad 0:cdf462088d13 122 unsigned char iv_str[100];
markrad 0:cdf462088d13 123 unsigned char src_str[100];
markrad 0:cdf462088d13 124 unsigned char dst_str[100];
markrad 0:cdf462088d13 125 unsigned char output[100];
markrad 0:cdf462088d13 126 mbedtls_camellia_context ctx;
markrad 0:cdf462088d13 127 int key_len, data_len;
markrad 0:cdf462088d13 128
markrad 0:cdf462088d13 129 memset(key_str, 0x00, 100);
markrad 0:cdf462088d13 130 memset(iv_str, 0x00, 100);
markrad 0:cdf462088d13 131 memset(src_str, 0x00, 100);
markrad 0:cdf462088d13 132 memset(dst_str, 0x00, 100);
markrad 0:cdf462088d13 133 memset(output, 0x00, 100);
markrad 0:cdf462088d13 134 mbedtls_camellia_init( &ctx );
markrad 0:cdf462088d13 135
markrad 0:cdf462088d13 136 key_len = unhexify( key_str, hex_key_string );
markrad 0:cdf462088d13 137 unhexify( iv_str, hex_iv_string );
markrad 0:cdf462088d13 138 data_len = unhexify( src_str, hex_src_string );
markrad 0:cdf462088d13 139
markrad 0:cdf462088d13 140 mbedtls_camellia_setkey_dec( &ctx, key_str, key_len * 8 );
markrad 0:cdf462088d13 141 TEST_ASSERT( mbedtls_camellia_crypt_cbc( &ctx, MBEDTLS_CAMELLIA_DECRYPT, data_len, iv_str, src_str, output ) == cbc_result );
markrad 0:cdf462088d13 142 if( cbc_result == 0 )
markrad 0:cdf462088d13 143 {
markrad 0:cdf462088d13 144 hexify( dst_str, output, data_len );
markrad 0:cdf462088d13 145
markrad 0:cdf462088d13 146 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
markrad 0:cdf462088d13 147 }
markrad 0:cdf462088d13 148
markrad 0:cdf462088d13 149 exit:
markrad 0:cdf462088d13 150 mbedtls_camellia_free( &ctx );
markrad 0:cdf462088d13 151 }
markrad 0:cdf462088d13 152 /* END_CASE */
markrad 0:cdf462088d13 153
markrad 0:cdf462088d13 154 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
markrad 0:cdf462088d13 155 void camellia_encrypt_cfb128( char *hex_key_string, char *hex_iv_string,
markrad 0:cdf462088d13 156 char *hex_src_string, char *hex_dst_string )
markrad 0:cdf462088d13 157 {
markrad 0:cdf462088d13 158 unsigned char key_str[100];
markrad 0:cdf462088d13 159 unsigned char iv_str[100];
markrad 0:cdf462088d13 160 unsigned char src_str[100];
markrad 0:cdf462088d13 161 unsigned char dst_str[100];
markrad 0:cdf462088d13 162 unsigned char output[100];
markrad 0:cdf462088d13 163 mbedtls_camellia_context ctx;
markrad 0:cdf462088d13 164 size_t iv_offset = 0;
markrad 0:cdf462088d13 165 int key_len;
markrad 0:cdf462088d13 166
markrad 0:cdf462088d13 167 memset(key_str, 0x00, 100);
markrad 0:cdf462088d13 168 memset(iv_str, 0x00, 100);
markrad 0:cdf462088d13 169 memset(src_str, 0x00, 100);
markrad 0:cdf462088d13 170 memset(dst_str, 0x00, 100);
markrad 0:cdf462088d13 171 memset(output, 0x00, 100);
markrad 0:cdf462088d13 172 mbedtls_camellia_init( &ctx );
markrad 0:cdf462088d13 173
markrad 0:cdf462088d13 174 key_len = unhexify( key_str, hex_key_string );
markrad 0:cdf462088d13 175 unhexify( iv_str, hex_iv_string );
markrad 0:cdf462088d13 176 unhexify( src_str, hex_src_string );
markrad 0:cdf462088d13 177
markrad 0:cdf462088d13 178 mbedtls_camellia_setkey_enc( &ctx, key_str, key_len * 8 );
markrad 0:cdf462088d13 179 TEST_ASSERT( mbedtls_camellia_crypt_cfb128( &ctx, MBEDTLS_CAMELLIA_ENCRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 );
markrad 0:cdf462088d13 180 hexify( dst_str, output, 16 );
markrad 0:cdf462088d13 181
markrad 0:cdf462088d13 182 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
markrad 0:cdf462088d13 183
markrad 0:cdf462088d13 184 exit:
markrad 0:cdf462088d13 185 mbedtls_camellia_free( &ctx );
markrad 0:cdf462088d13 186 }
markrad 0:cdf462088d13 187 /* END_CASE */
markrad 0:cdf462088d13 188
markrad 0:cdf462088d13 189 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
markrad 0:cdf462088d13 190 void camellia_decrypt_cfb128( char *hex_key_string, char *hex_iv_string,
markrad 0:cdf462088d13 191 char *hex_src_string, char *hex_dst_string )
markrad 0:cdf462088d13 192 {
markrad 0:cdf462088d13 193 unsigned char key_str[100];
markrad 0:cdf462088d13 194 unsigned char iv_str[100];
markrad 0:cdf462088d13 195 unsigned char src_str[100];
markrad 0:cdf462088d13 196 unsigned char dst_str[100];
markrad 0:cdf462088d13 197 unsigned char output[100];
markrad 0:cdf462088d13 198 mbedtls_camellia_context ctx;
markrad 0:cdf462088d13 199 size_t iv_offset = 0;
markrad 0:cdf462088d13 200 int key_len;
markrad 0:cdf462088d13 201
markrad 0:cdf462088d13 202 memset(key_str, 0x00, 100);
markrad 0:cdf462088d13 203 memset(iv_str, 0x00, 100);
markrad 0:cdf462088d13 204 memset(src_str, 0x00, 100);
markrad 0:cdf462088d13 205 memset(dst_str, 0x00, 100);
markrad 0:cdf462088d13 206 memset(output, 0x00, 100);
markrad 0:cdf462088d13 207 mbedtls_camellia_init( &ctx );
markrad 0:cdf462088d13 208
markrad 0:cdf462088d13 209 key_len = unhexify( key_str, hex_key_string );
markrad 0:cdf462088d13 210 unhexify( iv_str, hex_iv_string );
markrad 0:cdf462088d13 211 unhexify( src_str, hex_src_string );
markrad 0:cdf462088d13 212
markrad 0:cdf462088d13 213 mbedtls_camellia_setkey_enc( &ctx, key_str, key_len * 8 );
markrad 0:cdf462088d13 214 TEST_ASSERT( mbedtls_camellia_crypt_cfb128( &ctx, MBEDTLS_CAMELLIA_DECRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 );
markrad 0:cdf462088d13 215 hexify( dst_str, output, 16 );
markrad 0:cdf462088d13 216
markrad 0:cdf462088d13 217 TEST_ASSERT( strcasecmp( (char *) dst_str, hex_dst_string ) == 0 );
markrad 0:cdf462088d13 218
markrad 0:cdf462088d13 219 exit:
markrad 0:cdf462088d13 220 mbedtls_camellia_free( &ctx );
markrad 0:cdf462088d13 221 }
markrad 0:cdf462088d13 222 /* END_CASE */
markrad 0:cdf462088d13 223
markrad 0:cdf462088d13 224 /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
markrad 0:cdf462088d13 225 void camellia_selftest()
markrad 0:cdf462088d13 226 {
markrad 0:cdf462088d13 227 TEST_ASSERT( mbedtls_camellia_self_test( 1 ) == 0 );
markrad 0:cdf462088d13 228 }
markrad 0:cdf462088d13 229 /* END_CASE */