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); |
| 18 | |
Simon Butcher | 249b3d6 | 2018-12-09 22:18:46 +0000 | [diff] [blame] | 19 | mbedtls_aes_init( &ctx ); |
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); |
| 42 | |
Simon Butcher | 249b3d6 | 2018-12-09 22:18:46 +0000 | [diff] [blame] | 43 | mbedtls_aes_init( &ctx ); |
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); |
| 67 | |
Simon Butcher | 249b3d6 | 2018-12-09 22:18:46 +0000 | [diff] [blame] | 68 | mbedtls_aes_init( &ctx ); |
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 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 94 | mbedtls_aes_setkey_dec( &ctx, key_str->x, key_str->len * 8 ); |
| 95 | 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] | 96 | if( cbc_result == 0) |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 97 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 98 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 99 | 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] | 100 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 101 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 102 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 103 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 104 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 105 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 106 | |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 107 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
Jaeden Amero | cd9fc5e | 2018-05-30 15:23:24 +0100 | [diff] [blame] | 108 | 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] | 109 | char *hex_src_string, char *hex_dst_string ) |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 110 | { |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 111 | enum { AES_BLOCK_SIZE = 16 }; |
| 112 | unsigned char *data_unit = NULL; |
| 113 | unsigned char *key = NULL; |
| 114 | unsigned char *src = NULL; |
| 115 | unsigned char *dst = NULL; |
| 116 | unsigned char *output = NULL; |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 117 | mbedtls_aes_xts_context ctx; |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 118 | size_t key_len, src_len, dst_len, data_unit_len; |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 119 | |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 120 | mbedtls_aes_xts_init( &ctx ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 121 | |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 122 | data_unit = unhexify_alloc( hex_data_unit_string, &data_unit_len ); |
| 123 | TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 124 | |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 125 | key = unhexify_alloc( hex_key_string, &key_len ); |
| 126 | TEST_ASSERT( key_len % 2 == 0 ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 127 | |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 128 | src = unhexify_alloc( hex_src_string, &src_len ); |
| 129 | dst = unhexify_alloc( hex_dst_string, &dst_len ); |
| 130 | TEST_ASSERT( src_len == dst_len ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 131 | |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 132 | output = zero_alloc( dst_len ); |
| 133 | |
| 134 | TEST_ASSERT( mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ) == 0 ); |
| 135 | TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, src_len, |
| 136 | data_unit, src, output ) == 0 ); |
| 137 | |
| 138 | TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 139 | |
| 140 | exit: |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 141 | mbedtls_aes_xts_free( &ctx ); |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 142 | mbedtls_free( data_unit ); |
| 143 | mbedtls_free( key ); |
| 144 | mbedtls_free( src ); |
| 145 | mbedtls_free( dst ); |
| 146 | mbedtls_free( output ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 147 | } |
| 148 | /* END_CASE */ |
| 149 | |
| 150 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
Jaeden Amero | cd9fc5e | 2018-05-30 15:23:24 +0100 | [diff] [blame] | 151 | 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] | 152 | char *hex_dst_string, char *hex_src_string ) |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 153 | { |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 154 | enum { AES_BLOCK_SIZE = 16 }; |
| 155 | unsigned char *data_unit = NULL; |
| 156 | unsigned char *key = NULL; |
| 157 | unsigned char *src = NULL; |
| 158 | unsigned char *dst = NULL; |
| 159 | unsigned char *output = NULL; |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 160 | mbedtls_aes_xts_context ctx; |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 161 | size_t key_len, src_len, dst_len, data_unit_len; |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 162 | |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 163 | mbedtls_aes_xts_init( &ctx ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 164 | |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 165 | data_unit = unhexify_alloc( hex_data_unit_string, &data_unit_len ); |
| 166 | TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 167 | |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 168 | key = unhexify_alloc( hex_key_string, &key_len ); |
| 169 | TEST_ASSERT( key_len % 2 == 0 ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 170 | |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 171 | src = unhexify_alloc( hex_src_string, &src_len ); |
| 172 | dst = unhexify_alloc( hex_dst_string, &dst_len ); |
| 173 | TEST_ASSERT( src_len == dst_len ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 174 | |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 175 | output = zero_alloc( dst_len ); |
| 176 | |
| 177 | TEST_ASSERT( mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ) == 0 ); |
| 178 | TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_DECRYPT, src_len, |
| 179 | data_unit, src, output ) == 0 ); |
| 180 | |
| 181 | TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 182 | |
| 183 | exit: |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 184 | mbedtls_aes_xts_free( &ctx ); |
Jaeden Amero | e5c4b07 | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 185 | mbedtls_free( data_unit ); |
| 186 | mbedtls_free( key ); |
| 187 | mbedtls_free( src ); |
| 188 | mbedtls_free( dst ); |
| 189 | mbedtls_free( output ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 190 | } |
| 191 | /* END_CASE */ |
| 192 | |
Jaeden Amero | 425382d | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 193 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
| 194 | void aes_crypt_xts_size( int size, int retval ) |
| 195 | { |
| 196 | mbedtls_aes_xts_context ctx; |
Manuel Pégourié-Gonnard | 191af13 | 2018-12-13 10:15:30 +0100 | [diff] [blame] | 197 | const unsigned char src[16] = { 0 }; |
| 198 | unsigned char output[16]; |
Jaeden Amero | 425382d | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 199 | unsigned char data_unit[16]; |
| 200 | size_t length = size; |
| 201 | |
| 202 | mbedtls_aes_xts_init( &ctx ); |
| 203 | memset( data_unit, 0x00, sizeof( data_unit ) ); |
| 204 | |
| 205 | |
Manuel Pégourié-Gonnard | 191af13 | 2018-12-13 10:15:30 +0100 | [diff] [blame] | 206 | /* Valid pointers are passed for builds with MBEDTLS_CHECK_PARAMS, as |
| 207 | * otherwise we wouldn't get to the size check we're interested in. */ |
Jaeden Amero | 425382d | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 208 | TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, length, data_unit, src, output ) == retval ); |
| 209 | } |
| 210 | /* END_CASE */ |
| 211 | |
Jaeden Amero | 142383e | 2018-05-31 10:40:34 +0100 | [diff] [blame] | 212 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
| 213 | void aes_crypt_xts_keysize( int size, int retval ) |
| 214 | { |
| 215 | mbedtls_aes_xts_context ctx; |
Manuel Pégourié-Gonnard | 68e3dff | 2018-12-12 12:48:04 +0100 | [diff] [blame] | 216 | const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; |
Jaeden Amero | 142383e | 2018-05-31 10:40:34 +0100 | [diff] [blame] | 217 | size_t key_len = size; |
| 218 | |
| 219 | mbedtls_aes_xts_init( &ctx ); |
| 220 | |
| 221 | TEST_ASSERT( mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ) == retval ); |
| 222 | TEST_ASSERT( mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ) == retval ); |
| 223 | exit: |
| 224 | mbedtls_aes_xts_free( &ctx ); |
| 225 | } |
| 226 | /* END_CASE */ |
Jaeden Amero | 425382d | 2018-04-28 17:26:25 +0100 | [diff] [blame] | 227 | |
| 228 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 229 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 230 | void aes_encrypt_cfb128( data_t * key_str, data_t * iv_str, |
| 231 | data_t * src_str, data_t * hex_dst_string ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 232 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 233 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 234 | mbedtls_aes_context ctx; |
Paul Bakker | cd43a0b | 2011-06-09 13:55:44 +0000 | [diff] [blame] | 235 | size_t iv_offset = 0; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 236 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 237 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 238 | mbedtls_aes_init( &ctx ); |
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 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 241 | mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 ); |
| 242 | 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] | 243 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 244 | 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] | 245 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 246 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 247 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 248 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 249 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 250 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 251 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 252 | void aes_decrypt_cfb128( data_t * key_str, data_t * iv_str, |
| 253 | data_t * src_str, data_t * hex_dst_string ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 254 | { |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 255 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 256 | mbedtls_aes_context ctx; |
Paul Bakker | cd43a0b | 2011-06-09 13:55:44 +0000 | [diff] [blame] | 257 | size_t iv_offset = 0; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 258 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 259 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 260 | mbedtls_aes_init( &ctx ); |
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 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 263 | mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 ); |
| 264 | 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] | 265 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 266 | 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] | 267 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 268 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 269 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 270 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 271 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 272 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 273 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 274 | void aes_encrypt_cfb8( data_t * key_str, data_t * iv_str, |
| 275 | data_t * src_str, data_t * hex_dst_string ) |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 276 | { |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 277 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 278 | mbedtls_aes_context ctx; |
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 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 281 | mbedtls_aes_init( &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 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 284 | mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 ); |
| 285 | 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] | 286 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 287 | 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] | 288 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 289 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 290 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 291 | } |
| 292 | /* END_CASE */ |
| 293 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 294 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Azim Khan | 5fcca46 | 2018-06-29 11:05:32 +0100 | [diff] [blame] | 295 | void aes_decrypt_cfb8( data_t * key_str, data_t * iv_str, |
| 296 | data_t * src_str, data_t * hex_dst_string ) |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 297 | { |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 298 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 299 | mbedtls_aes_context ctx; |
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 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 302 | mbedtls_aes_init( &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 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 305 | mbedtls_aes_setkey_enc( &ctx, key_str->x, key_str->len * 8 ); |
| 306 | 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] | 307 | |
Azim Khan | d30ca13 | 2017-06-09 04:32:58 +0100 | [diff] [blame] | 308 | 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] | 309 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 310 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 311 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 312 | } |
| 313 | /* END_CASE */ |
| 314 | |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 315 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_OFB */ |
| 316 | void aes_encrypt_ofb( int fragment_size, char *hex_key_string, |
Simon Butcher | 0013144 | 2018-05-22 22:40:36 +0100 | [diff] [blame] | 317 | char *hex_iv_string, char *hex_src_string, |
| 318 | char *hex_dst_string ) |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 319 | { |
Simon Butcher | e416bf9 | 2018-06-02 18:28:32 +0100 | [diff] [blame] | 320 | unsigned char key_str[32]; |
| 321 | unsigned char iv_str[16]; |
| 322 | unsigned char src_str[64]; |
| 323 | unsigned char dst_str[64]; |
| 324 | unsigned char output[32]; |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 325 | mbedtls_aes_context ctx; |
| 326 | size_t iv_offset = 0; |
| 327 | int in_buffer_len; |
| 328 | unsigned char* src_str_next; |
Simon Butcher | dbe7fbf | 2018-04-29 14:51:35 +0100 | [diff] [blame] | 329 | int key_len; |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 330 | |
Simon Butcher | b7836e1 | 2018-06-02 18:36:49 +0100 | [diff] [blame] | 331 | memset( key_str, 0x00, sizeof( key_str ) ); |
| 332 | memset( iv_str, 0x00, sizeof( iv_str ) ); |
| 333 | memset( src_str, 0x00, sizeof( src_str ) ); |
| 334 | memset( dst_str, 0x00, sizeof( dst_str ) ); |
| 335 | memset( output, 0x00, sizeof( output ) ); |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 336 | mbedtls_aes_init( &ctx ); |
| 337 | |
Simon Butcher | e416bf9 | 2018-06-02 18:28:32 +0100 | [diff] [blame] | 338 | TEST_ASSERT( strlen( hex_key_string ) <= ( 32 * 2 ) ); |
| 339 | TEST_ASSERT( strlen( hex_iv_string ) <= ( 16 * 2 ) ); |
| 340 | TEST_ASSERT( strlen( hex_src_string ) <= ( 64 * 2 ) ); |
| 341 | TEST_ASSERT( strlen( hex_dst_string ) <= ( 64 * 2 ) ); |
| 342 | |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 343 | key_len = unhexify( key_str, hex_key_string ); |
Simon Butcher | dbe7fbf | 2018-04-29 14:51:35 +0100 | [diff] [blame] | 344 | unhexify( iv_str, hex_iv_string ); |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 345 | in_buffer_len = unhexify( src_str, hex_src_string ); |
| 346 | |
Simon Butcher | ad4e493 | 2018-04-29 00:43:47 +0100 | [diff] [blame] | 347 | 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] | 348 | src_str_next = src_str; |
| 349 | |
| 350 | while( in_buffer_len > 0 ) |
| 351 | { |
| 352 | TEST_ASSERT( mbedtls_aes_crypt_ofb( &ctx, fragment_size, &iv_offset, |
| 353 | iv_str, src_str_next, output ) == 0 ); |
| 354 | |
| 355 | hexify( dst_str, output, fragment_size ); |
| 356 | TEST_ASSERT( strncmp( (char *) dst_str, hex_dst_string, |
Simon Butcher | 0013144 | 2018-05-22 22:40:36 +0100 | [diff] [blame] | 357 | ( 2 * fragment_size ) ) == 0 ); |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 358 | |
| 359 | in_buffer_len -= fragment_size; |
| 360 | hex_dst_string += ( fragment_size * 2 ); |
| 361 | src_str_next += fragment_size; |
| 362 | |
| 363 | if( in_buffer_len < fragment_size ) |
| 364 | fragment_size = in_buffer_len; |
| 365 | } |
| 366 | |
| 367 | exit: |
| 368 | mbedtls_aes_free( &ctx ); |
| 369 | } |
| 370 | /* END_CASE */ |
| 371 | |
Manuel Pégourié-Gonnard | a2b0e27 | 2018-12-10 15:23:58 +0100 | [diff] [blame] | 372 | /* BEGIN_CASE depends_on:MBEDTLS_CHECK_PARAMS:!MBEDTLS_PARAM_FAILED_ALT */ |
Manuel Pégourié-Gonnard | 3178d1a | 2018-12-12 13:05:00 +0100 | [diff] [blame] | 373 | void aes_check_params( ) |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 374 | { |
Manuel Pégourié-Gonnard | 68e3dff | 2018-12-12 12:48:04 +0100 | [diff] [blame] | 375 | mbedtls_aes_context aes_ctx; |
| 376 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
| 377 | mbedtls_aes_xts_context xts_ctx; |
| 378 | #endif |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 379 | const unsigned char key[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 }; |
Manuel Pégourié-Gonnard | 1aca260 | 2018-12-12 12:56:55 +0100 | [diff] [blame] | 380 | const unsigned char in[16] = { 0 }; |
| 381 | unsigned char out[16]; |
Manuel Pégourié-Gonnard | 1677cca | 2018-12-13 10:27:13 +0100 | [diff] [blame^] | 382 | size_t size; |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 383 | |
| 384 | TEST_INVALID_PARAM( mbedtls_aes_init( NULL ) ); |
Manuel Pégourié-Gonnard | 68e3dff | 2018-12-12 12:48:04 +0100 | [diff] [blame] | 385 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
Manuel Pégourié-Gonnard | 44c5d58 | 2018-12-10 16:56:14 +0100 | [diff] [blame] | 386 | TEST_INVALID_PARAM( mbedtls_aes_xts_init( NULL ) ); |
Manuel Pégourié-Gonnard | 68e3dff | 2018-12-12 12:48:04 +0100 | [diff] [blame] | 387 | #endif |
Manuel Pégourié-Gonnard | 44c5d58 | 2018-12-10 16:56:14 +0100 | [diff] [blame] | 388 | |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 389 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 390 | mbedtls_aes_setkey_enc( NULL, key, 128 ) ); |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 391 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
Manuel Pégourié-Gonnard | 68e3dff | 2018-12-12 12:48:04 +0100 | [diff] [blame] | 392 | mbedtls_aes_setkey_enc( &aes_ctx, NULL, 128 ) ); |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 393 | |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 394 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 395 | mbedtls_aes_setkey_dec( NULL, key, 128 ) ); |
Manuel Pégourié-Gonnard | 68e3dff | 2018-12-12 12:48:04 +0100 | [diff] [blame] | 396 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 397 | mbedtls_aes_setkey_dec( &aes_ctx, NULL, 128 ) ); |
| 398 | |
| 399 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
| 400 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 401 | mbedtls_aes_xts_setkey_enc( NULL, key, 128 ) ); |
| 402 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 403 | mbedtls_aes_xts_setkey_enc( &xts_ctx, NULL, 128 ) ); |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 404 | |
| 405 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
Manuel Pégourié-Gonnard | 68e3dff | 2018-12-12 12:48:04 +0100 | [diff] [blame] | 406 | mbedtls_aes_xts_setkey_dec( NULL, key, 128 ) ); |
| 407 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 408 | mbedtls_aes_xts_setkey_dec( &xts_ctx, NULL, 128 ) ); |
| 409 | #endif |
Manuel Pégourié-Gonnard | 1aca260 | 2018-12-12 12:56:55 +0100 | [diff] [blame] | 410 | |
| 411 | |
| 412 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 413 | mbedtls_aes_crypt_ecb( NULL, |
| 414 | MBEDTLS_AES_ENCRYPT, in, out ) ); |
| 415 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 416 | mbedtls_aes_crypt_ecb( &aes_ctx, |
| 417 | 42, in, out ) ); |
| 418 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 419 | mbedtls_aes_crypt_ecb( &aes_ctx, |
| 420 | MBEDTLS_AES_ENCRYPT, NULL, out ) ); |
| 421 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 422 | mbedtls_aes_crypt_ecb( &aes_ctx, |
| 423 | MBEDTLS_AES_ENCRYPT, in, NULL ) ); |
Manuel Pégourié-Gonnard | 3178d1a | 2018-12-12 13:05:00 +0100 | [diff] [blame] | 424 | |
| 425 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 426 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 427 | mbedtls_aes_crypt_cbc( NULL, |
| 428 | MBEDTLS_AES_ENCRYPT, 16, |
| 429 | out, in, out ) ); |
| 430 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 431 | mbedtls_aes_crypt_cbc( &aes_ctx, |
| 432 | 42, 16, |
| 433 | out, in, out ) ); |
| 434 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 435 | mbedtls_aes_crypt_cbc( &aes_ctx, |
| 436 | MBEDTLS_AES_ENCRYPT, 16, |
| 437 | NULL, in, out ) ); |
| 438 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 439 | mbedtls_aes_crypt_cbc( &aes_ctx, |
| 440 | MBEDTLS_AES_ENCRYPT, 16, |
| 441 | out, NULL, out ) ); |
| 442 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 443 | mbedtls_aes_crypt_cbc( &aes_ctx, |
| 444 | MBEDTLS_AES_ENCRYPT, 16, |
| 445 | out, in, NULL ) ); |
| 446 | #endif /* MBEDTLS_CIPHER_MODE_CBC */ |
Manuel Pégourié-Gonnard | 191af13 | 2018-12-13 10:15:30 +0100 | [diff] [blame] | 447 | |
| 448 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
| 449 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 450 | mbedtls_aes_crypt_xts( NULL, |
| 451 | MBEDTLS_AES_ENCRYPT, 16, |
| 452 | in, in, out ) ); |
| 453 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 454 | mbedtls_aes_crypt_xts( &xts_ctx, |
| 455 | 42, 16, |
| 456 | in, in, out ) ); |
| 457 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 458 | mbedtls_aes_crypt_xts( &xts_ctx, |
| 459 | MBEDTLS_AES_ENCRYPT, 16, |
| 460 | NULL, in, out ) ); |
| 461 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 462 | mbedtls_aes_crypt_xts( &xts_ctx, |
| 463 | MBEDTLS_AES_ENCRYPT, 16, |
| 464 | in, NULL, out ) ); |
| 465 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 466 | mbedtls_aes_crypt_xts( &xts_ctx, |
| 467 | MBEDTLS_AES_ENCRYPT, 16, |
| 468 | in, in, NULL ) ); |
| 469 | #endif /* MBEDTLS_CIPHER_MODE_XTS */ |
Manuel Pégourié-Gonnard | 1677cca | 2018-12-13 10:27:13 +0100 | [diff] [blame^] | 470 | |
| 471 | #if defined(MBEDTLS_CIPHER_MODE_CFB) |
| 472 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 473 | mbedtls_aes_crypt_cfb128( NULL, |
| 474 | MBEDTLS_AES_ENCRYPT, 16, |
| 475 | &size, out, in, out ) ); |
| 476 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 477 | mbedtls_aes_crypt_cfb128( &aes_ctx, |
| 478 | 42, 16, |
| 479 | &size, out, in, out ) ); |
| 480 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 481 | mbedtls_aes_crypt_cfb128( &aes_ctx, |
| 482 | MBEDTLS_AES_ENCRYPT, 16, |
| 483 | NULL, out, in, out ) ); |
| 484 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 485 | mbedtls_aes_crypt_cfb128( &aes_ctx, |
| 486 | MBEDTLS_AES_ENCRYPT, 16, |
| 487 | &size, NULL, in, out ) ); |
| 488 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 489 | mbedtls_aes_crypt_cfb128( &aes_ctx, |
| 490 | MBEDTLS_AES_ENCRYPT, 16, |
| 491 | &size, out, NULL, out ) ); |
| 492 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 493 | mbedtls_aes_crypt_cfb128( &aes_ctx, |
| 494 | MBEDTLS_AES_ENCRYPT, 16, |
| 495 | &size, out, in, NULL ) ); |
| 496 | |
| 497 | |
| 498 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 499 | mbedtls_aes_crypt_cfb8( NULL, |
| 500 | MBEDTLS_AES_ENCRYPT, 16, |
| 501 | out, in, out ) ); |
| 502 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 503 | mbedtls_aes_crypt_cfb8( &aes_ctx, |
| 504 | 42, 16, |
| 505 | out, in, out ) ); |
| 506 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 507 | mbedtls_aes_crypt_cfb8( &aes_ctx, |
| 508 | MBEDTLS_AES_ENCRYPT, 16, |
| 509 | NULL, in, out ) ); |
| 510 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 511 | mbedtls_aes_crypt_cfb8( &aes_ctx, |
| 512 | MBEDTLS_AES_ENCRYPT, 16, |
| 513 | out, NULL, out ) ); |
| 514 | TEST_INVALID_PARAM_RET( MBEDTLS_ERR_AES_BAD_INPUT_DATA, |
| 515 | mbedtls_aes_crypt_cfb8( &aes_ctx, |
| 516 | MBEDTLS_AES_ENCRYPT, 16, |
| 517 | out, in, NULL ) ); |
| 518 | #endif /* MBEDTLS_CIPHER_MODE_CFB */ |
Manuel Pégourié-Gonnard | a4251f4 | 2018-12-12 12:04:51 +0100 | [diff] [blame] | 519 | } |
| 520 | /* END_CASE */ |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 521 | |
Manuel Pégourié-Gonnard | a4251f4 | 2018-12-12 12:04:51 +0100 | [diff] [blame] | 522 | /* BEGIN_CASE */ |
Manuel Pégourié-Gonnard | 3178d1a | 2018-12-12 13:05:00 +0100 | [diff] [blame] | 523 | void aes_misc_params( ) |
Manuel Pégourié-Gonnard | a4251f4 | 2018-12-12 12:04:51 +0100 | [diff] [blame] | 524 | { |
Manuel Pégourié-Gonnard | 3178d1a | 2018-12-12 13:05:00 +0100 | [diff] [blame] | 525 | mbedtls_aes_context aes_ctx; |
Manuel Pégourié-Gonnard | 191af13 | 2018-12-13 10:15:30 +0100 | [diff] [blame] | 526 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
| 527 | mbedtls_aes_xts_context xts_ctx; |
| 528 | #endif |
Manuel Pégourié-Gonnard | 3178d1a | 2018-12-12 13:05:00 +0100 | [diff] [blame] | 529 | const unsigned char in[16] = { 0 }; |
| 530 | unsigned char out[16]; |
| 531 | |
Manuel Pégourié-Gonnard | 44c5d58 | 2018-12-10 16:56:14 +0100 | [diff] [blame] | 532 | /* These calls accept NULL */ |
| 533 | TEST_VALID_PARAM( mbedtls_aes_free( NULL ) ); |
Manuel Pégourié-Gonnard | af0c6cb | 2018-12-18 12:02:52 +0100 | [diff] [blame] | 534 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
Manuel Pégourié-Gonnard | 44c5d58 | 2018-12-10 16:56:14 +0100 | [diff] [blame] | 535 | TEST_VALID_PARAM( mbedtls_aes_xts_free( NULL ) ); |
Manuel Pégourié-Gonnard | af0c6cb | 2018-12-18 12:02:52 +0100 | [diff] [blame] | 536 | #endif |
Manuel Pégourié-Gonnard | 3178d1a | 2018-12-12 13:05:00 +0100 | [diff] [blame] | 537 | |
| 538 | #if defined(MBEDTLS_CIPHER_MODE_CBC) |
| 539 | TEST_ASSERT( mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT, |
Manuel Pégourié-Gonnard | 191af13 | 2018-12-13 10:15:30 +0100 | [diff] [blame] | 540 | 15, |
| 541 | out, in, out ) |
Manuel Pégourié-Gonnard | 3178d1a | 2018-12-12 13:05:00 +0100 | [diff] [blame] | 542 | == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH ); |
| 543 | TEST_ASSERT( mbedtls_aes_crypt_cbc( &aes_ctx, MBEDTLS_AES_ENCRYPT, |
Manuel Pégourié-Gonnard | 191af13 | 2018-12-13 10:15:30 +0100 | [diff] [blame] | 544 | 17, |
| 545 | out, in, out ) |
Manuel Pégourié-Gonnard | 3178d1a | 2018-12-12 13:05:00 +0100 | [diff] [blame] | 546 | == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH ); |
| 547 | #endif |
| 548 | |
Manuel Pégourié-Gonnard | 191af13 | 2018-12-13 10:15:30 +0100 | [diff] [blame] | 549 | #if defined(MBEDTLS_CIPHER_MODE_XTS) |
| 550 | TEST_ASSERT( mbedtls_aes_crypt_xts( &xts_ctx, MBEDTLS_AES_ENCRYPT, |
| 551 | 15, |
| 552 | in, in, out ) |
| 553 | == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH ); |
| 554 | TEST_ASSERT( mbedtls_aes_crypt_xts( &xts_ctx, MBEDTLS_AES_ENCRYPT, |
| 555 | (1 << 24) + 1, |
| 556 | in, in, out ) |
| 557 | == MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH ); |
| 558 | #endif |
Simon Butcher | a646345 | 2018-12-06 17:41:56 +0000 | [diff] [blame] | 559 | } |
| 560 | /* END_CASE */ |
| 561 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 562 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Azim Khan | f1aaec9 | 2017-05-30 14:23:15 +0100 | [diff] [blame] | 563 | void aes_selftest( ) |
Paul Bakker | 3d36082 | 2009-07-05 11:29:38 +0000 | [diff] [blame] | 564 | { |
Andres AG | 93012e8 | 2016-09-09 09:10:28 +0100 | [diff] [blame] | 565 | TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 ); |
Paul Bakker | 3d36082 | 2009-07-05 11:29:38 +0000 | [diff] [blame] | 566 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 567 | /* END_CASE */ |