Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 1 | BEGIN_HEADER |
| 2 | #include <polarssl/aes.h> |
| 3 | END_HEADER |
| 4 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 5 | BEGIN_DEPENDENCIES |
| 6 | depends_on:POLARSSL_AES_C |
| 7 | END_DEPENDENCIES |
| 8 | |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 9 | BEGIN_CASE |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 10 | aes_encrypt_ecb:hex_key_string:hex_src_string:hex_dst_string:setkey_result |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 11 | { |
| 12 | unsigned char key_str[100]; |
| 13 | unsigned char src_str[100]; |
| 14 | unsigned char dst_str[100]; |
| 15 | unsigned char output[100]; |
| 16 | aes_context ctx; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 17 | int key_len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 18 | |
| 19 | memset(key_str, 0x00, 100); |
| 20 | memset(src_str, 0x00, 100); |
| 21 | memset(dst_str, 0x00, 100); |
| 22 | memset(output, 0x00, 100); |
| 23 | |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 24 | key_len = unhexify( key_str, {hex_key_string} ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 25 | unhexify( src_str, {hex_src_string} ); |
| 26 | |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 27 | TEST_ASSERT( aes_setkey_enc( &ctx, key_str, key_len * 8 ) == {setkey_result} ); |
| 28 | if( {setkey_result} == 0 ) |
| 29 | { |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 30 | TEST_ASSERT( aes_crypt_ecb( &ctx, AES_ENCRYPT, src_str, output ) == 0 ); |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 31 | hexify( dst_str, output, 16 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 32 | |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 33 | TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 ); |
| 34 | } |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 35 | } |
| 36 | END_CASE |
| 37 | |
| 38 | BEGIN_CASE |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 39 | aes_decrypt_ecb:hex_key_string:hex_src_string:hex_dst_string:setkey_result |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 40 | { |
| 41 | unsigned char key_str[100]; |
| 42 | unsigned char src_str[100]; |
| 43 | unsigned char dst_str[100]; |
| 44 | unsigned char output[100]; |
| 45 | aes_context ctx; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 46 | int key_len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 47 | |
| 48 | memset(key_str, 0x00, 100); |
| 49 | memset(src_str, 0x00, 100); |
| 50 | memset(dst_str, 0x00, 100); |
| 51 | memset(output, 0x00, 100); |
| 52 | |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 53 | key_len = unhexify( key_str, {hex_key_string} ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 54 | unhexify( src_str, {hex_src_string} ); |
| 55 | |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 56 | TEST_ASSERT( aes_setkey_dec( &ctx, key_str, key_len * 8 ) == {setkey_result} ); |
| 57 | if( {setkey_result} == 0 ) |
| 58 | { |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 59 | TEST_ASSERT( aes_crypt_ecb( &ctx, AES_DECRYPT, src_str, output ) == 0 ); |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 60 | hexify( dst_str, output, 16 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 61 | |
Paul Bakker | 2b222c8 | 2009-07-27 21:03:45 +0000 | [diff] [blame] | 62 | TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 ); |
| 63 | } |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 64 | } |
| 65 | END_CASE |
| 66 | |
| 67 | BEGIN_CASE |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 68 | aes_encrypt_cbc:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 69 | { |
| 70 | unsigned char key_str[100]; |
| 71 | unsigned char iv_str[100]; |
| 72 | unsigned char src_str[100]; |
| 73 | unsigned char dst_str[100]; |
| 74 | unsigned char output[100]; |
| 75 | aes_context ctx; |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 76 | int key_len, data_len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 77 | |
| 78 | memset(key_str, 0x00, 100); |
| 79 | memset(iv_str, 0x00, 100); |
| 80 | memset(src_str, 0x00, 100); |
| 81 | memset(dst_str, 0x00, 100); |
| 82 | memset(output, 0x00, 100); |
| 83 | |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 84 | key_len = unhexify( key_str, {hex_key_string} ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 85 | unhexify( iv_str, {hex_iv_string} ); |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 86 | data_len = unhexify( src_str, {hex_src_string} ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 87 | |
| 88 | aes_setkey_enc( &ctx, key_str, key_len * 8 ); |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 89 | TEST_ASSERT( aes_crypt_cbc( &ctx, AES_ENCRYPT, data_len, iv_str, src_str, output ) == {cbc_result} ); |
| 90 | if( {cbc_result} == 0 ) |
| 91 | { |
| 92 | hexify( dst_str, output, data_len ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 93 | |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 94 | TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 ); |
| 95 | } |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 96 | } |
| 97 | END_CASE |
| 98 | |
| 99 | BEGIN_CASE |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 100 | aes_decrypt_cbc:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string:cbc_result |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 101 | { |
| 102 | unsigned char key_str[100]; |
| 103 | unsigned char iv_str[100]; |
| 104 | unsigned char src_str[100]; |
| 105 | unsigned char dst_str[100]; |
| 106 | unsigned char output[100]; |
| 107 | aes_context ctx; |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 108 | int key_len, data_len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 109 | |
| 110 | memset(key_str, 0x00, 100); |
| 111 | memset(iv_str, 0x00, 100); |
| 112 | memset(src_str, 0x00, 100); |
| 113 | memset(dst_str, 0x00, 100); |
| 114 | memset(output, 0x00, 100); |
| 115 | |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 116 | key_len = unhexify( key_str, {hex_key_string} ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 117 | unhexify( iv_str, {hex_iv_string} ); |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 118 | data_len = unhexify( src_str, {hex_src_string} ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 119 | |
| 120 | aes_setkey_dec( &ctx, key_str, key_len * 8 ); |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 121 | TEST_ASSERT( aes_crypt_cbc( &ctx, AES_DECRYPT, data_len, iv_str, src_str, output ) == {cbc_result} ); |
| 122 | if( {cbc_result} == 0) |
| 123 | { |
| 124 | hexify( dst_str, output, data_len ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 125 | |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 126 | TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 ); |
| 127 | } |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 128 | } |
| 129 | END_CASE |
| 130 | |
| 131 | BEGIN_CASE |
| 132 | aes_encrypt_cfb128:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string |
| 133 | { |
| 134 | unsigned char key_str[100]; |
| 135 | unsigned char iv_str[100]; |
| 136 | unsigned char src_str[100]; |
| 137 | unsigned char dst_str[100]; |
| 138 | unsigned char output[100]; |
| 139 | aes_context ctx; |
| 140 | int iv_offset = 0; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 141 | int key_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); |
| 148 | |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 149 | key_len = unhexify( key_str, {hex_key_string} ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 150 | unhexify( iv_str, {hex_iv_string} ); |
| 151 | unhexify( src_str, {hex_src_string} ); |
| 152 | |
| 153 | aes_setkey_enc( &ctx, key_str, key_len * 8 ); |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 154 | TEST_ASSERT( aes_crypt_cfb128( &ctx, AES_ENCRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 155 | hexify( dst_str, output, 16 ); |
| 156 | |
| 157 | TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 ); |
| 158 | } |
| 159 | END_CASE |
| 160 | |
| 161 | BEGIN_CASE |
| 162 | aes_decrypt_cfb128:hex_key_string:hex_iv_string:hex_src_string:hex_dst_string |
| 163 | { |
| 164 | unsigned char key_str[100]; |
| 165 | unsigned char iv_str[100]; |
| 166 | unsigned char src_str[100]; |
| 167 | unsigned char dst_str[100]; |
| 168 | unsigned char output[100]; |
| 169 | aes_context ctx; |
| 170 | int iv_offset = 0; |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 171 | int key_len; |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 172 | |
| 173 | memset(key_str, 0x00, 100); |
| 174 | memset(iv_str, 0x00, 100); |
| 175 | memset(src_str, 0x00, 100); |
| 176 | memset(dst_str, 0x00, 100); |
| 177 | memset(output, 0x00, 100); |
| 178 | |
Paul Bakker | 69998dd | 2009-07-11 19:15:20 +0000 | [diff] [blame] | 179 | key_len = unhexify( key_str, {hex_key_string} ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 180 | unhexify( iv_str, {hex_iv_string} ); |
| 181 | unhexify( src_str, {hex_src_string} ); |
| 182 | |
| 183 | aes_setkey_enc( &ctx, key_str, key_len * 8 ); |
Paul Bakker | f3ccc68 | 2010-03-18 21:21:02 +0000 | [diff] [blame] | 184 | TEST_ASSERT( aes_crypt_cfb128( &ctx, AES_DECRYPT, 16, &iv_offset, iv_str, src_str, output ) == 0 ); |
Paul Bakker | 367dae4 | 2009-06-28 21:50:27 +0000 | [diff] [blame] | 185 | hexify( dst_str, output, 16 ); |
| 186 | |
| 187 | TEST_ASSERT( strcmp( (char *) dst_str, {hex_dst_string} ) == 0 ); |
| 188 | } |
| 189 | END_CASE |
| 190 | |
Paul Bakker | 3d36082 | 2009-07-05 11:29:38 +0000 | [diff] [blame] | 191 | BEGIN_CASE |
| 192 | aes_selftest: |
| 193 | { |
| 194 | TEST_ASSERT( aes_self_test( 0 ) == 0 ); |
| 195 | } |
| 196 | END_CASE |