155714Skris/* crypto/evp/evp_lib.c */ 255714Skris/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 355714Skris * All rights reserved. 455714Skris * 555714Skris * This package is an SSL implementation written 655714Skris * by Eric Young (eay@cryptsoft.com). 755714Skris * The implementation was written so as to conform with Netscapes SSL. 8296341Sdelphij * 955714Skris * This library is free for commercial and non-commercial use as long as 1055714Skris * the following conditions are aheared to. The following conditions 1155714Skris * apply to all code found in this distribution, be it the RC4, RSA, 1255714Skris * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1355714Skris * included with this distribution is covered by the same copyright terms 1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com). 15296341Sdelphij * 1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in 1755714Skris * the code are not to be removed. 1855714Skris * If this package is used in a product, Eric Young should be given attribution 1955714Skris * as the author of the parts of the library used. 2055714Skris * This can be in the form of a textual message at program startup or 2155714Skris * in documentation (online or textual) provided with the package. 22296341Sdelphij * 2355714Skris * Redistribution and use in source and binary forms, with or without 2455714Skris * modification, are permitted provided that the following conditions 2555714Skris * are met: 2655714Skris * 1. Redistributions of source code must retain the copyright 2755714Skris * notice, this list of conditions and the following disclaimer. 2855714Skris * 2. Redistributions in binary form must reproduce the above copyright 2955714Skris * notice, this list of conditions and the following disclaimer in the 3055714Skris * documentation and/or other materials provided with the distribution. 3155714Skris * 3. All advertising materials mentioning features or use of this software 3255714Skris * must display the following acknowledgement: 3355714Skris * "This product includes cryptographic software written by 3455714Skris * Eric Young (eay@cryptsoft.com)" 3555714Skris * The word 'cryptographic' can be left out if the rouines from the library 3655714Skris * being used are not cryptographic related :-). 37296341Sdelphij * 4. If you include any Windows specific code (or a derivative thereof) from 3855714Skris * the apps directory (application code) you must include an acknowledgement: 3955714Skris * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 40296341Sdelphij * 4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4455714Skris * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5155714Skris * SUCH DAMAGE. 52296341Sdelphij * 5355714Skris * The licence and distribution terms for any publically available version or 5455714Skris * derivative of this code cannot be changed. i.e. this code cannot simply be 5555714Skris * copied and put under another distribution licence 5655714Skris * [including the GNU Public Licence.] 5755714Skris */ 5855714Skris 5955714Skris#include <stdio.h> 6055714Skris#include "cryptlib.h" 6155714Skris#include <openssl/evp.h> 6255714Skris#include <openssl/objects.h> 6355714Skris 6455714Skrisint EVP_CIPHER_param_to_asn1(EVP_CIPHER_CTX *c, ASN1_TYPE *type) 65296341Sdelphij{ 66296341Sdelphij int ret; 6755714Skris 68296341Sdelphij if (c->cipher->set_asn1_parameters != NULL) 69296341Sdelphij ret = c->cipher->set_asn1_parameters(c, type); 70296341Sdelphij else if (c->cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1) 71296341Sdelphij ret = EVP_CIPHER_set_asn1_iv(c, type); 72296341Sdelphij else 73296341Sdelphij ret = -1; 74296341Sdelphij return (ret); 75296341Sdelphij} 7655714Skris 7755714Skrisint EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type) 78296341Sdelphij{ 79296341Sdelphij int ret; 8055714Skris 81296341Sdelphij if (c->cipher->get_asn1_parameters != NULL) 82296341Sdelphij ret = c->cipher->get_asn1_parameters(c, type); 83296341Sdelphij else if (c->cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1) 84296341Sdelphij ret = EVP_CIPHER_get_asn1_iv(c, type); 85296341Sdelphij else 86296341Sdelphij ret = -1; 87296341Sdelphij return (ret); 88296341Sdelphij} 8955714Skris 9055714Skrisint EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) 91296341Sdelphij{ 92296341Sdelphij int i = 0; 93296341Sdelphij unsigned int l; 9455714Skris 95296341Sdelphij if (type != NULL) { 96296341Sdelphij l = EVP_CIPHER_CTX_iv_length(c); 97296341Sdelphij OPENSSL_assert(l <= sizeof(c->iv)); 98296341Sdelphij i = ASN1_TYPE_get_octetstring(type, c->oiv, l); 99296341Sdelphij if (i != (int)l) 100296341Sdelphij return (-1); 101296341Sdelphij else if (i > 0) 102296341Sdelphij memcpy(c->iv, c->oiv, l); 103296341Sdelphij } 104296341Sdelphij return (i); 105296341Sdelphij} 10655714Skris 10755714Skrisint EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) 108296341Sdelphij{ 109296341Sdelphij int i = 0; 110296341Sdelphij unsigned int j; 11155714Skris 112296341Sdelphij if (type != NULL) { 113296341Sdelphij j = EVP_CIPHER_CTX_iv_length(c); 114296341Sdelphij OPENSSL_assert(j <= sizeof(c->iv)); 115296341Sdelphij i = ASN1_TYPE_set_octetstring(type, c->oiv, j); 116296341Sdelphij } 117296341Sdelphij return (i); 118296341Sdelphij} 11955714Skris 12055714Skris/* Convert the various cipher NIDs and dummies to a proper OID NID */ 12155714Skrisint EVP_CIPHER_type(const EVP_CIPHER *ctx) 12255714Skris{ 123296341Sdelphij int nid; 124296341Sdelphij ASN1_OBJECT *otmp; 125296341Sdelphij nid = EVP_CIPHER_nid(ctx); 12655714Skris 127296341Sdelphij switch (nid) { 12855714Skris 129296341Sdelphij case NID_rc2_cbc: 130296341Sdelphij case NID_rc2_64_cbc: 131296341Sdelphij case NID_rc2_40_cbc: 13255714Skris 133296341Sdelphij return NID_rc2_cbc; 13455714Skris 135296341Sdelphij case NID_rc4: 136296341Sdelphij case NID_rc4_40: 13755714Skris 138296341Sdelphij return NID_rc4; 13955714Skris 140296341Sdelphij case NID_aes_128_cfb128: 141296341Sdelphij case NID_aes_128_cfb8: 142296341Sdelphij case NID_aes_128_cfb1: 143142425Snectar 144296341Sdelphij return NID_aes_128_cfb128; 145142425Snectar 146296341Sdelphij case NID_aes_192_cfb128: 147296341Sdelphij case NID_aes_192_cfb8: 148296341Sdelphij case NID_aes_192_cfb1: 149142425Snectar 150296341Sdelphij return NID_aes_192_cfb128; 151142425Snectar 152296341Sdelphij case NID_aes_256_cfb128: 153296341Sdelphij case NID_aes_256_cfb8: 154296341Sdelphij case NID_aes_256_cfb1: 155142425Snectar 156296341Sdelphij return NID_aes_256_cfb128; 157142425Snectar 158296341Sdelphij case NID_des_cfb64: 159296341Sdelphij case NID_des_cfb8: 160296341Sdelphij case NID_des_cfb1: 161142425Snectar 162296341Sdelphij return NID_des_cfb64; 163142425Snectar 164296341Sdelphij case NID_des_ede3_cfb64: 165296341Sdelphij case NID_des_ede3_cfb8: 166296341Sdelphij case NID_des_ede3_cfb1: 167205128Ssimon 168296341Sdelphij return NID_des_cfb64; 169205128Ssimon 170296341Sdelphij default: 171296341Sdelphij /* Check it has an OID and it is valid */ 172296341Sdelphij otmp = OBJ_nid2obj(nid); 173296341Sdelphij if (!otmp || !otmp->data) 174296341Sdelphij nid = NID_undef; 175296341Sdelphij ASN1_OBJECT_free(otmp); 176296341Sdelphij return nid; 177296341Sdelphij } 17855714Skris} 17955714Skris 180167612Ssimonint EVP_CIPHER_block_size(const EVP_CIPHER *e) 181296341Sdelphij{ 182296341Sdelphij return e->block_size; 183296341Sdelphij} 184167612Ssimon 185167612Ssimonint EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx) 186296341Sdelphij{ 187296341Sdelphij return ctx->cipher->block_size; 188296341Sdelphij} 189167612Ssimon 190296341Sdelphijint EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 191296341Sdelphij const unsigned char *in, unsigned int inl) 192296341Sdelphij{ 193296341Sdelphij return ctx->cipher->do_cipher(ctx, out, in, inl); 194296341Sdelphij} 195238405Sjkim 196167612Ssimonconst EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx) 197296341Sdelphij{ 198296341Sdelphij return ctx->cipher; 199296341Sdelphij} 200167612Ssimon 201167612Ssimonunsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher) 202296341Sdelphij{ 203296341Sdelphij return cipher->flags; 204296341Sdelphij} 205167612Ssimon 206238405Sjkimunsigned long EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx) 207296341Sdelphij{ 208296341Sdelphij return ctx->cipher->flags; 209296341Sdelphij} 210238405Sjkim 211167612Ssimonvoid *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx) 212296341Sdelphij{ 213296341Sdelphij return ctx->app_data; 214296341Sdelphij} 215167612Ssimon 216167612Ssimonvoid EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data) 217296341Sdelphij{ 218296341Sdelphij ctx->app_data = data; 219296341Sdelphij} 220167612Ssimon 221167612Ssimonint EVP_CIPHER_iv_length(const EVP_CIPHER *cipher) 222296341Sdelphij{ 223296341Sdelphij return cipher->iv_len; 224296341Sdelphij} 225167612Ssimon 226238405Sjkimint EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx) 227296341Sdelphij{ 228296341Sdelphij return ctx->cipher->iv_len; 229296341Sdelphij} 230238405Sjkim 231167612Ssimonint EVP_CIPHER_key_length(const EVP_CIPHER *cipher) 232296341Sdelphij{ 233296341Sdelphij return cipher->key_len; 234296341Sdelphij} 235167612Ssimon 236167612Ssimonint EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx) 237296341Sdelphij{ 238296341Sdelphij return ctx->key_len; 239296341Sdelphij} 240167612Ssimon 241238405Sjkimint EVP_CIPHER_nid(const EVP_CIPHER *cipher) 242296341Sdelphij{ 243296341Sdelphij return cipher->nid; 244296341Sdelphij} 245238405Sjkim 246167612Ssimonint EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx) 247296341Sdelphij{ 248296341Sdelphij return ctx->cipher->nid; 249296341Sdelphij} 250167612Ssimon 251296341Sdelphijint EVP_MD_block_size(const EVP_MD *md) 252296341Sdelphij{ 253296341Sdelphij return md->block_size; 254296341Sdelphij} 255167612Ssimon 256167612Ssimonint EVP_MD_type(const EVP_MD *md) 257296341Sdelphij{ 258296341Sdelphij return md->type; 259296341Sdelphij} 260167612Ssimon 261167612Ssimonint EVP_MD_pkey_type(const EVP_MD *md) 262296341Sdelphij{ 263296341Sdelphij return md->pkey_type; 264296341Sdelphij} 265167612Ssimon 266167612Ssimonint EVP_MD_size(const EVP_MD *md) 267296341Sdelphij{ 268296341Sdelphij if (!md) { 269296341Sdelphij EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL); 270296341Sdelphij return -1; 271296341Sdelphij } 272296341Sdelphij return md->md_size; 273296341Sdelphij} 274167612Ssimon 275238405Sjkimunsigned long EVP_MD_flags(const EVP_MD *md) 276296341Sdelphij{ 277296341Sdelphij return md->flags; 278296341Sdelphij} 279238405Sjkim 280238405Sjkimconst EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx) 281296341Sdelphij{ 282296341Sdelphij if (!ctx) 283296341Sdelphij return NULL; 284296341Sdelphij return ctx->digest; 285296341Sdelphij} 286167612Ssimon 287167612Ssimonvoid EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags) 288296341Sdelphij{ 289296341Sdelphij ctx->flags |= flags; 290296341Sdelphij} 291167612Ssimon 292167612Ssimonvoid EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags) 293296341Sdelphij{ 294296341Sdelphij ctx->flags &= ~flags; 295296341Sdelphij} 296167612Ssimon 297167612Ssimonint EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags) 298296341Sdelphij{ 299296341Sdelphij return (ctx->flags & flags); 300296341Sdelphij} 301194206Ssimon 302194206Ssimonvoid EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags) 303296341Sdelphij{ 304296341Sdelphij ctx->flags |= flags; 305296341Sdelphij} 306194206Ssimon 307194206Ssimonvoid EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags) 308296341Sdelphij{ 309296341Sdelphij ctx->flags &= ~flags; 310296341Sdelphij} 311194206Ssimon 312194206Ssimonint EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags) 313296341Sdelphij{ 314296341Sdelphij return (ctx->flags & flags); 315296341Sdelphij} 316