1160814Ssimon/* ssl/d1_enc.c */
2280304Sjkim/*
3160814Ssimon * DTLS implementation written by Nagendra Modadugu
4280304Sjkim * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
5160814Ssimon */
6160814Ssimon/* ====================================================================
7160814Ssimon * Copyright (c) 1998-2005 The OpenSSL Project.  All rights reserved.
8160814Ssimon *
9160814Ssimon * Redistribution and use in source and binary forms, with or without
10160814Ssimon * modification, are permitted provided that the following conditions
11160814Ssimon * are met:
12160814Ssimon *
13160814Ssimon * 1. Redistributions of source code must retain the above copyright
14280304Sjkim *    notice, this list of conditions and the following disclaimer.
15160814Ssimon *
16160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright
17160814Ssimon *    notice, this list of conditions and the following disclaimer in
18160814Ssimon *    the documentation and/or other materials provided with the
19160814Ssimon *    distribution.
20160814Ssimon *
21160814Ssimon * 3. All advertising materials mentioning features or use of this
22160814Ssimon *    software must display the following acknowledgment:
23160814Ssimon *    "This product includes software developed by the OpenSSL Project
24160814Ssimon *    for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
25160814Ssimon *
26160814Ssimon * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27160814Ssimon *    endorse or promote products derived from this software without
28160814Ssimon *    prior written permission. For written permission, please contact
29160814Ssimon *    openssl-core@openssl.org.
30160814Ssimon *
31160814Ssimon * 5. Products derived from this software may not be called "OpenSSL"
32160814Ssimon *    nor may "OpenSSL" appear in their names without prior written
33160814Ssimon *    permission of the OpenSSL Project.
34160814Ssimon *
35160814Ssimon * 6. Redistributions of any form whatsoever must retain the following
36160814Ssimon *    acknowledgment:
37160814Ssimon *    "This product includes software developed by the OpenSSL Project
38160814Ssimon *    for use in the OpenSSL Toolkit (http://www.openssl.org/)"
39160814Ssimon *
40160814Ssimon * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41160814Ssimon * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43160814Ssimon * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44160814Ssimon * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45160814Ssimon * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46160814Ssimon * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47160814Ssimon * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49160814Ssimon * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50160814Ssimon * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51160814Ssimon * OF THE POSSIBILITY OF SUCH DAMAGE.
52160814Ssimon * ====================================================================
53160814Ssimon *
54160814Ssimon * This product includes cryptographic software written by Eric Young
55160814Ssimon * (eay@cryptsoft.com).  This product includes software written by Tim
56160814Ssimon * Hudson (tjh@cryptsoft.com).
57160814Ssimon *
58160814Ssimon */
59160814Ssimon/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
60160814Ssimon * All rights reserved.
61160814Ssimon *
62160814Ssimon * This package is an SSL implementation written
63160814Ssimon * by Eric Young (eay@cryptsoft.com).
64160814Ssimon * The implementation was written so as to conform with Netscapes SSL.
65280304Sjkim *
66160814Ssimon * This library is free for commercial and non-commercial use as long as
67160814Ssimon * the following conditions are aheared to.  The following conditions
68160814Ssimon * apply to all code found in this distribution, be it the RC4, RSA,
69160814Ssimon * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
70160814Ssimon * included with this distribution is covered by the same copyright terms
71160814Ssimon * except that the holder is Tim Hudson (tjh@cryptsoft.com).
72280304Sjkim *
73160814Ssimon * Copyright remains Eric Young's, and as such any Copyright notices in
74160814Ssimon * the code are not to be removed.
75160814Ssimon * If this package is used in a product, Eric Young should be given attribution
76160814Ssimon * as the author of the parts of the library used.
77160814Ssimon * This can be in the form of a textual message at program startup or
78160814Ssimon * in documentation (online or textual) provided with the package.
79280304Sjkim *
80160814Ssimon * Redistribution and use in source and binary forms, with or without
81160814Ssimon * modification, are permitted provided that the following conditions
82160814Ssimon * are met:
83160814Ssimon * 1. Redistributions of source code must retain the copyright
84160814Ssimon *    notice, this list of conditions and the following disclaimer.
85160814Ssimon * 2. Redistributions in binary form must reproduce the above copyright
86160814Ssimon *    notice, this list of conditions and the following disclaimer in the
87160814Ssimon *    documentation and/or other materials provided with the distribution.
88160814Ssimon * 3. All advertising materials mentioning features or use of this software
89160814Ssimon *    must display the following acknowledgement:
90160814Ssimon *    "This product includes cryptographic software written by
91160814Ssimon *     Eric Young (eay@cryptsoft.com)"
92160814Ssimon *    The word 'cryptographic' can be left out if the rouines from the library
93160814Ssimon *    being used are not cryptographic related :-).
94280304Sjkim * 4. If you include any Windows specific code (or a derivative thereof) from
95160814Ssimon *    the apps directory (application code) you must include an acknowledgement:
96160814Ssimon *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
97280304Sjkim *
98160814Ssimon * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
99160814Ssimon * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100160814Ssimon * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101160814Ssimon * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
102160814Ssimon * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103160814Ssimon * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104160814Ssimon * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105160814Ssimon * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106160814Ssimon * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107160814Ssimon * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
108160814Ssimon * SUCH DAMAGE.
109280304Sjkim *
110160814Ssimon * The licence and distribution terms for any publically available version or
111160814Ssimon * derivative of this code cannot be changed.  i.e. this code cannot simply be
112160814Ssimon * copied and put under another distribution licence
113160814Ssimon * [including the GNU Public Licence.]
114160814Ssimon */
115160814Ssimon
116160814Ssimon#include <stdio.h>
117160814Ssimon#include "ssl_locl.h"
118194206Ssimon#ifndef OPENSSL_NO_COMP
119280304Sjkim# include <openssl/comp.h>
120194206Ssimon#endif
121160814Ssimon#include <openssl/evp.h>
122160814Ssimon#include <openssl/hmac.h>
123160814Ssimon#include <openssl/md5.h>
124160814Ssimon#include <openssl/rand.h>
125194206Ssimon#ifdef KSSL_DEBUG
126280304Sjkim# include <openssl/des.h>
127194206Ssimon#endif
128160814Ssimon
129280304Sjkim/*-
130280304Sjkim * dtls1_enc encrypts/decrypts the record in |s->wrec| / |s->rrec|, respectively.
131246772Sjkim *
132246772Sjkim * Returns:
133246772Sjkim *   0: (in non-constant time) if the record is publically invalid (i.e. too
134246772Sjkim *       short etc).
135246772Sjkim *   1: if the record's padding is valid / the encryption was successful.
136246772Sjkim *   -1: if the record's padding/AEAD-authenticator is invalid or, if sending,
137280304Sjkim *       an internal error occured.
138280304Sjkim */
139160814Ssimonint dtls1_enc(SSL *s, int send)
140280304Sjkim{
141280304Sjkim    SSL3_RECORD *rec;
142280304Sjkim    EVP_CIPHER_CTX *ds;
143280304Sjkim    unsigned long l;
144280304Sjkim    int bs, i, j, k, mac_size = 0;
145280304Sjkim    const EVP_CIPHER *enc;
146160814Ssimon
147280304Sjkim    if (send) {
148280304Sjkim        if (EVP_MD_CTX_md(s->write_hash)) {
149280304Sjkim            mac_size = EVP_MD_CTX_size(s->write_hash);
150280304Sjkim            if (mac_size < 0)
151280304Sjkim                return -1;
152280304Sjkim        }
153280304Sjkim        ds = s->enc_write_ctx;
154280304Sjkim        rec = &(s->s3->wrec);
155280304Sjkim        if (s->enc_write_ctx == NULL)
156280304Sjkim            enc = NULL;
157280304Sjkim        else {
158280304Sjkim            enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
159280304Sjkim            if (rec->data != rec->input)
160280304Sjkim                /* we can't write into the input stream */
161280304Sjkim                fprintf(stderr, "%s:%d: rec->data != rec->input\n",
162280304Sjkim                        __FILE__, __LINE__);
163280304Sjkim            else if (EVP_CIPHER_block_size(ds->cipher) > 1) {
164280304Sjkim                if (RAND_bytes(rec->input, EVP_CIPHER_block_size(ds->cipher))
165280304Sjkim                    <= 0)
166280304Sjkim                    return -1;
167280304Sjkim            }
168280304Sjkim        }
169280304Sjkim    } else {
170280304Sjkim        if (EVP_MD_CTX_md(s->read_hash)) {
171280304Sjkim            mac_size = EVP_MD_CTX_size(s->read_hash);
172280304Sjkim            OPENSSL_assert(mac_size >= 0);
173280304Sjkim        }
174280304Sjkim        ds = s->enc_read_ctx;
175280304Sjkim        rec = &(s->s3->rrec);
176280304Sjkim        if (s->enc_read_ctx == NULL)
177280304Sjkim            enc = NULL;
178280304Sjkim        else
179280304Sjkim            enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
180280304Sjkim    }
181160814Ssimon
182160814Ssimon#ifdef KSSL_DEBUG
183280304Sjkim    printf("dtls1_enc(%d)\n", send);
184280304Sjkim#endif                          /* KSSL_DEBUG */
185160814Ssimon
186280304Sjkim    if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
187280304Sjkim        memmove(rec->data, rec->input, rec->length);
188280304Sjkim        rec->input = rec->data;
189280304Sjkim    } else {
190280304Sjkim        l = rec->length;
191280304Sjkim        bs = EVP_CIPHER_block_size(ds->cipher);
192160814Ssimon
193280304Sjkim        if ((bs != 1) && send) {
194280304Sjkim            i = bs - ((int)l % bs);
195160814Ssimon
196280304Sjkim            /* Add weird padding of upto 256 bytes */
197160814Ssimon
198280304Sjkim            /* we need to add 'i' padding bytes of value j */
199280304Sjkim            j = i - 1;
200280304Sjkim            if (s->options & SSL_OP_TLS_BLOCK_PADDING_BUG) {
201280304Sjkim                if (s->s3->flags & TLS1_FLAGS_TLS_PADDING_BUG)
202280304Sjkim                    j++;
203280304Sjkim            }
204280304Sjkim            for (k = (int)l; k < (int)(l + i); k++)
205280304Sjkim                rec->input[k] = j;
206280304Sjkim            l += i;
207280304Sjkim            rec->length += i;
208280304Sjkim        }
209160814Ssimon#ifdef KSSL_DEBUG
210280304Sjkim        {
211280304Sjkim            unsigned long ui;
212280304Sjkim            printf("EVP_Cipher(ds=%p,rec->data=%p,rec->input=%p,l=%ld) ==>\n",
213280304Sjkim                   ds, rec->data, rec->input, l);
214280304Sjkim            printf
215280304Sjkim                ("\tEVP_CIPHER_CTX: %d buf_len, %d key_len [%d %d], %d iv_len\n",
216280304Sjkim                 ds->buf_len, ds->cipher->key_len, DES_KEY_SZ,
217280304Sjkim                 DES_SCHEDULE_SZ, ds->cipher->iv_len);
218280304Sjkim            printf("\t\tIV: ");
219280304Sjkim            for (i = 0; i < ds->cipher->iv_len; i++)
220280304Sjkim                printf("%02X", ds->iv[i]);
221280304Sjkim            printf("\n");
222280304Sjkim            printf("\trec->input=");
223280304Sjkim            for (ui = 0; ui < l; ui++)
224280304Sjkim                printf(" %02x", rec->input[ui]);
225280304Sjkim            printf("\n");
226280304Sjkim        }
227280304Sjkim#endif                          /* KSSL_DEBUG */
228160814Ssimon
229280304Sjkim        if (!send) {
230280304Sjkim            if (l == 0 || l % bs != 0)
231280304Sjkim                return 0;
232280304Sjkim        }
233160814Ssimon
234280304Sjkim        if (EVP_Cipher(ds, rec->data, rec->input, l) < 1)
235280304Sjkim            return -1;
236280304Sjkim
237160814Ssimon#ifdef KSSL_DEBUG
238280304Sjkim        {
239280304Sjkim            unsigned long i;
240280304Sjkim            printf("\trec->data=");
241280304Sjkim            for (i = 0; i < l; i++)
242280304Sjkim                printf(" %02x", rec->data[i]);
243280304Sjkim            printf("\n");
244280304Sjkim        }
245280304Sjkim#endif                          /* KSSL_DEBUG */
246160814Ssimon
247280304Sjkim        if ((bs != 1) && !send)
248280304Sjkim            return tls1_cbc_remove_padding(s, rec, bs, mac_size);
249280304Sjkim    }
250280304Sjkim    return (1);
251280304Sjkim}
252