1160814Ssimon/* ssl/d1_enc.c */ 2296341Sdelphij/* 3160814Ssimon * DTLS implementation written by Nagendra Modadugu 4296341Sdelphij * (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 14296341Sdelphij * 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. 65296341Sdelphij * 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). 72296341Sdelphij * 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. 79296341Sdelphij * 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 :-). 94296341Sdelphij * 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)" 97296341Sdelphij * 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. 109296341Sdelphij * 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 119296341Sdelphij# 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 126296341Sdelphij# include <openssl/des.h> 127194206Ssimon#endif 128160814Ssimon 129296341Sdelphij/*- 130296341Sdelphij * 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, 137296341Sdelphij * an internal error occured. 138296341Sdelphij */ 139160814Ssimonint dtls1_enc(SSL *s, int send) 140296341Sdelphij{ 141296341Sdelphij SSL3_RECORD *rec; 142296341Sdelphij EVP_CIPHER_CTX *ds; 143296341Sdelphij unsigned long l; 144296341Sdelphij int bs, i, j, k, mac_size = 0; 145296341Sdelphij const EVP_CIPHER *enc; 146160814Ssimon 147296341Sdelphij if (send) { 148296341Sdelphij if (EVP_MD_CTX_md(s->write_hash)) { 149296341Sdelphij mac_size = EVP_MD_CTX_size(s->write_hash); 150296341Sdelphij if (mac_size < 0) 151296341Sdelphij return -1; 152296341Sdelphij } 153296341Sdelphij ds = s->enc_write_ctx; 154296341Sdelphij rec = &(s->s3->wrec); 155296341Sdelphij if (s->enc_write_ctx == NULL) 156296341Sdelphij enc = NULL; 157296341Sdelphij else { 158296341Sdelphij enc = EVP_CIPHER_CTX_cipher(s->enc_write_ctx); 159296341Sdelphij if (rec->data != rec->input) 160296341Sdelphij /* we can't write into the input stream */ 161296341Sdelphij fprintf(stderr, "%s:%d: rec->data != rec->input\n", 162296341Sdelphij __FILE__, __LINE__); 163296341Sdelphij else if (EVP_CIPHER_block_size(ds->cipher) > 1) { 164296341Sdelphij if (RAND_bytes(rec->input, EVP_CIPHER_block_size(ds->cipher)) 165296341Sdelphij <= 0) 166296341Sdelphij return -1; 167296341Sdelphij } 168296341Sdelphij } 169296341Sdelphij } else { 170296341Sdelphij if (EVP_MD_CTX_md(s->read_hash)) { 171296341Sdelphij mac_size = EVP_MD_CTX_size(s->read_hash); 172296341Sdelphij OPENSSL_assert(mac_size >= 0); 173296341Sdelphij } 174296341Sdelphij ds = s->enc_read_ctx; 175296341Sdelphij rec = &(s->s3->rrec); 176296341Sdelphij if (s->enc_read_ctx == NULL) 177296341Sdelphij enc = NULL; 178296341Sdelphij else 179296341Sdelphij enc = EVP_CIPHER_CTX_cipher(s->enc_read_ctx); 180296341Sdelphij } 181160814Ssimon 182160814Ssimon#ifdef KSSL_DEBUG 183296341Sdelphij printf("dtls1_enc(%d)\n", send); 184296341Sdelphij#endif /* KSSL_DEBUG */ 185160814Ssimon 186296341Sdelphij if ((s->session == NULL) || (ds == NULL) || (enc == NULL)) { 187296341Sdelphij memmove(rec->data, rec->input, rec->length); 188296341Sdelphij rec->input = rec->data; 189296341Sdelphij } else { 190296341Sdelphij l = rec->length; 191296341Sdelphij bs = EVP_CIPHER_block_size(ds->cipher); 192160814Ssimon 193296341Sdelphij if ((bs != 1) && send) { 194296341Sdelphij i = bs - ((int)l % bs); 195160814Ssimon 196296341Sdelphij /* Add weird padding of upto 256 bytes */ 197160814Ssimon 198296341Sdelphij /* we need to add 'i' padding bytes of value j */ 199296341Sdelphij j = i - 1; 200296341Sdelphij if (s->options & SSL_OP_TLS_BLOCK_PADDING_BUG) { 201296341Sdelphij if (s->s3->flags & TLS1_FLAGS_TLS_PADDING_BUG) 202296341Sdelphij j++; 203296341Sdelphij } 204296341Sdelphij for (k = (int)l; k < (int)(l + i); k++) 205296341Sdelphij rec->input[k] = j; 206296341Sdelphij l += i; 207296341Sdelphij rec->length += i; 208296341Sdelphij } 209160814Ssimon#ifdef KSSL_DEBUG 210296341Sdelphij { 211296341Sdelphij unsigned long ui; 212296341Sdelphij printf("EVP_Cipher(ds=%p,rec->data=%p,rec->input=%p,l=%ld) ==>\n", 213296341Sdelphij ds, rec->data, rec->input, l); 214296341Sdelphij printf 215296341Sdelphij ("\tEVP_CIPHER_CTX: %d buf_len, %d key_len [%d %d], %d iv_len\n", 216296341Sdelphij ds->buf_len, ds->cipher->key_len, DES_KEY_SZ, 217296341Sdelphij DES_SCHEDULE_SZ, ds->cipher->iv_len); 218296341Sdelphij printf("\t\tIV: "); 219296341Sdelphij for (i = 0; i < ds->cipher->iv_len; i++) 220296341Sdelphij printf("%02X", ds->iv[i]); 221296341Sdelphij printf("\n"); 222296341Sdelphij printf("\trec->input="); 223296341Sdelphij for (ui = 0; ui < l; ui++) 224296341Sdelphij printf(" %02x", rec->input[ui]); 225296341Sdelphij printf("\n"); 226296341Sdelphij } 227296341Sdelphij#endif /* KSSL_DEBUG */ 228160814Ssimon 229296341Sdelphij if (!send) { 230296341Sdelphij if (l == 0 || l % bs != 0) 231296341Sdelphij return 0; 232296341Sdelphij } 233160814Ssimon 234296341Sdelphij if (EVP_Cipher(ds, rec->data, rec->input, l) < 1) 235296341Sdelphij return -1; 236296341Sdelphij 237160814Ssimon#ifdef KSSL_DEBUG 238296341Sdelphij { 239296341Sdelphij unsigned long i; 240296341Sdelphij printf("\trec->data="); 241296341Sdelphij for (i = 0; i < l; i++) 242296341Sdelphij printf(" %02x", rec->data[i]); 243296341Sdelphij printf("\n"); 244296341Sdelphij } 245296341Sdelphij#endif /* KSSL_DEBUG */ 246160814Ssimon 247296341Sdelphij if ((bs != 1) && !send) 248296341Sdelphij return tls1_cbc_remove_padding(s, rec, bs, mac_size); 249296341Sdelphij } 250296341Sdelphij return (1); 251296341Sdelphij} 252