File: test_suite_nist_kw.function

package info (click to toggle)
skiboot 6.7.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 35,060 kB
  • sloc: ansic: 214,818; sh: 11,529; python: 3,418; cpp: 3,135; makefile: 1,773; perl: 1,479; asm: 1,445; tcl: 1,147; pascal: 107
file content (347 lines) | stat: -rw-r--r-- 10,857 bytes parent folder | download | duplicates (11)
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
/* BEGIN_HEADER */
#include "mbedtls/nist_kw.h"
/* END_HEADER */

/* BEGIN_DEPENDENCIES
 * depends_on:MBEDTLS_NIST_KW_C
 * END_DEPENDENCIES
 */

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

/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
void mbedtls_nist_kw_mix_contexts( )
{
    mbedtls_nist_kw_context ctx1, ctx2;
    unsigned char key[16];
    unsigned char plaintext[32];
    unsigned char ciphertext1[40];
    unsigned char ciphertext2[40];
    size_t output_len, i;

    memset( plaintext, 0, sizeof( plaintext ) );
    memset( ciphertext1, 0, sizeof( ciphertext1 ) );
    memset( ciphertext2, 0, sizeof( ciphertext2 ) );
    memset( key, 0, sizeof( key ) );

    /*
     * 1. Check wrap and unwrap with two seperate contexts
     */
    mbedtls_nist_kw_init( &ctx1 );
    mbedtls_nist_kw_init( &ctx2 );

    TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx1,
                                         MBEDTLS_CIPHER_ID_AES,
                                         key, sizeof( key ) * 8,
                                         1 ) == 0 );

    TEST_ASSERT( mbedtls_nist_kw_wrap( &ctx1, MBEDTLS_KW_MODE_KW,
                                       plaintext, sizeof( plaintext ),
                                       ciphertext1, &output_len,
                                       sizeof( ciphertext1 ) ) == 0 );
    TEST_ASSERT( output_len == sizeof( ciphertext1 ) );

    TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx2,
                                         MBEDTLS_CIPHER_ID_AES,
                                         key, sizeof( key ) * 8,
                                         0 ) == 0 );

    TEST_ASSERT( mbedtls_nist_kw_unwrap( &ctx2, MBEDTLS_KW_MODE_KW,
                                         ciphertext1, output_len,
                                         plaintext, &output_len,
                                         sizeof( plaintext ) ) == 0 );

    TEST_ASSERT( output_len == sizeof( plaintext ) );
    for( i = 0; i < sizeof( plaintext ); i++ )
    {
        TEST_ASSERT( plaintext[i] == 0 );
    }
    mbedtls_nist_kw_free( &ctx1 );
    mbedtls_nist_kw_free( &ctx2 );

    /*
     * 2. Check wrapping with two modes, on same context
     */
    mbedtls_nist_kw_init( &ctx1 );
    mbedtls_nist_kw_init( &ctx2 );
    output_len = sizeof( ciphertext1 );

    TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx1,
                                         MBEDTLS_CIPHER_ID_AES,
                                         key, sizeof( key ) * 8,
                                         1 ) == 0 );

    TEST_ASSERT( mbedtls_nist_kw_wrap( &ctx1, MBEDTLS_KW_MODE_KW,
                                       plaintext, sizeof( plaintext ),
                                       ciphertext1, &output_len,
                                       sizeof( ciphertext1 ) ) == 0 );
    TEST_ASSERT( output_len == sizeof( ciphertext1 ) );

    TEST_ASSERT( mbedtls_nist_kw_wrap( &ctx1, MBEDTLS_KW_MODE_KWP,
                                       plaintext, sizeof( plaintext ),
                                       ciphertext2, &output_len,
                                       sizeof( ciphertext2 ) ) == 0 );

    TEST_ASSERT( output_len == sizeof( ciphertext2 ) );

    TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx2,
                                         MBEDTLS_CIPHER_ID_AES,
                                         key, sizeof( key ) * 8,
                                         0 ) == 0 );

    TEST_ASSERT( mbedtls_nist_kw_unwrap( &ctx2, MBEDTLS_KW_MODE_KW,
                                         ciphertext1, sizeof( ciphertext1 ),
                                         plaintext, &output_len,
                                         sizeof( plaintext ) ) == 0 );

    TEST_ASSERT( output_len == sizeof( plaintext ) );

    for( i = 0; i < sizeof( plaintext ); i++ )
    {
        TEST_ASSERT( plaintext[i] == 0 );
    }

    TEST_ASSERT( mbedtls_nist_kw_unwrap( &ctx2, MBEDTLS_KW_MODE_KWP,
                                         ciphertext2, sizeof( ciphertext2 ),
                                         plaintext, &output_len,
                                         sizeof( plaintext ) ) == 0 );

    TEST_ASSERT( output_len == sizeof( plaintext ) );

    for( i = 0; i < sizeof( plaintext ); i++ )
    {
        TEST_ASSERT( plaintext[i] == 0 );
    }

