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 */ |
| 11 | void aes_encrypt_ecb( char *hex_key_string, char *hex_src_string, |
| 12 | char *hex_dst_string, int setkey_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 13 | { |
| 14 | unsigned char key_str[100]; |
| 15 | unsigned char src_str[100]; |
| 16 | unsigned char dst_str[100]; |
| 17 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 18 | mbedtls_aes_context ctx; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 19 | int key_len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 20 | |
| 21 | memset(key_str, 0x00, 100); |
| 22 | memset(src_str, 0x00, 100); |
| 23 | memset(dst_str, 0x00, 100); |
| 24 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 26 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 27 | key_len = unhexify( key_str, hex_key_string ); |
| 28 | unhexify( src_str, hex_src_string ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 29 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 30 | TEST_ASSERT( mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ) == setkey_result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 31 | if( setkey_result == 0 ) |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 32 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 33 | TEST_ASSERT( mbedtls_aes_crypt_ecb( &ctx, MBEDTLS_AES_ENCRYPT, src_str, output ) == 0 ); |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 34 | hexify( dst_str, output, 16 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 35 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 36 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 37 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 38 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 39 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 40 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 41 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 42 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 43 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 44 | /* BEGIN_CASE */ |
| 45 | void aes_decrypt_ecb( char *hex_key_string, char *hex_src_string, |
| 46 | char *hex_dst_string, int setkey_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 47 | { |
| 48 | unsigned char key_str[100]; |
| 49 | unsigned char src_str[100]; |
| 50 | unsigned char dst_str[100]; |
| 51 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | mbedtls_aes_context ctx; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 53 | int key_len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 54 | |
| 55 | memset(key_str, 0x00, 100); |
| 56 | memset(src_str, 0x00, 100); |
| 57 | memset(dst_str, 0x00, 100); |
| 58 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 60 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 61 | key_len = unhexify( key_str, hex_key_string ); |
| 62 | unhexify( src_str, hex_src_string ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 63 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 64 | TEST_ASSERT( mbedtls_aes_setkey_dec( &ctx, key_str, key_len * 8 ) == setkey_result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 65 | if( setkey_result == 0 ) |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 66 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 67 | TEST_ASSERT( mbedtls_aes_crypt_ecb( &ctx, MBEDTLS_AES_DECRYPT, src_str, output ) == 0 ); |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 68 | hexify( dst_str, output, 16 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 69 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 70 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 71 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 72 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 73 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 74 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 75 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 76 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 77 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 78 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 79 | void aes_encrypt_cbc( char *hex_key_string, char *hex_iv_string, |
| 80 | char *hex_src_string, char *hex_dst_string, |
| 81 | int cbc_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 82 | { |
| 83 | unsigned char key_str[100]; |
| 84 | unsigned char iv_str[100]; |
| 85 | unsigned char src_str[100]; |
| 86 | unsigned char dst_str[100]; |
| 87 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 88 | mbedtls_aes_context ctx; |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 89 | int key_len, data_len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 90 | |
| 91 | memset(key_str, 0x00, 100); |
| 92 | memset(iv_str, 0x00, 100); |
| 93 | memset(src_str, 0x00, 100); |
| 94 | memset(dst_str, 0x00, 100); |
| 95 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 96 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 97 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 98 | key_len = unhexify( key_str, hex_key_string ); |
| 99 | unhexify( iv_str, hex_iv_string ); |
| 100 | data_len = unhexify( src_str, hex_src_string ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 101 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 102 | mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ); |
| 103 | TEST_ASSERT( mbedtls_aes_crypt_cbc( &ctx, MBEDTLS_AES_ENCRYPT, data_len, iv_str, src_str, output ) == cbc_result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 104 | if( cbc_result == 0 ) |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 105 | { |
| 106 | hexify( dst_str, output, data_len ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 107 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 108 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 109 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 110 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 111 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 112 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 113 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 114 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 115 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 116 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 117 | void aes_decrypt_cbc( char *hex_key_string, char *hex_iv_string, |
| 118 | char *hex_src_string, char *hex_dst_string, |
| 119 | int cbc_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 120 | { |
| 121 | unsigned char key_str[100]; |
| 122 | unsigned char iv_str[100]; |
| 123 | unsigned char src_str[100]; |
| 124 | unsigned char dst_str[100]; |
| 125 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 126 | mbedtls_aes_context ctx; |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 127 | int key_len, data_len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 128 | |
| 129 | memset(key_str, 0x00, 100); |
| 130 | memset(iv_str, 0x00, 100); |
| 131 | memset(src_str, 0x00, 100); |
| 132 | memset(dst_str, 0x00, 100); |
| 133 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 134 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 135 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 136 | key_len = unhexify( key_str, hex_key_string ); |
| 137 | unhexify( iv_str, hex_iv_string ); |
| 138 | data_len = unhexify( src_str, hex_src_string ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 139 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | mbedtls_aes_setkey_dec( &ctx, key_str, key_len * 8 ); |
| 141 | TEST_ASSERT( mbedtls_aes_crypt_cbc( &ctx, MBEDTLS_AES_DECRYPT, data_len, iv_str, src_str, output ) == cbc_result ); |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 142 | if( cbc_result == 0) |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 143 | { |
| 144 | hexify( dst_str, output, data_len ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 145 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 146 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 147 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 148 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 149 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 150 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 151 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 152 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 153 | |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 154 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
Jaeden Amero | cd9fc5e | 2018-05-30 15:23:24 +0100 | [diff] [blame] | 155 | void aes_encrypt_xts( char *hex_key_string, char *hex_data_unit_string, |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 156 | char *hex_src_string, char *hex_dst_string, |
| 157 | int data_unit_len, int xts_result ) |
| 158 | { |
| 159 | unsigned char key_str[100] = { 0, }; |
Jaeden Amero | cd9fc5e | 2018-05-30 15:23:24 +0100 | [diff] [blame] | 160 | unsigned char data_unit_str[100] = { 0, }; |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 161 | unsigned char src_str[100] = { 0, }; |
| 162 | unsigned char dst_str[100] = { 0, }; |
| 163 | unsigned char output[100] = { 0, }; |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 164 | mbedtls_aes_xts_context ctx; |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 165 | int key_len, data_len; |
| 166 | |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 167 | mbedtls_aes_xts_init( &ctx ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 168 | |
| 169 | key_len = unhexify( key_str, hex_key_string ); |
Jaeden Amero | cd9fc5e | 2018-05-30 15:23:24 +0100 | [diff] [blame] | 170 | unhexify( data_unit_str, hex_data_unit_string ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 171 | data_len = unhexify( src_str, hex_src_string ); |
Jaeden Amero | 5162b93 | 2018-05-29 12:55:24 +0100 | [diff] [blame] | 172 | TEST_ASSERT( data_len == data_unit_len / 8 ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 173 | |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 174 | mbedtls_aes_xts_setkey_enc( &ctx, key_str, key_len * 8 ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 175 | |
Jaeden Amero | cd9fc5e | 2018-05-30 15:23:24 +0100 | [diff] [blame] | 176 | TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, data_len, data_unit_str, src_str, output ) == xts_result ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 177 | if( xts_result == 0 ) |
| 178 | { |
| 179 | hexify( dst_str, output, data_len ); |
| 180 | |
| 181 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
| 182 | } |
| 183 | |
| 184 | exit: |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 185 | mbedtls_aes_xts_free( &ctx ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 186 | } |
| 187 | /* END_CASE */ |
| 188 | |
| 189 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
Jaeden Amero | cd9fc5e | 2018-05-30 15:23:24 +0100 | [diff] [blame] | 190 | void aes_decrypt_xts( char *hex_key_string, char *hex_data_unit_string, |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 191 | char *hex_src_string, char *hex_dst_string, |
| 192 | int data_unit_len, int xts_result ) |
| 193 | { |
| 194 | unsigned char key_str[100] = { 0, }; |
Jaeden Amero | cd9fc5e | 2018-05-30 15:23:24 +0100 | [diff] [blame] | 195 | unsigned char data_unit_str[100] = { 0, }; |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 196 | unsigned char src_str[100] = { 0, }; |
| 197 | unsigned char dst_str[100] = { 0, }; |
| 198 | unsigned char output[100] = { 0, }; |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 199 | mbedtls_aes_xts_context ctx; |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 200 | int key_len, data_len; |
| 201 | |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 202 | mbedtls_aes_xts_init( &ctx ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 203 | |
| 204 | key_len = unhexify( key_str, hex_key_string ); |
Jaeden Amero | cd9fc5e | 2018-05-30 15:23:24 +0100 | [diff] [blame] | 205 | unhexify( data_unit_str, hex_data_unit_string ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 206 | data_len = unhexify( src_str, hex_src_string ); |
Jaeden Amero | 5162b93 | 2018-05-29 12:55:24 +0100 | [diff] [blame] | 207 | TEST_ASSERT( data_len == data_unit_len / 8 ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 208 | |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 209 | mbedtls_aes_xts_setkey_dec( &ctx, key_str, key_len * 8 ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 210 | |
Jaeden Amero | cd9fc5e | 2018-05-30 15:23:24 +0100 | [diff] [blame] | 211 | TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_DECRYPT, data_len, data_unit_str, src_str, output ) == xts_result ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 212 | if( xts_result == 0 ) |
| 213 | { |
| 214 | hexify( dst_str, output, data_len ); |
| 215 | |
| 216 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
| 217 | } |
| 218 | |
| 219 | exit: |
Jaeden Amero | 9366feb | 2018-05-29 18:55:17 +0100 | [diff] [blame] | 220 | mbedtls_aes_xts_free( &ctx ); |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame] | 221 | } |
| 222 | /* END_CASE */ |
| 223 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 224 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 225 | void aes_encrypt_cfb128( char *hex_key_string, char *hex_iv_string, |
| 226 | char *hex_src_string, char *hex_dst_string ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 227 | { |
| 228 | unsigned char key_str[100]; |
| 229 | unsigned char iv_str[100]; |
| 230 | unsigned char src_str[100]; |
| 231 | unsigned char dst_str[100]; |
| 232 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 233 | mbedtls_aes_context ctx; |
Paul Bakker | cd43a0b | 2011-06-09 13:55:44 +0000 | [diff] [blame] | 234 | size_t iv_offset = 0; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 235 | int key_len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 236 | |
| 237 | memset(key_str, 0x00, 100); |
| 238 | memset(iv_str, 0x00, 100); |
| 239 | memset(src_str, 0x00, 100); |
| 240 | memset(dst_str, 0x00, 100); |
| 241 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 242 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 243 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 244 | key_len = unhexify( key_str, hex_key_string ); |
| 245 | unhexify( iv_str, hex_iv_string ); |
| 246 | unhexify( src_str, hex_src_string ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 247 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 248 | mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ); |
| 249 | TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_ENCRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 250 | hexify( dst_str, output, 16 ); |
| 251 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 252 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 253 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 254 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 255 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 256 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 257 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 258 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 259 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 260 | void aes_decrypt_cfb128( char *hex_key_string, char *hex_iv_string, |
| 261 | char *hex_src_string, char *hex_dst_string ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 262 | { |
| 263 | unsigned char key_str[100]; |
| 264 | unsigned char iv_str[100]; |
| 265 | unsigned char src_str[100]; |
| 266 | unsigned char dst_str[100]; |
| 267 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 268 | mbedtls_aes_context ctx; |
Paul Bakker | cd43a0b | 2011-06-09 13:55:44 +0000 | [diff] [blame] | 269 | size_t iv_offset = 0; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 270 | int key_len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 271 | |
| 272 | memset(key_str, 0x00, 100); |
| 273 | memset(iv_str, 0x00, 100); |
| 274 | memset(src_str, 0x00, 100); |
| 275 | memset(dst_str, 0x00, 100); |
| 276 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 277 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 278 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 279 | key_len = unhexify( key_str, hex_key_string ); |
| 280 | unhexify( iv_str, hex_iv_string ); |
| 281 | unhexify( src_str, hex_src_string ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 282 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 283 | mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ); |
| 284 | TEST_ASSERT( mbedtls_aes_crypt_cfb128( &ctx, MBEDTLS_AES_DECRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 285 | hexify( dst_str, output, 16 ); |
| 286 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 287 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 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 | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 291 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 292 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 293 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 294 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 295 | void aes_encrypt_cfb8( char *hex_key_string, char *hex_iv_string, |
| 296 | char *hex_src_string, char *hex_dst_string ) |
| 297 | { |
| 298 | unsigned char key_str[100]; |
| 299 | unsigned char iv_str[100]; |
| 300 | unsigned char src_str[100]; |
| 301 | unsigned char dst_str[100]; |
| 302 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 303 | mbedtls_aes_context ctx; |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 304 | int key_len, src_len; |
| 305 | |
| 306 | memset(key_str, 0x00, 100); |
| 307 | memset(iv_str, 0x00, 100); |
| 308 | memset(src_str, 0x00, 100); |
| 309 | memset(dst_str, 0x00, 100); |
| 310 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 311 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 312 | |
| 313 | key_len = unhexify( key_str, hex_key_string ); |
| 314 | unhexify( iv_str, hex_iv_string ); |
| 315 | src_len = unhexify( src_str, hex_src_string ); |
| 316 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 317 | mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ); |
| 318 | TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_ENCRYPT, src_len, iv_str, src_str, output ) == 0 ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 319 | hexify( dst_str, output, src_len ); |
| 320 | |
| 321 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 322 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 323 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 324 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 325 | } |
| 326 | /* END_CASE */ |
| 327 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 328 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 329 | void aes_decrypt_cfb8( char *hex_key_string, char *hex_iv_string, |
| 330 | char *hex_src_string, char *hex_dst_string ) |
| 331 | { |
| 332 | unsigned char key_str[100]; |
| 333 | unsigned char iv_str[100]; |
| 334 | unsigned char src_str[100]; |
| 335 | unsigned char dst_str[100]; |
| 336 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 337 | mbedtls_aes_context ctx; |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 338 | int key_len, src_len; |
| 339 | |
| 340 | memset(key_str, 0x00, 100); |
| 341 | memset(iv_str, 0x00, 100); |
| 342 | memset(src_str, 0x00, 100); |
| 343 | memset(dst_str, 0x00, 100); |
| 344 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 345 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 346 | |
| 347 | key_len = unhexify( key_str, hex_key_string ); |
| 348 | unhexify( iv_str, hex_iv_string ); |
| 349 | src_len = unhexify( src_str, hex_src_string ); |
| 350 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 351 | mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ); |
| 352 | TEST_ASSERT( mbedtls_aes_crypt_cfb8( &ctx, MBEDTLS_AES_DECRYPT, src_len, iv_str, src_str, output ) == 0 ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 353 | hexify( dst_str, output, src_len ); |
| 354 | |
| 355 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 356 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 357 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 358 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 359 | } |
| 360 | /* END_CASE */ |
| 361 | |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 362 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_OFB */ |
| 363 | void aes_encrypt_ofb( int fragment_size, char *hex_key_string, |
Simon Butcher | 0013144 | 2018-05-22 22:40:36 +0100 | [diff] [blame] | 364 | char *hex_iv_string, char *hex_src_string, |
| 365 | char *hex_dst_string ) |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 366 | { |
Simon Butcher | e416bf9 | 2018-06-02 18:28:32 +0100 | [diff] [blame] | 367 | unsigned char key_str[32]; |
| 368 | unsigned char iv_str[16]; |
| 369 | unsigned char src_str[64]; |
| 370 | unsigned char dst_str[64]; |
| 371 | unsigned char output[32]; |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 372 | mbedtls_aes_context ctx; |
| 373 | size_t iv_offset = 0; |
| 374 | int in_buffer_len; |
| 375 | unsigned char* src_str_next; |
Simon Butcher | dbe7fbf | 2018-04-29 14:51:35 +0100 | [diff] [blame] | 376 | int key_len; |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 377 | |
Simon Butcher | b7836e1 | 2018-06-02 18:36:49 +0100 | [diff] [blame] | 378 | memset( key_str, 0x00, sizeof( key_str ) ); |
| 379 | memset( iv_str, 0x00, sizeof( iv_str ) ); |
| 380 | memset( src_str, 0x00, sizeof( src_str ) ); |
| 381 | memset( dst_str, 0x00, sizeof( dst_str ) ); |
| 382 | memset( output, 0x00, sizeof( output ) ); |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 383 | mbedtls_aes_init( &ctx ); |
| 384 | |
Simon Butcher | e416bf9 | 2018-06-02 18:28:32 +0100 | [diff] [blame] | 385 | TEST_ASSERT( strlen( hex_key_string ) <= ( 32 * 2 ) ); |
| 386 | TEST_ASSERT( strlen( hex_iv_string ) <= ( 16 * 2 ) ); |
| 387 | TEST_ASSERT( strlen( hex_src_string ) <= ( 64 * 2 ) ); |
| 388 | TEST_ASSERT( strlen( hex_dst_string ) <= ( 64 * 2 ) ); |
| 389 | |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 390 | key_len = unhexify( key_str, hex_key_string ); |
Simon Butcher | dbe7fbf | 2018-04-29 14:51:35 +0100 | [diff] [blame] | 391 | unhexify( iv_str, hex_iv_string ); |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 392 | in_buffer_len = unhexify( src_str, hex_src_string ); |
| 393 | |
Simon Butcher | ad4e493 | 2018-04-29 00:43:47 +0100 | [diff] [blame] | 394 | 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] | 395 | src_str_next = src_str; |
| 396 | |
| 397 | while( in_buffer_len > 0 ) |
| 398 | { |
| 399 | TEST_ASSERT( mbedtls_aes_crypt_ofb( &ctx, fragment_size, &iv_offset, |
| 400 | iv_str, src_str_next, output ) == 0 ); |
| 401 | |
| 402 | hexify( dst_str, output, fragment_size ); |
| 403 | TEST_ASSERT( strncmp( (char *) dst_str, hex_dst_string, |
Simon Butcher | 0013144 | 2018-05-22 22:40:36 +0100 | [diff] [blame] | 404 | ( 2 * fragment_size ) ) == 0 ); |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 405 | |
| 406 | in_buffer_len -= fragment_size; |
| 407 | hex_dst_string += ( fragment_size * 2 ); |
| 408 | src_str_next += fragment_size; |
| 409 | |
| 410 | if( in_buffer_len < fragment_size ) |
| 411 | fragment_size = in_buffer_len; |
| 412 | } |
| 413 | |
| 414 | exit: |
| 415 | mbedtls_aes_free( &ctx ); |
| 416 | } |
| 417 | /* END_CASE */ |
| 418 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 419 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 420 | void aes_selftest() |
Paul Bakker | 3d36082 | 2009-07-05 11:29:38 +0000 | [diff] [blame] | 421 | { |
Andres AG | 93012e8 | 2016-09-09 09:10:28 +0100 | [diff] [blame] | 422 | TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 ); |
Paul Bakker | 3d36082 | 2009-07-05 11:29:38 +0000 | [diff] [blame] | 423 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 424 | /* END_CASE */ |