~registry/dolphin-emu/triforce

« back to all changes in this revision

Viewing changes to Externals/polarssl/library/cipher.c

  • Committer: Sérgio Benjamim
  • Date: 2015-02-13 05:54:40 UTC
  • Revision ID: sergio_br2@yahoo.com.br-20150213055440-ey2rt3sjpy27km78
Dolphin Triforce branch from code.google, commit b957980 (4.0-315).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * \file cipher.c
 
3
 * 
 
4
 * \brief Generic cipher wrapper for PolarSSL
 
5
 *
 
6
 * \author Adriaan de Jong <dejong@fox-it.com>
 
7
 *
 
8
 *  Copyright (C) 2006-2012, Brainspark B.V.
 
9
 *
 
10
 *  This file is part of PolarSSL (http://www.polarssl.org)
 
11
 *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
 
12
 *
 
13
 *  All rights reserved.
 
14
 *
 
15
 *  This program is free software; you can redistribute it and/or modify
 
16
 *  it under the terms of the GNU General Public License as published by
 
17
 *  the Free Software Foundation; either version 2 of the License, or
 
18
 *  (at your option) any later version.
 
19
 *
 
20
 *  This program is distributed in the hope that it will be useful,
 
21
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
22
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
23
 *  GNU General Public License for more details.
 
24
 *
 
25
 *  You should have received a copy of the GNU General Public License along
 
26
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 
27
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
28
 */
 
29
 
 
30
#include "polarssl/config.h"
 
31
 
 
32
#if defined(POLARSSL_CIPHER_C)
 
33
 
 
34
#include "polarssl/cipher.h"
 
35
#include "polarssl/cipher_wrap.h"
 
36
 
 
37
#include <stdlib.h>
 
38
 
 
39
#if defined _MSC_VER && !defined strcasecmp
 
40
#define strcasecmp _stricmp
 
41
#endif
 
42
 
 
43
static const int supported_ciphers[] = {
 
44
 
 
45
#if defined(POLARSSL_AES_C)
 
46
        POLARSSL_CIPHER_AES_128_CBC,
 
47
        POLARSSL_CIPHER_AES_192_CBC,
 
48
        POLARSSL_CIPHER_AES_256_CBC,
 
49
 
 
50
#if defined(POLARSSL_CIPHER_MODE_CFB)
 
51
        POLARSSL_CIPHER_AES_128_CFB128,
 
52
        POLARSSL_CIPHER_AES_192_CFB128,
 
53
        POLARSSL_CIPHER_AES_256_CFB128,
 
54
#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
 
55
 
 
56
#if defined(POLARSSL_CIPHER_MODE_CTR)
 
57
        POLARSSL_CIPHER_AES_128_CTR,
 
58
        POLARSSL_CIPHER_AES_192_CTR,
 
59
        POLARSSL_CIPHER_AES_256_CTR,
 
60
#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
 
61
 
 
62
#endif /* defined(POLARSSL_AES_C) */
 
63
 
 
64
#if defined(POLARSSL_CAMELLIA_C)
 
65
        POLARSSL_CIPHER_CAMELLIA_128_CBC,
 
66
        POLARSSL_CIPHER_CAMELLIA_192_CBC,
 
67
        POLARSSL_CIPHER_CAMELLIA_256_CBC,
 
68
 
 
69
#if defined(POLARSSL_CIPHER_MODE_CFB)
 
70
        POLARSSL_CIPHER_CAMELLIA_128_CFB128,
 
71
        POLARSSL_CIPHER_CAMELLIA_192_CFB128,
 
72
        POLARSSL_CIPHER_CAMELLIA_256_CFB128,
 
73
#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
 
74
 
 
75
#if defined(POLARSSL_CIPHER_MODE_CTR)
 
76
        POLARSSL_CIPHER_CAMELLIA_128_CTR,
 
77
        POLARSSL_CIPHER_CAMELLIA_192_CTR,
 
78
        POLARSSL_CIPHER_CAMELLIA_256_CTR,
 
79
#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
 
80
 
 
81
#endif /* defined(POLARSSL_CAMELLIA_C) */
 
82
 
 
83
#if defined(POLARSSL_DES_C)
 
84
        POLARSSL_CIPHER_DES_CBC,
 
85
        POLARSSL_CIPHER_DES_EDE_CBC,
 
86
        POLARSSL_CIPHER_DES_EDE3_CBC,
 
87
#endif /* defined(POLARSSL_DES_C) */
 
88
 
 
89
#if defined(POLARSSL_BLOWFISH_C)
 
90
        POLARSSL_CIPHER_BLOWFISH_CBC,
 
91
 
 
92
#if defined(POLARSSL_CIPHER_MODE_CFB)
 
93
        POLARSSL_CIPHER_BLOWFISH_CFB64,
 
94
#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
 
95
 
 
96
#if defined(POLARSSL_CIPHER_MODE_CTR)
 
97
        POLARSSL_CIPHER_BLOWFISH_CTR,
 
98
#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
 
99
 
 
100
#endif /* defined(POLARSSL_BLOWFISH_C) */
 
101
 
 
102
#if defined(POLARSSL_CIPHER_NULL_CIPHER)
 
103
        POLARSSL_CIPHER_NULL,
 
104
#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
 
105
 
 
106
        0
 
107
};
 
108
 
 
109
const int *cipher_list( void )
 
110
{
 
111
    return supported_ciphers;
 
112
}
 
113
 
 
114
const cipher_info_t *cipher_info_from_type( const cipher_type_t cipher_type )
 
115
{
 
116
    /* Find static cipher information */
 
117
    switch ( cipher_type )
 
118
    {
 
119
#if defined(POLARSSL_AES_C)
 
120
        case POLARSSL_CIPHER_AES_128_CBC:
 
121
            return &aes_128_cbc_info;
 
122
        case POLARSSL_CIPHER_AES_192_CBC:
 
123
            return &aes_192_cbc_info;
 
124
        case POLARSSL_CIPHER_AES_256_CBC:
 
125
            return &aes_256_cbc_info;
 
126
 
 
127
#if defined(POLARSSL_CIPHER_MODE_CFB)
 
128
        case POLARSSL_CIPHER_AES_128_CFB128:
 
129
            return &aes_128_cfb128_info;
 
130
        case POLARSSL_CIPHER_AES_192_CFB128:
 
131
            return &aes_192_cfb128_info;
 
132
        case POLARSSL_CIPHER_AES_256_CFB128:
 
133
            return &aes_256_cfb128_info;
 
134
#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
 
135
 
 
136
#if defined(POLARSSL_CIPHER_MODE_CTR)
 
137
        case POLARSSL_CIPHER_AES_128_CTR:
 
138
            return &aes_128_ctr_info;
 
139
        case POLARSSL_CIPHER_AES_192_CTR:
 
140
            return &aes_192_ctr_info;
 
141
        case POLARSSL_CIPHER_AES_256_CTR:
 
142
            return &aes_256_ctr_info;
 
143
#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
 
144
 
 
145
#endif
 
146
 
 
147
#if defined(POLARSSL_CAMELLIA_C)
 
148
        case POLARSSL_CIPHER_CAMELLIA_128_CBC:
 
149
            return &camellia_128_cbc_info;
 
150
        case POLARSSL_CIPHER_CAMELLIA_192_CBC:
 
151
            return &camellia_192_cbc_info;
 
152
        case POLARSSL_CIPHER_CAMELLIA_256_CBC:
 
153
            return &camellia_256_cbc_info;
 
154
 
 
155
#if defined(POLARSSL_CIPHER_MODE_CFB)
 
156
        case POLARSSL_CIPHER_CAMELLIA_128_CFB128:
 
157
            return &camellia_128_cfb128_info;
 
158
        case POLARSSL_CIPHER_CAMELLIA_192_CFB128:
 
159
            return &camellia_192_cfb128_info;
 
160
        case POLARSSL_CIPHER_CAMELLIA_256_CFB128:
 
161
            return &camellia_256_cfb128_info;
 
162
#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
 
163
 
 
164
#if defined(POLARSSL_CIPHER_MODE_CTR)
 
165
        case POLARSSL_CIPHER_CAMELLIA_128_CTR:
 
166
            return &camellia_128_ctr_info;
 
167
        case POLARSSL_CIPHER_CAMELLIA_192_CTR:
 
168
            return &camellia_192_ctr_info;
 
169
        case POLARSSL_CIPHER_CAMELLIA_256_CTR:
 
170
            return &camellia_256_ctr_info;
 
171
#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
 
172
 
 
173
#endif
 
174
 
 
175
#if defined(POLARSSL_DES_C)
 
176
        case POLARSSL_CIPHER_DES_CBC:
 
177
            return &des_cbc_info;
 
178
        case POLARSSL_CIPHER_DES_EDE_CBC:
 
179
            return &des_ede_cbc_info;
 
180
        case POLARSSL_CIPHER_DES_EDE3_CBC:
 
181
            return &des_ede3_cbc_info;
 
182
#endif
 
183
 
 
184
#if defined(POLARSSL_BLOWFISH_C)
 
185
        case POLARSSL_CIPHER_BLOWFISH_CBC:
 
186
            return &blowfish_cbc_info;
 
187
 
 
188
#if defined(POLARSSL_CIPHER_MODE_CFB)
 
189
        case POLARSSL_CIPHER_BLOWFISH_CFB64:
 
190
            return &blowfish_cfb64_info;
 
191
#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
 
192
 
 
193
#if defined(POLARSSL_CIPHER_MODE_CTR)
 
194
        case POLARSSL_CIPHER_BLOWFISH_CTR:
 
195
            return &blowfish_ctr_info;
 
196
#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
 
197
 
 
198
#endif
 
199
 
 
200
#if defined(POLARSSL_CIPHER_NULL_CIPHER)
 
201
        case POLARSSL_CIPHER_NULL:
 
202
            return &null_cipher_info;
 
203
#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
 
204
 
 
205
        default:
 
206
            return NULL;
 
207
    }
 
208
}
 
209
 
 
210
const cipher_info_t *cipher_info_from_string( const char *cipher_name )
 
211
{
 
212
    if( NULL == cipher_name )
 
213
        return NULL;
 
214
 
 
215
    /* Get the appropriate cipher information */
 
216
#if defined(POLARSSL_CAMELLIA_C)
 
217
    if( !strcasecmp( "CAMELLIA-128-CBC", cipher_name ) )
 
218
        return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CBC );
 
219
    if( !strcasecmp( "CAMELLIA-192-CBC", cipher_name ) )
 
220
        return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CBC );
 
