DES_random_key.3ossl 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. .\" -*- mode: troff; coding: utf-8 -*-
  2. .\" Automatically generated by Pod::Man 5.01 (Pod::Simple 3.43)
  3. .\"
  4. .\" Standard preamble:
  5. .\" ========================================================================
  6. .de Sp \" Vertical space (when we can't use .PP)
  7. .if t .sp .5v
  8. .if n .sp
  9. ..
  10. .de Vb \" Begin verbatim text
  11. .ft CW
  12. .nf
  13. .ne \\$1
  14. ..
  15. .de Ve \" End verbatim text
  16. .ft R
  17. .fi
  18. ..
  19. .\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
  20. .ie n \{\
  21. . ds C` ""
  22. . ds C' ""
  23. 'br\}
  24. .el\{\
  25. . ds C`
  26. . ds C'
  27. 'br\}
  28. .\"
  29. .\" Escape single quotes in literal strings from groff's Unicode transform.
  30. .ie \n(.g .ds Aq \(aq
  31. .el .ds Aq '
  32. .\"
  33. .\" If the F register is >0, we'll generate index entries on stderr for
  34. .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index
  35. .\" entries marked with X<> in POD. Of course, you'll have to process the
  36. .\" output yourself in some meaningful fashion.
  37. .\"
  38. .\" Avoid warning from groff about undefined register 'F'.
  39. .de IX
  40. ..
  41. .nr rF 0
  42. .if \n(.g .if rF .nr rF 1
  43. .if (\n(rF:(\n(.g==0)) \{\
  44. . if \nF \{\
  45. . de IX
  46. . tm Index:\\$1\t\\n%\t"\\$2"
  47. ..
  48. . if !\nF==2 \{\
  49. . nr % 0
  50. . nr F 2
  51. . \}
  52. . \}
  53. .\}
  54. .rr rF
  55. .\" ========================================================================
  56. .\"
  57. .IX Title "DES_RANDOM_KEY 3ossl"
  58. .TH DES_RANDOM_KEY 3ossl 2025-01-17 3.4.0 OpenSSL
  59. .\" For nroff, turn off justification. Always turn off hyphenation; it makes
  60. .\" way too many mistakes in technical documents.
  61. .if n .ad l
  62. .nh
  63. .SH NAME
  64. DES_random_key, DES_set_key, DES_key_sched, DES_set_key_checked,
  65. DES_set_key_unchecked, DES_set_odd_parity, DES_is_weak_key,
  66. DES_ecb_encrypt, DES_ecb2_encrypt, DES_ecb3_encrypt, DES_ncbc_encrypt,
  67. DES_cfb_encrypt, DES_ofb_encrypt, DES_pcbc_encrypt, DES_cfb64_encrypt,
  68. DES_ofb64_encrypt, DES_xcbc_encrypt, DES_ede2_cbc_encrypt,
  69. DES_ede2_cfb64_encrypt, DES_ede2_ofb64_encrypt, DES_ede3_cbc_encrypt,
  70. DES_ede3_cfb64_encrypt, DES_ede3_ofb64_encrypt,
  71. DES_cbc_cksum, DES_quad_cksum, DES_string_to_key, DES_string_to_2keys,
  72. DES_fcrypt, DES_crypt \- DES encryption
  73. .SH SYNOPSIS
  74. .IX Header "SYNOPSIS"
  75. .Vb 1
  76. \& #include <openssl/des.h>
  77. .Ve
  78. .PP
  79. The following functions have been deprecated since OpenSSL 3.0, and can be
  80. hidden entirely by defining \fBOPENSSL_API_COMPAT\fR with a suitable version value,
  81. see \fBopenssl_user_macros\fR\|(7):
  82. .PP
  83. .Vb 1
  84. \& void DES_random_key(DES_cblock *ret);
  85. \&
  86. \& int DES_set_key(const_DES_cblock *key, DES_key_schedule *schedule);
  87. \& int DES_key_sched(const_DES_cblock *key, DES_key_schedule *schedule);
  88. \& int DES_set_key_checked(const_DES_cblock *key, DES_key_schedule *schedule);
  89. \& void DES_set_key_unchecked(const_DES_cblock *key, DES_key_schedule *schedule);
  90. \&
  91. \& void DES_set_odd_parity(DES_cblock *key);
  92. \& int DES_is_weak_key(const_DES_cblock *key);
  93. \&
  94. \& void DES_ecb_encrypt(const_DES_cblock *input, DES_cblock *output,
  95. \& DES_key_schedule *ks, int enc);
  96. \& void DES_ecb2_encrypt(const_DES_cblock *input, DES_cblock *output,
  97. \& DES_key_schedule *ks1, DES_key_schedule *ks2, int enc);
  98. \& void DES_ecb3_encrypt(const_DES_cblock *input, DES_cblock *output,
  99. \& DES_key_schedule *ks1, DES_key_schedule *ks2,
  100. \& DES_key_schedule *ks3, int enc);
  101. \&
  102. \& void DES_ncbc_encrypt(const unsigned char *input, unsigned char *output,
  103. \& long length, DES_key_schedule *schedule, DES_cblock *ivec,
  104. \& int enc);
  105. \& void DES_cfb_encrypt(const unsigned char *in, unsigned char *out,
  106. \& int numbits, long length, DES_key_schedule *schedule,
  107. \& DES_cblock *ivec, int enc);
  108. \& void DES_ofb_encrypt(const unsigned char *in, unsigned char *out,
  109. \& int numbits, long length, DES_key_schedule *schedule,
  110. \& DES_cblock *ivec);
  111. \& void DES_pcbc_encrypt(const unsigned char *input, unsigned char *output,
  112. \& long length, DES_key_schedule *schedule, DES_cblock *ivec,
  113. \& int enc);
  114. \& void DES_cfb64_encrypt(const unsigned char *in, unsigned char *out,
  115. \& long length, DES_key_schedule *schedule, DES_cblock *ivec,
  116. \& int *num, int enc);
  117. \& void DES_ofb64_encrypt(const unsigned char *in, unsigned char *out,
  118. \& long length, DES_key_schedule *schedule, DES_cblock *ivec,
  119. \& int *num);
  120. \&
  121. \& void DES_xcbc_encrypt(const unsigned char *input, unsigned char *output,
  122. \& long length, DES_key_schedule *schedule, DES_cblock *ivec,
  123. \& const_DES_cblock *inw, const_DES_cblock *outw, int enc);
  124. \&
  125. \& void DES_ede2_cbc_encrypt(const unsigned char *input, unsigned char *output,
  126. \& long length, DES_key_schedule *ks1,
  127. \& DES_key_schedule *ks2, DES_cblock *ivec, int enc);
  128. \& void DES_ede2_cfb64_encrypt(const unsigned char *in, unsigned char *out,
  129. \& long length, DES_key_schedule *ks1,
  130. \& DES_key_schedule *ks2, DES_cblock *ivec,
  131. \& int *num, int enc);
  132. \& void DES_ede2_ofb64_encrypt(const unsigned char *in, unsigned char *out,
  133. \& long length, DES_key_schedule *ks1,
  134. \& DES_key_schedule *ks2, DES_cblock *ivec, int *num);
  135. \&
  136. \& void DES_ede3_cbc_encrypt(const unsigned char *input, unsigned char *output,
  137. \& long length, DES_key_schedule *ks1,
  138. \& DES_key_schedule *ks2, DES_key_schedule *ks3,
  139. \& DES_cblock *ivec, int enc);
  140. \& void DES_ede3_cfb64_encrypt(const unsigned char *in, unsigned char *out,
  141. \& long length, DES_key_schedule *ks1,
  142. \& DES_key_schedule *ks2, DES_key_schedule *ks3,
  143. \& DES_cblock *ivec, int *num, int enc);
  144. \& void DES_ede3_ofb64_encrypt(const unsigned char *in, unsigned char *out,
  145. \& long length, DES_key_schedule *ks1,
  146. \& DES_key_schedule *ks2, DES_key_schedule *ks3,
  147. \& DES_cblock *ivec, int *num);
  148. \&
  149. \& DES_LONG DES_cbc_cksum(const unsigned char *input, DES_cblock *output,
  150. \& long length, DES_key_schedule *schedule,
  151. \& const_DES_cblock *ivec);
  152. \& DES_LONG DES_quad_cksum(const unsigned char *input, DES_cblock output[],
  153. \& long length, int out_count, DES_cblock *seed);
  154. \& void DES_string_to_key(const char *str, DES_cblock *key);
  155. \& void DES_string_to_2keys(const char *str, DES_cblock *key1, DES_cblock *key2);
  156. \&
  157. \& char *DES_fcrypt(const char *buf, const char *salt, char *ret);
  158. \& char *DES_crypt(const char *buf, const char *salt);
  159. .Ve
  160. .SH DESCRIPTION
  161. .IX Header "DESCRIPTION"
  162. All of the functions described on this page are deprecated. Applications should
  163. instead use \fBEVP_EncryptInit_ex\fR\|(3), \fBEVP_EncryptUpdate\fR\|(3) and
  164. \&\fBEVP_EncryptFinal_ex\fR\|(3) or the equivalently named decrypt functions.
  165. .PP
  166. This library contains a fast implementation of the DES encryption
  167. algorithm.
  168. .PP
  169. There are two phases to the use of DES encryption. The first is the
  170. generation of a \fIDES_key_schedule\fR from a key, the second is the
  171. actual encryption. A DES key is of type \fIDES_cblock\fR. This type
  172. consists of 8 bytes with odd parity. The least significant bit in
  173. each byte is the parity bit. The key schedule is an expanded form of
  174. the key; it is used to speed the encryption process.
  175. .PP
  176. \&\fBDES_random_key()\fR generates a random key. The random generator must be
  177. seeded when calling this function.
  178. If the automatic seeding or reseeding of the OpenSSL CSPRNG fails due to
  179. external circumstances (see \fBRAND\fR\|(7)), the operation will fail.
  180. If the function fails, 0 is returned.
  181. .PP
  182. Before a DES key can be used, it must be converted into the
  183. architecture dependent \fIDES_key_schedule\fR via the
  184. \&\fBDES_set_key_checked()\fR or \fBDES_set_key_unchecked()\fR function.
  185. .PP
  186. \&\fBDES_set_key_checked()\fR will check that the key passed is of odd parity
  187. and is not a weak or semi-weak key. If the parity is wrong, then \-1
  188. is returned. If the key is a weak key, then \-2 is returned. If an
  189. error is returned, the key schedule is not generated.
  190. .PP
  191. \&\fBDES_set_key()\fR works like \fBDES_set_key_checked()\fR and remains for
  192. backward compatibility.
  193. .PP
  194. \&\fBDES_set_odd_parity()\fR sets the parity of the passed \fIkey\fR to odd.
  195. .PP
  196. \&\fBDES_is_weak_key()\fR returns 1 if the passed key is a weak key, 0 if it
  197. is ok.
  198. .PP
  199. The following routines mostly operate on an input and output stream of
  200. \&\fIDES_cblock\fRs.
  201. .PP
  202. \&\fBDES_ecb_encrypt()\fR is the basic DES encryption routine that encrypts or
  203. decrypts a single 8\-byte \fIDES_cblock\fR in \fIelectronic code book\fR
  204. (ECB) mode. It always transforms the input data, pointed to by
  205. \&\fIinput\fR, into the output data, pointed to by the \fIoutput\fR argument.
  206. If the \fIencrypt\fR argument is nonzero (DES_ENCRYPT), the \fIinput\fR
  207. (cleartext) is encrypted in to the \fIoutput\fR (ciphertext) using the
  208. key_schedule specified by the \fIschedule\fR argument, previously set via
  209. \&\fIDES_set_key\fR. If \fIencrypt\fR is zero (DES_DECRYPT), the \fIinput\fR (now
  210. ciphertext) is decrypted into the \fIoutput\fR (now cleartext). Input
  211. and output may overlap. \fBDES_ecb_encrypt()\fR does not return a value.
  212. .PP
  213. \&\fBDES_ecb3_encrypt()\fR encrypts/decrypts the \fIinput\fR block by using
  214. three-key Triple-DES encryption in ECB mode. This involves encrypting
  215. the input with \fIks1\fR, decrypting with the key schedule \fIks2\fR, and
  216. then encrypting with \fIks3\fR. This routine greatly reduces the chances
  217. of brute force breaking of DES and has the advantage of if \fIks1\fR,
  218. \&\fIks2\fR and \fIks3\fR are the same, it is equivalent to just encryption
  219. using ECB mode and \fIks1\fR as the key.
  220. .PP
  221. The macro \fBDES_ecb2_encrypt()\fR is provided to perform two-key Triple-DES
  222. encryption by using \fIks1\fR for the final encryption.
  223. .PP
  224. \&\fBDES_ncbc_encrypt()\fR encrypts/decrypts using the \fIcipher-block-chaining\fR
  225. (CBC) mode of DES. If the \fIencrypt\fR argument is nonzero, the
  226. routine cipher-block-chain encrypts the cleartext data pointed to by
  227. the \fIinput\fR argument into the ciphertext pointed to by the \fIoutput\fR
  228. argument, using the key schedule provided by the \fIschedule\fR argument,
  229. and initialization vector provided by the \fIivec\fR argument. If the
  230. \&\fIlength\fR argument is not an integral multiple of eight bytes, the
  231. last block is copied to a temporary area and zero filled. The output
  232. is always an integral multiple of eight bytes.
  233. .PP
  234. \&\fBDES_xcbc_encrypt()\fR is RSA's DESX mode of DES. It uses \fIinw\fR and
  235. \&\fIoutw\fR to 'whiten' the encryption. \fIinw\fR and \fIoutw\fR are secret
  236. (unlike the iv) and are as such, part of the key. So the key is sort
  237. of 24 bytes. This is much better than CBC DES.
  238. .PP
  239. \&\fBDES_ede3_cbc_encrypt()\fR implements outer triple CBC DES encryption with
  240. three keys. This means that each DES operation inside the CBC mode is
  241. \&\f(CW\*(C`C=E(ks3,D(ks2,E(ks1,M)))\*(C'\fR. This mode is used by SSL.
  242. .PP
  243. The \fBDES_ede2_cbc_encrypt()\fR macro implements two-key Triple-DES by
  244. reusing \fIks1\fR for the final encryption. \f(CW\*(C`C=E(ks1,D(ks2,E(ks1,M)))\*(C'\fR.
  245. This form of Triple-DES is used by the RSAREF library.
  246. .PP
  247. \&\fBDES_pcbc_encrypt()\fR encrypts/decrypts using the propagating cipher block
  248. chaining mode used by Kerberos v4. Its parameters are the same as
  249. \&\fBDES_ncbc_encrypt()\fR.
  250. .PP
  251. \&\fBDES_cfb_encrypt()\fR encrypts/decrypts using cipher feedback mode. This
  252. method takes an array of characters as input and outputs an array of
  253. characters. It does not require any padding to 8 character groups.
  254. Note: the \fIivec\fR variable is changed and the new changed value needs to
  255. be passed to the next call to this function. Since this function runs
  256. a complete DES ECB encryption per \fInumbits\fR, this function is only
  257. suggested for use when sending a small number of characters.
  258. .PP
  259. \&\fBDES_cfb64_encrypt()\fR
  260. implements CFB mode of DES with 64\-bit feedback. Why is this
  261. useful you ask? Because this routine will allow you to encrypt an
  262. arbitrary number of bytes, without 8 byte padding. Each call to this
  263. routine will encrypt the input bytes to output and then update ivec
  264. and num. num contains 'how far' we are though ivec. If this does
  265. not make much sense, read more about CFB mode of DES.
  266. .PP
  267. \&\fBDES_ede3_cfb64_encrypt()\fR and \fBDES_ede2_cfb64_encrypt()\fR is the same as
  268. \&\fBDES_cfb64_encrypt()\fR except that Triple-DES is used.
  269. .PP
  270. \&\fBDES_ofb_encrypt()\fR encrypts using output feedback mode. This method
  271. takes an array of characters as input and outputs an array of
  272. characters. It does not require any padding to 8 character groups.
  273. Note: the \fIivec\fR variable is changed and the new changed value needs to
  274. be passed to the next call to this function. Since this function runs
  275. a complete DES ECB encryption per \fInumbits\fR, this function is only
  276. suggested for use when sending a small number of characters.
  277. .PP
  278. \&\fBDES_ofb64_encrypt()\fR is the same as \fBDES_cfb64_encrypt()\fR using Output
  279. Feed Back mode.
  280. .PP
  281. \&\fBDES_ede3_ofb64_encrypt()\fR and \fBDES_ede2_ofb64_encrypt()\fR is the same as
  282. \&\fBDES_ofb64_encrypt()\fR, using Triple-DES.
  283. .PP
  284. The following functions are included in the DES library for
  285. compatibility with the MIT Kerberos library.
  286. .PP
  287. \&\fBDES_cbc_cksum()\fR produces an 8 byte checksum based on the input stream
  288. (via CBC encryption). The last 4 bytes of the checksum are returned
  289. and the complete 8 bytes are placed in \fIoutput\fR. This function is
  290. used by Kerberos v4. Other applications should use
  291. \&\fBEVP_DigestInit\fR\|(3) etc. instead.
  292. .PP
  293. \&\fBDES_quad_cksum()\fR is a Kerberos v4 function. It returns a 4 byte
  294. checksum from the input bytes. The algorithm can be iterated over the
  295. input, depending on \fIout_count\fR, 1, 2, 3 or 4 times. If \fIoutput\fR is
  296. non-NULL, the 8 bytes generated by each pass are written into
  297. \&\fIoutput\fR.
  298. .PP
  299. The following are DES-based transformations:
  300. .PP
  301. \&\fBDES_fcrypt()\fR is a fast version of the Unix \fBcrypt\fR\|(3) function. This
  302. version takes only a small amount of space relative to other fast
  303. \&\fBcrypt()\fR implementations. This is different to the normal \fBcrypt()\fR in
  304. that the third parameter is the buffer that the return value is
  305. written into. It needs to be at least 14 bytes long. This function
  306. is thread safe, unlike the normal \fBcrypt()\fR.
  307. .PP
  308. \&\fBDES_crypt()\fR is a faster replacement for the normal system \fBcrypt()\fR.
  309. This function calls \fBDES_fcrypt()\fR with a static array passed as the
  310. third parameter. This mostly emulates the normal non-thread-safe semantics
  311. of \fBcrypt\fR\|(3).
  312. The \fBsalt\fR must be two ASCII characters.
  313. .PP
  314. The values returned by \fBDES_fcrypt()\fR and \fBDES_crypt()\fR are terminated by NUL
  315. character.
  316. .PP
  317. \&\fBDES_enc_write()\fR writes \fIlen\fR bytes to file descriptor \fIfd\fR from
  318. buffer \fIbuf\fR. The data is encrypted via \fIpcbc_encrypt\fR (default)
  319. using \fIsched\fR for the key and \fIiv\fR as a starting vector. The actual
  320. data send down \fIfd\fR consists of 4 bytes (in network byte order)
  321. containing the length of the following encrypted data. The encrypted
  322. data then follows, padded with random data out to a multiple of 8
  323. bytes.
  324. .SH BUGS
  325. .IX Header "BUGS"
  326. \&\fBDES_cbc_encrypt()\fR does not modify \fBivec\fR; use \fBDES_ncbc_encrypt()\fR
  327. instead.
  328. .PP
  329. \&\fBDES_cfb_encrypt()\fR and \fBDES_ofb_encrypt()\fR operates on input of 8 bits.
  330. What this means is that if you set numbits to 12, and length to 2, the
  331. first 12 bits will come from the 1st input byte and the low half of
  332. the second input byte. The second 12 bits will have the low 8 bits
  333. taken from the 3rd input byte and the top 4 bits taken from the 4th
  334. input byte. The same holds for output. This function has been
  335. implemented this way because most people will be using a multiple of 8
  336. and because once you get into pulling bytes input bytes apart things
  337. get ugly!
  338. .PP
  339. \&\fBDES_string_to_key()\fR is available for backward compatibility with the
  340. MIT library. New applications should use a cryptographic hash function.
  341. The same applies for \fBDES_string_to_2key()\fR.
  342. .SH NOTES
  343. .IX Header "NOTES"
  344. The \fBdes\fR library was written to be source code compatible with
  345. the MIT Kerberos library.
  346. .PP
  347. Applications should use the higher level functions
  348. \&\fBEVP_EncryptInit\fR\|(3) etc. instead of calling these
  349. functions directly.
  350. .PP
  351. Single-key DES is insecure due to its short key size. ECB mode is
  352. not suitable for most applications; see \fBdes_modes\fR\|(7).
  353. .SH "RETURN VALUES"
  354. .IX Header "RETURN VALUES"
  355. \&\fBDES_set_key()\fR, \fBDES_key_sched()\fR, and \fBDES_set_key_checked()\fR
  356. return 0 on success or negative values on error.
  357. .PP
  358. \&\fBDES_is_weak_key()\fR returns 1 if the passed key is a weak key, 0 if it
  359. is ok.
  360. .PP
  361. \&\fBDES_cbc_cksum()\fR and \fBDES_quad_cksum()\fR return 4\-byte integer representing the
  362. last 4 bytes of the checksum of the input.
  363. .PP
  364. \&\fBDES_fcrypt()\fR returns a pointer to the caller-provided buffer and \fBDES_crypt()\fR \-
  365. to a static buffer on success; otherwise they return NULL.
  366. .SH "SEE ALSO"
  367. .IX Header "SEE ALSO"
  368. \&\fBdes_modes\fR\|(7),
  369. \&\fBEVP_EncryptInit\fR\|(3)
  370. .SH HISTORY
  371. .IX Header "HISTORY"
  372. All of these functions were deprecated in OpenSSL 3.0.
  373. .PP
  374. The requirement that the \fBsalt\fR parameter to \fBDES_crypt()\fR and \fBDES_fcrypt()\fR
  375. be two ASCII characters was first enforced in
  376. OpenSSL 1.1.0. Previous versions tried to use the letter uppercase \fBA\fR
  377. if both character were not present, and could crash when given non-ASCII
  378. on some platforms.
  379. .SH COPYRIGHT
  380. .IX Header "COPYRIGHT"
  381. Copyright 2000\-2020 The OpenSSL Project Authors. All Rights Reserved.
  382. .PP
  383. Licensed under the Apache License 2.0 (the "License"). You may not use
  384. this file except in compliance with the License. You can obtain a copy
  385. in the file LICENSE in the source distribution or at
  386. <https://www.openssl.org/source/license.html>.