POK
des.h
1 /*
2  * POK header
3  *
4  * The following file is a part of the POK project. Any modification should
5  * made according to the POK licence. You CANNOT use this file or a part of
6  * this file is this part of a file for your own project
7  *
8  * For more information on the POK licence, please see our LICENCE FILE
9  *
10  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
11  *
12  * Copyright (c) 2007-2009 POK team
13  *
14  * Created by julien on Tue Dec 8 15:53:28 2009
15  */
16 
17 /* crypto/des/des.h */
18 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
19  * All rights reserved.
20  *
21  * This package is an SSL implementation written
22  * by Eric Young (eay@cryptsoft.com).
23  * The implementation was written so as to conform with Netscapes SSL.
24  *
25  * This library is free for commercial and non-commercial use as long as
26  * the following conditions are aheared to. The following conditions
27  * apply to all code found in this distribution, be it the RC4, RSA,
28  * lhash, DES, etc., code; not just the SSL code. The SSL documentation
29  * included with this distribution is covered by the same copyright terms
30  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
31  *
32  * Copyright remains Eric Young's, and as such any Copyright notices in
33  * the code are not to be removed.
34  * If this package is used in a product, Eric Young should be given attribution
35  * as the author of the parts of the library used.
36  * This can be in the form of a textual message at program startup or
37  * in documentation (online or textual) provided with the package.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the copyright
43  * notice, this list of conditions and the following disclaimer.
44  * 2. Redistributions in binary form must reproduce the above copyright
45  * notice, this list of conditions and the following disclaimer in the
46  * documentation and/or other materials provided with the distribution.
47  * 3. All advertising materials mentioning features or use of this software
48  * must display the following acknowledgement:
49  * "This product includes cryptographic software written by
50  * Eric Young (eay@cryptsoft.com)"
51  * The word 'cryptographic' can be left out if the rouines from the library
52  * being used are not cryptographic related :-).
53  * 4. If you include any Windows specific code (or a derivative thereof) from
54  * the apps directory (application code) you must include an acknowledgement:
55  * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
56  *
57  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
58  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
59  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
60  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
61  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
62  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
63  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
64  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
65  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67  * SUCH DAMAGE.
68  *
69  * The licence and distribution terms for any publically available version or
70  * derivative of this code cannot be changed. i.e. this code cannot simply be
71  * copied and put under another distribution licence
72  * [including the GNU Public Licence.]
73  */
74 
75 #ifdef POK_NEEDS_PROTOCOLS_DES
76 
77 #ifndef __POK_PROTOCOLS_OPENSSL_DES_H__
78 #define __POK_PROTOCOLS_OPENSSL_DES_H__
79 
80 #include <des_locl.h>
81 
82 /*
83 #include <openssl/e_os2.h>
84 */
85 /* OPENSSL_EXTERN, OPENSSL_NO_DES,
86  DES_LONG (via openssl/opensslconf.h */
87 
88 #ifdef __cplusplus
89 extern "C" {
90 #endif
91 
92 typedef unsigned char DES_cblock[8];
93 typedef /* const */ unsigned char const_DES_cblock[8];
94 /* With "const", gcc 2.8.1 on Solaris thinks that DES_cblock *
95  * and const_DES_cblock * are incompatible pointer types. */
96 
97 typedef struct DES_ks
98  {
99  union
100  {
101  DES_cblock cblock;
102  /* make sure things are correct size on machines with
103  * 8 byte longs */
104  DES_LONG deslong[2];
105  } ks[16];
107 
108 #ifndef OPENSSL_DISABLE_OLD_DES_SUPPORT
109 # ifndef OPENSSL_ENABLE_OLD_DES_SUPPORT
110 # define OPENSSL_ENABLE_OLD_DES_SUPPORT
111 # endif
112 #endif
113 
114 /*
115 #ifdef OPENSSL_ENABLE_OLD_DES_SUPPORT
116 # include <openssl/des_old.h>
117 #endif
118 */
119 
120 #define DES_KEY_SZ (sizeof(DES_cblock))
121 #define DES_SCHEDULE_SZ (sizeof(DES_key_schedule))
122 
123 #define DES_ENCRYPT 1
124 #define DES_DECRYPT 0
125 
126 #define DES_CBC_MODE 0
127 #define DES_PCBC_MODE 1
128 
129 #define DES_ecb2_encrypt(i,o,k1,k2,e) \
130  DES_ecb3_encrypt((i),(o),(k1),(k2),(k1),(e))
131 
132 #define DES_ede2_cbc_encrypt(i,o,l,k1,k2,iv,e) \
133  DES_ede3_cbc_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(e))
134 
135 #define DES_ede2_cfb64_encrypt(i,o,l,k1,k2,iv,n,e) \
136  DES_ede3_cfb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n),(e))
137 
138 #define DES_ede2_ofb64_encrypt(i,o,l,k1,k2,iv,n) \
139  DES_ede3_ofb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n))
140 
141 OPENSSL_DECLARE_GLOBAL(int,DES_check_key); /* defaults to false */
142 #define DES_check_key OPENSSL_GLOBAL_REF(DES_check_key)
143 OPENSSL_DECLARE_GLOBAL(int,DES_rw_mode); /* defaults to DES_PCBC_MODE */
144 #define DES_rw_mode OPENSSL_GLOBAL_REF(DES_rw_mode)
145 
146 const char *DES_options(void);
147 void DES_ecb3_encrypt(const_DES_cblock *input, DES_cblock *output,
149  DES_key_schedule *ks3, int enc);
150 DES_LONG DES_cbc_cksum(const unsigned char *input,DES_cblock *output,
151  long length,DES_key_schedule *schedule,
152  const_DES_cblock *ivec);
153 /* DES_cbc_encrypt does not update the IV! Use DES_ncbc_encrypt instead. */
154 void DES_cbc_encrypt(const unsigned char *input,unsigned char *output,
155  long length,DES_key_schedule *schedule,DES_cblock *ivec,
156  int enc);
157 void DES_ncbc_encrypt(const unsigned char *input,unsigned char *output,
158  long length,DES_key_schedule *schedule,DES_cblock *ivec,
159  int enc);
160 void DES_xcbc_encrypt(const unsigned char *input,unsigned char *output,
161  long length,DES_key_schedule *schedule,DES_cblock *ivec,
162  const_DES_cblock *inw,const_DES_cblock *outw,int enc);
163 void DES_cfb_encrypt(const unsigned char *in,unsigned char *out,int numbits,
164  long length,DES_key_schedule *schedule,DES_cblock *ivec,
165  int enc);
166 void DES_ecb_encrypt(const_DES_cblock *input,DES_cblock *output,
167  DES_key_schedule *ks,int enc);
168 
169 /* This is the DES encryption function that gets called by just about
170  every other DES routine in the library. You should not use this
171  function except to implement 'modes' of DES. I say this because the
172  functions that call this routine do the conversion from 'char *' to
173  long, and this needs to be done to make sure 'non-aligned' memory
174  access do not occur. The characters are loaded 'little endian'.
175  Data is a pointer to 2 unsigned long's and ks is the
176  DES_key_schedule to use. enc, is non zero specifies encryption,
177  zero if decryption. */
178 void DES_encrypt1(DES_LONG *data,DES_key_schedule *ks, int enc);
179 
180 /* This functions is the same as DES_encrypt1() except that the DES
181  initial permutation (IP) and final permutation (FP) have been left
182  out. As for DES_encrypt1(), you should not use this function.
183  It is used by the routines in the library that implement triple DES.
184  IP() DES_encrypt2() DES_encrypt2() DES_encrypt2() FP() is the same
185  as DES_encrypt1() DES_encrypt1() DES_encrypt1() except faster :-). */
186 void DES_encrypt2(DES_LONG *data,DES_key_schedule *ks, int enc);
187 
188 void DES_encrypt3(DES_LONG *data, DES_key_schedule *ks1,
190 void DES_decrypt3(DES_LONG *data, DES_key_schedule *ks1,
192 void DES_ede3_cbc_encrypt(const unsigned char *input,unsigned char *output,
193  long length,
195  DES_key_schedule *ks3,DES_cblock *ivec,int enc);
196 void DES_ede3_cbcm_encrypt(const unsigned char *in,unsigned char *out,
197  long length,
199  DES_key_schedule *ks3,
200  DES_cblock *ivec1,DES_cblock *ivec2,
201  int enc);
202 void DES_ede3_cfb64_encrypt(const unsigned char *in,unsigned char *out,
203  long length,DES_key_schedule *ks1,
205  DES_cblock *ivec,int *num,int enc);
206 void DES_ede3_cfb_encrypt(const unsigned char *in,unsigned char *out,
207  int numbits,long length,DES_key_schedule *ks1,
209  DES_cblock *ivec,int enc);
210 void DES_ede3_ofb64_encrypt(const unsigned char *in,unsigned char *out,
211  long length,DES_key_schedule *ks1,
213  DES_cblock *ivec,int *num);
214 #if 0
215 void DES_xwhite_in2out(const_DES_cblock *DES_key,const_DES_cblock *in_white,
216  DES_cblock *out_white);
217 #endif
218 
219 int DES_enc_read(int fd,void *buf,int len,DES_key_schedule *sched,
220  DES_cblock *iv);
221 int DES_enc_write(int fd,const void *buf,int len,DES_key_schedule *sched,
222  DES_cblock *iv);
223 char *DES_fcrypt(const char *buf,const char *salt, char *ret);
224 char *DES_crypt(const char *buf,const char *salt);
225 void DES_ofb_encrypt(const unsigned char *in,unsigned char *out,int numbits,
226  long length,DES_key_schedule *schedule,DES_cblock *ivec);
227 void DES_pcbc_encrypt(const unsigned char *input,unsigned char *output,
228  long length,DES_key_schedule *schedule,DES_cblock *ivec,
229  int enc);
230 DES_LONG DES_quad_cksum(const unsigned char *input,DES_cblock output[],
231  long length,int out_count,DES_cblock *seed);
232 int DES_random_key(DES_cblock *ret);
233 void DES_set_odd_parity(DES_cblock *key);
234 int DES_check_key_parity(const_DES_cblock *key);
235 int DES_is_weak_key(const_DES_cblock *key);
236 /* DES_set_key (= set_key = DES_key_sched = key_sched) calls
237  * DES_set_key_checked if global variable DES_check_key is set,
238  * DES_set_key_unchecked otherwise. */
239 int DES_set_key(const_DES_cblock *key,DES_key_schedule *schedule);
240 int DES_key_sched(const_DES_cblock *key,DES_key_schedule *schedule);
241 int DES_set_key_checked(const_DES_cblock *key,DES_key_schedule *schedule);
242 void DES_set_key_unchecked(const_DES_cblock *key,DES_key_schedule *schedule);
243 void DES_string_to_key(const char *str,DES_cblock *key);
244 void DES_string_to_2keys(const char *str,DES_cblock *key1,DES_cblock *key2);
245 void DES_cfb64_encrypt(const unsigned char *in,unsigned char *out,long length,
246  DES_key_schedule *schedule,DES_cblock *ivec,int *num,
247  int enc);
248 void DES_ofb64_encrypt(const unsigned char *in,unsigned char *out,long length,
249  DES_key_schedule *schedule,DES_cblock *ivec,int *num);
250 
251 int DES_read_password(DES_cblock *key, const char *prompt, int verify);
252 int DES_read_2passwords(DES_cblock *key1, DES_cblock *key2, const char *prompt,
253  int verify);
254 
255 #define DES_fixup_key_parity DES_set_odd_parity
256 
257 #ifdef __cplusplus
258 }
259 #endif
260 
261 #endif
262 
263 #endif /* POK_NEEDS_ ...*/