1 .\" $OpenBSD: DES_set_key.3,v 1.7 2017/02/09 03:43:05 dtucker Exp $
2 .\" OpenSSL c7497f34 Aug 14 10:50:26 2014 -0400
4 .\" --------------------------------------------------------------------------
5 .\" Major patches to this file were contributed by
6 .\" Ulf Moeller <ulf@openssl.org>, Ben Laurie <ben@openssl.org>,
7 .\" and Richard Levitte <levitte@openssl.org>.
8 .\" --------------------------------------------------------------------------
9 .\" Copyright (c) 2000, 2001 The OpenSSL Project. All rights reserved.
11 .\" Redistribution and use in source and binary forms, with or without
12 .\" modification, are permitted provided that the following conditions
15 .\" 1. Redistributions of source code must retain the above copyright
16 .\" notice, this list of conditions and the following disclaimer.
18 .\" 2. Redistributions in binary form must reproduce the above copyright
19 .\" notice, this list of conditions and the following disclaimer in
20 .\" the documentation and/or other materials provided with the
23 .\" 3. All advertising materials mentioning features or use of this
24 .\" software must display the following acknowledgment:
25 .\" "This product includes software developed by the OpenSSL Project
26 .\" for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
28 .\" 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
29 .\" endorse or promote products derived from this software without
30 .\" prior written permission. For written permission, please contact
31 .\" openssl-core@openssl.org.
33 .\" 5. Products derived from this software may not be called "OpenSSL"
34 .\" nor may "OpenSSL" appear in their names without prior written
35 .\" permission of the OpenSSL Project.
37 .\" 6. Redistributions of any form whatsoever must retain the following
39 .\" "This product includes software developed by the OpenSSL Project
40 .\" for use in the OpenSSL Toolkit (http://www.openssl.org/)"
42 .\" THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
43 .\" EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
45 .\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
46 .\" ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47 .\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
48 .\" NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
49 .\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
51 .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52 .\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
53 .\" OF THE POSSIBILITY OF SUCH DAMAGE.
55 .\" --------------------------------------------------------------------------
56 .\" Parts of this file are derived from SSLeay documentation,
57 .\" which is covered by the following Copyright and license:
58 .\" --------------------------------------------------------------------------
60 .\" Copyright (C) 1995-1998 Tim Hudson (tjh@cryptsoft.com)
61 .\" All rights reserved.
63 .\" This package is an SSL implementation written
64 .\" by Eric Young (eay@cryptsoft.com).
65 .\" The implementation was written so as to conform with Netscapes SSL.
67 .\" This library is free for commercial and non-commercial use as long as
68 .\" the following conditions are aheared to. The following conditions
69 .\" apply to all code found in this distribution, be it the RC4, RSA,
70 .\" lhash, DES, etc., code; not just the SSL code. The SSL documentation
71 .\" included with this distribution is covered by the same copyright terms
72 .\" except that the holder is Tim Hudson (tjh@cryptsoft.com).
74 .\" Copyright remains Eric Young's, and as such any Copyright notices in
75 .\" the code are not to be removed.
76 .\" If this package is used in a product, Eric Young should be given
77 .\" attribution as the author of the parts of the library used.
78 .\" This can be in the form of a textual message at program startup or
79 .\" in documentation (online or textual) provided with the package.
81 .\" Redistribution and use in source and binary forms, with or without
82 .\" modification, are permitted provided that the following conditions
84 .\" 1. Redistributions of source code must retain the copyright
85 .\" notice, this list of conditions and the following disclaimer.
86 .\" 2. Redistributions in binary form must reproduce the above copyright
87 .\" notice, this list of conditions and the following disclaimer in the
88 .\" documentation and/or other materials provided with the distribution.
89 .\" 3. All advertising materials mentioning features or use of this software
90 .\" must display the following acknowledgement:
91 .\" "This product includes cryptographic software written by
92 .\" Eric Young (eay@cryptsoft.com)"
93 .\" The word 'cryptographic' can be left out if the rouines from the
94 .\" library being used are not cryptographic related :-).
95 .\" 4. If you include any Windows specific code (or a derivative thereof)
96 .\" from the apps directory (application code) you must include an
97 .\" acknowledgement: "This product includes software written by
98 .\" Tim Hudson (tjh@cryptsoft.com)"
100 .\" THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
101 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
102 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
103 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
104 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
105 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
106 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
107 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
108 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
109 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
112 .\" The licence and distribution terms for any publically available version or
113 .\" derivative of this code cannot be changed. i.e. this code cannot simply be
114 .\" copied and put under another distribution licence
115 .\" [including the GNU Public Licence.]
117 .Dd $Mdocdate: February 9 2017 $
124 .Nm DES_set_key_checked ,
125 .Nm DES_set_key_unchecked ,
126 .Nm DES_set_odd_parity ,
127 .Nm DES_is_weak_key ,
128 .Nm DES_ecb_encrypt ,
129 .Nm DES_ecb2_encrypt ,
130 .Nm DES_ecb3_encrypt ,
131 .Nm DES_ncbc_encrypt ,
132 .Nm DES_cfb_encrypt ,
133 .Nm DES_ofb_encrypt ,
134 .Nm DES_pcbc_encrypt ,
135 .Nm DES_cfb64_encrypt ,
136 .Nm DES_ofb64_encrypt ,
137 .Nm DES_xcbc_encrypt ,
138 .Nm DES_ede2_cbc_encrypt ,
139 .Nm DES_ede2_cfb64_encrypt ,
140 .Nm DES_ede2_ofb64_encrypt ,
141 .Nm DES_ede3_cbc_encrypt ,
142 .Nm DES_ede3_cbcm_encrypt ,
143 .Nm DES_ede3_cfb64_encrypt ,
144 .Nm DES_ede3_ofb64_encrypt ,
147 .Nm DES_string_to_key ,
148 .Nm DES_string_to_2keys ,
158 .Fa "DES_cblock *ret"
162 .Fa "const_DES_cblock *key"
163 .Fa "DES_key_schedule *schedule"
167 .Fa "const_DES_cblock *key"
168 .Fa "DES_key_schedule *schedule"
171 .Fo DES_set_key_checked
172 .Fa "const_DES_cblock *key"
173 .Fa "DES_key_schedule *schedule"
176 .Fo DES_set_key_unchecked
177 .Fa "const_DES_cblock *key"
178 .Fa "DES_key_schedule *schedule"
181 .Fo DES_set_odd_parity
182 .Fa "DES_cblock *key"
186 .Fa "const_DES_cblock *key"
190 .Fa "const_DES_cblock *input"
191 .Fa "DES_cblock *output"
192 .Fa "DES_key_schedule *ks"
197 .Fa "const_DES_cblock *input"
198 .Fa "DES_cblock *output"
199 .Fa "DES_key_schedule *ks1"
200 .Fa "DES_key_schedule *ks2"
205 .Fa "const_DES_cblock *input"
206 .Fa "DES_cblock *output"
207 .Fa "DES_key_schedule *ks1"
208 .Fa "DES_key_schedule *ks2"
209 .Fa "DES_key_schedule *ks3"
214 .Fa "const unsigned char *input"
215 .Fa "unsigned char *output"
217 .Fa "DES_key_schedule *schedule"
218 .Fa "DES_cblock *ivec"
223 .Fa "const unsigned char *in"
224 .Fa "unsigned char *out"
227 .Fa "DES_key_schedule *schedule"
228 .Fa "DES_cblock *ivec"
233 .Fa "const unsigned char *in"
234 .Fa "unsigned char *out"
237 .Fa "DES_key_schedule *schedule"
238 .Fa "DES_cblock *ivec"
242 .Fa "const unsigned char *input"
243 .Fa "unsigned char *output"
245 .Fa "DES_key_schedule *schedule"
246 .Fa "DES_cblock *ivec"
250 .Fo DES_cfb64_encrypt
251 .Fa "const unsigned char *in"
252 .Fa "unsigned char *out"
254 .Fa "DES_key_schedule *schedule"
255 .Fa "DES_cblock *ivec"
260 .Fo DES_ofb64_encrypt
261 .Fa "const unsigned char *in"
262 .Fa "unsigned char *out"
264 .Fa "DES_key_schedule *schedule"
265 .Fa "DES_cblock *ivec"
270 .Fa "const unsigned char *input"
271 .Fa "unsigned char *output"
273 .Fa "DES_key_schedule *schedule"
274 .Fa "DES_cblock *ivec"
275 .Fa "const_DES_cblock *inw"
276 .Fa "const_DES_cblock *outw"
280 .Fo DES_ede2_cbc_encrypt
281 .Fa "const unsigned char *input"
282 .Fa "unsigned char *output"
284 .Fa "DES_key_schedule *ks1"
285 .Fa "DES_key_schedule *ks2"
286 .Fa "DES_cblock *ivec"
290 .Fo DES_ede2_cfb64_encrypt
291 .Fa "const unsigned char *in"
292 .Fa "unsigned char *out"
294 .Fa "DES_key_schedule *ks1"
295 .Fa "DES_key_schedule *ks2"
296 .Fa "DES_cblock *ivec"
301 .Fo DES_ede2_ofb64_encrypt
302 .Fa "const unsigned char *in"
303 .Fa "unsigned char *out"
305 .Fa "DES_key_schedule *ks1"
306 .Fa "DES_key_schedule *ks2"
307 .Fa "DES_cblock *ivec"
311 .Fo DES_ede3_cbc_encrypt
312 .Fa "const unsigned char *input"
313 .Fa "unsigned char *output"
315 .Fa "DES_key_schedule *ks1"
316 .Fa "DES_key_schedule *ks2"
317 .Fa "DES_key_schedule *ks3"
318 .Fa "DES_cblock *ivec"
322 .Fo DES_ede3_cbcm_encrypt
323 .Fa "const unsigned char *in"
324 .Fa "unsigned char *out"
326 .Fa "DES_key_schedule *ks1"
327 .Fa "DES_key_schedule *ks2"
328 .Fa "DES_key_schedule *ks3"
329 .Fa "DES_cblock *ivec1"
330 .Fa "DES_cblock *ivec2"
334 .Fo DES_ede3_cfb64_encrypt
335 .Fa "const unsigned char *in"
336 .Fa "unsigned char *out"
338 .Fa "DES_key_schedule *ks1"
339 .Fa "DES_key_schedule *ks2"
340 .Fa "DES_key_schedule *ks3"
341 .Fa "DES_cblock *ivec"
346 .Fo DES_ede3_ofb64_encrypt
347 .Fa "const unsigned char *in"
348 .Fa "unsigned char *out"
350 .Fa "DES_key_schedule *ks1"
351 .Fa "DES_key_schedule *ks2"
352 .Fa "DES_key_schedule *ks3"
353 .Fa "DES_cblock *ivec"
358 .Fa "const unsigned char *input"
359 .Fa "DES_cblock *output"
361 .Fa "DES_key_schedule *schedule"
362 .Fa "const_DES_cblock *ivec"
366 .Fa "const unsigned char *input"
367 .Fa "DES_cblock output[]"
370 .Fa "DES_cblock *seed"
373 .Fo DES_string_to_key
374 .Fa "const char *str"
375 .Fa "DES_cblock *key"
378 .Fo DES_string_to_2keys
379 .Fa "const char *str"
380 .Fa "DES_cblock *key1"
381 .Fa "DES_cblock *key2"
385 .Fa "const char *buf"
386 .Fa "const char *salt"
391 .Fa "const char *buf"
392 .Fa "const char *salt"
399 .Fa "DES_key_schedule *sched"
405 .Fa "const void *buf"
407 .Fa "DES_key_schedule *sched"
411 This library contains a fast implementation of the DES encryption
414 There are two phases to the use of DES encryption.
415 The first is the generation of a
417 from a key, and the second is the actual encryption.
420 This type consists of 8 bytes with odd parity.
421 The least significant bit in each byte is the parity bit.
422 The key schedule is an expanded form of the key; it is used to speed the
426 generates a random key in odd parity.
428 Before a DES key can be used, it must be converted into the architecture
432 .Fn DES_set_key_checked
434 .Fn DES_set_key_unchecked
437 .Fn DES_set_key_checked
438 will check that the key passed is of odd parity and is not a weak or
440 If the parity is wrong, then -1 is returned.
441 If the key is a weak key, then -2 is returned.
442 If an error is returned, the key schedule is not generated.
446 .Fn DES_set_key_checked
449 flag is non-zero, otherwise like
450 .Fn DES_set_key_unchecked .
451 These functions are available for compatibility; it is recommended to
452 use a function that does not depend on a global variable.
454 .Fn DES_set_odd_parity
455 sets the parity of the passed
460 returns 1 if the passed key is a weak key or 0 if it is ok.
462 The following routines mostly operate on an input and output stream of
463 .Vt DES_cblock Ns s .
466 is the basic DES encryption routine that encrypts or decrypts a single
469 in electronic code book (ECB) mode.
470 It always transforms the input data, pointed to by
472 into the output data, pointed to by the
481 (cleartext) is encrypted into the
483 (ciphertext) using the key_schedule specified by the
485 argument, previously set via
493 (now ciphertext) is decrypted into the
496 Input and output may overlap.
498 does not return a value.
501 encrypts/decrypts the
503 block by using three-key Triple-DES encryption in ECB mode.
504 This involves encrypting the input with
506 decrypting with the key schedule
508 and then encrypting with
510 This routine greatly reduces the chances of brute force breaking of DES
511 and has the advantage of if
516 are the same, it is equivalent to just encryption using ECB mode and
522 is provided to perform two-key Triple-DES encryption by using
524 for the final encryption.
527 encrypts/decrypts using the cipher-block-chaining (CBC) mode of DES.
530 argument is non-zero, the routine cipher-block-chain encrypts the
531 cleartext data pointed to by the
533 argument into the ciphertext pointed to by the
535 argument, using the key schedule provided by the
537 argument, and initialization vector provided by the
542 argument is not an integral multiple of eight bytes, the last block is
543 copied to a temporary area and zero filled.
544 The output is always an integral multiple of eight bytes.
547 is RSA's DESX mode of DES.
552 to "whiten" the encryption.
556 are secret (unlike the iv) and are as such, part of the key.
557 So the key is sort of 24 bytes.
558 This is much better than CBC DES.
560 .Fn DES_ede3_cbc_encrypt
561 implements outer triple CBC DES encryption with three keys.
562 This means that each DES operation inside the CBC mode is really an
563 .Qq Li C=E(ks3,D(ks2,E(ks1,M))) .
564 This mode is used by SSL.
567 .Fn DES_ede2_cbc_encrypt
568 macro implements two-key Triple-DES by reusing
570 for the final encryption.
571 .Qq Li C=E(ks1,D(ks2,E(ks1,M))) .
572 This form of Triple-DES is used by the RSAREF library.
575 encrypt/decrypts using the propagating cipher block chaining mode used
577 Its parameters are the same as
578 .Fn DES_ncbc_encrypt .
581 encrypt/decrypts using cipher feedback mode.
582 This method takes an array of characters as input and outputs an array
584 It does not require any padding to 8 character groups.
587 variable is changed and the new changed value needs to be passed to the
588 next call to this function.
589 Since this function runs a complete DES ECB encryption per
591 this function is only suggested for use when sending small numbers of
594 .Fn DES_cfb64_encrypt
595 implements CFB mode of DES with 64bit feedback.
596 Why is this useful you ask?
597 Because this routine will allow you to encrypt an arbitrary number of
598 bytes, no 8 byte padding.
599 Each call to this routine will encrypt the input bytes to output and
600 then update ivec and num.
601 num contains "how far" we are though ivec.
602 If this does not make much sense, read more about cfb mode of DES :-).
604 .Fn DES_ede3_cfb64_encrypt
606 .Fn DES_ede2_cfb64_encrypt
608 .Fn DES_cfb64_encrypt
609 except that Triple-DES is used.
612 encrypts using output feedback mode.
613 This method takes an array of characters as input and outputs an array
615 It does not require any padding to 8 character groups.
618 variable is changed and the new changed value needs to be passed to the
619 next call to this function.
620 Since this function runs a complete DES ECB encryption per numbits, this
621 function is only suggested for use when sending small numbers of
624 .Fn DES_ofb64_encrypt
626 .Fn DES_cfb64_encrypt
627 using Output Feed Back mode.
629 .Fn DES_ede3_ofb64_encrypt
631 .Fn DES_ede2_ofb64_encrypt
633 .Fn DES_ofb64_encrypt ,
636 The following functions are included in the DES library for
637 compatibility with the MIT Kerberos library.
640 produces an 8-byte checksum based on the input stream (via CBC
642 The last 4 bytes of the checksum are returned and the complete 8 bytes
645 This function is used by Kerberos v4.
646 Other applications should use
651 is a Kerberos v4 function.
652 It returns a 4-byte checksum from the input bytes.
653 The algorithm can be iterated over the input, depending on
660 the 8 bytes generated by each pass are written into
663 The following are DES-based transformations:
666 is a fast version of the Unix
669 This version takes only a small amount of space relative to other
670 fast crypt implementations.
671 This is different to the normal crypt in that the third parameter is the
672 buffer that the return value is written into.
673 It needs to be at least 14 bytes long.
674 This function is thread safe, unlike the normal crypt.
677 is a faster replacement for the normal system
681 with a static array passed as the third parameter.
682 This emulates the normal non-thread safe semantics of
688 bytes to file descriptor
692 The data is encrypted via
698 as a starting vector.
699 The actual data send down
701 consists of 4 bytes (in network byte order) containing the length of the
702 following encrypted data.
703 The encrypted data then follows, padded with random data out to a
709 bytes from file descriptor
713 The data being read from
715 is assumed to have come from
717 and is decrypted using
719 for the key schedule and
721 for the initial vector.
724 The data format used by
728 has a cryptographic weakness: when asked to write more than
732 will split the data into several chunks that are all encrypted using the
734 So don't use these functions unless you are sure you know what
735 you do (in which case you might not want to use them anyway).
736 They cannot handle non-blocking sockets.
738 uses an internal state and thus cannot be used on multiple files.
741 is used to specify the encryption mode to use with
745 (the default), DES_pcbc_encrypt is used.
748 DES_cbc_encrypt is used.
755 library provides higher-level encryption functions.
759 The DES library was initially written to be source code compatible
760 with the MIT Kerberos library.
762 In OpenSSL 0.9.7, all des_ functions were renamed to DES_ to avoid
763 clashes with older versions of libdes.
765 .Fn DES_set_key_checked
767 .Fn DES_set_key_unchecked
768 were added in OpenSSL 0.9.5.
770 .Fn des_generate_random_block ,
771 .Fn des_init_random_number_generator ,
772 .Fn des_new_random_key ,
773 .Fn des_set_random_generator_seed ,
774 .Fn des_set_sequence_number ,
777 are used in newer versions of Kerberos but are not implemented here.
780 generated cryptographically weak random data in SSLeay and in OpenSSL
781 prior version 0.9.5, as well as in the original MIT library.
783 .An Eric Young Aq Mt eay@cryptsoft.com
785 Single-key DES is insecure due to its short key size.
786 ECB mode is not suitable for most applications.
788 DES_cbc_encrypt does not modify
797 operates on input of 8 bits.
798 What this means is that if you set numbits to 12, and length to 2, the
799 first 12 bits will come from the 1st input byte and the low half of the
801 The second 12 bits will have the low 8 bits taken from the 3rd input
802 byte and the top 4 bits taken from the 4th input byte.
803 The same holds for output.
804 This function has been implemented this way because most people will be
805 using a multiple of 8 and because once you get into pulling input
806 bytes apart things get ugly!
808 .Fn DES_string_to_key
809 is available for backward compatibility with the MIT library.
810 New applications should use a cryptographic hash function.
812 .Fn DES_string_to_2key .