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