exit:
    mbedtls_nist_kw_free( &ctx1 );
    mbedtls_nist_kw_free( &ctx2 );
}
/* END_CASE */

/* BEGIN_CASE */
void mbedtls_nist_kw_setkey( int cipher_id, int key_size,
                             int is_wrap, int result )
{
    mbedtls_nist_kw_context ctx;
    unsigned char key[32];
    int ret;

    mbedtls_nist_kw_init( &ctx );

    memset( key, 0x2A, sizeof( key ) );
    TEST_ASSERT( (unsigned) key_size <= 8 * sizeof( key ) );

    ret = mbedtls_nist_kw_setkey( &ctx, cipher_id, key, key_size, is_wrap );
    TEST_ASSERT( ret == result );

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

/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
void nist_kw_plaintext_lengths( int in_len, int out_len, int mode, int res )
{
    mbedtls_nist_kw_context ctx;
    unsigned char key[16];
    unsigned char *plaintext = NULL;
    unsigned char *ciphertext = NULL;
    size_t output_len = out_len;

    mbedtls_nist_kw_init( &ctx );

    memset( key, 0, sizeof( key ) );

    if( in_len != 0 )
    {
        plaintext = mbedtls_calloc( 1, in_len );
        TEST_ASSERT( plaintext != NULL );
    }

    if( out_len != 0 )
    {
        ciphertext = mbedtls_calloc( 1, output_len );
        TEST_ASSERT( ciphertext != NULL );
    }

    memset( plaintext, 0, in_len );
    memset( ciphertext, 0, output_len );


    TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx, MBEDTLS_CIPHER_ID_AES,
                                         key, 8 * sizeof( key ), 1 ) == 0 );

    TEST_ASSERT( mbedtls_nist_kw_wrap( &ctx, mode, plaintext, in_len,
                                      ciphertext, &output_len,
                                      output_len ) == res );
    if( res == 0 )
    {
        if( mode == MBEDTLS_KW_MODE_KWP )
            TEST_ASSERT( output_len == (size_t) in_len + 8 -
                         ( in_len % 8 ) + 8 );
        else
            TEST_ASSERT( output_len == (size_t) in_len + 8 );
    }
    else
    {
        TEST_ASSERT( output_len == 0 );
    }

exit:
    mbedtls_free( ciphertext );
    mbedtls_free( plaintext );
    mbedtls_nist_kw_free( &ctx );
}
/* END_CASE */

/* BEGIN_CASE depends_on:MBEDTLS_AES_C */
void nist_kw_ciphertext_lengths( int in_len, int out_len, int mode, int res )
{
    mbedtls_nist_kw_context ctx;
    unsigned char key[16];
    unsigned char *plaintext = NULL;
    unsigned char *ciphertext = NULL;
    int unwrap_ret;
    size_t output_len = out_len;

    mbedtls_nist_kw_init( &ctx );

    memset( key, 0, sizeof( key ) );

    if( out_len != 0 )
    {
        plaintext = mbedtls_calloc( 1, output_len );
        TEST_ASSERT( plaintext != NULL );
    }
    if( in_len != 0 )
    {
        ciphertext = mbedtls_calloc( 1, in_len );
        TEST_ASSERT( ciphertext != NULL );
    }

    memset( plaintext, 0, output_len );
    memset( ciphertext, 0, in_len );


    TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx, MBEDTLS_CIPHER_ID_AES,
                                         key, 8 * sizeof( key ), 0 ) == 0 );
    unwrap_ret = mbedtls_nist_kw_unwrap( &ctx, mode, ciphertext, in_len,
                                         plaintext, &output_len,
                                         output_len );

    if( res == 0 )
        TEST_ASSERT( unwrap_ret == MBEDTLS_ERR_CIPHER_AUTH_FAILED );
    else
        TEST_ASSERT( unwrap_ret == res );

    TEST_ASSERT( output_len == 0 );