221
    if( !strcasecmp( "CAMELLIA-256-CBC", cipher_name ) )
 
222
        return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CBC );
 
223
 
 
224
#if defined(POLARSSL_CIPHER_MODE_CFB)
 
225
    if( !strcasecmp( "CAMELLIA-128-CFB128", cipher_name ) )
 
226
        return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CFB128 );
 
227
    if( !strcasecmp( "CAMELLIA-192-CFB128", cipher_name ) )
 
228
        return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CFB128 );
 
229
    if( !strcasecmp( "CAMELLIA-256-CFB128", cipher_name ) )
 
230
        return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CFB128 );
 
231
#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
 
232
 
 
233
#if defined(POLARSSL_CIPHER_MODE_CTR)
 
234
    if( !strcasecmp( "CAMELLIA-128-CTR", cipher_name ) )
 
235
        return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_128_CTR );
 
236
    if( !strcasecmp( "CAMELLIA-192-CTR", cipher_name ) )
 
237
        return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_192_CTR );
 
238
    if( !strcasecmp( "CAMELLIA-256-CTR", cipher_name ) )
 
239
        return cipher_info_from_type( POLARSSL_CIPHER_CAMELLIA_256_CTR );
 
240
#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
 
241
#endif
 
242
 
 
243
#if defined(POLARSSL_AES_C)
 
