POK
/home/jaouen/pok_official/pok/trunk/libpok/protocols/des/des.h
00001 /*
00002  *                               POK header
00003  * 
00004  * The following file is a part of the POK project. Any modification should
00005  * made according to the POK licence. You CANNOT use this file or a part of
00006  * this file is this part of a file for your own project
00007  *
00008  * For more information on the POK licence, please see our LICENCE FILE
00009  *
00010  * Please follow the coding guidelines described in doc/CODING_GUIDELINES
00011  *
00012  *                                      Copyright (c) 2007-2009 POK team 
00013  *
00014  * Created by julien on Tue Dec  8 15:53:28 2009 
00015  */
00016 
00017 /* crypto/des/des.h */
00018 /* Copyright (C) 1995-1997 Eric Young (eay@cryptsoft.com)
00019  * All rights reserved.
00020  *
00021  * This package is an SSL implementation written
00022  * by Eric Young (eay@cryptsoft.com).
00023  * The implementation was written so as to conform with Netscapes SSL.
00024  * 
00025  * This library is free for commercial and non-commercial use as long as
00026  * the following conditions are aheared to.  The following conditions
00027  * apply to all code found in this distribution, be it the RC4, RSA,
00028  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
00029  * included with this distribution is covered by the same copyright terms
00030  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
00031  * 
00032  * Copyright remains Eric Young's, and as such any Copyright notices in
00033  * the code are not to be removed.
00034  * If this package is used in a product, Eric Young should be given attribution
00035  * as the author of the parts of the library used.
00036  * This can be in the form of a textual message at program startup or
00037  * in documentation (online or textual) provided with the package.
00038  * 
00039  * Redistribution and use in source and binary forms, with or without
00040  * modification, are permitted provided that the following conditions
00041  * are met:
00042  * 1. Redistributions of source code must retain the copyright
00043  *    notice, this list of conditions and the following disclaimer.
00044  * 2. Redistributions in binary form must reproduce the above copyright
00045  *    notice, this list of conditions and the following disclaimer in the
00046  *    documentation and/or other materials provided with the distribution.
00047  * 3. All advertising materials mentioning features or use of this software
00048  *    must display the following acknowledgement:
00049  *    "This product includes cryptographic software written by
00050  *     Eric Young (eay@cryptsoft.com)"
00051  *    The word 'cryptographic' can be left out if the rouines from the library
00052  *    being used are not cryptographic related :-).
00053  * 4. If you include any Windows specific code (or a derivative thereof) from 
00054  *    the apps directory (application code) you must include an acknowledgement:
00055  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
00056  * 
00057  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
00058  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00059  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00060  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00061  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00062  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00063  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00064  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00065  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00066  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00067  * SUCH DAMAGE.
00068  * 
00069  * The licence and distribution terms for any publically available version or
00070  * derivative of this code cannot be changed.  i.e. this code cannot simply be
00071  * copied and put under another distribution licence
00072  * [including the GNU Public Licence.]
00073  */
00074 
00075 #ifdef POK_NEEDS_PROTOCOLS_DES
00076 
00077 #ifndef __POK_PROTOCOLS_OPENSSL_DES_H__
00078 #define __POK_PROTOCOLS_OPENSSL_DES_H__
00079 
00080 #include <des_locl.h>
00081 
00082 /*
00083 #include <openssl/e_os2.h>      
00084 */
00085 /* OPENSSL_EXTERN, OPENSSL_NO_DES,
00086                                    DES_LONG (via openssl/opensslconf.h */
00087 
00088 #ifdef  __cplusplus
00089 extern "C" {
00090 #endif
00091 
00092 typedef unsigned char DES_cblock[8];
00093 typedef /* const */ unsigned char const_DES_cblock[8];
00094 /* With "const", gcc 2.8.1 on Solaris thinks that DES_cblock *
00095  * and const_DES_cblock * are incompatible pointer types. */
00096 
00097 typedef struct DES_ks
00098     {
00099     union
00100         {
00101         DES_cblock cblock;
00102         /* make sure things are correct size on machines with
00103          * 8 byte longs */
00104         DES_LONG deslong[2];
00105         } ks[16];
00106     } DES_key_schedule;
00107 
00108 #ifndef OPENSSL_DISABLE_OLD_DES_SUPPORT
00109 # ifndef OPENSSL_ENABLE_OLD_DES_SUPPORT
00110 #  define OPENSSL_ENABLE_OLD_DES_SUPPORT
00111 # endif
00112 #endif
00113 
00114 /*
00115 #ifdef OPENSSL_ENABLE_OLD_DES_SUPPORT
00116 # include <openssl/des_old.h>
00117 #endif
00118 */
00119 
00120 #define DES_KEY_SZ      (sizeof(DES_cblock))
00121 #define DES_SCHEDULE_SZ (sizeof(DES_key_schedule))
00122 
00123 #define DES_ENCRYPT     1
00124 #define DES_DECRYPT     0
00125 
00126 #define DES_CBC_MODE    0
00127 #define DES_PCBC_MODE   1
00128 
00129 #define DES_ecb2_encrypt(i,o,k1,k2,e) \
00130         DES_ecb3_encrypt((i),(o),(k1),(k2),(k1),(e))
00131 
00132 #define DES_ede2_cbc_encrypt(i,o,l,k1,k2,iv,e) \
00133         DES_ede3_cbc_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(e))
00134 
00135 #define DES_ede2_cfb64_encrypt(i,o,l,k1,k2,iv,n,e) \
00136         DES_ede3_cfb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n),(e))
00137 
00138 #define DES_ede2_ofb64_encrypt(i,o,l,k1,k2,iv,n) \
00139         DES_ede3_ofb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n))
00140 
00141 OPENSSL_DECLARE_GLOBAL(int,DES_check_key);      /* defaults to false */
00142 #define DES_check_key OPENSSL_GLOBAL_REF(DES_check_key)
00143 OPENSSL_DECLARE_GLOBAL(int,DES_rw_mode);        /* defaults to DES_PCBC_MODE */
00144 #define DES_rw_mode OPENSSL_GLOBAL_REF(DES_rw_mode)
00145 
00146 const char *DES_options(void);
00147 void DES_ecb3_encrypt(const_DES_cblock *input, DES_cblock *output,
00148                       DES_key_schedule *ks1,DES_key_schedule *ks2,
00149                       DES_key_schedule *ks3, int enc);
00150 DES_LONG DES_cbc_cksum(const unsigned char *input,DES_cblock *output,
00151                        long length,DES_key_schedule *schedule,
00152                        const_DES_cblock *ivec);
00153 /* DES_cbc_encrypt does not update the IV!  Use DES_ncbc_encrypt instead. */
00154 void DES_cbc_encrypt(const unsigned char *input,unsigned char *output,
00155                      long length,DES_key_schedule *schedule,DES_cblock *ivec,
00156                      int enc);
00157 void DES_ncbc_encrypt(const unsigned char *input,unsigned char *output,
00158                       long length,DES_key_schedule *schedule,DES_cblock *ivec,
00159                       int enc);
00160 void DES_xcbc_encrypt(const unsigned char *input,unsigned char *output,
00161                       long length,DES_key_schedule *schedule,DES_cblock *ivec,
00162                       const_DES_cblock *inw,const_DES_cblock *outw,int enc);
00163 void DES_cfb_encrypt(const unsigned char *in,unsigned char *out,int numbits,
00164                      long length,DES_key_schedule *schedule,DES_cblock *ivec,
00165                      int enc);
00166 void DES_ecb_encrypt(const_DES_cblock *input,DES_cblock *output,
00167                      DES_key_schedule *ks,int enc);
00168 
00169 /*      This is the DES encryption function that gets called by just about
00170         every other DES routine in the library.  You should not use this
00171         function except to implement 'modes' of DES.  I say this because the
00172         functions that call this routine do the conversion from 'char *' to
00173         long, and this needs to be done to make sure 'non-aligned' memory
00174         access do not occur.  The characters are loaded 'little endian'.
00175         Data is a pointer to 2 unsigned long's and ks is the
00176         DES_key_schedule to use.  enc, is non zero specifies encryption,
00177         zero if decryption. */
00178 void DES_encrypt1(DES_LONG *data,DES_key_schedule *ks, int enc);
00179 
00180 /*      This functions is the same as DES_encrypt1() except that the DES
00181         initial permutation (IP) and final permutation (FP) have been left
00182         out.  As for DES_encrypt1(), you should not use this function.
00183         It is used by the routines in the library that implement triple DES.
00184         IP() DES_encrypt2() DES_encrypt2() DES_encrypt2() FP() is the same
00185         as DES_encrypt1() DES_encrypt1() DES_encrypt1() except faster :-). */
00186 void DES_encrypt2(DES_LONG *data,DES_key_schedule *ks, int enc);
00187 
00188 void DES_encrypt3(DES_LONG *data, DES_key_schedule *ks1,
00189                   DES_key_schedule *ks2, DES_key_schedule *ks3);
00190 void DES_decrypt3(DES_LONG *data, DES_key_schedule *ks1,
00191                   DES_key_schedule *ks2, DES_key_schedule *ks3);
00192 void DES_ede3_cbc_encrypt(const unsigned char *input,unsigned char *output, 
00193                           long length,
00194                           DES_key_schedule *ks1,DES_key_schedule *ks2,
00195                           DES_key_schedule *ks3,DES_cblock *ivec,int enc);
00196 void DES_ede3_cbcm_encrypt(const unsigned char *in,unsigned char *out,
00197                            long length,
00198                            DES_key_schedule *ks1,DES_key_schedule *ks2,
00199                            DES_key_schedule *ks3,
00200                            DES_cblock *ivec1,DES_cblock *ivec2,
00201                            int enc);
00202 void DES_ede3_cfb64_encrypt(const unsigned char *in,unsigned char *out,
00203                             long length,DES_key_schedule *ks1,
00204                             DES_key_schedule *ks2,DES_key_schedule *ks3,
00205                             DES_cblock *ivec,int *num,int enc);
00206 void DES_ede3_cfb_encrypt(const unsigned char *in,unsigned char *out,
00207                           int numbits,long length,DES_key_schedule *ks1,
00208                           DES_key_schedule *ks2,DES_key_schedule *ks3,
00209                           DES_cblock *ivec,int enc);
00210 void DES_ede3_ofb64_encrypt(const unsigned char *in,unsigned char *out,
00211                             long length,DES_key_schedule *ks1,
00212                             DES_key_schedule *ks2,DES_key_schedule *ks3,
00213                             DES_cblock *ivec,int *num);
00214 #if 0
00215 void DES_xwhite_in2out(const_DES_cblock *DES_key,const_DES_cblock *in_white,
00216                        DES_cblock *out_white);
00217 #endif
00218 
00219 int DES_enc_read(int fd,void *buf,int len,DES_key_schedule *sched,
00220                  DES_cblock *iv);
00221 int DES_enc_write(int fd,const void *buf,int len,DES_key_schedule *sched,
00222                   DES_cblock *iv);
00223 char *DES_fcrypt(const char *buf,const char *salt, char *ret);
00224 char *DES_crypt(const char *buf,const char *salt);
00225 void DES_ofb_encrypt(const unsigned char *in,unsigned char *out,int numbits,
00226                      long length,DES_key_schedule *schedule,DES_cblock *ivec);
00227 void DES_pcbc_encrypt(const unsigned char *input,unsigned char *output,
00228                       long length,DES_key_schedule *schedule,DES_cblock *ivec,
00229                       int enc);
00230 DES_LONG DES_quad_cksum(const unsigned char *input,DES_cblock output[],
00231                         long length,int out_count,DES_cblock *seed);
00232 int DES_random_key(DES_cblock *ret);
00233 void DES_set_odd_parity(DES_cblock *key);
00234 int DES_check_key_parity(const_DES_cblock *key);
00235 int DES_is_weak_key(const_DES_cblock *key);
00236 /* DES_set_key (= set_key = DES_key_sched = key_sched) calls
00237  * DES_set_key_checked if global variable DES_check_key is set,
00238  * DES_set_key_unchecked otherwise. */
00239 int DES_set_key(const_DES_cblock *key,DES_key_schedule *schedule);
00240 int DES_key_sched(const_DES_cblock *key,DES_key_schedule *schedule);
00241 int DES_set_key_checked(const_DES_cblock *key,DES_key_schedule *schedule);
00242 void DES_set_key_unchecked(const_DES_cblock *key,DES_key_schedule *schedule);
00243 void DES_string_to_key(const char *str,DES_cblock *key);
00244 void DES_string_to_2keys(const char *str,DES_cblock *key1,DES_cblock *key2);
00245 void DES_cfb64_encrypt(const unsigned char *in,unsigned char *out,long length,
00246                        DES_key_schedule *schedule,DES_cblock *ivec,int *num,
00247                        int enc);
00248 void DES_ofb64_encrypt(const unsigned char *in,unsigned char *out,long length,
00249                        DES_key_schedule *schedule,DES_cblock *ivec,int *num);
00250 
00251 int DES_read_password(DES_cblock *key, const char *prompt, int verify);
00252 int DES_read_2passwords(DES_cblock *key1, DES_cblock *key2, const char *prompt,
00253         int verify);
00254 
00255 #define DES_fixup_key_parity DES_set_odd_parity
00256 
00257 #ifdef  __cplusplus
00258 }
00259 #endif
00260 
00261 #endif
00262 
00263 #endif /* POK_NEEDS_ ...*/