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.
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
5959194Skris#include "ssl_locl.h"
60110007Smarkm#ifndef OPENSSL_NO_SSL2
61296341Sdelphij# include <stdio.h>
6255714Skris
6355714Skrisint ssl2_enc_init(SSL *s, int client)
64296341Sdelphij{
65296341Sdelphij    /* Max number of bytes needed */
66296341Sdelphij    EVP_CIPHER_CTX *rs, *ws;
67296341Sdelphij    const EVP_CIPHER *c;
68296341Sdelphij    const EVP_MD *md;
69296341Sdelphij    int num;
7055714Skris
71296341Sdelphij    if (!ssl_cipher_get_evp(s->session, &c, &md, NULL, NULL, NULL)) {
72296341Sdelphij        ssl2_return_error(s, SSL2_PE_NO_CIPHER);
73296341Sdelphij        SSLerr(SSL_F_SSL2_ENC_INIT, SSL_R_PROBLEMS_MAPPING_CIPHER_FUNCTIONS);
74296341Sdelphij        return (0);
75296341Sdelphij    }
76296341Sdelphij    ssl_replace_hash(&s->read_hash, md);
77296341Sdelphij    ssl_replace_hash(&s->write_hash, md);
7855714Skris
79296341Sdelphij    if ((s->enc_read_ctx == NULL) && ((s->enc_read_ctx = (EVP_CIPHER_CTX *)
80296341Sdelphij                                       OPENSSL_malloc(sizeof(EVP_CIPHER_CTX)))
81296341Sdelphij                                      == NULL))
82296341Sdelphij        goto err;
83167615Ssimon
84296341Sdelphij    /*
85296341Sdelphij     * make sure it's intialized in case the malloc for enc_write_ctx fails
86296341Sdelphij     * and we exit with an error
87296341Sdelphij     */
88296341Sdelphij    rs = s->enc_read_ctx;
89296341Sdelphij    EVP_CIPHER_CTX_init(rs);
90167615Ssimon
91296341Sdelphij    if ((s->enc_write_ctx == NULL) && ((s->enc_write_ctx = (EVP_CIPHER_CTX *)
92296341Sdelphij                                        OPENSSL_malloc(sizeof
93296341Sdelphij                                                       (EVP_CIPHER_CTX))) ==
94296341Sdelphij                                       NULL))
95296341Sdelphij        goto err;
9655714Skris
97296341Sdelphij    ws = s->enc_write_ctx;
98296341Sdelphij    EVP_CIPHER_CTX_init(ws);
9955714Skris
100296341Sdelphij    num = c->key_len;
101296341Sdelphij    s->s2->key_material_length = num * 2;
102296341Sdelphij    OPENSSL_assert(s->s2->key_material_length <= sizeof s->s2->key_material);
10355714Skris
104296341Sdelphij    if (ssl2_generate_key_material(s) <= 0)
105296341Sdelphij        return 0;
10655714Skris
107296341Sdelphij    OPENSSL_assert(c->iv_len <= (int)sizeof(s->session->key_arg));
108296341Sdelphij    EVP_EncryptInit_ex(ws, c, NULL,
109296341Sdelphij                       &(s->s2->key_material[(client) ? num : 0]),
110296341Sdelphij                       s->session->key_arg);
111296341Sdelphij    EVP_DecryptInit_ex(rs, c, NULL,
112296341Sdelphij                       &(s->s2->key_material[(client) ? 0 : num]),
113296341Sdelphij                       s->session->key_arg);
114296341Sdelphij    s->s2->read_key = &(s->s2->key_material[(client) ? 0 : num]);
115296341Sdelphij    s->s2->write_key = &(s->s2->key_material[(client) ? num : 0]);
116296341Sdelphij    return (1);
117296341Sdelphij err:
118296341Sdelphij    SSLerr(SSL_F_SSL2_ENC_INIT, ERR_R_MALLOC_FAILURE);
119296341Sdelphij    return (0);
120296341Sdelphij}
12155714Skris
122296341Sdelphij/*
123296341Sdelphij * read/writes from s->s2->mac_data using length for encrypt and decrypt.
124296341Sdelphij * It sets s->s2->padding and s->[rw]length if we are encrypting Returns 0 on
125296341Sdelphij * error and 1 on success
126296341Sdelphij */
127279264Sdelphijint ssl2_enc(SSL *s, int send)
128296341Sdelphij{
129296341Sdelphij    EVP_CIPHER_CTX *ds;
130296341Sdelphij    unsigned long l;
131296341Sdelphij    int bs;
13255714Skris
133296341Sdelphij    if (send) {
134296341Sdelphij        ds = s->enc_write_ctx;
135296341Sdelphij        l = s->s2->wlength;
136296341Sdelphij    } else {
137296341Sdelphij        ds = s->enc_read_ctx;
138296341Sdelphij        l = s->s2->rlength;
139296341Sdelphij    }
14055714Skris
141296341Sdelphij    /* check for NULL cipher */
142296341Sdelphij    if (ds == NULL)
143296341Sdelphij        return 1;
14455714Skris
145296341Sdelphij    bs = ds->cipher->block_size;
146296341Sdelphij    /*
147296341Sdelphij     * This should be using (bs-1) and bs instead of 7 and 8, but what the
148296341Sdelphij     * hell.
149296341Sdelphij     */
150296341Sdelphij    if (bs == 8)
151296341Sdelphij        l = (l + 7) / 8 * 8;
15255714Skris
153296341Sdelphij    if (EVP_Cipher(ds, s->s2->mac_data, s->s2->mac_data, l) < 1)
154296341Sdelphij        return 0;
15555714Skris
156296341Sdelphij    return 1;
157296341Sdelphij}
158279264Sdelphij
15955714Skrisvoid ssl2_mac(SSL *s, unsigned char *md, int send)
160296341Sdelphij{
161296341Sdelphij    EVP_MD_CTX c;
162296341Sdelphij    unsigned char sequence[4], *p, *sec, *act;
163296341Sdelphij    unsigned long seq;
164296341Sdelphij    unsigned int len;
16555714Skris
166296341Sdelphij    if (send) {
167296341Sdelphij        seq = s->s2->write_sequence;
168296341Sdelphij        sec = s->s2->write_key;
169296341Sdelphij        len = s->s2->wact_data_length;
170296341Sdelphij        act = s->s2->wact_data;
171296341Sdelphij    } else {
172296341Sdelphij        seq = s->s2->read_sequence;
173296341Sdelphij        sec = s->s2->read_key;
174296341Sdelphij        len = s->s2->ract_data_length;
175296341Sdelphij        act = s->s2->ract_data;
176296341Sdelphij    }
17755714Skris
178296341Sdelphij    p = &(sequence[0]);
179296341Sdelphij    l2n(seq, p);
18055714Skris
181296341Sdelphij    /* There has to be a MAC algorithm. */
182296341Sdelphij    EVP_MD_CTX_init(&c);
183296341Sdelphij    EVP_MD_CTX_copy(&c, s->read_hash);
184296341Sdelphij    EVP_DigestUpdate(&c, sec, EVP_CIPHER_CTX_key_length(s->enc_read_ctx));
185296341Sdelphij    EVP_DigestUpdate(&c, act, len);
186296341Sdelphij    /* the above line also does the pad data */
187296341Sdelphij    EVP_DigestUpdate(&c, sequence, 4);
188296341Sdelphij    EVP_DigestFinal_ex(&c, md, NULL);
189296341Sdelphij    EVP_MD_CTX_cleanup(&c);
190296341Sdelphij}
191296341Sdelphij#else                           /* !OPENSSL_NO_SSL2 */
19255714Skris
19359194Skris# if PEDANTIC
194296341Sdelphijstatic void *dummy = &dummy;
19559194Skris# endif
19659194Skris
19755949Skris#endif
198