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 | |
Jaeden Amero | 184d069 | 2018-04-28 17:26:25 +0100 | [diff] [blame^] | 10 | #if 0 |
| 11 | /* BEGIN_SUITE_HELPERS depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
| 12 | static void aes_crypt_xts( char *hex_key_string |
| 13 | char *hex_data_unit_string, |
| 14 | char *hex_src_string, char *hex_dst_string, |
| 15 | int mode) |
| 16 | { |
| 17 | /* XXX Code Review: is this possible? (having a shared helper function for |
| 18 | * a test, enabled only when that test is). Would line numbers and test |
| 19 | * results look appropriate still or would it be too much indirection? */ |
| 20 | } |
| 21 | /* END_SUITE_HELPERS */ |
| 22 | #endif |
| 23 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 24 | /* BEGIN_CASE */ |
| 25 | void aes_encrypt_ecb( char *hex_key_string, char *hex_src_string, |
| 26 | char *hex_dst_string, int setkey_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 27 | { |
| 28 | unsigned char key_str[100]; |
| 29 | unsigned char src_str[100]; |
| 30 | unsigned char dst_str[100]; |
| 31 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 32 | mbedtls_aes_context ctx; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 33 | int key_len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 34 | |
| 35 | memset(key_str, 0x00, 100); |
| 36 | memset(src_str, 0x00, 100); |
| 37 | memset(dst_str, 0x00, 100); |
| 38 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 39 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 40 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 41 | key_len = unhexify( key_str, hex_key_string ); |
| 42 | unhexify( src_str, hex_src_string ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 43 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 44 | 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] | 45 | if( setkey_result == 0 ) |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 46 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 47 | 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] | 48 | hexify( dst_str, output, 16 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 49 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 50 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 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 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 58 | /* BEGIN_CASE */ |
| 59 | void aes_decrypt_ecb( char *hex_key_string, char *hex_src_string, |
| 60 | char *hex_dst_string, int setkey_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 61 | { |
| 62 | unsigned char key_str[100]; |
| 63 | unsigned char src_str[100]; |
| 64 | unsigned char dst_str[100]; |
| 65 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 66 | mbedtls_aes_context ctx; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 67 | int key_len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 68 | |
| 69 | memset(key_str, 0x00, 100); |
| 70 | memset(src_str, 0x00, 100); |
| 71 | memset(dst_str, 0x00, 100); |
| 72 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 73 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 74 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 75 | key_len = unhexify( key_str, hex_key_string ); |
| 76 | unhexify( src_str, hex_src_string ); |
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 | 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] | 79 | if( setkey_result == 0 ) |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 80 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 81 | 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] | 82 | hexify( dst_str, output, 16 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 83 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 84 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 85 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 86 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 87 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 88 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 89 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 90 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 91 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 92 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 93 | void aes_encrypt_cbc( char *hex_key_string, char *hex_iv_string, |
| 94 | char *hex_src_string, char *hex_dst_string, |
| 95 | int cbc_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 96 | { |
| 97 | unsigned char key_str[100]; |
| 98 | unsigned char iv_str[100]; |
| 99 | unsigned char src_str[100]; |
| 100 | unsigned char dst_str[100]; |
| 101 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 102 | mbedtls_aes_context ctx; |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 103 | int key_len, data_len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 104 | |
| 105 | memset(key_str, 0x00, 100); |
| 106 | memset(iv_str, 0x00, 100); |
| 107 | memset(src_str, 0x00, 100); |
| 108 | memset(dst_str, 0x00, 100); |
| 109 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 110 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 111 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 112 | key_len = unhexify( key_str, hex_key_string ); |
| 113 | unhexify( iv_str, hex_iv_string ); |
| 114 | data_len = unhexify( src_str, hex_src_string ); |
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 | mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ); |
| 117 | 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] | 118 | if( cbc_result == 0 ) |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 119 | { |
| 120 | hexify( dst_str, output, data_len ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 121 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 122 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 123 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 124 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 125 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 126 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 127 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 128 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 129 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 131 | void aes_decrypt_cbc( char *hex_key_string, char *hex_iv_string, |
| 132 | char *hex_src_string, char *hex_dst_string, |
| 133 | int cbc_result ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 134 | { |
| 135 | unsigned char key_str[100]; |
| 136 | unsigned char iv_str[100]; |
| 137 | unsigned char src_str[100]; |
| 138 | unsigned char dst_str[100]; |
| 139 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | mbedtls_aes_context ctx; |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 141 | int key_len, data_len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 142 | |
| 143 | memset(key_str, 0x00, 100); |
| 144 | memset(iv_str, 0x00, 100); |
| 145 | memset(src_str, 0x00, 100); |
| 146 | memset(dst_str, 0x00, 100); |
| 147 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 148 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 149 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 150 | key_len = unhexify( key_str, hex_key_string ); |
| 151 | unhexify( iv_str, hex_iv_string ); |
| 152 | data_len = unhexify( src_str, hex_src_string ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 153 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 154 | mbedtls_aes_setkey_dec( &ctx, key_str, key_len * 8 ); |
| 155 | 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] | 156 | if( cbc_result == 0) |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 157 | { |
| 158 | hexify( dst_str, output, data_len ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 159 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 160 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 161 | } |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 162 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 163 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 164 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 165 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 166 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 167 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 168 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 169 | void aes_encrypt_cfb128( char *hex_key_string, char *hex_iv_string, |
| 170 | char *hex_src_string, char *hex_dst_string ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 171 | { |
| 172 | unsigned char key_str[100]; |
| 173 | unsigned char iv_str[100]; |
| 174 | unsigned char src_str[100]; |
| 175 | unsigned char dst_str[100]; |
| 176 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 177 | mbedtls_aes_context ctx; |
Paul Bakker | cd43a0b | 2011-06-09 13:55:44 +0000 | [diff] [blame] | 178 | size_t iv_offset = 0; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 179 | int key_len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 180 | |
| 181 | memset(key_str, 0x00, 100); |
| 182 | memset(iv_str, 0x00, 100); |
| 183 | memset(src_str, 0x00, 100); |
| 184 | memset(dst_str, 0x00, 100); |
| 185 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 186 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 187 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 188 | key_len = unhexify( key_str, hex_key_string ); |
| 189 | unhexify( iv_str, hex_iv_string ); |
| 190 | unhexify( src_str, hex_src_string ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 191 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 192 | mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ); |
| 193 | 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] | 194 | hexify( dst_str, output, 16 ); |
| 195 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 196 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 197 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 198 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 199 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 200 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 201 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 202 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 203 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 204 | void aes_decrypt_cfb128( char *hex_key_string, char *hex_iv_string, |
| 205 | char *hex_src_string, char *hex_dst_string ) |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 206 | { |
| 207 | unsigned char key_str[100]; |
| 208 | unsigned char iv_str[100]; |
| 209 | unsigned char src_str[100]; |
| 210 | unsigned char dst_str[100]; |
| 211 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 212 | mbedtls_aes_context ctx; |
Paul Bakker | cd43a0b | 2011-06-09 13:55:44 +0000 | [diff] [blame] | 213 | size_t iv_offset = 0; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 214 | int key_len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 215 | |
| 216 | memset(key_str, 0x00, 100); |
| 217 | memset(iv_str, 0x00, 100); |
| 218 | memset(src_str, 0x00, 100); |
| 219 | memset(dst_str, 0x00, 100); |
| 220 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 221 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 222 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 223 | key_len = unhexify( key_str, hex_key_string ); |
| 224 | unhexify( iv_str, hex_iv_string ); |
| 225 | unhexify( src_str, hex_src_string ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 226 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ); |
| 228 | 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] | 229 | hexify( dst_str, output, 16 ); |
| 230 | |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 231 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 232 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 233 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 234 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 235 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 236 | /* END_CASE */ |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 237 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 238 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 239 | void aes_encrypt_cfb8( char *hex_key_string, char *hex_iv_string, |
| 240 | char *hex_src_string, char *hex_dst_string ) |
| 241 | { |
| 242 | unsigned char key_str[100]; |
| 243 | unsigned char iv_str[100]; |
| 244 | unsigned char src_str[100]; |
| 245 | unsigned char dst_str[100]; |
| 246 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 247 | mbedtls_aes_context ctx; |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 248 | int key_len, src_len; |
| 249 | |
| 250 | memset(key_str, 0x00, 100); |
| 251 | memset(iv_str, 0x00, 100); |
| 252 | memset(src_str, 0x00, 100); |
| 253 | memset(dst_str, 0x00, 100); |
| 254 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 255 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 256 | |
| 257 | key_len = unhexify( key_str, hex_key_string ); |
| 258 | unhexify( iv_str, hex_iv_string ); |
| 259 | src_len = unhexify( src_str, hex_src_string ); |
| 260 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 261 | mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ); |
| 262 | 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] | 263 | hexify( dst_str, output, src_len ); |
| 264 | |
| 265 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 266 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 267 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 268 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 269 | } |
| 270 | /* END_CASE */ |
| 271 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 272 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CFB */ |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 273 | void aes_decrypt_cfb8( char *hex_key_string, char *hex_iv_string, |
| 274 | char *hex_src_string, char *hex_dst_string ) |
| 275 | { |
| 276 | unsigned char key_str[100]; |
| 277 | unsigned char iv_str[100]; |
| 278 | unsigned char src_str[100]; |
| 279 | unsigned char dst_str[100]; |
| 280 | unsigned char output[100]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 281 | mbedtls_aes_context ctx; |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 282 | int key_len, src_len; |
| 283 | |
| 284 | memset(key_str, 0x00, 100); |
| 285 | memset(iv_str, 0x00, 100); |
| 286 | memset(src_str, 0x00, 100); |
| 287 | memset(dst_str, 0x00, 100); |
| 288 | memset(output, 0x00, 100); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 289 | mbedtls_aes_init( &ctx ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 290 | |
| 291 | key_len = unhexify( key_str, hex_key_string ); |
| 292 | unhexify( iv_str, hex_iv_string ); |
| 293 | src_len = unhexify( src_str, hex_src_string ); |
| 294 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 295 | mbedtls_aes_setkey_enc( &ctx, key_str, key_len * 8 ); |
| 296 | 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] | 297 | hexify( dst_str, output, src_len ); |
| 298 | |
| 299 | TEST_ASSERT( strcmp( (char *) dst_str, hex_dst_string ) == 0 ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 300 | |
Paul Bakker | bd51b26 | 2014-07-10 15:26:12 +0200 | [diff] [blame] | 301 | exit: |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 302 | mbedtls_aes_free( &ctx ); |
Paul Bakker | 556efba | 2014-01-24 15:38:12 +0100 | [diff] [blame] | 303 | } |
| 304 | /* END_CASE */ |
| 305 | |
Jaeden Amero | 184d069 | 2018-04-28 17:26:25 +0100 | [diff] [blame^] | 306 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
| 307 | void aes_encrypt_xts( char *hex_key_string, char *hex_data_unit_string, |
| 308 | char *hex_src_string, char *hex_dst_string ) |
| 309 | { |
| 310 | enum { AES_BLOCK_SIZE = 16 }; |
| 311 | unsigned char *key; |
| 312 | unsigned char *data_unit; |
| 313 | unsigned char *src; |
| 314 | unsigned char *dst; |
| 315 | unsigned char *output; |
| 316 | mbedtls_aes_xts_context ctx; |
| 317 | size_t key_len, src_len, dst_len, data_unit_len; |
| 318 | |
| 319 | mbedtls_aes_xts_init( &ctx ); |
| 320 | |
| 321 | data_unit = unhexify_alloc( hex_data_unit_string, &data_unit_len ); |
| 322 | TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE ); |
| 323 | |
| 324 | key = unhexify_alloc( hex_key_string, &key_len ); |
| 325 | TEST_ASSERT( key_len % 2 == 0 ); |
| 326 | |
| 327 | src = unhexify_alloc( hex_src_string, &src_len ); |
| 328 | dst = unhexify_alloc( hex_dst_string, &dst_len ); |
| 329 | TEST_ASSERT( src_len == dst_len ); |
| 330 | |
| 331 | output = zero_alloc(dst_len); |
| 332 | |
| 333 | mbedtls_aes_xts_setkey_enc( &ctx, key, key_len * 8 ); |
| 334 | TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_ENCRYPT, src_len, data_unit, src, output ) == 0 ); // XXX Code Review: must this be one line? |
| 335 | |
| 336 | TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 ); |
| 337 | |
| 338 | exit: |
| 339 | mbedtls_aes_xts_free( &ctx ); |
| 340 | } |
| 341 | /* END_CASE */ |
| 342 | |
| 343 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
| 344 | void aes_decrypt_xts( char *hex_key_string, char *hex_data_unit_string, |
| 345 | char *hex_dst_string, char *hex_src_string ) |
| 346 | { |
| 347 | /* XXX Code Review: I don't really like all this duplicated code between |
| 348 | * encrypt and decrypt. The only thing different is the mode and key |
| 349 | * initialization. */ |
| 350 | enum { AES_BLOCK_SIZE = 16 }; |
| 351 | unsigned char *key; |
| 352 | unsigned char *data_unit; |
| 353 | unsigned char *src; |
| 354 | unsigned char *dst; |
| 355 | unsigned char *output; |
| 356 | mbedtls_aes_xts_context ctx; |
| 357 | size_t key_len, src_len, dst_len, data_unit_len; |
| 358 | |
| 359 | mbedtls_aes_xts_init( &ctx ); |
| 360 | |
| 361 | data_unit = unhexify_alloc( hex_data_unit_string, &data_unit_len ); |
| 362 | TEST_ASSERT( data_unit_len == AES_BLOCK_SIZE ); |
| 363 | |
| 364 | key = unhexify_alloc( hex_key_string, &key_len ); |
| 365 | TEST_ASSERT( key_len % 2 == 0 ); |
| 366 | src = unhexify_alloc( hex_src_string, &src_len ); |
| 367 | dst = unhexify_alloc( hex_dst_string, &dst_len ); |
| 368 | TEST_ASSERT( src_len == dst_len ); |
| 369 | |
| 370 | output = zero_alloc(dst_len); |
| 371 | |
| 372 | mbedtls_aes_xts_setkey_dec( &ctx, key, key_len * 8 ); |
| 373 | TEST_ASSERT( mbedtls_aes_crypt_xts( &ctx, MBEDTLS_AES_DECRYPT, src_len, data_unit, src, output ) == 0 ); // XXX Code Review: must this be one line? |
| 374 | |
| 375 | TEST_ASSERT( memcmp( output, dst, dst_len ) == 0 ); |
| 376 | |
| 377 | exit: |
| 378 | mbedtls_aes_xts_free( &ctx ); |
| 379 | } |
| 380 | /* END_CASE */ |
| 381 | |
| 382 | /* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_XTS */ |
| 383 | void aes_crypt_xts_size( int size, int retval ) |
| 384 | { |
| 385 | size_t length = size; |
| 386 | /* Note that this function will most likely crash on failure, as NULL |
| 387 | * parameters will be used. In the passing case, the length check in |
| 388 | * mbedtls_aes_crypt_xts() will prevent any accesses to parameters by |
| 389 | * exiting the function early. XXX <-- Hey look, Mr. Codereviewer. What do |
| 390 | * you think? Any ideas how to be safe and not have to actually allocate a |
| 391 | * 16 MiB buffer nor add NULL checks to mbedtls_aes_crypt_xts()? */ |
| 392 | TEST_ASSERT( mbedtls_aes_crypt_xts( NULL, MBEDTLS_AES_ENCRYPT, length, NULL, NULL, NULL ) == retval ); |
| 393 | } |
| 394 | /* END_CASE */ |
| 395 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 396 | /* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST */ |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 397 | void aes_selftest() |
Paul Bakker | 3d36082 | 2009-07-05 11:29:38 +0000 | [diff] [blame] | 398 | { |
Andres AG | 93012e8 | 2016-09-09 09:10:28 +0100 | [diff] [blame] | 399 | TEST_ASSERT( mbedtls_aes_self_test( 1 ) == 0 ); |
Paul Bakker | 3d36082 | 2009-07-05 11:29:38 +0000 | [diff] [blame] | 400 | } |
Paul Bakker | 33b43f1 | 2013-08-20 11:48:36 +0200 | [diff] [blame] | 401 | /* END_CASE */ |