s2_enc.c revision 59194
155714Skris/* ssl/s2_enc.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.] 5755949Skris * 5855949Skris * $FreeBSD: head/crypto/openssl/ssl/s2_enc.c 59194 2000-04-13 07:15:03Z kris $ 5955714Skris */ 6055714Skris 6159194Skris#include "ssl_locl.h" 6255949Skris#ifndef NO_SSL2 6355714Skris#include <stdio.h> 6455714Skris 6555714Skrisint ssl2_enc_init(SSL *s, int client) 6655714Skris { 6755714Skris /* Max number of bytes needed */ 6855714Skris EVP_CIPHER_CTX *rs,*ws; 6955714Skris const EVP_CIPHER *c; 7055714Skris const EVP_MD *md; 7155714Skris int num; 7255714Skris 7355714Skris if (!ssl_cipher_get_evp(s->session,&c,&md,NULL)) 7455714Skris { 7555714Skris ssl2_return_error(s,SSL2_PE_NO_CIPHER); 7655714Skris SSLerr(SSL_F_SSL2_ENC_INIT,SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS); 7755714Skris return(0); 7855714Skris } 7955714Skris 8055714Skris s->read_hash=md; 8155714Skris s->write_hash=md; 8255714Skris 8355714Skris if ((s->enc_read_ctx == NULL) && 8455714Skris ((s->enc_read_ctx=(EVP_CIPHER_CTX *) 8555714Skris Malloc(sizeof(EVP_CIPHER_CTX))) == NULL)) 8655714Skris goto err; 8755714Skris if ((s->enc_write_ctx == NULL) && 8855714Skris ((s->enc_write_ctx=(EVP_CIPHER_CTX *) 8955714Skris Malloc(sizeof(EVP_CIPHER_CTX))) == NULL)) 9055714Skris goto err; 9155714Skris 9255714Skris rs= s->enc_read_ctx; 9355714Skris ws= s->enc_write_ctx; 9455714Skris 9555714Skris EVP_CIPHER_CTX_init(rs); 9655714Skris EVP_CIPHER_CTX_init(ws); 9755714Skris 9855714Skris num=c->key_len; 9955714Skris s->s2->key_material_length=num*2; 10055714Skris 10155714Skris ssl2_generate_key_material(s); 10255714Skris 10355714Skris EVP_EncryptInit(ws,c,&(s->s2->key_material[(client)?num:0]), 10455714Skris s->session->key_arg); 10555714Skris EVP_DecryptInit(rs,c,&(s->s2->key_material[(client)?0:num]), 10655714Skris s->session->key_arg); 10755714Skris s->s2->read_key= &(s->s2->key_material[(client)?0:num]); 10855714Skris s->s2->write_key= &(s->s2->key_material[(client)?num:0]); 10955714Skris return(1); 11055714Skriserr: 11155714Skris SSLerr(SSL_F_SSL2_ENC_INIT,ERR_R_MALLOC_FAILURE); 11255714Skris return(0); 11355714Skris } 11455714Skris 11555714Skris/* read/writes from s->s2->mac_data using length for encrypt and 11655714Skris * decrypt. It sets the s->s2->padding, s->[rw]length and 11755714Skris * s->s2->pad_data ptr if we are encrypting */ 11855714Skrisvoid ssl2_enc(SSL *s, int send) 11955714Skris { 12055714Skris EVP_CIPHER_CTX *ds; 12155714Skris unsigned long l; 12255714Skris int bs; 12355714Skris 12455714Skris if (send) 12555714Skris { 12655714Skris ds=s->enc_write_ctx; 12755714Skris l=s->s2->wlength; 12855714Skris } 12955714Skris else 13055714Skris { 13155714Skris ds=s->enc_read_ctx; 13255714Skris l=s->s2->rlength; 13355714Skris } 13455714Skris 13555714Skris /* check for NULL cipher */ 13655714Skris if (ds == NULL) return; 13755714Skris 13855714Skris 13955714Skris bs=ds->cipher->block_size; 14055714Skris /* This should be using (bs-1) and bs instead of 7 and 8, but 14155714Skris * what the hell. */ 14255714Skris if (bs == 8) 14355714Skris l=(l+7)/8*8; 14455714Skris 14555714Skris EVP_Cipher(ds,s->s2->mac_data,s->s2->mac_data,l); 14655714Skris } 14755714Skris 14855714Skrisvoid ssl2_mac(SSL *s, unsigned char *md, int send) 14955714Skris { 15055714Skris EVP_MD_CTX c; 15155714Skris unsigned char sequence[4],*p,*sec,*act; 15255714Skris unsigned long seq; 15355714Skris unsigned int len; 15455714Skris 15555714Skris if (send) 15655714Skris { 15755714Skris seq=s->s2->write_sequence; 15855714Skris sec=s->s2->write_key; 15955714Skris len=s->s2->wact_data_length; 16055714Skris act=s->s2->wact_data; 16155714Skris } 16255714Skris else 16355714Skris { 16455714Skris seq=s->s2->read_sequence; 16555714Skris sec=s->s2->read_key; 16655714Skris len=s->s2->ract_data_length; 16755714Skris act=s->s2->ract_data; 16855714Skris } 16955714Skris 17055714Skris p= &(sequence[0]); 17155714Skris l2n(seq,p); 17255714Skris 17355714Skris /* There has to be a MAC algorithm. */ 17455714Skris EVP_DigestInit(&c,s->read_hash); 17555714Skris EVP_DigestUpdate(&c,sec, 17655714Skris EVP_CIPHER_CTX_key_length(s->enc_read_ctx)); 17755714Skris EVP_DigestUpdate(&c,act,len); 17855714Skris /* the above line also does the pad data */ 17955714Skris EVP_DigestUpdate(&c,sequence,4); 18055714Skris EVP_DigestFinal(&c,md,NULL); 18155714Skris /* some would say I should zero the md context */ 18255714Skris } 18359194Skris#else /* !NO_SSL2 */ 18455714Skris 18559194Skris# if PEDANTIC 18659194Skrisstatic void *dummy=&dummy; 18759194Skris# endif 18859194Skris 18955949Skris#endif 190