DES_CRYPT(3) UNIX Programmer's Manual DES_CRYPT(3)

des_read_password, des_read_2password, des_string_to_key, des_string_to_2key, des_read_pw_string, des_random_key, des_set_key, des_key_sched, des_ecb_encrypt, des_3ecb_encrypt, des_cbc_encrypt, des_3cbc_encrypt, des_pcbc_encrypt, des_cfb_encrypt, des_ofb_encrypt, des_cbc_cksum, des_quad_cksum, des_enc_read, des_enc_write, des_set_odd_parity, des_is_weak_key, crypt - (non USA) DES encryption

#include <des.h>int des_read_password(key,prompt,verify)des_cblock *key; char *prompt; int verify;int des_read_2password(key1,key2,prompt,verify)des_cblock *key1,*key2; char *prompt; int verify;int des_string_to_key(str,key)char *str; des_cblock *key;int des_string_to_2keys(str,key1,key2)char *str; des_cblock *key1,*key2;int des_read_pw_string(buf,length,prompt,verify)char *buf; int length; char *prompt; int verify;int des_random_key(key)des_cblock *key;int des_set_key(key,schedule)des_cblock *key; des_key_schedule schedule;int des_key_sched(key,schedule)des_cblock *key; des_key_schedule schedule;int des_ecb_encrypt(input,output,schedule,encrypt)des_cblock *input; des_cblock *output; des_key_schedule schedule; MirBSD #10-current Printed 2022-12-23 1 DES_CRYPT(3) UNIX Programmer's Manual DES_CRYPT(3) int encrypt;int des_3ecb_encrypt(input,output,ks1,ks2,encrypt)des_cblock *input; des_cblock *output; des_key_schedule ks1,ks2; int encrypt;int des_cbc_encrypt(input,output,length,schedule,ivec,encrypt)des_cblock *input; des_cblock *output; long length; des_key_schedule schedule; des_cblock *ivec; int encrypt;int des_3cbc_encrypt(input,output,length,sk1,sk2,ivec1,ivec2,encrypt)des_cblock *input; des_cblock *output; long length; des_key_schedule sk1; des_key_schedule sk2; des_cblock *ivec1; des_cblock *ivec2; int encrypt;int des_pcbc_encrypt(input,output,length,schedule,ivec,encrypt)des_cblock *input; des_cblock *output; long length; des_key_schedule schedule; des_cblock *ivec; int encrypt;int des_cfb_encrypt(input,output,numbits,length,schedule,ivec,encrypt)unsigned char *input; unsigned char *output; int numbits; long length; des_key_schedule schedule; des_cblock *ivec; int encrypt;int des_ofb_encrypt(input,output,numbits,length,schedule,ivec)unsigned char *input,*output; int numbits; long length; des_key_schedule schedule; des_cblock *ivec;unsigned long des_cbc_cksum(input,output,length,schedule,ivec)des_cblock *input; MirBSD #10-current Printed 2022-12-23 2 DES_CRYPT(3) UNIX Programmer's Manual DES_CRYPT(3) des_cblock *output; long length; des_key_schedule schedule; des_cblock *ivec;unsigned long des_quad_cksum(input,output,length,out_count,seed)des_cblock *input; des_cblock *output; long length; int out_count; des_cblock *seed;int des_check_key;int des_enc_read(fd,buf,len,sched,iv)int fd; char *buf; int len; des_key_schedule sched; des_cblock *iv;int des_enc_write(fd,buf,len,sched,iv)int fd; char *buf; int len; des_key_schedule sched; des_cblock *iv;extern int des_rw_mode;void des_set_odd_parity(key)des_cblock *key;int des_is_weak_key(key)des_cblock *key;char *crypt(passwd,salt)char *passwd; char *salt;

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 ades_key_schedulefrom a key, the second is the actual encryption. A des key is of typedes_cblock. This type is made from 8 characters with odd parity. The least significant bit in the character is the parity bit. The key schedule is an expanded form of the key; it is used to speed the encryption process. MirBSD #10-current Printed 2022-12-23 3 DES_CRYPT(3) UNIX Programmer's Manual DES_CRYPT(3)des_read_passwordwrites the string specified by prompt to the standard output, turns off echo and reads an input string from standard input until terminated with a newline. If verify is non-zero, it prompts and reads the input again and verifies that both entered passwords are the same. The entered string is converted into a des key by using thedes_string_to_keyroutine. The new key is placed in thedes_cblockthat was passed (by reference) to the routine. If there were no errors,des_read_passwordreturns 0, -1 is returned if there was a terminal error and 1 is returned for any other error.des_read_2passwordoperates in the same way asdes_read_passwordexcept that it generates 2 keys by using thedes_string_to_2keyfunction.des_read_pw_stringis called bydes_read_passwordto read and verify a string from a terminal device. The string is returned inbuf. The size ofbufis passed to the routine via thelengthparameter.des_string_to_keyconverts a string into a valid des key.des_string_to_2keyconverts a string into 2 valid des keys. This routine is best suited for use to generate keys for use withdes_3ecb_encrypt.des_random_keyreturns a random key that is made of a combi- nation of process id, time and an increasing counter. Before a des key can be used it is converted into ades_key_schedulevia thedes_set_keyroutine. If thedes_check_keyflag is non-zero,des_set_keywill 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.des_key_schedis another name for thedes_set_keyfunction. The following routines mostly operate on an input and output stream ofdes_cblock's.des_ecb_encryptis the basic DES encryption routine that encrypts or decrypts a single 8-bytedes_cblockinelec-tronic code bookmode. It always transforms the input data, pointed to byinput, into the output data, pointed to by theoutputargument. If theencryptargument is non-zero (DES_ENCRYPT), theinput(cleartext) is encrypted in to theoutput(ciphertext) using the key_schedule specified by thescheduleargument, previously set viades_set_key. Ifencryptis zero (DES_DECRYPT), theinput(now ciphertext) is MirBSD #10-current Printed 2022-12-23 4 DES_CRYPT(3) UNIX Programmer's Manual DES_CRYPT(3) decrypted into theoutput(now cleartext). Input and output may overlap. No meaningful value is returned.des_3ecb_encryptencrypts/decrypts theinputblock by using triple ecb DES encryption. This involves encrypting the input withks1, decryption with the key scheduleks2, and then encryption with the first again. This routine greatly reduces the chances of brute force breaking of DES and has the advantage of ifks1andks2are the same, it is equivalent to just encryption using ecb mode andks1as the key.des_cbc_encryptencrypts/decrypts using thecipher-block-chainingmode of DES. If theencryptargument is non-zero, the routine cipher-block-chain encrypts the cleartext data pointed to by theinputargument into the ciphertext pointed to by theoutputargument, using the key schedule provided by thescheduleargument, and initialisation vector provided by theivecargument. If thelengthargument 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. To make multiple cbc encrypt calls on a large amount of data appear to be onedes_cbc_encryptcall, theivecof subsequent calls should be the last 8 bytes of the output.des_3cbc_encryptencrypts/decrypts theinputblock by using triple cbc DES encryption. This involves encrypting the input with key scheduleks1, decryption with the key scheduleks2, and then encryption with the first again. 2 initialisation vectors are required,ivec1andivec2. Unlikedes_cbc_encrypt, these initialisation vectors are modified by the subroutine. This routine greatly reduces the chances of brute force breaking of DES and has the advantage of ifks1andks2are the same, it is equivalent to just encryp- tion using cbc mode andks1as the key.des_pcbc_encryptencrypt/decrypts using a modified block chaining mode. It provides better error propagation charac- teristics than cbc encryption.des_cfb_encryptencrypt/decrypts using cipher feedback mode. This method takes an array of characters as input and out- puts an array of characters. It does not require any pad- ding to 8 character groups. Note: the ivec variable is changed and the new changed value needs to be passed to the next call to this function. Since this function runs a com- plete DES ecb encryption per numbits, this function is only suggested for use when sending small numbers of characters.des_ofb_encryptencrypts using output feedback mode. This method takes an array of characters as input and outputs an MirBSD #10-current Printed 2022-12-23 5 DES_CRYPT(3) UNIX Programmer's Manual DES_CRYPT(3) array of characters. It does not require any padding to 8 character groups. Note: the ivec 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.des_cbc_cksumproduces an 8 byte checksum based on the input stream (via cbc encryption). The last 4 bytes of the check- sum is returned and the complete 8 bytes is placed inout-put.des_quad_cksumreturns a 4 byte checksum from the input bytes. The algorithm can be iterated over the input, depend- ing onout_count, 1, 2, 3 or 4 times. Ifoutputis non-NULL, the 8 bytes generated by each pass are written intooutput.des_enc_writeis used to writelenbytes to file descriptorfdfrom bufferbuf. The data is encrypted viapcbc_encrypt(default) usingschedfor the key andivas a starting vec- tor. The actual data sent downfdconsists 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.des_enc_readis used to readlenbytes from file descriptorfdinto bufferbuf. The data being read fromfdis assumed to have come fromdes_enc_writeand is decrypted usingschedfor the key schedule andivfor the initial vector. Thedes_enc_read/des_enc_writepair can be used to read/write to files, pipes and sockets. I have used them in implementing a version of rlogin in which all data is encrypted.des_rw_modeis used to specify the encryption mode to use withdes_enc_readanddes_end_write. If set toDES_PCBC_MODE(the default), des_pcbc_encrypt is used. If set toDES_CBC_MODEdes_cbc_encrypt is used. These two routines and the variable are not part of the normal MIT library.des_set_odd_paritysets the parity of the passedkeyto odd. This routine is not part of the standard MIT library.des_is_weak_keyreturns 1 if the passed key is a weak key (pick again :-), 0 if it is ok. This routine is not part of the standard MIT library.cryptis a replacement for the normal system crypt. It is much faster than the system crypt.

/usr/include/des.h /usr/lib/libdes.a MirBSD #10-current Printed 2022-12-23 6 DES_CRYPT(3) UNIX Programmer's Manual DES_CRYPT(3) The encryption routines have been tested on 16bit, 32bit and 64bit machines of various endian and even works under VMS.

If you think this manual is sparse, read the des_crypt(3) manual from the MIT kerberos (or bones outside of the USA) distribution.des_cfb_encryptanddes_ofb_encryptoperates 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 input bytes apart things get ugly!des_read_pw_stringis the most machine/OS dependent function and normally generates the most problems when porting this code.des_string_to_keyis probably different from the MIT version since there are lots of fun ways to implement one-way encryption of a text string. The routines are optimised for 32 bit machines and so are not efficient on IBM PCs. NOTE: extensive work has been done on this library since this document was originally written. Please try to read des.doc from the libdes distribution since it is far more up to date and documents more of the functions. Libdes is now also being shipped as part of SSLeay, a general crypto- graphic library that amongst other things implements Netscape's SSL protocol. The most recent version can be found in SSLeay distributions.

Eric Young (eay@mincom.oz.au or eay@psych.psy.uq.oz.au) MirBSD #10-current Printed 2022-12-23 7