Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 1 | /* BEGIN_HEADER */ |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 2 | #include "mbedtls/aes.h" |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 3 | /* END_HEADER */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 4 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 5 | /* BEGIN_DEPENDENCIES |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 6 | * depends_on:MBEDTLS_AES_C |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 7 | * END_DEPENDENCIES |
| 8 | */ |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 9 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 10 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 11 | void aes_encrypt_ecb( data_t * key_str, data_t * src_str, |
| 12 | data_t * hex_dst_string, int setkey_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 13 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 14 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 15 | mbedtls_aes_context ctx; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 16 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 17 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 18 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 19 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 20 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 21 | TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 ) == setkey_result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 22 | if( setkey_result == 0 ) |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 23 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 24 | TEST_ASSERT( mbedtls_aes_crypt_ecb( &ctx, MBEDTLS_AES_ENCRYPT, src_str->x, output ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 25 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 26 | TEST_ASSERT( hexcmp( output, hex_dst_string->x, 16, hex_dst_string->len ) == 0 ); |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 27 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 28 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 29 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 31 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 32 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 33 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 34 | /* BEGIN_CASE */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 35 | void aes_decrypt_ecb( data_t * key_str, data_t * src_str, |
| 36 | data_t * hex_dst_string, int setkey_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 37 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 38 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 39 | mbedtls_aes_context ctx; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 40 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 41 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 42 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 43 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 44 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 45 | TEST_ASSERT( mbedtls_aes_setkey_dec( &ctx, key_str->x, key_str->len * 8 ) == setkey_result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 46 | if( setkey_result == 0 ) |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 47 | { |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 48 | TEST_ASSERT( mbedtls_aes_crypt_ecb( &ctx, MBEDTLS_AES_DECRYPT, src_str->x, output ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 49 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 50 | TEST_ASSERT( hexcmp( output, hex_dst_string->x, 16, hex_dst_string->len ) == 0 ); |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 51 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 52 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 53 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 54 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 55 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 56 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 57 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 58 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 59 | void aes_encrypt_cbc( data_t * key_str, data_t * iv_str, |
| 60 | data_t * src_str, data_t * hex_dst_string, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 61 | int cbc_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 62 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 63 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 64 | mbedtls_aes_context ctx; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 65 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 66 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 67 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 68 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 69 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 70 | mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 ); |
| 71 | TEST_ASSERT( mbedtls_aes_crypt_cbc( &ctx, MBEDTLS_AES_ENCRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 72 | if( cbc_result == 0 ) |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 73 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 74 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 75 | TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 ); |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 76 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 77 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 78 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 79 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 80 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 81 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 82 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 83 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 84 | void aes_decrypt_cbc( data_t * key_str, data_t * iv_str, |
| 85 | data_t * src_str, data_t * hex_dst_string, |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 86 | int cbc_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 87 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 88 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 89 | mbedtls_aes_context ctx; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 90 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 91 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 92 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 93 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 94 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 95 | mbedtls_aes_setkey_dec( &ctx, key_str->x, key_str->len * 8 ); |
| 96 | TEST_ASSERT( mbedtls_aes_crypt_cbc( &ctx, MBEDTLS_AES_DECRYPT, src_str->len, iv_str->x, src_str->x, output ) == cbc_result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 97 | if( cbc_result == 0) |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 98 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 99 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 100 | TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 ); |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 101 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 102 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 103 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 104 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 105 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 106 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 107 | |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 108 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
Jaeden Amero | cd9fc5e | 2018-05-30 15:23:24 +0100 | [diff] [blame] | 109 | void aes_encrypt_xts( char *hex_key_string, char *hex_data_unit_string, |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 110 | char *hex_src_string, char *hex_dst_string ) |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 111 | { |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 112 | enum { AES_BLOCK_SIZE = 16 }; |
| 113 | unsigned char *data_unit = NULL; |
| 114 | unsigned char *key = NULL; |
| 115 | unsigned char *src = NULL; |
| 116 | unsigned char *dst = NULL; |
| 117 | unsigned char *output = NULL; |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 118 | mbedtls_aes_xts_context ctx; |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 119 | size_t key_len, src_len, dst_len, data_unit_len; |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 120 | |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 121 | mbedtls_aes_xts_init( &ctx ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 122 | |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 123 | data_unit = unhexify_alloc( hex_data_unit_string, &data_unit_len ); |
| 124 | TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 125 | |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 126 | key = unhexify_alloc( hex_key_string, &key_len ); |
| 127 | TEST_ASSERT( key_len % 2 == 0 ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 128 | |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 129 | src = unhexify_alloc( hex_src_string, &src_len ); |
| 130 | dst = unhexify_alloc( hex_dst_string, &dst_len ); |
| 131 | TEST_ASSERT( src_len == dst_len ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 132 | |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 133 | output = zero_alloc( dst_len ); |
| 134 | |
| 135 | TEST_ASSERT( mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ) == 0 ); |
| 136 | TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, src_len, |
| 137 | data_unit, src, output ) == 0 ); |
| 138 | |
| 139 | TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 140 | |
| 141 | exit: |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 142 | mbedtls_aes_xts_free( &ctx ); |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 143 | mbedtls_free( data_unit ); |
| 144 | mbedtls_free( key ); |
| 145 | mbedtls_free( src ); |
| 146 | mbedtls_free( dst ); |
| 147 | mbedtls_free( output ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 148 | } |
| 149 | /* END_CASE */ |
| 150 | |
| 151 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
Jaeden Amero | cd9fc5e | 2018-05-30 15:23:24 +0100 | [diff] [blame] | 152 | void aes_decrypt_xts( char *hex_key_string, char *hex_data_unit_string, |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 153 | char *hex_dst_string, char *hex_src_string ) |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 154 | { |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 155 | enum { AES_BLOCK_SIZE = 16 }; |
| 156 | unsigned char *data_unit = NULL; |
| 157 | unsigned char *key = NULL; |
| 158 | unsigned char *src = NULL; |
| 159 | unsigned char *dst = NULL; |
| 160 | unsigned char *output = NULL; |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 161 | mbedtls_aes_xts_context ctx; |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 162 | size_t key_len, src_len, dst_len, data_unit_len; |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 163 | |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 164 | mbedtls_aes_xts_init( &ctx ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 165 | |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 166 | data_unit = unhexify_alloc( hex_data_unit_string, &data_unit_len ); |
| 167 | TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 168 | |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 169 | key = unhexify_alloc( hex_key_string, &key_len ); |
| 170 | TEST_ASSERT( key_len % 2 == 0 ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 171 | |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 172 | src = unhexify_alloc( hex_src_string, &src_len ); |
| 173 | dst = unhexify_alloc( hex_dst_string, &dst_len ); |
| 174 | TEST_ASSERT( src_len == dst_len ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 175 | |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 176 | output = zero_alloc( dst_len ); |
| 177 | |
| 178 | TEST_ASSERT( mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ) == 0 ); |
| 179 | TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_DECRYPT, src_len, |
| 180 | data_unit, src, output ) == 0 ); |
| 181 | |
| 182 | TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 183 | |
| 184 | exit: |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 185 | mbedtls_aes_xts_free( &ctx ); |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 186 | mbedtls_free( data_unit ); |
| 187 | mbedtls_free( key ); |
| 188 | mbedtls_free( src ); |
| 189 | mbedtls_free( dst ); |
| 190 | mbedtls_free( output ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 191 | } |
| 192 | /* END_CASE */ |
| 193 | |
Jaeden Amero | 425382d | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 194 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
| 195 | void aes_crypt_xts_size( int size, int retval ) |
| 196 | { |
| 197 | mbedtls_aes_xts_context ctx; |
| 198 | const unsigned char *src = NULL; |
| 199 | unsigned char *output = NULL; |
| 200 | unsigned char data_unit[16]; |
| 201 | size_t length = size; |
| 202 | |
| 203 | mbedtls_aes_xts_init( &ctx ); |
| 204 | memset( data_unit, 0x00, sizeof( data_unit ) ); |
| 205 | |
| 206 | |
| 207 | /* Note that this function will most likely crash on failure, as NULL |
| 208 | * parameters will be used. In the passing case, the length check in |
| 209 | * mbedtls_aes_crypt_xts() will prevent any accesses to parameters by |
| 210 | * exiting the function early. */ |
| 211 | TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, length, data_unit, src, output ) == retval ); |
| 212 | } |
| 213 | /* END_CASE */ |
| 214 | |
Jaeden Amero | 142383e | 2018-05-31 10:40:34 +0100 | [diff] [blame] | 215 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
| 216 | void aes_crypt_xts_keysize( int size, int retval ) |
| 217 | { |
| 218 | mbedtls_aes_xts_context ctx; |
| 219 | const unsigned char *key = NULL; |
| 220 | size_t key_len = size; |
| 221 | |
| 222 | mbedtls_aes_xts_init( &ctx ); |
| 223 | |
| 224 | TEST_ASSERT( mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ) == retval ); |
| 225 | TEST_ASSERT( mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ) == retval ); |
| 226 | exit: |
| 227 | mbedtls_aes_xts_free( &ctx ); |
| 228 | } |
| 229 | /* END_CASE */ |
Jaeden Amero | 425382d | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 230 | |
| 231 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 232 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 233 | void aes_encrypt_cfb128( data_t * key_str, data_t * iv_str, |
| 234 | data_t * src_str, data_t * hex_dst_string ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 235 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 236 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 237 | mbedtls_aes_context ctx; |
Paul Bakker | cd43a0b | 2011-06-09 13:55:44 +0000 | [diff] [blame] | 238 | size_t iv_offset = 0; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 239 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 240 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 241 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 242 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 243 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 244 | mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 ); |
| 245 | TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_ENCRYPT, 16, &iv_offset, iv_str->x, src_str->x, output ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 246 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 247 | TEST_ASSERT( hexcmp( output, hex_dst_string->x, 16, hex_dst_string->len ) == 0 ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 248 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 249 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 250 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 251 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 252 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 253 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 254 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 255 | void aes_decrypt_cfb128( data_t * key_str, data_t * iv_str, |
| 256 | data_t * src_str, data_t * hex_dst_string ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 257 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 258 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | mbedtls_aes_context ctx; |
Paul Bakker | cd43a0b | 2011-06-09 13:55:44 +0000 | [diff] [blame] | 260 | size_t iv_offset = 0; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 261 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 262 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 263 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 264 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 265 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 266 | mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 ); |
| 267 | TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_DECRYPT, 16, &iv_offset, iv_str->x, src_str->x, output ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 268 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 269 | TEST_ASSERT( hexcmp( output, hex_dst_string->x, 16, hex_dst_string->len ) == 0 ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 270 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 271 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 272 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 273 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 274 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 275 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 276 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 277 | void aes_encrypt_cfb8( data_t * key_str, data_t * iv_str, |
| 278 | data_t * src_str, data_t * hex_dst_string ) |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 279 | { |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 280 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 281 | mbedtls_aes_context ctx; |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 282 | |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 283 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 284 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 285 | |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 286 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 287 | mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 ); |
| 288 | TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_ENCRYPT, src_str->len, iv_str->x, src_str->x, output ) == 0 ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 289 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 290 | TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 291 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 292 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 293 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 294 | } |
| 295 | /* END_CASE */ |
| 296 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 297 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 298 | void aes_decrypt_cfb8( data_t * key_str, data_t * iv_str, |
| 299 | data_t * src_str, data_t * hex_dst_string ) |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 300 | { |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 301 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 302 | mbedtls_aes_context ctx; |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 303 | |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 304 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 305 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 306 | |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 307 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 308 | mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 ); |
| 309 | TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_DECRYPT, src_str->len, iv_str->x, src_str->x, output ) == 0 ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 310 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 311 | TEST_ASSERT( hexcmp( output, hex_dst_string->x, src_str->len, hex_dst_string->len ) == 0 ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 312 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 313 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 314 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 315 | } |
| 316 | /* END_CASE */ |
| 317 | |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 318 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_OFB */ |
| 319 | void aes_encrypt_ofb( int fragment_size, char *hex_key_string, |
Simon Butcher | 0013144 | 2018-05-22 22:40:36 +0100 | [diff] [blame] | 320 | char *hex_iv_string, char *hex_src_string, |
| 321 | char *hex_dst_string ) |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 322 | { |
Simon Butcher | e416bf9 | 2018-06-02 18:28:32 +0100 | [diff] [blame] | 323 | unsigned char key_str[32]; |
| 324 | unsigned char iv_str[16]; |
| 325 | unsigned char src_str[64]; |
| 326 | unsigned char dst_str[64]; |
| 327 | unsigned char output[32]; |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 328 | mbedtls_aes_context ctx; |
| 329 | size_t iv_offset = 0; |
| 330 | int in_buffer_len; |
| 331 | unsigned char* src_str_next; |
Simon Butcher | dbe7fbf | 2018-04-29 14:51:35 +0100 | [diff] [blame] | 332 | int key_len; |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 333 | |
Simon Butcher | b7836e1 | 2018-06-02 18:36:49 +0100 | [diff] [blame] | 334 | memset( key_str, 0x00, sizeof( key_str ) ); |
| 335 | memset( iv_str, 0x00, sizeof( iv_str ) ); |
| 336 | memset( src_str, 0x00, sizeof( src_str ) ); |
| 337 | memset( dst_str, 0x00, sizeof( dst_str ) ); |
| 338 | memset( output, 0x00, sizeof( output ) ); |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 339 | mbedtls_aes_init( &ctx ); |
| 340 | |
Simon Butcher | e416bf9 | 2018-06-02 18:28:32 +0100 | [diff] [blame] | 341 | TEST_ASSERT( strlen( hex_key_string ) <= ( 32 * 2 ) ); |
| 342 | TEST_ASSERT( strlen( hex_iv_string ) <= ( 16 * 2 ) ); |
| 343 | TEST_ASSERT( strlen( hex_src_string ) <= ( 64 * 2 ) ); |
| 344 | TEST_ASSERT( strlen( hex_dst_string ) <= ( 64 * 2 ) ); |
| 345 | |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 346 | key_len = unhexify( key_str, hex_key_string ); |
Simon Butcher | dbe7fbf | 2018-04-29 14:51:35 +0100 | [diff] [blame] | 347 | unhexify( iv_str, hex_iv_string ); |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 348 | in_buffer_len = unhexify( src_str, hex_src_string ); |
| 349 | |
Simon Butcher | ad4e493 | 2018-04-29 00:43:47 +0100 | [diff] [blame] | 350 | TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ) == 0 ); |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 351 | src_str_next = src_str; |
| 352 | |
| 353 | while( in_buffer_len > 0 ) |
| 354 | { |
| 355 | TEST_ASSERT( mbedtls_aes_crypt_ofb( &ctx, fragment_size, &iv_offset, |
| 356 | iv_str, src_str_next, output ) == 0 ); |
| 357 | |
| 358 | hexify( dst_str, output, fragment_size ); |
| 359 | TEST_ASSERT( strncmp( (char *) dst_str, hex_dst_string, |
Simon Butcher | 0013144 | 2018-05-22 22:40:36 +0100 | [diff] [blame] | 360 | ( 2 * fragment_size ) ) == 0 ); |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 361 | |
| 362 | in_buffer_len -= fragment_size; |
| 363 | hex_dst_string += ( fragment_size * 2 ); |
| 364 | src_str_next += fragment_size; |
| 365 | |
| 366 | if( in_buffer_len < fragment_size ) |
| 367 | fragment_size = in_buffer_len; |
| 368 | } |
| 369 | |
| 370 | exit: |
| 371 | mbedtls_aes_free( &ctx ); |
| 372 | } |
| 373 | /* END_CASE */ |
| 374 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 375 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 376 | void aes_selftest( ) |
Paul Bakker | 3d36082 | 2009-07-05 11:29:38 +0000 | [diff] [blame] | 377 | { |
Andres AG | 93012e8 | 2016-09-09 09:10:28 +0100 | [diff] [blame] | 378 | TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 ); |
Paul Bakker | 3d36082 | 2009-07-05 11:29:38 +0000 | [diff] [blame] | 379 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 380 | /* END_CASE */ |