244
    if( !strcasecmp( "AES-128-CBC", cipher_name ) )
 
245
        return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CBC );
 
246
    if( !strcasecmp( "AES-192-CBC", cipher_name ) )
 
247
        return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CBC );
 
248
    if( !strcasecmp( "AES-256-CBC", cipher_name ) )
 
249
        return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CBC );
 
250
 
 
251
#if defined(POLARSSL_CIPHER_MODE_CFB)
 
252
    if( !strcasecmp( "AES-128-CFB128", cipher_name ) )
 
253
        return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CFB128 );
 
254
    if( !strcasecmp( "AES-192-CFB128", cipher_name ) )
 
255
        return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CFB128 );
 
256
    if( !strcasecmp( "AES-256-CFB128", cipher_name ) )
 
257
        return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CFB128 );
 
258
#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
 
259
 
 
260
#if defined(POLARSSL_CIPHER_MODE_CTR)
 
261
    if( !strcasecmp( "AES-128-CTR", cipher_name ) )
 
262
        return cipher_info_from_type( POLARSSL_CIPHER_AES_128_CTR );
 
263
    if( !strcasecmp( "AES-192-CTR", cipher_name ) )
 
264
        return cipher_info_from_type( POLARSSL_CIPHER_AES_192_CTR );
 
