File: test_suite_gcm.function

package info (click to toggle)
edk2 2025.02-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 271,704 kB
  • sloc: ansic: 2,109,987; asm: 263,832; perl: 227,730; python: 149,919; sh: 34,967; cpp: 21,813; makefile: 3,282; xml: 806; pascal: 721; lisp: 35; ruby: 16; sed: 6; tcl: 4
file content (462 lines) | stat: -rw-r--r-- 15,474 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/* BEGIN_HEADER */
#include "mbedtls/gcm.h"

/* Use the multipart interface to process the encrypted data in two parts
 * and check that the output matches the expected output.
 * The context must have been set up with the key. */
static int check_multipart( mbedtls_gcm_context *ctx,
                            int mode,
                            const data_t *iv,
                            const data_t *add,
                            const data_t *input,
                            const data_t *expected_output,
                            const data_t *tag,
                            size_t n1,
                            size_t n1_add)
{
    int ok = 0;
    uint8_t *output = NULL;
    size_t n2 = input->len - n1;
    size_t n2_add = add->len - n1_add;
    size_t olen;

    /* Sanity checks on the test data */
    TEST_ASSERT( n1 <= input->len );
    TEST_ASSERT( n1_add <= add->len );
    TEST_EQUAL( input->len, expected_output->len );

    TEST_EQUAL( 0, mbedtls_gcm_starts( ctx, mode,
                                         iv->x, iv->len ) );
    TEST_EQUAL( 0, mbedtls_gcm_update_ad( ctx, add->x, n1_add ) );
    TEST_EQUAL( 0, mbedtls_gcm_update_ad( ctx, add->x + n1_add, n2_add ) );

    /* Allocate a tight buffer for each update call. This way, if the function
     * tries to write beyond the advertised required buffer size, this will
     * count as an overflow for memory sanitizers and static checkers. */
    ASSERT_ALLOC( output, n1 );
    olen = 0xdeadbeef;
    TEST_EQUAL( 0, mbedtls_gcm_update( ctx, input->x, n1, output, n1, &olen ) );
    TEST_EQUAL( n1, olen );
    ASSERT_COMPARE( output, olen, expected_output->x, n1 );
    mbedtls_free( output );
    output = NULL;

    ASSERT_ALLOC( output, n2 );
    olen = 0xdeadbeef;
    TEST_EQUAL( 0, mbedtls_gcm_update( ctx, input->x + n1, n2, output, n2, &olen ) );
    TEST_EQUAL( n2, olen );
    ASSERT_COMPARE( output, olen, expected_output->x + n1, n2 );
    mbedtls_free( output );
    output = NULL;

    ASSERT_ALLOC( output, tag->len );
    TEST_EQUAL( 0, mbedtls_gcm_finish( ctx, NULL, 0, &olen, output, tag->len ) );
    TEST_EQUAL( 0, olen );
    ASSERT_COMPARE( output, tag->len, tag->x, tag->len );
    mbedtls_free( output );
    output = NULL;

    ok = 1;
exit:
    mbedtls_free( output );
    return( ok );
}

static void check_cipher_with_empty_ad( mbedtls_gcm_context *ctx,
                                        int mode,
                                        const data_t *iv,
                                        const data_t *input,
                                        const data_t *expected_output,
                                        const data_t *tag,
                                        size_t ad_update_count)
{
    size_t n;
    uint8_t *output = NULL;
    size_t olen;

    /* Sanity checks on the test data */
    TEST_EQUAL( input->len, expected_output->len );

    TEST_EQUAL( 0, mbedtls_gcm_starts( ctx, mode,
                                       iv->x, iv->len ) );

    for( n = 0; n < ad_update_count; n++ )
    {
        TEST_EQUAL( 0, mbedtls_gcm_update_ad( ctx, NULL, 0 ) );
    }

    /* Allocate a tight buffer for each update call. This way, if the function
     * tries to write beyond the advertised required buffer size, this will
     * count as an overflow for memory sanitizers and static checkers. */
    ASSERT_ALLOC( output, input->len );
    olen = 0xdeadbeef;
    TEST_EQUAL( 0, mbedtls_gcm_update( ctx, input->x, input->len, output, input->len, &olen ) );
    TEST_EQUAL( input->len, olen );
    ASSERT_COMPARE( output, olen, expected_output->x, input->len );
    mbedtls_free( output );
    output = NULL;

    ASSERT_ALLOC( output, tag->len );
    TEST_EQUAL( 0, mbedtls_gcm_finish( ctx, NULL, 0, &olen, output, tag->len ) );
    TEST_EQUAL( 0, olen );
    ASSERT_COMPARE( output, tag->len, tag->x, tag->len );

exit:
    mbedtls_free( output );
}

