1 /* $FreeBSD$ */ 2 /* $KAME: des.h,v 1.4 2000/06/14 10:41:17 itojun Exp $ */ 3 4 /* lib/des/des.h */ 5 /* Copyright (C) 1995-1996 Eric Young (eay@mincom.oz.au) 6 * All rights reserved. 7 * 8 * This file is part of an SSL implementation written 9 * by Eric Young (eay@mincom.oz.au). 10 * The implementation was written so as to conform with Netscapes SSL 11 * specification. This library and applications are 12 * FREE FOR COMMERCIAL AND NON-COMMERCIAL USE 13 * as long as the following conditions are aheared to. 14 * 15 * Copyright remains Eric Young's, and as such any Copyright notices in 16 * the code are not to be removed. If this code is used in a product, 17 * Eric Young should be given attribution as the author of the parts used. 18 * This can be in the form of a textual message at program startup or 19 * in documentation (online or textual) provided with the package. 20 * 21 * Redistribution and use in source and binary forms, with or without 22 * modification, are permitted provided that the following conditions 23 * are met: 24 * 1. Redistributions of source code must retain the copyright 25 * notice, this list of conditions and the following disclaimer. 26 * 2. Redistributions in binary form must reproduce the above copyright 27 * notice, this list of conditions and the following disclaimer in the 28 * documentation and/or other materials provided with the distribution. 29 * 3. All advertising materials mentioning features or use of this software 30 * must display the following acknowledgement: 31 * This product includes software developed by Eric Young (eay@mincom.oz.au) 32 * 33 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND 34 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 35 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 36 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 37 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 38 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 39 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 40 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 41 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 42 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 43 * SUCH DAMAGE. 44 * 45 * The licence and distribution terms for any publically available version or 46 * derivative of this code cannot be changed. i.e. this code cannot simply be 47 * copied and put under another distribution licence 48 * [including the GNU Public Licence.] 49 */ 50 51 #ifndef HEADER_DES_H 52 #define HEADER_DES_H 53 54 #ifdef __cplusplus 55 extern "C" { 56 #endif 57 58 /* If this is set to 'unsigned int' on a DEC Alpha, this gives about a 59 * %20 speed up (longs are 8 bytes, int's are 4). */ 60 #ifndef DES_LONG 61 #define DES_LONG unsigned long 62 #endif 63 64 typedef unsigned char des_cblock[8]; 65 typedef struct des_ks_struct 66 { 67 union { 68 des_cblock _; 69 /* make sure things are correct size on machines with 70 * 8 byte longs */ 71 DES_LONG pad[2]; 72 } ks; 73 #undef _ 74 #define _ ks._ 75 } des_key_schedule[16]; 76 77 #define DES_KEY_SZ (sizeof(des_cblock)) 78 #define DES_SCHEDULE_SZ (sizeof(des_key_schedule)) 79 80 #define DES_ENCRYPT 1 81 #define DES_DECRYPT 0 82 83 #define DES_CBC_MODE 0 84 #define DES_PCBC_MODE 1 85 86 #define des_ecb2_encrypt(i,o,k1,k2,e) \ 87 des_ecb3_encrypt((i),(o),(k1),(k2),(k1),(e)) 88 89 #define des_ede2_cbc_encrypt(i,o,l,k1,k2,iv,e) \ 90 des_ede3_cbc_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(e)) 91 92 #define des_ede2_cfb64_encrypt(i,o,l,k1,k2,iv,n,e) \ 93 des_ede3_cfb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n),(e)) 94 95 #define des_ede2_ofb64_encrypt(i,o,l,k1,k2,iv,n) \ 96 des_ede3_ofb64_encrypt((i),(o),(l),(k1),(k2),(k1),(iv),(n)) 97 98 #define C_Block des_cblock 99 #define Key_schedule des_key_schedule 100 #ifdef KERBEROS 101 #define ENCRYPT DES_ENCRYPT 102 #define DECRYPT DES_DECRYPT 103 #endif 104 #define KEY_SZ DES_KEY_SZ 105 #define string_to_key des_string_to_key 106 #define read_pw_string des_read_pw_string 107 #define random_key des_random_key 108 #define pcbc_encrypt des_pcbc_encrypt 109 #define set_key des_set_key 110 #define key_sched des_key_sched 111 #define ecb_encrypt des_ecb_encrypt 112 #define cbc_encrypt des_cbc_encrypt 113 #define ncbc_encrypt des_ncbc_encrypt 114 #define xcbc_encrypt des_xcbc_encrypt 115 #define cbc_cksum des_cbc_cksum 116 #define quad_cksum des_quad_cksum 117 118 /* For compatibility with the MIT lib - eay 20/05/92 */ 119 typedef des_key_schedule bit_64; 120 #define des_fixup_key_parity des_set_odd_parity 121 #define des_check_key_parity check_parity 122 123 extern int des_check_key; /* defaults to false */ 124 extern int des_rw_mode; /* defaults to DES_PCBC_MODE */ 125 126 /* The next line is used to disable full ANSI prototypes, if your 127 * compiler has problems with the prototypes, make sure this line always 128 * evaluates to true :-) */ 129 #if defined(MSDOS) || defined(__STDC__) 130 #undef NOPROTO 131 #endif 132 #ifndef NOPROTO 133 char *des_options(void); 134 void des_ecb3_encrypt(des_cblock *input,des_cblock *output, 135 des_key_schedule ks1,des_key_schedule ks2, 136 des_key_schedule ks3, int enc); 137 DES_LONG des_cbc_cksum(des_cblock *input,des_cblock *output, 138 long length,des_key_schedule schedule,des_cblock *ivec); 139 /* 140 void des_cbc_encrypt(des_cblock *input,des_cblock *output,long length, 141 des_key_schedule schedule,des_cblock *ivec,int enc); 142 */ 143 int des_cbc_encrypt(struct mbuf *, size_t, size_t, 144 des_key_schedule schedule,des_cblock *ivec, int enc); 145 void des_ncbc_encrypt(des_cblock *input,des_cblock *output,long length, 146 des_key_schedule schedule,des_cblock *ivec,int enc); 147 void des_xcbc_encrypt(des_cblock *input,des_cblock *output,long length, 148 des_key_schedule schedule,des_cblock *ivec, 149 des_cblock *inw,des_cblock *outw,int enc); 150 void des_3cbc_encrypt(des_cblock *input,des_cblock *output,long length, 151 des_key_schedule sk1,des_key_schedule sk2, 152 des_cblock *ivec1,des_cblock *ivec2,int enc); 153 extern int des_3cbc_process(struct mbuf *, size_t, size_t, 154 des_key_schedule *schedule, des_cblock *ivec, int mode); 155 void des_cfb_encrypt(unsigned char *in,unsigned char *out,int numbits, 156 long length,des_key_schedule schedule,des_cblock *ivec,int enc); 157 void des_ecb_encrypt(des_cblock *input,des_cblock *output, 158 des_key_schedule ks,int enc); 159 void des_encrypt(DES_LONG *data,des_key_schedule ks, int enc); 160 void des_encrypt2(DES_LONG *data,des_key_schedule ks, int enc); 161 void des_ede3_cbc_encrypt(des_cblock *input, des_cblock *output, 162 long length, des_key_schedule ks1, des_key_schedule ks2, 163 des_key_schedule ks3, des_cblock *ivec, int enc); 164 void des_ede3_cfb64_encrypt(unsigned char *in, unsigned char *out, 165 long length, des_key_schedule ks1, des_key_schedule ks2, 166 des_key_schedule ks3, des_cblock *ivec, int *num, int encrypt); 167 void des_ede3_ofb64_encrypt(unsigned char *in, unsigned char *out, 168 long length, des_key_schedule ks1, des_key_schedule ks2, 169 des_key_schedule ks3, des_cblock *ivec, int *num); 170 171 int des_enc_read(int fd,char *buf,int len,des_key_schedule sched, 172 des_cblock *iv); 173 int des_enc_write(int fd,char *buf,int len,des_key_schedule sched, 174 des_cblock *iv); 175 #ifdef PERL5 176 char *des_crypt(const char *buf,const char *salt); 177 #else 178 /* some stupid compilers complain because I have declared char instead 179 * of const char */ 180 #if 1 181 char *crypt(const char *buf,const char *salt); 182 #else 183 char *crypt(); 184 #endif 185 #endif 186 void des_ofb_encrypt(unsigned char *in,unsigned char *out, 187 int numbits,long length,des_key_schedule schedule,des_cblock *ivec); 188 void des_pcbc_encrypt(des_cblock *input,des_cblock *output,long length, 189 des_key_schedule schedule,des_cblock *ivec,int enc); 190 DES_LONG des_quad_cksum(des_cblock *input,des_cblock *output, 191 long length,int out_count,des_cblock *seed); 192 void des_random_seed(des_cblock key); 193 void des_random_key(des_cblock ret); 194 int des_read_password(des_cblock *key,char *prompt,int verify); 195 int des_read_2passwords(des_cblock *key1,des_cblock *key2, 196 char *prompt,int verify); 197 int des_read_pw_string(char *buf,int length,char *prompt,int verify); 198 void des_set_odd_parity(des_cblock *key); 199 int des_is_weak_key(des_cblock *key); 200 int des_set_key(des_cblock *key,des_key_schedule schedule); 201 int des_key_sched(des_cblock *key,des_key_schedule schedule); 202 void des_string_to_key(char *str,des_cblock *key); 203 void des_string_to_2keys(char *str,des_cblock *key1,des_cblock *key2); 204 void des_cfb64_encrypt(unsigned char *in, unsigned char *out, long length, 205 des_key_schedule schedule, des_cblock *ivec, int *num, int enc); 206 void des_ofb64_encrypt(unsigned char *in, unsigned char *out, long length, 207 des_key_schedule schedule, des_cblock *ivec, int *num); 208 209 /* Extra functions from Mark Murray <mark@grondar.za> */ 210 /* 211 void des_cblock_print_file(des_cblock *cb, FILE *fp); 212 */ 213 /* The following functions are not in the normal unix build or the 214 * SSLeay build. When using the SSLeay build, use RAND_seed() 215 * and RAND_bytes() instead. */ 216 int des_new_random_key(des_cblock *key); 217 void des_init_random_number_generator(des_cblock *key); 218 void des_set_random_generator_seed(des_cblock *key); 219 void des_set_sequence_number(des_cblock new_sequence_number); 220 void des_generate_random_block(des_cblock *block); 221 222 #else 223 224 char *des_options(); 225 void des_ecb3_encrypt(); 226 DES_LONG des_cbc_cksum(); 227 void des_cbc_encrypt(); 228 void des_ncbc_encrypt(); 229 void des_xcbc_encrypt(); 230 void des_3cbc_encrypt(); 231 void des_cfb_encrypt(); 232 void des_ede3_cfb64_encrypt(); 233 void des_ede3_ofb64_encrypt(); 234 void des_ecb_encrypt(); 235 void des_encrypt(); 236 void des_encrypt2(); 237 void des_ede3_cbc_encrypt(); 238 int des_enc_read(); 239 int des_enc_write(); 240 #ifdef PERL5 241 char *des_crypt(); 242 #else 243 char *crypt(); 244 #endif 245 void des_ofb_encrypt(); 246 void des_pcbc_encrypt(); 247 DES_LONG des_quad_cksum(); 248 void des_random_seed(); 249 void des_random_key(); 250 int des_read_password(); 251 int des_read_2passwords(); 252 int des_read_pw_string(); 253 void des_set_odd_parity(); 254 int des_is_weak_key(); 255 int des_set_key(); 256 int des_key_sched(); 257 void des_string_to_key(); 258 void des_string_to_2keys(); 259 void des_cfb64_encrypt(); 260 void des_ofb64_encrypt(); 261 262 /* Extra functions from Mark Murray <mark@grondar.za> */ 263 void des_cblock_print_file(); 264 /* The following functions are not in the normal unix build or the 265 * SSLeay build. When using the SSLeay build, use RAND_seed() 266 * and RAND_bytes() instead. */ 267 #ifdef FreeBSD 268 int des_new_random_key(); 269 void des_init_random_number_generator(); 270 void des_set_random_generator_seed(); 271 void des_set_sequence_number(); 272 void des_generate_random_block(); 273 #endif 274 275 #endif 276 277 #ifdef __cplusplus 278 } 279 #endif 280 281 #endif 282