Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 1 | BEGIN_HEADER |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 2 | #include <polarssl/cipher.h> |
| 3 | END_HEADER |
| 4 | |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame^] | 5 | BEGIN_DEPENDENCIES |
| 6 | depends_on:POLARSSL_CIPHER_C |
| 7 | END_DEPENDENCIES |
| 8 | |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 9 | BEGIN_CASE |
| 10 | enc_dec_buf:cipher_id:cipher_string:key_len:length: |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 11 | size_t length = {length}; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 12 | unsigned char key[32]; |
| 13 | unsigned char iv[16]; |
| 14 | |
| 15 | const cipher_info_t *cipher_info; |
| 16 | cipher_context_t ctx_dec; |
| 17 | cipher_context_t ctx_enc; |
| 18 | |
| 19 | unsigned char inbuf[64]; |
| 20 | unsigned char encbuf[64]; |
| 21 | unsigned char decbuf[64]; |
| 22 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 23 | size_t outlen = 0; |
| 24 | size_t enclen = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 25 | |
| 26 | memset( key, 0, 32 ); |
| 27 | memset( iv , 0, 16 ); |
| 28 | |
| 29 | memset( &ctx_dec, 0, sizeof( ctx_dec ) ); |
| 30 | memset( &ctx_enc, 0, sizeof( ctx_enc ) ); |
| 31 | |
| 32 | memset( inbuf, 5, 64 ); |
| 33 | memset( encbuf, 0, 64 ); |
| 34 | memset( decbuf, 0, 64 ); |
| 35 | |
| 36 | /* Check and get info structures */ |
| 37 | cipher_info = cipher_info_from_type( {cipher_id} ); |
| 38 | TEST_ASSERT( NULL != cipher_info ); |
| 39 | TEST_ASSERT( cipher_info_from_string( "{cipher_string}" ) == cipher_info ); |
| 40 | |
| 41 | /* Initialise enc and dec contexts */ |
| 42 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); |
| 43 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); |
| 44 | |
| 45 | TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, {key_len}, POLARSSL_DECRYPT ) ); |
| 46 | TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, {key_len}, POLARSSL_ENCRYPT ) ); |
| 47 | |
| 48 | TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) ); |
| 49 | TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv ) ); |
| 50 | |
| 51 | enclen = cipher_get_block_size( &ctx_enc ) |
| 52 | * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); |
| 53 | |
| 54 | /* encode length number of bytes from inbuf */ |
| 55 | TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) ); |
| 56 | TEST_ASSERT( outlen == enclen - cipher_get_block_size ( &ctx_enc ) ); |
| 57 | TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) ); |
| 58 | TEST_ASSERT( outlen == cipher_get_block_size ( &ctx_enc ) ); |
| 59 | |
| 60 | /* decode the previously encoded string */ |
| 61 | TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); |
| 62 | TEST_ASSERT( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); |
| 63 | TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); |
| 64 | TEST_ASSERT( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); |
| 65 | |
| 66 | TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) ); |
| 67 | |
| 68 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); |
| 69 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) ); |
| 70 | END_CASE |
| 71 | |
| 72 | BEGIN_CASE |
| 73 | dec_empty_buf: |
| 74 | unsigned char key[32]; |
| 75 | unsigned char iv[16]; |
| 76 | |
| 77 | cipher_context_t ctx_dec; |
| 78 | const cipher_info_t *cipher_info; |
| 79 | |
| 80 | unsigned char encbuf[64]; |
| 81 | unsigned char decbuf[64]; |
| 82 | |
Paul Bakker | f4a3f30 | 2011-04-24 15:53:29 +0000 | [diff] [blame] | 83 | size_t outlen = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 84 | |
| 85 | memset( key, 0, 32 ); |
| 86 | memset( iv , 0, 16 ); |
| 87 | |
| 88 | memset( &ctx_dec, 0, sizeof( ctx_dec ) ); |
| 89 | |
| 90 | memset( encbuf, 0, 64 ); |
| 91 | memset( decbuf, 0, 64 ); |
| 92 | |
| 93 | /* Initialise enc and dec contexts */ |
| 94 | cipher_info = cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC ); |
| 95 | TEST_ASSERT( NULL != cipher_info); |
| 96 | |
| 97 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); |
| 98 | |
| 99 | TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, 128, POLARSSL_DECRYPT ) ); |
| 100 | |
| 101 | TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) ); |
| 102 | |
| 103 | /* decode 0-byte string */ |
| 104 | TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) ); |
| 105 | TEST_ASSERT( 0 == outlen ); |
| 106 | TEST_ASSERT( 1 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); |
| 107 | TEST_ASSERT( 0 == outlen ); |
| 108 | |
| 109 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); |
| 110 | END_CASE |
| 111 | |
| 112 | BEGIN_CASE |
| 113 | enc_dec_buf_multipart:cipher_id:key_len:first_length:second_length: |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 114 | size_t first_length = {first_length}; |
| 115 | size_t second_length = {second_length}; |
| 116 | size_t length = first_length + second_length; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 117 | unsigned char key[32]; |
| 118 | unsigned char iv[16]; |
| 119 | |
| 120 | cipher_context_t ctx_dec; |
| 121 | cipher_context_t ctx_enc; |
| 122 | const cipher_info_t *cipher_info; |
| 123 | |
| 124 | unsigned char inbuf[64]; |
| 125 | unsigned char encbuf[64]; |
| 126 | unsigned char decbuf[64]; |
| 127 | |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 128 | size_t outlen = 0; |
| 129 | size_t totaloutlen = 0; |
| 130 | size_t enclen = 0; |
Paul Bakker | 8123e9d | 2011-01-06 15:37:30 +0000 | [diff] [blame] | 131 | |
| 132 | memset( key, 0, 32 ); |
| 133 | memset( iv , 0, 16 ); |
| 134 | |
| 135 | memset( &ctx_dec, 0, sizeof( ctx_dec ) ); |
| 136 | memset( &ctx_enc, 0, sizeof( ctx_enc ) ); |
| 137 | |
| 138 | memset( inbuf, 5, 64 ); |
| 139 | memset( encbuf, 0, 64 ); |
| 140 | memset( decbuf, 0, 64 ); |
| 141 | |
| 142 | /* Initialise enc and dec contexts */ |
| 143 | cipher_info = cipher_info_from_type( {cipher_id} ); |
| 144 | TEST_ASSERT( NULL != cipher_info); |
| 145 | |
| 146 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_dec, cipher_info ) ); |
| 147 | TEST_ASSERT( 0 == cipher_init_ctx( &ctx_enc, cipher_info ) ); |
| 148 | |
| 149 | TEST_ASSERT( 0 == cipher_setkey( &ctx_dec, key, {key_len}, POLARSSL_DECRYPT ) ); |
| 150 | TEST_ASSERT( 0 == cipher_setkey( &ctx_enc, key, {key_len}, POLARSSL_ENCRYPT ) ); |
| 151 | |
| 152 | TEST_ASSERT( 0 == cipher_reset( &ctx_dec, iv ) ); |
| 153 | TEST_ASSERT( 0 == cipher_reset( &ctx_enc, iv ) ); |
| 154 | |
| 155 | enclen = cipher_get_block_size(&ctx_enc ) |
| 156 | * ( 1 + length / cipher_get_block_size( &ctx_enc ) ); |
| 157 | |
| 158 | /* encode length number of bytes from inbuf */ |
| 159 | TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) ); |
| 160 | totaloutlen = outlen; |
| 161 | TEST_ASSERT( 0 == cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) ); |
| 162 | totaloutlen += outlen; |
| 163 | TEST_ASSERT( totaloutlen == enclen - cipher_get_block_size ( &ctx_enc ) ); |
| 164 | TEST_ASSERT( 0 == cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) ); |
| 165 | totaloutlen += outlen; |
| 166 | TEST_ASSERT( outlen == cipher_get_block_size ( &ctx_enc ) ); |
| 167 | |
| 168 | /* decode the previously encoded string */ |
| 169 | TEST_ASSERT( 0 == cipher_update( &ctx_dec, encbuf, enclen, decbuf, &outlen ) ); |
| 170 | TEST_ASSERT( enclen - cipher_get_block_size ( &ctx_enc ) == outlen ); |
| 171 | TEST_ASSERT( 0 == cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) ); |
| 172 | TEST_ASSERT( length - enclen + cipher_get_block_size ( &ctx_enc ) == outlen ); |
| 173 | |
| 174 | |
| 175 | TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) ); |
| 176 | |
| 177 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_dec ) ); |
| 178 | TEST_ASSERT( 0 == cipher_free_ctx( &ctx_enc ) ); |
| 179 | END_CASE |
| 180 | |
| 181 | |
| 182 | BEGIN_CASE |
| 183 | cipher_selftest: |
| 184 | { |
| 185 | TEST_ASSERT( cipher_self_test( 0 ) == 0 ); |
| 186 | } |
| 187 | END_CASE |