mbed TLS Build

Dependents:   Slave-prot-prod

Committer:
williequesada
Date:
Tue Jun 04 16:03:38 2019 +0000
Revision:
1:1a219dea6cb5
Parent:
0:cdf462088d13
compartir a Pablo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
markrad 0:cdf462088d13 1 /* BEGIN_HEADER */
markrad 0:cdf462088d13 2 #include "mbedtls/aes.h"
markrad 0:cdf462088d13 3 /* END_HEADER */
markrad 0:cdf462088d13 4
markrad 0:cdf462088d13 5 /* BEGIN_DEPENDENCIES
markrad 0:cdf462088d13 6 * depends_on:MBEDTLS_AES_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 aes_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_aes_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_aes_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_aes_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_aes_crypt_ecb( &ctx, MBEDTLS_AES_ENCRYPT, src_str, output ) == 0 );
markrad 0:cdf462088d13 34 hexify( dst_str, output, 16 );
markrad 0:cdf462088d13 35
markrad 0:cdf462088d13 36 TEST_ASSERT( strcmp( (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_aes_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 aes_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_aes_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_aes_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_aes_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_aes_crypt_ecb( &ctx, MBEDTLS_AES_DECRYPT, src_str, output ) == 0 );
markrad 0:cdf462088d13 68 hexify( dst_str, output, 16 );
markrad 0:cdf462088d13 69
markrad 0:cdf462088d13 70 TEST_ASSERT( strcmp( (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_aes_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 aes_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_aes_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_aes_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_aes_setkey_enc( &ctx, key_str, key_len * 8 );
markrad 0:cdf462088d13 103 TEST_ASSERT( mbedtls_aes_crypt_cbc( &ctx, MBEDTLS_AES_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( strcmp( (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_aes_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 aes_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_aes_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_aes_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_aes_setkey_dec( &ctx, key_str, key_len * 8 );
markrad 0:cdf462088d13 141 TEST_ASSERT( mbedtls_aes_crypt_cbc( &ctx, MBEDTLS_AES_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( strcmp( (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_aes_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 aes_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_aes_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_aes_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_aes_setkey_enc( &ctx, key_str, key_len * 8 );
markrad 0:cdf462088d13 179 TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_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( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
markrad 0:cdf462088d13 183
markrad 0:cdf462088d13 184 exit:
markrad 0:cdf462088d13 185 mbedtls_aes_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 aes_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_aes_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_aes_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_aes_setkey_enc( &ctx, key_str, key_len * 8 );
markrad 0:cdf462088d13 214 TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_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( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
markrad 0:cdf462088d13 218
markrad 0:cdf462088d13 219 exit:
markrad 0:cdf462088d13 220 mbedtls_aes_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_CIPHER_MODE_CFB */
markrad 0:cdf462088d13 225 void aes_encrypt_cfb8( char *hex_key_string, char *hex_iv_string,
markrad 0:cdf462088d13 226 char *hex_src_string, char *hex_dst_string )
markrad 0:cdf462088d13 227 {
markrad 0:cdf462088d13 228 unsigned char key_str[100];
markrad 0:cdf462088d13 229 unsigned char iv_str[100];
markrad 0:cdf462088d13 230 unsigned char src_str[100];
markrad 0:cdf462088d13 231 unsigned char dst_str[100];
markrad 0:cdf462088d13 232 unsigned char output[100];
markrad 0:cdf462088d13 233 mbedtls_aes_context ctx;
markrad 0:cdf462088d13 234 int key_len, src_len;
markrad 0:cdf462088d13 235
markrad 0:cdf462088d13 236 memset(key_str, 0x00, 100);
markrad 0:cdf462088d13 237 memset(iv_str, 0x00, 100);
markrad 0:cdf462088d13 238 memset(src_str, 0x00, 100);
markrad 0:cdf462088d13 239 memset(dst_str, 0x00, 100);
markrad 0:cdf462088d13 240 memset(output, 0x00, 100);
markrad 0:cdf462088d13 241 mbedtls_aes_init( &ctx );
markrad 0:cdf462088d13 242
markrad 0:cdf462088d13 243 key_len = unhexify( key_str, hex_key_string );
markrad 0:cdf462088d13 244 unhexify( iv_str, hex_iv_string );
markrad 0:cdf462088d13 245 src_len = unhexify( src_str, hex_src_string );
markrad 0:cdf462088d13 246
markrad 0:cdf462088d13 247 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
markrad 0:cdf462088d13 248 TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_ENCRYPT, src_len, iv_str, src_str, output ) == 0 );
markrad 0:cdf462088d13 249 hexify( dst_str, output, src_len );
markrad 0:cdf462088d13 250
markrad 0:cdf462088d13 251 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
markrad 0:cdf462088d13 252
markrad 0:cdf462088d13 253 exit:
markrad 0:cdf462088d13 254 mbedtls_aes_free( &ctx );
markrad 0:cdf462088d13 255 }
markrad 0:cdf462088d13 256 /* END_CASE */
markrad 0:cdf462088d13 257
markrad 0:cdf462088d13 258 /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */
markrad 0:cdf462088d13 259 void aes_decrypt_cfb8( char *hex_key_string, char *hex_iv_string,
markrad 0:cdf462088d13 260 char *hex_src_string, char *hex_dst_string )
markrad 0:cdf462088d13 261 {
markrad 0:cdf462088d13 262 unsigned char key_str[100];
markrad 0:cdf462088d13 263 unsigned char iv_str[100];
markrad 0:cdf462088d13 264 unsigned char src_str[100];
markrad 0:cdf462088d13 265 unsigned char dst_str[100];
markrad 0:cdf462088d13 266 unsigned char output[100];
markrad 0:cdf462088d13 267 mbedtls_aes_context ctx;
markrad 0:cdf462088d13 268 int key_len, src_len;
markrad 0:cdf462088d13 269
markrad 0:cdf462088d13 270 memset(key_str, 0x00, 100);
markrad 0:cdf462088d13 271 memset(iv_str, 0x00, 100);
markrad 0:cdf462088d13 272 memset(src_str, 0x00, 100);
markrad 0:cdf462088d13 273 memset(dst_str, 0x00, 100);
markrad 0:cdf462088d13 274 memset(output, 0x00, 100);
markrad 0:cdf462088d13 275 mbedtls_aes_init( &ctx );
markrad 0:cdf462088d13 276
markrad 0:cdf462088d13 277 key_len = unhexify( key_str, hex_key_string );
markrad 0:cdf462088d13 278 unhexify( iv_str, hex_iv_string );
markrad 0:cdf462088d13 279 src_len = unhexify( src_str, hex_src_string );
markrad 0:cdf462088d13 280
markrad 0:cdf462088d13 281 mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 );
markrad 0:cdf462088d13 282 TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_DECRYPT, src_len, iv_str, src_str, output ) == 0 );
markrad 0:cdf462088d13 283 hexify( dst_str, output, src_len );
markrad 0:cdf462088d13 284
markrad 0:cdf462088d13 285 TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 );
markrad 0:cdf462088d13 286
markrad 0:cdf462088d13 287 exit:
markrad 0:cdf462088d13 288 mbedtls_aes_free( &ctx );
markrad 0:cdf462088d13 289 }
markrad 0:cdf462088d13 290 /* END_CASE */
markrad 0:cdf462088d13 291
markrad 0:cdf462088d13 292 /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */
markrad 0:cdf462088d13 293 void aes_selftest()
markrad 0:cdf462088d13 294 {
markrad 0:cdf462088d13 295 TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 );
markrad 0:cdf462088d13 296 }
markrad 0:cdf462088d13 297 /* END_CASE */