265
    if( !strcasecmp( "AES-256-CTR", cipher_name ) )
 
266
        return cipher_info_from_type( POLARSSL_CIPHER_AES_256_CTR );
 
267
#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
 
268
#endif
 
269
 
 
270
#if defined(POLARSSL_DES_C)
 
271
    if( !strcasecmp( "DES-CBC", cipher_name ) )
 
272
        return cipher_info_from_type( POLARSSL_CIPHER_DES_CBC );
 
273
    if( !strcasecmp( "DES-EDE-CBC", cipher_name ) )
 
274
        return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE_CBC );
 
275
    if( !strcasecmp( "DES-EDE3-CBC", cipher_name ) )
 
276
        return cipher_info_from_type( POLARSSL_CIPHER_DES_EDE3_CBC );
 
277
#endif
 
278
 
 
279
#if defined(POLARSSL_BLOWFISH_C)
 
280
    if( !strcasecmp( "BLOWFISH-CBC", cipher_name ) )
 
281
        return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CBC );
 
282
 
 
283
#if defined(POLARSSL_CIPHER_MODE_CFB)
 
284
    if( !strcasecmp( "BLOWFISH-CFB64", cipher_name ) )
 
285
        return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CFB64 );
 
286
#endif /* defined(POLARSSL_CIPHER_MODE_CFB) */
 
287
 
 
288
#if defined(POLARSSL_CIPHER_MODE_CTR)
 
289
    if( !strcasecmp( "BLOWFISH-CTR", cipher_name ) )
 
290
        return cipher_info_from_type( POLARSSL_CIPHER_BLOWFISH_CTR );
 
291
#endif /* defined(POLARSSL_CIPHER_MODE_CTR) */
 
292
#endif
 
293
 
 
294
#if defined(POLARSSL_CIPHER_NULL_CIPHER)
 
295
    if( !strcasecmp( "NULL", cipher_name ) )
 
296
        return cipher_info_from_type( POLARSSL_CIPHER_NULL );
 
297
#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
 
298
 
 
299
    return NULL;
 
300
}
 
