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