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.
855714Skris *
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).
1555714Skris *
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.
2255714Skris *
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 :-).
3755714Skris * 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)"
4055714Skris *
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.
5255714Skris *
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)
6555714Skris	{
6655714Skris	int ret;
6755714Skris
6855714Skris	if (c->cipher->set_asn1_parameters != NULL)
6955714Skris		ret=c->cipher->set_asn1_parameters(c,type);
70194206Ssimon	else if (c->cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1)
71194206Ssimon		ret=EVP_CIPHER_set_asn1_iv(c, type);
7255714Skris	else
73160814Ssimon		ret=-1;
7455714Skris	return(ret);
7555714Skris	}
7655714Skris
7755714Skrisint EVP_CIPHER_asn1_to_param(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
7855714Skris	{
7955714Skris	int ret;
8055714Skris
8155714Skris	if (c->cipher->get_asn1_parameters != NULL)
8255714Skris		ret=c->cipher->get_asn1_parameters(c,type);
83194206Ssimon	else if (c->cipher->flags & EVP_CIPH_FLAG_DEFAULT_ASN1)
84194206Ssimon		ret=EVP_CIPHER_get_asn1_iv(c, type);
8555714Skris	else
86160814Ssimon		ret=-1;
8755714Skris	return(ret);
8855714Skris	}
8955714Skris
9055714Skrisint EVP_CIPHER_get_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
9155714Skris	{
92160814Ssimon	int i=0;
93160814Ssimon	unsigned int l;
9455714Skris
9555714Skris	if (type != NULL)
9655714Skris		{
9755714Skris		l=EVP_CIPHER_CTX_iv_length(c);
98160814Ssimon		OPENSSL_assert(l <= sizeof(c->iv));
9955714Skris		i=ASN1_TYPE_get_octetstring(type,c->oiv,l);
100160814Ssimon		if (i != (int)l)
10155714Skris			return(-1);
10255714Skris		else if (i > 0)
10355714Skris			memcpy(c->iv,c->oiv,l);
10455714Skris		}
10555714Skris	return(i);
10655714Skris	}
10755714Skris
10855714Skrisint EVP_CIPHER_set_asn1_iv(EVP_CIPHER_CTX *c, ASN1_TYPE *type)
10955714Skris	{
110160814Ssimon	int i=0;
111160814Ssimon	unsigned int j;
11255714Skris
11355714Skris	if (type != NULL)
11455714Skris		{
11555714Skris		j=EVP_CIPHER_CTX_iv_length(c);
116160814Ssimon		OPENSSL_assert(j <= sizeof(c->iv));
11755714Skris		i=ASN1_TYPE_set_octetstring(type,c->oiv,j);
11855714Skris		}
11955714Skris	return(i);
12055714Skris	}
12155714Skris
12255714Skris/* Convert the various cipher NIDs and dummies to a proper OID NID */
12355714Skrisint EVP_CIPHER_type(const EVP_CIPHER *ctx)
12455714Skris{
12555714Skris	int nid;
12659191Skris	ASN1_OBJECT *otmp;
12755714Skris	nid = EVP_CIPHER_nid(ctx);
12855714Skris
12955714Skris	switch(nid) {
13055714Skris
13155714Skris		case NID_rc2_cbc:
13255714Skris		case NID_rc2_64_cbc:
13355714Skris		case NID_rc2_40_cbc:
13455714Skris
13555714Skris		return NID_rc2_cbc;
13655714Skris
13755714Skris		case NID_rc4:
13855714Skris		case NID_rc4_40:
13955714Skris
14055714Skris		return NID_rc4;
14155714Skris
142142425Snectar		case NID_aes_128_cfb128:
143142425Snectar		case NID_aes_128_cfb8:
144142425Snectar		case NID_aes_128_cfb1:
145142425Snectar
146142425Snectar		return NID_aes_128_cfb128;
147142425Snectar
148142425Snectar		case NID_aes_192_cfb128:
149142425Snectar		case NID_aes_192_cfb8:
150142425Snectar		case NID_aes_192_cfb1:
151142425Snectar
152142425Snectar		return NID_aes_192_cfb128;
153142425Snectar
154142425Snectar		case NID_aes_256_cfb128:
155142425Snectar		case NID_aes_256_cfb8:
156142425Snectar		case NID_aes_256_cfb1:
157142425Snectar
158142425Snectar		return NID_aes_256_cfb128;
159142425Snectar
160142425Snectar		case NID_des_cfb64:
161142425Snectar		case NID_des_cfb8:
162142425Snectar		case NID_des_cfb1:
163142425Snectar
164142425Snectar		return NID_des_cfb64;
165142425Snectar
166205128Ssimon		case NID_des_ede3_cfb64:
167205128Ssimon		case NID_des_ede3_cfb8:
168205128Ssimon		case NID_des_ede3_cfb1:
169205128Ssimon
170205128Ssimon		return NID_des_cfb64;
171205128Ssimon
17255714Skris		default:
17359191Skris		/* Check it has an OID and it is valid */
17459191Skris		otmp = OBJ_nid2obj(nid);
17559191Skris		if(!otmp || !otmp->data) nid = NID_undef;
17659191Skris		ASN1_OBJECT_free(otmp);
17755714Skris		return nid;
17855714Skris	}
17955714Skris}
18055714Skris
181167612Ssimonint EVP_CIPHER_block_size(const EVP_CIPHER *e)
182167612Ssimon	{
183167612Ssimon	return e->block_size;
184167612Ssimon	}
185167612Ssimon
186167612Ssimonint EVP_CIPHER_CTX_block_size(const EVP_CIPHER_CTX *ctx)
187167612Ssimon	{
188167612Ssimon	return ctx->cipher->block_size;
189167612Ssimon	}
190167612Ssimon
191238405Sjkimint EVP_Cipher(EVP_CIPHER_CTX *ctx, unsigned char *out, const unsigned char *in, unsigned int inl)
192238405Sjkim	{
193238405Sjkim	return ctx->cipher->do_cipher(ctx,out,in,inl);
194238405Sjkim	}
195238405Sjkim
196167612Ssimonconst EVP_CIPHER *EVP_CIPHER_CTX_cipher(const EVP_CIPHER_CTX *ctx)
197167612Ssimon	{
198167612Ssimon	return ctx->cipher;
199167612Ssimon	}
200167612Ssimon
201167612Ssimonunsigned long EVP_CIPHER_flags(const EVP_CIPHER *cipher)
202167612Ssimon	{
203167612Ssimon	return cipher->flags;
204167612Ssimon	}
205167612Ssimon
206238405Sjkimunsigned long EVP_CIPHER_CTX_flags(const EVP_CIPHER_CTX *ctx)
207238405Sjkim	{
208238405Sjkim	return ctx->cipher->flags;
209238405Sjkim	}
210238405Sjkim
211167612Ssimonvoid *EVP_CIPHER_CTX_get_app_data(const EVP_CIPHER_CTX *ctx)
212167612Ssimon	{
213167612Ssimon	return ctx->app_data;
214167612Ssimon	}
215167612Ssimon
216167612Ssimonvoid EVP_CIPHER_CTX_set_app_data(EVP_CIPHER_CTX *ctx, void *data)
217167612Ssimon	{
218167612Ssimon	ctx->app_data = data;
219167612Ssimon	}
220167612Ssimon
221167612Ssimonint EVP_CIPHER_iv_length(const EVP_CIPHER *cipher)
222167612Ssimon	{
223167612Ssimon	return cipher->iv_len;
224167612Ssimon	}
225167612Ssimon
226238405Sjkimint EVP_CIPHER_CTX_iv_length(const EVP_CIPHER_CTX *ctx)
227238405Sjkim	{
228238405Sjkim	return ctx->cipher->iv_len;
229238405Sjkim	}
230238405Sjkim
231167612Ssimonint EVP_CIPHER_key_length(const EVP_CIPHER *cipher)
232167612Ssimon	{
233167612Ssimon	return cipher->key_len;
234167612Ssimon	}
235167612Ssimon
236167612Ssimonint EVP_CIPHER_CTX_key_length(const EVP_CIPHER_CTX *ctx)
237167612Ssimon	{
238167617Ssimon	return ctx->key_len;
239167612Ssimon	}
240167612Ssimon
241238405Sjkimint EVP_CIPHER_nid(const EVP_CIPHER *cipher)
242238405Sjkim	{
243238405Sjkim	return cipher->nid;
244238405Sjkim	}
245238405Sjkim
246167612Ssimonint EVP_CIPHER_CTX_nid(const EVP_CIPHER_CTX *ctx)
247167612Ssimon	{
248167612Ssimon	return ctx->cipher->nid;
249167612Ssimon	}
250167612Ssimon
251167612Ssimonint EVP_MD_block_size(const EVP_MD *md)
252167612Ssimon	{
253167612Ssimon	return md->block_size;
254167612Ssimon	}
255167612Ssimon
256167612Ssimonint EVP_MD_type(const EVP_MD *md)
257167612Ssimon	{
258167612Ssimon	return md->type;
259167612Ssimon	}
260167612Ssimon
261167612Ssimonint EVP_MD_pkey_type(const EVP_MD *md)
262167612Ssimon	{
263167612Ssimon	return md->pkey_type;
264167612Ssimon	}
265167612Ssimon
266167612Ssimonint EVP_MD_size(const EVP_MD *md)
267167612Ssimon	{
268238405Sjkim	if (!md)
269238405Sjkim		{
270238405Sjkim		EVPerr(EVP_F_EVP_MD_SIZE, EVP_R_MESSAGE_DIGEST_IS_NULL);
271238405Sjkim		return -1;
272238405Sjkim		}
273167612Ssimon	return md->md_size;
274167612Ssimon	}
275167612Ssimon
276238405Sjkimunsigned long EVP_MD_flags(const EVP_MD *md)
277167612Ssimon	{
278238405Sjkim	return md->flags;
279238405Sjkim	}
280238405Sjkim
281238405Sjkimconst EVP_MD *EVP_MD_CTX_md(const EVP_MD_CTX *ctx)
282238405Sjkim	{
283238405Sjkim	if (!ctx)
284238405Sjkim		return NULL;
285167612Ssimon	return ctx->digest;
286167612Ssimon	}
287167612Ssimon
288167612Ssimonvoid EVP_MD_CTX_set_flags(EVP_MD_CTX *ctx, int flags)
289167612Ssimon	{
290167612Ssimon	ctx->flags |= flags;
291167612Ssimon	}
292167612Ssimon
293167612Ssimonvoid EVP_MD_CTX_clear_flags(EVP_MD_CTX *ctx, int flags)
294167612Ssimon	{
295167612Ssimon	ctx->flags &= ~flags;
296167612Ssimon	}
297167612Ssimon
298167612Ssimonint EVP_MD_CTX_test_flags(const EVP_MD_CTX *ctx, int flags)
299167612Ssimon	{
300167612Ssimon	return (ctx->flags & flags);
301167612Ssimon	}
302194206Ssimon
303194206Ssimonvoid EVP_CIPHER_CTX_set_flags(EVP_CIPHER_CTX *ctx, int flags)
304194206Ssimon	{
305194206Ssimon	ctx->flags |= flags;
306194206Ssimon	}
307194206Ssimon
308194206Ssimonvoid EVP_CIPHER_CTX_clear_flags(EVP_CIPHER_CTX *ctx, int flags)
309194206Ssimon	{
310194206Ssimon	ctx->flags &= ~flags;
311194206Ssimon	}
312194206Ssimon
313194206Ssimonint EVP_CIPHER_CTX_test_flags(const EVP_CIPHER_CTX *ctx, int flags)
314194206Ssimon	{
315194206Ssimon	return (ctx->flags & flags);
316194206Ssimon	}
317