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. 8296465Sdelphij * 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). 15296465Sdelphij * 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. 22296465Sdelphij * 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 :-). 37296465Sdelphij * 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)" 40296465Sdelphij * 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. 52296465Sdelphij * 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 5959194Skris#include "ssl_locl.h" 60110007Smarkm#ifndef OPENSSL_NO_SSL2 61296465Sdelphij# include <stdio.h> 6255714Skris 6355714Skrisint ssl2_enc_init(SSL *s, int client) 64296465Sdelphij{ 65296465Sdelphij /* Max number of bytes needed */ 66296465Sdelphij EVP_CIPHER_CTX *rs, *ws; 67296465Sdelphij const EVP_CIPHER *c; 68296465Sdelphij const EVP_MD *md; 69296465Sdelphij int num; 7055714Skris 71296465Sdelphij if (!ssl_cipher_get_evp(s->session, &c, &md, NULL)) { 72296465Sdelphij ssl2_return_error(s, SSL2_PE_NO_CIPHER); 73296465Sdelphij SSLerr(SSL_F_SSL2_ENC_INIT, SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS); 74296465Sdelphij return (0); 75296465Sdelphij } 7655714Skris 77296465Sdelphij s->read_hash = md; 78296465Sdelphij s->write_hash = md; 7955714Skris 80296465Sdelphij if ((s->enc_read_ctx == NULL) && ((s->enc_read_ctx = (EVP_CIPHER_CTX *) 81296465Sdelphij OPENSSL_malloc(sizeof(EVP_CIPHER_CTX))) 82296465Sdelphij == NULL)) 83296465Sdelphij goto err; 84167615Ssimon 85296465Sdelphij /* 86296465Sdelphij * make sure it's intialized in case the malloc for enc_write_ctx fails 87296465Sdelphij * and we exit with an error 88296465Sdelphij */ 89296465Sdelphij rs = s->enc_read_ctx; 90296465Sdelphij EVP_CIPHER_CTX_init(rs); 91167615Ssimon 92296465Sdelphij if ((s->enc_write_ctx == NULL) && ((s->enc_write_ctx = (EVP_CIPHER_CTX *) 93296465Sdelphij OPENSSL_malloc(sizeof 94296465Sdelphij (EVP_CIPHER_CTX))) == 95296465Sdelphij NULL)) 96296465Sdelphij goto err; 9755714Skris 98296465Sdelphij ws = s->enc_write_ctx; 99296465Sdelphij EVP_CIPHER_CTX_init(ws); 10055714Skris 101296465Sdelphij num = c->key_len; 102296465Sdelphij s->s2->key_material_length = num * 2; 103296465Sdelphij OPENSSL_assert(s->s2->key_material_length <= sizeof s->s2->key_material); 10455714Skris 105296465Sdelphij if (ssl2_generate_key_material(s) <= 0) 106296465Sdelphij return 0; 10755714Skris 108296465Sdelphij OPENSSL_assert(c->iv_len <= (int)sizeof(s->session->key_arg)); 109296465Sdelphij EVP_EncryptInit_ex(ws, c, NULL, 110296465Sdelphij &(s->s2->key_material[(client) ? num : 0]), 111296465Sdelphij s->session->key_arg); 112296465Sdelphij EVP_DecryptInit_ex(rs, c, NULL, 113296465Sdelphij &(s->s2->key_material[(client) ? 0 : num]), 114296465Sdelphij s->session->key_arg); 115296465Sdelphij s->s2->read_key = &(s->s2->key_material[(client) ? 0 : num]); 116296465Sdelphij s->s2->write_key = &(s->s2->key_material[(client) ? num : 0]); 117296465Sdelphij return (1); 118296465Sdelphij err: 119296465Sdelphij SSLerr(SSL_F_SSL2_ENC_INIT, ERR_R_MALLOC_FAILURE); 120296465Sdelphij return (0); 121296465Sdelphij} 12255714Skris 123296465Sdelphij/* 124296465Sdelphij * read/writes from s->s2->mac_data using length for encrypt and decrypt. 125296465Sdelphij * It sets s->s2->padding and s->[rw]length if we are encrypting 126296465Sdelphij */ 12755714Skrisvoid ssl2_enc(SSL *s, int send) 128296465Sdelphij{ 129296465Sdelphij EVP_CIPHER_CTX *ds; 130296465Sdelphij unsigned long l; 131296465Sdelphij int bs; 13255714Skris 133296465Sdelphij if (send) { 134296465Sdelphij ds = s->enc_write_ctx; 135296465Sdelphij l = s->s2->wlength; 136296465Sdelphij } else { 137296465Sdelphij ds = s->enc_read_ctx; 138296465Sdelphij l = s->s2->rlength; 139296465Sdelphij } 14055714Skris 141296465Sdelphij /* check for NULL cipher */ 142296465Sdelphij if (ds == NULL) 143296465Sdelphij return; 14455714Skris 145296465Sdelphij bs = ds->cipher->block_size; 146296465Sdelphij /* 147296465Sdelphij * This should be using (bs-1) and bs instead of 7 and 8, but what the 148296465Sdelphij * hell. 149296465Sdelphij */ 150296465Sdelphij if (bs == 8) 151296465Sdelphij l = (l + 7) / 8 * 8; 15255714Skris 153296465Sdelphij EVP_Cipher(ds, s->s2->mac_data, s->s2->mac_data, l); 154296465Sdelphij} 15555714Skris 15655714Skrisvoid ssl2_mac(SSL *s, unsigned char *md, int send) 157296465Sdelphij{ 158296465Sdelphij EVP_MD_CTX c; 159296465Sdelphij unsigned char sequence[4], *p, *sec, *act; 160296465Sdelphij unsigned long seq; 161296465Sdelphij unsigned int len; 16255714Skris 163296465Sdelphij if (send) { 164296465Sdelphij seq = s->s2->write_sequence; 165296465Sdelphij sec = s->s2->write_key; 166296465Sdelphij len = s->s2->wact_data_length; 167296465Sdelphij act = s->s2->wact_data; 168296465Sdelphij } else { 169296465Sdelphij seq = s->s2->read_sequence; 170296465Sdelphij sec = s->s2->read_key; 171296465Sdelphij len = s->s2->ract_data_length; 172296465Sdelphij act = s->s2->ract_data; 173296465Sdelphij } 17455714Skris 175296465Sdelphij p = &(sequence[0]); 176296465Sdelphij l2n(seq, p); 17755714Skris 178296465Sdelphij /* There has to be a MAC algorithm. */ 179296465Sdelphij EVP_MD_CTX_init(&c); 180296465Sdelphij EVP_DigestInit_ex(&c, s->read_hash, NULL); 181296465Sdelphij EVP_DigestUpdate(&c, sec, EVP_CIPHER_CTX_key_length(s->enc_read_ctx)); 182296465Sdelphij EVP_DigestUpdate(&c, act, len); 183296465Sdelphij /* the above line also does the pad data */ 184296465Sdelphij EVP_DigestUpdate(&c, sequence, 4); 185296465Sdelphij EVP_DigestFinal_ex(&c, md, NULL); 186296465Sdelphij EVP_MD_CTX_cleanup(&c); 187296465Sdelphij} 188296465Sdelphij#else /* !OPENSSL_NO_SSL2 */ 18955714Skris 19059194Skris# if PEDANTIC 191296465Sdelphijstatic void *dummy = &dummy; 19259194Skris# endif 19359194Skris 19455949Skris#endif 195