10Sstevel@tonic-gate /* crypto/evp/e_des3.c */
20Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
30Sstevel@tonic-gate * All rights reserved.
40Sstevel@tonic-gate *
50Sstevel@tonic-gate * This package is an SSL implementation written
60Sstevel@tonic-gate * by Eric Young (eay@cryptsoft.com).
70Sstevel@tonic-gate * The implementation was written so as to conform with Netscapes SSL.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * This library is free for commercial and non-commercial use as long as
100Sstevel@tonic-gate * the following conditions are aheared to. The following conditions
110Sstevel@tonic-gate * apply to all code found in this distribution, be it the RC4, RSA,
120Sstevel@tonic-gate * lhash, DES, etc., code; not just the SSL code. The SSL documentation
130Sstevel@tonic-gate * included with this distribution is covered by the same copyright terms
140Sstevel@tonic-gate * except that the holder is Tim Hudson (tjh@cryptsoft.com).
150Sstevel@tonic-gate *
160Sstevel@tonic-gate * Copyright remains Eric Young's, and as such any Copyright notices in
170Sstevel@tonic-gate * the code are not to be removed.
180Sstevel@tonic-gate * If this package is used in a product, Eric Young should be given attribution
190Sstevel@tonic-gate * as the author of the parts of the library used.
200Sstevel@tonic-gate * This can be in the form of a textual message at program startup or
210Sstevel@tonic-gate * in documentation (online or textual) provided with the package.
220Sstevel@tonic-gate *
230Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
240Sstevel@tonic-gate * modification, are permitted provided that the following conditions
250Sstevel@tonic-gate * are met:
260Sstevel@tonic-gate * 1. Redistributions of source code must retain the copyright
270Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
280Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
290Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
300Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
310Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
320Sstevel@tonic-gate * must display the following acknowledgement:
330Sstevel@tonic-gate * "This product includes cryptographic software written by
340Sstevel@tonic-gate * Eric Young (eay@cryptsoft.com)"
350Sstevel@tonic-gate * The word 'cryptographic' can be left out if the rouines from the library
360Sstevel@tonic-gate * being used are not cryptographic related :-).
370Sstevel@tonic-gate * 4. If you include any Windows specific code (or a derivative thereof) from
380Sstevel@tonic-gate * the apps directory (application code) you must include an acknowledgement:
390Sstevel@tonic-gate * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
400Sstevel@tonic-gate *
410Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
420Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
430Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
440Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
450Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
460Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
470Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
480Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
490Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
500Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
510Sstevel@tonic-gate * SUCH DAMAGE.
520Sstevel@tonic-gate *
530Sstevel@tonic-gate * The licence and distribution terms for any publically available version or
540Sstevel@tonic-gate * derivative of this code cannot be changed. i.e. this code cannot simply be
550Sstevel@tonic-gate * copied and put under another distribution licence
560Sstevel@tonic-gate * [including the GNU Public Licence.]
570Sstevel@tonic-gate */
580Sstevel@tonic-gate
590Sstevel@tonic-gate #include <stdio.h>
600Sstevel@tonic-gate #include "cryptlib.h"
61*2139Sjp161948 #ifndef OPENSSL_NO_DES
620Sstevel@tonic-gate #include <openssl/evp.h>
630Sstevel@tonic-gate #include <openssl/objects.h>
640Sstevel@tonic-gate #include "evp_locl.h"
650Sstevel@tonic-gate #include <openssl/des.h>
66*2139Sjp161948 #include <openssl/rand.h>
670Sstevel@tonic-gate
680Sstevel@tonic-gate static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
690Sstevel@tonic-gate const unsigned char *iv,int enc);
700Sstevel@tonic-gate
710Sstevel@tonic-gate static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
720Sstevel@tonic-gate const unsigned char *iv,int enc);
730Sstevel@tonic-gate
74*2139Sjp161948 static int des3_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr);
75*2139Sjp161948
760Sstevel@tonic-gate typedef struct
770Sstevel@tonic-gate {
780Sstevel@tonic-gate DES_key_schedule ks1;/* key schedule */
790Sstevel@tonic-gate DES_key_schedule ks2;/* key schedule (for ede) */
800Sstevel@tonic-gate DES_key_schedule ks3;/* key schedule (for ede3) */
810Sstevel@tonic-gate } DES_EDE_KEY;
820Sstevel@tonic-gate
830Sstevel@tonic-gate #define data(ctx) ((DES_EDE_KEY *)(ctx)->cipher_data)
840Sstevel@tonic-gate
850Sstevel@tonic-gate /* Because of various casts and different args can't use IMPLEMENT_BLOCK_CIPHER */
860Sstevel@tonic-gate
des_ede_ecb_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,unsigned int inl)870Sstevel@tonic-gate static int des_ede_ecb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
880Sstevel@tonic-gate const unsigned char *in, unsigned int inl)
890Sstevel@tonic-gate {
900Sstevel@tonic-gate BLOCK_CIPHER_ecb_loop()
91*2139Sjp161948 DES_ecb3_encrypt((const_DES_cblock *)(in + i),
92*2139Sjp161948 (DES_cblock *)(out + i),
930Sstevel@tonic-gate &data(ctx)->ks1, &data(ctx)->ks2,
940Sstevel@tonic-gate &data(ctx)->ks3,
950Sstevel@tonic-gate ctx->encrypt);
960Sstevel@tonic-gate return 1;
970Sstevel@tonic-gate }
980Sstevel@tonic-gate
des_ede_ofb_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,unsigned int inl)990Sstevel@tonic-gate static int des_ede_ofb_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1000Sstevel@tonic-gate const unsigned char *in, unsigned int inl)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate DES_ede3_ofb64_encrypt(in, out, (long)inl,
1030Sstevel@tonic-gate &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3,
1040Sstevel@tonic-gate (DES_cblock *)ctx->iv, &ctx->num);
1050Sstevel@tonic-gate return 1;
1060Sstevel@tonic-gate }
1070Sstevel@tonic-gate
des_ede_cbc_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,unsigned int inl)1080Sstevel@tonic-gate static int des_ede_cbc_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1090Sstevel@tonic-gate const unsigned char *in, unsigned int inl)
1100Sstevel@tonic-gate {
1110Sstevel@tonic-gate #ifdef KSSL_DEBUG
1120Sstevel@tonic-gate {
1130Sstevel@tonic-gate int i;
1140Sstevel@tonic-gate char *cp;
1150Sstevel@tonic-gate printf("des_ede_cbc_cipher(ctx=%lx, buflen=%d)\n", ctx, ctx->buf_len);
1160Sstevel@tonic-gate printf("\t iv= ");
1170Sstevel@tonic-gate for(i=0;i<8;i++)
1180Sstevel@tonic-gate printf("%02X",ctx->iv[i]);
1190Sstevel@tonic-gate printf("\n");
1200Sstevel@tonic-gate }
1210Sstevel@tonic-gate #endif /* KSSL_DEBUG */
1220Sstevel@tonic-gate DES_ede3_cbc_encrypt(in, out, (long)inl,
1230Sstevel@tonic-gate &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3,
1240Sstevel@tonic-gate (DES_cblock *)ctx->iv, ctx->encrypt);
1250Sstevel@tonic-gate return 1;
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate
des_ede_cfb64_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,unsigned int inl)128*2139Sjp161948 static int des_ede_cfb64_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
1290Sstevel@tonic-gate const unsigned char *in, unsigned int inl)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate DES_ede3_cfb64_encrypt(in, out, (long)inl,
1320Sstevel@tonic-gate &data(ctx)->ks1, &data(ctx)->ks2, &data(ctx)->ks3,
1330Sstevel@tonic-gate (DES_cblock *)ctx->iv, &ctx->num, ctx->encrypt);
1340Sstevel@tonic-gate return 1;
1350Sstevel@tonic-gate }
1360Sstevel@tonic-gate
137*2139Sjp161948 /* Although we have a CFB-r implementation for 3-DES, it doesn't pack the right
138*2139Sjp161948 way, so wrap it here */
des_ede3_cfb1_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,unsigned int inl)139*2139Sjp161948 static int des_ede3_cfb1_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
140*2139Sjp161948 const unsigned char *in, unsigned int inl)
141*2139Sjp161948 {
142*2139Sjp161948 unsigned int n;
143*2139Sjp161948 unsigned char c[1],d[1];
144*2139Sjp161948
145*2139Sjp161948 for(n=0 ; n < inl ; ++n)
146*2139Sjp161948 {
147*2139Sjp161948 c[0]=(in[n/8]&(1 << (7-n%8))) ? 0x80 : 0;
148*2139Sjp161948 DES_ede3_cfb_encrypt(c,d,1,1,
149*2139Sjp161948 &data(ctx)->ks1,&data(ctx)->ks2,&data(ctx)->ks3,
150*2139Sjp161948 (DES_cblock *)ctx->iv,ctx->encrypt);
151*2139Sjp161948 out[n/8]=(out[n/8]&~(0x80 >> (n%8)))|((d[0]&0x80) >> (n%8));
152*2139Sjp161948 }
153*2139Sjp161948
154*2139Sjp161948 return 1;
155*2139Sjp161948 }
156*2139Sjp161948
des_ede3_cfb8_cipher(EVP_CIPHER_CTX * ctx,unsigned char * out,const unsigned char * in,unsigned int inl)157*2139Sjp161948 static int des_ede3_cfb8_cipher(EVP_CIPHER_CTX *ctx, unsigned char *out,
158*2139Sjp161948 const unsigned char *in, unsigned int inl)
159*2139Sjp161948 {
160*2139Sjp161948 DES_ede3_cfb_encrypt(in,out,8,inl,
161*2139Sjp161948 &data(ctx)->ks1,&data(ctx)->ks2,&data(ctx)->ks3,
162*2139Sjp161948 (DES_cblock *)ctx->iv,ctx->encrypt);
163*2139Sjp161948 return 1;
164*2139Sjp161948 }
165*2139Sjp161948
1660Sstevel@tonic-gate BLOCK_CIPHER_defs(des_ede, DES_EDE_KEY, NID_des_ede, 8, 16, 8, 64,
167*2139Sjp161948 EVP_CIPH_RAND_KEY, des_ede_init_key, NULL,
1680Sstevel@tonic-gate EVP_CIPHER_set_asn1_iv,
1690Sstevel@tonic-gate EVP_CIPHER_get_asn1_iv,
170*2139Sjp161948 des3_ctrl)
1710Sstevel@tonic-gate
172*2139Sjp161948 #define des_ede3_cfb64_cipher des_ede_cfb64_cipher
1730Sstevel@tonic-gate #define des_ede3_ofb_cipher des_ede_ofb_cipher
1740Sstevel@tonic-gate #define des_ede3_cbc_cipher des_ede_cbc_cipher
1750Sstevel@tonic-gate #define des_ede3_ecb_cipher des_ede_ecb_cipher
1760Sstevel@tonic-gate
1770Sstevel@tonic-gate BLOCK_CIPHER_defs(des_ede3, DES_EDE_KEY, NID_des_ede3, 8, 24, 8, 64,
178*2139Sjp161948 EVP_CIPH_RAND_KEY, des_ede3_init_key, NULL,
1790Sstevel@tonic-gate EVP_CIPHER_set_asn1_iv,
1800Sstevel@tonic-gate EVP_CIPHER_get_asn1_iv,
181*2139Sjp161948 des3_ctrl)
182*2139Sjp161948
183*2139Sjp161948 BLOCK_CIPHER_def_cfb(des_ede3,DES_EDE_KEY,NID_des_ede3,24,8,1,
184*2139Sjp161948 EVP_CIPH_RAND_KEY, des_ede3_init_key,NULL,
185*2139Sjp161948 EVP_CIPHER_set_asn1_iv,
186*2139Sjp161948 EVP_CIPHER_get_asn1_iv,
187*2139Sjp161948 des3_ctrl)
188*2139Sjp161948
189*2139Sjp161948 BLOCK_CIPHER_def_cfb(des_ede3,DES_EDE_KEY,NID_des_ede3,24,8,8,
190*2139Sjp161948 EVP_CIPH_RAND_KEY, des_ede3_init_key,NULL,
191*2139Sjp161948 EVP_CIPHER_set_asn1_iv,
192*2139Sjp161948 EVP_CIPHER_get_asn1_iv,
193*2139Sjp161948 des3_ctrl)
1940Sstevel@tonic-gate
des_ede_init_key(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int enc)1950Sstevel@tonic-gate static int des_ede_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
1960Sstevel@tonic-gate const unsigned char *iv, int enc)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate DES_cblock *deskey = (DES_cblock *)key;
199*2139Sjp161948 #ifdef EVP_CHECK_DES_KEY
200*2139Sjp161948 if (DES_set_key_checked(&deskey[0],&data(ctx)->ks1)
201*2139Sjp161948 !! DES_set_key_checked(&deskey[1],&data(ctx)->ks2))
202*2139Sjp161948 return 0;
203*2139Sjp161948 #else
2040Sstevel@tonic-gate DES_set_key_unchecked(&deskey[0],&data(ctx)->ks1);
2050Sstevel@tonic-gate DES_set_key_unchecked(&deskey[1],&data(ctx)->ks2);
206*2139Sjp161948 #endif
2070Sstevel@tonic-gate memcpy(&data(ctx)->ks3,&data(ctx)->ks1,
2080Sstevel@tonic-gate sizeof(data(ctx)->ks1));
2090Sstevel@tonic-gate return 1;
2100Sstevel@tonic-gate }
2110Sstevel@tonic-gate
des_ede3_init_key(EVP_CIPHER_CTX * ctx,const unsigned char * key,const unsigned char * iv,int enc)2120Sstevel@tonic-gate static int des_ede3_init_key(EVP_CIPHER_CTX *ctx, const unsigned char *key,
2130Sstevel@tonic-gate const unsigned char *iv, int enc)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate DES_cblock *deskey = (DES_cblock *)key;
2160Sstevel@tonic-gate #ifdef KSSL_DEBUG
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate int i;
2190Sstevel@tonic-gate printf("des_ede3_init_key(ctx=%lx)\n", ctx);
2200Sstevel@tonic-gate printf("\tKEY= ");
2210Sstevel@tonic-gate for(i=0;i<24;i++) printf("%02X",key[i]); printf("\n");
2220Sstevel@tonic-gate printf("\t IV= ");
2230Sstevel@tonic-gate for(i=0;i<8;i++) printf("%02X",iv[i]); printf("\n");
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate #endif /* KSSL_DEBUG */
2260Sstevel@tonic-gate
227*2139Sjp161948 #ifdef EVP_CHECK_DES_KEY
228*2139Sjp161948 if (DES_set_key_checked(&deskey[0],&data(ctx)->ks1)
229*2139Sjp161948 || DES_set_key_checked(&deskey[1],&data(ctx)->ks2)
230*2139Sjp161948 || DES_set_key_checked(&deskey[2],&data(ctx)->ks3))
231*2139Sjp161948 return 0;
232*2139Sjp161948 #else
2330Sstevel@tonic-gate DES_set_key_unchecked(&deskey[0],&data(ctx)->ks1);
2340Sstevel@tonic-gate DES_set_key_unchecked(&deskey[1],&data(ctx)->ks2);
2350Sstevel@tonic-gate DES_set_key_unchecked(&deskey[2],&data(ctx)->ks3);
236*2139Sjp161948 #endif
237*2139Sjp161948 return 1;
238*2139Sjp161948 }
2390Sstevel@tonic-gate
des3_ctrl(EVP_CIPHER_CTX * c,int type,int arg,void * ptr)240*2139Sjp161948 static int des3_ctrl(EVP_CIPHER_CTX *c, int type, int arg, void *ptr)
241*2139Sjp161948 {
242*2139Sjp161948
243*2139Sjp161948 DES_cblock *deskey = ptr;
244*2139Sjp161948
245*2139Sjp161948 switch(type)
246*2139Sjp161948 {
247*2139Sjp161948 case EVP_CTRL_RAND_KEY:
248*2139Sjp161948 if (RAND_bytes(ptr, c->key_len) <= 0)
249*2139Sjp161948 return 0;
250*2139Sjp161948 DES_set_odd_parity(deskey);
251*2139Sjp161948 if (c->key_len >= 16)
252*2139Sjp161948 DES_set_odd_parity(deskey + 1);
253*2139Sjp161948 if (c->key_len >= 24)
254*2139Sjp161948 DES_set_odd_parity(deskey + 2);
255*2139Sjp161948 return 1;
256*2139Sjp161948
257*2139Sjp161948 default:
258*2139Sjp161948 return -1;
259*2139Sjp161948 }
2600Sstevel@tonic-gate }
2610Sstevel@tonic-gate
EVP_des_ede(void)2620Sstevel@tonic-gate const EVP_CIPHER *EVP_des_ede(void)
2630Sstevel@tonic-gate {
2640Sstevel@tonic-gate return &des_ede_ecb;
2650Sstevel@tonic-gate }
2660Sstevel@tonic-gate
EVP_des_ede3(void)2670Sstevel@tonic-gate const EVP_CIPHER *EVP_des_ede3(void)
2680Sstevel@tonic-gate {
2690Sstevel@tonic-gate return &des_ede3_ecb;
2700Sstevel@tonic-gate }
2710Sstevel@tonic-gate #endif
272