des(3) OpenSSL des(3) NNAAMMEE DES_random_key, DES_set_key, DES_key_sched, DES_set_key_checked, DES_set_key_unchecked, DES_set_odd_parity, DES_is_weak_key, DES_ecb_encrypt, DES_ecb2_encrypt, DES_ecb3_encrypt, DES_ncbc_encrypt, DES_cfb_encrypt, DES_ofb_encrypt, DES_pcbc_encrypt, DES_cfb64_encrypt, DES_ofb64_encrypt, DES_xcbc_encrypt, DES_ede2_cbc_encrypt, DES_ede2_cfb64_encrypt, DES_ede2_ofb64_encrypt, DES_ede3_cbc_encrypt, DES_ede3_cbcm_encrypt, DES_ede3_cfb64_encrypt, DES_ede3_ofb64_encrypt, DES_cbc_cksum, DES_quad_cksum, DES_string_to_key, DES_string_to_2keys, DES_fcrypt, DES_crypt, DES_enc_read, DES_enc_write - DES encryption SSYYNNOOPPSSIISS #include void DES_random_key(DES_cblock *ret); int DES_set_key(const_DES_cblock *key, DES_key_schedule *schedule); int DES_key_sched(const_DES_cblock *key, DES_key_schedule *schedule); int DES_set_key_checked(const_DES_cblock *key, DES_key_schedule *schedule); void DES_set_key_unchecked(const_DES_cblock *key, DES_key_schedule *schedule); void DES_set_odd_parity(DES_cblock *key); int DES_is_weak_key(const_DES_cblock *key); void DES_ecb_encrypt(const_DES_cblock *input, DES_cblock *output, DES_key_schedule *ks, int enc); void DES_ecb2_encrypt(const_DES_cblock *input, DES_cblock *output, DES_key_schedule *ks1, DES_key_schedule *ks2, int enc); void DES_ecb3_encrypt(const_DES_cblock *input, DES_cblock *output, DES_key_schedule *ks1, DES_key_schedule *ks2, DES_key_schedule *ks3, int enc); void DES_ncbc_encrypt(const unsigned char *input, unsigned char *output, long length, DES_key_schedule *schedule, DES_cblock *ivec, int enc); void DES_cfb_encrypt(const unsigned char *in, unsigned char *out, int numbits, long length, DES_key_schedule *schedule, DES_cblock *ivec, int enc); void DES_ofb_encrypt(const unsigned char *in, unsigned char *out, int numbits, long length, DES_key_schedule *schedule, DES_cblock *ivec); void DES_pcbc_encrypt(const unsigned char *input, unsigned char *output, long length, DES_key_schedule *schedule, DES_cblock *ivec, int enc); void DES_cfb64_encrypt(const unsigned char *in, unsigned char *out, long length, DES_key_schedule *schedule, DES_cblock *ivec, int *num, int enc); void DES_ofb64_encrypt(const unsigned char *in, unsigned char *out, long length, DES_key_schedule *schedule, DES_cblock *ivec, int *num); void DES_xcbc_encrypt(const unsigned char *input, unsigned char *output, long length, DES_key_schedule *schedule, DES_cblock *ivec, const_DES_cblock *inw, const_DES_cblock *outw, int enc); void DES_ede2_cbc_encrypt(const unsigned char *input, unsigned char *output, long length, DES_key_schedule *ks1, DES_key_schedule *ks2, DES_cblock *ivec, int enc); void DES_ede2_cfb64_encrypt(const unsigned char *in, unsigned char *out, long length, DES_key_schedule *ks1, DES_key_schedule *ks2, DES_cblock *ivec, int *num, int enc); void DES_ede2_ofb64_encrypt(const unsigned char *in, unsigned char *out, long length, DES_key_schedule *ks1, DES_key_schedule *ks2, DES_cblock *ivec, int *num); void DES_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output, long length, DES_key_schedule *ks1, DES_key_schedule *ks2, DES_key_schedule *ks3, DES_cblock *ivec, int enc); void DES_ede3_cbcm_encrypt(const unsigned char *in, unsigned char *out, long length, DES_key_schedule *ks1, DES_key_schedule *ks2, DES_key_schedule *ks3, DES_cblock *ivec1, DES_cblock *ivec2, int enc); void DES_ede3_cfb64_encrypt(const unsigned char *in, unsigned char *out, long length, DES_key_schedule *ks1, DES_key_schedule *ks2, DES_key_schedule *ks3, DES_cblock *ivec, int *num, int enc); void DES_ede3_ofb64_encrypt(const unsigned char *in, unsigned char *out, long length, DES_key_schedule *ks1, DES_key_schedule *ks2, DES_key_schedule *ks3, DES_cblock *ivec, int *num); DES_LONG DES_cbc_cksum(const unsigned char *input, DES_cblock *output, long length, DES_key_schedule *schedule, const_DES_cblock *ivec); DES_LONG DES_quad_cksum(const unsigned char *input, DES_cblock output[], long length, int out_count, DES_cblock *seed); void DES_string_to_key(const char *str, DES_cblock *key); void DES_string_to_2keys(const char *str, DES_cblock *key1, DES_cblock *key2); char *DES_fcrypt(const char *buf, const char *salt, char *ret); char *DES_crypt(const char *buf, const char *salt); int DES_enc_read(int fd, void *buf, int len, DES_key_schedule *sched, DES_cblock *iv); int DES_enc_write(int fd, const void *buf, int len, DES_key_schedule *sched, DES_cblock *iv); DDEESSCCRRIIPPTTIIOONN This library contains a fast implementation of the DES encryption algo- rithm. There are two phases to the use of DES encryption. The first is the generation of a _D_E_S___k_e_y___s_c_h_e_d_u_l_e from a key, the second is the actual encryption. A DES key is of type _D_E_S___c_b_l_o_c_k. This type is consists of 8 bytes with odd parity. The least significant bit in each byte is the parity bit. The key schedule is an expanded form of the key; it is used to speed the encryption process. _D_E_S___r_a_n_d_o_m___k_e_y_(_) generates a random key. The PRNG must be seeded prior to using this function (see _r_a_n_d(3)). If the PRNG could not generate a secure key, 0 is returned. Before a DES key can be used, it must be converted into the architec- ture dependent _D_E_S___k_e_y___s_c_h_e_d_u_l_e via the _D_E_S___s_e_t___k_e_y___c_h_e_c_k_e_d_(_) or _D_E_S___s_e_t___k_e_y___u_n_c_h_e_c_k_e_d_(_) function. _D_E_S___s_e_t___k_e_y___c_h_e_c_k_e_d_(_) will check that the key passed is of odd parity and is not a weak or semi-weak key. If the parity is wrong, then -1 is returned. If the key is a weak key, then -2 is returned. If an error is returned, the key schedule is not generated. _D_E_S___s_e_t___k_e_y_(_) works like _D_E_S___s_e_t___k_e_y___c_h_e_c_k_e_d_(_) if the _D_E_S___c_h_e_c_k___k_e_y flag is non-zero, otherwise like _D_E_S___s_e_t___k_e_y___u_n_c_h_e_c_k_e_d_(_). These func- tions are available for compatibility; it is recommended to use a func- tion that does not depend on a global variable. _D_E_S___s_e_t___o_d_d___p_a_r_i_t_y_(_) sets the parity of the passed _k_e_y to odd. _D_E_S___i_s___w_e_a_k___k_e_y_(_) returns 1 if the passed key is a weak key, 0 if it is ok. The following routines mostly operate on an input and output stream of _D_E_S___c_b_l_o_c_ks. _D_E_S___e_c_b___e_n_c_r_y_p_t_(_) is the basic DES encryption routine that encrypts or decrypts a single 8-byte _D_E_S___c_b_l_o_c_k in _e_l_e_c_t_r_o_n_i_c _c_o_d_e _b_o_o_k (ECB) mode. It always transforms the input data, pointed to by _i_n_p_u_t, into the out- put data, pointed to by the _o_u_t_p_u_t argument. If the _e_n_c_r_y_p_t argument is non-zero (DES_ENCRYPT), the _i_n_p_u_t (cleartext) is encrypted in to the _o_u_t_p_u_t (ciphertext) using the key_schedule specified by the _s_c_h_e_d_u_l_e argument, previously set via _D_E_S___s_e_t___k_e_y. If _e_n_c_r_y_p_t is zero (DES_DECRYPT), the _i_n_p_u_t (now ciphertext) is decrypted into the _o_u_t_p_u_t (now cleartext). Input and output may overlap. _D_E_S___e_c_b___e_n_c_r_y_p_t_(_) does not return a value. _D_E_S___e_c_b_3___e_n_c_r_y_p_t_(_) encrypts/decrypts the _i_n_p_u_t block by using three-key Triple-DES encryption in ECB mode. This involves encrypting the input with _k_s_1, decrypting with the key schedule _k_s_2, and then encrypting with _k_s_3. This routine greatly reduces the chances of brute force breaking of DES and has the advantage of if _k_s_1, _k_s_2 and _k_s_3 are the same, it is equivalent to just encryption using ECB mode and _k_s_1 as the key. The macro _D_E_S___e_c_b_2___e_n_c_r_y_p_t_(_) is provided to perform two-key Triple-DES encryption by using _k_s_1 for the final encryption. _D_E_S___n_c_b_c___e_n_c_r_y_p_t_(_) encrypts/decrypts using the _c_i_p_h_e_r_-_b_l_o_c_k_-_c_h_a_i_n_i_n_g (CBC) mode of DES. If the _e_n_c_r_y_p_t argument is non-zero, the routine cipher-block-chain encrypts the cleartext data pointed to by the _i_n_p_u_t argument into the ciphertext pointed to by the _o_u_t_p_u_t argument, using the key schedule provided by the _s_c_h_e_d_u_l_e argument, and initialization vector provided by the _i_v_e_c argument. If the _l_e_n_g_t_h argument is not an integral multiple of eight bytes, the last block is copied to a tempo- rary area and zero filled. The output is always an integral multiple of eight bytes. _D_E_S___x_c_b_c___e_n_c_r_y_p_t_(_) is RSA's DESX mode of DES. It uses _i_n_w and _o_u_t_w to 'whiten' the encryption. _i_n_w and _o_u_t_w are secret (unlike the iv) and are as such, part of the key. So the key is sort of 24 bytes. This is much better than CBC DES. _D_E_S___e_d_e_3___c_b_c___e_n_c_r_y_p_t_(_) implements outer triple CBC DES encryption with three keys. This means that each DES operation inside the CBC mode is an "C=E(ks3,D(ks2,E(ks1,M)))". This mode is used by SSL. The _D_E_S___e_d_e_2___c_b_c___e_n_c_r_y_p_t_(_) macro implements two-key Triple-DES by reusing _k_s_1 for the final encryption. "C=E(ks1,D(ks2,E(ks1,M)))". This form of Triple-DES is used by the RSAREF library. _D_E_S___p_c_b_c___e_n_c_r_y_p_t_(_) encrypt/decrypts using the propagating cipher block chaining mode used by Kerberos v4. Its parameters are the same as _D_E_S___n_c_b_c___e_n_c_r_y_p_t_(_). _D_E_S___c_f_b___e_n_c_r_y_p_t_(_) encrypt/decrypts using cipher feedback mode. This method takes an array of characters as input and outputs and array of characters. It does not require any padding to 8 character groups. Note: the _i_v_e_c variable is changed and the new changed value needs to be passed to the next call to this function. Since this function runs a complete DES ECB encryption per _n_u_m_b_i_t_s, this function is only sug- gested for use when sending small numbers of characters. _D_E_S___c_f_b_6_4___e_n_c_r_y_p_t_(_) implements CFB mode of DES with 64bit feedback. Why is this useful you ask? Because this routine will allow you to encrypt an arbitrary number of bytes, no 8 byte padding. Each call to this routine will encrypt the input bytes to output and then update ivec and num. num contains 'how far' we are though ivec. If this does not make much sense, read more about cfb mode of DES :-). _D_E_S___e_d_e_3___c_f_b_6_4___e_n_c_r_y_p_t_(_) and _D_E_S___e_d_e_2___c_f_b_6_4___e_n_c_r_y_p_t_(_) is the same as _D_E_S___c_f_b_6_4___e_n_c_r_y_p_t_(_) except that Triple-DES is used. _D_E_S___o_f_b___e_n_c_r_y_p_t_(_) encrypts using output feedback mode. This method takes an array of characters as input and outputs and array of charac- ters. It does not require any padding to 8 character groups. Note: the _i_v_e_c variable is changed and the new changed value needs to be passed to the next call to this function. Since this function runs a complete DES ECB encryption per numbits, this function is only sug- gested for use when sending small numbers of characters. _D_E_S___o_f_b_6_4___e_n_c_r_y_p_t_(_) is the same as _D_E_S___c_f_b_6_4___e_n_c_r_y_p_t_(_) using Output Feed Back mode. _D_E_S___e_d_e_3___o_f_b_6_4___e_n_c_r_y_p_t_(_) and _D_E_S___e_d_e_2___o_f_b_6_4___e_n_c_r_y_p_t_(_) is the same as _D_E_S___o_f_b_6_4___e_n_c_r_y_p_t_(_), using Triple-DES. The following functions are included in the DES library for compatibil- ity with the MIT Kerberos library. _D_E_S___c_b_c___c_k_s_u_m_(_) produces an 8 byte checksum based on the input stream (via CBC encryption). The last 4 bytes of the checksum are returned and the complete 8 bytes are placed in _o_u_t_p_u_t. This function is used by Kerberos v4. Other applications should use _E_V_P___D_i_g_e_s_t_I_n_i_t(3) etc. instead. _D_E_S___q_u_a_d___c_k_s_u_m_(_) is a Kerberos v4 function. It returns a 4 byte check- sum from the input bytes. The algorithm can be iterated over the input, depending on _o_u_t___c_o_u_n_t, 1, 2, 3 or 4 times. If _o_u_t_p_u_t is non-NULL, the 8 bytes generated by each pass are written into _o_u_t_p_u_t. The following are DES-based transformations: _D_E_S___f_c_r_y_p_t_(_) is a fast version of the Unix _c_r_y_p_t(3) function. This version takes only a small amount of space relative to other fast _c_r_y_p_t_(_) implementations. This is different to the normal crypt in that the third parameter is the buffer that the return value is written into. It needs to be at least 14 bytes long. This function is thread safe, unlike the normal crypt. _D_E_S___c_r_y_p_t_(_) is a faster replacement for the normal system _c_r_y_p_t_(_). This function calls _D_E_S___f_c_r_y_p_t_(_) with a static array passed as the third parameter. This emulates the normal non-thread safe semantics of _c_r_y_p_t(3). _D_E_S___e_n_c___w_r_i_t_e_(_) writes _l_e_n bytes to file descriptor _f_d from buffer _b_u_f. The data is encrypted via _p_c_b_c___e_n_c_r_y_p_t (default) using _s_c_h_e_d for the key and _i_v as a starting vector. The actual data send down _f_d consists of 4 bytes (in network byte order) containing the length of the follow- ing encrypted data. The encrypted data then follows, padded with ran- dom data out to a multiple of 8 bytes. _D_E_S___e_n_c___r_e_a_d_(_) is used to read _l_e_n bytes from file descriptor _f_d into buffer _b_u_f. The data being read from _f_d is assumed to have come from _D_E_S___e_n_c___w_r_i_t_e_(_) and is decrypted using _s_c_h_e_d for the key schedule and _i_v for the initial vector. WWaarrnniinngg:: The data format used by _D_E_S___e_n_c___w_r_i_t_e_(_) and _D_E_S___e_n_c___r_e_a_d_(_) has a cryptographic weakness: When asked to write more than MAXWRITE bytes, _D_E_S___e_n_c___w_r_i_t_e_(_) will split the data into several chunks that are all encrypted using the same IV. So don't use these functions unless you are sure you know what you do (in which case you might not want to use them anyway). They cannot handle non-blocking sockets. _D_E_S___e_n_c___r_e_a_d_(_) uses an internal state and thus cannot be used on multiple files. _D_E_S___r_w___m_o_d_e is used to specify the encryption mode to use with _D_E_S___e_n_c___r_e_a_d_(_) and _D_E_S___e_n_d___w_r_i_t_e_(_). If set to _D_E_S___P_C_B_C___M_O_D_E (the default), DES_pcbc_encrypt is used. If set to _D_E_S___C_B_C___M_O_D_E DES_cbc_encrypt is used. NNOOTTEESS Single-key DES is insecure due to its short key size. ECB mode is not suitable for most applications; see _d_e_s___m_o_d_e_s(7). The _e_v_p(3) library provides higher-level encryption functions. BBUUGGSS _D_E_S___3_c_b_c___e_n_c_r_y_p_t_(_) is flawed and must not be used in applications. _D_E_S___c_b_c___e_n_c_r_y_p_t_(_) does not modify iivveecc; use _D_E_S___n_c_b_c___e_n_c_r_y_p_t_(_) instead. _D_E_S___c_f_b___e_n_c_r_y_p_t_(_) and _D_E_S___o_f_b___e_n_c_r_y_p_t_(_) operates on input of 8 bits. What this means is that if you set numbits to 12, and length to 2, the first 12 bits will come from the 1st input byte and the low half of the second input byte. The second 12 bits will have the low 8 bits taken from the 3rd input byte and the top 4 bits taken from the 4th input byte. The same holds for output. This function has been implemented this way because most people will be using a multiple of 8 and because once you get into pulling bytes input bytes apart things get ugly! _D_E_S___s_t_r_i_n_g___t_o___k_e_y_(_) is available for backward compatibility with the MIT library. New applications should use a cryptographic hash func- tion. The same applies for _D_E_S___s_t_r_i_n_g___t_o___2_k_e_y_(_). CCOONNFFOORRMMIINNGG TTOO ANSI X3.106 The ddeess library was written to be source code compatible with the MIT Kerberos library. SSEEEE AALLSSOO _c_r_y_p_t(3), _d_e_s___m_o_d_e_s(7), _e_v_p(3), _r_a_n_d(3) HHIISSTTOORRYY In OpenSSL 0.9.7, all des_ functions were renamed to DES_ to avoid clashes with older versions of libdes. Compatibility des_ functions are provided for a short while, as well as _c_r_y_p_t_(_). Declarations for these are in . There is no DES_ variant for _d_e_s___r_a_n_- _d_o_m___s_e_e_d_(_). This will happen to other functions as well if they are deemed redundant (_d_e_s___r_a_n_d_o_m___s_e_e_d_(_) just calls _R_A_N_D___s_e_e_d_(_) and is present for backward compatibility only), buggy or already scheduled for removal. _d_e_s___c_b_c___c_k_s_u_m_(_), _d_e_s___c_b_c___e_n_c_r_y_p_t_(_), _d_e_s___e_c_b___e_n_c_r_y_p_t_(_), _d_e_s___i_s___w_e_a_k___k_e_y_(_), _d_e_s___k_e_y___s_c_h_e_d_(_), _d_e_s___p_c_b_c___e_n_c_r_y_p_t_(_), _d_e_s___q_u_a_d___c_k_s_u_m_(_), _d_e_s___r_a_n_d_o_m___k_e_y_(_) and _d_e_s___s_t_r_i_n_g___t_o___k_e_y_(_) are avail- able in the MIT Kerberos library; _d_e_s___c_h_e_c_k___k_e_y___p_a_r_i_t_y_(_), _d_e_s___f_i_x_u_p___k_e_y___p_a_r_i_t_y_(_) and _d_e_s___i_s___w_e_a_k___k_e_y_(_) are available in newer versions of that library. _d_e_s___s_e_t___k_e_y___c_h_e_c_k_e_d_(_) and _d_e_s___s_e_t___k_e_y___u_n_c_h_e_c_k_e_d_(_) were added in OpenSSL 0.9.5. _d_e_s___g_e_n_e_r_a_t_e___r_a_n_d_o_m___b_l_o_c_k_(_), _d_e_s___i_n_i_t___r_a_n_d_o_m___n_u_m_b_e_r___g_e_n_e_r_a_t_o_r_(_), _d_e_s___n_e_w___r_a_n_d_o_m___k_e_y_(_), _d_e_s___s_e_t___r_a_n_d_o_m___g_e_n_e_r_a_t_o_r___s_e_e_d_(_) and _d_e_s___s_e_t___s_e_q_u_e_n_c_e___n_u_m_b_e_r_(_) and _d_e_s___r_a_n_d___d_a_t_a_(_) are used in newer ver- sions of Kerberos but are not implemented here. _d_e_s___r_a_n_d_o_m___k_e_y_(_) generated cryptographically weak random data in SSLeay and in OpenSSL prior version 0.9.5, as well as in the original MIT library. AAUUTTHHOORR Eric Young (eay@cryptsoft.com). Modified for the OpenSSL project (http://www.openssl.org). 1.0.2u 2019-12-20 des(3)