DES_RANDOM_KEY(3ossl)
๐ฆ DES_RANDOM_KEY(3ossl) โ okay so, DES ๐
Hi bestie!! Welcome to the 3ossl manual, part one of this whole saga, and listen โ today we're talking about DES. Like, old-school DES. The whole squad of DES functions. There's genuinely a lot of them and I'm not gonna gatekeep a single one, so buckle up ๐
โจ NAME โจ
So the whole cast of characters here is: 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_cfb64_encrypt, DES_ede3_ofb64_encrypt, DES_cbc_cksum, DES_quad_cksum, DES_string_to_key, DES_string_to_2keys, DES_fcrypt, and DES_crypt โ and that entire iconic lineup is, collectively, DES encryption ๐ฅ
Let me break down the friend group so you actually know who's who:
- the key girlies ๐๏ธ โ
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 - the ECB trio ๐ฏ โ
DES_ecb_encrypt,DES_ecb2_encrypt,DES_ecb3_encrypt - the single-key cipher modes ๐ โ
DES_ncbc_encrypt,DES_cfb_encrypt,DES_ofb_encrypt,DES_pcbc_encrypt,DES_cfb64_encrypt,DES_ofb64_encrypt,DES_xcbc_encrypt - the EDE2 duo ๐ซถ โ
DES_ede2_cbc_encrypt,DES_ede2_cfb64_encrypt,DES_ede2_ofb64_encrypt - the EDE3 trio ๐ โ
DES_ede3_cbc_encrypt,DES_ede3_cfb64_encrypt,DES_ede3_ofb64_encrypt - the checksum besties ๐งฎ โ
DES_cbc_cksum,DES_quad_cksum - the string-to-key girlies ๐ โ
DES_string_to_key,DES_string_to_2keys - the crypt legends ๐ โ
DES_fcrypt,DES_crypt
โญ SYNOPSIS โญ
First things first, you gotta bring in the header, no cap:
#include <openssl/des.h>
โ ๏ธ Okay so heads up bestie, this is important: the following functions have been deprecated since OpenSSL 3.0, and can be hidden entirely by definingOPENSSL_API_COMPATwith a suitable version value, seeopenssl_user_macros(7):
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_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);
Okay so that's the whole lineup and their exact signatures โ like, don't come for me, I copied them character for character so you can trust them ๐ The actual tea about what each one does is coming up next ๐
๐ DESCRIPTION ๐
โ ๏ธ girlypop warning: all of the functions on this page are deprecated. Like, do not build new stuff on them. Applications should instead useEVP_EncryptInit_ex(3),EVP_EncryptUpdate(3)andEVP_EncryptFinal_ex(3)โ or the equivalently named decrypt functions. That's the vibe. ๐
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. The random generator must be seeded when calling this function. If the automatic seeding or reseeding of the OpenSSL CSPRNG fails due to external circumstances (seeRAND(7)), the operation will fail. If the function fails,0is returned. ๐ญ- Before a DES key can be used, it must be converted into the architecture dependent
DES_key_schedulevia theDES_set_key_checked()orDES_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-1is returned. If the key is a weak key, then-2is returned. If an error is returned, the key schedule is not generated. ๐DES_set_key()โ works likeDES_set_key_checked()and remains for backward compatibility.DES_set_odd_parity()โ sets the parity of the passed key to odd.DES_is_weak_key()โ returns1if the passed key is a weak key,0if it is ok.
The following routines mostly operate on an input and output stream of DES_cblocks. ๐ฆ
DES_ecb_encrypt()โ is the basic DES encryption routine that encrypts or decrypts a single 8-byteDES_cblockin electronic code book (ECB) mode. It always transforms the input data, pointed to byinput, into the output data, pointed to by theoutputargument. If theencryptargument is nonzero (DES_ENCRYPT), the input (cleartext) is encrypted in to the output (ciphertext) using thekey_schedulespecified by thescheduleargument, previously set viaDES_set_key. Ifencryptis 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 withks1, decrypting with the key scheduleks2, and then encrypting withks3. This routine greatly reduces the chances of brute force breaking of DES and has the advantage of ifks1,ks2andks3are the same, it is equivalent to just encryption using ECB mode andks1as the key.- The macro
DES_ecb2_encrypt()โ is provided to perform two-key Triple-DES encryption by usingks1for the final encryption. DES_ncbc_encrypt()โ encrypts/decrypts using the cipher-block-chaining (CBC) mode of DES. If theencryptargument is nonzero, 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 initialization 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.DES_xcbc_encrypt()โ is RSA's DESX mode of DES. It usesinwandoutwto 'whiten' the encryption.inwandoutware secret (unlike theiv) 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 reusingks1for 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 asDES_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: theivecvariable 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 pernumbits, 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 updateivecandnum.numcontains 'how far' we are throughivec. If this does not make much sense, read more about CFB mode of DES. ๐งDES_ede3_cfb64_encrypt()andDES_ede2_cfb64_encrypt()โ is the same asDES_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: theivecvariable 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 pernumbits, this function is only suggested for use when sending a small number of characters.DES_ofb64_encrypt()โ is the same asDES_cfb64_encrypt()using Output Feed Back mode.DES_ede3_ofb64_encrypt()andDES_ede2_ofb64_encrypt()โ is the same asDES_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 inoutput. This function is used by Kerberos v4. Other applications should useEVP_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 onout_count, 1, 2, 3 or 4 times. Ifoutputis non-NULL, the 8 bytes generated by each pass are written intooutput.
The following are DES-based transformations: ๐
DES_fcrypt()โ is a fast version of the Unixcrypt(3)function. This version takes only a small amount of space relative to other fastcrypt()implementations. This is different to the normalcrypt()in that the third parameter is the buffer that the return value is written into. It needs to be at least 14 bytes long. This function is thread safe, unlike the normalcrypt().DES_crypt()โ is a faster replacement for the normal systemcrypt(). This function callsDES_fcrypt()with a static array passed as the third parameter. This mostly emulates the normal non-thread-safe semantics ofcrypt(3). Thesaltmust be two ASCII characters.
The values returned by DES_fcrypt() and DES_crypt() are terminated by NUL character.
DES_enc_write()โ writeslenbytes to file descriptorfdfrom bufferbuf. The data is encrypted viapcbc_encrypt(default) usingschedfor the key andivas a starting vector. The actual data send 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.
๐ BUGS ๐
DES_cbc_encrypt()does not modifyivec; useDES_ncbc_encrypt()instead. periodt.DES_cfb_encrypt()andDES_ofb_encrypt()operates on input of 8 bits. What this means is that if you setnumbitsto12, andlengthto2, 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 bytes 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 forDES_string_to_2key().
๐ NOTES ๐
The des library was written to be source code compatible with the MIT Kerberos library.
Applications should use the higher level functions EVP_EncryptInit(3) etc. instead of calling these functions directly.
Single-key DES is insecure due to its short key size. ECB mode is not suitable for most applications; see des_modes(7). ๐จ
๐ RETURN VALUES ๐
DES_set_key(),DES_key_sched(), andDES_set_key_checked()return0on success or negative values on error.DES_is_weak_key()returns1if the passed key is a weak key,0if it is ok.DES_cbc_cksum()andDES_quad_cksum()return 4-byte integer representing the last 4 bytes of the checksum of the input.DES_fcrypt()returns a pointer to the caller-provided buffer andDES_crypt()โ to a static buffer on success; otherwise they returnNULL.
๐ SEE ALSO ๐
des_modes(7), EVP_EncryptInit(3)
๐ฐ๏ธ HISTORY ๐ฐ๏ธ
All of these functions were deprecated in OpenSSL 3.0. ๐
The requirement that the salt parameter to DES_crypt() and DES_fcrypt() be two ASCII characters was first enforced in OpenSSL 1.1.0. Previous versions tried to use the letter uppercase A if both character were not present, and could crash when given non-ASCII on some platforms.
ยฉ๏ธ COPYRIGHT ยฉ๏ธ
Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
Licensed under the Apache License 2.0 (the "License"). You may not use this file except in compliance with the License. You can obtain a copy in the file LICENSE in the source distribution or at https://www.openssl.org/source/license.html.
okay bestie that's the whole page ๐ this one's from OpenSSL 3.5.6, dated 2026-04-07 โ go hydrate, and maybe don't use DES for anything new, mwah ๐
โจ
๐ฆ where this page comes from
- freebsd 15.1: base 15.1
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms