takashi kadono / Mbed OS Nucleo_446

Dependencies:   ssd1331

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers cipher_wrap.c Source File

cipher_wrap.c

Go to the documentation of this file.
00001 /**
00002  * \file cipher_wrap.c
00003  *
00004  * \brief Generic cipher wrapper for mbed TLS
00005  *
00006  * \author Adriaan de Jong <dejong@fox-it.com>
00007  *
00008  *  Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
00009  *  SPDX-License-Identifier: Apache-2.0
00010  *
00011  *  Licensed under the Apache License, Version 2.0 (the "License"); you may
00012  *  not use this file except in compliance with the License.
00013  *  You may obtain a copy of the License at
00014  *
00015  *  http://www.apache.org/licenses/LICENSE-2.0
00016  *
00017  *  Unless required by applicable law or agreed to in writing, software
00018  *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00019  *  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020  *  See the License for the specific language governing permissions and
00021  *  limitations under the License.
00022  *
00023  *  This file is part of mbed TLS (https://tls.mbed.org)
00024  */
00025 
00026 #if !defined(MBEDTLS_CONFIG_FILE)
00027 #include "mbedtls/config.h"
00028 #else
00029 #include MBEDTLS_CONFIG_FILE
00030 #endif
00031 
00032 #if defined(MBEDTLS_CIPHER_C)
00033 
00034 #include "mbedtls/cipher_internal.h"
00035 
00036 #if defined(MBEDTLS_CHACHAPOLY_C)
00037 #include "mbedtls/chachapoly.h"
00038 #endif
00039 
00040 #if defined(MBEDTLS_AES_C)
00041 #include "mbedtls/aes.h"
00042 #endif
00043 
00044 #if defined(MBEDTLS_ARC4_C)
00045 #include "mbedtls/arc4.h"
00046 #endif
00047 
00048 #if defined(MBEDTLS_CAMELLIA_C)
00049 #include "mbedtls/camellia.h"
00050 #endif
00051 
00052 #if defined(MBEDTLS_ARIA_C)
00053 #include "mbedtls/aria.h"
00054 #endif
00055 
00056 #if defined(MBEDTLS_DES_C)
00057 #include "mbedtls/des.h"
00058 #endif
00059 
00060 #if defined(MBEDTLS_BLOWFISH_C)
00061 #include "mbedtls/blowfish.h"
00062 #endif
00063 
00064 #if defined(MBEDTLS_CHACHA20_C)
00065 #include "mbedtls/chacha20.h"
00066 #endif
00067 
00068 #if defined(MBEDTLS_GCM_C)
00069 #include "mbedtls/gcm.h"
00070 #endif
00071 
00072 #if defined(MBEDTLS_CCM_C)
00073 #include "mbedtls/ccm.h"
00074 #endif
00075 
00076 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
00077 #include <string.h>
00078 #endif
00079 
00080 #if defined(MBEDTLS_PLATFORM_C)
00081 #include "mbedtls/platform.h"
00082 #else
00083 #include <stdlib.h>
00084 #define mbedtls_calloc    calloc
00085 #define mbedtls_free       free
00086 #endif
00087 
00088 #if defined(MBEDTLS_GCM_C)
00089 /* shared by all GCM ciphers */
00090 static void *gcm_ctx_alloc( void )
00091 {
00092     void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_gcm_context ) );
00093 
00094     if( ctx != NULL )
00095         mbedtls_gcm_init( (mbedtls_gcm_context *) ctx );
00096 
00097     return( ctx );
00098 }
00099 
00100 static void gcm_ctx_free( void *ctx )
00101 {
00102     mbedtls_gcm_free( ctx );
00103     mbedtls_free( ctx );
00104 }
00105 #endif /* MBEDTLS_GCM_C */
00106 
00107 #if defined(MBEDTLS_CCM_C)
00108 /* shared by all CCM ciphers */
00109 static void *ccm_ctx_alloc( void )
00110 {
00111     void *ctx = mbedtls_calloc( 1, sizeof( mbedtls_ccm_context ) );
00112 
00113     if( ctx != NULL )
00114         mbedtls_ccm_init( (mbedtls_ccm_context *) ctx );
00115 
00116     return( ctx );
00117 }
00118 
00119 static void ccm_ctx_free( void *ctx )
00120 {
00121     mbedtls_ccm_free( ctx );
00122     mbedtls_free( ctx );
00123 }
00124 #endif /* MBEDTLS_CCM_C */
00125 
00126 #if defined(MBEDTLS_AES_C)
00127 
00128 static int aes_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
00129         const unsigned char *input, unsigned char *output )
00130 {
00131     return mbedtls_aes_crypt_ecb( (mbedtls_aes_context *) ctx, operation, input, output );
00132 }
00133 
00134 #if defined(MBEDTLS_CIPHER_MODE_CBC)
00135 static int aes_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
00136         unsigned char *iv, const unsigned char *input, unsigned char *output )
00137 {
00138     return mbedtls_aes_crypt_cbc( (mbedtls_aes_context *) ctx, operation, length, iv, input,
00139                           output );
00140 }
00141 #endif /* MBEDTLS_CIPHER_MODE_CBC */
00142 
00143 #if defined(MBEDTLS_CIPHER_MODE_CFB)
00144 static int aes_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
00145         size_t length, size_t *iv_off, unsigned char *iv,
00146         const unsigned char *input, unsigned char *output )
00147 {
00148     return mbedtls_aes_crypt_cfb128( (mbedtls_aes_context *) ctx, operation, length, iv_off, iv,
00149                              input, output );
00150 }
00151 #endif /* MBEDTLS_CIPHER_MODE_CFB */
00152 
00153 #if defined(MBEDTLS_CIPHER_MODE_OFB)
00154 static int aes_crypt_ofb_wrap( void *ctx, size_t length, size_t *iv_off,
00155         unsigned char *iv, const unsigned char *input, unsigned char *output )
00156 {
00157     return mbedtls_aes_crypt_ofb( (mbedtls_aes_context *) ctx, length, iv_off,
00158                                     iv, input, output );
00159 }
00160 #endif /* MBEDTLS_CIPHER_MODE_OFB */
00161 
00162 #if defined(MBEDTLS_CIPHER_MODE_CTR)
00163 static int aes_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
00164         unsigned char *nonce_counter, unsigned char *stream_block,
00165         const unsigned char *input, unsigned char *output )
00166 {
00167     return mbedtls_aes_crypt_ctr( (mbedtls_aes_context *) ctx, length, nc_off, nonce_counter,
00168                           stream_block, input, output );
00169 }
00170 #endif /* MBEDTLS_CIPHER_MODE_CTR */
00171 
00172 #if defined(MBEDTLS_CIPHER_MODE_XTS)
00173 static int aes_crypt_xts_wrap( void *ctx, mbedtls_operation_t operation,
00174                                size_t length,
00175                                const unsigned char data_unit[16],
00176                                const unsigned char *input,
00177                                unsigned char *output )
00178 {
00179     mbedtls_aes_xts_context *xts_ctx = ctx;
00180     int mode;
00181 
00182     switch( operation )
00183     {
00184         case MBEDTLS_ENCRYPT:
00185             mode = MBEDTLS_AES_ENCRYPT;
00186             break;
00187         case MBEDTLS_DECRYPT:
00188             mode = MBEDTLS_AES_DECRYPT;
00189             break;
00190         default:
00191             return MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA;
00192     }
00193 
00194     return mbedtls_aes_crypt_xts( xts_ctx, mode, length,
00195                                   data_unit, input, output );
00196 }
00197 #endif /* MBEDTLS_CIPHER_MODE_XTS */
00198 
00199 static int aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
00200                                 unsigned int key_bitlen )
00201 {
00202     return mbedtls_aes_setkey_dec( (mbedtls_aes_context *) ctx, key, key_bitlen );
00203 }
00204 
00205 static int aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
00206                                 unsigned int key_bitlen )
00207 {
00208     return mbedtls_aes_setkey_enc( (mbedtls_aes_context *) ctx, key, key_bitlen );
00209 }
00210 
00211 static void * aes_ctx_alloc( void )
00212 {
00213     mbedtls_aes_context *aes = mbedtls_calloc( 1, sizeof( mbedtls_aes_context ) );
00214 
00215     if( aes == NULL )
00216         return( NULL );
00217 
00218     mbedtls_aes_init( aes );
00219 
00220     return( aes );
00221 }
00222 
00223 static void aes_ctx_free( void *ctx )
00224 {
00225     mbedtls_aes_free( (mbedtls_aes_context *) ctx );
00226     mbedtls_free( ctx );
00227 }
00228 
00229 static const mbedtls_cipher_base_t aes_info = {
00230     MBEDTLS_CIPHER_ID_AES,
00231     aes_crypt_ecb_wrap,
00232 #if defined(MBEDTLS_CIPHER_MODE_CBC)
00233     aes_crypt_cbc_wrap,
00234 #endif
00235 #if defined(MBEDTLS_CIPHER_MODE_CFB)
00236     aes_crypt_cfb128_wrap,
00237 #endif
00238 #if defined(MBEDTLS_CIPHER_MODE_OFB)
00239     aes_crypt_ofb_wrap,
00240 #endif
00241 #if defined(MBEDTLS_CIPHER_MODE_CTR)
00242     aes_crypt_ctr_wrap,
00243 #endif
00244 #if defined(MBEDTLS_CIPHER_MODE_XTS)
00245     NULL,
00246 #endif
00247 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
00248     NULL,
00249 #endif
00250     aes_setkey_enc_wrap,
00251     aes_setkey_dec_wrap,
00252     aes_ctx_alloc,
00253     aes_ctx_free
00254 };
00255 
00256 static const mbedtls_cipher_info_t aes_128_ecb_info = {
00257     MBEDTLS_CIPHER_AES_128_ECB,
00258     MBEDTLS_MODE_ECB,
00259     128,
00260     "AES-128-ECB",
00261     16,
00262     0,
00263     16,
00264     &aes_info
00265 };
00266 
00267 static const mbedtls_cipher_info_t aes_192_ecb_info = {
00268     MBEDTLS_CIPHER_AES_192_ECB,
00269     MBEDTLS_MODE_ECB,
00270     192,
00271     "AES-192-ECB",
00272     16,
00273     0,
00274     16,
00275     &aes_info
00276 };
00277 
00278 static const mbedtls_cipher_info_t aes_256_ecb_info = {
00279     MBEDTLS_CIPHER_AES_256_ECB,
00280     MBEDTLS_MODE_ECB,
00281     256,
00282     "AES-256-ECB",
00283     16,
00284     0,
00285     16,
00286     &aes_info
00287 };
00288 
00289 #if defined(MBEDTLS_CIPHER_MODE_CBC)
00290 static const mbedtls_cipher_info_t aes_128_cbc_info = {
00291     MBEDTLS_CIPHER_AES_128_CBC,
00292     MBEDTLS_MODE_CBC,
00293     128,
00294     "AES-128-CBC",
00295     16,
00296     0,
00297     16,
00298     &aes_info
00299 };
00300 
00301 static const mbedtls_cipher_info_t aes_192_cbc_info = {
00302     MBEDTLS_CIPHER_AES_192_CBC,
00303     MBEDTLS_MODE_CBC,
00304     192,
00305     "AES-192-CBC",
00306     16,
00307     0,
00308     16,
00309     &aes_info
00310 };
00311 
00312 static const mbedtls_cipher_info_t aes_256_cbc_info = {
00313     MBEDTLS_CIPHER_AES_256_CBC,
00314     MBEDTLS_MODE_CBC,
00315     256,
00316     "AES-256-CBC",
00317     16,
00318     0,
00319     16,
00320     &aes_info
00321 };
00322 #endif /* MBEDTLS_CIPHER_MODE_CBC */
00323 
00324 #if defined(MBEDTLS_CIPHER_MODE_CFB)
00325 static const mbedtls_cipher_info_t aes_128_cfb128_info = {
00326     MBEDTLS_CIPHER_AES_128_CFB128,
00327     MBEDTLS_MODE_CFB,
00328     128,
00329     "AES-128-CFB128",
00330     16,
00331     0,
00332     16,
00333     &aes_info
00334 };
00335 
00336 static const mbedtls_cipher_info_t aes_192_cfb128_info = {
00337     MBEDTLS_CIPHER_AES_192_CFB128,
00338     MBEDTLS_MODE_CFB,
00339     192,
00340     "AES-192-CFB128",
00341     16,
00342     0,
00343     16,
00344     &aes_info
00345 };
00346 
00347 static const mbedtls_cipher_info_t aes_256_cfb128_info = {
00348     MBEDTLS_CIPHER_AES_256_CFB128,
00349     MBEDTLS_MODE_CFB,
00350     256,
00351     "AES-256-CFB128",
00352     16,
00353     0,
00354     16,
00355     &aes_info
00356 };
00357 #endif /* MBEDTLS_CIPHER_MODE_CFB */
00358 
00359 #if defined(MBEDTLS_CIPHER_MODE_OFB)
00360 static const mbedtls_cipher_info_t aes_128_ofb_info = {
00361     MBEDTLS_CIPHER_AES_128_OFB,
00362     MBEDTLS_MODE_OFB,
00363     128,
00364     "AES-128-OFB",
00365     16,
00366     0,
00367     16,
00368     &aes_info
00369 };
00370 
00371 static const mbedtls_cipher_info_t aes_192_ofb_info = {
00372     MBEDTLS_CIPHER_AES_192_OFB,
00373     MBEDTLS_MODE_OFB,
00374     192,
00375     "AES-192-OFB",
00376     16,
00377     0,
00378     16,
00379     &aes_info
00380 };
00381 
00382 static const mbedtls_cipher_info_t aes_256_ofb_info = {
00383     MBEDTLS_CIPHER_AES_256_OFB,
00384     MBEDTLS_MODE_OFB,
00385     256,
00386     "AES-256-OFB",
00387     16,
00388     0,
00389     16,
00390     &aes_info
00391 };
00392 #endif /* MBEDTLS_CIPHER_MODE_OFB */
00393 
00394 #if defined(MBEDTLS_CIPHER_MODE_CTR)
00395 static const mbedtls_cipher_info_t aes_128_ctr_info = {
00396     MBEDTLS_CIPHER_AES_128_CTR,
00397     MBEDTLS_MODE_CTR,
00398     128,
00399     "AES-128-CTR",
00400     16,
00401     0,
00402     16,
00403     &aes_info
00404 };
00405 
00406 static const mbedtls_cipher_info_t aes_192_ctr_info = {
00407     MBEDTLS_CIPHER_AES_192_CTR,
00408     MBEDTLS_MODE_CTR,
00409     192,
00410     "AES-192-CTR",
00411     16,
00412     0,
00413     16,
00414     &aes_info
00415 };
00416 
00417 static const mbedtls_cipher_info_t aes_256_ctr_info = {
00418     MBEDTLS_CIPHER_AES_256_CTR,
00419     MBEDTLS_MODE_CTR,
00420     256,
00421     "AES-256-CTR",
00422     16,
00423     0,
00424     16,
00425     &aes_info
00426 };
00427 #endif /* MBEDTLS_CIPHER_MODE_CTR */
00428 
00429 #if defined(MBEDTLS_CIPHER_MODE_XTS)
00430 static int xts_aes_setkey_enc_wrap( void *ctx, const unsigned char *key,
00431                                     unsigned int key_bitlen )
00432 {
00433     mbedtls_aes_xts_context *xts_ctx = ctx;
00434     return( mbedtls_aes_xts_setkey_enc( xts_ctx, key, key_bitlen ) );
00435 }
00436 
00437 static int xts_aes_setkey_dec_wrap( void *ctx, const unsigned char *key,
00438                                     unsigned int key_bitlen )
00439 {
00440     mbedtls_aes_xts_context *xts_ctx = ctx;
00441     return( mbedtls_aes_xts_setkey_dec( xts_ctx, key, key_bitlen ) );
00442 }
00443 
00444 static void *xts_aes_ctx_alloc( void )
00445 {
00446     mbedtls_aes_xts_context *xts_ctx = mbedtls_calloc( 1, sizeof( *xts_ctx ) );
00447 
00448     if( xts_ctx != NULL )
00449         mbedtls_aes_xts_init( xts_ctx );
00450 
00451     return( xts_ctx );
00452 }
00453 
00454 static void xts_aes_ctx_free( void *ctx )
00455 {
00456     mbedtls_aes_xts_context *xts_ctx = ctx;
00457 
00458     if( xts_ctx == NULL )
00459         return;
00460 
00461     mbedtls_aes_xts_free( xts_ctx );
00462     mbedtls_free( xts_ctx );
00463 }
00464 
00465 static const mbedtls_cipher_base_t xts_aes_info = {
00466     MBEDTLS_CIPHER_ID_AES,
00467     NULL,
00468 #if defined(MBEDTLS_CIPHER_MODE_CBC)
00469     NULL,
00470 #endif
00471 #if defined(MBEDTLS_CIPHER_MODE_CFB)
00472     NULL,
00473 #endif
00474 #if defined(MBEDTLS_CIPHER_MODE_OFB)
00475     NULL,
00476 #endif
00477 #if defined(MBEDTLS_CIPHER_MODE_CTR)
00478     NULL,
00479 #endif
00480 #if defined(MBEDTLS_CIPHER_MODE_XTS)
00481     aes_crypt_xts_wrap,
00482 #endif
00483 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
00484     NULL,
00485 #endif
00486     xts_aes_setkey_enc_wrap,
00487     xts_aes_setkey_dec_wrap,
00488     xts_aes_ctx_alloc,
00489     xts_aes_ctx_free
00490 };
00491 
00492 static const mbedtls_cipher_info_t aes_128_xts_info = {
00493     MBEDTLS_CIPHER_AES_128_XTS,
00494     MBEDTLS_MODE_XTS,
00495     256,
00496     "AES-128-XTS",
00497     16,
00498     0,
00499     16,
00500     &xts_aes_info
00501 };
00502 
00503 static const mbedtls_cipher_info_t aes_256_xts_info = {
00504     MBEDTLS_CIPHER_AES_256_XTS,
00505     MBEDTLS_MODE_XTS,
00506     512,
00507     "AES-256-XTS",
00508     16,
00509     0,
00510     16,
00511     &xts_aes_info
00512 };
00513 #endif /* MBEDTLS_CIPHER_MODE_XTS */
00514 
00515 #if defined(MBEDTLS_GCM_C)
00516 static int gcm_aes_setkey_wrap( void *ctx, const unsigned char *key,
00517                                 unsigned int key_bitlen )
00518 {
00519     return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
00520                      key, key_bitlen );
00521 }
00522 
00523 static const mbedtls_cipher_base_t gcm_aes_info = {
00524     MBEDTLS_CIPHER_ID_AES,
00525     NULL,
00526 #if defined(MBEDTLS_CIPHER_MODE_CBC)
00527     NULL,
00528 #endif
00529 #if defined(MBEDTLS_CIPHER_MODE_CFB)
00530     NULL,
00531 #endif
00532 #if defined(MBEDTLS_CIPHER_MODE_OFB)
00533     NULL,
00534 #endif
00535 #if defined(MBEDTLS_CIPHER_MODE_CTR)
00536     NULL,
00537 #endif
00538 #if defined(MBEDTLS_CIPHER_MODE_XTS)
00539     NULL,
00540 #endif
00541 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
00542     NULL,
00543 #endif
00544     gcm_aes_setkey_wrap,
00545     gcm_aes_setkey_wrap,
00546     gcm_ctx_alloc,
00547     gcm_ctx_free,
00548 };
00549 
00550 static const mbedtls_cipher_info_t aes_128_gcm_info = {
00551     MBEDTLS_CIPHER_AES_128_GCM,
00552     MBEDTLS_MODE_GCM,
00553     128,
00554     "AES-128-GCM",
00555     12,
00556     MBEDTLS_CIPHER_VARIABLE_IV_LEN,
00557     16,
00558     &gcm_aes_info
00559 };
00560 
00561 static const mbedtls_cipher_info_t aes_192_gcm_info = {
00562     MBEDTLS_CIPHER_AES_192_GCM,
00563     MBEDTLS_MODE_GCM,
00564     192,
00565     "AES-192-GCM",
00566     12,
00567     MBEDTLS_CIPHER_VARIABLE_IV_LEN,
00568     16,
00569     &gcm_aes_info
00570 };
00571 
00572 static const mbedtls_cipher_info_t aes_256_gcm_info = {
00573     MBEDTLS_CIPHER_AES_256_GCM,
00574     MBEDTLS_MODE_GCM,
00575     256,
00576     "AES-256-GCM",
00577     12,
00578     MBEDTLS_CIPHER_VARIABLE_IV_LEN,
00579     16,
00580     &gcm_aes_info
00581 };
00582 #endif /* MBEDTLS_GCM_C */
00583 
00584 #if defined(MBEDTLS_CCM_C)
00585 static int ccm_aes_setkey_wrap( void *ctx, const unsigned char *key,
00586                                 unsigned int key_bitlen )
00587 {
00588     return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_AES,
00589                      key, key_bitlen );
00590 }
00591 
00592 static const mbedtls_cipher_base_t ccm_aes_info = {
00593     MBEDTLS_CIPHER_ID_AES,
00594     NULL,
00595 #if defined(MBEDTLS_CIPHER_MODE_CBC)
00596     NULL,
00597 #endif
00598 #if defined(MBEDTLS_CIPHER_MODE_CFB)
00599     NULL,
00600 #endif
00601 #if defined(MBEDTLS_CIPHER_MODE_OFB)
00602     NULL,
00603 #endif
00604 #if defined(MBEDTLS_CIPHER_MODE_CTR)
00605     NULL,
00606 #endif
00607 #if defined(MBEDTLS_CIPHER_MODE_XTS)
00608     NULL,
00609 #endif
00610 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
00611     NULL,
00612 #endif
00613     ccm_aes_setkey_wrap,
00614     ccm_aes_setkey_wrap,
00615     ccm_ctx_alloc,
00616     ccm_ctx_free,
00617 };
00618 
00619 static const mbedtls_cipher_info_t aes_128_ccm_info = {
00620     MBEDTLS_CIPHER_AES_128_CCM,
00621     MBEDTLS_MODE_CCM,
00622     128,
00623     "AES-128-CCM",
00624     12,
00625     MBEDTLS_CIPHER_VARIABLE_IV_LEN,
00626     16,
00627     &ccm_aes_info
00628 };
00629 
00630 static const mbedtls_cipher_info_t aes_192_ccm_info = {
00631     MBEDTLS_CIPHER_AES_192_CCM,
00632     MBEDTLS_MODE_CCM,
00633     192,
00634     "AES-192-CCM",
00635     12,
00636     MBEDTLS_CIPHER_VARIABLE_IV_LEN,
00637     16,
00638     &ccm_aes_info
00639 };
00640 
00641 static const mbedtls_cipher_info_t aes_256_ccm_info = {
00642     MBEDTLS_CIPHER_AES_256_CCM,
00643     MBEDTLS_MODE_CCM,
00644     256,
00645     "AES-256-CCM",
00646     12,
00647     MBEDTLS_CIPHER_VARIABLE_IV_LEN,
00648     16,
00649     &ccm_aes_info
00650 };
00651 #endif /* MBEDTLS_CCM_C */
00652 
00653 #endif /* MBEDTLS_AES_C */
00654 
00655 #if defined(MBEDTLS_CAMELLIA_C)
00656 
00657 static int camellia_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
00658         const unsigned char *input, unsigned char *output )
00659 {
00660     return mbedtls_camellia_crypt_ecb( (mbedtls_camellia_context *) ctx, operation, input,
00661                                output );
00662 }
00663 
00664 #if defined(MBEDTLS_CIPHER_MODE_CBC)
00665 static int camellia_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
00666         size_t length, unsigned char *iv,
00667         const unsigned char *input, unsigned char *output )
00668 {
00669     return mbedtls_camellia_crypt_cbc( (mbedtls_camellia_context *) ctx, operation, length, iv,
00670                                input, output );
00671 }
00672 #endif /* MBEDTLS_CIPHER_MODE_CBC */
00673 
00674 #if defined(MBEDTLS_CIPHER_MODE_CFB)
00675 static int camellia_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
00676         size_t length, size_t *iv_off, unsigned char *iv,
00677         const unsigned char *input, unsigned char *output )
00678 {
00679     return mbedtls_camellia_crypt_cfb128( (mbedtls_camellia_context *) ctx, operation, length,
00680                                   iv_off, iv, input, output );
00681 }
00682 #endif /* MBEDTLS_CIPHER_MODE_CFB */
00683 
00684 #if defined(MBEDTLS_CIPHER_MODE_CTR)
00685 static int camellia_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
00686         unsigned char *nonce_counter, unsigned char *stream_block,
00687         const unsigned char *input, unsigned char *output )
00688 {
00689     return mbedtls_camellia_crypt_ctr( (mbedtls_camellia_context *) ctx, length, nc_off,
00690                                nonce_counter, stream_block, input, output );
00691 }
00692 #endif /* MBEDTLS_CIPHER_MODE_CTR */
00693 
00694 static int camellia_setkey_dec_wrap( void *ctx, const unsigned char *key,
00695                                      unsigned int key_bitlen )
00696 {
00697     return mbedtls_camellia_setkey_dec( (mbedtls_camellia_context *) ctx, key, key_bitlen );
00698 }
00699 
00700 static int camellia_setkey_enc_wrap( void *ctx, const unsigned char *key,
00701                                      unsigned int key_bitlen )
00702 {
00703     return mbedtls_camellia_setkey_enc( (mbedtls_camellia_context *) ctx, key, key_bitlen );
00704 }
00705 
00706 static void * camellia_ctx_alloc( void )
00707 {
00708     mbedtls_camellia_context *ctx;
00709     ctx = mbedtls_calloc( 1, sizeof( mbedtls_camellia_context ) );
00710 
00711     if( ctx == NULL )
00712         return( NULL );
00713 
00714     mbedtls_camellia_init( ctx );
00715 
00716     return( ctx );
00717 }
00718 
00719 static void camellia_ctx_free( void *ctx )
00720 {
00721     mbedtls_camellia_free( (mbedtls_camellia_context *) ctx );
00722     mbedtls_free( ctx );
00723 }
00724 
00725 static const mbedtls_cipher_base_t camellia_info = {
00726     MBEDTLS_CIPHER_ID_CAMELLIA,
00727     camellia_crypt_ecb_wrap,
00728 #if defined(MBEDTLS_CIPHER_MODE_CBC)
00729     camellia_crypt_cbc_wrap,
00730 #endif
00731 #if defined(MBEDTLS_CIPHER_MODE_CFB)
00732     camellia_crypt_cfb128_wrap,
00733 #endif
00734 #if defined(MBEDTLS_CIPHER_MODE_OFB)
00735     NULL,
00736 #endif
00737 #if defined(MBEDTLS_CIPHER_MODE_CTR)
00738     camellia_crypt_ctr_wrap,
00739 #endif
00740 #if defined(MBEDTLS_CIPHER_MODE_XTS)
00741     NULL,
00742 #endif
00743 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
00744     NULL,
00745 #endif
00746     camellia_setkey_enc_wrap,
00747     camellia_setkey_dec_wrap,
00748     camellia_ctx_alloc,
00749     camellia_ctx_free
00750 };
00751 
00752 static const mbedtls_cipher_info_t camellia_128_ecb_info = {
00753     MBEDTLS_CIPHER_CAMELLIA_128_ECB,
00754     MBEDTLS_MODE_ECB,
00755     128,
00756     "CAMELLIA-128-ECB",
00757     16,
00758     0,
00759     16,
00760     &camellia_info
00761 };
00762 
00763 static const mbedtls_cipher_info_t camellia_192_ecb_info = {
00764     MBEDTLS_CIPHER_CAMELLIA_192_ECB,
00765     MBEDTLS_MODE_ECB,
00766     192,
00767     "CAMELLIA-192-ECB",
00768     16,
00769     0,
00770     16,
00771     &camellia_info
00772 };
00773 
00774 static const mbedtls_cipher_info_t camellia_256_ecb_info = {
00775     MBEDTLS_CIPHER_CAMELLIA_256_ECB,
00776     MBEDTLS_MODE_ECB,
00777     256,
00778     "CAMELLIA-256-ECB",
00779     16,
00780     0,
00781     16,
00782     &camellia_info
00783 };
00784 
00785 #if defined(MBEDTLS_CIPHER_MODE_CBC)
00786 static const mbedtls_cipher_info_t camellia_128_cbc_info = {
00787     MBEDTLS_CIPHER_CAMELLIA_128_CBC,
00788     MBEDTLS_MODE_CBC,
00789     128,
00790     "CAMELLIA-128-CBC",
00791     16,
00792     0,
00793     16,
00794     &camellia_info
00795 };
00796 
00797 static const mbedtls_cipher_info_t camellia_192_cbc_info = {
00798     MBEDTLS_CIPHER_CAMELLIA_192_CBC,
00799     MBEDTLS_MODE_CBC,
00800     192,
00801     "CAMELLIA-192-CBC",
00802     16,
00803     0,
00804     16,
00805     &camellia_info
00806 };
00807 
00808 static const mbedtls_cipher_info_t camellia_256_cbc_info = {
00809     MBEDTLS_CIPHER_CAMELLIA_256_CBC,
00810     MBEDTLS_MODE_CBC,
00811     256,
00812     "CAMELLIA-256-CBC",
00813     16,
00814     0,
00815     16,
00816     &camellia_info
00817 };
00818 #endif /* MBEDTLS_CIPHER_MODE_CBC */
00819 
00820 #if defined(MBEDTLS_CIPHER_MODE_CFB)
00821 static const mbedtls_cipher_info_t camellia_128_cfb128_info = {
00822     MBEDTLS_CIPHER_CAMELLIA_128_CFB128,
00823     MBEDTLS_MODE_CFB,
00824     128,
00825     "CAMELLIA-128-CFB128",
00826     16,
00827     0,
00828     16,
00829     &camellia_info
00830 };
00831 
00832 static const mbedtls_cipher_info_t camellia_192_cfb128_info = {
00833     MBEDTLS_CIPHER_CAMELLIA_192_CFB128,
00834     MBEDTLS_MODE_CFB,
00835     192,
00836     "CAMELLIA-192-CFB128",
00837     16,
00838     0,
00839     16,
00840     &camellia_info
00841 };
00842 
00843 static const mbedtls_cipher_info_t camellia_256_cfb128_info = {
00844     MBEDTLS_CIPHER_CAMELLIA_256_CFB128,
00845     MBEDTLS_MODE_CFB,
00846     256,
00847     "CAMELLIA-256-CFB128",
00848     16,
00849     0,
00850     16,
00851     &camellia_info
00852 };
00853 #endif /* MBEDTLS_CIPHER_MODE_CFB */
00854 
00855 #if defined(MBEDTLS_CIPHER_MODE_CTR)
00856 static const mbedtls_cipher_info_t camellia_128_ctr_info = {
00857     MBEDTLS_CIPHER_CAMELLIA_128_CTR,
00858     MBEDTLS_MODE_CTR,
00859     128,
00860     "CAMELLIA-128-CTR",
00861     16,
00862     0,
00863     16,
00864     &camellia_info
00865 };
00866 
00867 static const mbedtls_cipher_info_t camellia_192_ctr_info = {
00868     MBEDTLS_CIPHER_CAMELLIA_192_CTR,
00869     MBEDTLS_MODE_CTR,
00870     192,
00871     "CAMELLIA-192-CTR",
00872     16,
00873     0,
00874     16,
00875     &camellia_info
00876 };
00877 
00878 static const mbedtls_cipher_info_t camellia_256_ctr_info = {
00879     MBEDTLS_CIPHER_CAMELLIA_256_CTR,
00880     MBEDTLS_MODE_CTR,
00881     256,
00882     "CAMELLIA-256-CTR",
00883     16,
00884     0,
00885     16,
00886     &camellia_info
00887 };
00888 #endif /* MBEDTLS_CIPHER_MODE_CTR */
00889 
00890 #if defined(MBEDTLS_GCM_C)
00891 static int gcm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
00892                                      unsigned int key_bitlen )
00893 {
00894     return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
00895                      key, key_bitlen );
00896 }
00897 
00898 static const mbedtls_cipher_base_t gcm_camellia_info = {
00899     MBEDTLS_CIPHER_ID_CAMELLIA,
00900     NULL,
00901 #if defined(MBEDTLS_CIPHER_MODE_CBC)
00902     NULL,
00903 #endif
00904 #if defined(MBEDTLS_CIPHER_MODE_CFB)
00905     NULL,
00906 #endif
00907 #if defined(MBEDTLS_CIPHER_MODE_OFB)
00908     NULL,
00909 #endif
00910 #if defined(MBEDTLS_CIPHER_MODE_CTR)
00911     NULL,
00912 #endif
00913 #if defined(MBEDTLS_CIPHER_MODE_XTS)
00914     NULL,
00915 #endif
00916 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
00917     NULL,
00918 #endif
00919     gcm_camellia_setkey_wrap,
00920     gcm_camellia_setkey_wrap,
00921     gcm_ctx_alloc,
00922     gcm_ctx_free,
00923 };
00924 
00925 static const mbedtls_cipher_info_t camellia_128_gcm_info = {
00926     MBEDTLS_CIPHER_CAMELLIA_128_GCM,
00927     MBEDTLS_MODE_GCM,
00928     128,
00929     "CAMELLIA-128-GCM",
00930     12,
00931     MBEDTLS_CIPHER_VARIABLE_IV_LEN,
00932     16,
00933     &gcm_camellia_info
00934 };
00935 
00936 static const mbedtls_cipher_info_t camellia_192_gcm_info = {
00937     MBEDTLS_CIPHER_CAMELLIA_192_GCM,
00938     MBEDTLS_MODE_GCM,
00939     192,
00940     "CAMELLIA-192-GCM",
00941     12,
00942     MBEDTLS_CIPHER_VARIABLE_IV_LEN,
00943     16,
00944     &gcm_camellia_info
00945 };
00946 
00947 static const mbedtls_cipher_info_t camellia_256_gcm_info = {
00948     MBEDTLS_CIPHER_CAMELLIA_256_GCM,
00949     MBEDTLS_MODE_GCM,
00950     256,
00951     "CAMELLIA-256-GCM",
00952     12,
00953     MBEDTLS_CIPHER_VARIABLE_IV_LEN,
00954     16,
00955     &gcm_camellia_info
00956 };
00957 #endif /* MBEDTLS_GCM_C */
00958 
00959 #if defined(MBEDTLS_CCM_C)
00960 static int ccm_camellia_setkey_wrap( void *ctx, const unsigned char *key,
00961                                      unsigned int key_bitlen )
00962 {
00963     return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_CAMELLIA,
00964                      key, key_bitlen );
00965 }
00966 
00967 static const mbedtls_cipher_base_t ccm_camellia_info = {
00968     MBEDTLS_CIPHER_ID_CAMELLIA,
00969     NULL,
00970 #if defined(MBEDTLS_CIPHER_MODE_CBC)
00971     NULL,
00972 #endif
00973 #if defined(MBEDTLS_CIPHER_MODE_CFB)
00974     NULL,
00975 #endif
00976 #if defined(MBEDTLS_CIPHER_MODE_OFB)
00977     NULL,
00978 #endif
00979 #if defined(MBEDTLS_CIPHER_MODE_CTR)
00980     NULL,
00981 #endif
00982 #if defined(MBEDTLS_CIPHER_MODE_XTS)
00983     NULL,
00984 #endif
00985 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
00986     NULL,
00987 #endif
00988     ccm_camellia_setkey_wrap,
00989     ccm_camellia_setkey_wrap,
00990     ccm_ctx_alloc,
00991     ccm_ctx_free,
00992 };
00993 
00994 static const mbedtls_cipher_info_t camellia_128_ccm_info = {
00995     MBEDTLS_CIPHER_CAMELLIA_128_CCM,
00996     MBEDTLS_MODE_CCM,
00997     128,
00998     "CAMELLIA-128-CCM",
00999     12,
01000     MBEDTLS_CIPHER_VARIABLE_IV_LEN,
01001     16,
01002     &ccm_camellia_info
01003 };
01004 
01005 static const mbedtls_cipher_info_t camellia_192_ccm_info = {
01006     MBEDTLS_CIPHER_CAMELLIA_192_CCM,
01007     MBEDTLS_MODE_CCM,
01008     192,
01009     "CAMELLIA-192-CCM",
01010     12,
01011     MBEDTLS_CIPHER_VARIABLE_IV_LEN,
01012     16,
01013     &ccm_camellia_info
01014 };
01015 
01016 static const mbedtls_cipher_info_t camellia_256_ccm_info = {
01017     MBEDTLS_CIPHER_CAMELLIA_256_CCM,
01018     MBEDTLS_MODE_CCM,
01019     256,
01020     "CAMELLIA-256-CCM",
01021     12,
01022     MBEDTLS_CIPHER_VARIABLE_IV_LEN,
01023     16,
01024     &ccm_camellia_info
01025 };
01026 #endif /* MBEDTLS_CCM_C */
01027 
01028 #endif /* MBEDTLS_CAMELLIA_C */
01029 
01030 #if defined(MBEDTLS_ARIA_C)
01031 
01032 static int aria_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
01033         const unsigned char *input, unsigned char *output )
01034 {
01035     (void) operation;
01036     return mbedtls_aria_crypt_ecb( (mbedtls_aria_context *) ctx, input,
01037                                output );
01038 }
01039 
01040 #if defined(MBEDTLS_CIPHER_MODE_CBC)
01041 static int aria_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
01042         size_t length, unsigned char *iv,
01043         const unsigned char *input, unsigned char *output )
01044 {
01045     return mbedtls_aria_crypt_cbc( (mbedtls_aria_context *) ctx, operation, length, iv,
01046                                input, output );
01047 }
01048 #endif /* MBEDTLS_CIPHER_MODE_CBC */
01049 
01050 #if defined(MBEDTLS_CIPHER_MODE_CFB)
01051 static int aria_crypt_cfb128_wrap( void *ctx, mbedtls_operation_t operation,
01052         size_t length, size_t *iv_off, unsigned char *iv,
01053         const unsigned char *input, unsigned char *output )
01054 {
01055     return mbedtls_aria_crypt_cfb128( (mbedtls_aria_context *) ctx, operation, length,
01056                                   iv_off, iv, input, output );
01057 }
01058 #endif /* MBEDTLS_CIPHER_MODE_CFB */
01059 
01060 #if defined(MBEDTLS_CIPHER_MODE_CTR)
01061 static int aria_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
01062         unsigned char *nonce_counter, unsigned char *stream_block,
01063         const unsigned char *input, unsigned char *output )
01064 {
01065     return mbedtls_aria_crypt_ctr( (mbedtls_aria_context *) ctx, length, nc_off,
01066                                nonce_counter, stream_block, input, output );
01067 }
01068 #endif /* MBEDTLS_CIPHER_MODE_CTR */
01069 
01070 static int aria_setkey_dec_wrap( void *ctx, const unsigned char *key,
01071                                      unsigned int key_bitlen )
01072 {
01073     return mbedtls_aria_setkey_dec( (mbedtls_aria_context *) ctx, key, key_bitlen );
01074 }
01075 
01076 static int aria_setkey_enc_wrap( void *ctx, const unsigned char *key,
01077                                      unsigned int key_bitlen )
01078 {
01079     return mbedtls_aria_setkey_enc( (mbedtls_aria_context *) ctx, key, key_bitlen );
01080 }
01081 
01082 static void * aria_ctx_alloc( void )
01083 {
01084     mbedtls_aria_context *ctx;
01085     ctx = mbedtls_calloc( 1, sizeof( mbedtls_aria_context ) );
01086 
01087     if( ctx == NULL )
01088         return( NULL );
01089 
01090     mbedtls_aria_init( ctx );
01091 
01092     return( ctx );
01093 }
01094 
01095 static void aria_ctx_free( void *ctx )
01096 {
01097     mbedtls_aria_free( (mbedtls_aria_context *) ctx );
01098     mbedtls_free( ctx );
01099 }
01100 
01101 static const mbedtls_cipher_base_t aria_info = {
01102     MBEDTLS_CIPHER_ID_ARIA,
01103     aria_crypt_ecb_wrap,
01104 #if defined(MBEDTLS_CIPHER_MODE_CBC)
01105     aria_crypt_cbc_wrap,
01106 #endif
01107 #if defined(MBEDTLS_CIPHER_MODE_CFB)
01108     aria_crypt_cfb128_wrap,
01109 #endif
01110 #if defined(MBEDTLS_CIPHER_MODE_OFB)
01111     NULL,
01112 #endif
01113 #if defined(MBEDTLS_CIPHER_MODE_CTR)
01114     aria_crypt_ctr_wrap,
01115 #endif
01116 #if defined(MBEDTLS_CIPHER_MODE_XTS)
01117     NULL,
01118 #endif
01119 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
01120     NULL,
01121 #endif
01122     aria_setkey_enc_wrap,
01123     aria_setkey_dec_wrap,
01124     aria_ctx_alloc,
01125     aria_ctx_free
01126 };
01127 
01128 static const mbedtls_cipher_info_t aria_128_ecb_info = {
01129     MBEDTLS_CIPHER_ARIA_128_ECB,
01130     MBEDTLS_MODE_ECB,
01131     128,
01132     "ARIA-128-ECB",
01133     16,
01134     0,
01135     16,
01136     &aria_info
01137 };
01138 
01139 static const mbedtls_cipher_info_t aria_192_ecb_info = {
01140     MBEDTLS_CIPHER_ARIA_192_ECB,
01141     MBEDTLS_MODE_ECB,
01142     192,
01143     "ARIA-192-ECB",
01144     16,
01145     0,
01146     16,
01147     &aria_info
01148 };
01149 
01150 static const mbedtls_cipher_info_t aria_256_ecb_info = {
01151     MBEDTLS_CIPHER_ARIA_256_ECB,
01152     MBEDTLS_MODE_ECB,
01153     256,
01154     "ARIA-256-ECB",
01155     16,
01156     0,
01157     16,
01158     &aria_info
01159 };
01160 
01161 #if defined(MBEDTLS_CIPHER_MODE_CBC)
01162 static const mbedtls_cipher_info_t aria_128_cbc_info = {
01163     MBEDTLS_CIPHER_ARIA_128_CBC,
01164     MBEDTLS_MODE_CBC,
01165     128,
01166     "ARIA-128-CBC",
01167     16,
01168     0,
01169     16,
01170     &aria_info
01171 };
01172 
01173 static const mbedtls_cipher_info_t aria_192_cbc_info = {
01174     MBEDTLS_CIPHER_ARIA_192_CBC,
01175     MBEDTLS_MODE_CBC,
01176     192,
01177     "ARIA-192-CBC",
01178     16,
01179     0,
01180     16,
01181     &aria_info
01182 };
01183 
01184 static const mbedtls_cipher_info_t aria_256_cbc_info = {
01185     MBEDTLS_CIPHER_ARIA_256_CBC,
01186     MBEDTLS_MODE_CBC,
01187     256,
01188     "ARIA-256-CBC",
01189     16,
01190     0,
01191     16,
01192     &aria_info
01193 };
01194 #endif /* MBEDTLS_CIPHER_MODE_CBC */
01195 
01196 #if defined(MBEDTLS_CIPHER_MODE_CFB)
01197 static const mbedtls_cipher_info_t aria_128_cfb128_info = {
01198     MBEDTLS_CIPHER_ARIA_128_CFB128,
01199     MBEDTLS_MODE_CFB,
01200     128,
01201     "ARIA-128-CFB128",
01202     16,
01203     0,
01204     16,
01205     &aria_info
01206 };
01207 
01208 static const mbedtls_cipher_info_t aria_192_cfb128_info = {
01209     MBEDTLS_CIPHER_ARIA_192_CFB128,
01210     MBEDTLS_MODE_CFB,
01211     192,
01212     "ARIA-192-CFB128",
01213     16,
01214     0,
01215     16,
01216     &aria_info
01217 };
01218 
01219 static const mbedtls_cipher_info_t aria_256_cfb128_info = {
01220     MBEDTLS_CIPHER_ARIA_256_CFB128,
01221     MBEDTLS_MODE_CFB,
01222     256,
01223     "ARIA-256-CFB128",
01224     16,
01225     0,
01226     16,
01227     &aria_info
01228 };
01229 #endif /* MBEDTLS_CIPHER_MODE_CFB */
01230 
01231 #if defined(MBEDTLS_CIPHER_MODE_CTR)
01232 static const mbedtls_cipher_info_t aria_128_ctr_info = {
01233     MBEDTLS_CIPHER_ARIA_128_CTR,
01234     MBEDTLS_MODE_CTR,
01235     128,
01236     "ARIA-128-CTR",
01237     16,
01238     0,
01239     16,
01240     &aria_info
01241 };
01242 
01243 static const mbedtls_cipher_info_t aria_192_ctr_info = {
01244     MBEDTLS_CIPHER_ARIA_192_CTR,
01245     MBEDTLS_MODE_CTR,
01246     192,
01247     "ARIA-192-CTR",
01248     16,
01249     0,
01250     16,
01251     &aria_info
01252 };
01253 
01254 static const mbedtls_cipher_info_t aria_256_ctr_info = {
01255     MBEDTLS_CIPHER_ARIA_256_CTR,
01256     MBEDTLS_MODE_CTR,
01257     256,
01258     "ARIA-256-CTR",
01259     16,
01260     0,
01261     16,
01262     &aria_info
01263 };
01264 #endif /* MBEDTLS_CIPHER_MODE_CTR */
01265 
01266 #if defined(MBEDTLS_GCM_C)
01267 static int gcm_aria_setkey_wrap( void *ctx, const unsigned char *key,
01268                                      unsigned int key_bitlen )
01269 {
01270     return mbedtls_gcm_setkey( (mbedtls_gcm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
01271                      key, key_bitlen );
01272 }
01273 
01274 static const mbedtls_cipher_base_t gcm_aria_info = {
01275     MBEDTLS_CIPHER_ID_ARIA,
01276     NULL,
01277 #if defined(MBEDTLS_CIPHER_MODE_CBC)
01278     NULL,
01279 #endif
01280 #if defined(MBEDTLS_CIPHER_MODE_CFB)
01281     NULL,
01282 #endif
01283 #if defined(MBEDTLS_CIPHER_MODE_OFB)
01284     NULL,
01285 #endif
01286 #if defined(MBEDTLS_CIPHER_MODE_CTR)
01287     NULL,
01288 #endif
01289 #if defined(MBEDTLS_CIPHER_MODE_XTS)
01290     NULL,
01291 #endif
01292 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
01293     NULL,
01294 #endif
01295     gcm_aria_setkey_wrap,
01296     gcm_aria_setkey_wrap,
01297     gcm_ctx_alloc,
01298     gcm_ctx_free,
01299 };
01300 
01301 static const mbedtls_cipher_info_t aria_128_gcm_info = {
01302     MBEDTLS_CIPHER_ARIA_128_GCM,
01303     MBEDTLS_MODE_GCM,
01304     128,
01305     "ARIA-128-GCM",
01306     12,
01307     MBEDTLS_CIPHER_VARIABLE_IV_LEN,
01308     16,
01309     &gcm_aria_info
01310 };
01311 
01312 static const mbedtls_cipher_info_t aria_192_gcm_info = {
01313     MBEDTLS_CIPHER_ARIA_192_GCM,
01314     MBEDTLS_MODE_GCM,
01315     192,
01316     "ARIA-192-GCM",
01317     12,
01318     MBEDTLS_CIPHER_VARIABLE_IV_LEN,
01319     16,
01320     &gcm_aria_info
01321 };
01322 
01323 static const mbedtls_cipher_info_t aria_256_gcm_info = {
01324     MBEDTLS_CIPHER_ARIA_256_GCM,
01325     MBEDTLS_MODE_GCM,
01326     256,
01327     "ARIA-256-GCM",
01328     12,
01329     MBEDTLS_CIPHER_VARIABLE_IV_LEN,
01330     16,
01331     &gcm_aria_info
01332 };
01333 #endif /* MBEDTLS_GCM_C */
01334 
01335 #if defined(MBEDTLS_CCM_C)
01336 static int ccm_aria_setkey_wrap( void *ctx, const unsigned char *key,
01337                                      unsigned int key_bitlen )
01338 {
01339     return mbedtls_ccm_setkey( (mbedtls_ccm_context *) ctx, MBEDTLS_CIPHER_ID_ARIA,
01340                      key, key_bitlen );
01341 }
01342 
01343 static const mbedtls_cipher_base_t ccm_aria_info = {
01344     MBEDTLS_CIPHER_ID_ARIA,
01345     NULL,
01346 #if defined(MBEDTLS_CIPHER_MODE_CBC)
01347     NULL,
01348 #endif
01349 #if defined(MBEDTLS_CIPHER_MODE_CFB)
01350     NULL,
01351 #endif
01352 #if defined(MBEDTLS_CIPHER_MODE_OFB)
01353     NULL,
01354 #endif
01355 #if defined(MBEDTLS_CIPHER_MODE_CTR)
01356     NULL,
01357 #endif
01358 #if defined(MBEDTLS_CIPHER_MODE_XTS)
01359     NULL,
01360 #endif
01361 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
01362     NULL,
01363 #endif
01364     ccm_aria_setkey_wrap,
01365     ccm_aria_setkey_wrap,
01366     ccm_ctx_alloc,
01367     ccm_ctx_free,
01368 };
01369 
01370 static const mbedtls_cipher_info_t aria_128_ccm_info = {
01371     MBEDTLS_CIPHER_ARIA_128_CCM,
01372     MBEDTLS_MODE_CCM,
01373     128,
01374     "ARIA-128-CCM",
01375     12,
01376     MBEDTLS_CIPHER_VARIABLE_IV_LEN,
01377     16,
01378     &ccm_aria_info
01379 };
01380 
01381 static const mbedtls_cipher_info_t aria_192_ccm_info = {
01382     MBEDTLS_CIPHER_ARIA_192_CCM,
01383     MBEDTLS_MODE_CCM,
01384     192,
01385     "ARIA-192-CCM",
01386     12,
01387     MBEDTLS_CIPHER_VARIABLE_IV_LEN,
01388     16,
01389     &ccm_aria_info
01390 };
01391 
01392 static const mbedtls_cipher_info_t aria_256_ccm_info = {
01393     MBEDTLS_CIPHER_ARIA_256_CCM,
01394     MBEDTLS_MODE_CCM,
01395     256,
01396     "ARIA-256-CCM",
01397     12,
01398     MBEDTLS_CIPHER_VARIABLE_IV_LEN,
01399     16,
01400     &ccm_aria_info
01401 };
01402 #endif /* MBEDTLS_CCM_C */
01403 
01404 #endif /* MBEDTLS_ARIA_C */
01405 
01406 #if defined(MBEDTLS_DES_C)
01407 
01408 static int des_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
01409         const unsigned char *input, unsigned char *output )
01410 {
01411     ((void) operation);
01412     return mbedtls_des_crypt_ecb( (mbedtls_des_context *) ctx, input, output );
01413 }
01414 
01415 static int des3_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
01416         const unsigned char *input, unsigned char *output )
01417 {
01418     ((void) operation);
01419     return mbedtls_des3_crypt_ecb( (mbedtls_des3_context *) ctx, input, output );
01420 }
01421 
01422 #if defined(MBEDTLS_CIPHER_MODE_CBC)
01423 static int des_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
01424         unsigned char *iv, const unsigned char *input, unsigned char *output )
01425 {
01426     return mbedtls_des_crypt_cbc( (mbedtls_des_context *) ctx, operation, length, iv, input,
01427                           output );
01428 }
01429 #endif /* MBEDTLS_CIPHER_MODE_CBC */
01430 
01431 #if defined(MBEDTLS_CIPHER_MODE_CBC)
01432 static int des3_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation, size_t length,
01433         unsigned char *iv, const unsigned char *input, unsigned char *output )
01434 {
01435     return mbedtls_des3_crypt_cbc( (mbedtls_des3_context *) ctx, operation, length, iv, input,
01436                            output );
01437 }
01438 #endif /* MBEDTLS_CIPHER_MODE_CBC */
01439 
01440 static int des_setkey_dec_wrap( void *ctx, const unsigned char *key,
01441                                 unsigned int key_bitlen )
01442 {
01443     ((void) key_bitlen);
01444 
01445     return mbedtls_des_setkey_dec( (mbedtls_des_context *) ctx, key );
01446 }
01447 
01448 static int des_setkey_enc_wrap( void *ctx, const unsigned char *key,
01449                                 unsigned int key_bitlen )
01450 {
01451     ((void) key_bitlen);
01452 
01453     return mbedtls_des_setkey_enc( (mbedtls_des_context *) ctx, key );
01454 }
01455 
01456 static int des3_set2key_dec_wrap( void *ctx, const unsigned char *key,
01457                                   unsigned int key_bitlen )
01458 {
01459     ((void) key_bitlen);
01460 
01461     return mbedtls_des3_set2key_dec( (mbedtls_des3_context *) ctx, key );
01462 }
01463 
01464 static int des3_set2key_enc_wrap( void *ctx, const unsigned char *key,
01465                                   unsigned int key_bitlen )
01466 {
01467     ((void) key_bitlen);
01468 
01469     return mbedtls_des3_set2key_enc( (mbedtls_des3_context *) ctx, key );
01470 }
01471 
01472 static int des3_set3key_dec_wrap( void *ctx, const unsigned char *key,
01473                                   unsigned int key_bitlen )
01474 {
01475     ((void) key_bitlen);
01476 
01477     return mbedtls_des3_set3key_dec( (mbedtls_des3_context *) ctx, key );
01478 }
01479 
01480 static int des3_set3key_enc_wrap( void *ctx, const unsigned char *key,
01481                                   unsigned int key_bitlen )
01482 {
01483     ((void) key_bitlen);
01484 
01485     return mbedtls_des3_set3key_enc( (mbedtls_des3_context *) ctx, key );
01486 }
01487 
01488 static void * des_ctx_alloc( void )
01489 {
01490     mbedtls_des_context *des = mbedtls_calloc( 1, sizeof( mbedtls_des_context ) );
01491 
01492     if( des == NULL )
01493         return( NULL );
01494 
01495     mbedtls_des_init( des );
01496 
01497     return( des );
01498 }
01499 
01500 static void des_ctx_free( void *ctx )
01501 {
01502     mbedtls_des_free( (mbedtls_des_context *) ctx );
01503     mbedtls_free( ctx );
01504 }
01505 
01506 static void * des3_ctx_alloc( void )
01507 {
01508     mbedtls_des3_context *des3;
01509     des3 = mbedtls_calloc( 1, sizeof( mbedtls_des3_context ) );
01510 
01511     if( des3 == NULL )
01512         return( NULL );
01513 
01514     mbedtls_des3_init( des3 );
01515 
01516     return( des3 );
01517 }
01518 
01519 static void des3_ctx_free( void *ctx )
01520 {
01521     mbedtls_des3_free( (mbedtls_des3_context *) ctx );
01522     mbedtls_free( ctx );
01523 }
01524 
01525 static const mbedtls_cipher_base_t des_info = {
01526     MBEDTLS_CIPHER_ID_DES,
01527     des_crypt_ecb_wrap,
01528 #if defined(MBEDTLS_CIPHER_MODE_CBC)
01529     des_crypt_cbc_wrap,
01530 #endif
01531 #if defined(MBEDTLS_CIPHER_MODE_CFB)
01532     NULL,
01533 #endif
01534 #if defined(MBEDTLS_CIPHER_MODE_OFB)
01535     NULL,
01536 #endif
01537 #if defined(MBEDTLS_CIPHER_MODE_CTR)
01538     NULL,
01539 #endif
01540 #if defined(MBEDTLS_CIPHER_MODE_XTS)
01541     NULL,
01542 #endif
01543 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
01544     NULL,
01545 #endif
01546     des_setkey_enc_wrap,
01547     des_setkey_dec_wrap,
01548     des_ctx_alloc,
01549     des_ctx_free
01550 };
01551 
01552 static const mbedtls_cipher_info_t des_ecb_info = {
01553     MBEDTLS_CIPHER_DES_ECB,
01554     MBEDTLS_MODE_ECB,
01555     MBEDTLS_KEY_LENGTH_DES,
01556     "DES-ECB",
01557     8,
01558     0,
01559     8,
01560     &des_info
01561 };
01562 
01563 #if defined(MBEDTLS_CIPHER_MODE_CBC)
01564 static const mbedtls_cipher_info_t des_cbc_info = {
01565     MBEDTLS_CIPHER_DES_CBC,
01566     MBEDTLS_MODE_CBC,
01567     MBEDTLS_KEY_LENGTH_DES,
01568     "DES-CBC",
01569     8,
01570     0,
01571     8,
01572     &des_info
01573 };
01574 #endif /* MBEDTLS_CIPHER_MODE_CBC */
01575 
01576 static const mbedtls_cipher_base_t des_ede_info = {
01577     MBEDTLS_CIPHER_ID_DES,
01578     des3_crypt_ecb_wrap,
01579 #if defined(MBEDTLS_CIPHER_MODE_CBC)
01580     des3_crypt_cbc_wrap,
01581 #endif
01582 #if defined(MBEDTLS_CIPHER_MODE_CFB)
01583     NULL,
01584 #endif
01585 #if defined(MBEDTLS_CIPHER_MODE_OFB)
01586     NULL,
01587 #endif
01588 #if defined(MBEDTLS_CIPHER_MODE_CTR)
01589     NULL,
01590 #endif
01591 #if defined(MBEDTLS_CIPHER_MODE_XTS)
01592     NULL,
01593 #endif
01594 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
01595     NULL,
01596 #endif
01597     des3_set2key_enc_wrap,
01598     des3_set2key_dec_wrap,
01599     des3_ctx_alloc,
01600     des3_ctx_free
01601 };
01602 
01603 static const mbedtls_cipher_info_t des_ede_ecb_info = {
01604     MBEDTLS_CIPHER_DES_EDE_ECB,
01605     MBEDTLS_MODE_ECB,
01606     MBEDTLS_KEY_LENGTH_DES_EDE,
01607     "DES-EDE-ECB",
01608     8,
01609     0,
01610     8,
01611     &des_ede_info
01612 };
01613 
01614 #if defined(MBEDTLS_CIPHER_MODE_CBC)
01615 static const mbedtls_cipher_info_t des_ede_cbc_info = {
01616     MBEDTLS_CIPHER_DES_EDE_CBC,
01617     MBEDTLS_MODE_CBC,
01618     MBEDTLS_KEY_LENGTH_DES_EDE,
01619     "DES-EDE-CBC",
01620     8,
01621     0,
01622     8,
01623     &des_ede_info
01624 };
01625 #endif /* MBEDTLS_CIPHER_MODE_CBC */
01626 
01627 static const mbedtls_cipher_base_t des_ede3_info = {
01628     MBEDTLS_CIPHER_ID_3DES,
01629     des3_crypt_ecb_wrap,
01630 #if defined(MBEDTLS_CIPHER_MODE_CBC)
01631     des3_crypt_cbc_wrap,
01632 #endif
01633 #if defined(MBEDTLS_CIPHER_MODE_CFB)
01634     NULL,
01635 #endif
01636 #if defined(MBEDTLS_CIPHER_MODE_OFB)
01637     NULL,
01638 #endif
01639 #if defined(MBEDTLS_CIPHER_MODE_CTR)
01640     NULL,
01641 #endif
01642 #if defined(MBEDTLS_CIPHER_MODE_XTS)
01643     NULL,
01644 #endif
01645 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
01646     NULL,
01647 #endif
01648     des3_set3key_enc_wrap,
01649     des3_set3key_dec_wrap,
01650     des3_ctx_alloc,
01651     des3_ctx_free
01652 };
01653 
01654 static const mbedtls_cipher_info_t des_ede3_ecb_info = {
01655     MBEDTLS_CIPHER_DES_EDE3_ECB,
01656     MBEDTLS_MODE_ECB,
01657     MBEDTLS_KEY_LENGTH_DES_EDE3,
01658     "DES-EDE3-ECB",
01659     8,
01660     0,
01661     8,
01662     &des_ede3_info
01663 };
01664 #if defined(MBEDTLS_CIPHER_MODE_CBC)
01665 static const mbedtls_cipher_info_t des_ede3_cbc_info = {
01666     MBEDTLS_CIPHER_DES_EDE3_CBC,
01667     MBEDTLS_MODE_CBC,
01668     MBEDTLS_KEY_LENGTH_DES_EDE3,
01669     "DES-EDE3-CBC",
01670     8,
01671     0,
01672     8,
01673     &des_ede3_info
01674 };
01675 #endif /* MBEDTLS_CIPHER_MODE_CBC */
01676 #endif /* MBEDTLS_DES_C */
01677 
01678 #if defined(MBEDTLS_BLOWFISH_C)
01679 
01680 static int blowfish_crypt_ecb_wrap( void *ctx, mbedtls_operation_t operation,
01681         const unsigned char *input, unsigned char *output )
01682 {
01683     return mbedtls_blowfish_crypt_ecb( (mbedtls_blowfish_context *) ctx, operation, input,
01684                                output );
01685 }
01686 
01687 #if defined(MBEDTLS_CIPHER_MODE_CBC)
01688 static int blowfish_crypt_cbc_wrap( void *ctx, mbedtls_operation_t operation,
01689         size_t length, unsigned char *iv, const unsigned char *input,
01690         unsigned char *output )
01691 {
01692     return mbedtls_blowfish_crypt_cbc( (mbedtls_blowfish_context *) ctx, operation, length, iv,
01693                                input, output );
01694 }
01695 #endif /* MBEDTLS_CIPHER_MODE_CBC */
01696 
01697 #if defined(MBEDTLS_CIPHER_MODE_CFB)
01698 static int blowfish_crypt_cfb64_wrap( void *ctx, mbedtls_operation_t operation,
01699         size_t length, size_t *iv_off, unsigned char *iv,
01700         const unsigned char *input, unsigned char *output )
01701 {
01702     return mbedtls_blowfish_crypt_cfb64( (mbedtls_blowfish_context *) ctx, operation, length,
01703                                  iv_off, iv, input, output );
01704 }
01705 #endif /* MBEDTLS_CIPHER_MODE_CFB */
01706 
01707 #if defined(MBEDTLS_CIPHER_MODE_CTR)
01708 static int blowfish_crypt_ctr_wrap( void *ctx, size_t length, size_t *nc_off,
01709         unsigned char *nonce_counter, unsigned char *stream_block,
01710         const unsigned char *input, unsigned char *output )
01711 {
01712     return mbedtls_blowfish_crypt_ctr( (mbedtls_blowfish_context *) ctx, length, nc_off,
01713                                nonce_counter, stream_block, input, output );
01714 }
01715 #endif /* MBEDTLS_CIPHER_MODE_CTR */
01716 
01717 static int blowfish_setkey_wrap( void *ctx, const unsigned char *key,
01718                                  unsigned int key_bitlen )
01719 {
01720     return mbedtls_blowfish_setkey( (mbedtls_blowfish_context *) ctx, key, key_bitlen );
01721 }
01722 
01723 static void * blowfish_ctx_alloc( void )
01724 {
01725     mbedtls_blowfish_context *ctx;
01726     ctx = mbedtls_calloc( 1, sizeof( mbedtls_blowfish_context ) );
01727 
01728     if( ctx == NULL )
01729         return( NULL );
01730 
01731     mbedtls_blowfish_init( ctx );
01732 
01733     return( ctx );
01734 }
01735 
01736 static void blowfish_ctx_free( void *ctx )
01737 {
01738     mbedtls_blowfish_free( (mbedtls_blowfish_context *) ctx );
01739     mbedtls_free( ctx );
01740 }
01741 
01742 static const mbedtls_cipher_base_t blowfish_info = {
01743     MBEDTLS_CIPHER_ID_BLOWFISH,
01744     blowfish_crypt_ecb_wrap,
01745 #if defined(MBEDTLS_CIPHER_MODE_CBC)
01746     blowfish_crypt_cbc_wrap,
01747 #endif
01748 #if defined(MBEDTLS_CIPHER_MODE_CFB)
01749     blowfish_crypt_cfb64_wrap,
01750 #endif
01751 #if defined(MBEDTLS_CIPHER_MODE_OFB)
01752     NULL,
01753 #endif
01754 #if defined(MBEDTLS_CIPHER_MODE_CTR)
01755     blowfish_crypt_ctr_wrap,
01756 #endif
01757 #if defined(MBEDTLS_CIPHER_MODE_XTS)
01758     NULL,
01759 #endif
01760 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
01761     NULL,
01762 #endif
01763     blowfish_setkey_wrap,
01764     blowfish_setkey_wrap,
01765     blowfish_ctx_alloc,
01766     blowfish_ctx_free
01767 };
01768 
01769 static const mbedtls_cipher_info_t blowfish_ecb_info = {
01770     MBEDTLS_CIPHER_BLOWFISH_ECB,
01771     MBEDTLS_MODE_ECB,
01772     128,
01773     "BLOWFISH-ECB",
01774     8,
01775     MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
01776     8,
01777     &blowfish_info
01778 };
01779 
01780 #if defined(MBEDTLS_CIPHER_MODE_CBC)
01781 static const mbedtls_cipher_info_t blowfish_cbc_info = {
01782     MBEDTLS_CIPHER_BLOWFISH_CBC,
01783     MBEDTLS_MODE_CBC,
01784     128,
01785     "BLOWFISH-CBC",
01786     8,
01787     MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
01788     8,
01789     &blowfish_info
01790 };
01791 #endif /* MBEDTLS_CIPHER_MODE_CBC */
01792 
01793 #if defined(MBEDTLS_CIPHER_MODE_CFB)
01794 static const mbedtls_cipher_info_t blowfish_cfb64_info = {
01795     MBEDTLS_CIPHER_BLOWFISH_CFB64,
01796     MBEDTLS_MODE_CFB,
01797     128,
01798     "BLOWFISH-CFB64",
01799     8,
01800     MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
01801     8,
01802     &blowfish_info
01803 };
01804 #endif /* MBEDTLS_CIPHER_MODE_CFB */
01805 
01806 #if defined(MBEDTLS_CIPHER_MODE_CTR)
01807 static const mbedtls_cipher_info_t blowfish_ctr_info = {
01808     MBEDTLS_CIPHER_BLOWFISH_CTR,
01809     MBEDTLS_MODE_CTR,
01810     128,
01811     "BLOWFISH-CTR",
01812     8,
01813     MBEDTLS_CIPHER_VARIABLE_KEY_LEN,
01814     8,
01815     &blowfish_info
01816 };
01817 #endif /* MBEDTLS_CIPHER_MODE_CTR */
01818 #endif /* MBEDTLS_BLOWFISH_C */
01819 
01820 #if defined(MBEDTLS_ARC4_C)
01821 static int arc4_crypt_stream_wrap( void *ctx, size_t length,
01822                                    const unsigned char *input,
01823                                    unsigned char *output )
01824 {
01825     return( mbedtls_arc4_crypt( (mbedtls_arc4_context *) ctx, length, input, output ) );
01826 }
01827 
01828 static int arc4_setkey_wrap( void *ctx, const unsigned char *key,
01829                              unsigned int key_bitlen )
01830 {
01831     /* we get key_bitlen in bits, arc4 expects it in bytes */
01832     if( key_bitlen % 8 != 0 )
01833         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
01834 
01835     mbedtls_arc4_setup( (mbedtls_arc4_context *) ctx, key, key_bitlen / 8 );
01836     return( 0 );
01837 }
01838 
01839 static void * arc4_ctx_alloc( void )
01840 {
01841     mbedtls_arc4_context *ctx;
01842     ctx = mbedtls_calloc( 1, sizeof( mbedtls_arc4_context ) );
01843 
01844     if( ctx == NULL )
01845         return( NULL );
01846 
01847     mbedtls_arc4_init( ctx );
01848 
01849     return( ctx );
01850 }
01851 
01852 static void arc4_ctx_free( void *ctx )
01853 {
01854     mbedtls_arc4_free( (mbedtls_arc4_context *) ctx );
01855     mbedtls_free( ctx );
01856 }
01857 
01858 static const mbedtls_cipher_base_t arc4_base_info = {
01859     MBEDTLS_CIPHER_ID_ARC4,
01860     NULL,
01861 #if defined(MBEDTLS_CIPHER_MODE_CBC)
01862     NULL,
01863 #endif
01864 #if defined(MBEDTLS_CIPHER_MODE_CFB)
01865     NULL,
01866 #endif
01867 #if defined(MBEDTLS_CIPHER_MODE_OFB)
01868     NULL,
01869 #endif
01870 #if defined(MBEDTLS_CIPHER_MODE_CTR)
01871     NULL,
01872 #endif
01873 #if defined(MBEDTLS_CIPHER_MODE_XTS)
01874     NULL,
01875 #endif
01876 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
01877     arc4_crypt_stream_wrap,
01878 #endif
01879     arc4_setkey_wrap,
01880     arc4_setkey_wrap,
01881     arc4_ctx_alloc,
01882     arc4_ctx_free
01883 };
01884 
01885 static const mbedtls_cipher_info_t arc4_128_info = {
01886     MBEDTLS_CIPHER_ARC4_128,
01887     MBEDTLS_MODE_STREAM,
01888     128,
01889     "ARC4-128",
01890     0,
01891     0,
01892     1,
01893     &arc4_base_info
01894 };
01895 #endif /* MBEDTLS_ARC4_C */
01896 
01897 #if defined(MBEDTLS_CHACHA20_C)
01898 
01899 static int chacha20_setkey_wrap( void *ctx, const unsigned char *key,
01900                                  unsigned int key_bitlen )
01901 {
01902     if( key_bitlen != 256U )
01903         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
01904 
01905     if ( 0 != mbedtls_chacha20_setkey( (mbedtls_chacha20_context*)ctx, key ) )
01906         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
01907 
01908     return( 0 );
01909 }
01910 
01911 static int chacha20_stream_wrap( void *ctx,  size_t length,
01912                                  const unsigned char *input,
01913                                  unsigned char *output )
01914 {
01915     int ret;
01916 
01917     ret = mbedtls_chacha20_update( ctx, length, input, output );
01918     if( ret == MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA )
01919         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
01920 
01921     return( ret );
01922 }
01923 
01924 static void * chacha20_ctx_alloc( void )
01925 {
01926     mbedtls_chacha20_context *ctx;
01927     ctx = mbedtls_calloc( 1, sizeof( mbedtls_chacha20_context ) );
01928 
01929     if( ctx == NULL )
01930         return( NULL );
01931 
01932     mbedtls_chacha20_init( ctx );
01933 
01934     return( ctx );
01935 }
01936 
01937 static void chacha20_ctx_free( void *ctx )
01938 {
01939     mbedtls_chacha20_free( (mbedtls_chacha20_context *) ctx );
01940     mbedtls_free( ctx );
01941 }
01942 
01943 static const mbedtls_cipher_base_t chacha20_base_info = {
01944     MBEDTLS_CIPHER_ID_CHACHA20,
01945     NULL,
01946 #if defined(MBEDTLS_CIPHER_MODE_CBC)
01947     NULL,
01948 #endif
01949 #if defined(MBEDTLS_CIPHER_MODE_CFB)
01950     NULL,
01951 #endif
01952 #if defined(MBEDTLS_CIPHER_MODE_OFB)
01953     NULL,
01954 #endif
01955 #if defined(MBEDTLS_CIPHER_MODE_CTR)
01956     NULL,
01957 #endif
01958 #if defined(MBEDTLS_CIPHER_MODE_XTS)
01959     NULL,
01960 #endif
01961 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
01962     chacha20_stream_wrap,
01963 #endif
01964     chacha20_setkey_wrap,
01965     chacha20_setkey_wrap,
01966     chacha20_ctx_alloc,
01967     chacha20_ctx_free
01968 };
01969 static const mbedtls_cipher_info_t chacha20_info = {
01970     MBEDTLS_CIPHER_CHACHA20,
01971     MBEDTLS_MODE_STREAM,
01972     256,
01973     "CHACHA20",
01974     12,
01975     0,
01976     1,
01977     &chacha20_base_info
01978 };
01979 #endif /* MBEDTLS_CHACHA20_C */
01980 
01981 #if defined(MBEDTLS_CHACHAPOLY_C)
01982 
01983 static int chachapoly_setkey_wrap( void *ctx,
01984                                    const unsigned char *key,
01985                                    unsigned int key_bitlen )
01986 {
01987     if( key_bitlen != 256U )
01988         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
01989 
01990     if ( 0 != mbedtls_chachapoly_setkey( (mbedtls_chachapoly_context*)ctx, key ) )
01991         return( MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA );
01992 
01993     return( 0 );
01994 }
01995 
01996 static void * chachapoly_ctx_alloc( void )
01997 {
01998     mbedtls_chachapoly_context *ctx;
01999     ctx = mbedtls_calloc( 1, sizeof( mbedtls_chachapoly_context ) );
02000 
02001     if( ctx == NULL )
02002         return( NULL );
02003 
02004     mbedtls_chachapoly_init( ctx );
02005 
02006     return( ctx );
02007 }
02008 
02009 static void chachapoly_ctx_free( void *ctx )
02010 {
02011     mbedtls_chachapoly_free( (mbedtls_chachapoly_context *) ctx );
02012     mbedtls_free( ctx );
02013 }
02014 
02015 static const mbedtls_cipher_base_t chachapoly_base_info = {
02016     MBEDTLS_CIPHER_ID_CHACHA20,
02017     NULL,
02018 #if defined(MBEDTLS_CIPHER_MODE_CBC)
02019     NULL,
02020 #endif
02021 #if defined(MBEDTLS_CIPHER_MODE_CFB)
02022     NULL,
02023 #endif
02024 #if defined(MBEDTLS_CIPHER_MODE_OFB)
02025     NULL,
02026 #endif
02027 #if defined(MBEDTLS_CIPHER_MODE_CTR)
02028     NULL,
02029 #endif
02030 #if defined(MBEDTLS_CIPHER_MODE_XTS)
02031     NULL,
02032 #endif
02033 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
02034     NULL,
02035 #endif
02036     chachapoly_setkey_wrap,
02037     chachapoly_setkey_wrap,
02038     chachapoly_ctx_alloc,
02039     chachapoly_ctx_free
02040 };
02041 static const mbedtls_cipher_info_t chachapoly_info = {
02042     MBEDTLS_CIPHER_CHACHA20_POLY1305,
02043     MBEDTLS_MODE_CHACHAPOLY,
02044     256,
02045     "CHACHA20-POLY1305",
02046     12,
02047     0,
02048     1,
02049     &chachapoly_base_info
02050 };
02051 #endif /* MBEDTLS_CHACHAPOLY_C */
02052 
02053 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
02054 static int null_crypt_stream( void *ctx, size_t length,
02055                               const unsigned char *input,
02056                               unsigned char *output )
02057 {
02058     ((void) ctx);
02059     memmove( output, input, length );
02060     return( 0 );
02061 }
02062 
02063 static int null_setkey( void *ctx, const unsigned char *key,
02064                         unsigned int key_bitlen )
02065 {
02066     ((void) ctx);
02067     ((void) key);
02068     ((void) key_bitlen);
02069 
02070     return( 0 );
02071 }
02072 
02073 static void * null_ctx_alloc( void )
02074 {
02075     return( (void *) 1 );
02076 }
02077 
02078 static void null_ctx_free( void *ctx )
02079 {
02080     ((void) ctx);
02081 }
02082 
02083 static const mbedtls_cipher_base_t null_base_info = {
02084     MBEDTLS_CIPHER_ID_NULL,
02085     NULL,
02086 #if defined(MBEDTLS_CIPHER_MODE_CBC)
02087     NULL,
02088 #endif
02089 #if defined(MBEDTLS_CIPHER_MODE_CFB)
02090     NULL,
02091 #endif
02092 #if defined(MBEDTLS_CIPHER_MODE_OFB)
02093     NULL,
02094 #endif
02095 #if defined(MBEDTLS_CIPHER_MODE_CTR)
02096     NULL,
02097 #endif
02098 #if defined(MBEDTLS_CIPHER_MODE_XTS)
02099     NULL,
02100 #endif
02101 #if defined(MBEDTLS_CIPHER_MODE_STREAM)
02102     null_crypt_stream,
02103 #endif
02104     null_setkey,
02105     null_setkey,
02106     null_ctx_alloc,
02107     null_ctx_free
02108 };
02109 
02110 static const mbedtls_cipher_info_t null_cipher_info = {
02111     MBEDTLS_CIPHER_NULL,
02112     MBEDTLS_MODE_STREAM,
02113     0,
02114     "NULL",
02115     0,
02116     0,
02117     1,
02118     &null_base_info
02119 };
02120 #endif /* defined(MBEDTLS_CIPHER_NULL_CIPHER) */
02121 
02122 const mbedtls_cipher_definition_t mbedtls_cipher_definitions[] =
02123 {
02124 #if defined(MBEDTLS_AES_C)
02125     { MBEDTLS_CIPHER_AES_128_ECB,          &aes_128_ecb_info },
02126     { MBEDTLS_CIPHER_AES_192_ECB,          &aes_192_ecb_info },
02127     { MBEDTLS_CIPHER_AES_256_ECB,          &aes_256_ecb_info },
02128 #if defined(MBEDTLS_CIPHER_MODE_CBC)
02129     { MBEDTLS_CIPHER_AES_128_CBC,          &aes_128_cbc_info },
02130     { MBEDTLS_CIPHER_AES_192_CBC,          &aes_192_cbc_info },
02131     { MBEDTLS_CIPHER_AES_256_CBC,          &aes_256_cbc_info },
02132 #endif
02133 #if defined(MBEDTLS_CIPHER_MODE_CFB)
02134     { MBEDTLS_CIPHER_AES_128_CFB128,       &aes_128_cfb128_info },
02135     { MBEDTLS_CIPHER_AES_192_CFB128,       &aes_192_cfb128_info },
02136     { MBEDTLS_CIPHER_AES_256_CFB128,       &aes_256_cfb128_info },
02137 #endif
02138 #if defined(MBEDTLS_CIPHER_MODE_OFB)
02139     { MBEDTLS_CIPHER_AES_128_OFB,          &aes_128_ofb_info },
02140     { MBEDTLS_CIPHER_AES_192_OFB,          &aes_192_ofb_info },
02141     { MBEDTLS_CIPHER_AES_256_OFB,          &aes_256_ofb_info },
02142 #endif
02143 #if defined(MBEDTLS_CIPHER_MODE_CTR)
02144     { MBEDTLS_CIPHER_AES_128_CTR,          &aes_128_ctr_info },
02145     { MBEDTLS_CIPHER_AES_192_CTR,          &aes_192_ctr_info },
02146     { MBEDTLS_CIPHER_AES_256_CTR,          &aes_256_ctr_info },
02147 #endif
02148 #if defined(MBEDTLS_CIPHER_MODE_XTS)
02149     { MBEDTLS_CIPHER_AES_128_XTS,          &aes_128_xts_info },
02150     { MBEDTLS_CIPHER_AES_256_XTS,          &aes_256_xts_info },
02151 #endif
02152 #if defined(MBEDTLS_GCM_C)
02153     { MBEDTLS_CIPHER_AES_128_GCM,          &aes_128_gcm_info },
02154     { MBEDTLS_CIPHER_AES_192_GCM,          &aes_192_gcm_info },
02155     { MBEDTLS_CIPHER_AES_256_GCM,          &aes_256_gcm_info },
02156 #endif
02157 #if defined(MBEDTLS_CCM_C)
02158     { MBEDTLS_CIPHER_AES_128_CCM,          &aes_128_ccm_info },
02159     { MBEDTLS_CIPHER_AES_192_CCM,          &aes_192_ccm_info },
02160     { MBEDTLS_CIPHER_AES_256_CCM,          &aes_256_ccm_info },
02161 #endif
02162 #endif /* MBEDTLS_AES_C */
02163 
02164 #if defined(MBEDTLS_ARC4_C)
02165     { MBEDTLS_CIPHER_ARC4_128,             &arc4_128_info },
02166 #endif
02167 
02168 #if defined(MBEDTLS_BLOWFISH_C)
02169     { MBEDTLS_CIPHER_BLOWFISH_ECB,         &blowfish_ecb_info },
02170 #if defined(MBEDTLS_CIPHER_MODE_CBC)
02171     { MBEDTLS_CIPHER_BLOWFISH_CBC,         &blowfish_cbc_info },
02172 #endif
02173 #if defined(MBEDTLS_CIPHER_MODE_CFB)
02174     { MBEDTLS_CIPHER_BLOWFISH_CFB64,       &blowfish_cfb64_info },
02175 #endif
02176 #if defined(MBEDTLS_CIPHER_MODE_CTR)
02177     { MBEDTLS_CIPHER_BLOWFISH_CTR,         &blowfish_ctr_info },
02178 #endif
02179 #endif /* MBEDTLS_BLOWFISH_C */
02180 
02181 #if defined(MBEDTLS_CAMELLIA_C)
02182     { MBEDTLS_CIPHER_CAMELLIA_128_ECB,     &camellia_128_ecb_info },
02183     { MBEDTLS_CIPHER_CAMELLIA_192_ECB,     &camellia_192_ecb_info },
02184     { MBEDTLS_CIPHER_CAMELLIA_256_ECB,     &camellia_256_ecb_info },
02185 #if defined(MBEDTLS_CIPHER_MODE_CBC)
02186     { MBEDTLS_CIPHER_CAMELLIA_128_CBC,     &camellia_128_cbc_info },
02187     { MBEDTLS_CIPHER_CAMELLIA_192_CBC,     &camellia_192_cbc_info },
02188     { MBEDTLS_CIPHER_CAMELLIA_256_CBC,     &camellia_256_cbc_info },
02189 #endif
02190 #if defined(MBEDTLS_CIPHER_MODE_CFB)
02191     { MBEDTLS_CIPHER_CAMELLIA_128_CFB128,  &camellia_128_cfb128_info },
02192     { MBEDTLS_CIPHER_CAMELLIA_192_CFB128,  &camellia_192_cfb128_info },
02193     { MBEDTLS_CIPHER_CAMELLIA_256_CFB128,  &camellia_256_cfb128_info },
02194 #endif
02195 #if defined(MBEDTLS_CIPHER_MODE_CTR)
02196     { MBEDTLS_CIPHER_CAMELLIA_128_CTR,     &camellia_128_ctr_info },
02197     { MBEDTLS_CIPHER_CAMELLIA_192_CTR,     &camellia_192_ctr_info },
02198     { MBEDTLS_CIPHER_CAMELLIA_256_CTR,     &camellia_256_ctr_info },
02199 #endif
02200 #if defined(MBEDTLS_GCM_C)
02201     { MBEDTLS_CIPHER_CAMELLIA_128_GCM,     &camellia_128_gcm_info },
02202     { MBEDTLS_CIPHER_CAMELLIA_192_GCM,     &camellia_192_gcm_info },
02203     { MBEDTLS_CIPHER_CAMELLIA_256_GCM,     &camellia_256_gcm_info },
02204 #endif
02205 #if defined(MBEDTLS_CCM_C)
02206     { MBEDTLS_CIPHER_CAMELLIA_128_CCM,     &camellia_128_ccm_info },
02207     { MBEDTLS_CIPHER_CAMELLIA_192_CCM,     &camellia_192_ccm_info },
02208     { MBEDTLS_CIPHER_CAMELLIA_256_CCM,     &camellia_256_ccm_info },
02209 #endif
02210 #endif /* MBEDTLS_CAMELLIA_C */
02211 
02212 #if defined(MBEDTLS_ARIA_C)
02213     { MBEDTLS_CIPHER_ARIA_128_ECB,     &aria_128_ecb_info },
02214     { MBEDTLS_CIPHER_ARIA_192_ECB,     &aria_192_ecb_info },
02215     { MBEDTLS_CIPHER_ARIA_256_ECB,     &aria_256_ecb_info },
02216 #if defined(MBEDTLS_CIPHER_MODE_CBC)
02217     { MBEDTLS_CIPHER_ARIA_128_CBC,     &aria_128_cbc_info },
02218     { MBEDTLS_CIPHER_ARIA_192_CBC,     &aria_192_cbc_info },
02219     { MBEDTLS_CIPHER_ARIA_256_CBC,     &aria_256_cbc_info },
02220 #endif
02221 #if defined(MBEDTLS_CIPHER_MODE_CFB)
02222     { MBEDTLS_CIPHER_ARIA_128_CFB128,  &aria_128_cfb128_info },
02223     { MBEDTLS_CIPHER_ARIA_192_CFB128,  &aria_192_cfb128_info },
02224     { MBEDTLS_CIPHER_ARIA_256_CFB128,  &aria_256_cfb128_info },
02225 #endif
02226 #if defined(MBEDTLS_CIPHER_MODE_CTR)
02227     { MBEDTLS_CIPHER_ARIA_128_CTR,     &aria_128_ctr_info },
02228     { MBEDTLS_CIPHER_ARIA_192_CTR,     &aria_192_ctr_info },
02229     { MBEDTLS_CIPHER_ARIA_256_CTR,     &aria_256_ctr_info },
02230 #endif
02231 #if defined(MBEDTLS_GCM_C)
02232     { MBEDTLS_CIPHER_ARIA_128_GCM,     &aria_128_gcm_info },
02233     { MBEDTLS_CIPHER_ARIA_192_GCM,     &aria_192_gcm_info },
02234     { MBEDTLS_CIPHER_ARIA_256_GCM,     &aria_256_gcm_info },
02235 #endif
02236 #if defined(MBEDTLS_CCM_C)
02237     { MBEDTLS_CIPHER_ARIA_128_CCM,     &aria_128_ccm_info },
02238     { MBEDTLS_CIPHER_ARIA_192_CCM,     &aria_192_ccm_info },
02239     { MBEDTLS_CIPHER_ARIA_256_CCM,     &aria_256_ccm_info },
02240 #endif
02241 #endif /* MBEDTLS_ARIA_C */
02242 
02243 #if defined(MBEDTLS_DES_C)
02244     { MBEDTLS_CIPHER_DES_ECB,              &des_ecb_info },
02245     { MBEDTLS_CIPHER_DES_EDE_ECB,          &des_ede_ecb_info },
02246     { MBEDTLS_CIPHER_DES_EDE3_ECB,         &des_ede3_ecb_info },
02247 #if defined(MBEDTLS_CIPHER_MODE_CBC)
02248     { MBEDTLS_CIPHER_DES_CBC,              &des_cbc_info },
02249     { MBEDTLS_CIPHER_DES_EDE_CBC,          &des_ede_cbc_info },
02250     { MBEDTLS_CIPHER_DES_EDE3_CBC,         &des_ede3_cbc_info },
02251 #endif
02252 #endif /* MBEDTLS_DES_C */
02253 
02254 #if defined(MBEDTLS_CHACHA20_C)
02255     { MBEDTLS_CIPHER_CHACHA20,             &chacha20_info },
02256 #endif
02257 
02258 #if defined(MBEDTLS_CHACHAPOLY_C)
02259     { MBEDTLS_CIPHER_CHACHA20_POLY1305,    &chachapoly_info },
02260 #endif
02261 
02262 #if defined(MBEDTLS_CIPHER_NULL_CIPHER)
02263     { MBEDTLS_CIPHER_NULL,                 &null_cipher_info },
02264 #endif /* MBEDTLS_CIPHER_NULL_CIPHER */
02265 
02266     { MBEDTLS_CIPHER_NONE, NULL }
02267 };
02268 
02269 #define NUM_CIPHERS sizeof mbedtls_cipher_definitions / sizeof mbedtls_cipher_definitions[0]
02270 int mbedtls_cipher_supported[NUM_CIPHERS];
02271 
02272 #endif /* MBEDTLS_CIPHER_C */