LCOV - code coverage report
Current view: top level - third_party/heimdal/lib/hcrypto - evp.c (source / functions) Hit Total Coverage
Test: coverage report for v4-17-test 1498b464 Lines: 126 353 35.7 %
Date: 2024-06-13 04:01:37 Functions: 28 70 40.0 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2006 - 2016 Kungliga Tekniska Högskolan
       3             :  * (Royal Institute of Technology, Stockholm, Sweden).
       4             :  * All rights reserved.
       5             :  *
       6             :  * Redistribution and use in source and binary forms, with or without
       7             :  * modification, are permitted provided that the following conditions
       8             :  * are met:
       9             :  *
      10             :  * 1. Redistributions of source code must retain the above copyright
      11             :  *    notice, this list of conditions and the following disclaimer.
      12             :  *
      13             :  * 2. Redistributions in binary form must reproduce the above copyright
      14             :  *    notice, this list of conditions and the following disclaimer in the
      15             :  *    documentation and/or other materials provided with the distribution.
      16             :  *
      17             :  * 3. Neither the name of the Institute nor the names of its contributors
      18             :  *    may be used to endorse or promote products derived from this software
      19             :  *    without specific prior written permission.
      20             :  *
      21             :  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
      22             :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      23             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      24             :  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
      25             :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      26             :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      27             :  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      28             :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      29             :  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      30             :  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      31             :  * SUCH DAMAGE.
      32             :  */
      33             : 
      34             : #ifdef HAVE_CONFIG_H
      35             : #include <config.h>
      36             : #endif
      37             : #include <roken.h>
      38             : 
      39             : #define HC_DEPRECATED
      40             : #define HC_DEPRECATED_CRYPTO
      41             : 
      42             : #include <assert.h>
      43             : 
      44             : #include <evp.h>
      45             : #include <evp-hcrypto.h>
      46             : #include <evp-cc.h>
      47             : #if defined(_WIN32)
      48             : #include <evp-w32.h>
      49             : #endif
      50             : #include <evp-pkcs11.h>
      51             : #include <evp-openssl.h>
      52             : 
      53             : #include <krb5-types.h>
      54             : 
      55             : #ifndef HCRYPTO_DEF_PROVIDER
      56             : # ifdef __APPLE__
      57             : #  define HCRYPTO_DEF_PROVIDER cc
      58             : # elif __sun
      59             : #  define HCRYPTO_DEF_PROVIDER pkcs11_hcrypto
      60             : # elif HAVE_HCRYPTO_W_OPENSSL
      61             : #  define HCRYPTO_DEF_PROVIDER ossl
      62             : # else
      63             : #  define HCRYPTO_DEF_PROVIDER hcrypto
      64             : # endif
      65             : #endif
      66             : 
      67             : #define HC_CONCAT4(x,y,z,aa)    x ## y ## z ## aa
      68             : 
      69             : 
      70             : #define EVP_DEF_OP(_prov,_op) HC_CONCAT4(EVP_,_prov,_,_op)()
      71             : 
      72             : /**
      73             :  * @page page_evp EVP - generic crypto interface
      74             :  *
      75             :  * See the library functions here: @ref hcrypto_evp
      76             :  *
      77             :  * @section evp_cipher EVP Cipher
      78             :  *
      79             :  * The use of EVP_CipherInit_ex() and EVP_Cipher() is pretty easy to
      80             :  * understand forward, then EVP_CipherUpdate() and
      81             :  * EVP_CipherFinal_ex() really needs an example to explain @ref
      82             :  * example_evp_cipher.c .
      83             :  *
      84             :  * @example example_evp_cipher.c
      85             :  *
      86             :  * This is an example how to use EVP_CipherInit_ex(),
      87             :  * EVP_CipherUpdate() and EVP_CipherFinal_ex().
      88             :  */
      89             : 
      90             : struct hc_EVP_MD_CTX {
      91             :     const EVP_MD *md;
      92             :     ENGINE *engine;
      93             :     void *ptr;
      94             : };
      95             : 
      96             : 
      97             : /**
      98             :  * Return the output size of the message digest function.
      99             :  *
     100             :  * @param md the evp message
     101             :  *
     102             :  * @return size output size of the message digest function.
     103             :  *
     104             :  * @ingroup hcrypto_evp
     105             :  */
     106             : 
     107             : size_t
     108   638131227 : EVP_MD_size(const EVP_MD *md)
     109             : {
     110   638131227 :     return md->hash_size;
     111             : }
     112             : 
     113             : /**
     114             :  * Return the blocksize of the message digest function.
     115             :  *
     116             :  * @param md the evp message
     117             :  *
     118             :  * @return size size of the message digest block size
     119             :  *
     120             :  * @ingroup hcrypto_evp
     121             :  */
     122             : 
     123             : size_t
     124  2368279609 : EVP_MD_block_size(const EVP_MD *md)
     125             : {
     126  2368279609 :     return md->block_size;
     127             : }
     128             : 
     129             : /**
     130             :  * Allocate a messsage digest context object. Free with
     131             :  * EVP_MD_CTX_destroy().
     132             :  *
     133             :  * @return a newly allocated message digest context object.
     134             :  *
     135             :  * @ingroup hcrypto_evp
     136             :  */
     137             : 
     138             : EVP_MD_CTX *
     139   639835896 : EVP_MD_CTX_create(void)
     140             : {
     141   639835896 :     return calloc(1, sizeof(EVP_MD_CTX));
     142             : }
     143             : 
     144             : /**
     145             :  * Initiate a messsage digest context object. Deallocate with
     146             :  * EVP_MD_CTX_cleanup(). Please use EVP_MD_CTX_create() instead.
     147             :  *
     148             :  * @param ctx variable to initiate.
     149             :  *
     150             :  * @ingroup hcrypto_evp
     151             :  */
     152             : 
     153             : void
     154           0 : EVP_MD_CTX_init(EVP_MD_CTX *ctx) HC_DEPRECATED
     155             : {
     156           0 :     memset(ctx, 0, sizeof(*ctx));
     157           0 : }
     158             : 
     159             : /**
     160             :  * Free a messsage digest context object.
     161             :  *
     162             :  * @param ctx context to free.
     163             :  *
     164             :  * @ingroup hcrypto_evp
     165             :  */
     166             : 
     167             : void
     168   639835896 : EVP_MD_CTX_destroy(EVP_MD_CTX *ctx)
     169             : {
     170   639835896 :     EVP_MD_CTX_cleanup(ctx);
     171   639835896 :     free(ctx);
     172   639835896 : }
     173             : 
     174             : /**
     175             :  * Free the resources used by the EVP_MD context.
     176             :  *
     177             :  * @param ctx the context to free the resources from.
     178             :  *
     179             :  * @return 1 on success.
     180             :  *
     181             :  * @ingroup hcrypto_evp
     182             :  */
     183             : 
     184             : int
     185  1279671792 : EVP_MD_CTX_cleanup(EVP_MD_CTX *ctx) HC_DEPRECATED
     186             : {
     187  1279671792 :     if (ctx->md && ctx->md->cleanup) {
     188           0 :         int ret = (ctx->md->cleanup)(ctx->ptr);
     189           0 :         if (!ret)
     190           0 :             return ret;
     191  1279671792 :     } else if (ctx->md) {
     192   639835896 :         memset(ctx->ptr, 0, ctx->md->ctx_size);
     193             :     }
     194  1279671792 :     ctx->md = NULL;
     195  1279671792 :     ctx->engine = NULL;
     196  1279671792 :     free(ctx->ptr);
     197  1279671792 :     memset(ctx, 0, sizeof(*ctx));
     198  1279671792 :     return 1;
     199             : }
     200             : 
     201             : /**
     202             :  * Get the EVP_MD use for a specified context.
     203             :  *
     204             :  * @param ctx the EVP_MD context to get the EVP_MD for.
     205             :  *
     206             :  * @return the EVP_MD used for the context.
     207             :  *
     208             :  * @ingroup hcrypto_evp
     209             :  */
     210             : 
     211             : const EVP_MD *
     212           0 : EVP_MD_CTX_md(EVP_MD_CTX *ctx)
     213             : {
     214           0 :     return ctx->md;
     215             : }
     216             : 
     217             : /**
     218             :  * Return the output size of the message digest function.
     219             :  *
     220             :  * @param ctx the evp message digest context
     221             :  *
     222             :  * @return size output size of the message digest function.
     223             :  *
     224             :  * @ingroup hcrypto_evp
     225             :  */
     226             : 
     227             : size_t
     228           0 : EVP_MD_CTX_size(EVP_MD_CTX *ctx)
     229             : {
     230           0 :     return EVP_MD_size(ctx->md);
     231             : }
     232             : 
     233             : /**
     234             :  * Return the blocksize of the message digest function.
     235             :  *
     236             :  * @param ctx the evp message digest context
     237             :  *
     238             :  * @return size size of the message digest block size
     239             :  *
     240             :  * @ingroup hcrypto_evp
     241             :  */
     242             : 
     243             : size_t
     244           0 : EVP_MD_CTX_block_size(EVP_MD_CTX *ctx)
     245             : {
     246           0 :     return EVP_MD_block_size(ctx->md);
     247             : }
     248             : 
     249             : /**
     250             :  * Init a EVP_MD_CTX for use a specific message digest and engine.
     251             :  *
     252             :  * @param ctx the message digest context to init.
     253             :  * @param md the message digest to use.
     254             :  * @param engine the engine to use, NULL to use the default engine.
     255             :  *
     256             :  * @return 1 on success.
     257             :  *
     258             :  * @ingroup hcrypto_evp
     259             :  */
     260             : 
     261             : int
     262  1118522537 : EVP_DigestInit_ex(EVP_MD_CTX *ctx, const EVP_MD *md, ENGINE *engine)
     263             : {
     264  1118522537 :     if (ctx->md != md || ctx->engine != engine) {
     265   639835896 :         EVP_MD_CTX_cleanup(ctx);
     266   639835896 :         ctx->md = md;
     267   639835896 :         ctx->engine = engine;
     268   639835896 :         if (md == NULL)
     269           0 :             return 0;
     270             : 
     271   639835896 :         ctx->ptr = calloc(1, md->ctx_size);
     272   639835896 :         if (ctx->ptr == NULL)
     273           0 :             return 0;
     274             :     }
     275  1118522537 :     if (ctx->md == 0)
     276           0 :         return 0;
     277  1118522537 :     return (ctx->md->init)(ctx->ptr);
     278             : }
     279             : 
     280             : /**
     281             :  * Update the digest with some data.
     282             :  *
     283             :  * @param ctx the context to update
     284             :  * @param data the data to update the context with
     285             :  * @param size length of data
     286             :  *
     287             :  * @return 1 on success.
     288             :  *
     289             :  * @ingroup hcrypto_evp
     290             :  */
     291             : 
     292             : int
     293  2072200555 : EVP_DigestUpdate(EVP_MD_CTX *ctx, const void *data, size_t size)
     294             : {
     295  2072200555 :     (ctx->md->update)(ctx->ptr, data, size);
     296  2072200555 :     return 1;
     297             : }
     298             : 
     299             : /**
     300             :  * Complete the message digest.
     301             :  *
     302             :  * @param ctx the context to complete.
     303             :  * @param hash the output of the message digest function. At least
     304             :  * EVP_MD_size().
     305             :  * @param size the output size of hash.
     306             :  *
     307             :  * @return 1 on success.
     308             :  *
     309             :  * @ingroup hcrypto_evp
     310             :  */
     311             : 
     312             : int
     313  1118522537 : EVP_DigestFinal_ex(EVP_MD_CTX *ctx, void *hash, unsigned int *size)
     314             : {
     315  1118522537 :     (ctx->md->final)(hash, ctx->ptr);
     316  1118522537 :     if (size)
     317   474855947 :         *size = ctx->md->hash_size;
     318  1118522537 :     return 1;
     319             : }
     320             : 
     321             : /**
     322             :  * Do the whole EVP_MD_CTX_create(), EVP_DigestInit_ex(),
     323             :  * EVP_DigestUpdate(), EVP_DigestFinal_ex(), EVP_MD_CTX_destroy()
     324             :  * dance in one call.
     325             :  *
     326             :  * @param data the data to update the context with
     327             :  * @param dsize length of data
     328             :  * @param hash output data of at least EVP_MD_size() length.
     329             :  * @param hsize output length of hash.
     330             :  * @param md message digest to use
     331             :  * @param engine engine to use, NULL for default engine.
     332             :  *
     333             :  * @return 1 on success.
     334             :  *
     335             :  * @ingroup hcrypto_evp
     336             :  */
     337             : 
     338             : int
     339   166207488 : EVP_Digest(const void *data, size_t dsize, void *hash, unsigned int *hsize,
     340             :            const EVP_MD *md, ENGINE *engine)
     341             : {
     342             :     EVP_MD_CTX *ctx;
     343             :     int ret;
     344             : 
     345   166207488 :     ctx = EVP_MD_CTX_create();
     346   166207488 :     if (ctx == NULL)
     347           0 :         return 0;
     348   166207488 :     ret = EVP_DigestInit_ex(ctx, md, engine);
     349   166207488 :     if (ret != 1) {
     350           0 :         EVP_MD_CTX_destroy(ctx);
     351           0 :         return ret;
     352             :     }
     353   166207488 :     ret = EVP_DigestUpdate(ctx, data, dsize);
     354   166207488 :     if (ret != 1) {
     355           0 :         EVP_MD_CTX_destroy(ctx);
     356           0 :         return ret;
     357             :     }
     358   166207488 :     ret = EVP_DigestFinal_ex(ctx, hash, hsize);
     359   166207488 :     EVP_MD_CTX_destroy(ctx);
     360   166207488 :     return ret;
     361             : }
     362             : 
     363             : /**
     364             :  * The message digest SHA256
     365             :  *
     366             :  * @return the message digest type.
     367             :  *
     368             :  * @ingroup hcrypto_evp
     369             :  */
     370             : 
     371             : const EVP_MD *
     372           0 : EVP_sha256(void)
     373             : {
     374           0 :     hcrypto_validate();
     375           0 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha256);
     376             : }
     377             : 
     378             : /**
     379             :  * The message digest SHA384
     380             :  *
     381             :  * @return the message digest type.
     382             :  *
     383             :  * @ingroup hcrypto_evp
     384             :  */
     385             : 
     386             : const EVP_MD *
     387           0 : EVP_sha384(void)
     388             : {
     389           0 :     hcrypto_validate();
     390           0 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha384);
     391             : }
     392             : 
     393             : /**
     394             :  * The message digest SHA512
     395             :  *
     396             :  * @return the message digest type.
     397             :  *
     398             :  * @ingroup hcrypto_evp
     399             :  */
     400             : 
     401             : const EVP_MD *
     402           0 : EVP_sha512(void)
     403             : {
     404           0 :     hcrypto_validate();
     405           0 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha512);
     406             : }
     407             : 
     408             : /**
     409             :  * The message digest SHA1
     410             :  *
     411             :  * @return the message digest type.
     412             :  *
     413             :  * @ingroup hcrypto_evp
     414             :  */
     415             : 
     416             : const EVP_MD *
     417     4480053 : EVP_sha1(void)
     418             : {
     419     4480053 :     hcrypto_validate();
     420     4480053 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, sha1);
     421             : }
     422             : 
     423             : /**
     424             :  * The message digest SHA1
     425             :  *
     426             :  * @return the message digest type.
     427             :  *
     428             :  * @ingroup hcrypto_evp
     429             :  */
     430             : 
     431             : const EVP_MD *
     432           0 : EVP_sha(void) HC_DEPRECATED
     433             : 
     434             : {
     435           0 :     hcrypto_validate();
     436           0 :     return EVP_sha1();
     437             : }
     438             : 
     439             : /**
     440             :  * The message digest MD5
     441             :  *
     442             :  * @return the message digest type.
     443             :  *
     444             :  * @ingroup hcrypto_evp
     445             :  */
     446             : 
     447             : const EVP_MD *
     448     2075355 : EVP_md5(void) HC_DEPRECATED_CRYPTO
     449             : {
     450     2075355 :     hcrypto_validate();
     451     2075355 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, md5);
     452             : }
     453             : 
     454             : /**
     455             :  * The message digest MD4
     456             :  *
     457             :  * @return the message digest type.
     458             :  *
     459             :  * @ingroup hcrypto_evp
     460             :  */
     461             : 
     462             : const EVP_MD *
     463        1181 : EVP_md4(void) HC_DEPRECATED_CRYPTO
     464             : {
     465        1181 :     hcrypto_validate();
     466        1181 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, md4);
     467             : }
     468             : 
     469             : /**
     470             :  * The message digest MD2
     471             :  *
     472             :  * @return the message digest type.
     473             :  *
     474             :  * @ingroup hcrypto_evp
     475             :  */
     476             : 
     477             : const EVP_MD *
     478           0 : EVP_md2(void) HC_DEPRECATED_CRYPTO
     479             : {
     480           0 :     hcrypto_validate();
     481           0 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, md2);
     482             : }
     483             : 
     484             : /*
     485             :  *
     486             :  */
     487             : 
     488             : static int
     489           0 : null_Init (void *m)
     490             : {
     491           0 :     return 1;
     492             : }
     493             : static int
     494           0 : null_Update (void *m, const void * data, size_t size)
     495             : {
     496           0 :     return 1;
     497             : }
     498             : static int
     499           0 : null_Final(void *res, void *m)
     500             : {
     501           0 :     return 1;
     502             : }
     503             : 
     504             : /**
     505             :  * The null message digest
     506             :  *
     507             :  * @return the message digest type.
     508             :  *
     509             :  * @ingroup hcrypto_evp
     510             :  */
     511             : 
     512             : const EVP_MD *
     513           0 : EVP_md_null(void)
     514             : {
     515             :     static const struct hc_evp_md null = {
     516             :         0,
     517             :         0,
     518             :         0,
     519             :         (hc_evp_md_init)null_Init,
     520             :         (hc_evp_md_update)null_Update,
     521             :         (hc_evp_md_final)null_Final,
     522             :         NULL
     523             :     };
     524           0 :     return &null;
     525             : }
     526             : 
     527             : /**
     528             :  * Return the block size of the cipher.
     529             :  *
     530             :  * @param c cipher to get the block size from.
     531             :  *
     532             :  * @return the block size of the cipher.
     533             :  *
     534             :  * @ingroup hcrypto_evp
     535             :  */
     536             : 
     537             : size_t
     538    17394485 : EVP_CIPHER_block_size(const EVP_CIPHER *c)
     539             : {
     540    17394485 :     return c->block_size;
     541             : }
     542             : 
     543             : /**
     544             :  * Return the key size of the cipher.
     545             :  *
     546             :  * @param c cipher to get the key size from.
     547             :  *
     548             :  * @return the key size of the cipher.
     549             :  *
     550             :  * @ingroup hcrypto_evp
     551             :  */
     552             : 
     553             : size_t
     554           0 : EVP_CIPHER_key_length(const EVP_CIPHER *c)
     555             : {
     556           0 :     return c->key_len;
     557             : }
     558             : 
     559             : /**
     560             :  * Return the IV size of the cipher.
     561             :  *
     562             :  * @param c cipher to get the IV size from.
     563             :  *
     564             :  * @return the IV size of the cipher.
     565             :  *
     566             :  * @ingroup hcrypto_evp
     567             :  */
     568             : 
     569             : size_t
     570    54508090 : EVP_CIPHER_iv_length(const EVP_CIPHER *c)
     571             : {
     572    54508090 :     return c->iv_len;
     573             : }
     574             : 
     575             : /**
     576             :  * Initiate a EVP_CIPHER_CTX context. Clean up with
     577             :  * EVP_CIPHER_CTX_cleanup().
     578             :  *
     579             :  * @param c the cipher initiate.
     580             :  *
     581             :  * @ingroup hcrypto_evp
     582             :  */
     583             : 
     584             : void
     585     9544825 : EVP_CIPHER_CTX_init(EVP_CIPHER_CTX *c)
     586             : {
     587     9544825 :     memset(c, 0, sizeof(*c));
     588     9544825 : }
     589             : 
     590             : /**
     591             :  * Clean up the EVP_CIPHER_CTX context.
     592             :  *
     593             :  * @param c the cipher to clean up.
     594             :  *
     595             :  * @return 1 on success.
     596             :  *
     597             :  * @ingroup hcrypto_evp
     598             :  */
     599             : 
     600             : int
     601    19089650 : EVP_CIPHER_CTX_cleanup(EVP_CIPHER_CTX *c)
     602             : {
     603    19089650 :     if (c->cipher && c->cipher->cleanup) {
     604           0 :         int ret = c->cipher->cleanup(c);
     605           0 :         if (!ret)
     606           0 :             return ret;
     607             :     }
     608    19089650 :     if (c->cipher_data) {
     609     9544825 :         if (c->cipher)
     610     9544825 :             memset(c->cipher_data, 0, c->cipher->ctx_size);
     611     9544825 :         free(c->cipher_data);
     612     9544825 :         c->cipher_data = NULL;
     613             :     }
     614    19089650 :     return 1;
     615             : }
     616             : 
     617             : /**
     618             :  * If the cipher type supports it, change the key length
     619             :  *
     620             :  * @param c the cipher context to change the key length for
     621             :  * @param length new key length
     622             :  *
     623             :  * @return 1 on success.
     624             :  *
     625             :  * @ingroup hcrypto_evp
     626             :  */
     627             : 
     628             : int
     629      109370 : EVP_CIPHER_CTX_set_key_length(EVP_CIPHER_CTX *c, int length)
     630             : {
     631      109370 :     if ((c->cipher->flags & EVP_CIPH_VARIABLE_LENGTH) && length > 0) {
     632       65622 :         c->key_len = length;
     633       65622 :         return 1;
     634             :     }
     635       43748 :     return 0;
     636             : }
     637             : 
     638             : #if 0
     639             : int
     640             : EVP_CIPHER_CTX_set_padding(EVP_CIPHER_CTX *c, int pad)
     641             : {
     642             :     return 0;
     643             : }
     644             : #endif
     645             : 
     646             : /**
     647             :  * Return the EVP_CIPHER for a EVP_CIPHER_CTX context.
     648             :  *
     649             :  * @param ctx the context to get the cipher type from.
     650             :  *
     651             :  * @return the EVP_CIPHER pointer.
     652             :  *
     653             :  * @ingroup hcrypto_evp
     654             :  */
     655             : 
     656             : const EVP_CIPHER *
     657           0 : EVP_CIPHER_CTX_cipher(EVP_CIPHER_CTX *ctx)
     658             : {
     659           0 :     return ctx->cipher;
     660             : }
     661             : 
     662             : /**
     663             :  * Return the block size of the cipher context.
     664             :  *
     665             :  * @param ctx cipher context to get the block size from.
     666             :  *
     667             :  * @return the block size of the cipher context.
     668             :  *
     669             :  * @ingroup hcrypto_evp
     670             :  */
     671             : 
     672             : size_t
     673     7849660 : EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
     674             : {
     675     7849660 :     return EVP_CIPHER_block_size(ctx->cipher);
     676             : }
     677             : 
     678             : /**
     679             :  * Return the key size of the cipher context.
     680             :  *
     681             :  * @param ctx cipher context to get the key size from.
     682             :  *
     683             :  * @return the key size of the cipher context.
     684             :  *
     685             :  * @ingroup hcrypto_evp
     686             :  */
     687             : 
     688             : size_t
     689           0 : EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
     690             : {
     691           0 :     return EVP_CIPHER_key_length(ctx->cipher);
     692             : }
     693             : 
     694             : /**
     695             :  * Return the IV size of the cipher context.
     696             :  *
     697             :  * @param ctx cipher context to get the IV size from.
     698             :  *
     699             :  * @return the IV size of the cipher context.
     700             :  *
     701             :  * @ingroup hcrypto_evp
     702             :  */
     703             : 
     704             : size_t
     705    54508090 : EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
     706             : {
     707    54508090 :     return EVP_CIPHER_iv_length(ctx->cipher);
     708             : }
     709             : 
     710             : /**
     711             :  * Get the flags for an EVP_CIPHER_CTX context.
     712             :  *
     713             :  * @param ctx the EVP_CIPHER_CTX to get the flags from
     714             :  *
     715             :  * @return the flags for an EVP_CIPHER_CTX.
     716             :  *
     717             :  * @ingroup hcrypto_evp
     718             :  */
     719             : 
     720             : unsigned long
     721    41530972 : EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx)
     722             : {
     723    41530972 :     return ctx->cipher->flags;
     724             : }
     725             : 
     726             : /**
     727             :  * Get the mode for an EVP_CIPHER_CTX context.
     728             :  *
     729             :  * @param ctx the EVP_CIPHER_CTX to get the mode from
     730             :  *
     731             :  * @return the mode for an EVP_CIPHER_CTX.
     732             :  *
     733             :  * @ingroup hcrypto_evp
     734             :  */
     735             : 
     736             : int
     737    41530972 : EVP_CIPHER_CTX_mode(const EVP_CIPHER_CTX *ctx)
     738             : {
     739    41530972 :     return EVP_CIPHER_CTX_flags(ctx) & EVP_CIPH_MODE;
     740             : }
     741             : 
     742             : /**
     743             :  * Get the app data for an EVP_CIPHER_CTX context.
     744             :  *
     745             :  * @param ctx the EVP_CIPHER_CTX to get the app data from
     746             :  *
     747             :  * @return the app data for an EVP_CIPHER_CTX.
     748             :  *
     749             :  * @ingroup hcrypto_evp
     750             :  */
     751             : 
     752             : void *
     753           0 : EVP_CIPHER_CTX_get_app_data(EVP_CIPHER_CTX *ctx)
     754             : {
     755           0 :     return ctx->app_data;
     756             : }
     757             : 
     758             : /**
     759             :  * Set the app data for an EVP_CIPHER_CTX context.
     760             :  *
     761             :  * @param ctx the EVP_CIPHER_CTX to set the app data for
     762             :  * @param data the app data to set for an EVP_CIPHER_CTX.
     763             :  *
     764             :  * @ingroup hcrypto_evp
     765             :  */
     766             : 
     767             : void
     768           0 : EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
     769             : {
     770           0 :     ctx->app_data = data;
     771           0 : }
     772             : 
     773             : /**
     774             :  * Initiate the EVP_CIPHER_CTX context to encrypt or decrypt data.
     775             :  * Clean up with EVP_CIPHER_CTX_cleanup().
     776             :  *
     777             :  * @param ctx context to initiate
     778             :  * @param c cipher to use.
     779             :  * @param engine crypto engine to use, NULL to select default.
     780             :  * @param key the crypto key to use, NULL will use the previous value.
     781             :  * @param iv the IV to use, NULL will use the previous value.
     782             :  * @param encp non zero will encrypt, -1 use the previous value.
     783             :  *
     784             :  * @return 1 on success.
     785             :  *
     786             :  * @ingroup hcrypto_evp
     787             :  */
     788             : 
     789             : int
     790    21841741 : EVP_CipherInit_ex(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *c, ENGINE *engine,
     791             :                   const void *key, const void *iv, int encp)
     792             : {
     793    21841741 :     ctx->buf_len = 0;
     794             : 
     795    21841741 :     if (encp == -1)
     796    12187546 :         encp = ctx->encrypt;
     797             :     else
     798     9654195 :         ctx->encrypt = (encp ? 1 : 0);
     799             : 
     800    21841741 :     if (c && (c != ctx->cipher)) {
     801     9544825 :         EVP_CIPHER_CTX_cleanup(ctx);
     802     9544825 :         ctx->cipher = c;
     803     9544825 :         ctx->key_len = c->key_len;
     804             : 
     805     9544825 :         ctx->cipher_data = calloc(1, c->ctx_size);
     806     9544825 :         if (ctx->cipher_data == NULL && c->ctx_size != 0)
     807           0 :             return 0;
     808             : 
     809             :         /* assume block size is a multiple of 2 */
     810     9544825 :         ctx->block_mask = EVP_CIPHER_block_size(c) - 1;
     811             : 
     812    19089650 :         if ((ctx->cipher->flags & EVP_CIPH_CTRL_INIT) &&
     813           0 :             !EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_INIT, 0, NULL))
     814           0 :             return 0;
     815             : 
     816    12296916 :     } else if (ctx->cipher == NULL) {
     817             :         /* reuse of cipher, but not any cipher ever set! */
     818           0 :         return 0;
     819             :     }
     820             : 
     821    21841741 :     switch (EVP_CIPHER_CTX_mode(ctx)) {
     822    21138398 :     case EVP_CIPH_CBC_MODE:
     823             : 
     824    21138398 :         assert(EVP_CIPHER_CTX_iv_length(ctx) <= sizeof(ctx->iv));
     825             : 
     826    21138398 :         if (iv)
     827    12231294 :             memcpy(ctx->oiv, iv, EVP_CIPHER_CTX_iv_length(ctx));
     828    21138398 :         memcpy(ctx->iv, ctx->oiv, EVP_CIPHER_CTX_iv_length(ctx));
     829    21138398 :         break;
     830             : 
     831      703343 :     case EVP_CIPH_STREAM_CIPHER:
     832      703343 :         break;
     833           0 :     case EVP_CIPH_CFB8_MODE:
     834           0 :         if (iv)
     835           0 :             memcpy(ctx->iv, iv, EVP_CIPHER_CTX_iv_length(ctx));
     836           0 :         break;
     837             : 
     838           0 :     default:
     839           0 :         return 0;
     840             :     }
     841             : 
     842    21841741 :     if (key || (ctx->cipher->flags & EVP_CIPH_ALWAYS_CALL_INIT))
     843     9544825 :         return ctx->cipher->init(ctx, key, iv, encp);
     844             : 
     845    12296916 :     return 1;
     846             : }
     847             : 
     848             : /**
     849             :  * Encipher/decipher partial data
     850             :  *
     851             :  * @param ctx the cipher context.
     852             :  * @param out output data from the operation.
     853             :  * @param outlen output length
     854             :  * @param in input data to the operation.
     855             :  * @param inlen length of data.
     856             :  *
     857             :  * The output buffer length should at least be EVP_CIPHER_block_size()
     858             :  * byte longer then the input length.
     859             :  *
     860             :  * See @ref evp_cipher for an example how to use this function.
     861             :  *
     862             :  * @return 1 on success.
     863             :  *
     864             :  * @ingroup hcrypto_evp
     865             :  */
     866             : 
     867             : int
     868           0 : EVP_CipherUpdate(EVP_CIPHER_CTX *ctx, void *out, int *outlen,
     869             :                  void *in, size_t inlen)
     870             : {
     871             :     int ret, left, blocksize;
     872             : 
     873           0 :     *outlen = 0;
     874             : 
     875             :     /**
     876             :      * If there in no spare bytes in the left from last Update and the
     877             :      * input length is on the block boundery, the EVP_CipherUpdate()
     878             :      * function can take a shortcut (and preformance gain) and
     879             :      * directly encrypt the data, otherwise we hav to fix it up and
     880             :      * store extra it the EVP_CIPHER_CTX.
     881             :      */
     882           0 :     if (ctx->buf_len == 0 && (inlen & ctx->block_mask) == 0) {
     883           0 :         ret = (*ctx->cipher->do_cipher)(ctx, out, in, inlen);
     884           0 :         if (ret == 1)
     885           0 :             *outlen = inlen;
     886             :         else
     887           0 :             *outlen = 0;
     888           0 :         return ret;
     889             :     }
     890             : 
     891             : 
     892           0 :     blocksize = EVP_CIPHER_CTX_block_size(ctx);
     893           0 :     left = blocksize - ctx->buf_len;
     894           0 :     assert(left > 0);
     895             : 
     896           0 :     if (ctx->buf_len) {
     897             : 
     898             :         /* if total buffer is smaller then input, store locally */
     899           0 :         if (inlen < left) {
     900           0 :             memcpy(ctx->buf + ctx->buf_len, in, inlen);
     901           0 :             ctx->buf_len += inlen;
     902           0 :             return 1;
     903             :         }
     904             : 
     905             :         /* fill in local buffer and encrypt */
     906           0 :         memcpy(ctx->buf + ctx->buf_len, in, left);
     907           0 :         ret = (*ctx->cipher->do_cipher)(ctx, out, ctx->buf, blocksize);
     908           0 :         memset(ctx->buf, 0, blocksize);
     909           0 :         if (ret != 1)
     910           0 :             return ret;
     911             : 
     912           0 :         *outlen += blocksize;
     913           0 :         inlen -= left;
     914           0 :         in = ((unsigned char *)in) + left;
     915           0 :         out = ((unsigned char *)out) + blocksize;
     916           0 :         ctx->buf_len = 0;
     917             :     }
     918             : 
     919           0 :     if (inlen) {
     920           0 :         ctx->buf_len = (inlen & ctx->block_mask);
     921           0 :         inlen &= ~ctx->block_mask;
     922             : 
     923           0 :         ret = (*ctx->cipher->do_cipher)(ctx, out, in, inlen);
     924           0 :         if (ret != 1)
     925           0 :             return ret;
     926             : 
     927           0 :         *outlen += inlen;
     928             : 
     929           0 :         in = ((unsigned char *)in) + inlen;
     930           0 :         memcpy(ctx->buf, in, ctx->buf_len);
     931             :     }
     932             : 
     933           0 :     return 1;
     934             : }
     935             : 
     936             : /**
     937             :  * Encipher/decipher final data
     938             :  *
     939             :  * @param ctx the cipher context.
     940             :  * @param out output data from the operation.
     941             :  * @param outlen output length
     942             :  *
     943             :  * The input length needs to be at least EVP_CIPHER_block_size() bytes
     944             :  * long.
     945             :  *
     946             :  * See @ref evp_cipher for an example how to use this function.
     947             :  *
     948             :  * @return 1 on success.
     949             :  *
     950             :  * @ingroup hcrypto_evp
     951             :  */
     952             : 
     953             : int
     954           0 : EVP_CipherFinal_ex(EVP_CIPHER_CTX *ctx, void *out, int *outlen)
     955             : {
     956           0 :     *outlen = 0;
     957             : 
     958           0 :     if (ctx->buf_len) {
     959             :         int ret, left, blocksize;
     960             : 
     961           0 :         blocksize = EVP_CIPHER_CTX_block_size(ctx);
     962             : 
     963           0 :         left = blocksize - ctx->buf_len;
     964           0 :         assert(left > 0);
     965             : 
     966             :         /* zero fill local buffer */
     967           0 :         memset(ctx->buf + ctx->buf_len, 0, left);
     968           0 :         ret = (*ctx->cipher->do_cipher)(ctx, out, ctx->buf, blocksize);
     969           0 :         memset(ctx->buf, 0, blocksize);
     970           0 :         if (ret != 1)
     971           0 :             return ret;
     972             : 
     973           0 :         *outlen += blocksize;
     974             :     }
     975             : 
     976           0 :     return 1;
     977             : }
     978             : 
     979             : /**
     980             :  * Encipher/decipher data
     981             :  *
     982             :  * @param ctx the cipher context.
     983             :  * @param out out data from the operation.
     984             :  * @param in in data to the operation.
     985             :  * @param size length of data.
     986             :  *
     987             :  * @return 1 on success.
     988             :  */
     989             : 
     990             : int
     991    15946641 : EVP_Cipher(EVP_CIPHER_CTX *ctx, void *out, const void *in,size_t size)
     992             : {
     993    15946641 :     return ctx->cipher->do_cipher(ctx, out, in, size);
     994             : }
     995             : 
     996             : /*
     997             :  *
     998             :  */
     999             : 
    1000             : static int
    1001           0 : enc_null_init(EVP_CIPHER_CTX *ctx,
    1002             :                   const unsigned char * key,
    1003             :                   const unsigned char * iv,
    1004             :                   int encp)
    1005             : {
    1006           0 :     return 1;
    1007             : }
    1008             : 
    1009             : static int
    1010           0 : enc_null_do_cipher(EVP_CIPHER_CTX *ctx,
    1011             :               unsigned char *out,
    1012             :               const unsigned char *in,
    1013             :               unsigned int size)
    1014             : {
    1015           0 :     memmove(out, in, size);
    1016           0 :     return 1;
    1017             : }
    1018             : 
    1019             : static int
    1020           0 : enc_null_cleanup(EVP_CIPHER_CTX *ctx)
    1021             : {
    1022           0 :     return 1;
    1023             : }
    1024             : 
    1025             : /**
    1026             :  * The NULL cipher type, does no encryption/decryption.
    1027             :  *
    1028             :  * @return the null EVP_CIPHER pointer.
    1029             :  *
    1030             :  * @ingroup hcrypto_evp
    1031             :  */
    1032             : 
    1033             : const EVP_CIPHER *
    1034           0 : EVP_enc_null(void)
    1035             : {
    1036             :     static const EVP_CIPHER enc_null = {
    1037             :         0,
    1038             :         0,
    1039             :         0,
    1040             :         0,
    1041             :         EVP_CIPH_CBC_MODE,
    1042             :         enc_null_init,
    1043             :         enc_null_do_cipher,
    1044             :         enc_null_cleanup,
    1045             :         0,
    1046             :         NULL,
    1047             :         NULL,
    1048             :         NULL,
    1049             :         NULL
    1050             :     };
    1051           0 :     return &enc_null;
    1052             : }
    1053             : 
    1054             : /**
    1055             :  * The RC2 cipher type
    1056             :  *
    1057             :  * @return the RC2 EVP_CIPHER pointer.
    1058             :  *
    1059             :  * @ingroup hcrypto_evp
    1060             :  */
    1061             : 
    1062             : const EVP_CIPHER *
    1063           0 : EVP_rc2_cbc(void)
    1064             : {
    1065           0 :     hcrypto_validate();
    1066           0 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc2_cbc);
    1067             : }
    1068             : 
    1069             : /**
    1070             :  * The RC2 cipher type
    1071             :  *
    1072             :  * @return the RC2 EVP_CIPHER pointer.
    1073             :  *
    1074             :  * @ingroup hcrypto_evp
    1075             :  */
    1076             : 
    1077             : const EVP_CIPHER *
    1078           0 : EVP_rc2_40_cbc(void)
    1079             : {
    1080           0 :     hcrypto_validate();
    1081           0 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc2_40_cbc);
    1082             : }
    1083             : 
    1084             : /**
    1085             :  * The RC2 cipher type
    1086             :  *
    1087             :  * @return the RC2 EVP_CIPHER pointer.
    1088             :  *
    1089             :  * @ingroup hcrypto_evp
    1090             :  */
    1091             : 
    1092             : const EVP_CIPHER *
    1093           0 : EVP_rc2_64_cbc(void)
    1094             : {
    1095           0 :     hcrypto_validate();
    1096           0 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc2_64_cbc);
    1097             : }
    1098             : 
    1099             : /**
    1100             :  * The RC4 cipher type
    1101             :  *
    1102             :  * @return the RC4 EVP_CIPHER pointer.
    1103             :  *
    1104             :  * @ingroup hcrypto_evp
    1105             :  */
    1106             : 
    1107             : const EVP_CIPHER *
    1108      463208 : EVP_rc4(void)
    1109             : {
    1110      463208 :     hcrypto_validate();
    1111      463208 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc4);
    1112             : }
    1113             : 
    1114             : /**
    1115             :  * The RC4-40 cipher type
    1116             :  *
    1117             :  * @return the RC4-40 EVP_CIPHER pointer.
    1118             :  *
    1119             :  * @ingroup hcrypto_evp
    1120             :  */
    1121             : 
    1122             : const EVP_CIPHER *
    1123           0 : EVP_rc4_40(void)
    1124             : {
    1125           0 :     hcrypto_validate();
    1126           0 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, rc4_40);
    1127             : }
    1128             : 
    1129             : /**
    1130             :  * The DES cipher type
    1131             :  *
    1132             :  * @return the DES-CBC EVP_CIPHER pointer.
    1133             :  *
    1134             :  * @ingroup hcrypto_evp
    1135             :  */
    1136             : 
    1137             : const EVP_CIPHER *
    1138           0 : EVP_des_cbc(void)
    1139             : {
    1140           0 :     hcrypto_validate();
    1141           0 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, des_cbc);
    1142             : }
    1143             : 
    1144             : /**
    1145             :  * The triple DES cipher type
    1146             :  *
    1147             :  * @return the DES-EDE3-CBC EVP_CIPHER pointer.
    1148             :  *
    1149             :  * @ingroup hcrypto_evp
    1150             :  */
    1151             : 
    1152             : const EVP_CIPHER *
    1153       10937 : EVP_des_ede3_cbc(void)
    1154             : {
    1155       10937 :     hcrypto_validate();
    1156       10937 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, des_ede3_cbc);
    1157             : }
    1158             : 
    1159             : /**
    1160             :  * The AES-128 cipher type
    1161             :  *
    1162             :  * @return the AES-128 EVP_CIPHER pointer.
    1163             :  *
    1164             :  * @ingroup hcrypto_evp
    1165             :  */
    1166             : 
    1167             : const EVP_CIPHER *
    1168       23295 : EVP_aes_128_cbc(void)
    1169             : {
    1170       23295 :     hcrypto_validate();
    1171       23295 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_128_cbc);
    1172             : }
    1173             : 
    1174             : /**
    1175             :  * The AES-192 cipher type
    1176             :  *
    1177             :  * @return the AES-192 EVP_CIPHER pointer.
    1178             :  *
    1179             :  * @ingroup hcrypto_evp
    1180             :  */
    1181             : 
    1182             : const EVP_CIPHER *
    1183           0 : EVP_aes_192_cbc(void)
    1184             : {
    1185           0 :     hcrypto_validate();
    1186           0 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_192_cbc);
    1187             : }
    1188             : 
    1189             : /**
    1190             :  * The AES-256 cipher type
    1191             :  *
    1192             :  * @return the AES-256 EVP_CIPHER pointer.
    1193             :  *
    1194             :  * @ingroup hcrypto_evp
    1195             :  */
    1196             : 
    1197             : const EVP_CIPHER *
    1198     4674909 : EVP_aes_256_cbc(void)
    1199             : {
    1200     4674909 :     hcrypto_validate();
    1201     4674909 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_256_cbc);
    1202             : }
    1203             : 
    1204             : /**
    1205             :  * The AES-128 cipher type
    1206             :  *
    1207             :  * @return the AES-128 EVP_CIPHER pointer.
    1208             :  *
    1209             :  * @ingroup hcrypto_evp
    1210             :  */
    1211             : 
    1212             : const EVP_CIPHER *
    1213           0 : EVP_aes_128_cfb8(void)
    1214             : {
    1215           0 :     hcrypto_validate();
    1216           0 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_128_cfb8);
    1217             : }
    1218             : 
    1219             : /**
    1220             :  * The AES-192 cipher type
    1221             :  *
    1222             :  * @return the AES-192 EVP_CIPHER pointer.
    1223             :  *
    1224             :  * @ingroup hcrypto_evp
    1225             :  */
    1226             : 
    1227             : const EVP_CIPHER *
    1228           0 : EVP_aes_192_cfb8(void)
    1229             : {
    1230           0 :     hcrypto_validate();
    1231           0 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_192_cfb8);
    1232             : }
    1233             : 
    1234             : /**
    1235             :  * The AES-256 cipher type
    1236             :  *
    1237             :  * @return the AES-256 EVP_CIPHER pointer.
    1238             :  *
    1239             :  * @ingroup hcrypto_evp
    1240             :  */
    1241             : 
    1242             : const EVP_CIPHER *
    1243           0 : EVP_aes_256_cfb8(void)
    1244             : {
    1245           0 :     hcrypto_validate();
    1246           0 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, aes_256_cfb8);
    1247             : }
    1248             : 
    1249             : /**
    1250             :  * The Camellia-128 cipher type
    1251             :  *
    1252             :  * @return the Camellia-128 EVP_CIPHER pointer.
    1253             :  *
    1254             :  * @ingroup hcrypto_evp
    1255             :  */
    1256             : 
    1257             : const EVP_CIPHER *
    1258           0 : EVP_camellia_128_cbc(void)
    1259             : {
    1260           0 :     hcrypto_validate();
    1261           0 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, camellia_128_cbc);
    1262             : }
    1263             : 
    1264             : /**
    1265             :  * The Camellia-198 cipher type
    1266             :  *
    1267             :  * @return the Camellia-198 EVP_CIPHER pointer.
    1268             :  *
    1269             :  * @ingroup hcrypto_evp
    1270             :  */
    1271             : 
    1272             : const EVP_CIPHER *
    1273           0 : EVP_camellia_192_cbc(void)
    1274             : {
    1275           0 :     hcrypto_validate();
    1276           0 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, camellia_192_cbc);
    1277             : }
    1278             : 
    1279             : /**
    1280             :  * The Camellia-256 cipher type
    1281             :  *
    1282             :  * @return the Camellia-256 EVP_CIPHER pointer.
    1283             :  *
    1284             :  * @ingroup hcrypto_evp
    1285             :  */
    1286             : 
    1287             : const EVP_CIPHER *
    1288           0 : EVP_camellia_256_cbc(void)
    1289             : {
    1290           0 :     hcrypto_validate();
    1291           0 :     return EVP_DEF_OP(HCRYPTO_DEF_PROVIDER, camellia_256_cbc);
    1292             : }
    1293             : 
    1294             : /*
    1295             :  *
    1296             :  */
    1297             : 
    1298             : static const struct cipher_name {
    1299             :     const char *name;
    1300             :     const EVP_CIPHER *(*func)(void);
    1301             : } cipher_name[] = {
    1302             :     { "des-ede3-cbc", EVP_des_ede3_cbc },
    1303             :     { "aes-128-cbc", EVP_aes_128_cbc },
    1304             :     { "aes-192-cbc", EVP_aes_192_cbc },
    1305             :     { "aes-256-cbc", EVP_aes_256_cbc },
    1306             :     { "aes-128-cfb8", EVP_aes_128_cfb8 },
    1307             :     { "aes-192-cfb8", EVP_aes_192_cfb8 },
    1308             :     { "aes-256-cfb8", EVP_aes_256_cfb8 },
    1309             :     { "camellia-128-cbc", EVP_camellia_128_cbc },
    1310             :     { "camellia-192-cbc", EVP_camellia_192_cbc },
    1311             :     { "camellia-256-cbc", EVP_camellia_256_cbc }
    1312             : };
    1313             : 
    1314             : /**
    1315             :  * Get the cipher type using their name.
    1316             :  *
    1317             :  * @param name the name of the cipher.
    1318             :  *
    1319             :  * @return the selected EVP_CIPHER pointer or NULL if not found.
    1320             :  *
    1321             :  * @ingroup hcrypto_evp
    1322             :  */
    1323             : 
    1324             : const EVP_CIPHER *
    1325           0 : EVP_get_cipherbyname(const char *name)
    1326             : {
    1327             :     int i;
    1328           0 :     for (i = 0; i < sizeof(cipher_name)/sizeof(cipher_name[0]); i++) {
    1329           0 :         if (strcasecmp(cipher_name[i].name, name) == 0)
    1330           0 :             return (*cipher_name[i].func)();
    1331             :     }
    1332           0 :     return NULL;
    1333             : }
    1334             : 
    1335             : 
    1336             : /*
    1337             :  *
    1338             :  */
    1339             : 
    1340             : #ifndef min
    1341             : #define min(a,b) (((a)>(b))?(b):(a))
    1342             : #endif
    1343             : 
    1344             : /**
    1345             :  * Provides a legancy string to key function, used in PEM files.
    1346             :  *
    1347             :  * New protocols should use new string to key functions like NIST
    1348             :  * SP56-800A or PKCS#5 v2.0 (see PKCS5_PBKDF2_HMAC_SHA1()).
    1349             :  *
    1350             :  * @param type type of cipher to use
    1351             :  * @param md message digest to use
    1352             :  * @param salt salt salt string, should be an binary 8 byte buffer.
    1353             :  * @param data the password/input key string.
    1354             :  * @param datalen length of data parameter.
    1355             :  * @param count iteration counter.
    1356             :  * @param keydata output keydata, needs to of the size EVP_CIPHER_key_length().
    1357             :  * @param ivdata output ivdata, needs to of the size EVP_CIPHER_block_size().
    1358             :  *
    1359             :  * @return the size of derived key.
    1360             :  *
    1361             :  * @ingroup hcrypto_evp
    1362             :  */
    1363             : 
    1364             : int
    1365           0 : EVP_BytesToKey(const EVP_CIPHER *type,
    1366             :                const EVP_MD *md,
    1367             :                const void *salt,
    1368             :                const void *data, size_t datalen,
    1369             :                unsigned int count,
    1370             :                void *keydata,
    1371             :                void *ivdata)
    1372             : {
    1373             :     unsigned int ivlen, keylen;
    1374           0 :     int first = 0;
    1375           0 :     unsigned int mds = 0, i;
    1376           0 :     unsigned char *key = keydata;
    1377           0 :     unsigned char *iv = ivdata;
    1378             :     unsigned char *buf;
    1379             :     EVP_MD_CTX c;
    1380             : 
    1381           0 :     keylen = EVP_CIPHER_key_length(type);
    1382           0 :     ivlen = EVP_CIPHER_iv_length(type);
    1383             : 
    1384           0 :     if (data == NULL)
    1385           0 :         return keylen;
    1386             : 
    1387           0 :     buf = malloc(EVP_MD_size(md));
    1388           0 :     if (buf == NULL)
    1389           0 :         return -1;
    1390             : 
    1391           0 :     EVP_MD_CTX_init(&c);
    1392             : 
    1393           0 :     first = 1;
    1394             :     while (1) {
    1395           0 :         EVP_DigestInit_ex(&c, md, NULL);
    1396           0 :         if (!first)
    1397           0 :             EVP_DigestUpdate(&c, buf, mds);
    1398           0 :         first = 0;
    1399           0 :         EVP_DigestUpdate(&c,data,datalen);
    1400             : 
    1401             : #define PKCS5_SALT_LEN 8
    1402             : 
    1403           0 :         if (salt)
    1404           0 :             EVP_DigestUpdate(&c, salt, PKCS5_SALT_LEN);
    1405             : 
    1406           0 :         EVP_DigestFinal_ex(&c, buf, &mds);
    1407           0 :         assert(mds == EVP_MD_size(md));
    1408             : 
    1409           0 :         for (i = 1; i < count; i++) {
    1410           0 :             EVP_DigestInit_ex(&c, md, NULL);
    1411           0 :             EVP_DigestUpdate(&c, buf, mds);
    1412           0 :             EVP_DigestFinal_ex(&c, buf, &mds);
    1413           0 :             assert(mds == EVP_MD_size(md));
    1414             :         }
    1415             : 
    1416           0 :         i = 0;
    1417           0 :         if (keylen) {
    1418           0 :             size_t sz = min(keylen, mds);
    1419           0 :             if (key) {
    1420           0 :                 memcpy(key, buf, sz);
    1421           0 :                 key += sz;
    1422             :             }
    1423           0 :             keylen -= sz;
    1424           0 :             i += sz;
    1425             :         }
    1426           0 :         if (ivlen && mds > i) {
    1427           0 :             size_t sz = min(ivlen, (mds - i));
    1428           0 :             if (iv) {
    1429           0 :                 memcpy(iv, &buf[i], sz);
    1430           0 :                 iv += sz;
    1431             :             }
    1432           0 :             ivlen -= sz;
    1433             :         }
    1434           0 :         if (keylen == 0 && ivlen == 0)
    1435           0 :             break;
    1436             :     }
    1437             : 
    1438           0 :     EVP_MD_CTX_cleanup(&c);
    1439           0 :     free(buf);
    1440             : 
    1441           0 :     return EVP_CIPHER_key_length(type);
    1442             : }
    1443             : 
    1444             : /**
    1445             :  * Generate a random key for the specificed EVP_CIPHER.
    1446             :  *
    1447             :  * @param ctx EVP_CIPHER_CTX type to build the key for.
    1448             :  * @param key return key, must be at least EVP_CIPHER_key_length() byte long.
    1449             :  *
    1450             :  * @return 1 for success, 0 for failure.
    1451             :  *
    1452             :  * @ingroup hcrypto_core
    1453             :  */
    1454             : 
    1455             : int
    1456           0 : EVP_CIPHER_CTX_rand_key(EVP_CIPHER_CTX *ctx, void *key)
    1457             : {
    1458           0 :     if (ctx->cipher->flags & EVP_CIPH_RAND_KEY)
    1459           0 :         return EVP_CIPHER_CTX_ctrl(ctx, EVP_CTRL_RAND_KEY, 0, key);
    1460           0 :     if (RAND_bytes(key, ctx->key_len) != 1)
    1461           0 :         return 0;
    1462           0 :     return 1;
    1463             : }
    1464             : 
    1465             : /**
    1466             :  * Perform a operation on a ctx
    1467             :  *
    1468             :  * @param ctx context to perform operation on.
    1469             :  * @param type type of operation.
    1470             :  * @param arg argument to operation.
    1471             :  * @param data addition data to operation.
    1472             : 
    1473             :  * @return 1 for success, 0 for failure.
    1474             :  *
    1475             :  * @ingroup hcrypto_core
    1476             :  */
    1477             : 
    1478             : int
    1479           0 : EVP_CIPHER_CTX_ctrl(EVP_CIPHER_CTX *ctx, int type, int arg, void *data)
    1480             : {
    1481           0 :     if (ctx->cipher == NULL || ctx->cipher->ctrl == NULL)
    1482           0 :         return 0;
    1483           0 :     return (*ctx->cipher->ctrl)(ctx, type, arg, data);
    1484             : }
    1485             : 
    1486             : /**
    1487             :  * Add all algorithms to the crypto core.
    1488             :  *
    1489             :  * @ingroup hcrypto_core
    1490             :  */
    1491             : 
    1492             : void
    1493       16879 : OpenSSL_add_all_algorithms(void)
    1494             : {
    1495       16879 :     return;
    1496             : }
    1497             : 
    1498             : /**
    1499             :  * Add all algorithms to the crypto core using configuration file.
    1500             :  *
    1501             :  * @ingroup hcrypto_core
    1502             :  */
    1503             : 
    1504             : void
    1505           0 : OpenSSL_add_all_algorithms_conf(void)
    1506             : {
    1507           0 :     return;
    1508             : }
    1509             : 
    1510             : /**
    1511             :  * Add all algorithms to the crypto core, but don't use the
    1512             :  * configuration file.
    1513             :  *
    1514             :  * @ingroup hcrypto_core
    1515             :  */
    1516             : 
    1517             : void
    1518           0 : OpenSSL_add_all_algorithms_noconf(void)
    1519             : {
    1520           0 :     return;
    1521             : }

Generated by: LCOV version 1.13