301
 
 
302
int cipher_init_ctx( cipher_context_t *ctx, const cipher_info_t *cipher_info )
 
303
{
 
304
    if( NULL == cipher_info || NULL == ctx )
 
305
        return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
 
306
 
 
307
    memset( ctx, 0, sizeof( cipher_context_t ) );
 
308
 
 
309
    if( NULL == ( ctx->cipher_ctx = cipher_info->base->ctx_alloc_func() ) )
 
310
        return POLARSSL_ERR_CIPHER_ALLOC_FAILED;
 
311
 
 
312
    ctx->cipher_info = cipher_info;
 
313
 
 
314
    return 0;
 
315
}
 
316
 
 
317
int cipher_free_ctx( cipher_context_t *ctx )
 
318
{
 
319
    if( ctx == NULL || ctx->cipher_info == NULL )
 
320
        return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
 
321
 
 
322
    ctx->cipher_info->base->ctx_free_func( ctx->cipher_ctx );
 
323
 
 
324
    return 0;
 
325
}
 
326
 
 
327
int cipher_setkey( cipher_context_t *ctx, const unsigned char *key,
 
328
        int key_length, const operation_t operation )
 
329
{
 
330
    if( NULL == ctx || NULL == ctx->cipher_info )
 
331
        return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
 
332
 
 
333
    ctx->key_length = key_length;
 
334
    ctx->operation = operation;
 
335
 
 
336
#if defined(POLARSSL_CIPHER_NULL_CIPHER)
 
337
    if( ctx->cipher_info->mode == POLARSSL_MODE_NULL )
 
338
        return 0;
 
339
#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
 
340
 
 
341
    /*
 
342
     * For CFB and CTR mode always use the encryption key schedule
 
343
     */
 
344
    if( POLARSSL_ENCRYPT == operation ||
 
345
        POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
 
346
        POLARSSL_MODE_CTR == ctx->cipher_info->mode )
 
347
    {
 
348
        return ctx->cipher_info->base->setkey_enc_func( ctx->cipher_ctx, key,
 
349
                ctx->key_length );
 
350
    }
 
351
 
 
352
    if( POLARSSL_DECRYPT == operation )
 
353
        return ctx->cipher_info->base->setkey_dec_func( ctx->cipher_ctx, key,
 
354
                ctx->key_length );
 
355
 
 
356
    return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
 
357
}
 
358
 
 
359
int cipher_reset( cipher_context_t *ctx, const unsigned char *iv )
 
360
{
 
361
    if( NULL == ctx || NULL == ctx->cipher_info || NULL == iv )
 
362
        return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
 
363
 
 
364
    ctx->unprocessed_len = 0;
 
365
 
 
366
    memcpy( ctx->iv, iv, cipher_get_iv_size( ctx ) );
 
367
 
 
368
    return 0;
 
369
}
 
370
 
 
371
int cipher_update( cipher_context_t *ctx, const unsigned char *input, size_t ilen,
 
372
        unsigned char *output, size_t *olen )
 
373
{
 
374
    int ret;
 
375
    size_t copy_len = 0;
 
376
 
 
377
    if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen ||
 
378
        input == output )
 
379
    {
 
380
        return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
 
381
    }
 
382
 
 
383
    *olen = 0;
 
384
 
 
385
#if defined(POLARSSL_CIPHER_NULL_CIPHER)
 
386
    if( ctx->cipher_info->mode == POLARSSL_MODE_NULL )
 
387
    {
 
388
        memcpy( output, input, ilen );
 
389
        *olen = ilen;
 
390
        return 0;
 
391
    }
 
392
#endif /* defined(POLARSSL_CIPHER_NULL_CIPHER) */
 
393
 
 
394
    if( ctx->cipher_info->mode == POLARSSL_MODE_CBC )
 
