LCOV - code coverage report
Current view: top level - source4/heimdal/lib/hcrypto - evp.c (source / functions) Hit Total Coverage
Test: coverage report for abartlet/fix-coverage dd10fb34 Lines: 129 342 37.7 %
Date: 2021-09-23 10:06:22 Functions: 30 70 42.9 %

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

Generated by: LCOV version 1.13