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.
8280304Sjkim *
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).
15280304Sjkim *
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.
22280304Sjkim *
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 :-).
37280304Sjkim * 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)"
40280304Sjkim *
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.
52280304Sjkim *
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.]
5755714Skris */
5855714Skris
59110007Smarkm#ifndef HEADER_NEW_DES_H
60280304Sjkim# define HEADER_NEW_DES_H
6155714Skris
62280304Sjkim# include <openssl/e_os2.h>     /* OPENSSL_EXTERN, OPENSSL_NO_DES, DES_LONG
63280304Sjkim                                 * (via openssl/opensslconf.h */
64160817Ssimon
65280304Sjkim# ifdef OPENSSL_NO_DES
66280304Sjkim#  error DES is disabled.
67280304Sjkim# endif
6855714Skris
69280304Sjkim# ifdef OPENSSL_BUILD_SHLIBCRYPTO
70280304Sjkim#  undef OPENSSL_EXTERN
71280304Sjkim#  define OPENSSL_EXTERN OPENSSL_EXPORT
72280304Sjkim# endif
73110007Smarkm
7468654Skris#ifdef  __cplusplus
7568654Skrisextern "C" {
7668654Skris#endif
7768654Skris
78110007Smarkmtypedef unsigned char DES_cblock[8];
79110007Smarkmtypedef /* const */ unsigned char const_DES_cblock[8];
80280304Sjkim/*
81280304Sjkim * With "const", gcc 2.8.1 on Solaris thinks that DES_cblock * and
82280304Sjkim * const_DES_cblock * are incompatible pointer types.
83280304Sjkim */
8455714Skris
85280304Sjkimtypedef struct DES_ks {
86280304Sjkim    union {
87280304Sjkim        DES_cblock cblock;
88280304Sjkim        /*
89280304Sjkim         * make sure things are correct size on machines with 8 byte longs
90280304Sjkim         */
91280304Sjkim        DES_LONG deslong[2];
92280304Sjkim    } ks[16];
93280304Sjkim} DES_key_schedule;
9455714Skris
95280304Sjkim# ifndef OPENSSL_DISABLE_OLD_DES_SUPPORT
96280304Sjkim#  ifndef OPENSSL_ENABLE_OLD_DES_SUPPORT
97280304Sjkim#   define OPENSSL_ENABLE_OLD_DES_SUPPORT
98280304Sjkim#  endif
99110007Smarkm# endif
10055714Skris
101280304Sjkim# ifdef OPENSSL_ENABLE_OLD_DES_SUPPORT
102280304Sjkim#  include <openssl/des_old.h>
103280304Sjkim# endif
104110007Smarkm
105280304Sjkim# define DES_KEY_SZ      (sizeof(DES_cblock))
106280304Sjkim# define DES_SCHEDULE_SZ (sizeof(DES_key_schedule))
107110007Smarkm
108280304Sjkim# define DES_ENCRYPT     1
109280304Sjkim# define DES_DECRYPT     0
11055714Skris
111280304Sjkim# define DES_CBC_MODE    0
112280304Sjkim# define DES_PCBC_MODE   1
11355714Skris
114280304Sjkim# define DES_ecb2_encrypt(i,o,k1,k2,e) \
115280304Sjkim        DES_ecb3_encrypt((i),(o),(k1),(k2),(k1),(e))
11655714Skris
117280304Sjkim# define DES_ede2_cbc_encrypt(i,o,l,k1,k2,iv,e) \
118280304Sjkim        DES_ede3_cbc_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(e))
11955714Skris
120280304Sjkim# define DES_ede2_cfb64_encrypt(i,o,l,k1,k2,iv,n,e) \
121280304Sjkim        DES_ede3_cfb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n),(e))
12255714Skris
123280304Sjkim# define DES_ede2_ofb64_encrypt(i,o,l,k1,k2,iv,n) \
124280304Sjkim        DES_ede3_ofb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n))
12555714Skris
126280304SjkimOPENSSL_DECLARE_GLOBAL(int, DES_check_key); /* defaults to false */
127280304Sjkim# define DES_check_key OPENSSL_GLOBAL_REF(DES_check_key)
128280304SjkimOPENSSL_DECLARE_GLOBAL(int, DES_rw_mode); /* defaults to DES_PCBC_MODE */
129280304Sjkim# define DES_rw_mode OPENSSL_GLOBAL_REF(DES_rw_mode)
13055714Skris
131110007Smarkmconst char *DES_options(void);
132160817Ssimonvoid DES_ecb3_encrypt(const_DES_cblock *input, DES_cblock *output,
133280304Sjkim                      DES_key_schedule *ks1, DES_key_schedule *ks2,
134280304Sjkim                      DES_key_schedule *ks3, int enc);
135280304SjkimDES_LONG DES_cbc_cksum(const unsigned char *input, DES_cblock *output,
136280304Sjkim                       long length, DES_key_schedule *schedule,
137280304Sjkim                       const_DES_cblock *ivec);
138110007Smarkm/* DES_cbc_encrypt does not update the IV!  Use DES_ncbc_encrypt instead. */
139280304Sjkimvoid DES_cbc_encrypt(const unsigned char *input, unsigned char *output,
140280304Sjkim                     long length, DES_key_schedule *schedule,
141280304Sjkim                     DES_cblock *ivec, int enc);
142280304Sjkimvoid DES_ncbc_encrypt(const unsigned char *input, unsigned char *output,
143280304Sjkim                      long length, DES_key_schedule *schedule,
144280304Sjkim                      DES_cblock *ivec, int enc);
145280304Sjkimvoid DES_xcbc_encrypt(const unsigned char *input, unsigned char *output,
146280304Sjkim                      long length, DES_key_schedule *schedule,
147280304Sjkim                      DES_cblock *ivec, const_DES_cblock *inw,
148280304Sjkim                      const_DES_cblock *outw, int enc);
149280304Sjkimvoid DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits,
150280304Sjkim                     long length, DES_key_schedule *schedule,
151280304Sjkim                     DES_cblock *ivec, int enc);
152280304Sjkimvoid DES_ecb_encrypt(const_DES_cblock *input, DES_cblock *output,
153280304Sjkim                     DES_key_schedule *ks, int enc);
15459194Skris
155280304Sjkim/*
156280304Sjkim * This is the DES encryption function that gets called by just about every
157280304Sjkim * other DES routine in the library.  You should not use this function except
158280304Sjkim * to implement 'modes' of DES.  I say this because the functions that call
159280304Sjkim * this routine do the conversion from 'char *' to long, and this needs to be
160280304Sjkim * done to make sure 'non-aligned' memory access do not occur.  The
161280304Sjkim * characters are loaded 'little endian'. Data is a pointer to 2 unsigned
162280304Sjkim * long's and ks is the DES_key_schedule to use.  enc, is non zero specifies
163280304Sjkim * encryption, zero if decryption.
164280304Sjkim */
165280304Sjkimvoid DES_encrypt1(DES_LONG *data, DES_key_schedule *ks, int enc);
16659194Skris
167280304Sjkim/*
168280304Sjkim * This functions is the same as DES_encrypt1() except that the DES initial
169280304Sjkim * permutation (IP) and final permutation (FP) have been left out.  As for
170280304Sjkim * DES_encrypt1(), you should not use this function. It is used by the
171280304Sjkim * routines in the library that implement triple DES. IP() DES_encrypt2()
172280304Sjkim * DES_encrypt2() DES_encrypt2() FP() is the same as DES_encrypt1()
173280304Sjkim * DES_encrypt1() DES_encrypt1() except faster :-).
174280304Sjkim */
175280304Sjkimvoid DES_encrypt2(DES_LONG *data, DES_key_schedule *ks, int enc);
17659194Skris
177110007Smarkmvoid DES_encrypt3(DES_LONG *data, DES_key_schedule *ks1,
178280304Sjkim                  DES_key_schedule *ks2, DES_key_schedule *ks3);
179110007Smarkmvoid DES_decrypt3(DES_LONG *data, DES_key_schedule *ks1,
180280304Sjkim                  DES_key_schedule *ks2, DES_key_schedule *ks3);
181280304Sjkimvoid DES_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output,
182280304Sjkim                          long length,
183280304Sjkim                          DES_key_schedule *ks1, DES_key_schedule *ks2,
184280304Sjkim                          DES_key_schedule *ks3, DES_cblock *ivec, int enc);
185280304Sjkimvoid DES_ede3_cbcm_encrypt(const unsigned char *in, unsigned char *out,
186280304Sjkim                           long length,
187280304Sjkim                           DES_key_schedule *ks1, DES_key_schedule *ks2,
188280304Sjkim                           DES_key_schedule *ks3,
189280304Sjkim                           DES_cblock *ivec1, DES_cblock *ivec2, int enc);
190280304Sjkimvoid DES_ede3_cfb64_encrypt(const unsigned char *in, unsigned char *out,
191280304Sjkim                            long length, DES_key_schedule *ks1,
192280304Sjkim                            DES_key_schedule *ks2, DES_key_schedule *ks3,
193280304Sjkim                            DES_cblock *ivec, int *num, int enc);
194280304Sjkimvoid DES_ede3_cfb_encrypt(const unsigned char *in, unsigned char *out,
195280304Sjkim                          int numbits, long length, DES_key_schedule *ks1,
196280304Sjkim                          DES_key_schedule *ks2, DES_key_schedule *ks3,
197280304Sjkim                          DES_cblock *ivec, int enc);
198280304Sjkimvoid DES_ede3_ofb64_encrypt(const unsigned char *in, unsigned char *out,
199280304Sjkim                            long length, DES_key_schedule *ks1,
200280304Sjkim                            DES_key_schedule *ks2, DES_key_schedule *ks3,
201280304Sjkim                            DES_cblock *ivec, int *num);
202280304Sjkim# if 0
203280304Sjkimvoid DES_xwhite_in2out(const_DES_cblock *DES_key, const_DES_cblock *in_white,
204280304Sjkim                       DES_cblock *out_white);
205280304Sjkim# endif
20655714Skris
207280304Sjkimint DES_enc_read(int fd, void *buf, int len, DES_key_schedule *sched,
208280304Sjkim                 DES_cblock *iv);
209280304Sjkimint DES_enc_write(int fd, const void *buf, int len, DES_key_schedule *sched,
210280304Sjkim                  DES_cblock *iv);
211280304Sjkimchar *DES_fcrypt(const char *buf, const char *salt, char *ret);
212280304Sjkimchar *DES_crypt(const char *buf, const char *salt);
213280304Sjkimvoid DES_ofb_encrypt(const unsigned char *in, unsigned char *out, int numbits,
214280304Sjkim                     long length, DES_key_schedule *schedule,
215280304Sjkim                     DES_cblock *ivec);
216280304Sjkimvoid DES_pcbc_encrypt(const unsigned char *input, unsigned char *output,
217280304Sjkim                      long length, DES_key_schedule *schedule,
218280304Sjkim                      DES_cblock *ivec, int enc);
219280304SjkimDES_LONG DES_quad_cksum(const unsigned char *input, DES_cblock output[],
220280304Sjkim                        long length, int out_count, DES_cblock *seed);
221110007Smarkmint DES_random_key(DES_cblock *ret);
222110007Smarkmvoid DES_set_odd_parity(DES_cblock *key);
223110007Smarkmint DES_check_key_parity(const_DES_cblock *key);
224110007Smarkmint DES_is_weak_key(const_DES_cblock *key);
225280304Sjkim/*
226280304Sjkim * DES_set_key (= set_key = DES_key_sched = key_sched) calls
227110007Smarkm * DES_set_key_checked if global variable DES_check_key is set,
228280304Sjkim * DES_set_key_unchecked otherwise.
229280304Sjkim */
230280304Sjkimint DES_set_key(const_DES_cblock *key, DES_key_schedule *schedule);
231280304Sjkimint DES_key_sched(const_DES_cblock *key, DES_key_schedule *schedule);
232280304Sjkimint DES_set_key_checked(const_DES_cblock *key, DES_key_schedule *schedule);
233280304Sjkimvoid DES_set_key_unchecked(const_DES_cblock *key, DES_key_schedule *schedule);
234280304Sjkim# ifdef OPENSSL_FIPS
235280304Sjkimvoid private_DES_set_key_unchecked(const_DES_cblock *key,
236280304Sjkim                                   DES_key_schedule *schedule);
237280304Sjkim# endif
238280304Sjkimvoid DES_string_to_key(const char *str, DES_cblock *key);
239280304Sjkimvoid DES_string_to_2keys(const char *str, DES_cblock *key1, DES_cblock *key2);
240280304Sjkimvoid DES_cfb64_encrypt(const unsigned char *in, unsigned char *out,
241280304Sjkim                       long length, DES_key_schedule *schedule,
242280304Sjkim                       DES_cblock *ivec, int *num, int enc);
243280304Sjkimvoid DES_ofb64_encrypt(const unsigned char *in, unsigned char *out,
244280304Sjkim                       long length, DES_key_schedule *schedule,
245280304Sjkim                       DES_cblock *ivec, int *num);
24655714Skris
247110007Smarkmint DES_read_password(DES_cblock *key, const char *prompt, int verify);
248280304Sjkimint DES_read_2passwords(DES_cblock *key1, DES_cblock *key2,
249280304Sjkim                        const char *prompt, int verify);
25055714Skris
251280304Sjkim# define DES_fixup_key_parity DES_set_odd_parity
25255714Skris
25355714Skris#ifdef  __cplusplus
25455714Skris}
25555714Skris#endif
25655714Skris
25755714Skris#endif
258