---
name: "libressl-DES_set_key"
section: "3"
platform: "linux"
package: "libressl"
version: "4.4.0-1"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/libressl-DES_set_key"
---

# libressl-DES\_set\_key(3)

DES encryption

## 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\_cbc\_cksum**, **DES\_quad\_cksum**, **DES\_string\_to\_key**, **DES\_string\_to\_2keys**, **DES\_fcrypt**, **DES\_crypt** – DES encryption

## SYNOPSIS

Crypto Library (libcrypto, -lcrypto)

**#include \<openssl/des.h\>**

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

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

## 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 *DES\_key\_schedule* from a key, and the second is the actual encryption. A DES key is of type *DES\_cblock*. This type 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.

**DES\_random\_key**() generates a random key in odd parity.

Before a DES key can be used, it must be converted into the architecture dependent *DES\_key\_schedule* via the **DES\_set\_key\_checked**() or **DES\_set\_key\_unchecked**() function.

**DES\_set\_key\_checked**() will 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\_set\_key**() works like **DES\_set\_key\_checked**() if the *DES\_check\_key* flag is non-zero, otherwise like **DES\_set\_key\_unchecked**(). These functions are available for compatibility; it is recommended to use a function that does not depend on a global variable.

**DES\_set\_odd\_parity**() sets the parity of the passed *key* to odd.

The following routines mostly operate on an input and output stream of *DES\_cblock*s.

**DES\_ecb\_encrypt**() is the basic DES encryption routine that encrypts or decrypts a single 8-byte *DES\_cblock* in electronic code book (ECB) mode. It always transforms the input data, pointed to by *input*, into the output data, pointed to by the *output* argument. If the *enc* argument is non-zero (DES\_ENCRYPT), the *input* (cleartext) is encrypted into the *output* (ciphertext) using the key\_schedule specified by the *schedule* argument, previously set via **DES\_set\_key**(). If *enc* is zero (DES\_DECRYPT), the *input* (now ciphertext) is decrypted into the *output* (now cleartext). Input and output may overlap. **DES\_ecb\_encrypt**() does not return a value.

**DES\_ecb3\_encrypt**() encrypts/decrypts the *input* block by using three-key Triple-DES encryption in ECB mode. This involves encrypting the input with *ks1*, decrypting with the key schedule *ks2*, and then encrypting with *ks3*. This routine greatly reduces the chances of brute force breaking of DES and has the advantage of if *ks1*, *ks2*, and *ks3* are the same, it is equivalent to just encryption using ECB mode and *ks1* as the key.

The macro **DES\_ecb2\_encrypt**() is provided to perform two-key Triple-DES encryption by using *ks1* for the final encryption.

**DES\_ncbc\_encrypt**() encrypts/decrypts using the cipher-block-chaining (CBC) mode of DES. If the *enc* argument is non-zero, the routine cipher-block-chain encrypts the cleartext data pointed to by the *input* argument into the ciphertext pointed to by the *output* argument, using the key schedule provided by the *schedule* argument, and initialization vector provided by the *ivec* argument. If the *length* 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.

**DES\_xcbc\_encrypt**() is RSA's DESX mode of DES. It uses *inw* and *outw* to "whiten" the encryption. *inw* and *outw* 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.

**DES\_ede3\_cbc\_encrypt**() implements outer triple CBC DES encryption with three keys. This means that each DES operation inside the CBC mode is "C=E(ks3,D(ks2,E(ks1,M)))". This mode is used by SSL.

The **DES\_ede2\_cbc\_encrypt**() macro implements two-key Triple-DES by reusing *ks1* for the final encryption. "C=E(ks1,D(ks2,E(ks1,M)))". This form of Triple-DES is used by the RSAREF library.

**DES\_pcbc\_encrypt**() encrypts/decrypts using the propagating cipher block chaining mode used by Kerberos v4. Its parameters are the same as **DES\_ncbc\_encrypt**().

**DES\_cfb\_encrypt**() encrypts/decrypts using cipher feedback mode. This method takes an array of characters as input and outputs an 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 a small number of characters.

**DES\_cfb64\_encrypt**() implements CFB mode of DES with 64-bit feedback. Why is this useful you ask? Because this routine will allow you to encrypt an arbitrary number of bytes, without 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.

The **DES\_ede3\_cfb64\_encrypt**() function and the **DES\_ede2\_cfb64\_encrypt**() macro are the same as **DES\_cfb64\_encrypt**() except that Triple-DES is used.

**DES\_ofb\_encrypt**() encrypts using output feedback mode. This method takes an array of characters as input and outputs an 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 a small number of characters.

**DES\_ofb64\_encrypt**() is the same as **DES\_cfb64\_encrypt**() using Output Feed Back mode.

