159191Skris=pod 259191Skris 359191Skris=head1 NAME 459191Skris 5109998SmarkmDES_random_key, DES_set_key, DES_key_sched, DES_set_key_checked, 6109998SmarkmDES_set_key_unchecked, DES_set_odd_parity, DES_is_weak_key, 7109998SmarkmDES_ecb_encrypt, DES_ecb2_encrypt, DES_ecb3_encrypt, DES_ncbc_encrypt, 8109998SmarkmDES_cfb_encrypt, DES_ofb_encrypt, DES_pcbc_encrypt, DES_cfb64_encrypt, 9109998SmarkmDES_ofb64_encrypt, DES_xcbc_encrypt, DES_ede2_cbc_encrypt, 10109998SmarkmDES_ede2_cfb64_encrypt, DES_ede2_ofb64_encrypt, DES_ede3_cbc_encrypt, 11109998SmarkmDES_ede3_cbcm_encrypt, DES_ede3_cfb64_encrypt, DES_ede3_ofb64_encrypt, 12109998SmarkmDES_cbc_cksum, DES_quad_cksum, DES_string_to_key, DES_string_to_2keys, 13109998SmarkmDES_fcrypt, DES_crypt, DES_enc_read, DES_enc_write - DES encryption 1459191Skris 1559191Skris=head1 SYNOPSIS 1659191Skris 1759191Skris #include <openssl/des.h> 1859191Skris 19109998Smarkm void DES_random_key(DES_cblock *ret); 2059191Skris 21109998Smarkm int DES_set_key(const_DES_cblock *key, DES_key_schedule *schedule); 22109998Smarkm int DES_key_sched(const_DES_cblock *key, DES_key_schedule *schedule); 23109998Smarkm int DES_set_key_checked(const_DES_cblock *key, 24109998Smarkm DES_key_schedule *schedule); 25109998Smarkm void DES_set_key_unchecked(const_DES_cblock *key, 26109998Smarkm DES_key_schedule *schedule); 2759191Skris 28109998Smarkm void DES_set_odd_parity(DES_cblock *key); 29109998Smarkm int DES_is_weak_key(const_DES_cblock *key); 3059191Skris 31109998Smarkm void DES_ecb_encrypt(const_DES_cblock *input, DES_cblock *output, 32109998Smarkm DES_key_schedule *ks, int enc); 33109998Smarkm void DES_ecb2_encrypt(const_DES_cblock *input, DES_cblock *output, 34109998Smarkm DES_key_schedule *ks1, DES_key_schedule *ks2, int enc); 35109998Smarkm void DES_ecb3_encrypt(const_DES_cblock *input, DES_cblock *output, 36109998Smarkm DES_key_schedule *ks1, DES_key_schedule *ks2, 37109998Smarkm DES_key_schedule *ks3, int enc); 3859191Skris 39109998Smarkm void DES_ncbc_encrypt(const unsigned char *input, unsigned char *output, 40109998Smarkm long length, DES_key_schedule *schedule, DES_cblock *ivec, 4159191Skris int enc); 42109998Smarkm void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, 43109998Smarkm int numbits, long length, DES_key_schedule *schedule, 44109998Smarkm DES_cblock *ivec, int enc); 45109998Smarkm void DES_ofb_encrypt(const unsigned char *in, unsigned char *out, 46109998Smarkm int numbits, long length, DES_key_schedule *schedule, 47109998Smarkm DES_cblock *ivec); 48109998Smarkm void DES_pcbc_encrypt(const unsigned char *input, unsigned char *output, 49109998Smarkm long length, DES_key_schedule *schedule, DES_cblock *ivec, 5059191Skris int enc); 51109998Smarkm void DES_cfb64_encrypt(const unsigned char *in, unsigned char *out, 52109998Smarkm long length, DES_key_schedule *schedule, DES_cblock *ivec, 5359191Skris int *num, int enc); 54109998Smarkm void DES_ofb64_encrypt(const unsigned char *in, unsigned char *out, 55109998Smarkm long length, DES_key_schedule *schedule, DES_cblock *ivec, 5659191Skris int *num); 5759191Skris 58109998Smarkm void DES_xcbc_encrypt(const unsigned char *input, unsigned char *output, 59109998Smarkm long length, DES_key_schedule *schedule, DES_cblock *ivec, 60109998Smarkm const_DES_cblock *inw, const_DES_cblock *outw, int enc); 6159191Skris 62109998Smarkm void DES_ede2_cbc_encrypt(const unsigned char *input, 63109998Smarkm unsigned char *output, long length, DES_key_schedule *ks1, 64109998Smarkm DES_key_schedule *ks2, DES_cblock *ivec, int enc); 65109998Smarkm void DES_ede2_cfb64_encrypt(const unsigned char *in, 66109998Smarkm unsigned char *out, long length, DES_key_schedule *ks1, 67109998Smarkm DES_key_schedule *ks2, DES_cblock *ivec, int *num, int enc); 68109998Smarkm void DES_ede2_ofb64_encrypt(const unsigned char *in, 69109998Smarkm unsigned char *out, long length, DES_key_schedule *ks1, 70109998Smarkm DES_key_schedule *ks2, DES_cblock *ivec, int *num); 7159191Skris 72109998Smarkm void DES_ede3_cbc_encrypt(const unsigned char *input, 73109998Smarkm unsigned char *output, long length, DES_key_schedule *ks1, 74109998Smarkm DES_key_schedule *ks2, DES_key_schedule *ks3, DES_cblock *ivec, 7559191Skris int enc); 76109998Smarkm void DES_ede3_cbcm_encrypt(const unsigned char *in, unsigned char *out, 77109998Smarkm long length, DES_key_schedule *ks1, DES_key_schedule *ks2, 78109998Smarkm DES_key_schedule *ks3, DES_cblock *ivec1, DES_cblock *ivec2, 7959191Skris int enc); 80109998Smarkm void DES_ede3_cfb64_encrypt(const unsigned char *in, unsigned char *out, 81109998Smarkm long length, DES_key_schedule *ks1, DES_key_schedule *ks2, 82109998Smarkm DES_key_schedule *ks3, DES_cblock *ivec, int *num, int enc); 83109998Smarkm void DES_ede3_ofb64_encrypt(const unsigned char *in, unsigned char *out, 84109998Smarkm long length, DES_key_schedule *ks1, 85109998Smarkm DES_key_schedule *ks2, DES_key_schedule *ks3, 86109998Smarkm DES_cblock *ivec, int *num); 8759191Skris 88109998Smarkm DES_LONG DES_cbc_cksum(const unsigned char *input, DES_cblock *output, 89109998Smarkm long length, DES_key_schedule *schedule, 90109998Smarkm const_DES_cblock *ivec); 91109998Smarkm DES_LONG DES_quad_cksum(const unsigned char *input, DES_cblock output[], 92109998Smarkm long length, int out_count, DES_cblock *seed); 93109998Smarkm void DES_string_to_key(const char *str, DES_cblock *key); 94109998Smarkm void DES_string_to_2keys(const char *str, DES_cblock *key1, 95109998Smarkm DES_cblock *key2); 9659191Skris 97109998Smarkm char *DES_fcrypt(const char *buf, const char *salt, char *ret); 98109998Smarkm char *DES_crypt(const char *buf, const char *salt); 9959191Skris 100109998Smarkm int DES_enc_read(int fd, void *buf, int len, DES_key_schedule *sched, 101109998Smarkm DES_cblock *iv); 102109998Smarkm int DES_enc_write(int fd, const void *buf, int len, 103109998Smarkm DES_key_schedule *sched, DES_cblock *iv); 10459191Skris 10559191Skris=head1 DESCRIPTION 10659191Skris 10759191SkrisThis library contains a fast implementation of the DES encryption 10859191Skrisalgorithm. 10959191Skris 11059191SkrisThere are two phases to the use of DES encryption. The first is the 111109998Smarkmgeneration of a I<DES_key_schedule> from a key, the second is the 112109998Smarkmactual encryption. A DES key is of type I<DES_cblock>. This type is 11359191Skrisconsists of 8 bytes with odd parity. The least significant bit in 11459191Skriseach byte is the parity bit. The key schedule is an expanded form of 11559191Skristhe key; it is used to speed the encryption process. 11659191Skris 117109998SmarkmDES_random_key() generates a random key. The PRNG must be seeded 118109998Smarkmprior to using this function (see L<rand(3)|rand(3)>). If the PRNG 119109998Smarkmcould not generate a secure key, 0 is returned. 12059191Skris 12159191SkrisBefore a DES key can be used, it must be converted into the 122109998Smarkmarchitecture dependent I<DES_key_schedule> via the 123109998SmarkmDES_set_key_checked() or DES_set_key_unchecked() function. 12459191Skris 125109998SmarkmDES_set_key_checked() will check that the key passed is of odd parity 12659191Skrisand is not a week or semi-weak key. If the parity is wrong, then -1 12759191Skrisis returned. If the key is a weak key, then -2 is returned. If an 12859191Skriserror is returned, the key schedule is not generated. 12959191Skris 130109998SmarkmDES_set_key() works like 131109998SmarkmDES_set_key_checked() if the I<DES_check_key> flag is non-zero, 132109998Smarkmotherwise like DES_set_key_unchecked(). These functions are available 13359191Skrisfor compatibility; it is recommended to use a function that does not 13459191Skrisdepend on a global variable. 13559191Skris 136109998SmarkmDES_set_odd_parity() sets the parity of the passed I<key> to odd. 13759191Skris 138279265SdelphijDES_is_weak_key() returns 1 if the passed key is a weak key, 0 if it 139279265Sdelphijis ok. 14059191Skris 14159191SkrisThe following routines mostly operate on an input and output stream of 142109998SmarkmI<DES_cblock>s. 14359191Skris 144109998SmarkmDES_ecb_encrypt() is the basic DES encryption routine that encrypts or 145109998Smarkmdecrypts a single 8-byte I<DES_cblock> in I<electronic code book> 14659191Skris(ECB) mode. It always transforms the input data, pointed to by 14759191SkrisI<input>, into the output data, pointed to by the I<output> argument. 14859191SkrisIf the I<encrypt> argument is non-zero (DES_ENCRYPT), the I<input> 14959191Skris(cleartext) is encrypted in to the I<output> (ciphertext) using the 15059191Skriskey_schedule specified by the I<schedule> argument, previously set via 151109998SmarkmI<DES_set_key>. If I<encrypt> is zero (DES_DECRYPT), the I<input> (now 15259191Skrisciphertext) is decrypted into the I<output> (now cleartext). Input 153109998Smarkmand output may overlap. DES_ecb_encrypt() does not return a value. 15459191Skris 155109998SmarkmDES_ecb3_encrypt() encrypts/decrypts the I<input> block by using 15659191Skristhree-key Triple-DES encryption in ECB mode. This involves encrypting 15759191Skristhe input with I<ks1>, decrypting with the key schedule I<ks2>, and 15859191Skristhen encrypting with I<ks3>. This routine greatly reduces the chances 15959191Skrisof brute force breaking of DES and has the advantage of if I<ks1>, 16059191SkrisI<ks2> and I<ks3> are the same, it is equivalent to just encryption 16159191Skrisusing ECB mode and I<ks1> as the key. 16259191Skris 163109998SmarkmThe macro DES_ecb2_encrypt() is provided to perform two-key Triple-DES 16459191Skrisencryption by using I<ks1> for the final encryption. 16559191Skris 166109998SmarkmDES_ncbc_encrypt() encrypts/decrypts using the I<cipher-block-chaining> 16759191Skris(CBC) mode of DES. If the I<encrypt> argument is non-zero, the 16859191Skrisroutine cipher-block-chain encrypts the cleartext data pointed to by 16959191Skristhe I<input> argument into the ciphertext pointed to by the I<output> 17059191Skrisargument, using the key schedule provided by the I<schedule> argument, 17159191Skrisand initialization vector provided by the I<ivec> argument. If the 17259191SkrisI<length> argument is not an integral multiple of eight bytes, the 17359191Skrislast block is copied to a temporary area and zero filled. The output 17459191Skrisis always an integral multiple of eight bytes. 17559191Skris 176109998SmarkmDES_xcbc_encrypt() is RSA's DESX mode of DES. It uses I<inw> and 17759191SkrisI<outw> to 'whiten' the encryption. I<inw> and I<outw> are secret 17859191Skris(unlike the iv) and are as such, part of the key. So the key is sort 17959191Skrisof 24 bytes. This is much better than CBC DES. 18059191Skris 181109998SmarkmDES_ede3_cbc_encrypt() implements outer triple CBC DES encryption with 18259191Skristhree keys. This means that each DES operation inside the CBC mode is 183279265Sdelphijan C<C=E(ks3,D(ks2,E(ks1,M)))>. This mode is used by SSL. 18459191Skris 185109998SmarkmThe DES_ede2_cbc_encrypt() macro implements two-key Triple-DES by 18659191Skrisreusing I<ks1> for the final encryption. C<C=E(ks1,D(ks2,E(ks1,M)))>. 18759191SkrisThis form of Triple-DES is used by the RSAREF library. 18859191Skris 189109998SmarkmDES_pcbc_encrypt() encrypt/decrypts using the propagating cipher block 19068651Skrischaining mode used by Kerberos v4. Its parameters are the same as 191109998SmarkmDES_ncbc_encrypt(). 19259191Skris 193109998SmarkmDES_cfb_encrypt() encrypt/decrypts using cipher feedback mode. This 19459191Skrismethod takes an array of characters as input and outputs and array of 19559191Skrischaracters. It does not require any padding to 8 character groups. 19659191SkrisNote: the I<ivec> variable is changed and the new changed value needs to 19759191Skrisbe passed to the next call to this function. Since this function runs 19859191Skrisa complete DES ECB encryption per I<numbits>, this function is only 19959191Skrissuggested for use when sending small numbers of characters. 20059191Skris 201109998SmarkmDES_cfb64_encrypt() 20259191Skrisimplements CFB mode of DES with 64bit feedback. Why is this 20359191Skrisuseful you ask? Because this routine will allow you to encrypt an 20459191Skrisarbitrary number of bytes, no 8 byte padding. Each call to this 20559191Skrisroutine will encrypt the input bytes to output and then update ivec 20659191Skrisand num. num contains 'how far' we are though ivec. If this does 20759191Skrisnot make much sense, read more about cfb mode of DES :-). 20859191Skris 209109998SmarkmDES_ede3_cfb64_encrypt() and DES_ede2_cfb64_encrypt() is the same as 210109998SmarkmDES_cfb64_encrypt() except that Triple-DES is used. 21159191Skris 212109998SmarkmDES_ofb_encrypt() encrypts using output feedback mode. This method 21359191Skristakes an array of characters as input and outputs and array of 21459191Skrischaracters. It does not require any padding to 8 character groups. 21559191SkrisNote: the I<ivec> variable is changed and the new changed value needs to 21659191Skrisbe passed to the next call to this function. Since this function runs 21759191Skrisa complete DES ECB encryption per numbits, this function is only 21859191Skrissuggested for use when sending small numbers of characters. 21959191Skris 220109998SmarkmDES_ofb64_encrypt() is the same as DES_cfb64_encrypt() using Output 22159191SkrisFeed Back mode. 22259191Skris 223109998SmarkmDES_ede3_ofb64_encrypt() and DES_ede2_ofb64_encrypt() is the same as 224109998SmarkmDES_ofb64_encrypt(), using Triple-DES. 22559191Skris 22659191SkrisThe following functions are included in the DES library for 227109998Smarkmcompatibility with the MIT Kerberos library. 22859191Skris 229109998SmarkmDES_cbc_cksum() produces an 8 byte checksum based on the input stream 23059191Skris(via CBC encryption). The last 4 bytes of the checksum are returned 23159191Skrisand the complete 8 bytes are placed in I<output>. This function is 23259191Skrisused by Kerberos v4. Other applications should use 23359191SkrisL<EVP_DigestInit(3)|EVP_DigestInit(3)> etc. instead. 23459191Skris 235109998SmarkmDES_quad_cksum() is a Kerberos v4 function. It returns a 4 byte 23659191Skrischecksum from the input bytes. The algorithm can be iterated over the 23759191Skrisinput, depending on I<out_count>, 1, 2, 3 or 4 times. If I<output> is 23859191Skrisnon-NULL, the 8 bytes generated by each pass are written into 23959191SkrisI<output>. 24059191Skris 24168651SkrisThe following are DES-based transformations: 24259191Skris 243109998SmarkmDES_fcrypt() is a fast version of the Unix crypt(3) function. This 24459191Skrisversion takes only a small amount of space relative to other fast 24559191Skriscrypt() implementations. This is different to the normal crypt in 24659191Skristhat the third parameter is the buffer that the return value is 24759191Skriswritten into. It needs to be at least 14 bytes long. This function 24859191Skrisis thread safe, unlike the normal crypt. 24959191Skris 250109998SmarkmDES_crypt() is a faster replacement for the normal system crypt(). 251109998SmarkmThis function calls DES_fcrypt() with a static array passed as the 25259191Skristhird parameter. This emulates the normal non-thread safe semantics 25359191Skrisof crypt(3). 25459191Skris 255109998SmarkmDES_enc_write() writes I<len> bytes to file descriptor I<fd> from 25659191Skrisbuffer I<buf>. The data is encrypted via I<pcbc_encrypt> (default) 25759191Skrisusing I<sched> for the key and I<iv> as a starting vector. The actual 25859191Skrisdata send down I<fd> consists of 4 bytes (in network byte order) 25959191Skriscontaining the length of the following encrypted data. The encrypted 26059191Skrisdata then follows, padded with random data out to a multiple of 8 26159191Skrisbytes. 26259191Skris 263109998SmarkmDES_enc_read() is used to read I<len> bytes from file descriptor 26459191SkrisI<fd> into buffer I<buf>. The data being read from I<fd> is assumed to 265109998Smarkmhave come from DES_enc_write() and is decrypted using I<sched> for 26659191Skristhe key schedule and I<iv> for the initial vector. 26759191Skris 268109998SmarkmB<Warning:> The data format used by DES_enc_write() and DES_enc_read() 26959191Skrishas a cryptographic weakness: When asked to write more than MAXWRITE 270109998Smarkmbytes, DES_enc_write() will split the data into several chunks that 27159191Skrisare all encrypted using the same IV. So don't use these functions 27259191Skrisunless you are sure you know what you do (in which case you might not 27359191Skriswant to use them anyway). They cannot handle non-blocking sockets. 274109998SmarkmDES_enc_read() uses an internal state and thus cannot be used on 27559191Skrismultiple files. 27659191Skris 277109998SmarkmI<DES_rw_mode> is used to specify the encryption mode to use with 278109998SmarkmDES_enc_read() and DES_end_write(). If set to I<DES_PCBC_MODE> (the 279109998Smarkmdefault), DES_pcbc_encrypt is used. If set to I<DES_CBC_MODE> 280109998SmarkmDES_cbc_encrypt is used. 28159191Skris 28259191Skris=head1 NOTES 28359191Skris 28459191SkrisSingle-key DES is insecure due to its short key size. ECB mode is 285127128Snectarnot suitable for most applications; see L<des_modes(7)|des_modes(7)>. 28659191Skris 28759191SkrisThe L<evp(3)|evp(3)> library provides higher-level encryption functions. 28859191Skris 28959191Skris=head1 BUGS 29059191Skris 291109998SmarkmDES_3cbc_encrypt() is flawed and must not be used in applications. 29259191Skris 293109998SmarkmDES_cbc_encrypt() does not modify B<ivec>; use DES_ncbc_encrypt() 29459191Skrisinstead. 29559191Skris 296109998SmarkmDES_cfb_encrypt() and DES_ofb_encrypt() operates on input of 8 bits. 29759191SkrisWhat this means is that if you set numbits to 12, and length to 2, the 29859191Skrisfirst 12 bits will come from the 1st input byte and the low half of 29959191Skristhe second input byte. The second 12 bits will have the low 8 bits 30059191Skristaken from the 3rd input byte and the top 4 bits taken from the 4th 30159191Skrisinput byte. The same holds for output. This function has been 30259191Skrisimplemented this way because most people will be using a multiple of 8 30359191Skrisand because once you get into pulling bytes input bytes apart things 30459191Skrisget ugly! 30559191Skris 306109998SmarkmDES_string_to_key() is available for backward compatibility with the 307109998SmarkmMIT library. New applications should use a cryptographic hash function. 308109998SmarkmThe same applies for DES_string_to_2key(). 30959191Skris 31059191Skris=head1 CONFORMING TO 31159191Skris 31259191SkrisANSI X3.106 31359191Skris 31459191SkrisThe B<des> library was written to be source code compatible with 31559191Skristhe MIT Kerberos library. 31659191Skris 31759191Skris=head1 SEE ALSO 31859191Skris 31972613Skriscrypt(3), L<des_modes(7)|des_modes(7)>, L<evp(3)|evp(3)>, L<rand(3)|rand(3)> 32059191Skris 32159191Skris=head1 HISTORY 32259191Skris 323109998SmarkmIn OpenSSL 0.9.7, all des_ functions were renamed to DES_ to avoid 324109998Smarkmclashes with older versions of libdes. Compatibility des_ functions 325109998Smarkmare provided for a short while, as well as crypt(). 326109998SmarkmDeclarations for these are in <openssl/des_old.h>. There is no DES_ 327109998Smarkmvariant for des_random_seed(). 328109998SmarkmThis will happen to other functions 329109998Smarkmas well if they are deemed redundant (des_random_seed() just calls 330109998SmarkmRAND_seed() and is present for backward compatibility only), buggy or 331109998Smarkmalready scheduled for removal. 332109998Smarkm 33359191Skrisdes_cbc_cksum(), des_cbc_encrypt(), des_ecb_encrypt(), 33459191Skrisdes_is_weak_key(), des_key_sched(), des_pcbc_encrypt(), 335109998Smarkmdes_quad_cksum(), des_random_key() and des_string_to_key() 336109998Smarkmare available in the MIT Kerberos library; 33759191Skrisdes_check_key_parity(), des_fixup_key_parity() and des_is_weak_key() 33859191Skrisare available in newer versions of that library. 33959191Skris 34059191Skrisdes_set_key_checked() and des_set_key_unchecked() were added in 34159191SkrisOpenSSL 0.9.5. 34259191Skris 34359191Skrisdes_generate_random_block(), des_init_random_number_generator(), 34459191Skrisdes_new_random_key(), des_set_random_generator_seed() and 34559191Skrisdes_set_sequence_number() and des_rand_data() are used in newer 34659191Skrisversions of Kerberos but are not implemented here. 34759191Skris 34859191Skrisdes_random_key() generated cryptographically weak random data in 34959191SkrisSSLeay and in OpenSSL prior version 0.9.5, as well as in the original 35059191SkrisMIT library. 35159191Skris 35259191Skris=head1 AUTHOR 35359191Skris 35459191SkrisEric Young (eay@cryptsoft.com). Modified for the OpenSSL project 35559191Skris(http://www.openssl.org). 35659191Skris 35759191Skris=cut 358