blob: da9dfa13801887418f8711e8b6a38e2f6af43921 [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 */
Azim Khanf1aaec92017-05-30 14:23:15 +010015void 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 */
Azim Khanf1aaec92017-05-30 14:23:15 +010025void cipher_null_args( )
Manuel Pégourié-Gonnard5e7693f2014-06-13 16:08:07 +020026{
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 */
Azim Khanf1aaec92017-05-30 14:23:15 +010095void cipher_special_behaviours( )
Paul Bakker6a9c7252016-07-14 13:46:10 +010096{
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 */
Azim Khanf1aaec92017-05-30 14:23:15 +0100144void enc_dec_buf( int cipher_id, char * cipher_string, int key_len,
Paul Bakker33b43f12013-08-20 11:48:36 +0200145 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 */
Azim Khanf1aaec92017-05-30 14:23:15 +0100269void enc_fail( int cipher_id, int pad_mode, int key_len, int length_val,
270 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 */
Azim Khanf1aaec92017-05-30 14:23:15 +0100321void 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 */
Azim Khan5fcca462018-06-29 11:05:32 +0100485void decrypt_test_vec( int cipher_id, int pad_mode, data_t * key,
486 data_t * iv, data_t * cipher,
487 data_t * clear, data_t * ad, data_t * tag,
Azim Khand30ca132017-06-09 04:32:58 +0100488 int finish_result, int tag_result )
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200489{
Manuel Pégourié-Gonnard234e1ce2018-05-10 12:54:32 +0200490 unsigned char output[265];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200491 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200492 size_t outlen, total_len;
493
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200494 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200495
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200496 memset( output, 0x00, sizeof( output ) );
497
Azim Khanf1aaec92017-05-30 14:23:15 +0100498#if !defined(MBEDTLS_GCM_C) && !defined(MBEDTLS_CHACHAPOLY_C)
Mohammad Azim Khancf32c452017-06-13 14:55:58 +0100499 ((void) ad);
500 ((void) tag);
Manuel Pégourié-Gonnarda7496f02013-09-20 11:29:59 +0200501#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200502
503 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200504 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200505 mbedtls_cipher_info_from_type( cipher_id ) ) );
Azim Khand30ca132017-06-09 04:32:58 +0100506 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200507#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200508 if( pad_mode != -1 )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200509 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnard989ed382013-09-13 14:41:45 +0200510#else
511 (void) pad_mode;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200512#endif /* MBEDTLS_CIPHER_MODE_WITH_PADDING */
Azim Khand30ca132017-06-09 04:32:58 +0100513 TEST_ASSERT( 0 == mbedtls_cipher_set_iv( &ctx, iv->x, iv->len ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200514 TEST_ASSERT( 0 == mbedtls_cipher_reset( &ctx ) );
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200515#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100516 TEST_ASSERT( 0 == mbedtls_cipher_update_ad( &ctx, ad->x, ad->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200517#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200518
Azim Khand30ca132017-06-09 04:32:58 +0100519 /* decode buffer and check tag->x */
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200520 total_len = 0;
Azim Khand30ca132017-06-09 04:32:58 +0100521 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, cipher->x, cipher->len, output, &outlen ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200522 total_len += outlen;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200523 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200524 &outlen ) );
525 total_len += outlen;
Manuel Pégourié-Gonnarddca3a5d2018-05-07 10:43:27 +0200526#if defined(MBEDTLS_GCM_C) || defined(MBEDTLS_CHACHAPOLY_C)
Azim Khand30ca132017-06-09 04:32:58 +0100527 TEST_ASSERT( tag_result == mbedtls_cipher_check_tag( &ctx, tag->x, tag->len ) );
Manuel Pégourié-Gonnard8f625632014-06-24 15:26:28 +0200528#endif
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200529
530 /* check plaintext only if everything went fine */
531 if( 0 == finish_result && 0 == tag_result )
532 {
Azim Khand30ca132017-06-09 04:32:58 +0100533 TEST_ASSERT( total_len == clear->len );
534 TEST_ASSERT( 0 == memcmp( output, clear->x, clear->len ) );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200535 }
536
Paul Bakkerbd51b262014-07-10 15:26:12 +0200537exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200538 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200539}
540/* END_CASE */
541
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200542/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_AEAD */
Azim Khan5fcca462018-06-29 11:05:32 +0100543void auth_crypt_tv( int cipher_id, data_t * key, data_t * iv,
544 data_t * ad, data_t * cipher, data_t * tag,
545 char * result, data_t * clear )
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200546{
547 int ret;
Manuel Pégourié-Gonnard69767d12018-05-09 12:25:18 +0200548 unsigned char output[267]; /* above + 2 (overwrite check) */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200549 unsigned char my_tag[20];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200550 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200551 size_t outlen;
552
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200553 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200554
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200555 memset( output, 0xFF, sizeof( output ) );
Azim Khan46c9b1f2017-05-31 20:46:35 +0100556 memset( my_tag, 0xFF, sizeof( my_tag ) );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200557
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200558
559 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200560 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200561 mbedtls_cipher_info_from_type( cipher_id ) ) );
Azim Khand30ca132017-06-09 04:32:58 +0100562 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, MBEDTLS_DECRYPT ) );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200563
Azim Khand30ca132017-06-09 04:32:58 +0100564 /* decode buffer and check tag->x */
565 ret = mbedtls_cipher_auth_decrypt( &ctx, iv->x, iv->len, ad->x, ad->len,
566 cipher->x, cipher->len, output, &outlen,
567 tag->x, tag->len );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200568
569 /* make sure we didn't overwrite */
570 TEST_ASSERT( output[outlen + 0] == 0xFF );
571 TEST_ASSERT( output[outlen + 1] == 0xFF );
572
573 /* make sure the message is rejected if it should be */
Azim Khan46c9b1f2017-05-31 20:46:35 +0100574 if( strcmp( result, "FAIL" ) == 0 )
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200575 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576 TEST_ASSERT( ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
Paul Bakkerbd51b262014-07-10 15:26:12 +0200577 goto exit;
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200578 }
579
580 /* otherwise, make sure it was decrypted properly */
581 TEST_ASSERT( ret == 0 );
582
Azim Khand30ca132017-06-09 04:32:58 +0100583 TEST_ASSERT( outlen == clear->len );
584 TEST_ASSERT( memcmp( output, clear->x, clear->len ) == 0 );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200585
Azim Khand30ca132017-06-09 04:32:58 +0100586 /* then encrypt the clear->x and make sure we get the same ciphertext and tag->x */
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200587 memset( output, 0xFF, sizeof( output ) );
588 outlen = 0;
589
Azim Khand30ca132017-06-09 04:32:58 +0100590 ret = mbedtls_cipher_auth_encrypt( &ctx, iv->x, iv->len, ad->x, ad->len,
591 clear->x, clear->len, output, &outlen,
592 my_tag, tag->len );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200593 TEST_ASSERT( ret == 0 );
594
Azim Khand30ca132017-06-09 04:32:58 +0100595 TEST_ASSERT( outlen == clear->len );
596 TEST_ASSERT( memcmp( output, cipher->x, clear->len ) == 0 );
597 TEST_ASSERT( memcmp( my_tag, tag->x, tag->len ) == 0 );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200598
599 /* make sure we didn't overwrite */
600 TEST_ASSERT( output[outlen + 0] == 0xFF );
601 TEST_ASSERT( output[outlen + 1] == 0xFF );
Azim Khand30ca132017-06-09 04:32:58 +0100602 TEST_ASSERT( my_tag[tag->len + 0] == 0xFF );
603 TEST_ASSERT( my_tag[tag->len + 1] == 0xFF );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200604
605
Paul Bakkerbd51b262014-07-10 15:26:12 +0200606exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200607 mbedtls_cipher_free( &ctx );
Manuel Pégourié-Gonnard542eac52014-05-15 16:03:07 +0200608}
609/* END_CASE */
610
Manuel Pégourié-Gonnard8eccab52013-09-03 18:31:25 +0200611/* BEGIN_CASE */
Azim Khan5fcca462018-06-29 11:05:32 +0100612void test_vec_ecb( int cipher_id, int operation, data_t * key,
613 data_t * input, data_t * result, int finish_result
Azim Khand30ca132017-06-09 04:32:58 +0100614 )
Paul Bakker5e0efa72013-09-08 23:04:04 +0200615{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200616 mbedtls_cipher_context_t ctx;
Paul Bakker5e0efa72013-09-08 23:04:04 +0200617 unsigned char output[32];
618 size_t outlen;
619
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200620 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200621
Paul Bakker5e0efa72013-09-08 23:04:04 +0200622 memset( output, 0x00, sizeof( output ) );
623
624 /* Prepare context */
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200625 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200626 mbedtls_cipher_info_from_type( cipher_id ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200627
Paul Bakker5e0efa72013-09-08 23:04:04 +0200628
Azim Khand30ca132017-06-09 04:32:58 +0100629 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key->x, 8 * key->len, operation ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200630
Azim Khand30ca132017-06-09 04:32:58 +0100631 TEST_ASSERT( 0 == mbedtls_cipher_update( &ctx, input->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200632 mbedtls_cipher_get_block_size( &ctx ),
Paul Bakker5e0efa72013-09-08 23:04:04 +0200633 output, &outlen ) );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200634 TEST_ASSERT( outlen == mbedtls_cipher_get_block_size( &ctx ) );
635 TEST_ASSERT( finish_result == mbedtls_cipher_finish( &ctx, output + outlen,
Paul Bakker5e0efa72013-09-08 23:04:04 +0200636 &outlen ) );
637 TEST_ASSERT( 0 == outlen );
638
639 /* check plaintext only if everything went fine */
640 if( 0 == finish_result )
Azim Khand30ca132017-06-09 04:32:58 +0100641 TEST_ASSERT( 0 == memcmp( output, result->x,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200642 mbedtls_cipher_get_block_size( &ctx ) ) );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200643
Paul Bakkerbd51b262014-07-10 15:26:12 +0200644exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200645 mbedtls_cipher_free( &ctx );
Paul Bakker5e0efa72013-09-08 23:04:04 +0200646}
647/* END_CASE */
648
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200649/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Ron Eldor7b012442017-09-25 17:03:12 +0300650void test_vec_crypt( int cipher_id, int operation, char *hex_key,
Hanno Beckere43164e2018-11-12 12:46:35 +0000651 char *hex_iv, char *hex_input, char *hex_result,
652 int finish_result, int use_psa )
Ron Eldor7b012442017-09-25 17:03:12 +0300653{
654 unsigned char key[50];
655 unsigned char input[16];
656 unsigned char result[16];
657 unsigned char iv[16];
658 size_t key_len, iv_len, inputlen, resultlen;
659 mbedtls_cipher_context_t ctx;
660 unsigned char output[32];
661 size_t outlen;
662
663 mbedtls_cipher_init( &ctx );
664
665 memset( key, 0x00, sizeof( key ) );
666 memset( input, 0x00, sizeof( input ) );
667 memset( result, 0x00, sizeof( result ) );
668 memset( output, 0x00, sizeof( output ) );
669 memset( iv, 0x00, sizeof( iv ) );
670
671 /* Prepare context */
Hanno Beckere43164e2018-11-12 12:46:35 +0000672#if !defined(MBEDTLS_USE_PSA_CRYPTO)
673 (void) use_psa;
674#else
675 if( use_psa == 1 )
676 {
677 TEST_ASSERT( 0 == mbedtls_cipher_setup_psa( &ctx,
678 mbedtls_cipher_info_from_type( cipher_id ) ) );
679 }
680 else
681#endif /* MBEDTLS_USE_PSA_CRYPTO */
Ron Eldor7b012442017-09-25 17:03:12 +0300682 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx,
Hanno Beckere43164e2018-11-12 12:46:35 +0000683 mbedtls_cipher_info_from_type( cipher_id ) ) );
Ron Eldor7b012442017-09-25 17:03:12 +0300684
685 key_len = unhexify( key, hex_key );
686 inputlen = unhexify( input, hex_input );
687 resultlen = unhexify( result, hex_result );
688
689 TEST_ASSERT( 0 == mbedtls_cipher_setkey( &ctx, key, 8 * key_len, operation ) );
690 if( MBEDTLS_MODE_CBC == ctx.cipher_info->mode )
691 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, MBEDTLS_PADDING_NONE ) );
692
Ron Eldor4e64e0b2017-09-25 18:22:32 +0300693 iv_len = unhexify( iv, hex_iv );
Ron Eldor7b012442017-09-25 17:03:12 +0300694
695 TEST_ASSERT( finish_result == mbedtls_cipher_crypt( &ctx, iv_len ? iv : NULL,
696 iv_len, input, inputlen,
697 output, &outlen ) );
698 TEST_ASSERT( resultlen == outlen );
699 /* check plaintext only if everything went fine */
700 if( 0 == finish_result )
701 TEST_ASSERT( 0 == memcmp( output, result, outlen ) );
702
703exit:
704 mbedtls_cipher_free( &ctx );
705}
706/* END_CASE */
707
708/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_WITH_PADDING */
Paul Bakker33b43f12013-08-20 11:48:36 +0200709void set_padding( int cipher_id, int pad_mode, int ret )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200710{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200711 const mbedtls_cipher_info_t *cipher_info;
712 mbedtls_cipher_context_t ctx;
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200713
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200714 mbedtls_cipher_init( &ctx );
Paul Bakkerd2a2d612014-07-01 15:45:49 +0200715
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200716 cipher_info = mbedtls_cipher_info_from_type( cipher_id );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200717 TEST_ASSERT( NULL != cipher_info );
Manuel Pégourié-Gonnard8473f872015-05-14 13:51:45 +0200718 TEST_ASSERT( 0 == mbedtls_cipher_setup( &ctx, cipher_info ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200719
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200720 TEST_ASSERT( ret == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnardd5fdcaf2013-07-24 18:05:00 +0200721
Paul Bakkerbd51b262014-07-10 15:26:12 +0200722exit:
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200723 mbedtls_cipher_free( &ctx );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200724}
Paul Bakker33b43f12013-08-20 11:48:36 +0200725/* END_CASE */
Paul Bakker8123e9d2011-01-06 15:37:30 +0000726
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200727/* BEGIN_CASE depends_on:MBEDTLS_CIPHER_MODE_CBC */
Azim Khan5fcca462018-06-29 11:05:32 +0100728void check_padding( int pad_mode, data_t * input, int ret, int dlen_check
Azim Khand30ca132017-06-09 04:32:58 +0100729 )
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200730{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200731 mbedtls_cipher_info_t cipher_info;
732 mbedtls_cipher_context_t ctx;
Azim Khanf1aaec92017-05-30 14:23:15 +0100733 size_t dlen;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200734
735 /* build a fake context just for getting access to get_padding */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200736 mbedtls_cipher_init( &ctx );
737 cipher_info.mode = MBEDTLS_MODE_CBC;
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200738 ctx.cipher_info = &cipher_info;
739
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200740 TEST_ASSERT( 0 == mbedtls_cipher_set_padding_mode( &ctx, pad_mode ) );
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200741
Manuel Pégourié-Gonnarda6408492013-07-26 10:55:02 +0200742
Azim Khand30ca132017-06-09 04:32:58 +0100743 TEST_ASSERT( ret == ctx.get_padding( input->x, input->len, &dlen ) );
Paul Bakker33b43f12013-08-20 11:48:36 +0200744 if( 0 == ret )
745 TEST_ASSERT( dlen == (size_t) dlen_check );
Paul Bakkerdbd443d2013-08-16 13:38:47 +0200746}
Paul Bakker33b43f12013-08-20 11:48:36 +0200747/* END_CASE */