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