The **DES\_ede3\_ofb64\_encrypt**() function and the **DES\_ede2\_ofb64\_encrypt**() macro are the same as **DES\_ofb64\_encrypt**(), using Triple-DES.

The following functions are included in the DES library for compatibility with the MIT Kerberos library.

**DES\_cbc\_cksum**() produces an 8-byte checksum based on the input stream (via CBC encryption). The last 4 bytes of the checksum are returned and the complete 8 bytes are placed in *output*. This function is used by Kerberos v4. Other applications should use EVP\_DigestInit(3) etc. instead.

**DES\_quad\_cksum**() is a Kerberos v4 function. It returns a 4-byte checksum from the input bytes. The algorithm can be iterated over the input, depending on *out\_count*, 1, 2, 3 or 4 times. If *output* is non-NULL, the 8 bytes generated by each pass are written into *output*.

The following are DES-based transformations:

**DES\_fcrypt**() is a fast version of the Unix [crypt(3)](/crypt/3.md) function. The *salt* must be two ASCII characters. This version is different from 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. The fourteenth byte is set to NUL. This version takes only a small amount of space relative to other fast crypt implementations. It is thread safe, unlike the normal crypt.

**DES\_crypt**() is a faster replacement for the normal system [crypt(3)](/crypt/3.md). This function calls **DES\_fcrypt**() with a static array passed as the third parameter. This emulates the normal non-thread safe semantics of [crypt(3)](/crypt/3.md).

## RETURN VALUES

**DES\_set\_key**(), **DES\_key\_sched**(), and **DES\_set\_key\_checked**() return 0 on success or a negative value on error.

**DES\_is\_weak\_key**() returns 1 if the passed key is a weak key or 0 if it is ok.

**DES\_cbc\_cksum**() and **DES\_quad\_cksum**() return a 4-byte integer representing the last 4 bytes of the checksum of the input.

**DES\_fcrypt**() returns a pointer to the caller-provided buffer *ret*, and **DES\_crypt**() returns a pointer to a static buffer. Both are allowed to return NULL to indicate failure, but currently, they cannot fail.

## SEE ALSO

[crypt(3)](/crypt/3.md), EVP\_des\_cbc(3), EVP\_EncryptInit(3)

## STANDARDS

ANSI X3.106

The DES library was initially written to be source code compatible with the MIT Kerberos library.

## HISTORY

**DES\_random\_key**(), **DES\_set\_key**(), **DES\_key\_sched**(), **DES\_set\_odd\_parity**(), **DES\_is\_weak\_key**(), **DES\_ecb\_encrypt**(), **DES\_cfb\_encrypt**(), **DES\_ofb\_encrypt**(), **DES\_pcbc\_encrypt**(), **DES\_cfb64\_encrypt**(), **DES\_ofb64\_encrypt**(), **DES\_ede3\_cbc\_encrypt**(), **DES\_cbc\_cksum**(), **DES\_quad\_cksum**(), **DES\_string\_to\_key**(), **DES\_string\_to\_2keys**(), and **DES\_crypt**() appeared in SSLeay 0.4 or earlier. **DES\_ncbc\_encrypt**() first appeared in SSLeay 0.4.2. **DES\_ede2\_cbc\_encrypt**() first appeared in SSLeay 0.4.4. **DES\_ecb2\_encrypt**(), **DES\_ecb3\_encrypt**(), **DES\_ede2\_cfb64\_encrypt**(), **DES\_ede2\_ofb64\_encrypt**(), **DES\_ede3\_cfb64\_encrypt**(), and **DES\_ede3\_ofb64\_encrypt**() first appeared in SSLeay 0.5.1. **DES\_xcbc\_encrypt**() first appeared in SSLeay 0.6.2. **DES\_fcrypt**() first appeared in SSLeay 0.6.5. These functions have been available since OpenBSD 2.4.

**DES\_set\_key\_checked**() and **DES\_set\_key\_unchecked**() first appeared in OpenSSL 0.9.5 and have been available since OpenBSD 2.7.

In OpenSSL 0.9.7 and OpenBSD 3.2, all **des\_** functions were renamed to **DES\_** to avoid clashes with older versions of libdes.

## AUTHORS

Eric Young ⟨*eay@cryptsoft.com*⟩

## CAVEATS

Single-key DES is insecure due to its short key size. ECB mode is not suitable for most applications.

## BUGS

DES\_cbc\_encrypt does not modify *ivec*; use **DES\_ncbc\_encrypt**() instead.

**DES\_cfb\_encrypt**() and **DES\_ofb\_encrypt**() 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 input bytes apart things get ugly!

**DES\_string\_to\_key**() is available for backward compatibility with the MIT library. New applications should use a cryptographic hash function. The same applies for **DES\_string\_to\_2key**().