static void check_empty_cipher_with_ad( mbedtls_gcm_context *ctx,
                                       int mode,
                                       const data_t *iv,
                                       const data_t *add,
                                       const data_t *tag,
                                       size_t cipher_update_count)
{
    size_t olen;
    size_t n;
    uint8_t* output_tag = NULL;

    TEST_EQUAL( 0, mbedtls_gcm_starts( ctx, mode, iv->x, iv->len ) );
    TEST_EQUAL( 0, mbedtls_gcm_update_ad( ctx, add->x, add->len ) );

    for( n = 0; n < cipher_update_count; n++ )
    {
        olen = 0xdeadbeef;
        TEST_EQUAL( 0, mbedtls_gcm_update( ctx, NULL, 0, NULL, 0, &olen ) );
        TEST_EQUAL( 0, olen );
    }

    ASSERT_ALLOC( output_tag, tag->len );
    TEST_EQUAL( 0, mbedtls_gcm_finish( ctx, NULL, 0, &olen,
                                       output_tag, tag->len ) );
    TEST_EQUAL( 0, olen );
    ASSERT_COMPARE( output_tag, tag->len, tag->x, tag->len );

exit:
    mbedtls_free( output_tag );
}

static void check_no_cipher_no_ad( mbedtls_gcm_context *ctx,
                                   int mode,
                                   const data_t *iv,
                                   const data_t *tag )
{
    uint8_t *output = NULL;
    size_t olen = 0;

    TEST_EQUAL( 0, mbedtls_gcm_starts( ctx, mode,
                                       iv->x, iv->len ) );
    ASSERT_ALLOC( output, tag->len );
    TEST_EQUAL( 0, mbedtls_gcm_finish( ctx, NULL, 0, &olen, output, tag->len ) );
    TEST_EQUAL( 0, olen );
    ASSERT_COMPARE( output, tag->len, tag->x, tag->len );

exit:
    mbedtls_free( output );
}

/* END_HEADER */

/* BEGIN_DEPENDENCIES
 * depends_on:MBEDTLS_GCM_C
 * END_DEPENDENCIES
 */

/* BEGIN_CASE */
void gcm_bad_parameters( int cipher_id, int direction,
                         data_t *key_str, data_t *src_str,
                         data_t *iv_str, data_t *add_str,
                         int tag_len_bits, int gcm_result )
{
    unsigned char output[128];
    unsigned char tag_output[16];
    mbedtls_gcm_context ctx;
    size_t tag_len = tag_len_bits / 8;

    mbedtls_gcm_init( &ctx );

    memset( output, 0x00, sizeof( output ) );
    memset( tag_output, 0x00, sizeof( tag_output ) );

    TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == 0 );
    TEST_ASSERT( mbedtls_gcm_crypt_and_tag( &ctx, direction, src_str->len, iv_str->x, iv_str->len,
                 add_str->x, add_str->len, src_str->x, output, tag_len, tag_output ) == gcm_result );

exit:
    mbedtls_gcm_free( &ctx );
}
/* END_CASE */

/* BEGIN_CASE */
void gcm_encrypt_and_tag( int cipher_id, data_t * key_str,
                          data_t * src_str, data_t * iv_str,
                          data_t * add_str, data_t * dst,
                          int tag_len_bits, data_t * tag,
                          int init_result )
{
    unsigned char output[128];
    unsigned char tag_output[16];
    mbedtls_gcm_context ctx;
    size_t tag_len = tag_len_bits / 8;
    size_t n1;
    size_t n1_add;

    mbedtls_gcm_init( &ctx );

    memset(output, 0x00, 128);
    memset(tag_output, 0x00, 16);


    TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == init_result );
    if( init_result == 0 )
    {
        TEST_ASSERT( mbedtls_gcm_crypt_and_tag( &ctx, MBEDTLS_GCM_ENCRYPT, src_str->len, iv_str->x, iv_str->len, add_str->x, add_str->len, src_str->x, output, tag_len, tag_output ) == 0 );

        ASSERT_COMPARE( output, src_str->len, dst->x, dst->len );
        ASSERT_COMPARE( tag_output, tag_len, tag->x, tag->len );

        for( n1 = 0; n1 <= src_str->len; n1 += 1 )
        {
            for( n1_add = 0; n1_add <= add_str->len; n1_add += 1 )
            {
                mbedtls_test_set_step( n1 * 10000 + n1_add );
                if( !check_multipart( &ctx, MBEDTLS_GCM_ENCRYPT,
                                    iv_str, add_str, src_str,
                                    dst, tag,
                                    n1, n1_add ) )
                    goto exit;
            }
        }
    }

exit:
    mbedtls_gcm_free( &ctx );
}
/* END_CASE */

/* BEGIN_CASE */
void gcm_decrypt_and_verify( int cipher_id, data_t * key_str,
                             data_t * src_str, data_t * iv_str,
                             data_t * add_str, int tag_len_bits,
                             data_t * tag_str, char * result,
                             data_t * pt_result, int init_result )
{
    unsigned char output[128];
    mbedtls_gcm_context ctx;
    int ret;
    size_t tag_len = tag_len_bits / 8;
    size_t n1;
    size_t n1_add;

    mbedtls_gcm_init( &ctx );

    memset(output, 0x00, 128);


    TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == init_result );
    if( init_result == 0 )
    {
        ret = mbedtls_gcm_auth_decrypt( &ctx, src_str->len, iv_str->x, iv_str->len, add_str->x, add_str->len, tag_str->x, tag_len, src_str->x, output );

        if( strcmp( "FAIL", result ) == 0 )
        {
            TEST_ASSERT( ret == MBEDTLS_ERR_GCM_AUTH_FAILED );
        }
        else
        {
            TEST_ASSERT( ret == 0 );
            ASSERT_COMPARE( output, src_str->len, pt_result->x, pt_result->len );

            for( n1 = 0; n1 <= src_str->len; n1 += 1 )
            {
                for( n1_add = 0; n1_add <= add_str->len; n1_add += 1 )
                {
                    mbedtls_test_set_step( n1 * 10000 + n1_add );
                    if( !check_multipart( &ctx, MBEDTLS_GCM_DECRYPT,
                                        iv_str, add_str, src_str,
                                        pt_result, tag_str,
                                        n1, n1_add ) )
                        goto exit;
                }
            }
        }
    }

exit:
    mbedtls_gcm_free( &ctx );
}
/* END_CASE */

/* BEGIN_CASE */
void gcm_decrypt_and_verify_empty_cipher( int cipher_id,
                                          data_t * key_str,
                                          data_t * iv_str,
                                          data_t * add_str,
                                          data_t * tag_str,
                                          int cipher_update_calls )
{
    mbedtls_gcm_context ctx;

    mbedtls_gcm_init( &ctx );

    TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == 0 );
    check_empty_cipher_with_ad( &ctx, MBEDTLS_GCM_DECRYPT,
                                iv_str, add_str, tag_str,
                                cipher_update_calls );

    mbedtls_gcm_free( &ctx );
}
/* END_CASE */

/* BEGIN_CASE */
void gcm_decrypt_and_verify_empty_ad( int cipher_id,
                                      data_t * key_str,
                                      data_t * iv_str,
                                      data_t * src_str,
                                      data_t * tag_str,
                                      data_t * pt_result,
                                      int ad_update_calls )
{
    mbedtls_gcm_context ctx;

    mbedtls_gcm_init( &ctx );

    TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == 0 );
    check_cipher_with_empty_ad( &ctx, MBEDTLS_GCM_DECRYPT,
                                iv_str, src_str, pt_result, tag_str,
                                ad_update_calls );

    mbedtls_gcm_free( &ctx );
}
/* END_CASE */