395
    {
 
396
        /*
 
397
         * If there is not enough data for a full block, cache it.
 
398
         */
 
399
        if( ( ctx->operation == POLARSSL_DECRYPT &&
 
400
                ilen + ctx->unprocessed_len <= cipher_get_block_size( ctx ) ) ||
 
401
             ( ctx->operation == POLARSSL_ENCRYPT &&
 
402
                ilen + ctx->unprocessed_len < cipher_get_block_size( ctx ) ) )
 
403
        {
 
404
            memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
 
405
                    ilen );
 
406
 
 
407
            ctx->unprocessed_len += ilen;
 
408
            return 0;
 
409
        }
 
410
 
 
411
        /*
 
412
         * Process cached data first
 
413
         */
 
414
        if( ctx->unprocessed_len != 0 )
 
415
        {
 
416
            copy_len = cipher_get_block_size( ctx ) - ctx->unprocessed_len;
 
417
 
 
418
            memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ), input,
 
419
                    copy_len );
 
420
 
 
421
            if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
 
422
                    ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
 
423
                    ctx->unprocessed_data, output ) ) )
 
424
            {
 
425
                return ret;
 
426
            }
 
427
 
 
428
            *olen += cipher_get_block_size( ctx );
 
429
            output += cipher_get_block_size( ctx );
 
430
            ctx->unprocessed_len = 0;
 
431
 
 
432
            input += copy_len;
 
433
            ilen -= copy_len;
 
434
        }
 
435
 
 
436
        /*
 
437
         * Cache final, incomplete block
 
438
         */
 
439
        if( 0 != ilen )
 
440
        {
 
441
            copy_len = ilen % cipher_get_block_size( ctx );
 
442
            if( copy_len == 0 && ctx->operation == POLARSSL_DECRYPT )
 
443
                copy_len = cipher_get_block_size(ctx);
 
444
 
 
445
            memcpy( ctx->unprocessed_data, &( input[ilen - copy_len] ),
 
446
                    copy_len );
 
447
 
 
448
            ctx->unprocessed_len += copy_len;
 
449
            ilen -= copy_len;
 
450
        }
 
451
 
 
452
        /*
 
453
         * Process remaining full blocks
 
454
         */
 
455
        if( ilen )
 
456
        {
 
457
            if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
 
458
                    ctx->operation, ilen, ctx->iv, input, output ) ) )
 
459
            {
 
460
                return ret;
 
461
            }
 
462
            *olen += ilen;
 
463
        }
 
464
 
 
465
        return 0;
 
466
    }
 
467
 
 
468
    if( ctx->cipher_info->mode == POLARSSL_MODE_CFB )
 
469
    {
 
470
        if( 0 != ( ret = ctx->cipher_info->base->cfb_func( ctx->cipher_ctx,
 
471
                ctx->operation, ilen, &ctx->unprocessed_len, ctx->iv,
 
472
                input, output ) ) )
 
473
        {
 
474
            return ret;
 
475
        }
 
476
 
 
477
        *olen = ilen;
 
478
 
 
479
        return 0;
 
480
    }
 
481
 
 
482
    if( ctx->cipher_info->mode == POLARSSL_MODE_CTR )
 
483
    {
 
484
        if( 0 != ( ret = ctx->cipher_info->base->ctr_func( ctx->cipher_ctx,
 
485
                ilen, &ctx->unprocessed_len, ctx->iv,
 
486
                ctx->unprocessed_data, input, output ) ) )
 
487
        {
 
488
            return ret;
 
489
        }
 
490
 
 
491
        *olen = ilen;
 
492
 
 
493
        return 0;
 
494
    }
 
495
 
 
496
    return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
 
497
}
 
498
 
 
499
static void add_pkcs_padding( unsigned char *output, size_t output_len,
 
500
        size_t data_len )
 
501
{
 
502
    size_t padding_len = output_len - data_len;
 
503
    unsigned char i = 0;
 
504
 
 
505
    for( i = 0; i < padding_len; i++ )
 
506
        output[data_len + i] = (unsigned char) padding_len;
 
507
}
 
