blob: 8e7b12e350d50426a6e7bc43116b452a2f069db4 [file] [log] [blame]
Paul Bakker33b43f12013-08-20 11:48:36 +02001/* BEGIN_HEADER */
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00002#include "mbedtls/cipher.h"
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +02003
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02004#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00005#include "mbedtls/gcm.h"
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +02006#endif
Paul Bakker33b43f12013-08-20 11:48:36 +02007/* END_HEADER */
Paul Bakker8123e9d2011-01-06 15:37:30 +00008
Paul Bakker33b43f12013-08-20 11:48:36 +02009/* BEGIN_DEPENDENCIES
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020010 * depends_on:MBEDTLS_CIPHER_C
Paul Bakker33b43f12013-08-20 11:48:36 +020011 * END_DEPENDENCIES
12 */
Paul Bakker5690efc2011-05-26 13:16:06 +000013
Paul Bakker33b43f12013-08-20 11:48:36 +020014/* BEGIN_CASE */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020015void mbedtls_cipher_list( )
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +010016{
17 const int *cipher_type;
18
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019 for( cipher_type = mbedtls_cipher_list(); *cipher_type != 0; cipher_type++ )
20 TEST_ASSERT( mbedtls_cipher_info_from_type( *cipher_type ) != NULL );
Manuel Pégourié-Gonnard66dfc5a2014-03-29 16:10:55 +010021}
22/* END_CASE */
23
24/* BEGIN_CASE */
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020025void cipher_null_args( )
26{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020027 mbedtls_cipher_context_t ctx;
28 const mbedtls_cipher_info_t *info = mbedtls_cipher_info_from_type( *( mbedtls_cipher_list() ) );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020029 unsigned char buf[1] = { 0 };
30 size_t olen;
31
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020032 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034 TEST_ASSERT( mbedtls_cipher_get_block_size( NULL ) == 0 );
35 TEST_ASSERT( mbedtls_cipher_get_block_size( &ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020036
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037 TEST_ASSERT( mbedtls_cipher_get_cipher_mode( NULL ) == MBEDTLS_MODE_NONE );
38 TEST_ASSERT( mbedtls_cipher_get_cipher_mode( &ctx ) == MBEDTLS_MODE_NONE );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020039
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 TEST_ASSERT( mbedtls_cipher_get_iv_size( NULL ) == 0 );
41 TEST_ASSERT( mbedtls_cipher_get_iv_size( &ctx ) == 0 );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020042
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 TEST_ASSERT( mbedtls_cipher_info_from_string( NULL ) == NULL );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020044
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +020045 TEST_ASSERT( mbedtls_cipher_setup( &ctx, NULL )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020046 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +020047 TEST_ASSERT( mbedtls_cipher_setup( NULL, info )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020049
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020050 TEST_ASSERT( mbedtls_cipher_setkey( NULL, buf, 0, MBEDTLS_ENCRYPT )
51 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
52 TEST_ASSERT( mbedtls_cipher_setkey( &ctx, buf, 0, MBEDTLS_ENCRYPT )
53 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055 TEST_ASSERT( mbedtls_cipher_set_iv( NULL, buf, 0 )
56 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
57 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, buf, 0 )
58 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020059
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060 TEST_ASSERT( mbedtls_cipher_reset( NULL ) == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
61 TEST_ASSERT( mbedtls_cipher_reset( &ctx ) == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063#if defined(MBEDTLS_GCM_C)
64 TEST_ASSERT( mbedtls_cipher_update_ad( NULL, buf, 0 )
65 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
66 TEST_ASSERT( mbedtls_cipher_update_ad( &ctx, buf, 0 )
67 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020068#endif
69
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 TEST_ASSERT( mbedtls_cipher_update( NULL, buf, 0, buf, &olen )
71 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
72 TEST_ASSERT( mbedtls_cipher_update( &ctx, buf, 0, buf, &olen )
73 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 TEST_ASSERT( mbedtls_cipher_finish( NULL, buf, &olen )
76 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
77 TEST_ASSERT( mbedtls_cipher_finish( &ctx, buf, &olen )
78 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020079
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020080#if defined(MBEDTLS_GCM_C)
81 TEST_ASSERT( mbedtls_cipher_write_tag( NULL, buf, olen )
82 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
83 TEST_ASSERT( mbedtls_cipher_write_tag( &ctx, buf, olen )
84 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020085
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 TEST_ASSERT( mbedtls_cipher_check_tag( NULL, buf, olen )
87 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
88 TEST_ASSERT( mbedtls_cipher_check_tag( &ctx, buf, olen )
89 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020090#endif
91}
92/* END_CASE */
93
94/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +020095void enc_dec_buf( int cipher_id, char *cipher_string, int key_len,
96 int length_val, int pad_mode )
Paul Bakkerdbd443d2013-08-16 13:38:47 +020097{
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +020098 size_t length = length_val, outlen, total_len, i, block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +000099 unsigned char key[32];
100 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200101 unsigned char ad[13];
102 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200103 unsigned char inbuf[64];
104 unsigned char encbuf[64];
105 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000106
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 const mbedtls_cipher_info_t *cipher_info;
108 mbedtls_cipher_context_t ctx_dec;
109 mbedtls_cipher_context_t ctx_enc;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000110
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200111 /*
112 * Prepare contexts
113 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_cipher_init( &ctx_dec );
115 mbedtls_cipher_init( &ctx_enc );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200116
117 memset( key, 0x2a, sizeof( key ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000118
119 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000121 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000123
124 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200125 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
126 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
129 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000130
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200131#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Paul Bakker33b43f12013-08-20 11:48:36 +0200132 if( -1 != pad_mode )
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
135 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200136 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200137#else
138 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200140
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200141 /*
142 * Do a few encode/decode cycles
143 */
144 for( i = 0; i < 3; i++ )
145 {
146 memset( iv , 0x00 + i, sizeof( iv ) );
147 memset( ad, 0x10 + i, sizeof( ad ) );
148 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
149
150 memset( encbuf, 0, sizeof( encbuf ) );
151 memset( decbuf, 0, sizeof( decbuf ) );
152 memset( tag, 0, sizeof( tag ) );
153
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200154 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
155 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
158 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200159
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#if defined(MBEDTLS_GCM_C)
161 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
162 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200163#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000164
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200165 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
166 TEST_ASSERT( block_size != 0 );
167
Paul Bakker8123e9d2011-01-06 15:37:30 +0000168 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200170 total_len = outlen;
171
172 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200173 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200174 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200175 total_len + block_size > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200178 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#if defined(MBEDTLS_GCM_C)
181 TEST_ASSERT( 0 == mbedtls_cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200182#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200183
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200184 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200185 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200186 total_len > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200187 total_len <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000188
189 /* decode the previously encoded string */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200191 total_len = outlen;
192
193 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200194 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200195 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200196 total_len + block_size >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000197
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200199 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000200
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200201#if defined(MBEDTLS_GCM_C)
202 TEST_ASSERT( 0 == mbedtls_cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200203#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200204
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200205 /* check result */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200206 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000207 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200208 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000209
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200210 /*
211 * Done
212 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200213exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 mbedtls_cipher_free( &ctx_dec );
215 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200216}
Paul Bakker33b43f12013-08-20 11:48:36 +0200217/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000218
Paul Bakker33b43f12013-08-20 11:48:36 +0200219/* BEGIN_CASE */
220void enc_fail( int cipher_id, int pad_mode, int key_len,
221 int length_val, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200222{
Paul Bakker33b43f12013-08-20 11:48:36 +0200223 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200224 unsigned char key[32];
225 unsigned char iv[16];
226
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200227 const mbedtls_cipher_info_t *cipher_info;
228 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200229
230 unsigned char inbuf[64];
231 unsigned char encbuf[64];
232
233 size_t outlen = 0;
234
235 memset( key, 0, 32 );
236 memset( iv , 0, 16 );
237
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200238 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200239
240 memset( inbuf, 5, 64 );
241 memset( encbuf, 0, 64 );
242
243 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200245 TEST_ASSERT( NULL != cipher_info );
246
247 /* Initialise context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200248 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200249 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) );
250#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
251 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200252#else
253 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
255 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) );
256 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
257#if defined(MBEDTLS_GCM_C)
258 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200259#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200260
261 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200262 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
263 TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200264
265 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200266exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200267 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200268}
Paul Bakker33b43f12013-08-20 11:48:36 +0200269/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200270
Paul Bakker33b43f12013-08-20 11:48:36 +0200271/* BEGIN_CASE */
272void dec_empty_buf()
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200273{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000274 unsigned char key[32];
275 unsigned char iv[16];
276
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200277 mbedtls_cipher_context_t ctx_dec;
278 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000279
280 unsigned char encbuf[64];
281 unsigned char decbuf[64];
282
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000283 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000284
285 memset( key, 0, 32 );
286 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200287
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200288 mbedtls_cipher_init( &ctx_dec );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200289
Paul Bakker8123e9d2011-01-06 15:37:30 +0000290 memset( encbuf, 0, 64 );
291 memset( decbuf, 0, 64 );
292
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200293 /* Initialise context */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200294 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000295 TEST_ASSERT( NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200296
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200297 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000298
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, 128, MBEDTLS_DECRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000300
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200301 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200302
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200304
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305#if defined(MBEDTLS_GCM_C)
306 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200307#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000308
309 /* decode 0-byte string */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200310 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000311 TEST_ASSERT( 0 == outlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200312 TEST_ASSERT( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED == mbedtls_cipher_finish(
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200313 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000314 TEST_ASSERT( 0 == outlen );
315
Paul Bakkerbd51b262014-07-10 15:26:12 +0200316exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200317 mbedtls_cipher_free( &ctx_dec );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200318}
Paul Bakker33b43f12013-08-20 11:48:36 +0200319/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000320
Paul Bakker33b43f12013-08-20 11:48:36 +0200321/* BEGIN_CASE */
322void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
Andrzej Kureka1149a72018-03-30 05:00:19 -0400323 int second_length_val, int pad_mode,
324 int first_encrypt_output_len, int second_encrypt_output_len,
325 int first_decrypt_output_len, int second_decrypt_output_len )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200326{
Paul Bakker33b43f12013-08-20 11:48:36 +0200327 size_t first_length = first_length_val;
328 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000329 size_t length = first_length + second_length;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200330 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000331 unsigned char key[32];
332 unsigned char iv[16];
333
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200334 mbedtls_cipher_context_t ctx_dec;
335 mbedtls_cipher_context_t ctx_enc;
336 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000337
338 unsigned char inbuf[64];
339 unsigned char encbuf[64];
340 unsigned char decbuf[64];
341
Paul Bakker23986e52011-04-24 08:57:21 +0000342 size_t outlen = 0;
343 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000344
345 memset( key, 0, 32 );
346 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 mbedtls_cipher_init( &ctx_dec );
349 mbedtls_cipher_init( &ctx_enc );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200350
Paul Bakker8123e9d2011-01-06 15:37:30 +0000351 memset( inbuf, 5, 64 );
352 memset( encbuf, 0, 64 );
353 memset( decbuf, 0, 64 );
354
355 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200356 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000357 TEST_ASSERT( NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200358
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200359 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
360 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200361
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200362 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
363 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000364
Andrzej Kureka1149a72018-03-30 05:00:19 -0400365#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
366 if( -1 != pad_mode )
367 {
368 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
369 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
370 }
371#else
372 (void) pad_mode;
373#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
374
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200375 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
376 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, 16 ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200377
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200378 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
379 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200380
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200381#if defined(MBEDTLS_GCM_C)
382 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
383 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200384#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000385
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200386 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
387 TEST_ASSERT( block_size != 0 );
388
Paul Bakker8123e9d2011-01-06 15:37:30 +0000389 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200390 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
Andrzej Kureka1149a72018-03-30 05:00:19 -0400391 TEST_ASSERT( (size_t)first_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000392 totaloutlen = outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200393 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
Andrzej Kureka1149a72018-03-30 05:00:19 -0400394 TEST_ASSERT( (size_t)second_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000395 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200396 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200397 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200398 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200399 totaloutlen + block_size > length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200400
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200401 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000402 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200403 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200404 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200405 totaloutlen > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200406 totaloutlen <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000407
408 /* decode the previously encoded string */
Andrzej Kureka1149a72018-03-30 05:00:19 -0400409 second_length = totaloutlen - first_length;
410 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, first_length, decbuf, &outlen ) );
411 TEST_ASSERT( (size_t)first_decrypt_output_len == outlen );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200412 totaloutlen = outlen;
Andrzej Kureka1149a72018-03-30 05:00:19 -0400413 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf + first_length, second_length, decbuf + totaloutlen, &outlen ) );
414 TEST_ASSERT( (size_t)second_decrypt_output_len == outlen );
415 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200416
417 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200418 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200419 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200420 totaloutlen + block_size >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200421
Andrzej Kureka1149a72018-03-30 05:00:19 -0400422 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + totaloutlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200423 totaloutlen += outlen;
424
425 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000426
427 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
428
Paul Bakkerbd51b262014-07-10 15:26:12 +0200429exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200430 mbedtls_cipher_free( &ctx_dec );
431 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200432}
Paul Bakker33b43f12013-08-20 11:48:36 +0200433/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000434
Paul Bakker33b43f12013-08-20 11:48:36 +0200435/* BEGIN_CASE */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200436void decrypt_test_vec( int cipher_id, int pad_mode,
437 char *hex_key, char *hex_iv,
438 char *hex_cipher, char *hex_clear,
439 char *hex_ad, char *hex_tag,
440 int finish_result, int tag_result )
441{
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +0200442 unsigned char key[50];
443 unsigned char iv[50];
444 unsigned char cipher[200];
445 unsigned char clear[200];
446 unsigned char ad[200];
447 unsigned char tag[20];
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200448 size_t key_len, iv_len, cipher_len, clear_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200449#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200450 size_t ad_len, tag_len;
451#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200452 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +0200453 unsigned char output[200];
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200454 size_t outlen, total_len;
455
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200456 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200457
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200458 memset( key, 0x00, sizeof( key ) );
459 memset( iv, 0x00, sizeof( iv ) );
460 memset( cipher, 0x00, sizeof( cipher ) );
461 memset( clear, 0x00, sizeof( clear ) );
462 memset( ad, 0x00, sizeof( ad ) );
463 memset( tag, 0x00, sizeof( tag ) );
464 memset( output, 0x00, sizeof( output ) );
465
466 key_len = unhexify( key, hex_key );
467 iv_len = unhexify( iv, hex_iv );
468 cipher_len = unhexify( cipher, hex_cipher );
469 clear_len = unhexify( clear, hex_clear );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200470#if defined(MBEDTLS_GCM_C)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200471 ad_len = unhexify( ad, hex_ad );
472 tag_len = unhexify( tag, hex_tag );
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200473#else
474 ((void) hex_ad);
475 ((void) hex_tag);
476#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200477
478 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200479 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200480 mbedtls_cipher_info_from_type( cipher_id ) ) );
481 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, MBEDTLS_DECRYPT ) );
482#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200483 if( pad_mode != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200484 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200485#else
486 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200487#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
488 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, iv_len ) );
489 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
490#if defined(MBEDTLS_GCM_C)
491 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad, ad_len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200492#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200493
494 /* decode buffer and check tag */
495 total_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200496 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200497 total_len += outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200498 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200499 &outlen ) );
500 total_len += outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200501#if defined(MBEDTLS_GCM_C)
502 TEST_ASSERT( tag_result == mbedtls_cipher_check_tag( &ctx, tag, tag_len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200503#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200504
505 /* check plaintext only if everything went fine */
506 if( 0 == finish_result && 0 == tag_result )
507 {
508 TEST_ASSERT( total_len == clear_len );
509 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
510 }
511
Paul Bakkerbd51b262014-07-10 15:26:12 +0200512exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200513 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200514}
515/* END_CASE */
516
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200517/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200518void auth_crypt_tv( int cipher_id, char *hex_key, char *hex_iv,
519 char *hex_ad, char *hex_cipher,
520 char *hex_tag, char *hex_clear )
521{
522 int ret;
523 unsigned char key[50];
524 unsigned char iv[50];
525 unsigned char cipher[200];
526 unsigned char clear[200];
527 unsigned char ad[200];
528 unsigned char tag[20];
529 unsigned char my_tag[20];
530 size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200531 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200532 unsigned char output[200];
533 size_t outlen;
534
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200535 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200536
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200537 memset( key, 0x00, sizeof( key ) );
538 memset( iv, 0x00, sizeof( iv ) );
539 memset( cipher, 0x00, sizeof( cipher ) );
540 memset( clear, 0x00, sizeof( clear ) );
541 memset( ad, 0x00, sizeof( ad ) );
542 memset( tag, 0x00, sizeof( tag ) );
543 memset( my_tag, 0xFF, sizeof( my_tag ) );
544 memset( output, 0xFF, sizeof( output ) );
545
546 key_len = unhexify( key, hex_key );
547 iv_len = unhexify( iv, hex_iv );
548 cipher_len = unhexify( cipher, hex_cipher );
549 ad_len = unhexify( ad, hex_ad );
550 tag_len = unhexify( tag, hex_tag );
551
552 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200553 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200554 mbedtls_cipher_info_from_type( cipher_id ) ) );
555 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, MBEDTLS_DECRYPT ) );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200556
557 /* decode buffer and check tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200558 ret = mbedtls_cipher_auth_decrypt( &ctx, iv, iv_len, ad, ad_len,
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200559 cipher, cipher_len, output, &outlen,
560 tag, tag_len );
561
562 /* make sure we didn't overwrite */
563 TEST_ASSERT( output[outlen + 0] == 0xFF );
564 TEST_ASSERT( output[outlen + 1] == 0xFF );
565
566 /* make sure the message is rejected if it should be */
567 if( strcmp( hex_clear, "FAIL" ) == 0 )
568 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200569 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200570 goto exit;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200571 }
572
573 /* otherwise, make sure it was decrypted properly */
574 TEST_ASSERT( ret == 0 );
575
576 clear_len = unhexify( clear, hex_clear );
577 TEST_ASSERT( outlen == clear_len );
578 TEST_ASSERT( memcmp( output, clear, clear_len ) == 0 );
579
580 /* then encrypt the clear and make sure we get the same ciphertext and tag */
581 memset( output, 0xFF, sizeof( output ) );
582 outlen = 0;
583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 ret = mbedtls_cipher_auth_encrypt( &ctx, iv, iv_len, ad, ad_len,
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200585 clear, clear_len, output, &outlen,
586 my_tag, tag_len );
587 TEST_ASSERT( ret == 0 );
588
589 TEST_ASSERT( outlen == clear_len );
590 TEST_ASSERT( memcmp( output, cipher, clear_len ) == 0 );
591 TEST_ASSERT( memcmp( my_tag, tag, tag_len ) == 0 );
592
593 /* make sure we didn't overwrite */
594 TEST_ASSERT( output[outlen + 0] == 0xFF );
595 TEST_ASSERT( output[outlen + 1] == 0xFF );
596 TEST_ASSERT( my_tag[tag_len + 0] == 0xFF );
597 TEST_ASSERT( my_tag[tag_len + 1] == 0xFF );
598
599
Paul Bakkerbd51b262014-07-10 15:26:12 +0200600exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200601 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200602}
603/* END_CASE */
604
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200605/* BEGIN_CASE */
Paul Bakker5e0efa72013-09-08 23:04:04 +0200606void test_vec_ecb( int cipher_id, int operation, char *hex_key,
607 char *hex_input, char *hex_result,
608 int finish_result )
609{
610 unsigned char key[50];
611 unsigned char input[16];
612 unsigned char result[16];
613 size_t key_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200614 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200615 unsigned char output[32];
616 size_t outlen;
617
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200619
Paul Bakker5e0efa72013-09-08 23:04:04 +0200620 memset( key, 0x00, sizeof( key ) );
621 memset( input, 0x00, sizeof( input ) );
622 memset( result, 0x00, sizeof( result ) );
623 memset( output, 0x00, sizeof( output ) );
624
625 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200626 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200627 mbedtls_cipher_info_from_type( cipher_id ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200628
629 key_len = unhexify( key, hex_key );
630 TEST_ASSERT( unhexify( input, hex_input ) ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200631 (int) mbedtls_cipher_get_block_size( &ctx ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200632 TEST_ASSERT( unhexify( result, hex_result ) ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 (int) mbedtls_cipher_get_block_size( &ctx ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200634
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200635 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, operation ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200636
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200637 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input,
638 mbedtls_cipher_get_block_size( &ctx ),
Paul Bakker5e0efa72013-09-08 23:04:04 +0200639 output, &outlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200640 TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) );
641 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200642 &outlen ) );
643 TEST_ASSERT( 0 == outlen );
644
645 /* check plaintext only if everything went fine */
646 if( 0 == finish_result )
647 TEST_ASSERT( 0 == memcmp( output, result,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200648 mbedtls_cipher_get_block_size( &ctx ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200649
Paul Bakkerbd51b262014-07-10 15:26:12 +0200650exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200651 mbedtls_cipher_free( &ctx );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200652}
653/* END_CASE */
654
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200655/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Paul Bakker33b43f12013-08-20 11:48:36 +0200656void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200657{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200658 const mbedtls_cipher_info_t *cipher_info;
659 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200660
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200661 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200662
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200664 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200665 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 TEST_ASSERT( ret == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200668
Paul Bakkerbd51b262014-07-10 15:26:12 +0200669exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200670 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200671}
Paul Bakker33b43f12013-08-20 11:48:36 +0200672/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000673
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200674/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200675void check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200676{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200677 mbedtls_cipher_info_t cipher_info;
678 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200679 unsigned char input[16];
680 size_t ilen, dlen;
681
682 /* build a fake context just for getting access to get_padding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200683 mbedtls_cipher_init( &ctx );
684 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200685 ctx.cipher_info = &cipher_info;
686
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200687 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200688
Paul Bakker33b43f12013-08-20 11:48:36 +0200689 ilen = unhexify( input, input_str );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200690
Paul Bakker33b43f12013-08-20 11:48:36 +0200691 TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
692 if( 0 == ret )
693 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200694}
Paul Bakker33b43f12013-08-20 11:48:36 +0200695/* END_CASE */