blob: 1536c9d315b0b2dcf087a02e70b5666d9fe8cc2b [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é-Gonnarddca3a5d2018-05-07 10:43:27 +020063#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 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é-Gonnarddca3a5d2018-05-07 10:43:27 +020080#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 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
Paul Bakker6a9c7252016-07-14 13:46:10 +010094/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
95void cipher_special_behaviours( )
96{
97 const mbedtls_cipher_info_t *cipher_info;
98 mbedtls_cipher_context_t ctx;
99 unsigned char input[32];
100 unsigned char output[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300101#if defined (MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100102 unsigned char iv[32];
Ron Eldor6f90ed82017-09-26 12:08:54 +0300103#endif
Paul Bakker6a9c7252016-07-14 13:46:10 +0100104 size_t olen = 0;
105
106 mbedtls_cipher_init( &ctx );
107 memset( input, 0, sizeof( input ) );
108 memset( output, 0, sizeof( output ) );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300109#if defined(MBEDTLS_CIPHER_MODE_CBC)
Paul Bakker6a9c7252016-07-14 13:46:10 +0100110 memset( iv, 0, sizeof( iv ) );
111
112 /* Check and get info structures */
Ron Eldor7b012442017-09-25 17:03:12 +0300113 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
Paul Bakker6a9c7252016-07-14 13:46:10 +0100114 TEST_ASSERT( NULL != cipher_info );
115
116 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
117
118 /* IV too big */
119 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, MBEDTLS_MAX_IV_LENGTH + 1 )
120 == MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE );
121
122 /* IV too small */
123 TEST_ASSERT( mbedtls_cipher_set_iv( &ctx, iv, 0 )
124 == MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
125
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300126 mbedtls_cipher_free( &ctx );
Ron Eldorbb4bbbb2017-10-01 17:04:54 +0300127 mbedtls_cipher_init( &ctx );
Ron Eldor6f90ed82017-09-26 12:08:54 +0300128#endif /* MBEDTLS_CIPHER_MODE_CBC */
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300129 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_ECB );
Ron Eldor7b012442017-09-25 17:03:12 +0300130 TEST_ASSERT( NULL != cipher_info );
131
132 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
133
Paul Bakker6a9c7252016-07-14 13:46:10 +0100134 /* Update ECB with partial block */
135 TEST_ASSERT( mbedtls_cipher_update( &ctx, input, 1, output, &olen )
136 == MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED );
137
138exit:
139 mbedtls_cipher_free( &ctx );
140}
141/* END_CASE */
142
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +0200143/* BEGIN_CASE */
Paul Bakker33b43f12013-08-20 11:48:36 +0200144void enc_dec_buf( int cipher_id, char *cipher_string, int key_len,
145 int length_val, int pad_mode )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200146{
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200147 size_t length = length_val, outlen, total_len, i, block_size;
Jaeden Amerod906b812018-06-08 11:03:16 +0100148 unsigned char key[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000149 unsigned char iv[16];
Manuel Pégourié-Gonnard9241be72013-08-31 17:31:03 +0200150 unsigned char ad[13];
151 unsigned char tag[16];
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200152 unsigned char inbuf[64];
153 unsigned char encbuf[64];
154 unsigned char decbuf[64];
Paul Bakker8123e9d2011-01-06 15:37:30 +0000155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 const mbedtls_cipher_info_t *cipher_info;
157 mbedtls_cipher_context_t ctx_dec;
158 mbedtls_cipher_context_t ctx_enc;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000159
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200160 /*
161 * Prepare contexts
162 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_cipher_init( &ctx_dec );
164 mbedtls_cipher_init( &ctx_enc );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200165
166 memset( key, 0x2a, sizeof( key ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000167
168 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000170 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 TEST_ASSERT( mbedtls_cipher_info_from_string( cipher_string ) == cipher_info );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000172
173 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200174 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
175 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200176
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
178 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Paul Bakker33b43f12013-08-20 11:48:36 +0200181 if( -1 != pad_mode )
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
184 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200185 }
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200186#else
187 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200188#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Manuel Pégourié-Gonnard6c978992013-07-26 13:20:42 +0200189
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200190 /*
191 * Do a few encode/decode cycles
192 */
193 for( i = 0; i < 3; i++ )
194 {
195 memset( iv , 0x00 + i, sizeof( iv ) );
196 memset( ad, 0x10 + i, sizeof( ad ) );
197 memset( inbuf, 0x20 + i, sizeof( inbuf ) );
198
199 memset( encbuf, 0, sizeof( encbuf ) );
200 memset( decbuf, 0, sizeof( decbuf ) );
201 memset( tag, 0, sizeof( tag ) );
202
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200203 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, sizeof( iv ) ) );
204 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, sizeof( iv ) ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200205
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200206 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
207 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200208
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200209#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, ad, sizeof( ad ) - i ) );
211 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, ad, sizeof( ad ) - i ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200212#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000213
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200214 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
215 TEST_ASSERT( block_size != 0 );
216
Paul Bakker8123e9d2011-01-06 15:37:30 +0000217 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, length, encbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200219 total_len = outlen;
220
221 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200222 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200223 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200224 total_len + block_size > length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000225
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200226 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200227 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000228
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200229#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200230 TEST_ASSERT( 0 == mbedtls_cipher_write_tag( &ctx_enc, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200231#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200232
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200233 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200234 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200235 total_len > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200236 total_len <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000237
238 /* decode the previously encoded string */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, total_len, decbuf, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200240 total_len = outlen;
241
242 TEST_ASSERT( total_len == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200243 ( total_len % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200244 total_len < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200245 total_len + block_size >= length ) );
Paul Bakker343a8702011-06-09 14:27:58 +0000246
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200247 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200248 total_len += outlen;
Paul Bakker343a8702011-06-09 14:27:58 +0000249
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200250#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200251 TEST_ASSERT( 0 == mbedtls_cipher_check_tag( &ctx_dec, tag, sizeof( tag ) ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200252#endif
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200253
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200254 /* check result */
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200255 TEST_ASSERT( total_len == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000256 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200257 }
Paul Bakker8123e9d2011-01-06 15:37:30 +0000258
Manuel Pégourié-Gonnard1af50a22013-09-05 10:30:32 +0200259 /*
260 * Done
261 */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200262exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200263 mbedtls_cipher_free( &ctx_dec );
264 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200265}
Paul Bakker33b43f12013-08-20 11:48:36 +0200266/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000267
Paul Bakker33b43f12013-08-20 11:48:36 +0200268/* BEGIN_CASE */
269void enc_fail( int cipher_id, int pad_mode, int key_len,
270 int length_val, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200271{
Paul Bakker33b43f12013-08-20 11:48:36 +0200272 size_t length = length_val;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200273 unsigned char key[32];
274 unsigned char iv[16];
275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 const mbedtls_cipher_info_t *cipher_info;
277 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200278
279 unsigned char inbuf[64];
280 unsigned char encbuf[64];
281
282 size_t outlen = 0;
283
284 memset( key, 0, 32 );
285 memset( iv , 0, 16 );
286
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200287 mbedtls_cipher_init( &ctx );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200288
289 memset( inbuf, 5, 64 );
290 memset( encbuf, 0, 64 );
291
292 /* Check and get info structures */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200294 TEST_ASSERT( NULL != cipher_info );
295
296 /* Initialise context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200297 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200298 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, key_len, MBEDTLS_ENCRYPT ) );
299#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
300 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200301#else
302 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200303#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
304 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, 16 ) );
305 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200306#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200308#endif
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200309
310 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200311 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, inbuf, length, encbuf, &outlen ) );
312 TEST_ASSERT( ret == mbedtls_cipher_finish( &ctx, encbuf + outlen, &outlen ) );
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200313
314 /* done */
Paul Bakkerbd51b262014-07-10 15:26:12 +0200315exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200316 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200317}
Paul Bakker33b43f12013-08-20 11:48:36 +0200318/* END_CASE */
Manuel Pégourié-Gonnardebdc4132013-07-26 16:50:44 +0200319
Paul Bakker33b43f12013-08-20 11:48:36 +0200320/* BEGIN_CASE */
321void dec_empty_buf()
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200322{
Paul Bakker8123e9d2011-01-06 15:37:30 +0000323 unsigned char key[32];
324 unsigned char iv[16];
325
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200326 mbedtls_cipher_context_t ctx_dec;
327 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000328
329 unsigned char encbuf[64];
330 unsigned char decbuf[64];
331
Paul Bakkerf4a3f302011-04-24 15:53:29 +0000332 size_t outlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000333
334 memset( key, 0, 32 );
335 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200336
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200337 mbedtls_cipher_init( &ctx_dec );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200338
Paul Bakker8123e9d2011-01-06 15:37:30 +0000339 memset( encbuf, 0, 64 );
340 memset( decbuf, 0, 64 );
341
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200342 /* Initialise context */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200343 cipher_info = mbedtls_cipher_info_from_type( MBEDTLS_CIPHER_AES_128_CBC );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000344 TEST_ASSERT( NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200345
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200346 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000347
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200348 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, 128, MBEDTLS_DECRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000349
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200350 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200351
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200352 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200353
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200354#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200355 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200356#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000357
358 /* decode 0-byte string */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200359 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, 0, decbuf, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000360 TEST_ASSERT( 0 == outlen );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200361 TEST_ASSERT( MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED == mbedtls_cipher_finish(
Manuel Pégourié-Gonnardaa9ffc52013-09-03 16:19:22 +0200362 &ctx_dec, decbuf + outlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000363 TEST_ASSERT( 0 == outlen );
364
Paul Bakkerbd51b262014-07-10 15:26:12 +0200365exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200366 mbedtls_cipher_free( &ctx_dec );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200367}
Paul Bakker33b43f12013-08-20 11:48:36 +0200368/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000369
Paul Bakker33b43f12013-08-20 11:48:36 +0200370/* BEGIN_CASE */
371void enc_dec_buf_multipart( int cipher_id, int key_len, int first_length_val,
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700372 int second_length_val, int pad_mode,
373 int first_encrypt_output_len, int second_encrypt_output_len,
374 int first_decrypt_output_len, int second_decrypt_output_len )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200375{
Paul Bakker33b43f12013-08-20 11:48:36 +0200376 size_t first_length = first_length_val;
377 size_t second_length = second_length_val;
Paul Bakker23986e52011-04-24 08:57:21 +0000378 size_t length = first_length + second_length;
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200379 size_t block_size;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000380 unsigned char key[32];
381 unsigned char iv[16];
382
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200383 mbedtls_cipher_context_t ctx_dec;
384 mbedtls_cipher_context_t ctx_enc;
385 const mbedtls_cipher_info_t *cipher_info;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000386
387 unsigned char inbuf[64];
388 unsigned char encbuf[64];
389 unsigned char decbuf[64];
390
Paul Bakker23986e52011-04-24 08:57:21 +0000391 size_t outlen = 0;
392 size_t totaloutlen = 0;
Paul Bakker8123e9d2011-01-06 15:37:30 +0000393
394 memset( key, 0, 32 );
395 memset( iv , 0, 16 );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200396
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200397 mbedtls_cipher_init( &ctx_dec );
398 mbedtls_cipher_init( &ctx_enc );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200399
Paul Bakker8123e9d2011-01-06 15:37:30 +0000400 memset( inbuf, 5, 64 );
401 memset( encbuf, 0, 64 );
402 memset( decbuf, 0, 64 );
403
404 /* Initialise enc and dec contexts */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200405 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000406 TEST_ASSERT( NULL != cipher_info);
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200407
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200408 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_dec, cipher_info ) );
409 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx_enc, cipher_info ) );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200410
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200411 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_dec, key, key_len, MBEDTLS_DECRYPT ) );
412 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx_enc, key, key_len, MBEDTLS_ENCRYPT ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000413
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700414#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
415 if( -1 != pad_mode )
416 {
417 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_dec, pad_mode ) );
418 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx_enc, pad_mode ) );
419 }
420#else
421 (void) pad_mode;
422#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
423
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200424 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_dec, iv, 16 ) );
425 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx_enc, iv, 16 ) );
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200426
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200427 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_dec ) );
428 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx_enc ) );
Manuel Pégourié-Gonnard2adc40c2013-09-03 13:54:12 +0200429
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200430#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200431 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_dec, NULL, 0 ) );
432 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx_enc, NULL, 0 ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200433#endif
Paul Bakker8123e9d2011-01-06 15:37:30 +0000434
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200435 block_size = mbedtls_cipher_get_block_size( &ctx_enc );
436 TEST_ASSERT( block_size != 0 );
437
Paul Bakker8123e9d2011-01-06 15:37:30 +0000438 /* encode length number of bytes from inbuf */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200439 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf, first_length, encbuf, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700440 TEST_ASSERT( (size_t)first_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000441 totaloutlen = outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200442 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_enc, inbuf + first_length, second_length, encbuf + totaloutlen, &outlen ) );
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700443 TEST_ASSERT( (size_t)second_encrypt_output_len == outlen );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000444 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200445 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200446 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200447 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200448 totaloutlen + block_size > length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200449
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200450 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_enc, encbuf + totaloutlen, &outlen ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000451 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200452 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200453 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200454 totaloutlen > length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200455 totaloutlen <= length + block_size ) );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000456
457 /* decode the previously encoded string */
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700458 second_length = totaloutlen - first_length;
459 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf, first_length, decbuf, &outlen ) );
460 TEST_ASSERT( (size_t)first_decrypt_output_len == outlen );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200461 totaloutlen = outlen;
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700462 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx_dec, encbuf + first_length, second_length, decbuf + totaloutlen, &outlen ) );
463 TEST_ASSERT( (size_t)second_decrypt_output_len == outlen );
464 totaloutlen += outlen;
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200465
466 TEST_ASSERT( totaloutlen == length ||
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200467 ( totaloutlen % block_size == 0 &&
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200468 totaloutlen < length &&
Manuel Pégourié-Gonnardac5361f2015-06-24 01:08:09 +0200469 totaloutlen + block_size >= length ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200470
Jethro Beekman6c563fa2018-03-27 19:16:17 -0700471 TEST_ASSERT( 0 == mbedtls_cipher_finish( &ctx_dec, decbuf + totaloutlen, &outlen ) );
Manuel Pégourié-Gonnard725680f2013-07-25 15:26:54 +0200472 totaloutlen += outlen;
473
474 TEST_ASSERT( totaloutlen == length );
Paul Bakker8123e9d2011-01-06 15:37:30 +0000475
476 TEST_ASSERT( 0 == memcmp(inbuf, decbuf, length) );
477
Paul Bakkerbd51b262014-07-10 15:26:12 +0200478exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200479 mbedtls_cipher_free( &ctx_dec );
480 mbedtls_cipher_free( &ctx_enc );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200481}
Paul Bakker33b43f12013-08-20 11:48:36 +0200482/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000483
Paul Bakker33b43f12013-08-20 11:48:36 +0200484/* BEGIN_CASE */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200485void decrypt_test_vec( int cipher_id, int pad_mode,
486 char *hex_key, char *hex_iv,
487 char *hex_cipher, char *hex_clear,
488 char *hex_ad, char *hex_tag,
489 int finish_result, int tag_result )
490{
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +0200491 unsigned char key[50];
492 unsigned char iv[50];
Manuel Pégourié-Gonnard234e1ce2018-05-10 12:54:32 +0200493 unsigned char cipher[265]; /* max length of test data so far */
494 unsigned char clear[265];
495 unsigned char output[265];
Manuel Pégourié-Gonnardf7ce67f2013-09-03 20:17:35 +0200496 unsigned char ad[200];
497 unsigned char tag[20];
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200498 size_t key_len, iv_len, cipher_len, clear_len;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200499#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200500 size_t ad_len, tag_len;
501#endif
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200502 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200503 size_t outlen, total_len;
504
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200506
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200507 memset( key, 0x00, sizeof( key ) );
508 memset( iv, 0x00, sizeof( iv ) );
509 memset( cipher, 0x00, sizeof( cipher ) );
510 memset( clear, 0x00, sizeof( clear ) );
511 memset( ad, 0x00, sizeof( ad ) );
512 memset( tag, 0x00, sizeof( tag ) );
513 memset( output, 0x00, sizeof( output ) );
514
515 key_len = unhexify( key, hex_key );
516 iv_len = unhexify( iv, hex_iv );
517 cipher_len = unhexify( cipher, hex_cipher );
518 clear_len = unhexify( clear, hex_clear );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200519#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200520 ad_len = unhexify( ad, hex_ad );
521 tag_len = unhexify( tag, hex_tag );
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200522#else
523 ((void) hex_ad);
524 ((void) hex_tag);
525#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200526
527 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200528 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200529 mbedtls_cipher_info_from_type( cipher_id ) ) );
530 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, MBEDTLS_DECRYPT ) );
531#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200532 if( pad_mode != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200533 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200534#else
535 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200536#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
537 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv, iv_len ) );
538 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200539#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200540 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad, ad_len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200541#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200542
543 /* decode buffer and check tag */
544 total_len = 0;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200545 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher, cipher_len, output, &outlen ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200546 total_len += outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200547 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200548 &outlen ) );
549 total_len += outlen;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200550#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200551 TEST_ASSERT( tag_result == mbedtls_cipher_check_tag( &ctx, tag, tag_len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200552#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200553
554 /* check plaintext only if everything went fine */
555 if( 0 == finish_result && 0 == tag_result )
556 {
557 TEST_ASSERT( total_len == clear_len );
558 TEST_ASSERT( 0 == memcmp( output, clear, clear_len ) );
559 }
560
Paul Bakkerbd51b262014-07-10 15:26:12 +0200561exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200562 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200563}
564/* END_CASE */
565
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200566/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_AEAD */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200567void auth_crypt_tv( int cipher_id, char *hex_key, char *hex_iv,
568 char *hex_ad, char *hex_cipher,
569 char *hex_tag, char *hex_clear )
570{
571 int ret;
572 unsigned char key[50];
573 unsigned char iv[50];
Manuel Pégourié-Gonnard69767d12018-05-09 12:25:18 +0200574 unsigned char cipher[265]; /* max size of test data so far */
575 unsigned char clear[265];
576 unsigned char output[267]; /* above + 2 (overwrite check) */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200577 unsigned char ad[200];
578 unsigned char tag[20];
579 unsigned char my_tag[20];
580 size_t key_len, iv_len, cipher_len, clear_len, ad_len, tag_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200581 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200582 size_t outlen;
583
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200585
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200586 memset( key, 0x00, sizeof( key ) );
587 memset( iv, 0x00, sizeof( iv ) );
588 memset( cipher, 0x00, sizeof( cipher ) );
589 memset( clear, 0x00, sizeof( clear ) );
590 memset( ad, 0x00, sizeof( ad ) );
591 memset( tag, 0x00, sizeof( tag ) );
592 memset( my_tag, 0xFF, sizeof( my_tag ) );
593 memset( output, 0xFF, sizeof( output ) );
594
595 key_len = unhexify( key, hex_key );
596 iv_len = unhexify( iv, hex_iv );
597 cipher_len = unhexify( cipher, hex_cipher );
598 ad_len = unhexify( ad, hex_ad );
599 tag_len = unhexify( tag, hex_tag );
600
601 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200602 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200603 mbedtls_cipher_info_from_type( cipher_id ) ) );
604 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, MBEDTLS_DECRYPT ) );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200605
606 /* decode buffer and check tag */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 ret = mbedtls_cipher_auth_decrypt( &ctx, iv, iv_len, ad, ad_len,
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200608 cipher, cipher_len, output, &outlen,
609 tag, tag_len );
610
611 /* make sure we didn't overwrite */
612 TEST_ASSERT( output[outlen + 0] == 0xFF );
613 TEST_ASSERT( output[outlen + 1] == 0xFF );
614
615 /* make sure the message is rejected if it should be */
616 if( strcmp( hex_clear, "FAIL" ) == 0 )
617 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200618 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200619 goto exit;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200620 }
621
622 /* otherwise, make sure it was decrypted properly */
623 TEST_ASSERT( ret == 0 );
624
625 clear_len = unhexify( clear, hex_clear );
626 TEST_ASSERT( outlen == clear_len );
627 TEST_ASSERT( memcmp( output, clear, clear_len ) == 0 );
628
629 /* then encrypt the clear and make sure we get the same ciphertext and tag */
630 memset( output, 0xFF, sizeof( output ) );
631 outlen = 0;
632
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200633 ret = mbedtls_cipher_auth_encrypt( &ctx, iv, iv_len, ad, ad_len,
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200634 clear, clear_len, output, &outlen,
635 my_tag, tag_len );
636 TEST_ASSERT( ret == 0 );
637
638 TEST_ASSERT( outlen == clear_len );
639 TEST_ASSERT( memcmp( output, cipher, clear_len ) == 0 );
640 TEST_ASSERT( memcmp( my_tag, tag, tag_len ) == 0 );
641
642 /* make sure we didn't overwrite */
643 TEST_ASSERT( output[outlen + 0] == 0xFF );
644 TEST_ASSERT( output[outlen + 1] == 0xFF );
645 TEST_ASSERT( my_tag[tag_len + 0] == 0xFF );
646 TEST_ASSERT( my_tag[tag_len + 1] == 0xFF );
647
648
Paul Bakkerbd51b262014-07-10 15:26:12 +0200649exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200650 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200651}
652/* END_CASE */
653
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200654/* BEGIN_CASE */
Paul Bakker5e0efa72013-09-08 23:04:04 +0200655void test_vec_ecb( int cipher_id, int operation, char *hex_key,
656 char *hex_input, char *hex_result,
657 int finish_result )
658{
659 unsigned char key[50];
660 unsigned char input[16];
661 unsigned char result[16];
662 size_t key_len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200663 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200664 unsigned char output[32];
665 size_t outlen;
666
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200667 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200668
Paul Bakker5e0efa72013-09-08 23:04:04 +0200669 memset( key, 0x00, sizeof( key ) );
670 memset( input, 0x00, sizeof( input ) );
671 memset( result, 0x00, sizeof( result ) );
672 memset( output, 0x00, sizeof( output ) );
673
674 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200675 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200676 mbedtls_cipher_info_from_type( cipher_id ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200677
678 key_len = unhexify( key, hex_key );
679 TEST_ASSERT( unhexify( input, hex_input ) ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200680 (int) mbedtls_cipher_get_block_size( &ctx ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200681 TEST_ASSERT( unhexify( result, hex_result ) ==
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200682 (int) mbedtls_cipher_get_block_size( &ctx ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200683
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200684 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, operation ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200685
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200686 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input,
687 mbedtls_cipher_get_block_size( &ctx ),
Paul Bakker5e0efa72013-09-08 23:04:04 +0200688 output, &outlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200689 TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) );
690 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200691 &outlen ) );
692 TEST_ASSERT( 0 == outlen );
693
694 /* check plaintext only if everything went fine */
695 if( 0 == finish_result )
696 TEST_ASSERT( 0 == memcmp( output, result,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200697 mbedtls_cipher_get_block_size( &ctx ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200698
Paul Bakkerbd51b262014-07-10 15:26:12 +0200699exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200700 mbedtls_cipher_free( &ctx );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200701}
702/* END_CASE */
703
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200704/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Ron Eldor7b012442017-09-25 17:03:12 +0300705void test_vec_crypt( int cipher_id, int operation, char *hex_key,
706 char *hex_iv, char *hex_input, char *hex_result,
707 int finish_result )
708{
709 unsigned char key[50];
710 unsigned char input[16];
711 unsigned char result[16];
712 unsigned char iv[16];
713 size_t key_len, iv_len, inputlen, resultlen;
714 mbedtls_cipher_context_t ctx;
715 unsigned char output[32];
716 size_t outlen;
717
718 mbedtls_cipher_init( &ctx );
719
720 memset( key, 0x00, sizeof( key ) );
721 memset( input, 0x00, sizeof( input ) );
722 memset( result, 0x00, sizeof( result ) );
723 memset( output, 0x00, sizeof( output ) );
724 memset( iv, 0x00, sizeof( iv ) );
725
726 /* Prepare context */
727 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
728 mbedtls_cipher_info_from_type( cipher_id ) ) );
729
730 key_len = unhexify( key, hex_key );
731 inputlen = unhexify( input, hex_input );
732 resultlen = unhexify( result, hex_result );
733
734 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, operation ) );
735 if( MBEDTLS_MODE_CBC == ctx.cipher_info->mode )
736 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, MBEDTLS_PADDING_NONE ) );
737
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300738 iv_len = unhexify( iv, hex_iv );
Ron Eldor7b012442017-09-25 17:03:12 +0300739
740 TEST_ASSERT( finish_result == mbedtls_cipher_crypt( &ctx, iv_len ? iv : NULL,
741 iv_len, input, inputlen,
742 output, &outlen ) );
743 TEST_ASSERT( resultlen == outlen );
744 /* check plaintext only if everything went fine */
745 if( 0 == finish_result )
746 TEST_ASSERT( 0 == memcmp( output, result, outlen ) );
747
748exit:
749 mbedtls_cipher_free( &ctx );
750}
751/* END_CASE */
752
753/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Paul Bakker33b43f12013-08-20 11:48:36 +0200754void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200755{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200756 const mbedtls_cipher_info_t *cipher_info;
757 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200758
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200759 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200760
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200761 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200762 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200763 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200764
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200765 TEST_ASSERT( ret == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200766
Paul Bakkerbd51b262014-07-10 15:26:12 +0200767exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200768 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200769}
Paul Bakker33b43f12013-08-20 11:48:36 +0200770/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000771
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200772/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Paul Bakker33b43f12013-08-20 11:48:36 +0200773void check_padding( int pad_mode, char *input_str, int ret, int dlen_check )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200774{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200775 mbedtls_cipher_info_t cipher_info;
776 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200777 unsigned char input[16];
778 size_t ilen, dlen;
779
780 /* build a fake context just for getting access to get_padding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200781 mbedtls_cipher_init( &ctx );
782 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200783 ctx.cipher_info = &cipher_info;
784
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200785 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200786
Paul Bakker33b43f12013-08-20 11:48:36 +0200787 ilen = unhexify( input, input_str );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200788
Paul Bakker33b43f12013-08-20 11:48:36 +0200789 TEST_ASSERT( ret == ctx.get_padding( input, ilen, &dlen ) );
790 if( 0 == ret )
791 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200792}
Paul Bakker33b43f12013-08-20 11:48:36 +0200793/* END_CASE */