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 | fb67fae | 2016-01-31 12:30:55 +0100 | [diff] [blame] | 154 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XEX */ |
| 155 | void aes_encrypt_xex( char *hex_key_string, char *hex_iv_string, |
| 156 | char *hex_src_string, char *hex_dst_string, |
| 157 | int xex_result ) |
| 158 | { |
| 159 | unsigned char key_str[100] = { 0, }; |
| 160 | unsigned char iv_str[100] = { 0, }; |
| 161 | unsigned char src_str[100] = { 0, }; |
| 162 | unsigned char dst_str[100] = { 0, }; |
| 163 | unsigned char output[100] = { 0, }; |
| 164 | mbedtls_aes_context crypt_ctx, tweak_ctx; |
| 165 | int key_len, data_len; |
| 166 | |
| 167 | mbedtls_aes_init( &crypt_ctx ); |
| 168 | mbedtls_aes_init( &tweak_ctx ); |
| 169 | |
| 170 | key_len = unhexify( key_str, hex_key_string ); |
| 171 | unhexify( iv_str, hex_iv_string ); |
| 172 | data_len = unhexify( src_str, hex_src_string ); |
| 173 | |
| 174 | mbedtls_aes_setkey_enc( &crypt_ctx, key_str, ( key_len * 8 ) / 2 ); |
| 175 | mbedtls_aes_setkey_enc( &tweak_ctx, key_str + key_len / 2, ( key_len * 8 ) / 2 ); |
| 176 | |
| 177 | TEST_ASSERT( mbedtls_aes_crypt_xex( &crypt_ctx, &tweak_ctx, MBEDTLS_AES_ENCRYPT, data_len, iv_str, src_str, output ) == xex_result ); |
| 178 | if( xex_result == 0 ) |
| 179 | { |
| 180 | hexify( dst_str, output, data_len ); |
| 181 | |
| 182 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
| 183 | } |
| 184 | |
| 185 | exit: |
| 186 | mbedtls_aes_free( &crypt_ctx ); |
| 187 | mbedtls_aes_free( &tweak_ctx ); |
| 188 | } |
| 189 | /* END_CASE */ |
| 190 | |
| 191 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XEX */ |
| 192 | void aes_decrypt_xex( char *hex_key_string, char *hex_iv_string, |
| 193 | char *hex_src_string, char *hex_dst_string, |
| 194 | int xex_result ) |
| 195 | { |
| 196 | unsigned char key_str[100] = { 0, }; |
| 197 | unsigned char iv_str[100] = { 0, }; |
| 198 | unsigned char src_str[100] = { 0, }; |
| 199 | unsigned char dst_str[100] = { 0, }; |
| 200 | unsigned char output[100] = { 0, }; |
| 201 | mbedtls_aes_context crypt_ctx, tweak_ctx; |
| 202 | int key_len, data_len; |
| 203 | |
| 204 | mbedtls_aes_init( &crypt_ctx ); |
| 205 | mbedtls_aes_init( &tweak_ctx ); |
| 206 | |
| 207 | key_len = unhexify( key_str, hex_key_string ); |
| 208 | unhexify( iv_str, hex_iv_string ); |
| 209 | data_len = unhexify( src_str, hex_src_string ); |
| 210 | |
| 211 | mbedtls_aes_setkey_dec( &crypt_ctx, key_str, ( key_len * 8 ) / 2 ); |
| 212 | mbedtls_aes_setkey_enc( &tweak_ctx, key_str + key_len / 2, ( key_len * 8 ) / 2 ); |
| 213 | |
| 214 | TEST_ASSERT( mbedtls_aes_crypt_xex( &crypt_ctx, &tweak_ctx, MBEDTLS_AES_DECRYPT, data_len, iv_str, src_str, output ) == xex_result ); |
| 215 | if( xex_result == 0 ) |
| 216 | { |
| 217 | hexify( dst_str, output, data_len ); |
| 218 | |
| 219 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
| 220 | } |
| 221 | |
| 222 | exit: |
| 223 | mbedtls_aes_free( &crypt_ctx ); |
| 224 | mbedtls_aes_free( &tweak_ctx ); |
| 225 | } |
| 226 | /* END_CASE */ |
| 227 | |
Aorimn | 5f77801 | 2016-06-09 23:22:58 +0200 | [diff] [blame^] | 228 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
| 229 | void aes_encrypt_xts( char *hex_key_string, char *hex_iv_string, |
| 230 | char *hex_src_string, char *hex_dst_string, |
| 231 | int data_unit_len, int xts_result ) |
| 232 | { |
| 233 | unsigned char key_str[100] = { 0, }; |
| 234 | unsigned char iv_str[100] = { 0, }; |
| 235 | unsigned char src_str[100] = { 0, }; |
| 236 | unsigned char dst_str[100] = { 0, }; |
| 237 | unsigned char output[100] = { 0, }; |
| 238 | mbedtls_aes_context crypt_ctx, tweak_ctx; |
| 239 | int key_len, data_len; |
| 240 | |
| 241 | mbedtls_aes_init( &crypt_ctx ); |
| 242 | mbedtls_aes_init( &tweak_ctx ); |
| 243 | |
| 244 | key_len = unhexify( key_str, hex_key_string ); |
| 245 | unhexify( iv_str, hex_iv_string ); |
| 246 | data_len = unhexify( src_str, hex_src_string ); |
| 247 | |
| 248 | mbedtls_aes_setkey_enc( &crypt_ctx, key_str, ( key_len * 8 ) / 2 ); |
| 249 | mbedtls_aes_setkey_enc( &tweak_ctx, key_str + key_len / 2, ( key_len * 8 ) / 2 ); |
| 250 | |
| 251 | TEST_ASSERT( mbedtls_aes_crypt_xts( &crypt_ctx, &tweak_ctx, MBEDTLS_AES_ENCRYPT, data_unit_len, iv_str, src_str, output ) == xts_result ); |
| 252 | if( xts_result == 0 ) |
| 253 | { |
| 254 | hexify( dst_str, output, data_len ); |
| 255 | |
| 256 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
| 257 | } |
| 258 | |
| 259 | exit: |
| 260 | mbedtls_aes_free( &crypt_ctx ); |
| 261 | mbedtls_aes_free( &tweak_ctx ); |
| 262 | } |
| 263 | /* END_CASE */ |
| 264 | |
| 265 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
| 266 | void aes_decrypt_xts( char *hex_key_string, char *hex_iv_string, |
| 267 | char *hex_src_string, char *hex_dst_string, |
| 268 | int data_unit_len, int xts_result ) |
| 269 | { |
| 270 | unsigned char key_str[100] = { 0, }; |
| 271 | unsigned char iv_str[100] = { 0, }; |
| 272 | unsigned char src_str[100] = { 0, }; |
| 273 | unsigned char dst_str[100] = { 0, }; |
| 274 | unsigned char output[100] = { 0, }; |
| 275 | mbedtls_aes_context crypt_ctx, tweak_ctx; |
| 276 | int key_len, data_len; |
| 277 | |
| 278 | mbedtls_aes_init( &crypt_ctx ); |
| 279 | mbedtls_aes_init( &tweak_ctx ); |
| 280 | |
| 281 | key_len = unhexify( key_str, hex_key_string ); |
| 282 | unhexify( iv_str, hex_iv_string ); |
| 283 | data_len = unhexify( src_str, hex_src_string ); |
| 284 | |
| 285 | mbedtls_aes_setkey_dec( &crypt_ctx, key_str, ( key_len * 8 ) / 2 ); |
| 286 | mbedtls_aes_setkey_enc( &tweak_ctx, key_str + key_len / 2, ( key_len * 8 ) / 2 ); |
| 287 | |
| 288 | TEST_ASSERT( mbedtls_aes_crypt_xts( &crypt_ctx, &tweak_ctx, MBEDTLS_AES_DECRYPT, data_unit_len, iv_str, src_str, output ) == xts_result ); |
| 289 | if( xts_result == 0 ) |
| 290 | { |
| 291 | hexify( dst_str, output, data_len ); |
| 292 | |
| 293 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
| 294 | } |
| 295 | |
| 296 | exit: |
| 297 | mbedtls_aes_free( &crypt_ctx ); |
| 298 | mbedtls_aes_free( &tweak_ctx ); |
| 299 | } |
| 300 | /* END_CASE */ |
| 301 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 302 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 303 | void aes_encrypt_cfb128( char *hex_key_string, char *hex_iv_string, |
| 304 | char *hex_src_string, char *hex_dst_string ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 305 | { |
| 306 | unsigned char key_str[100]; |
| 307 | unsigned char iv_str[100]; |
| 308 | unsigned char src_str[100]; |
| 309 | unsigned char dst_str[100]; |
| 310 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 311 | mbedtls_aes_context ctx; |
Paul Bakker | cd43a0b | 2011-06-09 13:55:44 +0000 | [diff] [blame] | 312 | size_t iv_offset = 0; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 313 | int key_len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 314 | |
| 315 | memset(key_str, 0x00, 100); |
| 316 | memset(iv_str, 0x00, 100); |
| 317 | memset(src_str, 0x00, 100); |
| 318 | memset(dst_str, 0x00, 100); |
| 319 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 320 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 321 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 322 | key_len = unhexify( key_str, hex_key_string ); |
| 323 | unhexify( iv_str, hex_iv_string ); |
| 324 | unhexify( src_str, hex_src_string ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 325 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 326 | mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ); |
| 327 | 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] | 328 | hexify( dst_str, output, 16 ); |
| 329 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 330 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 331 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 332 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 333 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 334 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 335 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 336 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 337 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 338 | void aes_decrypt_cfb128( char *hex_key_string, char *hex_iv_string, |
| 339 | char *hex_src_string, char *hex_dst_string ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 340 | { |
| 341 | unsigned char key_str[100]; |
| 342 | unsigned char iv_str[100]; |
| 343 | unsigned char src_str[100]; |
| 344 | unsigned char dst_str[100]; |
| 345 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 346 | mbedtls_aes_context ctx; |
Paul Bakker | cd43a0b | 2011-06-09 13:55:44 +0000 | [diff] [blame] | 347 | size_t iv_offset = 0; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 348 | int key_len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 349 | |
| 350 | memset(key_str, 0x00, 100); |
| 351 | memset(iv_str, 0x00, 100); |
| 352 | memset(src_str, 0x00, 100); |
| 353 | memset(dst_str, 0x00, 100); |
| 354 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 355 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 356 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 357 | key_len = unhexify( key_str, hex_key_string ); |
| 358 | unhexify( iv_str, hex_iv_string ); |
| 359 | unhexify( src_str, hex_src_string ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 360 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 361 | mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ); |
| 362 | 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] | 363 | hexify( dst_str, output, 16 ); |
| 364 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 365 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 366 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 367 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 368 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 369 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 370 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 371 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 372 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 373 | void aes_encrypt_cfb8( char *hex_key_string, char *hex_iv_string, |
| 374 | char *hex_src_string, char *hex_dst_string ) |
| 375 | { |
| 376 | unsigned char key_str[100]; |
| 377 | unsigned char iv_str[100]; |
| 378 | unsigned char src_str[100]; |
| 379 | unsigned char dst_str[100]; |
| 380 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 381 | mbedtls_aes_context ctx; |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 382 | int key_len, src_len; |
| 383 | |
| 384 | memset(key_str, 0x00, 100); |
| 385 | memset(iv_str, 0x00, 100); |
| 386 | memset(src_str, 0x00, 100); |
| 387 | memset(dst_str, 0x00, 100); |
| 388 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 389 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 390 | |
| 391 | key_len = unhexify( key_str, hex_key_string ); |
| 392 | unhexify( iv_str, hex_iv_string ); |
| 393 | src_len = unhexify( src_str, hex_src_string ); |
| 394 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 395 | mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ); |
| 396 | 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] | 397 | hexify( dst_str, output, src_len ); |
| 398 | |
| 399 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 400 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 401 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 402 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 403 | } |
| 404 | /* END_CASE */ |
| 405 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 406 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 407 | void aes_decrypt_cfb8( char *hex_key_string, char *hex_iv_string, |
| 408 | char *hex_src_string, char *hex_dst_string ) |
| 409 | { |
| 410 | unsigned char key_str[100]; |
| 411 | unsigned char iv_str[100]; |
| 412 | unsigned char src_str[100]; |
| 413 | unsigned char dst_str[100]; |
| 414 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 415 | mbedtls_aes_context ctx; |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 416 | int key_len, src_len; |
| 417 | |
| 418 | memset(key_str, 0x00, 100); |
| 419 | memset(iv_str, 0x00, 100); |
| 420 | memset(src_str, 0x00, 100); |
| 421 | memset(dst_str, 0x00, 100); |
| 422 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 423 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 424 | |
| 425 | key_len = unhexify( key_str, hex_key_string ); |
| 426 | unhexify( iv_str, hex_iv_string ); |
| 427 | src_len = unhexify( src_str, hex_src_string ); |
| 428 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 429 | mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ); |
| 430 | 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] | 431 | hexify( dst_str, output, src_len ); |
| 432 | |
| 433 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 434 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 435 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 436 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 437 | } |
| 438 | /* END_CASE */ |
| 439 | |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 440 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_OFB */ |
| 441 | void aes_encrypt_ofb( int fragment_size, char *hex_key_string, |
Simon Butcher | 0013144 | 2018-05-22 22:40:36 +0100 | [diff] [blame] | 442 | char *hex_iv_string, char *hex_src_string, |
| 443 | char *hex_dst_string ) |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 444 | { |
Simon Butcher | e416bf9 | 2018-06-02 18:28:32 +0100 | [diff] [blame] | 445 | unsigned char key_str[32]; |
| 446 | unsigned char iv_str[16]; |
| 447 | unsigned char src_str[64]; |
| 448 | unsigned char dst_str[64]; |
| 449 | unsigned char output[32]; |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 450 | mbedtls_aes_context ctx; |
| 451 | size_t iv_offset = 0; |
| 452 | int in_buffer_len; |
| 453 | unsigned char* src_str_next; |
Simon Butcher | dbe7fbf | 2018-04-29 14:51:35 +0100 | [diff] [blame] | 454 | int key_len; |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 455 | |
Simon Butcher | b7836e1 | 2018-06-02 18:36:49 +0100 | [diff] [blame] | 456 | memset( key_str, 0x00, sizeof( key_str ) ); |
| 457 | memset( iv_str, 0x00, sizeof( iv_str ) ); |
| 458 | memset( src_str, 0x00, sizeof( src_str ) ); |
| 459 | memset( dst_str, 0x00, sizeof( dst_str ) ); |
| 460 | memset( output, 0x00, sizeof( output ) ); |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 461 | mbedtls_aes_init( &ctx ); |
| 462 | |
Simon Butcher | e416bf9 | 2018-06-02 18:28:32 +0100 | [diff] [blame] | 463 | TEST_ASSERT( strlen( hex_key_string ) <= ( 32 * 2 ) ); |
| 464 | TEST_ASSERT( strlen( hex_iv_string ) <= ( 16 * 2 ) ); |
| 465 | TEST_ASSERT( strlen( hex_src_string ) <= ( 64 * 2 ) ); |
| 466 | TEST_ASSERT( strlen( hex_dst_string ) <= ( 64 * 2 ) ); |
| 467 | |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 468 | key_len = unhexify( key_str, hex_key_string ); |
Simon Butcher | dbe7fbf | 2018-04-29 14:51:35 +0100 | [diff] [blame] | 469 | unhexify( iv_str, hex_iv_string ); |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 470 | in_buffer_len = unhexify( src_str, hex_src_string ); |
| 471 | |
Simon Butcher | ad4e493 | 2018-04-29 00:43:47 +0100 | [diff] [blame] | 472 | 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] | 473 | src_str_next = src_str; |
| 474 | |
| 475 | while( in_buffer_len > 0 ) |
| 476 | { |
| 477 | TEST_ASSERT( mbedtls_aes_crypt_ofb( &ctx, fragment_size, &iv_offset, |
| 478 | iv_str, src_str_next, output ) == 0 ); |
| 479 | |
| 480 | hexify( dst_str, output, fragment_size ); |
| 481 | TEST_ASSERT( strncmp( (char *) dst_str, hex_dst_string, |
Simon Butcher | 0013144 | 2018-05-22 22:40:36 +0100 | [diff] [blame] | 482 | ( 2 * fragment_size ) ) == 0 ); |
Simon Butcher | 0301884 | 2018-04-22 22:57:58 +0100 | [diff] [blame] | 483 | |
| 484 | in_buffer_len -= fragment_size; |
| 485 | hex_dst_string += ( fragment_size * 2 ); |
| 486 | src_str_next += fragment_size; |
| 487 | |
| 488 | if( in_buffer_len < fragment_size ) |
| 489 | fragment_size = in_buffer_len; |
| 490 | } |
| 491 | |
| 492 | exit: |
| 493 | mbedtls_aes_free( &ctx ); |
| 494 | } |
| 495 | /* END_CASE */ |
| 496 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 497 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 498 | void aes_selftest() |
Paul Bakker | 3d36082 | 2009-07-05 11:29:38 +0000 | [diff] [blame] | 499 | { |
Andres AG | 93012e8 | 2016-09-09 09:10:28 +0100 | [diff] [blame] | 500 | TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 ); |
Paul Bakker | 3d36082 | 2009-07-05 11:29:38 +0000 | [diff] [blame] | 501 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 502 | /* END_CASE */ |