exit:
    mbedtls_free( ciphertext );
    mbedtls_free( plaintext );
    mbedtls_nist_kw_free( &ctx );
}
/* END_CASE */

/* BEGIN_CASE */
void mbedtls_nist_kw_wrap( int cipher_id, int mode,
                           char *key_hex, char *msg_hex,
                           char *result_hex )
{
    unsigned char key[32];
    unsigned char msg[512];
    unsigned char result[528];
    unsigned char expected_result[528];
    mbedtls_nist_kw_context ctx;
    size_t key_len, msg_len, output_len, result_len, i, padlen;

    mbedtls_nist_kw_init( &ctx );

    memset( key, 0x00, sizeof( key ) );
    memset( msg, 0x00, sizeof( msg ) );
    memset( result, '+', sizeof( result ) );

    key_len = unhexify( key, key_hex );
    msg_len = unhexify( msg, msg_hex );
    result_len = unhexify( expected_result, result_hex );
    output_len = sizeof( result );

    TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx, cipher_id, key, key_len * 8, 1 )
                 == 0 );

    /* Test with input == output */
    TEST_ASSERT( mbedtls_nist_kw_wrap( &ctx, mode, msg, msg_len,
                 result, &output_len, sizeof( result ) ) == 0 );

    TEST_ASSERT( output_len == result_len );

    TEST_ASSERT( memcmp( expected_result, result, result_len ) == 0 );

    padlen = ( msg_len % 8 != 0 ) ? 8 - (msg_len % 8 ) : 0;
    /* Check that the function didn't write beyond the end of the buffer. */
    for( i = msg_len + 8 + padlen; i < sizeof( result ); i++ )
    {
        TEST_ASSERT( result[i] == '+' );
    }

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

/* BEGIN_CASE */
void mbedtls_nist_kw_unwrap( int cipher_id, int mode,
                             char *key_hex, char *msg_hex,
                             char *result_hex, int expected_ret )
{
    unsigned char key[32];
    unsigned char msg[528];
    unsigned char result[528];
    unsigned char expected_result[528];
    mbedtls_nist_kw_context ctx;
    size_t key_len, msg_len, output_len, result_len, i;

    mbedtls_nist_kw_init( &ctx );

    memset( key, 0x00, sizeof( key ) );
    memset( msg, 0x00, sizeof( msg ) );
    memset( result, '+', sizeof( result ) );
    memset( expected_result, 0x00, sizeof( expected_result ) );

    key_len = unhexify( key, key_hex );
    msg_len = unhexify( msg, msg_hex );
    result_len = unhexify( expected_result, result_hex );
    output_len = sizeof( result );

    TEST_ASSERT( mbedtls_nist_kw_setkey( &ctx, cipher_id, key, key_len * 8, 0 )
                 == 0 );

    /* Test with input == output */
    TEST_ASSERT( mbedtls_nist_kw_unwrap( &ctx, mode, msg, msg_len,
                 result, &output_len, sizeof( result ) ) == expected_ret );
    if( expected_ret == 0 )
    {
        TEST_ASSERT( output_len == result_len );
        TEST_ASSERT( memcmp( expected_result, result, result_len ) == 0 );
    }
    else
    {
        TEST_ASSERT( output_len == 0 );
    }

    /* Check that the function didn't write beyond the end of the buffer. */
    for( i = msg_len - 8; i < sizeof( result ); i++ )
    {
        TEST_ASSERT( result[i] == '+' );
    }

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