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