1ebfedea0SLionel Sambuc /* ssl/d1_enc.c */
2ebfedea0SLionel Sambuc /*
3ebfedea0SLionel Sambuc * DTLS implementation written by Nagendra Modadugu
4ebfedea0SLionel Sambuc * (nagendra@cs.stanford.edu) for the OpenSSL project 2005.
5ebfedea0SLionel Sambuc */
6ebfedea0SLionel Sambuc /* ====================================================================
7ebfedea0SLionel Sambuc * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
8ebfedea0SLionel Sambuc *
9ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
10ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
11ebfedea0SLionel Sambuc * are met:
12ebfedea0SLionel Sambuc *
13ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
15ebfedea0SLionel Sambuc *
16ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
17ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in
18ebfedea0SLionel Sambuc * the documentation and/or other materials provided with the
19ebfedea0SLionel Sambuc * distribution.
20ebfedea0SLionel Sambuc *
21ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this
22ebfedea0SLionel Sambuc * software must display the following acknowledgment:
23ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
24ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
25ebfedea0SLionel Sambuc *
26ebfedea0SLionel Sambuc * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27ebfedea0SLionel Sambuc * endorse or promote products derived from this software without
28ebfedea0SLionel Sambuc * prior written permission. For written permission, please contact
29ebfedea0SLionel Sambuc * openssl-core@openssl.org.
30ebfedea0SLionel Sambuc *
31ebfedea0SLionel Sambuc * 5. Products derived from this software may not be called "OpenSSL"
32ebfedea0SLionel Sambuc * nor may "OpenSSL" appear in their names without prior written
33ebfedea0SLionel Sambuc * permission of the OpenSSL Project.
34ebfedea0SLionel Sambuc *
35ebfedea0SLionel Sambuc * 6. Redistributions of any form whatsoever must retain the following
36ebfedea0SLionel Sambuc * acknowledgment:
37ebfedea0SLionel Sambuc * "This product includes software developed by the OpenSSL Project
38ebfedea0SLionel Sambuc * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
39ebfedea0SLionel Sambuc *
40ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41ebfedea0SLionel Sambuc * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43ebfedea0SLionel Sambuc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44ebfedea0SLionel Sambuc * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45ebfedea0SLionel Sambuc * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46ebfedea0SLionel Sambuc * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47ebfedea0SLionel Sambuc * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49ebfedea0SLionel Sambuc * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50ebfedea0SLionel Sambuc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51ebfedea0SLionel Sambuc * OF THE POSSIBILITY OF SUCH DAMAGE.
52ebfedea0SLionel Sambuc * ====================================================================
53ebfedea0SLionel Sambuc *
54ebfedea0SLionel Sambuc * This product includes cryptographic software written by Eric Young
55ebfedea0SLionel Sambuc * (eay@cryptsoft.com). This product includes software written by Tim
56ebfedea0SLionel Sambuc * Hudson (tjh@cryptsoft.com).
57ebfedea0SLionel Sambuc *
58ebfedea0SLionel Sambuc */
59ebfedea0SLionel Sambuc /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
60ebfedea0SLionel Sambuc * All rights reserved.
61ebfedea0SLionel Sambuc *
62ebfedea0SLionel Sambuc * This package is an SSL implementation written
63ebfedea0SLionel Sambuc * by Eric Young (eay@cryptsoft.com).
64ebfedea0SLionel Sambuc * The implementation was written so as to conform with Netscapes SSL.
65ebfedea0SLionel Sambuc *
66ebfedea0SLionel Sambuc * This library is free for commercial and non-commercial use as long as
67ebfedea0SLionel Sambuc * the following conditions are aheared to. The following conditions
68ebfedea0SLionel Sambuc * apply to all code found in this distribution, be it the RC4, RSA,
69ebfedea0SLionel Sambuc * lhash, DES, etc., code; not just the SSL code. The SSL documentation
70ebfedea0SLionel Sambuc * included with this distribution is covered by the same copyright terms
71ebfedea0SLionel Sambuc * except that the holder is Tim Hudson (tjh@cryptsoft.com).
72ebfedea0SLionel Sambuc *
73ebfedea0SLionel Sambuc * Copyright remains Eric Young's, and as such any Copyright notices in
74ebfedea0SLionel Sambuc * the code are not to be removed.
75ebfedea0SLionel Sambuc * If this package is used in a product, Eric Young should be given attribution
76ebfedea0SLionel Sambuc * as the author of the parts of the library used.
77ebfedea0SLionel Sambuc * This can be in the form of a textual message at program startup or
78ebfedea0SLionel Sambuc * in documentation (online or textual) provided with the package.
79ebfedea0SLionel Sambuc *
80ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without
81ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions
82ebfedea0SLionel Sambuc * are met:
83ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the copyright
84ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer.
85ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
86ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the
87ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution.
88ebfedea0SLionel Sambuc * 3. All advertising materials mentioning features or use of this software
89ebfedea0SLionel Sambuc * must display the following acknowledgement:
90ebfedea0SLionel Sambuc * "This product includes cryptographic software written by
91ebfedea0SLionel Sambuc * Eric Young (eay@cryptsoft.com)"
92ebfedea0SLionel Sambuc * The word 'cryptographic' can be left out if the rouines from the library
93ebfedea0SLionel Sambuc * being used are not cryptographic related :-).
94ebfedea0SLionel Sambuc * 4. If you include any Windows specific code (or a derivative thereof) from
95ebfedea0SLionel Sambuc * the apps directory (application code) you must include an acknowledgement:
96ebfedea0SLionel Sambuc * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
97ebfedea0SLionel Sambuc *
98ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
99ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
100ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
101ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
102ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
103ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
104ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
105ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
106ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
107ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
108ebfedea0SLionel Sambuc * SUCH DAMAGE.
109ebfedea0SLionel Sambuc *
110ebfedea0SLionel Sambuc * The licence and distribution terms for any publically available version or
111ebfedea0SLionel Sambuc * derivative of this code cannot be changed. i.e. this code cannot simply be
112ebfedea0SLionel Sambuc * copied and put under another distribution licence
113ebfedea0SLionel Sambuc * [including the GNU Public Licence.]
114ebfedea0SLionel Sambuc */
115ebfedea0SLionel Sambuc
116ebfedea0SLionel Sambuc #include <stdio.h>
117ebfedea0SLionel Sambuc #include "ssl_locl.h"
118ebfedea0SLionel Sambuc #ifndef OPENSSL_NO_COMP
119ebfedea0SLionel Sambuc # include <openssl/comp.h>
120ebfedea0SLionel Sambuc #endif
121ebfedea0SLionel Sambuc #include <openssl/evp.h>
122ebfedea0SLionel Sambuc #include <openssl/hmac.h>
123ebfedea0SLionel Sambuc #include <openssl/md5.h>
124ebfedea0SLionel Sambuc #include <openssl/rand.h>
125ebfedea0SLionel Sambuc #ifdef KSSL_DEBUG
126ebfedea0SLionel Sambuc # include <openssl/des.h>
127ebfedea0SLionel Sambuc #endif
128ebfedea0SLionel Sambuc
129*0a6a1f1dSLionel Sambuc /*-
130*0a6a1f1dSLionel Sambuc * dtls1_enc encrypts/decrypts the record in |s->wrec| / |s->rrec|, respectively.
131ebfedea0SLionel Sambuc *
132ebfedea0SLionel Sambuc * Returns:
133ebfedea0SLionel Sambuc * 0: (in non-constant time) if the record is publically invalid (i.e. too
134ebfedea0SLionel Sambuc * short etc).
135ebfedea0SLionel Sambuc * 1: if the record's padding is valid / the encryption was successful.
136ebfedea0SLionel Sambuc * -1: if the record's padding/AEAD-authenticator is invalid or, if sending,
137*0a6a1f1dSLionel Sambuc * an internal error occured.
138*0a6a1f1dSLionel Sambuc */
dtls1_enc(SSL * s,int send)139ebfedea0SLionel Sambuc int dtls1_enc(SSL *s, int send)
140ebfedea0SLionel Sambuc {
141ebfedea0SLionel Sambuc SSL3_RECORD *rec;
142ebfedea0SLionel Sambuc EVP_CIPHER_CTX *ds;
143ebfedea0SLionel Sambuc unsigned long l;
144ebfedea0SLionel Sambuc int bs, i, j, k, mac_size = 0;
145ebfedea0SLionel Sambuc const EVP_CIPHER *enc;
146ebfedea0SLionel Sambuc
147*0a6a1f1dSLionel Sambuc if (send) {
148*0a6a1f1dSLionel Sambuc if (EVP_MD_CTX_md(s->write_hash)) {
149ebfedea0SLionel Sambuc mac_size = EVP_MD_CTX_size(s->write_hash);
150ebfedea0SLionel Sambuc if (mac_size < 0)
151ebfedea0SLionel Sambuc return -1;
152ebfedea0SLionel Sambuc }
153ebfedea0SLionel Sambuc ds = s->enc_write_ctx;
154ebfedea0SLionel Sambuc rec = &(s->s3->wrec);
155ebfedea0SLionel Sambuc if (s->enc_write_ctx == NULL)
156ebfedea0SLionel Sambuc enc = NULL;
157*0a6a1f1dSLionel Sambuc else {
158ebfedea0SLionel Sambuc enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx);
159ebfedea0SLionel Sambuc if (rec->data != rec->input)
160ebfedea0SLionel Sambuc /* we can't write into the input stream */
161ebfedea0SLionel Sambuc fprintf(stderr, "%s:%d: rec->data != rec->input\n",
162ebfedea0SLionel Sambuc __FILE__, __LINE__);
163*0a6a1f1dSLionel Sambuc else if (EVP_CIPHER_block_size(ds->cipher) > 1) {
164*0a6a1f1dSLionel Sambuc if (RAND_bytes(rec->input, EVP_CIPHER_block_size(ds->cipher))
165*0a6a1f1dSLionel Sambuc <= 0)
166ebfedea0SLionel Sambuc return -1;
167ebfedea0SLionel Sambuc }
168ebfedea0SLionel Sambuc }
169*0a6a1f1dSLionel Sambuc } else {
170*0a6a1f1dSLionel Sambuc if (EVP_MD_CTX_md(s->read_hash)) {
171ebfedea0SLionel Sambuc mac_size = EVP_MD_CTX_size(s->read_hash);
172ebfedea0SLionel Sambuc OPENSSL_assert(mac_size >= 0);
173ebfedea0SLionel Sambuc }
174ebfedea0SLionel Sambuc ds = s->enc_read_ctx;
175ebfedea0SLionel Sambuc rec = &(s->s3->rrec);
176ebfedea0SLionel Sambuc if (s->enc_read_ctx == NULL)
177ebfedea0SLionel Sambuc enc = NULL;
178ebfedea0SLionel Sambuc else
179ebfedea0SLionel Sambuc enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx);
180ebfedea0SLionel Sambuc }
181ebfedea0SLionel Sambuc
182ebfedea0SLionel Sambuc #ifdef KSSL_DEBUG
183ebfedea0SLionel Sambuc printf("dtls1_enc(%d)\n", send);
184ebfedea0SLionel Sambuc #endif /* KSSL_DEBUG */
185ebfedea0SLionel Sambuc
186*0a6a1f1dSLionel Sambuc if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) {
187ebfedea0SLionel Sambuc memmove(rec->data, rec->input, rec->length);
188ebfedea0SLionel Sambuc rec->input = rec->data;
189*0a6a1f1dSLionel Sambuc } else {
190ebfedea0SLionel Sambuc l = rec->length;
191ebfedea0SLionel Sambuc bs = EVP_CIPHER_block_size(ds->cipher);
192ebfedea0SLionel Sambuc
193*0a6a1f1dSLionel Sambuc if ((bs != 1) && send) {
194ebfedea0SLionel Sambuc i = bs - ((int)l % bs);
195ebfedea0SLionel Sambuc
196ebfedea0SLionel Sambuc /* Add weird padding of upto 256 bytes */
197ebfedea0SLionel Sambuc
198ebfedea0SLionel Sambuc /* we need to add 'i' padding bytes of value j */
199ebfedea0SLionel Sambuc j = i - 1;
200*0a6a1f1dSLionel Sambuc if (s->options & SSL_OP_TLS_BLOCK_PADDING_BUG) {
201ebfedea0SLionel Sambuc if (s->s3->flags & TLS1_FLAGS_TLS_PADDING_BUG)
202ebfedea0SLionel Sambuc j++;
203ebfedea0SLionel Sambuc }
204ebfedea0SLionel Sambuc for (k = (int)l; k < (int)(l + i); k++)
205ebfedea0SLionel Sambuc rec->input[k] = j;
206ebfedea0SLionel Sambuc l += i;
207ebfedea0SLionel Sambuc rec->length += i;
208ebfedea0SLionel Sambuc }
209ebfedea0SLionel Sambuc #ifdef KSSL_DEBUG
210ebfedea0SLionel Sambuc {
211ebfedea0SLionel Sambuc unsigned long ui;
212ebfedea0SLionel Sambuc printf("EVP_Cipher(ds=%p,rec->data=%p,rec->input=%p,l=%ld) ==>\n",
213ebfedea0SLionel Sambuc ds, rec->data, rec->input, l);
214*0a6a1f1dSLionel Sambuc printf
215*0a6a1f1dSLionel Sambuc ("\tEVP_CIPHER_CTX: %d buf_len, %d key_len [%d %d], %d iv_len\n",
216*0a6a1f1dSLionel Sambuc ds->buf_len, ds->cipher->key_len, DES_KEY_SZ,
217*0a6a1f1dSLionel Sambuc DES_SCHEDULE_SZ, ds->cipher->iv_len);
218ebfedea0SLionel Sambuc printf("\t\tIV: ");
219*0a6a1f1dSLionel Sambuc for (i = 0; i < ds->cipher->iv_len; i++)
220*0a6a1f1dSLionel Sambuc printf("%02X", ds->iv[i]);
221ebfedea0SLionel Sambuc printf("\n");
222ebfedea0SLionel Sambuc printf("\trec->input=");
223*0a6a1f1dSLionel Sambuc for (ui = 0; ui < l; ui++)
224*0a6a1f1dSLionel Sambuc printf(" %02x", rec->input[ui]);
225ebfedea0SLionel Sambuc printf("\n");
226ebfedea0SLionel Sambuc }
227ebfedea0SLionel Sambuc #endif /* KSSL_DEBUG */
228ebfedea0SLionel Sambuc
229*0a6a1f1dSLionel Sambuc if (!send) {
230ebfedea0SLionel Sambuc if (l == 0 || l % bs != 0)
231ebfedea0SLionel Sambuc return 0;
232ebfedea0SLionel Sambuc }
233ebfedea0SLionel Sambuc
234*0a6a1f1dSLionel Sambuc if (EVP_Cipher(ds, rec->data, rec->input, l) < 1)
235*0a6a1f1dSLionel Sambuc return -1;
236ebfedea0SLionel Sambuc
237ebfedea0SLionel Sambuc #ifdef KSSL_DEBUG
238ebfedea0SLionel Sambuc {
239ebfedea0SLionel Sambuc unsigned long i;
240ebfedea0SLionel Sambuc printf("\trec->data=");
241ebfedea0SLionel Sambuc for (i = 0; i < l; i++)
242*0a6a1f1dSLionel Sambuc printf(" %02x", rec->data[i]);
243*0a6a1f1dSLionel Sambuc printf("\n");
244ebfedea0SLionel Sambuc }
245ebfedea0SLionel Sambuc #endif /* KSSL_DEBUG */
246ebfedea0SLionel Sambuc
247ebfedea0SLionel Sambuc if ((bs != 1) && !send)
248ebfedea0SLionel Sambuc return tls1_cbc_remove_padding(s, rec, bs, mac_size);
249ebfedea0SLionel Sambuc }
250ebfedea0SLionel Sambuc return (1);
251ebfedea0SLionel Sambuc }
252