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. 8280304Sjkim * 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). 15280304Sjkim * 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. 22280304Sjkim * 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 :-). 37280304Sjkim * 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)" 40280304Sjkim * 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. 52280304Sjkim * 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) 65280304Sjkim{ 66280304Sjkim int ret; 6755714Skris 68280304Sjkim if (c->cipher->set_asn1_parameters != NULL) 69280304Sjkim ret = c->cipher->set_asn1_parameters(c, type); 70291721Sjkim else if (c->cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1) { 71291721Sjkim switch (EVP_CIPHER_CTX_mode(c)) { 72291721Sjkim 73291721Sjkim case EVP_CIPH_GCM_MODE: 74291721Sjkim case EVP_CIPH_CCM_MODE: 75291721Sjkim case EVP_CIPH_XTS_MODE: 76291721Sjkim ret = -1; 77291721Sjkim break; 78291721Sjkim 79291721Sjkim default: 80291721Sjkim ret = EVP_CIPHER_set_asn1_iv(c, type); 81291721Sjkim } 82291721Sjkim } else 83280304Sjkim ret = -1; 84280304Sjkim return (ret); 85280304Sjkim} 8655714Skris 8755714Skrisint EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type) 88280304Sjkim{ 89280304Sjkim int ret; 9055714Skris 91280304Sjkim if (c->cipher->get_asn1_parameters != NULL) 92280304Sjkim ret = c->cipher->get_asn1_parameters(c, type); 93291721Sjkim else if (c->cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1) { 94291721Sjkim switch (EVP_CIPHER_CTX_mode(c)) { 95291721Sjkim 96291721Sjkim case EVP_CIPH_GCM_MODE: 97291721Sjkim case EVP_CIPH_CCM_MODE: 98291721Sjkim case EVP_CIPH_XTS_MODE: 99291721Sjkim ret = -1; 100291721Sjkim break; 101291721Sjkim 102291721Sjkim default: 103291721Sjkim ret = EVP_CIPHER_get_asn1_iv(c, type); 104291721Sjkim break; 105291721Sjkim } 106291721Sjkim } else 107280304Sjkim ret = -1; 108280304Sjkim return (ret); 109280304Sjkim} 11055714Skris 11155714Skrisint EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) 112280304Sjkim{ 113280304Sjkim int i = 0; 114280304Sjkim unsigned int l; 11555714Skris 116280304Sjkim if (type != NULL) { 117280304Sjkim l = EVP_CIPHER_CTX_iv_length(c); 118280304Sjkim OPENSSL_assert(l <= sizeof(c->iv)); 119280304Sjkim i = ASN1_TYPE_get_octetstring(type, c->oiv, l); 120280304Sjkim if (i != (int)l) 121280304Sjkim return (-1); 122280304Sjkim else if (i > 0) 123280304Sjkim memcpy(c->iv, c->oiv, l); 124280304Sjkim } 125280304Sjkim return (i); 126280304Sjkim} 12755714Skris 12855714Skrisint EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type) 129280304Sjkim{ 130280304Sjkim int i = 0; 131280304Sjkim unsigned int j; 13255714Skris 133280304Sjkim if (type != NULL) { 134280304Sjkim j = EVP_CIPHER_CTX_iv_length(c); 135280304Sjkim OPENSSL_assert(j <= sizeof(c->iv)); 136280304Sjkim i = ASN1_TYPE_set_octetstring(type, c->oiv, j); 137280304Sjkim } 138280304Sjkim return (i); 139280304Sjkim} 14055714Skris 14155714Skris/* Convert the various cipher NIDs and dummies to a proper OID NID */ 14255714Skrisint EVP_CIPHER_type(const EVP_CIPHER *ctx) 14355714Skris{ 144280304Sjkim int nid; 145280304Sjkim ASN1_OBJECT *otmp; 146280304Sjkim nid = EVP_CIPHER_nid(ctx); 14755714Skris 148280304Sjkim switch (nid) { 14955714Skris 150280304Sjkim case NID_rc2_cbc: 151280304Sjkim case NID_rc2_64_cbc: 152280304Sjkim case NID_rc2_40_cbc: 15355714Skris 154280304Sjkim return NID_rc2_cbc; 15555714Skris 156280304Sjkim case NID_rc4: 157280304Sjkim case NID_rc4_40: 15855714Skris 159280304Sjkim return NID_rc4; 16055714Skris 161280304Sjkim case NID_aes_128_cfb128: 162280304Sjkim case NID_aes_128_cfb8: 163280304Sjkim case NID_aes_128_cfb1: 164142425Snectar 165280304Sjkim return NID_aes_128_cfb128; 166142425Snectar 167280304Sjkim case NID_aes_192_cfb128: 168280304Sjkim case NID_aes_192_cfb8: 169280304Sjkim case NID_aes_192_cfb1: 170142425Snectar 171280304Sjkim return NID_aes_192_cfb128; 172142425Snectar 173280304Sjkim case NID_aes_256_cfb128: 174280304Sjkim case NID_aes_256_cfb8: 175280304Sjkim case NID_aes_256_cfb1: 176142425Snectar 177280304Sjkim return NID_aes_256_cfb128; 178142425Snectar 179280304Sjkim case NID_des_cfb64: 180280304Sjkim case NID_des_cfb8: 181280304Sjkim case NID_des_cfb1: 182142425Snectar 183280304Sjkim return NID_des_cfb64; 184142425Snectar 185280304Sjkim case NID_des_ede3_cfb64: 186280304Sjkim case NID_des_ede3_cfb8: 187280304Sjkim case NID_des_ede3_cfb1: 188205128Ssimon 189280304Sjkim return NID_des_cfb64; 190205128Ssimon 191280304Sjkim default: 192280304Sjkim /* Check it has an OID and it is valid */ 193280304Sjkim otmp = OBJ_nid2obj(nid); 194280304Sjkim if (!otmp || !otmp->data) 195280304Sjkim nid = NID_undef; 196280304Sjkim ASN1_OBJECT_free(otmp); 197280304Sjkim return nid; 198280304Sjkim } 19955714Skris} 20055714Skris 201167612Ssimonint EVP_CIPHER_block_size(const EVP_CIPHER *e) 202280304Sjkim{ 203280304Sjkim return e->block_size; 204280304Sjkim} 205167612Ssimon 206167612Ssimonint EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx) 207280304Sjkim{ 208280304Sjkim return ctx->cipher->block_size; 209280304Sjkim} 210167612Ssimon 211280304Sjkimint EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, 212280304Sjkim const unsigned char *in, unsigned int inl) 213280304Sjkim{ 214280304Sjkim return ctx->cipher->do_cipher(ctx, out, in, inl); 215280304Sjkim} 216238405Sjkim 217167612Ssimonconst EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx) 218280304Sjkim{ 219280304Sjkim return ctx->cipher; 220280304Sjkim} 221167612Ssimon 222167612Ssimonunsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher) 223280304Sjkim{ 224280304Sjkim return cipher->flags; 225280304Sjkim} 226167612Ssimon 227238405Sjkimunsigned long EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx) 228280304Sjkim{ 229280304Sjkim return ctx->cipher->flags; 230280304Sjkim} 231238405Sjkim 232167612Ssimonvoid *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx) 233280304Sjkim{ 234280304Sjkim return ctx->app_data; 235280304Sjkim} 236167612Ssimon 237167612Ssimonvoid EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data) 238280304Sjkim{ 239280304Sjkim ctx->app_data = data; 240280304Sjkim} 241167612Ssimon 242167612Ssimonint EVP_CIPHER_iv_length(const EVP_CIPHER *cipher) 243280304Sjkim{ 244280304Sjkim return cipher->iv_len; 245280304Sjkim} 246167612Ssimon 247238405Sjkimint EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx) 248280304Sjkim{ 249280304Sjkim return ctx->cipher->iv_len; 250280304Sjkim} 251238405Sjkim 252167612Ssimonint EVP_CIPHER_key_length(const EVP_CIPHER *cipher) 253280304Sjkim{ 254280304Sjkim return cipher->key_len; 255280304Sjkim} 256167612Ssimon 257167612Ssimonint EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx) 258280304Sjkim{ 259280304Sjkim return ctx->key_len; 260280304Sjkim} 261167612Ssimon 262238405Sjkimint EVP_CIPHER_nid(const EVP_CIPHER *cipher) 263280304Sjkim{ 264280304Sjkim return cipher->nid; 265280304Sjkim} 266238405Sjkim 267167612Ssimonint EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx) 268280304Sjkim{ 269280304Sjkim return ctx->cipher->nid; 270280304Sjkim} 271167612Ssimon 272280304Sjkimint EVP_MD_block_size(const EVP_MD *md) 273280304Sjkim{ 274280304Sjkim return md->block_size; 275280304Sjkim} 276167612Ssimon 277167612Ssimonint EVP_MD_type(const EVP_MD *md) 278280304Sjkim{ 279280304Sjkim return md->type; 280280304Sjkim} 281167612Ssimon 282167612Ssimonint EVP_MD_pkey_type(const EVP_MD *md) 283280304Sjkim{ 284280304Sjkim return md->pkey_type; 285280304Sjkim} 286167612Ssimon 287167612Ssimonint EVP_MD_size(const EVP_MD *md) 288280304Sjkim{ 289280304Sjkim if (!md) { 290280304Sjkim EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL); 291280304Sjkim return -1; 292280304Sjkim } 293280304Sjkim return md->md_size; 294280304Sjkim} 295167612Ssimon 296238405Sjkimunsigned long EVP_MD_flags(const EVP_MD *md) 297280304Sjkim{ 298280304Sjkim return md->flags; 299280304Sjkim} 300238405Sjkim 301238405Sjkimconst EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx) 302280304Sjkim{ 303280304Sjkim if (!ctx) 304280304Sjkim return NULL; 305280304Sjkim return ctx->digest; 306280304Sjkim} 307167612Ssimon 308167612Ssimonvoid EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags) 309280304Sjkim{ 310280304Sjkim ctx->flags |= flags; 311280304Sjkim} 312167612Ssimon 313167612Ssimonvoid EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags) 314280304Sjkim{ 315280304Sjkim ctx->flags &= ~flags; 316280304Sjkim} 317167612Ssimon 318167612Ssimonint EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags) 319280304Sjkim{ 320280304Sjkim return (ctx->flags & flags); 321280304Sjkim} 322194206Ssimon 323194206Ssimonvoid EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags) 324280304Sjkim{ 325280304Sjkim ctx->flags |= flags; 326280304Sjkim} 327194206Ssimon 328194206Ssimonvoid EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags) 329280304Sjkim{ 330280304Sjkim ctx->flags &= ~flags; 331280304Sjkim} 332194206Ssimon 333194206Ssimonint EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags) 334280304Sjkim{ 335280304Sjkim return (ctx->flags & flags); 336280304Sjkim} 337