Home | History | Annotate | Line # | Download | only in libdes
      1 /*	$NetBSD: des.h,v 1.1.1.1 2018/02/03 22:43:39 christos Exp $	*/
      2 
      3 /* crypto/des/des.h */
      4 /* Copyright (C) 1995-1997 Eric Young (eay (at) cryptsoft.com)
      5  * All rights reserved.
      6  *
      7  * This package is an SSL implementation written
      8  * by Eric Young (eay (at) cryptsoft.com).
      9  * The implementation was written so as to conform with Netscapes SSL.
     10  *
     11  * This library is free for commercial and non-commercial use as long as
     12  * the following conditions are aheared to.  The following conditions
     13  * apply to all code found in this distribution, be it the RC4, RSA,
     14  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
     15  * included with this distribution is covered by the same copyright terms
     16  * except that the holder is Tim Hudson (tjh (at) cryptsoft.com).
     17  *
     18  * Copyright remains Eric Young's, and as such any Copyright notices in
     19  * the code are not to be removed.
     20  * If this package is used in a product, Eric Young should be given attribution
     21  * as the author of the parts of the library used.
     22  * This can be in the form of a textual message at program startup or
     23  * in documentation (online or textual) provided with the package.
     24  *
     25  * Redistribution and use in source and binary forms, with or without
     26  * modification, are permitted provided that the following conditions
     27  * are met:
     28  * 1. Redistributions of source code must retain the copyright
     29  *    notice, this list of conditions and the following disclaimer.
     30  * 2. Redistributions in binary form must reproduce the above copyright
     31  *    notice, this list of conditions and the following disclaimer in the
     32  *    documentation and/or other materials provided with the distribution.
     33  * 3. All advertising materials mentioning features or use of this software
     34  *    must display the following acknowledgement:
     35  *    "This product includes cryptographic software written by
     36  *     Eric Young (eay (at) cryptsoft.com)"
     37  *    The word 'cryptographic' can be left out if the rouines from the library
     38  *    being used are not cryptographic related :-).
     39  * 4. If you include any Windows specific code (or a derivative thereof) from
     40  *    the apps directory (application code) you must include an acknowledgement:
     41  *    "This product includes software written by Tim Hudson (tjh (at) cryptsoft.com)"
     42  *
     43  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
     44  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     46  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     47  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     49  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     50  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     51  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     52  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     53  * SUCH DAMAGE.
     54  *
     55  * The licence and distribution terms for any publically available version or
     56  * derivative of this code cannot be changed.  i.e. this code cannot simply be
     57  * copied and put under another distribution licence
     58  * [including the GNU Public Licence.]
     59  */
     60 
     61 #ifndef HEADER_DES_H
     62 #define HEADER_DES_H
     63 
     64 #ifdef _KERBEROS_DES_H
     65 #error <openssl/des.h> replaces <kerberos/des.h>.
     66 #endif
     67 
     68 #include <sys/types.h>
     69 #define DES_LONG	u_int32_t
     70 
     71 #ifdef  __cplusplus
     72 extern "C" {
     73 #endif
     74 
     75 typedef unsigned char des_cblock[8];
     76 typedef /* const */ unsigned char const_des_cblock[8];
     77 /* With "const", gcc 2.8.1 on Solaris thinks that des_cblock *
     78  * and const_des_cblock * are incompatible pointer types. */
     79 
     80 #define DES_KEY_SZ 	8	/*(sizeof(des_cblock))*/
     81 #define DES_SCHEDULE_SZ 128	/*(sizeof(des_key_schedule))*/
     82 
     83 typedef DES_LONG des_key_schedule[DES_SCHEDULE_SZ / sizeof(DES_LONG)];
     84 
     85 #define DES_ENCRYPT	1
     86 #define DES_DECRYPT	0
     87 
     88 #define DES_CBC_MODE	0
     89 #define DES_PCBC_MODE	1
     90 
     91 #define des_ecb2_encrypt(i,o,k1,k2,e) \
     92 	des_ecb3_encrypt((i),(o),(k1),(k2),(k1),(e))
     93 
     94 #define des_ede2_cbc_encrypt(i,o,l,k1,k2,iv,e) \
     95 	des_ede3_cbc_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(e))
     96 
     97 #define des_ede2_cfb64_encrypt(i,o,l,k1,k2,iv,n,e) \
     98 	des_ede3_cfb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n),(e))
     99 
    100 #define des_ede2_ofb64_encrypt(i,o,l,k1,k2,iv,n) \
    101 	des_ede3_ofb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n))
    102 
    103 extern int des_check_key;	/* defaults to false */
    104 extern int des_rw_mode;		/* defaults to DES_PCBC_MODE */
    105 extern int des_set_weak_key_flag; /* set the weak key flag */
    106 
    107 const char *des_options(void);
    108 void des_ecb3_encrypt(const_des_cblock *, des_cblock *,
    109 		      des_key_schedule,des_key_schedule,
    110 		      des_key_schedule, int);
    111 DES_LONG des_cbc_cksum(const unsigned char *,des_cblock *,
    112 		       long,des_key_schedule,
    113 		       const_des_cblock *);
    114 /* des_cbc_encrypt does not update the IV!  Use des_ncbc_encrypt instead. */
    115 void des_cbc_encrypt(const unsigned char *input,unsigned char *,
    116 		     long,des_key_schedule,des_cblock *,
    117 		     int);
    118 void des_ncbc_encrypt(const unsigned char *input,unsigned char *,
    119 		      long,des_key_schedule,des_cblock *,
    120 		      int);
    121 void des_xcbc_encrypt(const unsigned char *input,unsigned char *,
    122 		      long,des_key_schedule,des_cblock *,
    123 		      const_des_cblock *inw,const_des_cblock *w,int);
    124 void des_cfb_encrypt(const unsigned char *,unsigned char *,int,
    125 		     long,des_key_schedule,des_cblock *,
    126 		     int);
    127 void des_ecb_encrypt(const_des_cblock *input,des_cblock *,
    128 		     des_key_schedule,int);
    129 
    130 /* 	This is the DES encryption function that gets called by just about
    131 	every other DES routine in the library.  You should not use this
    132 	function except to implement 'modes' of DES.  I say this because the
    133 	functions that call this routine do the conversion from 'char *' to
    134 	long, and this needs to be done to make sure 'non-aligned' memory
    135 	access do not occur.  The characters are loaded 'little endian'.
    136 	Data is a pointer to 2 unsigned long's and ks is the
    137 	des_key_schedule to use.  enc, is non zero specifies encryption,
    138 	zero if decryption. */
    139 void des_encrypt1(DES_LONG *,des_key_schedule, int);
    140 
    141 /* 	This functions is the same as des_encrypt1() except that the DES
    142 	initial permutation (IP) and final permutation (FP) have been left
    143 	out.  As for des_encrypt1(), you should not use this function.
    144 	It is used by the routines in the library that implement triple DES.
    145 	IP() des_encrypt2() des_encrypt2() des_encrypt2() FP() is the same
    146 	as des_encrypt1() des_encrypt1() des_encrypt1() except faster :-). */
    147 void des_encrypt2(DES_LONG *,des_key_schedule, int);
    148 
    149 void des_encrypt3(DES_LONG *, des_key_schedule,
    150 	des_key_schedule, des_key_schedule);
    151 void des_decrypt3(DES_LONG *, des_key_schedule,
    152 	des_key_schedule, des_key_schedule);
    153 void des_ede3_cbc_encrypt(const unsigned char *,unsigned char *,
    154 			  long,
    155 			  des_key_schedule,des_key_schedule,
    156 			  des_key_schedule,des_cblock *,int);
    157 void des_ede3_cbcm_encrypt(const unsigned char *,unsigned char *,
    158 			   long,
    159 			   des_key_schedule,des_key_schedule,
    160 			   des_key_schedule,
    161 			   des_cblock *,des_cblock *,
    162 			   int);
    163 void des_ede3_cfb64_encrypt(const unsigned char *,unsigned char *,
    164 			    long,des_key_schedule,
    165 			    des_key_schedule,des_key_schedule,
    166 			    des_cblock *,int *,int);
    167 void des_ede3_ofb64_encrypt(const unsigned char *,unsigned char *,
    168 			    long,des_key_schedule,
    169 			    des_key_schedule,des_key_schedule,
    170 			    des_cblock *,int *);
    171 
    172 void des_xwhite_in2out(const_des_cblock *des_key,const_des_cblock *in_white,
    173 		       des_cblock *_white);
    174 
    175 int des_enc_read(int fd,void *,int len,des_key_schedule,
    176 		 des_cblock *);
    177 int des_enc_write(int fd,const void *,int len,des_key_schedule,
    178 		  des_cblock *);
    179 char *des_fcrypt(const char *,const char *, char *);
    180 char *des_crypt(const char *,const char *);
    181 void des_ofb_encrypt(const unsigned char *,unsigned char *,int,
    182 		     long,des_key_schedule,des_cblock *);
    183 void des_pcbc_encrypt(const unsigned char *input,unsigned char *,
    184 		      long,des_key_schedule,des_cblock *,
    185 		      int);
    186 DES_LONG des_quad_cksum(const unsigned char *,des_cblock [],
    187 			long,int,des_cblock *);
    188 void des_random_seed(des_cblock *);
    189 int des_random_key(des_cblock *);
    190 int des_read_password(des_cblock *,const char *,int);
    191 int des_read_2passwords(des_cblock *,des_cblock *,
    192 			const char *,int);
    193 int des_read_pw_string(char *,int,const char *,int);
    194 void des_set_odd_parity(des_cblock *);
    195 void des_fixup_key_parity(des_cblock *);
    196 int des_check_key_parity(const_des_cblock *);
    197 int des_is_weak_key(const_des_cblock *);
    198 /* des_set_key (= set_key = des_key_sched = key_sched) calls
    199  * des_set_key_checked if global variable des_check_key is set,
    200  * des_set_key_unchecked otherwise. */
    201 int des_set_key(const_des_cblock *,des_key_schedule);
    202 int des_key_sched(const_des_cblock *,des_key_schedule);
    203 int des_set_key_checked(const_des_cblock *,des_key_schedule);
    204 void des_set_key_unchecked(const_des_cblock *,des_key_schedule);
    205 void des_string_to_key(const char *,des_cblock *);
    206 void des_string_to_2keys(const char *,des_cblock *,des_cblock *);
    207 void des_cfb64_encrypt(const unsigned char *,unsigned char *,long,
    208 		       des_key_schedule,des_cblock *,int *,
    209 		       int);
    210 void des_ofb64_encrypt(const unsigned char *,unsigned char *,long,
    211 		       des_key_schedule,des_cblock *,int *);
    212 int des_read_pw(char *,char *,int size,const char *,int);
    213 
    214 /* The following functions are not in the normal unix build or the
    215  * SSLeay build.  When using the SSLeay build, use RAND_seed()
    216  * and RAND_bytes() instead. */
    217 int des_new_random_key(des_cblock *);
    218 void des_init_random_number_generator(des_cblock *);
    219 void des_set_random_generator_seed(des_cblock *);
    220 
    221 /* The following definitions provide compatibility with the MIT Kerberos
    222  * library. The des_key_schedule structure is not binary compatible. */
    223 
    224 #define _KERBEROS_DES_H
    225 
    226 #define KRBDES_ENCRYPT DES_ENCRYPT
    227 #define KRBDES_DECRYPT DES_DECRYPT
    228 
    229 #ifdef KERBEROS
    230 #  define ENCRYPT DES_ENCRYPT
    231 #  define DECRYPT DES_DECRYPT
    232 #endif
    233 
    234 #ifndef NCOMPAT
    235 #  define C_Block des_cblock
    236 #  define Key_schedule des_key_schedule
    237 #  define KEY_SZ DES_KEY_SZ
    238 #  define string_to_key des_string_to_key
    239 #  define read_pw_string des_read_pw_string
    240 #  define random_key des_random_key
    241 #  define pcbc_encrypt des_pcbc_encrypt
    242 #  define set_key des_set_key
    243 #  define key_sched des_key_sched
    244 #  define ecb_encrypt des_ecb_encrypt
    245 #  define cbc_encrypt des_cbc_encrypt
    246 #  define ncbc_encrypt des_ncbc_encrypt
    247 #  define xcbc_encrypt des_xcbc_encrypt
    248 #  define cbc_cksum des_cbc_cksum
    249 #  define quad_cksum des_quad_cksum
    250 #  define check_parity des_check_key_parity
    251 #endif
    252 
    253 typedef des_key_schedule bit_64;
    254 
    255 #ifdef  __cplusplus
    256 }
    257 #endif
    258 
    259 #endif
    260