des 3 0.9.6j 2003-07-23 libdes

NAME

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_read_password, des_read_2passwords, des_read_pw_string, 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

SYNOPSIS


 #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);


 int des_read_password(des_cblock *key, const char *prompt, int verify);
 int des_read_2passwords(des_cblock *key1, des_cblock *key2,
        const char *prompt, int verify);
 int des_read_pw_string(char *buf, int length, const char *prompt,
        int verify);




 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);
 char *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);

DESCRIPTION

This library contains a fast implementation of the DES encryption algorithm.

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 rand(3); for backward compatibility the function _d_e_s___r_a_n_d_o_m___s_e_e_d_(_) is available as well). If the PRNG could not generate a secure key, 0 is returned. In earlier versions of the library, _d_e_s___r_a_n_d_o_m___k_e_y_(_) did not generate secure keys.

Before a DES key can be used, it must be converted into the architecture 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 week 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_(_) (called _d_e_s___k_e_y___s_c_h_e_d_(_) in the MIT library) 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 functions are available for compatibility; it is recommended to use a function that does not depend on a global variable.

_d_e_s___s_e_t___o_d_d___p_a_r_i_t_y_(_) (called _d_e_s___f_i_x_u_p___k_e_y___p_a_r_i_t_y_(_) in the MIT library) 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 is the passed key is a weak key, 0 if it is ok. The probability that a randomly generated key is weak is 1/2^52, so it is not really worth checking for them.

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 output 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 temporary 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 really 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___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 suggested 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 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 numbits, this function is only suggested 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.

_d_e_s___r_e_a_d___p_w___s_t_r_i_n_g_(_) writes the string specified by _p_r_o_m_p_t to standard output, turns echo off and reads in input string from the terminal. The string is returned in _b_u_f, which must have space for at least _l_e_n_g_t_h bytes. If _v_e_r_i_f_y is set, the user is asked for the password twice and unless the two copies match, an error is returned. A return code of -1 indicates a system error, 1 failure due to use interaction, and 0 is success.

_d_e_s___r_e_a_d___p_a_s_s_w_o_r_d_(_) does the same and converts the password to a DES key by calling _d_e_s___s_t_r_i_n_g___t_o___k_e_y_(_); _d_e_s___r_e_a_d___2_p_a_s_s_w_o_r_d_(_) operates in the same way as _d_e_s___r_e_a_d___p_a_s_s_w_o_r_d_(_) except that it generates two keys by using the _d_e_s___s_t_r_i_n_g___t_o___2_k_e_y_(_) function. _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 function. The same applies for _d_e_s___s_t_r_i_n_g___t_o___2_k_e_y_(_).

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 following encrypted data. The encrypted data then follows, padded with random 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.

WWWWaaaarrrrnnnniiiinnnngggg:::: 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.

NOTES

Single-key DES is insecure due to its short key size. ECB mode is not suitable for most applications; see des_modes(7).

The evp(3) library provides higher-level encryption functions.

BUGS

_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 iiiivvvveeeecccc; 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___r_e_a_d___p_w___s_t_r_i_n_g_(_) is the most machine/OS dependent function and normally generates the most problems when porting this code.

CONFORMING TO

ANSI X3.106

SEE ALSO

_c_r_y_p_t(3), des_modes(7), evp(3), rand(3)

HISTORY

_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_(_), _d_e_s___r_e_a_d___p_a_s_s_w_o_r_d_(_) and _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 versions 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.

AUTHOR

Eric Young (eay@cryptsoft.com). Modified for the OpenSSL project (http://www.openssl.org).