cipher_wrap.c 35.3 KB
Newer Older
1
/**
2
 * \file cipher_wrap.c
3
 *
4
 * \brief Generic cipher wrapper for mbed TLS
5 6 7
 *
 * \author Adriaan de Jong <dejong@fox-it.com>
 *
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
8
 *  Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
9
 *
10
 *  This file is part of mbed TLS (https://tls.mbed.org)
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
27
#if !defined(MBEDTLS_CONFIG_FILE)
28
#include "mbedtls/config.h"
29
#else
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
30
#include MBEDTLS_CONFIG_FILE
31
#endif
32

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
33
#if defined(MBEDTLS_CIPHER_C)
34

35
#include "mbedtls/cipher_internal.h"
36

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
37
#if defined(MBEDTLS_AES_C)
38
#include "mbedtls/aes.h"
39 40
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
41
#if defined(MBEDTLS_ARC4_C)
42
#include "mbedtls/arc4.h"
43 44
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
45
#if defined(MBEDTLS_CAMELLIA_C)
46
#include "mbedtls/camellia.h"
47 48
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
49
#if defined(MBEDTLS_DES_C)
50
#include "mbedtls/des.h"
Paul Bakker's avatar
Paul Bakker committed
51
#endif
52

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
53
#if defined(MBEDTLS_BLOWFISH_C)
54
#include "mbedtls/blowfish.h"
55 56
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
57
#if defined(MBEDTLS_GCM_C)
58
#include "mbedtls/gcm.h"
59 60
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
61
#if defined(MBEDTLS_CCM_C)
62
#include "mbedtls/ccm.h"
63 64
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
65
#if defined(MBEDTLS_CIPHER_NULL_CIPHER)
66 67 68
#include <string.h>
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
69
#if defined(MBEDTLS_PLATFORM_C)
70
#include "mbedtls/platform.h"
71
#else
72
#include <stdlib.h>
73
#define mbedtls_calloc    calloc
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
74
#define mbedtls_free       free
75 76
#endif

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
77
#if defined(MBEDTLS_GCM_C)
78 79 80
/* shared by all GCM ciphers */
static void *gcm_ctx_alloc( void )
{
81 82 83 84 85 86
    void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_gcm_context ) );

    if( ctx != NULL )
        mbedtls_gcm_init( (mbedtls_gcm_context *) ctx );

    return( ctx );
87 88 89 90
}

static void gcm_ctx_free( void *ctx )
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
91 92
    mbedtls_gcm_free( ctx );
    mbedtls_free( ctx );
93
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
94
#endif /* MBEDTLS_GCM_C */
95

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
96
#if defined(MBEDTLS_CCM_C)
97 98 99
/* shared by all CCM ciphers */
static void *ccm_ctx_alloc( void )
{
100 101 102 103 104 105
    void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ccm_context ) );

    if( ctx != NULL )
        mbedtls_ccm_init( (mbedtls_ccm_context *) ctx );

    return( ctx );
106 107 108 109
}

static void ccm_ctx_free( void *ctx )
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
110 111
    mbedtls_ccm_free( ctx );
    mbedtls_free( ctx );
112
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
113
#endif /* MBEDTLS_CCM_C */
114

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
115
#if defined(MBEDTLS_AES_C)
116

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
117
static int aes_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
118 119
        const unsigned char *input, unsigned char *output )
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
120
    return mbedtls_aes_crypt_ecb( (mbedtls_aes_context *) ctx, operation, input, output );
121 122
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
123 124
#if defined(MBEDTLS_CIPHER_MODE_CBC)
static int aes_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
125 126
        unsigned char *iv, const unsigned char *input, unsigned char *output )
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
127
    return mbedtls_aes_crypt_cbc( (mbedtls_aes_context *) ctx, operation, length, iv, input,
128
                          output );
129
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
130
#endif /* MBEDTLS_CIPHER_MODE_CBC */
131

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
132 133
#if defined(MBEDTLS_CIPHER_MODE_CFB)
static int aes_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
134 135
        size_t length, size_t *iv_off, unsigned char *iv,
        const unsigned char *input, unsigned char *output )
136
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
137
    return mbedtls_aes_crypt_cfb128( (mbedtls_aes_context *) ctx, operation, length, iv_off, iv,
138
                             input, output );
139
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
140
#endif /* MBEDTLS_CIPHER_MODE_CFB */
141

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
142
#if defined(MBEDTLS_CIPHER_MODE_CTR)
143 144
static int aes_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
        unsigned char *nonce_counter, unsigned char *stream_block,
