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