/* BEGIN_CASE */
void gcm_decrypt_and_verify_no_ad_no_cipher( int cipher_id,
                                             data_t * key_str,
                                             data_t * iv_str,
                                             data_t * tag_str )
{
    mbedtls_gcm_context ctx;

    mbedtls_gcm_init( &ctx );

    TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == 0 );
    check_no_cipher_no_ad( &ctx, MBEDTLS_GCM_DECRYPT,
                           iv_str, tag_str );

    mbedtls_gcm_free( &ctx );
}
/* END_CASE */

/* BEGIN_CASE */
void gcm_encrypt_and_tag_empty_cipher( int cipher_id,
                                       data_t * key_str,
                                       data_t * iv_str,
                                       data_t * add_str,
                                       data_t * tag_str,
                                       int cipher_update_calls )
{
    mbedtls_gcm_context ctx;

    mbedtls_gcm_init( &ctx );

    TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == 0 );
    check_empty_cipher_with_ad( &ctx, MBEDTLS_GCM_ENCRYPT,
                                iv_str, add_str, tag_str,
                                cipher_update_calls );

exit:
    mbedtls_gcm_free( &ctx );
}
/* END_CASE */

/* BEGIN_CASE */
void gcm_encrypt_and_tag_empty_ad( int cipher_id,
                                   data_t * key_str,
                                   data_t * iv_str,
                                   data_t * src_str,
                                   data_t * dst,
                                   data_t * tag_str,
                                   int ad_update_calls )
{
    mbedtls_gcm_context ctx;

    mbedtls_gcm_init( &ctx );

    TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == 0 );
    check_cipher_with_empty_ad( &ctx, MBEDTLS_GCM_ENCRYPT,
                                iv_str, src_str, dst, tag_str,
                                ad_update_calls );

exit:
    mbedtls_gcm_free( &ctx );
}
/* END_CASE */

/* BEGIN_CASE */
void gcm_encrypt_and_verify_no_ad_no_cipher( int cipher_id,
                                             data_t * key_str,
                                             data_t * iv_str,
                                             data_t * tag_str )
{
    mbedtls_gcm_context ctx;

    mbedtls_gcm_init( &ctx );

    TEST_ASSERT( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ) == 0 );
    check_no_cipher_no_ad( &ctx, MBEDTLS_GCM_ENCRYPT,
                           iv_str, tag_str );

    mbedtls_gcm_free( &ctx );
}
/* END_CASE */

/* BEGIN_CASE */
void gcm_invalid_param( )
{
    mbedtls_gcm_context ctx;
    unsigned char valid_buffer[] = { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06 };
    mbedtls_cipher_id_t valid_cipher = MBEDTLS_CIPHER_ID_AES;
    int invalid_bitlen = 1;

    mbedtls_gcm_init( &ctx );

    /* mbedtls_gcm_setkey */
    TEST_EQUAL(
        MBEDTLS_ERR_GCM_BAD_INPUT,
        mbedtls_gcm_setkey( &ctx, valid_cipher, valid_buffer, invalid_bitlen ) );

exit:
    mbedtls_gcm_free( &ctx );
}
/* END_CASE */

/* BEGIN_CASE */
void gcm_update_output_buffer_too_small( int cipher_id, int mode,
                                         data_t * key_str, const data_t *input,
                                         const data_t *iv )
{
    mbedtls_gcm_context ctx;
    uint8_t *output = NULL;
    size_t olen = 0;
    size_t output_len = input->len - 1;

    mbedtls_gcm_init( &ctx );
    TEST_EQUAL( mbedtls_gcm_setkey( &ctx, cipher_id, key_str->x, key_str->len * 8 ), 0 );
    TEST_EQUAL( 0, mbedtls_gcm_starts( &ctx, mode, iv->x, iv->len ) );

    ASSERT_ALLOC( output, output_len );
    TEST_EQUAL( MBEDTLS_ERR_GCM_BUFFER_TOO_SMALL, mbedtls_gcm_update( &ctx, input->x, input->len, output, output_len, &olen ) );

exit:
    mbedtls_free( output );
    mbedtls_gcm_free( &ctx );
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_SELF_TEST:MBEDTLS_AES_C */
void gcm_selftest(  )
{
    TEST_ASSERT( mbedtls_gcm_self_test( 1 ) == 0 );
}
/* END_CASE */