508
 
 
509
static int get_pkcs_padding( unsigned char *input, unsigned int input_len,
 
510
        size_t *data_len)
 
511
{
 
512
    unsigned int i, padding_len = 0;
 
513
 
 
514
    if( NULL == input || NULL == data_len )
 
515
        return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
 
516
 
 
517
    padding_len = input[input_len - 1];
 
518
 
 
519
    if( padding_len > input_len )
 
520
        return POLARSSL_ERR_CIPHER_INVALID_PADDING;
 
521
 
 
522
    for( i = input_len - padding_len; i < input_len; i++ )
 
523
        if( input[i] != padding_len )
 
524
            return POLARSSL_ERR_CIPHER_INVALID_PADDING;
 
525
 
 
526
    *data_len = input_len - padding_len;
 
527
 
 
528
    return 0;
 
529
}
 
530
 
 
531
int cipher_finish( cipher_context_t *ctx, unsigned char *output, size_t *olen)
 
532
{
 
533
    int ret = 0;
 
534
 
 
535
    if( NULL == ctx || NULL == ctx->cipher_info || NULL == olen )
 
536
        return POLARSSL_ERR_CIPHER_BAD_INPUT_DATA;
 
537
 
 
538
    *olen = 0;
 
539
 
 
540
    if( POLARSSL_MODE_CFB == ctx->cipher_info->mode ||
 
541
        POLARSSL_MODE_CTR == ctx->cipher_info->mode ||
 
542
        POLARSSL_MODE_NULL == ctx->cipher_info->mode )
 
543
    {
 
544
        return 0;
 
545
    }
 
546
 
 
547
    if( POLARSSL_MODE_CBC == ctx->cipher_info->mode )
 
548
    {
 
549
        if( POLARSSL_ENCRYPT == ctx->operation )
 
550
        {
 
551
            add_pkcs_padding( ctx->unprocessed_data, cipher_get_iv_size( ctx ),
 
552
                    ctx->unprocessed_len );
 
553
        }
 
554
        else if ( cipher_get_block_size( ctx ) != ctx->unprocessed_len )
 
555
        {
 
556
            /* For decrypt operations, expect a full block */
 
557
            return POLARSSL_ERR_CIPHER_FULL_BLOCK_EXPECTED;
 
558
        }
 
559
 
 
560
        /* cipher block */
 
561
        if( 0 != ( ret = ctx->cipher_info->base->cbc_func( ctx->cipher_ctx,
 
562
                ctx->operation, cipher_get_block_size( ctx ), ctx->iv,
 
563
                ctx->unprocessed_data, output ) ) )
 
564
        {
 
565
            return ret;
 
566
        }
 
567
 
 
568
        /* Set output size for decryption */
 
569
        if( POLARSSL_DECRYPT == ctx->operation )
 
570
            return get_pkcs_padding( output, cipher_get_block_size( ctx ), olen );
 
571
 
 
572
        /* Set output size for encryption */
 
573
        *olen = cipher_get_block_size( ctx );
 
574
        return 0;
 
575
    }
 
576
 
 
577
    return POLARSSL_ERR_CIPHER_FEATURE_UNAVAILABLE;
 
578
}
 
579
 
 
580
#if defined(POLARSSL_SELF_TEST)
 
581
 
 
582
#include <stdio.h>
 
583
 
 
584
#define ASSERT(x) if (!(x)) { \
 
585
        printf( "failed with %i at %s\n", value, (#x) ); \
 
586
    return( 1 ); \
 
587
}
 
588
/*
 
589
 * Checkup routine
 
590
 */
 
591
 
 
592
int cipher_self_test( int verbose )
 
593
{
 
594
    ((void) verbose);
 
595
 
 
596
    return( 0 );
 
597
}
 
598
 
 
599
#endif
 
600
 
 
601
#endif