LCOV - code coverage report
Current view: top level - third_party/heimdal/lib/hcrypto - evp.c (source / functions) Hit Total Coverage
Test: coverage report for support-claim-type-attributes 6b5c566e Lines: 136 362 37.6 %
Date: 2023-11-21 12:31:41 Functions: 31 69 44.9 %

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

Generated by: LCOV version 1.14