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