195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) 295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * All rights reserved. 395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * This package is an SSL implementation written 595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * by Eric Young (eay@cryptsoft.com). 695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * The implementation was written so as to conform with Netscapes SSL. 795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * This library is free for commercial and non-commercial use as long as 995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * the following conditions are aheared to. The following conditions 1095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * apply to all code found in this distribution, be it the RC4, RSA, 1195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * lhash, DES, etc., code; not just the SSL code. The SSL documentation 1295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * included with this distribution is covered by the same copyright terms 1395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * except that the holder is Tim Hudson (tjh@cryptsoft.com). 1495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 1595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * Copyright remains Eric Young's, and as such any Copyright notices in 1695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * the code are not to be removed. 1795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * If this package is used in a product, Eric Young should be given attribution 1895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * as the author of the parts of the library used. 1995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * This can be in the form of a textual message at program startup or 2095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * in documentation (online or textual) provided with the package. 2195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 2295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * Redistribution and use in source and binary forms, with or without 2395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * modification, are permitted provided that the following conditions 2495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * are met: 2595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 1. Redistributions of source code must retain the copyright 2695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * notice, this list of conditions and the following disclaimer. 2795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 2. Redistributions in binary form must reproduce the above copyright 2895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * notice, this list of conditions and the following disclaimer in the 2995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * documentation and/or other materials provided with the distribution. 3095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 3. All advertising materials mentioning features or use of this software 3195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * must display the following acknowledgement: 3295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * "This product includes cryptographic software written by 3395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * Eric Young (eay@cryptsoft.com)" 3495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * The word 'cryptographic' can be left out if the rouines from the library 3595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * being used are not cryptographic related :-). 3695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 4. If you include any Windows specific code (or a derivative thereof) from 3795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * the apps directory (application code) you must include an acknowledgement: 3895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" 3995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 4095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 4195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 4295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 4395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 4495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 4595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 4695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 4795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 4895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 4995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 5095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * SUCH DAMAGE. 5195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * 5295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * The licence and distribution terms for any publically available version or 5395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * derivative of this code cannot be changed. i.e. this code cannot simply be 5495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * copied and put under another distribution licence 5595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley * [including the GNU Public Licence.] */ 5695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 5795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#include <openssl/cipher.h> 5895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#include <openssl/des.h> 5995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#include <openssl/obj.h> 6095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 6195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#include "internal.h" 6295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 6395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 6495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley#define EVP_MAXCHUNK (1<<30) 6595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 6695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleytypedef struct { 6795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley union { 6895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley double align; 6995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley DES_key_schedule ks; 7095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley } ks; 7195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley} EVP_DES_KEY; 7295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 7395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic int des_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, 7495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley const uint8_t *iv, int enc) { 7595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley DES_cblock *deskey = (DES_cblock *)key; 7695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley EVP_DES_KEY *dat = (EVP_DES_KEY *)ctx->cipher_data; 7795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 7895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley DES_set_key(deskey, &dat->ks.ks); 7995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley return 1; 8095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley} 8195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 8295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic int des_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, const uint8_t *in, 8395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley size_t in_len) { 8495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley EVP_DES_KEY *dat = (EVP_DES_KEY *)ctx->cipher_data; 8595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 8695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley while (in_len >= EVP_MAXCHUNK) { 8795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley DES_ncbc_encrypt(in, out, EVP_MAXCHUNK, &dat->ks.ks, (DES_cblock *)ctx->iv, 8895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley ctx->encrypt); 8995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley in_len -= EVP_MAXCHUNK; 9095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley in += EVP_MAXCHUNK; 9195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley out += EVP_MAXCHUNK; 9295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley } 9395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 9495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley if (in_len) { 9595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley DES_ncbc_encrypt(in, out, (long)in_len, &dat->ks.ks, 9695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley (DES_cblock *)ctx->iv, ctx->encrypt); 9795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley } 9895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 9995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley return 1; 10095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley} 10195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 10295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic const EVP_CIPHER des_cbc = { 10395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley NID_des_cbc, 8 /* block_size */, 8 /* key_size */, 10495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 8 /* iv_len */, sizeof(EVP_DES_KEY), EVP_CIPH_CBC_MODE, 10595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley NULL /* app_data */, des_init_key, des_cbc_cipher, 10695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley NULL /* cleanup */, NULL /* ctrl */, }; 10795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 10895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyconst EVP_CIPHER *EVP_des_cbc(void) { return &des_cbc; } 10995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 11095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 11195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleytypedef struct { 11295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley union { 11395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley double align; 11495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley DES_key_schedule ks[3]; 11595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley } ks; 11695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley} DES_EDE_KEY; 11795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 11895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 11995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const uint8_t *key, 12095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley const uint8_t *iv, int enc) { 12195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley DES_cblock *deskey = (DES_cblock *)key; 12295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley DES_EDE_KEY *dat = (DES_EDE_KEY*) ctx->cipher_data; 12395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 12495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley DES_set_key(&deskey[0], &dat->ks.ks[0]); 12595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley DES_set_key(&deskey[1], &dat->ks.ks[1]); 12695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley DES_set_key(&deskey[2], &dat->ks.ks[2]); 12795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 12895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley return 1; 12995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley} 13095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 13195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic int des_ede3_cbc_cipher(EVP_CIPHER_CTX *ctx, uint8_t *out, 13295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley const uint8_t *in, size_t in_len) { 13395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley DES_EDE_KEY *dat = (DES_EDE_KEY*) ctx->cipher_data; 13495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 13595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley while (in_len >= EVP_MAXCHUNK) { 13695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley DES_ede3_cbc_encrypt(in, out, EVP_MAXCHUNK, &dat->ks.ks[0], &dat->ks.ks[1], 13795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley &dat->ks.ks[2], (DES_cblock *)ctx->iv, ctx->encrypt); 13895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley in_len -= EVP_MAXCHUNK; 13995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley in += EVP_MAXCHUNK; 14095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley out += EVP_MAXCHUNK; 14195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley } 14295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 14395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley if (in_len) { 14495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley DES_ede3_cbc_encrypt(in, out, in_len, &dat->ks.ks[0], &dat->ks.ks[1], 14595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley &dat->ks.ks[2], (DES_cblock *)ctx->iv, ctx->encrypt); 14695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley } 14795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 14895c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley return 1; 14995c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley} 15095c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 15195c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleystatic const EVP_CIPHER des3_cbc = { 15295c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley NID_des_cbc, 8 /* block_size */, 24 /* key_size */, 15395c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 8 /* iv_len */, sizeof(DES_EDE_KEY), EVP_CIPH_CBC_MODE, 15495c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley NULL /* app_data */, des_ede3_init_key, des_ede3_cbc_cipher, 15595c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley NULL /* cleanup */, NULL /* ctrl */, }; 15695c29f3cd1f6c08c6c0927868683392eea727ccAdam Langley 15795c29f3cd1f6c08c6c0927868683392eea727ccAdam Langleyconst EVP_CIPHER *EVP_des_ede3_cbc(void) { return &des3_cbc; } 158