145 146
        const unsigned char *input, unsigned char *output )
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
147
    return mbedtls_aes_crypt_ctr( (mbedtls_aes_context *) ctx, length, nc_off, nonce_counter,
148 149
                          stream_block, input, output );
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
150
#endif /* MBEDTLS_CIPHER_MODE_CTR */
151

152
static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
153
                                unsigned int key_bitlen )
154
{
155
    return mbedtls_aes_setkey_dec( (mbedtls_aes_context *) ctx, key, key_bitlen );
156 157
}

158
static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
159
                                unsigned int key_bitlen )
160
{
161
    return mbedtls_aes_setkey_enc( (mbedtls_aes_context *) ctx, key, key_bitlen );
162 163 164 165
}

static void * aes_ctx_alloc( void )
{
166
    mbedtls_aes_context *aes = mbedtls_calloc( 1, sizeof( mbedtls_aes_context ) );
167 168 169 170

    if( aes == NULL )
        return( NULL );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
171
    mbedtls_aes_init( aes );
172 173

    return( aes );
174 175 176 177
}

static void aes_ctx_free( void *ctx )
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
178 179
    mbedtls_aes_free( (mbedtls_aes_context *) ctx );
    mbedtls_free( ctx );
180 181
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
182 183
static const mbedtls_cipher_base_t aes_info = {
    MBEDTLS_CIPHER_ID_AES,
184
    aes_crypt_ecb_wrap,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
185
#if defined(MBEDTLS_CIPHER_MODE_CBC)
186
    aes_crypt_cbc_wrap,
187
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
188
#if defined(MBEDTLS_CIPHER_MODE_CFB)
189
    aes_crypt_cfb128_wrap,
190
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
191
#if defined(MBEDTLS_CIPHER_MODE_CTR)
192
    aes_crypt_ctr_wrap,
193
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
194
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
195
    NULL,
196
#endif
197 198 199 200 201 202
    aes_setkey_enc_wrap,
    aes_setkey_dec_wrap,
    aes_ctx_alloc,
    aes_ctx_free
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
203 204 205
static const mbedtls_cipher_info_t aes_128_ecb_info = {
    MBEDTLS_CIPHER_AES_128_ECB,
    MBEDTLS_MODE_ECB,
206 207 208 209 210 211 212 213
    128,
    "AES-128-ECB",
    16,
    0,
    16,
    &aes_info
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
214 215 216
static const mbedtls_cipher_info_t aes_192_ecb_info = {
    MBEDTLS_CIPHER_AES_192_ECB,
    MBEDTLS_MODE_ECB,
217 218 219 220 221 222 223 224
    192,
    "AES-192-ECB",
    16,
    0,
    16,
    &aes_info
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
225 226 227
static const mbedtls_cipher_info_t aes_256_ecb_info = {
    MBEDTLS_CIPHER_AES_256_ECB,
    MBEDTLS_MODE_ECB,
228 229 230 231 232 233 234 235
    256,
    "AES-256-ECB",
    16,
    0,
    16,
    &aes_info
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
236 237 238 239
#if defined(MBEDTLS_CIPHER_MODE_CBC)
static const mbedtls_cipher_info_t aes_128_cbc_info = {
    MBEDTLS_CIPHER_AES_128_CBC,
    MBEDTLS_MODE_CBC,
240 241 242
    128,
    "AES-128-CBC",
    16,
243
    0,
244
    16,
245
    &aes_info
246 247
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
248 249 250
static const mbedtls_cipher_info_t aes_192_cbc_info = {
    MBEDTLS_CIPHER_AES_192_CBC,
    MBEDTLS_MODE_CBC,
251 252 253
    192,
    "AES-192-CBC",
    16,
254
    0,
255
    16,
256
    &aes_info
257 258
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
259 260 261
static const mbedtls_cipher_info_t aes_256_cbc_info = {
    MBEDTLS_CIPHER_AES_256_CBC,
    MBEDTLS_MODE_CBC,
262 263 264
    256,
    "AES-256-CBC",
    16,
265
    0,
266
    16,
267 268
    &aes_info
};
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
269
#endif /* MBEDTLS_CIPHER_MODE_CBC */
270

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
271 272 273 274
#if defined(MBEDTLS_CIPHER_MODE_CFB)
static const mbedtls_cipher_info_t aes_128_cfb128_info = {
    MBEDTLS_CIPHER_AES_128_CFB128,
    MBEDTLS_MODE_CFB,
275 276 277
    128,
    "AES-128-CFB128",
    16,
278
    0,
279 280 281 282
    16,
    &aes_info
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
283 284 285
static const mbedtls_cipher_info_t aes_192_cfb128_info = {
    MBEDTLS_CIPHER_AES_192_CFB128,
    MBEDTLS_MODE_CFB,
286 287 288
    192,
    "AES-192-CFB128",
    16,
289
    0,
290 291 292 293
    16,
    &aes_info
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
294 295 296
static const mbedtls_cipher_info_t aes_256_cfb128_info = {
    MBEDTLS_CIPHER_AES_256_CFB128,
    MBEDTLS_MODE_CFB,
297 298 299
    256,
    "AES-256-CFB128",
    16,
300
    0,
301 302 303
    16,
    &aes_info
};
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
304
#endif /* MBEDTLS_CIPHER_MODE_CFB */
305

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
306 307 308 309
#if defined(MBEDTLS_CIPHER_MODE_CTR)
static const mbedtls_cipher_info_t aes_128_ctr_info = {
    MBEDTLS_CIPHER_AES_128_CTR,
    MBEDTLS_MODE_CTR,
310 311 312
    128,
    "AES-128-CTR",
    16,
313
    0,
314 315
    16,
    &aes_info
316
};
317

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
318 319 320
static const mbedtls_cipher_info_t aes_192_ctr_info = {
    MBEDTLS_CIPHER_AES_192_CTR,
    MBEDTLS_MODE_CTR,
321 322 323
    192,
    "AES-192-CTR",
    16,
324
    0,
325 326 327 328
    16,
    &aes_info
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
329 330 331
static const mbedtls_cipher_info_t aes_256_ctr_info = {
    MBEDTLS_CIPHER_AES_256_CTR,
    MBEDTLS_MODE_CTR,
332 333 334
    256,
    "AES-256-CTR",
    16,
335
    0,
336 337 338
    16,
    &aes_info
};
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
339
#endif /* MBEDTLS_CIPHER_MODE_CTR */
340

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
341
#if defined(MBEDTLS_GCM_C)
342
static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key,
343
                                unsigned int key_bitlen )
344
{
345
    return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
346
                     key, key_bitlen );
347 348
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
349 350
static const mbedtls_cipher_base_t gcm_aes_info = {
    MBEDTLS_CIPHER_ID_AES,
351
    NULL,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
352
#if defined(MBEDTLS_CIPHER_MODE_CBC)
353
    NULL,
354
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
355
#if defined(MBEDTLS_CIPHER_MODE_CFB)
356
    NULL,
357
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
358
#if defined(MBEDTLS_CIPHER_MODE_CTR)
359
    NULL,
360
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
361
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
362
    NULL,
363
#endif
364 365
    gcm_aes_setkey_wrap,
    gcm_aes_setkey_wrap,
366 367 368 369
    gcm_ctx_alloc,
    gcm_ctx_free,
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
370 371 372
static const mbedtls_cipher_info_t aes_128_gcm_info = {
    MBEDTLS_CIPHER_AES_128_GCM,
    MBEDTLS_MODE_GCM,
373 374
    128,
    "AES-128-GCM",
375
    12,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
376
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
377
    16,
378
    &gcm_aes_info
379 380
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
381 382 383
static const mbedtls_cipher_info_t aes_192_gcm_info = {
    MBEDTLS_CIPHER_AES_192_GCM,
    MBEDTLS_MODE_GCM,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
384 385 386
    192,
    "AES-192-GCM",
    12,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
387
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
388 389 390 391
    16,
    &gcm_aes_info
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
392 393 394
static const mbedtls_cipher_info_t aes_256_gcm_info = {
    MBEDTLS_CIPHER_AES_256_GCM,
    MBEDTLS_MODE_GCM,
395 396
    256,
    "AES-256-GCM",
397
    12,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
398
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
399
    16,
400
    &gcm_aes_info
401
};
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
402
#endif /* MBEDTLS_GCM_C */
403

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
404
#if defined(MBEDTLS_CCM_C)
405
static int ccm_aes_setkey_wrap( void *ctx, const unsigned char *key,
406
                                unsigned int key_bitlen )
407
{
408
    return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
409
                     key, key_bitlen );
410 411
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
412 413
static const mbedtls_cipher_base_t ccm_aes_info = {
    MBEDTLS_CIPHER_ID_AES,
414
    NULL,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
415
#if defined(MBEDTLS_CIPHER_MODE_CBC)
416
    NULL,
417
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
418
#if defined(MBEDTLS_CIPHER_MODE_CFB)
419
    NULL,
420
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
421
#if defined(MBEDTLS_CIPHER_MODE_CTR)
422
    NULL,
423
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
424
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
425
    NULL,
426
#endif
427 428 429 430 431 432
    ccm_aes_setkey_wrap,
    ccm_aes_setkey_wrap,
    ccm_ctx_alloc,
    ccm_ctx_free,
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
433 434 435
static const mbedtls_cipher_info_t aes_128_ccm_info = {
    MBEDTLS_CIPHER_AES_128_CCM,
    MBEDTLS_MODE_CCM,
436 437 438
    128,
    "AES-128-CCM",
    12,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
439
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
440 441 442 443
    16,
    &ccm_aes_info
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
444 445 446
static const mbedtls_cipher_info_t aes_192_ccm_info = {
    MBEDTLS_CIPHER_AES_192_CCM,
    MBEDTLS_MODE_CCM,
447 448 449
    192,
    "AES-192-CCM",
    12,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
450
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
451 452 453 454
    16,
    &ccm_aes_info
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
455 456 457
static const mbedtls_cipher_info_t aes_256_ccm_info = {
    MBEDTLS_CIPHER_AES_256_CCM,
    MBEDTLS_MODE_CCM,
458 459 460
    256,
    "AES-256-CCM",
    12,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
461
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
462 463 464
    16,
    &ccm_aes_info
};
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
465
#endif /* MBEDTLS_CCM_C */
466

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
467
#endif /* MBEDTLS_AES_C */
468

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
469
#if defined(MBEDTLS_CAMELLIA_C)
470

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
471
static int camellia_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
472 473
        const unsigned char *input, unsigned char *output )
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
474
    return mbedtls_camellia_crypt_ecb( (mbedtls_camellia_context *) ctx, operation, input,
475
                               output );
476 477
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
478 479
#if defined(MBEDTLS_CIPHER_MODE_CBC)
static int camellia_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
480 481
        size_t length, unsigned char *iv,
        const unsigned char *input, unsigned char *output )
482
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
483
    return mbedtls_camellia_crypt_cbc( (mbedtls_camellia_context *) ctx, operation, length, iv,
484
                               input, output );
485
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
486
#endif /* MBEDTLS_CIPHER_MODE_CBC */
487

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
488 489
#if defined(MBEDTLS_CIPHER_MODE_CFB)
static int camellia_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
490 491
        size_t length, size_t *iv_off, unsigned char *iv,
        const unsigned char *input, unsigned char *output )
492
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
493
    return mbedtls_camellia_crypt_cfb128( (mbedtls_camellia_context *) ctx, operation, length,
494
                                  iv_off, iv, input, output );
495
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
496
#endif /* MBEDTLS_CIPHER_MODE_CFB */
497

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
498
#if defined(MBEDTLS_CIPHER_MODE_CTR)
499 500
static int camellia_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
        unsigned char *nonce_counter, unsigned char *stream_block,
501 502
        const unsigned char *input, unsigned char *output )
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
503
    return mbedtls_camellia_crypt_ctr( (mbedtls_camellia_context *) ctx, length, nc_off,
504
                               nonce_counter, stream_block, input, output );
505
}
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
506
#endif /* MBEDTLS_CIPHER_MODE_CTR */
507

508
static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key,
509
                                     unsigned int key_bitlen )
510
{
511
    return mbedtls_camellia_setkey_dec( (mbedtls_camellia_context *) ctx, key, key_bitlen );
512 513
}

514
static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key,
515
                                     unsigned int key_bitlen )
516
{
517
    return mbedtls_camellia_setkey_enc( (mbedtls_camellia_context *) ctx, key, key_bitlen );
518 519 520 521
}

static void * camellia_ctx_alloc( void )
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
522
    mbedtls_camellia_context *ctx;
523
    ctx = mbedtls_calloc( 1, sizeof( mbedtls_camellia_context ) );
524 525 526 527

    if( ctx == NULL )
        return( NULL );

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
528
    mbedtls_camellia_init( ctx );
529 530

    return( ctx );
531 532 533 534
}

static void camellia_ctx_free( void *ctx )
{
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
535 536
    mbedtls_camellia_free( (mbedtls_camellia_context *) ctx );
    mbedtls_free( ctx );
537 538
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
539 540
static const mbedtls_cipher_base_t camellia_info = {
    MBEDTLS_CIPHER_ID_CAMELLIA,
541
    camellia_crypt_ecb_wrap,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
542
#if defined(MBEDTLS_CIPHER_MODE_CBC)
543
    camellia_crypt_cbc_wrap,
544
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
545
#if defined(MBEDTLS_CIPHER_MODE_CFB)
546
    camellia_crypt_cfb128_wrap,
547
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
548
#if defined(MBEDTLS_CIPHER_MODE_CTR)
549
    camellia_crypt_ctr_wrap,
550
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
551
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
552
    NULL,
553
#endif
554 555 556 557 558 559
    camellia_setkey_enc_wrap,
    camellia_setkey_dec_wrap,
    camellia_ctx_alloc,
    camellia_ctx_free
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
560 561 562
static const mbedtls_cipher_info_t camellia_128_ecb_info = {
    MBEDTLS_CIPHER_CAMELLIA_128_ECB,
    MBEDTLS_MODE_ECB,
563 564 565 566 567 568 569 570
    128,
    "CAMELLIA-128-ECB",
    16,
    0,
    16,
    &camellia_info
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
571 572 573
static const mbedtls_cipher_info_t camellia_192_ecb_info = {
    MBEDTLS_CIPHER_CAMELLIA_192_ECB,
    MBEDTLS_MODE_ECB,
574 575 576 577 578 579 580 581
    192,
    "CAMELLIA-192-ECB",
    16,
    0,
    16,
    &camellia_info
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
582 583 584
static const mbedtls_cipher_info_t camellia_256_ecb_info = {
    MBEDTLS_CIPHER_CAMELLIA_256_ECB,
    MBEDTLS_MODE_ECB,
585 586 587 588 589 590 591 592
    256,
    "CAMELLIA-256-ECB",
    16,
    0,
    16,
    &camellia_info
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
593 594 595 596
#if defined(MBEDTLS_CIPHER_MODE_CBC)
static const mbedtls_cipher_info_t camellia_128_cbc_info = {
    MBEDTLS_CIPHER_CAMELLIA_128_CBC,
    MBEDTLS_MODE_CBC,
597 598 599
    128,
    "CAMELLIA-128-CBC",
    16,
600
    0,
601
    16,
602
    &camellia_info
603 604
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
605 606 607
static const mbedtls_cipher_info_t camellia_192_cbc_info = {
    MBEDTLS_CIPHER_CAMELLIA_192_CBC,
    MBEDTLS_MODE_CBC,
608 609 610
    192,
    "CAMELLIA-192-CBC",
    16,
611
    0,
612
    16,
613
    &camellia_info
614 615
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
616 617 618
static const mbedtls_cipher_info_t camellia_256_cbc_info = {
    MBEDTLS_CIPHER_CAMELLIA_256_CBC,
    MBEDTLS_MODE_CBC,
619 620 621
    256,
    "CAMELLIA-256-CBC",
    16,
622
    0,
623
    16,
624 625
    &camellia_info
};
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
626
#endif /* MBEDTLS_CIPHER_MODE_CBC */
627

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
628 629 630 631
#if defined(MBEDTLS_CIPHER_MODE_CFB)
static const mbedtls_cipher_info_t camellia_128_cfb128_info = {
    MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
    MBEDTLS_MODE_CFB,
632 633 634
    128,
    "CAMELLIA-128-CFB128",
    16,
635
    0,
636 637 638 639
    16,
    &camellia_info
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
640 641 642
static const mbedtls_cipher_info_t camellia_192_cfb128_info = {
    MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
    MBEDTLS_MODE_CFB,
643 644 645
    192,
    "CAMELLIA-192-CFB128",
    16,
646
    0,
647 648 649 650
    16,
    &camellia_info
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
651 652 653
static const mbedtls_cipher_info_t camellia_256_cfb128_info = {
    MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
    MBEDTLS_MODE_CFB,
654 655 656
    256,
    "CAMELLIA-256-CFB128",
    16,
657
    0,
658 659 660
    16,
    &camellia_info
};
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
661
#endif /* MBEDTLS_CIPHER_MODE_CFB */
662

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
663 664 665 666
#if defined(MBEDTLS_CIPHER_MODE_CTR)
static const mbedtls_cipher_info_t camellia_128_ctr_info = {
    MBEDTLS_CIPHER_CAMELLIA_128_CTR,
    MBEDTLS_MODE_CTR,
667 668 669
    128,
    "CAMELLIA-128-CTR",
    16,
670
    0,
671 672 673 674
    16,
    &camellia_info
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
675 676 677
static const mbedtls_cipher_info_t camellia_192_ctr_info = {
    MBEDTLS_CIPHER_CAMELLIA_192_CTR,
    MBEDTLS_MODE_CTR,
678 679 680
    192,
    "CAMELLIA-192-CTR",
    16,
681
    0,
682 683 684 685
    16,
    &camellia_info
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
686 687 688
static const mbedtls_cipher_info_t camellia_256_ctr_info = {
    MBEDTLS_CIPHER_CAMELLIA_256_CTR,
    MBEDTLS_MODE_CTR,
689 690 691
    256,
    "CAMELLIA-256-CTR",
    16,
692
    0,
693 694
    16,
    &camellia_info
695
};
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
696
#endif /* MBEDTLS_CIPHER_MODE_CTR */
697

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
698
#if defined(MBEDTLS_GCM_C)
699
static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
700
                                     unsigned int key_bitlen )
701
{
702
    return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
703
                     key, key_bitlen );
704 705
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
706 707
static const mbedtls_cipher_base_t gcm_camellia_info = {
    MBEDTLS_CIPHER_ID_CAMELLIA,
708
    NULL,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
709
#if defined(MBEDTLS_CIPHER_MODE_CBC)
710
    NULL,
711
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
712
#if defined(MBEDTLS_CIPHER_MODE_CFB)
713
    NULL,
714
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
715
#if defined(MBEDTLS_CIPHER_MODE_CTR)
716
    NULL,
717
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
718
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
719
    NULL,
720
#endif
721 722 723 724 725 726
    gcm_camellia_setkey_wrap,
    gcm_camellia_setkey_wrap,
    gcm_ctx_alloc,
    gcm_ctx_free,
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
727 728 729
static const mbedtls_cipher_info_t camellia_128_gcm_info = {
    MBEDTLS_CIPHER_CAMELLIA_128_GCM,
    MBEDTLS_MODE_GCM,
730 731 732
    128,
    "CAMELLIA-128-GCM",
    12,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
733
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
734 735 736 737
    16,
    &gcm_camellia_info
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
738 739 740
static const mbedtls_cipher_info_t camellia_192_gcm_info = {
    MBEDTLS_CIPHER_CAMELLIA_192_GCM,
    MBEDTLS_MODE_GCM,
741 742 743
    192,
    "CAMELLIA-192-GCM",
    12,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
744
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
745 746 747 748
    16,
    &gcm_camellia_info
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
749 750 751
static const mbedtls_cipher_info_t camellia_256_gcm_info = {
    MBEDTLS_CIPHER_CAMELLIA_256_GCM,
    MBEDTLS_MODE_GCM,
752 753 754
    256,
    "CAMELLIA-256-GCM",
    12,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
755
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
756 757 758
    16,
    &gcm_camellia_info
};
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
759
#endif /* MBEDTLS_GCM_C */
760

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
761
#if defined(MBEDTLS_CCM_C)
762
static int ccm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
763
                                     unsigned int key_bitlen )
764
{
765
    return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
766
                     key, key_bitlen );
767 768
}

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
769 770
static const mbedtls_cipher_base_t ccm_camellia_info = {
    MBEDTLS_CIPHER_ID_CAMELLIA,
771
    NULL,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
772
#if defined(MBEDTLS_CIPHER_MODE_CBC)
773
    NULL,
774
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
775
#if defined(MBEDTLS_CIPHER_MODE_CFB)
776
    NULL,
777
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
778
#if defined(MBEDTLS_CIPHER_MODE_CTR)
779
    NULL,
780
#endif
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
781
#if defined(MBEDTLS_CIPHER_MODE_STREAM)
782
    NULL,
783
#endif
784 785 786 787 788 789
    ccm_camellia_setkey_wrap,
    ccm_camellia_setkey_wrap,
    ccm_ctx_alloc,
    ccm_ctx_free,
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
790 791 792
static const mbedtls_cipher_info_t camellia_128_ccm_info = {
    MBEDTLS_CIPHER_CAMELLIA_128_CCM,
    MBEDTLS_MODE_CCM,
793 794 795
    128,
    "CAMELLIA-128-CCM",
    12,
Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
796
    MBEDTLS_CIPHER_VARIABLE_IV_LEN,
797 798 799 800
    16,
    &ccm_camellia_info
};

Manuel Pégourié-Gonnard's avatar
Manuel Pégourié-Gonnard committed
801 802 803
static const mbedtls_cipher_info_t