des.h revision 68654
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.]
5757426Smarkm * $FreeBSD: head/crypto/openssl/crypto/des/des.h 68654 2000-11-13 02:20:29Z kris $
5855714Skris */
5955714Skris
6055714Skris#ifndef HEADER_DES_H
6155714Skris#define HEADER_DES_H
6255714Skris
6355714Skris#ifdef NO_DES
6455714Skris#error DES is disabled.
6555714Skris#endif
6655714Skris
6755714Skris#ifdef _KERBEROS_DES_H
6855714Skris#error <openssl/des.h> replaces <kerberos/des.h>.
6955714Skris#endif
7055714Skris
7155714Skris#include <openssl/opensslconf.h> /* DES_LONG */
7255714Skris#include <openssl/e_os2.h>	/* OPENSSL_EXTERN */
7355714Skris
7468654Skris#ifdef  __cplusplus
7568654Skrisextern "C" {
7668654Skris#endif
7768654Skris
7855714Skristypedef unsigned char des_cblock[8];
7955714Skristypedef /* const */ unsigned char const_des_cblock[8];
8055714Skris/* With "const", gcc 2.8.1 on Solaris thinks that des_cblock *
8159194Skris * and const_des_cblock * are incompatible pointer types. */
8255714Skris
8355714Skristypedef struct des_ks_struct
8455714Skris	{
8555714Skris	union	{
8655714Skris		des_cblock cblock;
8755714Skris		/* make sure things are correct size on machines with
8855714Skris		 * 8 byte longs */
8955714Skris		DES_LONG deslong[2];
9055714Skris		} ks;
9155714Skris	int weak_key;
9255714Skris	} des_key_schedule[16];
9355714Skris
9455714Skris#define DES_KEY_SZ 	(sizeof(des_cblock))
9555714Skris#define DES_SCHEDULE_SZ (sizeof(des_key_schedule))
9655714Skris
9755714Skris#define DES_ENCRYPT	1
9855714Skris#define DES_DECRYPT	0
9955714Skris
10055714Skris#define DES_CBC_MODE	0
10155714Skris#define DES_PCBC_MODE	1
10255714Skris
10355714Skris#define des_ecb2_encrypt(i,o,k1,k2,e) \
10455714Skris	des_ecb3_encrypt((i),(o),(k1),(k2),(k1),(e))
10555714Skris
10655714Skris#define des_ede2_cbc_encrypt(i,o,l,k1,k2,iv,e) \
10755714Skris	des_ede3_cbc_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(e))
10855714Skris
10955714Skris#define des_ede2_cfb64_encrypt(i,o,l,k1,k2,iv,n,e) \
11055714Skris	des_ede3_cfb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n),(e))
11155714Skris
11255714Skris#define des_ede2_ofb64_encrypt(i,o,l,k1,k2,iv,n) \
11355714Skris	des_ede3_ofb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n))
11455714Skris
11555714SkrisOPENSSL_EXTERN int des_check_key;	/* defaults to false */
11655714SkrisOPENSSL_EXTERN int des_rw_mode;		/* defaults to DES_PCBC_MODE */
11755714SkrisOPENSSL_EXTERN int des_set_weak_key_flag; /* set the weak key flag */
11855714Skris
11955714Skrisconst char *des_options(void);
12055714Skrisvoid des_ecb3_encrypt(const_des_cblock *input, des_cblock *output,
12155714Skris		      des_key_schedule ks1,des_key_schedule ks2,
12255714Skris		      des_key_schedule ks3, int enc);
12355714SkrisDES_LONG des_cbc_cksum(const unsigned char *input,des_cblock *output,
12455714Skris		       long length,des_key_schedule schedule,
12555714Skris		       const_des_cblock *ivec);
12655714Skris/* des_cbc_encrypt does not update the IV!  Use des_ncbc_encrypt instead. */
12755714Skrisvoid des_cbc_encrypt(const unsigned char *input,unsigned char *output,
12855714Skris		     long length,des_key_schedule schedule,des_cblock *ivec,
12955714Skris		     int enc);
13055714Skrisvoid des_ncbc_encrypt(const unsigned char *input,unsigned char *output,
13155714Skris		      long length,des_key_schedule schedule,des_cblock *ivec,
13255714Skris		      int enc);
13355714Skrisvoid des_xcbc_encrypt(const unsigned char *input,unsigned char *output,
13455714Skris		      long length,des_key_schedule schedule,des_cblock *ivec,
13555714Skris		      const_des_cblock *inw,const_des_cblock *outw,int enc);
13655714Skrisvoid des_cfb_encrypt(const unsigned char *in,unsigned char *out,int numbits,
13755714Skris		     long length,des_key_schedule schedule,des_cblock *ivec,
13855714Skris		     int enc);
13955714Skrisvoid des_ecb_encrypt(const_des_cblock *input,des_cblock *output,
14055714Skris		     des_key_schedule ks,int enc);
14159194Skris
14259194Skris/* 	This is the DES encryption function that gets called by just about
14359194Skris	every other DES routine in the library.  You should not use this
14459194Skris	function except to implement 'modes' of DES.  I say this because the
14559194Skris	functions that call this routine do the conversion from 'char *' to
14659194Skris	long, and this needs to be done to make sure 'non-aligned' memory
14759194Skris	access do not occur.  The characters are loaded 'little endian'.
14859194Skris	Data is a pointer to 2 unsigned long's and ks is the
14959194Skris	des_key_schedule to use.  enc, is non zero specifies encryption,
15059194Skris	zero if decryption. */
15155714Skrisvoid des_encrypt(DES_LONG *data,des_key_schedule ks, int enc);
15259194Skris
15359194Skris/* 	This functions is the same as des_encrypt() except that the DES
15459194Skris	initial permutation (IP) and final permutation (FP) have been left
15559194Skris	out.  As for des_encrypt(), you should not use this function.
15659194Skris	It is used by the routines in the library that implement triple DES.
15759194Skris	IP() des_encrypt2() des_encrypt2() des_encrypt2() FP() is the same
15859194Skris	as des_encrypt() des_encrypt() des_encrypt() except faster :-). */
15955714Skrisvoid des_encrypt2(DES_LONG *data,des_key_schedule ks, int enc);
16059194Skris
16155714Skrisvoid des_encrypt3(DES_LONG *data, des_key_schedule ks1,
16255714Skris	des_key_schedule ks2, des_key_schedule ks3);
16355714Skrisvoid des_decrypt3(DES_LONG *data, des_key_schedule ks1,
16455714Skris	des_key_schedule ks2, des_key_schedule ks3);
16555714Skrisvoid des_ede3_cbc_encrypt(const unsigned char *input,unsigned char *output,
16655714Skris			  long length,
16755714Skris			  des_key_schedule ks1,des_key_schedule ks2,
16855714Skris			  des_key_schedule ks3,des_cblock *ivec,int enc);
16955714Skrisvoid des_ede3_cbcm_encrypt(const unsigned char *in,unsigned char *out,
17055714Skris			   long length,
17155714Skris			   des_key_schedule ks1,des_key_schedule ks2,
17255714Skris			   des_key_schedule ks3,
17355714Skris			   des_cblock *ivec1,des_cblock *ivec2,
17455714Skris			   int enc);
17555714Skrisvoid des_ede3_cfb64_encrypt(const unsigned char *in,unsigned char *out,
17655714Skris			    long length,des_key_schedule ks1,
17755714Skris			    des_key_schedule ks2,des_key_schedule ks3,
17855714Skris			    des_cblock *ivec,int *num,int enc);
17955714Skrisvoid des_ede3_ofb64_encrypt(const unsigned char *in,unsigned char *out,
18055714Skris			    long length,des_key_schedule ks1,
18155714Skris			    des_key_schedule ks2,des_key_schedule ks3,
18255714Skris			    des_cblock *ivec,int *num);
18355714Skris
18455714Skrisvoid des_xwhite_in2out(const_des_cblock *des_key,const_des_cblock *in_white,
18555714Skris		       des_cblock *out_white);
18655714Skris
18755714Skrisint des_enc_read(int fd,void *buf,int len,des_key_schedule sched,
18855714Skris		 des_cblock *iv);
18955714Skrisint des_enc_write(int fd,const void *buf,int len,des_key_schedule sched,
19055714Skris		  des_cblock *iv);
19155714Skrischar *des_fcrypt(const char *buf,const char *salt, char *ret);
19255714Skrischar *des_crypt(const char *buf,const char *salt);
19355714Skris#if !defined(PERL5) && !defined(__FreeBSD__) && !defined(NeXT)
19455714Skrischar *crypt(const char *buf,const char *salt);
19555714Skris#endif
19655714Skrisvoid des_ofb_encrypt(const unsigned char *in,unsigned char *out,int numbits,
19755714Skris		     long length,des_key_schedule schedule,des_cblock *ivec);
19855714Skrisvoid des_pcbc_encrypt(const unsigned char *input,unsigned char *output,
19955714Skris		      long length,des_key_schedule schedule,des_cblock *ivec,
20055714Skris		      int enc);
20155714SkrisDES_LONG des_quad_cksum(const unsigned char *input,des_cblock output[],
20255714Skris			long length,int out_count,des_cblock *seed);
20355714Skrisvoid des_random_seed(des_cblock *key);
20457426Smarkmint des_new_random_key(des_cblock *key);
20557426Smarkmvoid des_init_random_number_generator(des_cblock *seed);
20657426Smarkmvoid des_rand_data(unsigned char *data, int size);
20759194Skrisint des_random_key(des_cblock *ret);
20855714Skrisint des_read_password(des_cblock *key,const char *prompt,int verify);
20955714Skrisint des_read_2passwords(des_cblock *key1,des_cblock *key2,
21055714Skris			const char *prompt,int verify);
21155714Skrisint des_read_pw_string(char *buf,int length,const char *prompt,int verify);
21255714Skrisvoid des_set_odd_parity(des_cblock *key);
21359194Skrisint des_check_key_parity(const_des_cblock *key);
21455714Skrisint des_is_weak_key(const_des_cblock *key);
21559194Skris/* des_set_key (= set_key = des_key_sched = key_sched) calls
21659194Skris * des_set_key_checked if global variable des_check_key is set,
21759194Skris * des_set_key_unchecked otherwise. */
21855714Skrisint des_set_key(const_des_cblock *key,des_key_schedule schedule);
21955714Skrisint des_key_sched(const_des_cblock *key,des_key_schedule schedule);
22059194Skrisint des_set_key_checked(const_des_cblock *key,des_key_schedule schedule);
22159194Skrisvoid des_set_key_unchecked(const_des_cblock *key,des_key_schedule schedule);
22255714Skrisvoid des_string_to_key(const char *str,des_cblock *key);
22355714Skrisvoid des_string_to_2keys(const char *str,des_cblock *key1,des_cblock *key2);
22455714Skrisvoid des_cfb64_encrypt(const unsigned char *in,unsigned char *out,long length,
22555714Skris		       des_key_schedule schedule,des_cblock *ivec,int *num,
22655714Skris		       int enc);
22755714Skrisvoid des_ofb64_encrypt(const unsigned char *in,unsigned char *out,long length,
22855714Skris		       des_key_schedule schedule,des_cblock *ivec,int *num);
22955714Skrisint des_read_pw(char *buf,char *buff,int size,const char *prompt,int verify);
23055714Skris
23155714Skris/* The following definitions provide compatibility with the MIT Kerberos
23255714Skris * library. The des_key_schedule structure is not binary compatible. */
23355714Skris
23455714Skris#define _KERBEROS_DES_H
23555714Skris
23655714Skris#define KRBDES_ENCRYPT DES_ENCRYPT
23755714Skris#define KRBDES_DECRYPT DES_DECRYPT
23855714Skris
23955714Skris#ifdef KERBEROS
24055714Skris#  define ENCRYPT DES_ENCRYPT
24155714Skris#  define DECRYPT DES_DECRYPT
24255714Skris#endif
24355714Skris
24455714Skris#ifndef NCOMPAT
24555714Skris#  define C_Block des_cblock
24655714Skris#  define Key_schedule des_key_schedule
24755714Skris#  define KEY_SZ DES_KEY_SZ
24855714Skris#  define string_to_key des_string_to_key
24955714Skris#  define read_pw_string des_read_pw_string
25055714Skris#  define random_key des_random_key
25155714Skris#  define pcbc_encrypt des_pcbc_encrypt
25255714Skris#  define set_key des_set_key
25355714Skris#  define key_sched des_key_sched
25455714Skris#  define ecb_encrypt des_ecb_encrypt
25555714Skris#  define cbc_encrypt des_cbc_encrypt
25655714Skris#  define ncbc_encrypt des_ncbc_encrypt
25755714Skris#  define xcbc_encrypt des_xcbc_encrypt
25855714Skris#  define cbc_cksum des_cbc_cksum
25955714Skris#  define quad_cksum des_quad_cksum
26059194Skris#  define check_parity des_check_key_parity
26155714Skris#endif
26255714Skris
26355714Skristypedef des_key_schedule bit_64;
26455714Skris#define des_fixup_key_parity des_set_odd_parity
26555714Skris
26655714Skris#ifdef  __cplusplus
26755714Skris}
26855714Skris#endif
26955714Skris
27055714Skris#endif
271