des.pod 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. =pod
  2. =head1 NAME
  3. des_random_key, des_set_key, des_key_sched, des_set_key_checked,
  4. des_set_key_unchecked, des_set_odd_parity, des_is_weak_key,
  5. des_ecb_encrypt, des_ecb2_encrypt, des_ecb3_encrypt, des_ncbc_encrypt,
  6. des_cfb_encrypt, des_ofb_encrypt, des_pcbc_encrypt, des_cfb64_encrypt,
  7. des_ofb64_encrypt, des_xcbc_encrypt, des_ede2_cbc_encrypt,
  8. des_ede2_cfb64_encrypt, des_ede2_ofb64_encrypt, des_ede3_cbc_encrypt,
  9. des_ede3_cbcm_encrypt, des_ede3_cfb64_encrypt, des_ede3_ofb64_encrypt,
  10. des_read_password, des_read_2passwords, des_read_pw_string,
  11. des_cbc_cksum, des_quad_cksum, des_string_to_key, des_string_to_2keys,
  12. des_fcrypt, des_crypt, des_enc_read, des_enc_write - DES encryption
  13. =head1 SYNOPSIS
  14. #include <openssl/des.h>
  15. void des_random_key(des_cblock *ret);
  16. int des_set_key(const_des_cblock *key, des_key_schedule *schedule);
  17. int des_key_sched(const_des_cblock *key, des_key_schedule *schedule);
  18. int des_set_key_checked(const_des_cblock *key,
  19. des_key_schedule *schedule);
  20. void des_set_key_unchecked(const_des_cblock *key,
  21. des_key_schedule *schedule);
  22. void des_set_odd_parity(des_cblock *key);
  23. int des_is_weak_key(const_des_cblock *key);
  24. void des_ecb_encrypt(const_des_cblock *input, des_cblock *output,
  25. des_key_schedule *ks, int enc);
  26. void des_ecb2_encrypt(const_des_cblock *input, des_cblock *output,
  27. des_key_schedule *ks1, des_key_schedule *ks2, int enc);
  28. void des_ecb3_encrypt(const_des_cblock *input, des_cblock *output,
  29. des_key_schedule *ks1, des_key_schedule *ks2,
  30. des_key_schedule *ks3, int enc);
  31. void des_ncbc_encrypt(const unsigned char *input, unsigned char *output,
  32. long length, des_key_schedule *schedule, des_cblock *ivec,
  33. int enc);
  34. void des_cfb_encrypt(const unsigned char *in, unsigned char *out,
  35. int numbits, long length, des_key_schedule *schedule,
  36. des_cblock *ivec, int enc);
  37. void des_ofb_encrypt(const unsigned char *in, unsigned char *out,
  38. int numbits, long length, des_key_schedule *schedule,
  39. des_cblock *ivec);
  40. void des_pcbc_encrypt(const unsigned char *input, unsigned char *output,
  41. long length, des_key_schedule *schedule, des_cblock *ivec,
  42. int enc);
  43. void des_cfb64_encrypt(const unsigned char *in, unsigned char *out,
  44. long length, des_key_schedule *schedule, des_cblock *ivec,
  45. int *num, int enc);
  46. void des_ofb64_encrypt(const unsigned char *in, unsigned char *out,
  47. long length, des_key_schedule *schedule, des_cblock *ivec,
  48. int *num);
  49. void des_xcbc_encrypt(const unsigned char *input, unsigned char *output,
  50. long length, des_key_schedule *schedule, des_cblock *ivec,
  51. const_des_cblock *inw, const_des_cblock *outw, int enc);
  52. void des_ede2_cbc_encrypt(const unsigned char *input,
  53. unsigned char *output, long length, des_key_schedule *ks1,
  54. des_key_schedule *ks2, des_cblock *ivec, int enc);
  55. void des_ede2_cfb64_encrypt(const unsigned char *in,
  56. unsigned char *out, long length, des_key_schedule *ks1,
  57. des_key_schedule *ks2, des_cblock *ivec, int *num, int enc);
  58. void des_ede2_ofb64_encrypt(const unsigned char *in,
  59. unsigned char *out, long length, des_key_schedule *ks1,
  60. des_key_schedule *ks2, des_cblock *ivec, int *num);
  61. void des_ede3_cbc_encrypt(const unsigned char *input,
  62. unsigned char *output, long length, des_key_schedule *ks1,
  63. des_key_schedule *ks2, des_key_schedule *ks3, des_cblock *ivec,
  64. int enc);
  65. void des_ede3_cbcm_encrypt(const unsigned char *in, unsigned char *out,
  66. long length, des_key_schedule *ks1, des_key_schedule *ks2,
  67. des_key_schedule *ks3, des_cblock *ivec1, des_cblock *ivec2,
  68. int enc);
  69. void des_ede3_cfb64_encrypt(const unsigned char *in, unsigned char *out,
  70. long length, des_key_schedule *ks1, des_key_schedule *ks2,
  71. des_key_schedule *ks3, des_cblock *ivec, int *num, int enc);
  72. void des_ede3_ofb64_encrypt(const unsigned char *in, unsigned char *out,
  73. long length, des_key_schedule *ks1,
  74. des_key_schedule *ks2, des_key_schedule *ks3,
  75. des_cblock *ivec, int *num);
  76. int des_read_password(des_cblock *key, const char *prompt, int verify);
  77. int des_read_2passwords(des_cblock *key1, des_cblock *key2,
  78. const char *prompt, int verify);
  79. int des_read_pw_string(char *buf, int length, const char *prompt,
  80. int verify);
  81. DES_LONG des_cbc_cksum(const unsigned char *input, des_cblock *output,
  82. long length, des_key_schedule *schedule,
  83. const_des_cblock *ivec);
  84. DES_LONG des_quad_cksum(const unsigned char *input, des_cblock output[],
  85. long length, int out_count, des_cblock *seed);
  86. void des_string_to_key(const char *str, des_cblock *key);
  87. void des_string_to_2keys(const char *str, des_cblock *key1,
  88. des_cblock *key2);
  89. char *des_fcrypt(const char *buf, const char *salt, char *ret);
  90. char *des_crypt(const char *buf, const char *salt);
  91. char *crypt(const char *buf, const char *salt);
  92. int des_enc_read(int fd, void *buf, int len, des_key_schedule *sched,
  93. des_cblock *iv);
  94. int des_enc_write(int fd, const void *buf, int len,
  95. des_key_schedule *sched, des_cblock *iv);
  96. =head1 DESCRIPTION
  97. This library contains a fast implementation of the DES encryption
  98. algorithm.
  99. There are two phases to the use of DES encryption. The first is the
  100. generation of a I<des_key_schedule> from a key, the second is the
  101. actual encryption. A DES key is of type I<des_cblock>. This type is
  102. consists of 8 bytes with odd parity. The least significant bit in
  103. each byte is the parity bit. The key schedule is an expanded form of
  104. the key; it is used to speed the encryption process.
  105. des_random_key() generates a random key. The PRNG must be seeded
  106. prior to using this function (see L<rand(3)|rand(3)>; for backward
  107. compatibility the function des_random_seed() is available as well).
  108. If the PRNG could not generate a secure key, 0 is returned. In
  109. earlier versions of the library, des_random_key() did not generate
  110. secure keys.
  111. Before a DES key can be used, it must be converted into the
  112. architecture dependent I<des_key_schedule> via the
  113. des_set_key_checked() or des_set_key_unchecked() function.
  114. des_set_key_checked() will check that the key passed is of odd parity
  115. and is not a week or semi-weak key. If the parity is wrong, then -1
  116. is returned. If the key is a weak key, then -2 is returned. If an
  117. error is returned, the key schedule is not generated.
  118. des_set_key() (called des_key_sched() in the MIT library) works like
  119. des_set_key_checked() if the I<des_check_key> flag is non-zero,
  120. otherwise like des_set_key_unchecked(). These functions are available
  121. for compatibility; it is recommended to use a function that does not
  122. depend on a global variable.
  123. des_set_odd_parity() (called des_fixup_key_parity() in the MIT
  124. library) sets the parity of the passed I<key> to odd.
  125. des_is_weak_key() returns 1 is the passed key is a weak key, 0 if it
  126. is ok. The probability that a randomly generated key is weak is
  127. 1/2^52, so it is not really worth checking for them.
  128. The following routines mostly operate on an input and output stream of
  129. I<des_cblock>s.
  130. des_ecb_encrypt() is the basic DES encryption routine that encrypts or
  131. decrypts a single 8-byte I<des_cblock> in I<electronic code book>
  132. (ECB) mode. It always transforms the input data, pointed to by
  133. I<input>, into the output data, pointed to by the I<output> argument.
  134. If the I<encrypt> argument is non-zero (DES_ENCRYPT), the I<input>
  135. (cleartext) is encrypted in to the I<output> (ciphertext) using the
  136. key_schedule specified by the I<schedule> argument, previously set via
  137. I<des_set_key>. If I<encrypt> is zero (DES_DECRYPT), the I<input> (now
  138. ciphertext) is decrypted into the I<output> (now cleartext). Input
  139. and output may overlap. des_ecb_encrypt() does not return a value.
  140. des_ecb3_encrypt() encrypts/decrypts the I<input> block by using
  141. three-key Triple-DES encryption in ECB mode. This involves encrypting
  142. the input with I<ks1>, decrypting with the key schedule I<ks2>, and
  143. then encrypting with I<ks3>. This routine greatly reduces the chances
  144. of brute force breaking of DES and has the advantage of if I<ks1>,
  145. I<ks2> and I<ks3> are the same, it is equivalent to just encryption
  146. using ECB mode and I<ks1> as the key.
  147. The macro des_ecb2_encrypt() is provided to perform two-key Triple-DES
  148. encryption by using I<ks1> for the final encryption.
  149. des_ncbc_encrypt() encrypts/decrypts using the I<cipher-block-chaining>
  150. (CBC) mode of DES. If the I<encrypt> argument is non-zero, the
  151. routine cipher-block-chain encrypts the cleartext data pointed to by
  152. the I<input> argument into the ciphertext pointed to by the I<output>
  153. argument, using the key schedule provided by the I<schedule> argument,
  154. and initialization vector provided by the I<ivec> argument. If the
  155. I<length> argument is not an integral multiple of eight bytes, the
  156. last block is copied to a temporary area and zero filled. The output
  157. is always an integral multiple of eight bytes.
  158. des_xcbc_encrypt() is RSA's DESX mode of DES. It uses I<inw> and
  159. I<outw> to 'whiten' the encryption. I<inw> and I<outw> are secret
  160. (unlike the iv) and are as such, part of the key. So the key is sort
  161. of 24 bytes. This is much better than CBC DES.
  162. des_ede3_cbc_encrypt() implements outer triple CBC DES encryption with
  163. three keys. This means that each DES operation inside the CBC mode is
  164. really an C<C=E(ks3,D(ks2,E(ks1,M)))>. This mode is used by SSL.
  165. The des_ede2_cbc_encrypt() macro implements two-key Triple-DES by
  166. reusing I<ks1> for the final encryption. C<C=E(ks1,D(ks2,E(ks1,M)))>.
  167. This form of Triple-DES is used by the RSAREF library.
  168. des_pcbc_encrypt() encrypt/decrypts using the propagating cipher block
  169. chaining mode used by Kerberos v4. Its parameters are the same as
  170. des_ncbc_encrypt().
  171. des_cfb_encrypt() encrypt/decrypts using cipher feedback mode. This
  172. method takes an array of characters as input and outputs and array of
  173. characters. It does not require any padding to 8 character groups.
  174. Note: the I<ivec> variable is changed and the new changed value needs to
  175. be passed to the next call to this function. Since this function runs
  176. a complete DES ECB encryption per I<numbits>, this function is only
  177. suggested for use when sending small numbers of characters.
  178. des_cfb64_encrypt()
  179. implements CFB mode of DES with 64bit feedback. Why is this
  180. useful you ask? Because this routine will allow you to encrypt an
  181. arbitrary number of bytes, no 8 byte padding. Each call to this
  182. routine will encrypt the input bytes to output and then update ivec
  183. and num. num contains 'how far' we are though ivec. If this does
  184. not make much sense, read more about cfb mode of DES :-).
  185. des_ede3_cfb64_encrypt() and des_ede2_cfb64_encrypt() is the same as
  186. des_cfb64_encrypt() except that Triple-DES is used.
  187. des_ofb_encrypt() encrypts using output feedback mode. This method
  188. takes an array of characters as input and outputs and array of
  189. characters. It does not require any padding to 8 character groups.
  190. Note: the I<ivec> variable is changed and the new changed value needs to
  191. be passed to the next call to this function. Since this function runs
  192. a complete DES ECB encryption per numbits, this function is only
  193. suggested for use when sending small numbers of characters.
  194. des_ofb64_encrypt() is the same as des_cfb64_encrypt() using Output
  195. Feed Back mode.
  196. des_ede3_ofb64_encrypt() and des_ede2_ofb64_encrypt() is the same as
  197. des_ofb64_encrypt(), using Triple-DES.
  198. The following functions are included in the DES library for
  199. compatibility with the MIT Kerberos library. des_read_pw_string()
  200. is also available under the name EVP_read_pw_string().
  201. des_read_pw_string() writes the string specified by I<prompt> to
  202. standard output, turns echo off and reads in input string from the
  203. terminal. The string is returned in I<buf>, which must have space for
  204. at least I<length> bytes. If I<verify> is set, the user is asked for
  205. the password twice and unless the two copies match, an error is
  206. returned. A return code of -1 indicates a system error, 1 failure due
  207. to use interaction, and 0 is success.
  208. des_read_password() does the same and converts the password to a DES
  209. key by calling des_string_to_key(); des_read_2password() operates in
  210. the same way as des_read_password() except that it generates two keys
  211. by using the des_string_to_2key() function. des_string_to_key() is
  212. available for backward compatibility with the MIT library. New
  213. applications should use a cryptographic hash function. The same
  214. applies for des_string_to_2key().
  215. des_cbc_cksum() produces an 8 byte checksum based on the input stream
  216. (via CBC encryption). The last 4 bytes of the checksum are returned
  217. and the complete 8 bytes are placed in I<output>. This function is
  218. used by Kerberos v4. Other applications should use
  219. L<EVP_DigestInit(3)|EVP_DigestInit(3)> etc. instead.
  220. des_quad_cksum() is a Kerberos v4 function. It returns a 4 byte
  221. checksum from the input bytes. The algorithm can be iterated over the
  222. input, depending on I<out_count>, 1, 2, 3 or 4 times. If I<output> is
  223. non-NULL, the 8 bytes generated by each pass are written into
  224. I<output>.
  225. The following are DES-based transformations:
  226. des_fcrypt() is a fast version of the Unix crypt(3) function. This
  227. version takes only a small amount of space relative to other fast
  228. crypt() implementations. This is different to the normal crypt in
  229. that the third parameter is the buffer that the return value is
  230. written into. It needs to be at least 14 bytes long. This function
  231. is thread safe, unlike the normal crypt.
  232. des_crypt() is a faster replacement for the normal system crypt().
  233. This function calls des_fcrypt() with a static array passed as the
  234. third parameter. This emulates the normal non-thread safe semantics
  235. of crypt(3).
  236. des_enc_write() writes I<len> bytes to file descriptor I<fd> from
  237. buffer I<buf>. The data is encrypted via I<pcbc_encrypt> (default)
  238. using I<sched> for the key and I<iv> as a starting vector. The actual
  239. data send down I<fd> consists of 4 bytes (in network byte order)
  240. containing the length of the following encrypted data. The encrypted
  241. data then follows, padded with random data out to a multiple of 8
  242. bytes.
  243. des_enc_read() is used to read I<len> bytes from file descriptor
  244. I<fd> into buffer I<buf>. The data being read from I<fd> is assumed to
  245. have come from des_enc_write() and is decrypted using I<sched> for
  246. the key schedule and I<iv> for the initial vector.
  247. B<Warning:> The data format used by des_enc_write() and des_enc_read()
  248. has a cryptographic weakness: When asked to write more than MAXWRITE
  249. bytes, des_enc_write() will split the data into several chunks that
  250. are all encrypted using the same IV. So don't use these functions
  251. unless you are sure you know what you do (in which case you might not
  252. want to use them anyway). They cannot handle non-blocking sockets.
  253. des_enc_read() uses an internal state and thus cannot be used on
  254. multiple files.
  255. I<des_rw_mode> is used to specify the encryption mode to use with
  256. des_enc_read() and des_end_write(). If set to I<DES_PCBC_MODE> (the
  257. default), des_pcbc_encrypt is used. If set to I<DES_CBC_MODE>
  258. des_cbc_encrypt is used.
  259. =head1 NOTES
  260. Single-key DES is insecure due to its short key size. ECB mode is
  261. not suitable for most applications; see L<des_modes(7)|des_modes(7)>.
  262. The L<evp(3)|evp(3)> library provides higher-level encryption functions.
  263. =head1 BUGS
  264. des_3cbc_encrypt() is flawed and must not be used in applications.
  265. des_cbc_encrypt() does not modify B<ivec>; use des_ncbc_encrypt()
  266. instead.
  267. des_cfb_encrypt() and des_ofb_encrypt() operates on input of 8 bits.
  268. What this means is that if you set numbits to 12, and length to 2, the
  269. first 12 bits will come from the 1st input byte and the low half of
  270. the second input byte. The second 12 bits will have the low 8 bits
  271. taken from the 3rd input byte and the top 4 bits taken from the 4th
  272. input byte. The same holds for output. This function has been
  273. implemented this way because most people will be using a multiple of 8
  274. and because once you get into pulling bytes input bytes apart things
  275. get ugly!
  276. des_read_pw_string() is the most machine/OS dependent function and
  277. normally generates the most problems when porting this code.
  278. =head1 CONFORMING TO
  279. ANSI X3.106
  280. The B<des> library was written to be source code compatible with
  281. the MIT Kerberos library.
  282. =head1 SEE ALSO
  283. crypt(3), L<des_modes(7)|des_modes(7)>, L<evp(3)|evp(3)>, L<rand(3)|rand(3)>
  284. =head1 HISTORY
  285. des_cbc_cksum(), des_cbc_encrypt(), des_ecb_encrypt(),
  286. des_is_weak_key(), des_key_sched(), des_pcbc_encrypt(),
  287. des_quad_cksum(), des_random_key(), des_read_password() and
  288. des_string_to_key() are available in the MIT Kerberos library;
  289. des_check_key_parity(), des_fixup_key_parity() and des_is_weak_key()
  290. are available in newer versions of that library.
  291. des_set_key_checked() and des_set_key_unchecked() were added in
  292. OpenSSL 0.9.5.
  293. des_generate_random_block(), des_init_random_number_generator(),
  294. des_new_random_key(), des_set_random_generator_seed() and
  295. des_set_sequence_number() and des_rand_data() are used in newer
  296. versions of Kerberos but are not implemented here.
  297. des_random_key() generated cryptographically weak random data in
  298. SSLeay and in OpenSSL prior version 0.9.5, as well as in the original
  299. MIT library.
  300. =head1 AUTHOR
  301. Eric Young (eay@cryptsoft.com). Modified for the OpenSSL project
  302. (http://www.openssl.org).
  303. =cut