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