Kenji Arai / mbed-os_TYBLE16

Dependents:   TYBLE16_simple_data_logger TYBLE16_MP3_Air

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