1*4724848cSchristos=pod 2*4724848cSchristos 3*4724848cSchristos=head1 NAME 4*4724848cSchristos 5*4724848cSchristosDES_random_key, DES_set_key, DES_key_sched, DES_set_key_checked, 6*4724848cSchristosDES_set_key_unchecked, DES_set_odd_parity, DES_is_weak_key, 7*4724848cSchristosDES_ecb_encrypt, DES_ecb2_encrypt, DES_ecb3_encrypt, DES_ncbc_encrypt, 8*4724848cSchristosDES_cfb_encrypt, DES_ofb_encrypt, DES_pcbc_encrypt, DES_cfb64_encrypt, 9*4724848cSchristosDES_ofb64_encrypt, DES_xcbc_encrypt, DES_ede2_cbc_encrypt, 10*4724848cSchristosDES_ede2_cfb64_encrypt, DES_ede2_ofb64_encrypt, DES_ede3_cbc_encrypt, 11*4724848cSchristosDES_ede3_cfb64_encrypt, DES_ede3_ofb64_encrypt, 12*4724848cSchristosDES_cbc_cksum, DES_quad_cksum, DES_string_to_key, DES_string_to_2keys, 13*4724848cSchristosDES_fcrypt, DES_crypt - DES encryption 14*4724848cSchristos 15*4724848cSchristos=head1 SYNOPSIS 16*4724848cSchristos 17*4724848cSchristos #include <openssl/des.h> 18*4724848cSchristos 19*4724848cSchristos void DES_random_key(DES_cblock *ret); 20*4724848cSchristos 21*4724848cSchristos int DES_set_key(const_DES_cblock *key, DES_key_schedule *schedule); 22*4724848cSchristos int DES_key_sched(const_DES_cblock *key, DES_key_schedule *schedule); 23*4724848cSchristos int DES_set_key_checked(const_DES_cblock *key, DES_key_schedule *schedule); 24*4724848cSchristos void DES_set_key_unchecked(const_DES_cblock *key, DES_key_schedule *schedule); 25*4724848cSchristos 26*4724848cSchristos void DES_set_odd_parity(DES_cblock *key); 27*4724848cSchristos int DES_is_weak_key(const_DES_cblock *key); 28*4724848cSchristos 29*4724848cSchristos void DES_ecb_encrypt(const_DES_cblock *input, DES_cblock *output, 30*4724848cSchristos DES_key_schedule *ks, int enc); 31*4724848cSchristos void DES_ecb2_encrypt(const_DES_cblock *input, DES_cblock *output, 32*4724848cSchristos DES_key_schedule *ks1, DES_key_schedule *ks2, int enc); 33*4724848cSchristos void DES_ecb3_encrypt(const_DES_cblock *input, DES_cblock *output, 34*4724848cSchristos DES_key_schedule *ks1, DES_key_schedule *ks2, 35*4724848cSchristos DES_key_schedule *ks3, int enc); 36*4724848cSchristos 37*4724848cSchristos void DES_ncbc_encrypt(const unsigned char *input, unsigned char *output, 38*4724848cSchristos long length, DES_key_schedule *schedule, DES_cblock *ivec, 39*4724848cSchristos int enc); 40*4724848cSchristos void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, 41*4724848cSchristos int numbits, long length, DES_key_schedule *schedule, 42*4724848cSchristos DES_cblock *ivec, int enc); 43*4724848cSchristos void DES_ofb_encrypt(const unsigned char *in, unsigned char *out, 44*4724848cSchristos int numbits, long length, DES_key_schedule *schedule, 45*4724848cSchristos DES_cblock *ivec); 46*4724848cSchristos void DES_pcbc_encrypt(const unsigned char *input, unsigned char *output, 47*4724848cSchristos long length, DES_key_schedule *schedule, DES_cblock *ivec, 48*4724848cSchristos int enc); 49*4724848cSchristos void DES_cfb64_encrypt(const unsigned char *in, unsigned char *out, 50*4724848cSchristos long length, DES_key_schedule *schedule, DES_cblock *ivec, 51*4724848cSchristos int *num, int enc); 52*4724848cSchristos void DES_ofb64_encrypt(const unsigned char *in, unsigned char *out, 53*4724848cSchristos long length, DES_key_schedule *schedule, DES_cblock *ivec, 54*4724848cSchristos int *num); 55*4724848cSchristos 56*4724848cSchristos void DES_xcbc_encrypt(const unsigned char *input, unsigned char *output, 57*4724848cSchristos long length, DES_key_schedule *schedule, DES_cblock *ivec, 58*4724848cSchristos const_DES_cblock *inw, const_DES_cblock *outw, int enc); 59*4724848cSchristos 60*4724848cSchristos void DES_ede2_cbc_encrypt(const unsigned char *input, unsigned char *output, 61*4724848cSchristos long length, DES_key_schedule *ks1, 62*4724848cSchristos DES_key_schedule *ks2, DES_cblock *ivec, int enc); 63*4724848cSchristos void DES_ede2_cfb64_encrypt(const unsigned char *in, unsigned char *out, 64*4724848cSchristos long length, DES_key_schedule *ks1, 65*4724848cSchristos DES_key_schedule *ks2, DES_cblock *ivec, 66*4724848cSchristos int *num, int enc); 67*4724848cSchristos void DES_ede2_ofb64_encrypt(const unsigned char *in, unsigned char *out, 68*4724848cSchristos long length, DES_key_schedule *ks1, 69*4724848cSchristos DES_key_schedule *ks2, DES_cblock *ivec, int *num); 70*4724848cSchristos 71*4724848cSchristos void DES_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output, 72*4724848cSchristos long length, DES_key_schedule *ks1, 73*4724848cSchristos DES_key_schedule *ks2, DES_key_schedule *ks3, 74*4724848cSchristos DES_cblock *ivec, int enc); 75*4724848cSchristos void DES_ede3_cfb64_encrypt(const unsigned char *in, unsigned char *out, 76*4724848cSchristos long length, DES_key_schedule *ks1, 77*4724848cSchristos DES_key_schedule *ks2, DES_key_schedule *ks3, 78*4724848cSchristos DES_cblock *ivec, int *num, int enc); 79*4724848cSchristos void DES_ede3_ofb64_encrypt(const unsigned char *in, unsigned char *out, 80*4724848cSchristos long length, DES_key_schedule *ks1, 81*4724848cSchristos DES_key_schedule *ks2, DES_key_schedule *ks3, 82*4724848cSchristos DES_cblock *ivec, int *num); 83*4724848cSchristos 84*4724848cSchristos DES_LONG DES_cbc_cksum(const unsigned char *input, DES_cblock *output, 85*4724848cSchristos long length, DES_key_schedule *schedule, 86*4724848cSchristos const_DES_cblock *ivec); 87*4724848cSchristos DES_LONG DES_quad_cksum(const unsigned char *input, DES_cblock output[], 88*4724848cSchristos long length, int out_count, DES_cblock *seed); 89*4724848cSchristos void DES_string_to_key(const char *str, DES_cblock *key); 90*4724848cSchristos void DES_string_to_2keys(const char *str, DES_cblock *key1, DES_cblock *key2); 91*4724848cSchristos 92*4724848cSchristos char *DES_fcrypt(const char *buf, const char *salt, char *ret); 93*4724848cSchristos char *DES_crypt(const char *buf, const char *salt); 94*4724848cSchristos 95*4724848cSchristos=head1 DESCRIPTION 96*4724848cSchristos 97*4724848cSchristosThis library contains a fast implementation of the DES encryption 98*4724848cSchristosalgorithm. 99*4724848cSchristos 100*4724848cSchristosThere are two phases to the use of DES encryption. The first is the 101*4724848cSchristosgeneration of a I<DES_key_schedule> from a key, the second is the 102*4724848cSchristosactual encryption. A DES key is of type I<DES_cblock>. This type 103*4724848cSchristosconsists of 8 bytes with odd parity. The least significant bit in 104*4724848cSchristoseach byte is the parity bit. The key schedule is an expanded form of 105*4724848cSchristosthe key; it is used to speed the encryption process. 106*4724848cSchristos 107*4724848cSchristosDES_random_key() generates a random key. The random generator must be 108*4724848cSchristosseeded when calling this function. 109*4724848cSchristosIf the automatic seeding or reseeding of the OpenSSL CSPRNG fails due to 110*4724848cSchristosexternal circumstances (see L<RAND(7)>), the operation will fail. 111*4724848cSchristosIf the function fails, 0 is returned. 112*4724848cSchristos 113*4724848cSchristosBefore a DES key can be used, it must be converted into the 114*4724848cSchristosarchitecture dependent I<DES_key_schedule> via the 115*4724848cSchristosDES_set_key_checked() or DES_set_key_unchecked() function. 116*4724848cSchristos 117*4724848cSchristosDES_set_key_checked() will check that the key passed is of odd parity 118*4724848cSchristosand is not a weak or semi-weak key. If the parity is wrong, then -1 119*4724848cSchristosis returned. If the key is a weak key, then -2 is returned. If an 120*4724848cSchristoserror is returned, the key schedule is not generated. 121*4724848cSchristos 122*4724848cSchristosDES_set_key() works like 123*4724848cSchristosDES_set_key_checked() if the I<DES_check_key> flag is nonzero, 124*4724848cSchristosotherwise like DES_set_key_unchecked(). These functions are available 125*4724848cSchristosfor compatibility; it is recommended to use a function that does not 126*4724848cSchristosdepend on a global variable. 127*4724848cSchristos 128*4724848cSchristosDES_set_odd_parity() sets the parity of the passed I<key> to odd. 129*4724848cSchristos 130*4724848cSchristosDES_is_weak_key() returns 1 if the passed key is a weak key, 0 if it 131*4724848cSchristosis ok. 132*4724848cSchristos 133*4724848cSchristosThe following routines mostly operate on an input and output stream of 134*4724848cSchristosI<DES_cblock>s. 135*4724848cSchristos 136*4724848cSchristosDES_ecb_encrypt() is the basic DES encryption routine that encrypts or 137*4724848cSchristosdecrypts a single 8-byte I<DES_cblock> in I<electronic code book> 138*4724848cSchristos(ECB) mode. It always transforms the input data, pointed to by 139*4724848cSchristosI<input>, into the output data, pointed to by the I<output> argument. 140*4724848cSchristosIf the I<encrypt> argument is nonzero (DES_ENCRYPT), the I<input> 141*4724848cSchristos(cleartext) is encrypted in to the I<output> (ciphertext) using the 142*4724848cSchristoskey_schedule specified by the I<schedule> argument, previously set via 143*4724848cSchristosI<DES_set_key>. If I<encrypt> is zero (DES_DECRYPT), the I<input> (now 144*4724848cSchristosciphertext) is decrypted into the I<output> (now cleartext). Input 145*4724848cSchristosand output may overlap. DES_ecb_encrypt() does not return a value. 146*4724848cSchristos 147*4724848cSchristosDES_ecb3_encrypt() encrypts/decrypts the I<input> block by using 148*4724848cSchristosthree-key Triple-DES encryption in ECB mode. This involves encrypting 149*4724848cSchristosthe input with I<ks1>, decrypting with the key schedule I<ks2>, and 150*4724848cSchristosthen encrypting with I<ks3>. This routine greatly reduces the chances 151*4724848cSchristosof brute force breaking of DES and has the advantage of if I<ks1>, 152*4724848cSchristosI<ks2> and I<ks3> are the same, it is equivalent to just encryption 153*4724848cSchristosusing ECB mode and I<ks1> as the key. 154*4724848cSchristos 155*4724848cSchristosThe macro DES_ecb2_encrypt() is provided to perform two-key Triple-DES 156*4724848cSchristosencryption by using I<ks1> for the final encryption. 157*4724848cSchristos 158*4724848cSchristosDES_ncbc_encrypt() encrypts/decrypts using the I<cipher-block-chaining> 159*4724848cSchristos(CBC) mode of DES. If the I<encrypt> argument is nonzero, the 160*4724848cSchristosroutine cipher-block-chain encrypts the cleartext data pointed to by 161*4724848cSchristosthe I<input> argument into the ciphertext pointed to by the I<output> 162*4724848cSchristosargument, using the key schedule provided by the I<schedule> argument, 163*4724848cSchristosand initialization vector provided by the I<ivec> argument. If the 164*4724848cSchristosI<length> argument is not an integral multiple of eight bytes, the 165*4724848cSchristoslast block is copied to a temporary area and zero filled. The output 166*4724848cSchristosis always an integral multiple of eight bytes. 167*4724848cSchristos 168*4724848cSchristosDES_xcbc_encrypt() is RSA's DESX mode of DES. It uses I<inw> and 169*4724848cSchristosI<outw> to 'whiten' the encryption. I<inw> and I<outw> are secret 170*4724848cSchristos(unlike the iv) and are as such, part of the key. So the key is sort 171*4724848cSchristosof 24 bytes. This is much better than CBC DES. 172*4724848cSchristos 173*4724848cSchristosDES_ede3_cbc_encrypt() implements outer triple CBC DES encryption with 174*4724848cSchristosthree keys. This means that each DES operation inside the CBC mode is 175*4724848cSchristosC<C=E(ks3,D(ks2,E(ks1,M)))>. This mode is used by SSL. 176*4724848cSchristos 177*4724848cSchristosThe DES_ede2_cbc_encrypt() macro implements two-key Triple-DES by 178*4724848cSchristosreusing I<ks1> for the final encryption. C<C=E(ks1,D(ks2,E(ks1,M)))>. 179*4724848cSchristosThis form of Triple-DES is used by the RSAREF library. 180*4724848cSchristos 181*4724848cSchristosDES_pcbc_encrypt() encrypts/decrypts using the propagating cipher block 182*4724848cSchristoschaining mode used by Kerberos v4. Its parameters are the same as 183*4724848cSchristosDES_ncbc_encrypt(). 184*4724848cSchristos 185*4724848cSchristosDES_cfb_encrypt() encrypts/decrypts using cipher feedback mode. This 186*4724848cSchristosmethod takes an array of characters as input and outputs an array of 187*4724848cSchristoscharacters. It does not require any padding to 8 character groups. 188*4724848cSchristosNote: the I<ivec> variable is changed and the new changed value needs to 189*4724848cSchristosbe passed to the next call to this function. Since this function runs 190*4724848cSchristosa complete DES ECB encryption per I<numbits>, this function is only 191*4724848cSchristossuggested for use when sending a small number of characters. 192*4724848cSchristos 193*4724848cSchristosDES_cfb64_encrypt() 194*4724848cSchristosimplements CFB mode of DES with 64-bit feedback. Why is this 195*4724848cSchristosuseful you ask? Because this routine will allow you to encrypt an 196*4724848cSchristosarbitrary number of bytes, without 8 byte padding. Each call to this 197*4724848cSchristosroutine will encrypt the input bytes to output and then update ivec 198*4724848cSchristosand num. num contains 'how far' we are though ivec. If this does 199*4724848cSchristosnot make much sense, read more about CFB mode of DES. 200*4724848cSchristos 201*4724848cSchristosDES_ede3_cfb64_encrypt() and DES_ede2_cfb64_encrypt() is the same as 202*4724848cSchristosDES_cfb64_encrypt() except that Triple-DES is used. 203*4724848cSchristos 204*4724848cSchristosDES_ofb_encrypt() encrypts using output feedback mode. This method 205*4724848cSchristostakes an array of characters as input and outputs an array of 206*4724848cSchristoscharacters. It does not require any padding to 8 character groups. 207*4724848cSchristosNote: the I<ivec> variable is changed and the new changed value needs to 208*4724848cSchristosbe passed to the next call to this function. Since this function runs 209*4724848cSchristosa complete DES ECB encryption per I<numbits>, this function is only 210*4724848cSchristossuggested for use when sending a small number of characters. 211*4724848cSchristos 212*4724848cSchristosDES_ofb64_encrypt() is the same as DES_cfb64_encrypt() using Output 213*4724848cSchristosFeed Back mode. 214*4724848cSchristos 215*4724848cSchristosDES_ede3_ofb64_encrypt() and DES_ede2_ofb64_encrypt() is the same as 216*4724848cSchristosDES_ofb64_encrypt(), using Triple-DES. 217*4724848cSchristos 218*4724848cSchristosThe following functions are included in the DES library for 219*4724848cSchristoscompatibility with the MIT Kerberos library. 220*4724848cSchristos 221*4724848cSchristosDES_cbc_cksum() produces an 8 byte checksum based on the input stream 222*4724848cSchristos(via CBC encryption). The last 4 bytes of the checksum are returned 223*4724848cSchristosand the complete 8 bytes are placed in I<output>. This function is 224*4724848cSchristosused by Kerberos v4. Other applications should use 225*4724848cSchristosL<EVP_DigestInit(3)> etc. instead. 226*4724848cSchristos 227*4724848cSchristosDES_quad_cksum() is a Kerberos v4 function. It returns a 4 byte 228*4724848cSchristoschecksum from the input bytes. The algorithm can be iterated over the 229*4724848cSchristosinput, depending on I<out_count>, 1, 2, 3 or 4 times. If I<output> is 230*4724848cSchristosnon-NULL, the 8 bytes generated by each pass are written into 231*4724848cSchristosI<output>. 232*4724848cSchristos 233*4724848cSchristosThe following are DES-based transformations: 234*4724848cSchristos 235*4724848cSchristosDES_fcrypt() is a fast version of the Unix crypt(3) function. This 236*4724848cSchristosversion takes only a small amount of space relative to other fast 237*4724848cSchristoscrypt() implementations. This is different to the normal crypt() in 238*4724848cSchristosthat the third parameter is the buffer that the return value is 239*4724848cSchristoswritten into. It needs to be at least 14 bytes long. This function 240*4724848cSchristosis thread safe, unlike the normal crypt(). 241*4724848cSchristos 242*4724848cSchristosDES_crypt() is a faster replacement for the normal system crypt(). 243*4724848cSchristosThis function calls DES_fcrypt() with a static array passed as the 244*4724848cSchristosthird parameter. This mostly emulates the normal non-thread-safe semantics 245*4724848cSchristosof crypt(3). 246*4724848cSchristosThe B<salt> must be two ASCII characters. 247*4724848cSchristos 248*4724848cSchristosThe values returned by DES_fcrypt() and DES_crypt() are terminated by NUL 249*4724848cSchristoscharacter. 250*4724848cSchristos 251*4724848cSchristosDES_enc_write() writes I<len> bytes to file descriptor I<fd> from 252*4724848cSchristosbuffer I<buf>. The data is encrypted via I<pcbc_encrypt> (default) 253*4724848cSchristosusing I<sched> for the key and I<iv> as a starting vector. The actual 254*4724848cSchristosdata send down I<fd> consists of 4 bytes (in network byte order) 255*4724848cSchristoscontaining the length of the following encrypted data. The encrypted 256*4724848cSchristosdata then follows, padded with random data out to a multiple of 8 257*4724848cSchristosbytes. 258*4724848cSchristos 259*4724848cSchristos=head1 BUGS 260*4724848cSchristos 261*4724848cSchristosDES_cbc_encrypt() does not modify B<ivec>; use DES_ncbc_encrypt() 262*4724848cSchristosinstead. 263*4724848cSchristos 264*4724848cSchristosDES_cfb_encrypt() and DES_ofb_encrypt() operates on input of 8 bits. 265*4724848cSchristosWhat this means is that if you set numbits to 12, and length to 2, the 266*4724848cSchristosfirst 12 bits will come from the 1st input byte and the low half of 267*4724848cSchristosthe second input byte. The second 12 bits will have the low 8 bits 268*4724848cSchristostaken from the 3rd input byte and the top 4 bits taken from the 4th 269*4724848cSchristosinput byte. The same holds for output. This function has been 270*4724848cSchristosimplemented this way because most people will be using a multiple of 8 271*4724848cSchristosand because once you get into pulling bytes input bytes apart things 272*4724848cSchristosget ugly! 273*4724848cSchristos 274*4724848cSchristosDES_string_to_key() is available for backward compatibility with the 275*4724848cSchristosMIT library. New applications should use a cryptographic hash function. 276*4724848cSchristosThe same applies for DES_string_to_2key(). 277*4724848cSchristos 278*4724848cSchristos=head1 NOTES 279*4724848cSchristos 280*4724848cSchristosThe B<des> library was written to be source code compatible with 281*4724848cSchristosthe MIT Kerberos library. 282*4724848cSchristos 283*4724848cSchristosApplications should use the higher level functions 284*4724848cSchristosL<EVP_EncryptInit(3)> etc. instead of calling these 285*4724848cSchristosfunctions directly. 286*4724848cSchristos 287*4724848cSchristosSingle-key DES is insecure due to its short key size. ECB mode is 288*4724848cSchristosnot suitable for most applications; see L<des_modes(7)>. 289*4724848cSchristos 290*4724848cSchristos=head1 RETURN VALUES 291*4724848cSchristos 292*4724848cSchristosDES_set_key(), DES_key_sched(), DES_set_key_checked() and DES_is_weak_key() 293*4724848cSchristosreturn 0 on success or negative values on error. 294*4724848cSchristos 295*4724848cSchristosDES_cbc_cksum() and DES_quad_cksum() return 4-byte integer representing the 296*4724848cSchristoslast 4 bytes of the checksum of the input. 297*4724848cSchristos 298*4724848cSchristosDES_fcrypt() returns a pointer to the caller-provided buffer and DES_crypt() - 299*4724848cSchristosto a static buffer on success; otherwise they return NULL. 300*4724848cSchristos 301*4724848cSchristos=head1 SEE ALSO 302*4724848cSchristos 303*4724848cSchristosL<des_modes(7)>, 304*4724848cSchristosL<EVP_EncryptInit(3)> 305*4724848cSchristos 306*4724848cSchristos=head1 HISTORY 307*4724848cSchristos 308*4724848cSchristosThe requirement that the B<salt> parameter to DES_crypt() and DES_fcrypt() 309*4724848cSchristosbe two ASCII characters was first enforced in 310*4724848cSchristosOpenSSL 1.1.0. Previous versions tried to use the letter uppercase B<A> 311*4724848cSchristosif both character were not present, and could crash when given non-ASCII 312*4724848cSchristoson some platforms. 313*4724848cSchristos 314*4724848cSchristos=head1 COPYRIGHT 315*4724848cSchristos 316*4724848cSchristosCopyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved. 317*4724848cSchristos 318*4724848cSchristosLicensed under the OpenSSL license (the "License"). You may not use 319*4724848cSchristosthis file except in compliance with the License. You can obtain a copy 320*4724848cSchristosin the file LICENSE in the source distribution or at 321*4724848cSchristosL<https://www.openssl.org/source/license.html>. 322*4724848cSchristos 323*4724848cSchristos=cut 324