xref: /freebsd/crypto/openssl/crypto/evp/e_xcbc_d.c (revision 6f9291cea8b06d251243fd47a7234018541832a3)
174664626SKris Kennaway /* crypto/evp/e_xcbc_d.c */
274664626SKris Kennaway /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
374664626SKris Kennaway  * All rights reserved.
474664626SKris Kennaway  *
574664626SKris Kennaway  * This package is an SSL implementation written
674664626SKris Kennaway  * by Eric Young (eay@cryptsoft.com).
774664626SKris Kennaway  * The implementation was written so as to conform with Netscapes SSL.
874664626SKris Kennaway  *
974664626SKris Kennaway  * This library is free for commercial and non-commercial use as long as
1074664626SKris Kennaway  * the following conditions are aheared to.  The following conditions
1174664626SKris Kennaway  * apply to all code found in this distribution, be it the RC4, RSA,
1274664626SKris Kennaway  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1374664626SKris Kennaway  * included with this distribution is covered by the same copyright terms
1474664626SKris Kennaway  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1574664626SKris Kennaway  *
1674664626SKris Kennaway  * Copyright remains Eric Young's, and as such any Copyright notices in
1774664626SKris Kennaway  * the code are not to be removed.
1874664626SKris Kennaway  * If this package is used in a product, Eric Young should be given attribution
1974664626SKris Kennaway  * as the author of the parts of the library used.
2074664626SKris Kennaway  * This can be in the form of a textual message at program startup or
2174664626SKris Kennaway  * in documentation (online or textual) provided with the package.
2274664626SKris Kennaway  *
2374664626SKris Kennaway  * Redistribution and use in source and binary forms, with or without
2474664626SKris Kennaway  * modification, are permitted provided that the following conditions
2574664626SKris Kennaway  * are met:
2674664626SKris Kennaway  * 1. Redistributions of source code must retain the copyright
2774664626SKris Kennaway  *    notice, this list of conditions and the following disclaimer.
2874664626SKris Kennaway  * 2. Redistributions in binary form must reproduce the above copyright
2974664626SKris Kennaway  *    notice, this list of conditions and the following disclaimer in the
3074664626SKris Kennaway  *    documentation and/or other materials provided with the distribution.
3174664626SKris Kennaway  * 3. All advertising materials mentioning features or use of this software
3274664626SKris Kennaway  *    must display the following acknowledgement:
3374664626SKris Kennaway  *    "This product includes cryptographic software written by
3474664626SKris Kennaway  *     Eric Young (eay@cryptsoft.com)"
3574664626SKris Kennaway  *    The word 'cryptographic' can be left out if the rouines from the library
3674664626SKris Kennaway  *    being used are not cryptographic related :-).
3774664626SKris Kennaway  * 4. If you include any Windows specific code (or a derivative thereof) from
3874664626SKris Kennaway  *    the apps directory (application code) you must include an acknowledgement:
3974664626SKris Kennaway  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4074664626SKris Kennaway  *
4174664626SKris Kennaway  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4274664626SKris Kennaway  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4374664626SKris Kennaway  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4474664626SKris Kennaway  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4574664626SKris Kennaway  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4674664626SKris Kennaway  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4774664626SKris Kennaway  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4874664626SKris Kennaway  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4974664626SKris Kennaway  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5074664626SKris Kennaway  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5174664626SKris Kennaway  * SUCH DAMAGE.
5274664626SKris Kennaway  *
5374664626SKris Kennaway  * The licence and distribution terms for any publically available version or
5474664626SKris Kennaway  * derivative of this code cannot be changed.  i.e. this code cannot simply be
5574664626SKris Kennaway  * copied and put under another distribution licence
5674664626SKris Kennaway  * [including the GNU Public Licence.]
5774664626SKris Kennaway  */
5874664626SKris Kennaway 
5974664626SKris Kennaway #include <stdio.h>
6074664626SKris Kennaway #include "cryptlib.h"
613b4e3dcbSSimon L. B. Nielsen 
623b4e3dcbSSimon L. B. Nielsen #ifndef OPENSSL_NO_DES
633b4e3dcbSSimon L. B. Nielsen 
6474664626SKris Kennaway # include <openssl/evp.h>
6574664626SKris Kennaway # include <openssl/objects.h>
661f13597dSJung-uk Kim # include "evp_locl.h"
675c87c606SMark Murray # include <openssl/des.h>
6874664626SKris Kennaway 
69ddd58736SKris Kennaway static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
70ddd58736SKris Kennaway                              const unsigned char *iv, int enc);
71ddd58736SKris Kennaway static int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
721f13597dSJung-uk Kim                            const unsigned char *in, size_t inl);
735c87c606SMark Murray 
74*6f9291ceSJung-uk Kim typedef struct {
755c87c606SMark Murray     DES_key_schedule ks;        /* key schedule */
765c87c606SMark Murray     DES_cblock inw;
775c87c606SMark Murray     DES_cblock outw;
785c87c606SMark Murray } DESX_CBC_KEY;
795c87c606SMark Murray 
805c87c606SMark Murray # define data(ctx) ((DESX_CBC_KEY *)(ctx)->cipher_data)
815c87c606SMark Murray 
82*6f9291ceSJung-uk Kim static const EVP_CIPHER d_xcbc_cipher = {
8374664626SKris Kennaway     NID_desx_cbc,
8474664626SKris Kennaway     8, 24, 8,
85ddd58736SKris Kennaway     EVP_CIPH_CBC_MODE,
8674664626SKris Kennaway     desx_cbc_init_key,
8774664626SKris Kennaway     desx_cbc_cipher,
8874664626SKris Kennaway     NULL,
895c87c606SMark Murray     sizeof(DESX_CBC_KEY),
9074664626SKris Kennaway     EVP_CIPHER_set_asn1_iv,
9174664626SKris Kennaway     EVP_CIPHER_get_asn1_iv,
923b4e3dcbSSimon L. B. Nielsen     NULL,
93ddd58736SKris Kennaway     NULL
9474664626SKris Kennaway };
9574664626SKris Kennaway 
965c87c606SMark Murray const EVP_CIPHER *EVP_desx_cbc(void)
9774664626SKris Kennaway {
9874664626SKris Kennaway     return (&d_xcbc_cipher);
9974664626SKris Kennaway }
10074664626SKris Kennaway 
101ddd58736SKris Kennaway static int desx_cbc_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
102ddd58736SKris Kennaway                              const unsigned char *iv, int enc)
10374664626SKris Kennaway {
1045c87c606SMark Murray     DES_cblock *deskey = (DES_cblock *)key;
10574664626SKris Kennaway 
1065c87c606SMark Murray     DES_set_key_unchecked(deskey, &data(ctx)->ks);
1075c87c606SMark Murray     memcpy(&data(ctx)->inw[0], &key[8], 8);
1085c87c606SMark Murray     memcpy(&data(ctx)->outw[0], &key[16], 8);
109ddd58736SKris Kennaway 
110ddd58736SKris Kennaway     return 1;
11174664626SKris Kennaway }
11274664626SKris Kennaway 
113ddd58736SKris Kennaway static int desx_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1141f13597dSJung-uk Kim                            const unsigned char *in, size_t inl)
11574664626SKris Kennaway {
116*6f9291ceSJung-uk Kim     while (inl >= EVP_MAXCHUNK) {
1171f13597dSJung-uk Kim         DES_xcbc_encrypt(in, out, (long)EVP_MAXCHUNK, &data(ctx)->ks,
1181f13597dSJung-uk Kim                          (DES_cblock *)&(ctx->iv[0]),
119*6f9291ceSJung-uk Kim                          &data(ctx)->inw, &data(ctx)->outw, ctx->encrypt);
1201f13597dSJung-uk Kim         inl -= EVP_MAXCHUNK;
1211f13597dSJung-uk Kim         in += EVP_MAXCHUNK;
1221f13597dSJung-uk Kim         out += EVP_MAXCHUNK;
1231f13597dSJung-uk Kim     }
1241f13597dSJung-uk Kim     if (inl)
1251f13597dSJung-uk Kim         DES_xcbc_encrypt(in, out, (long)inl, &data(ctx)->ks,
1265c87c606SMark Murray                          (DES_cblock *)&(ctx->iv[0]),
127*6f9291ceSJung-uk Kim                          &data(ctx)->inw, &data(ctx)->outw, ctx->encrypt);
128ddd58736SKris Kennaway     return 1;
12974664626SKris Kennaway }
13074664626SKris Kennaway #endif
131