des.h revision 110007
155714Skris/* crypto/des/des.h */
255714Skris/* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
355714Skris * All rights reserved.
455714Skris *
555714Skris * This package is an SSL implementation written
655714Skris * by Eric Young (eay@cryptsoft.com).
755714Skris * The implementation was written so as to conform with Netscapes SSL.
855714Skris *
955714Skris * This library is free for commercial and non-commercial use as long as
1055714Skris * the following conditions are aheared to.  The following conditions
1155714Skris * apply to all code found in this distribution, be it the RC4, RSA,
1255714Skris * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
1355714Skris * included with this distribution is covered by the same copyright terms
1455714Skris * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1555714Skris *
1655714Skris * Copyright remains Eric Young's, and as such any Copyright notices in
1755714Skris * the code are not to be removed.
1855714Skris * If this package is used in a product, Eric Young should be given attribution
1955714Skris * as the author of the parts of the library used.
2055714Skris * This can be in the form of a textual message at program startup or
2155714Skris * in documentation (online or textual) provided with the package.
2255714Skris *
2355714Skris * Redistribution and use in source and binary forms, with or without
2455714Skris * modification, are permitted provided that the following conditions
2555714Skris * are met:
2655714Skris * 1. Redistributions of source code must retain the copyright
2755714Skris *    notice, this list of conditions and the following disclaimer.
2855714Skris * 2. Redistributions in binary form must reproduce the above copyright
2955714Skris *    notice, this list of conditions and the following disclaimer in the
3055714Skris *    documentation and/or other materials provided with the distribution.
3155714Skris * 3. All advertising materials mentioning features or use of this software
3255714Skris *    must display the following acknowledgement:
3355714Skris *    "This product includes cryptographic software written by
3455714Skris *     Eric Young (eay@cryptsoft.com)"
3555714Skris *    The word 'cryptographic' can be left out if the rouines from the library
3655714Skris *    being used are not cryptographic related :-).
3755714Skris * 4. If you include any Windows specific code (or a derivative thereof) from
3855714Skris *    the apps directory (application code) you must include an acknowledgement:
3955714Skris *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
4055714Skris *
4155714Skris * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
4255714Skris * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
4355714Skris * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
4455714Skris * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
4555714Skris * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
4655714Skris * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4755714Skris * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4855714Skris * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4955714Skris * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
5055714Skris * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
5155714Skris * SUCH DAMAGE.
5255714Skris *
5355714Skris * The licence and distribution terms for any publically available version or
5455714Skris * derivative of this code cannot be changed.  i.e. this code cannot simply be
5555714Skris * copied and put under another distribution licence
5655714Skris * [including the GNU Public Licence.]
5755714Skris */
5855714Skris
59110007Smarkm#ifndef HEADER_NEW_DES_H
60110007Smarkm#define HEADER_NEW_DES_H
6155714Skris
62110007Smarkm#ifdef OPENSSL_NO_DES
6355714Skris#error DES is disabled.
6455714Skris#endif
6555714Skris
6655714Skris#include <openssl/opensslconf.h> /* DES_LONG */
6755714Skris#include <openssl/e_os2.h>	/* OPENSSL_EXTERN */
6855714Skris
69110007Smarkm#ifdef OPENSSL_BUILD_SHLIBCRYPTO
70110007Smarkm# undef OPENSSL_EXTERN
71110007Smarkm# define OPENSSL_EXTERN OPENSSL_EXPORT
72110007Smarkm#endif
73110007Smarkm
7468654Skris#ifdef  __cplusplus
7568654Skrisextern "C" {
7668654Skris#endif
7768654Skris
78110007Smarkmtypedef unsigned char DES_cblock[8];
79110007Smarkmtypedef /* const */ unsigned char const_DES_cblock[8];
80110007Smarkm/* With "const", gcc 2.8.1 on Solaris thinks that DES_cblock *
81110007Smarkm * and const_DES_cblock * are incompatible pointer types. */
8255714Skris
83110007Smarkmtypedef struct DES_ks
84110007Smarkm    {
85110007Smarkm    union
8655714Skris	{
87110007Smarkm	DES_cblock cblock;
88110007Smarkm	/* make sure things are correct size on machines with
89110007Smarkm	 * 8 byte longs */
90110007Smarkm	DES_LONG deslong[2];
91110007Smarkm	} ks[16];
92110007Smarkm    } DES_key_schedule;
9355714Skris
94110007Smarkm#ifndef OPENSSL_DISABLE_OLD_DES_SUPPORT
95110007Smarkm# ifndef OPENSSL_ENABLE_OLD_DES_SUPPORT
96110007Smarkm#  define OPENSSL_ENABLE_OLD_DES_SUPPORT
97110007Smarkm# endif
98110007Smarkm#endif
9955714Skris
100110007Smarkm#ifdef OPENSSL_ENABLE_OLD_DES_SUPPORT
101110007Smarkm# include <openssl/des_old.h>
102110007Smarkm#endif
103110007Smarkm
104110007Smarkm#define DES_KEY_SZ 	(sizeof(DES_cblock))
105110007Smarkm#define DES_SCHEDULE_SZ (sizeof(DES_key_schedule))
106110007Smarkm
10755714Skris#define DES_ENCRYPT	1
10855714Skris#define DES_DECRYPT	0
10955714Skris
11055714Skris#define DES_CBC_MODE	0
11155714Skris#define DES_PCBC_MODE	1
11255714Skris
113110007Smarkm#define DES_ecb2_encrypt(i,o,k1,k2,e) \
114110007Smarkm	DES_ecb3_encrypt((i),(o),(k1),(k2),(k1),(e))
11555714Skris
116110007Smarkm#define DES_ede2_cbc_encrypt(i,o,l,k1,k2,iv,e) \
117110007Smarkm	DES_ede3_cbc_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(e))
11855714Skris
119110007Smarkm#define DES_ede2_cfb64_encrypt(i,o,l,k1,k2,iv,n,e) \
120110007Smarkm	DES_ede3_cfb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n),(e))
12155714Skris
122110007Smarkm#define DES_ede2_ofb64_encrypt(i,o,l,k1,k2,iv,n) \
123110007Smarkm	DES_ede3_ofb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n))
12455714Skris
125110007SmarkmOPENSSL_DECLARE_GLOBAL(int,DES_check_key);	/* defaults to false */
126110007Smarkm#define DES_check_key OPENSSL_GLOBAL_REF(DES_check_key)
127110007SmarkmOPENSSL_DECLARE_GLOBAL(int,DES_rw_mode);	/* defaults to DES_PCBC_MODE */
128110007Smarkm#define DES_rw_mode OPENSSL_GLOBAL_REF(DES_rw_mode)
12955714Skris
130110007Smarkmconst char *DES_options(void);
131110007Smarkmvoid DES_ecb3_encrypt(const_DES_cblock *input, DES_cblock *output,
132110007Smarkm		      DES_key_schedule *ks1,DES_key_schedule *ks2,
133110007Smarkm		      DES_key_schedule *ks3, int enc);
134110007SmarkmDES_LONG DES_cbc_cksum(const unsigned char *input,DES_cblock *output,
135110007Smarkm		       long length,DES_key_schedule *schedule,
136110007Smarkm		       const_DES_cblock *ivec);
137110007Smarkm/* DES_cbc_encrypt does not update the IV!  Use DES_ncbc_encrypt instead. */
138110007Smarkmvoid DES_cbc_encrypt(const unsigned char *input,unsigned char *output,
139110007Smarkm		     long length,DES_key_schedule *schedule,DES_cblock *ivec,
14055714Skris		     int enc);
141110007Smarkmvoid DES_ncbc_encrypt(const unsigned char *input,unsigned char *output,
142110007Smarkm		      long length,DES_key_schedule *schedule,DES_cblock *ivec,
14355714Skris		      int enc);
144110007Smarkmvoid DES_xcbc_encrypt(const unsigned char *input,unsigned char *output,
145110007Smarkm		      long length,DES_key_schedule *schedule,DES_cblock *ivec,
146110007Smarkm		      const_DES_cblock *inw,const_DES_cblock *outw,int enc);
147110007Smarkmvoid DES_cfb_encrypt(const unsigned char *in,unsigned char *out,int numbits,
148110007Smarkm		     long length,DES_key_schedule *schedule,DES_cblock *ivec,
14955714Skris		     int enc);
150110007Smarkmvoid DES_ecb_encrypt(const_DES_cblock *input,DES_cblock *output,
151110007Smarkm		     DES_key_schedule *ks,int enc);
15259194Skris
15359194Skris/* 	This is the DES encryption function that gets called by just about
15459194Skris	every other DES routine in the library.  You should not use this
15559194Skris	function except to implement 'modes' of DES.  I say this because the
15659194Skris	functions that call this routine do the conversion from 'char *' to
15759194Skris	long, and this needs to be done to make sure 'non-aligned' memory
15859194Skris	access do not occur.  The characters are loaded 'little endian'.
15959194Skris	Data is a pointer to 2 unsigned long's and ks is the
160110007Smarkm	DES_key_schedule to use.  enc, is non zero specifies encryption,
16159194Skris	zero if decryption. */
162110007Smarkmvoid DES_encrypt1(DES_LONG *data,DES_key_schedule *ks, int enc);
16359194Skris
164110007Smarkm/* 	This functions is the same as DES_encrypt1() except that the DES
16559194Skris	initial permutation (IP) and final permutation (FP) have been left
166110007Smarkm	out.  As for DES_encrypt1(), you should not use this function.
16759194Skris	It is used by the routines in the library that implement triple DES.
168110007Smarkm	IP() DES_encrypt2() DES_encrypt2() DES_encrypt2() FP() is the same
169110007Smarkm	as DES_encrypt1() DES_encrypt1() DES_encrypt1() except faster :-). */
170110007Smarkmvoid DES_encrypt2(DES_LONG *data,DES_key_schedule *ks, int enc);
17159194Skris
172110007Smarkmvoid DES_encrypt3(DES_LONG *data, DES_key_schedule *ks1,
173110007Smarkm		  DES_key_schedule *ks2, DES_key_schedule *ks3);
174110007Smarkmvoid DES_decrypt3(DES_LONG *data, DES_key_schedule *ks1,
175110007Smarkm		  DES_key_schedule *ks2, DES_key_schedule *ks3);
176110007Smarkmvoid DES_ede3_cbc_encrypt(const unsigned char *input,unsigned char *output,
17755714Skris			  long length,
178110007Smarkm			  DES_key_schedule *ks1,DES_key_schedule *ks2,
179110007Smarkm			  DES_key_schedule *ks3,DES_cblock *ivec,int enc);
180110007Smarkmvoid DES_ede3_cbcm_encrypt(const unsigned char *in,unsigned char *out,
18155714Skris			   long length,
182110007Smarkm			   DES_key_schedule *ks1,DES_key_schedule *ks2,
183110007Smarkm			   DES_key_schedule *ks3,
184110007Smarkm			   DES_cblock *ivec1,DES_cblock *ivec2,
18555714Skris			   int enc);
186110007Smarkmvoid DES_ede3_cfb64_encrypt(const unsigned char *in,unsigned char *out,
187110007Smarkm			    long length,DES_key_schedule *ks1,
188110007Smarkm			    DES_key_schedule *ks2,DES_key_schedule *ks3,
189110007Smarkm			    DES_cblock *ivec,int *num,int enc);
190110007Smarkmvoid DES_ede3_ofb64_encrypt(const unsigned char *in,unsigned char *out,
191110007Smarkm			    long length,DES_key_schedule *ks1,
192110007Smarkm			    DES_key_schedule *ks2,DES_key_schedule *ks3,
193110007Smarkm			    DES_cblock *ivec,int *num);
19455714Skris
195110007Smarkmvoid DES_xwhite_in2out(const_DES_cblock *DES_key,const_DES_cblock *in_white,
196110007Smarkm		       DES_cblock *out_white);
19755714Skris
198110007Smarkmint DES_enc_read(int fd,void *buf,int len,DES_key_schedule *sched,
199110007Smarkm		 DES_cblock *iv);
200110007Smarkmint DES_enc_write(int fd,const void *buf,int len,DES_key_schedule *sched,
201110007Smarkm		  DES_cblock *iv);
202110007Smarkmchar *DES_fcrypt(const char *buf,const char *salt, char *ret);
203110007Smarkmchar *DES_crypt(const char *buf,const char *salt);
204110007Smarkmvoid DES_ofb_encrypt(const unsigned char *in,unsigned char *out,int numbits,
205110007Smarkm		     long length,DES_key_schedule *schedule,DES_cblock *ivec);
206110007Smarkmvoid DES_pcbc_encrypt(const unsigned char *input,unsigned char *output,
207110007Smarkm		      long length,DES_key_schedule *schedule,DES_cblock *ivec,
20855714Skris		      int enc);
209110007SmarkmDES_LONG DES_quad_cksum(const unsigned char *input,DES_cblock output[],
210110007Smarkm			long length,int out_count,DES_cblock *seed);
211110007Smarkmint DES_random_key(DES_cblock *ret);
212110007Smarkmvoid DES_set_odd_parity(DES_cblock *key);
213110007Smarkmint DES_check_key_parity(const_DES_cblock *key);
214110007Smarkmint DES_is_weak_key(const_DES_cblock *key);
215110007Smarkm/* DES_set_key (= set_key = DES_key_sched = key_sched) calls
216110007Smarkm * DES_set_key_checked if global variable DES_check_key is set,
217110007Smarkm * DES_set_key_unchecked otherwise. */
218110007Smarkmint DES_set_key(const_DES_cblock *key,DES_key_schedule *schedule);
219110007Smarkmint DES_key_sched(const_DES_cblock *key,DES_key_schedule *schedule);
220110007Smarkmint DES_set_key_checked(const_DES_cblock *key,DES_key_schedule *schedule);
221110007Smarkmvoid DES_set_key_unchecked(const_DES_cblock *key,DES_key_schedule *schedule);
222110007Smarkmvoid DES_string_to_key(const char *str,DES_cblock *key);
223110007Smarkmvoid DES_string_to_2keys(const char *str,DES_cblock *key1,DES_cblock *key2);
224110007Smarkmvoid DES_cfb64_encrypt(const unsigned char *in,unsigned char *out,long length,
225110007Smarkm		       DES_key_schedule *schedule,DES_cblock *ivec,int *num,
22655714Skris		       int enc);
227110007Smarkmvoid DES_ofb64_encrypt(const unsigned char *in,unsigned char *out,long length,
228110007Smarkm		       DES_key_schedule *schedule,DES_cblock *ivec,int *num);
22955714Skris
230110007Smarkmint DES_read_password(DES_cblock *key, const char *prompt, int verify);
231110007Smarkmint DES_read_2passwords(DES_cblock *key1, DES_cblock *key2, const char *prompt,
232110007Smarkm	int verify);
23355714Skris
234110007Smarkm#define DES_fixup_key_parity DES_set_odd_parity
23555714Skris
23655714Skris#ifdef  __cplusplus
23755714Skris}
23855714Skris#endif
23955714Skris
24055714Skris#endif
241