DES_RANDOM_KEY(3SSL)
π DES_RANDOM_KEY π
okay so bestie, welcome to the 3SSL corner of the OpenSSL manual πΈ this page is the whole DES saga and it is long, so we're doing it in two parts β this is part 1, grab a snack π
If you're here for key schedules, ECB/CBC/CFB/OFB modes, checksums or that classic crypt energy, you are in exactly the right place. Sit down, it's giving β¨cryptographyβ¨.
β¨ NAME β¨
Meet the squad. Every single one of these functions is about DES encryption π
DES_random_keyDES_set_keyDES_key_schedDES_set_key_checkedDES_set_key_uncheckedDES_set_odd_parityDES_is_weak_keyDES_ecb_encryptDES_ecb2_encryptDES_ecb3_encryptDES_ncbc_encryptDES_cfb_encryptDES_ofb_encryptDES_pcbc_encryptDES_cfb64_encryptDES_ofb64_encryptDES_xcbc_encryptDES_ede2_cbc_encryptDES_ede2_cfb64_encryptDES_ede2_ofb64_encryptDES_ede3_cbc_encryptDES_ede3_cfb64_encryptDES_ede3_ofb64_encryptDES_cbc_cksumDES_quad_cksumDES_string_to_keyDES_string_to_2keysDES_fcryptDES_crypt
that's the whole guest list. no gatekeeping π«Ά
π SYNOPSIS π
First things first, you gotta pull in the header:
#include <openssl/des.h>
β οΈ BIG WARNING, READ IT TWICE bestie π β all of the functions below have been deprecated since OpenSSL 3.0, and you can hide them entirely by definingOPENSSL_API_COMPATwith a suitable version value, seeopenssl_user_macros(7).
So likeβ¦ they still exist, but the vibe is very much "legacy era" π. The OPENSSL_API_COMPAT define is your "make it disappear" button if you don't want to look at them.
Anyways, here's every prototype, verbatim, no edits, no crumbs left behind π§
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);
look at all those ede2 and ede3 triplets, they really said "we contain multitudes" π₯
okay that's the cast and the signatures, and the prose breakdown (what all these parameters actually mean, the return values, the warnings, the whole tea βοΈ) is coming up next β part 2 loadingβ¦ don't go anywhere π
β¨ DESCRIPTION β¨
π¨ SPOILER ALERT bestie: literally every single function on this page is deprecated. Like, all of them. Every one. If you're writing new code, you should be usingEVP_EncryptInit_ex(3),EVP_EncryptUpdate(3)andEVP_EncryptFinal_ex(3)β or the decrypt versions, which have the exact same names except they say "Decrypt" instead of "Encrypt" cause that's just how EVP rolls. π«Ά
So this library has a fast implementation of the DES encryption algorithm. Fast! Speedy! Ate. π
Using DES encryption is a whole two-phase situationship:
- You generate a
DES_key_schedulefrom a key. - You actually do the encrypting. π₯
A DES key is of type DES_cblock. That type is 8 bytes with odd parity. The least significant bit in each byte is the parity bit, okay? That's just the rules. And the key schedule? It's an expanded form of the key β its whole job is to make the encryption go faster. The more you know. β¨
π² The key-generating girlies
DES_random_key() generates a random key. Super simple. But β and this is the important part β the random generator must be seeded when you call this function. If the automatic seeding or reseeding of the OpenSSL CSPRNG fails because of external circumstances (peep RAND(7) if you wanna read more), the whole operation just fails. And if the function fails, it returns 0. That's it. That's the vibe check. π
π Converting your key into a schedule
Before a DES key can be used, periodt, it has to be converted into the architecture dependent DES_key_schedule. You do that with either DES_set_key_checked() or DES_set_key_unchecked(). Pick your fighter.
DES_set_key_checked() will check that the key you passed is of odd parity AND that it's not a weak or semi-weak key. Here's the scoring system:
- If the parity is wrong β returns
-1π¬ - If the key is a weak key β returns
-2π - If any error is returned β the key schedule is not generated. Nothing happens. You get nothing.
DES_set_key() works exactly like DES_set_key_checked() and is still hanging around for backward compatibility. It's grandfathered in. Iconic of it, honestly.
DES_set_odd_parity() sets the parity of the key you passed to odd. That's its whole thing. π€
DES_is_weak_key() returns 1 if the key you passed is a weak key, and 0 if it's all good. King behavior.
π The stream routines (they mostly work on DES_cblock input and output streams)
The following routines mostly operate on an input and output stream of DES_cblocks. Let's go bestie, one by one π
DES_ecb_encrypt()β the basic DES encryption routine, she's the foundation. She encrypts or decrypts a single 8-byteDES_cblockin 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 encrypt argument is nonzero (DES_ENCRYPT), the input (cleartext) gets encrypted into the output (ciphertext) using the key_schedule specified by the schedule argument, which you set earlier viaDES_set_key. If encrypt is zero (DES_DECRYPT), the input (now ciphertext) gets decrypted into the output (now cleartext). Input and output may overlap. AndDES_ecb_encrypt()does not return a value. She gives you nothing back and that's the deal. πDES_ecb3_encrypt()β encrypts/decrypts the input block using three-key Triple-DES encryption in ECB mode. So what actually goes down: encrypt the input with ks1, decrypt with the key schedule ks2, then encrypt with ks3. This routine greatly reduces the chances of brute force breaking of DES, and it has the cute little advantage that if ks1, ks2 and ks3 are all the same, it's just equivalent to plain ECB encryption with ks1 as the key. Convenient!DES_ecb2_encrypt()β this one's a macro β¨ provided for you 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 encrypt argument is nonzero, 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 the initialization vector provided by the ivec argument. And heads up: if the length argument is not an integral multiple of eight bytes, the last block gets copied to a temporary area and zero filled. The output is always an integral multiple of eight bytes. Always. No exceptions. πDES_xcbc_encrypt()β this is RSA's DESX mode of DES. It uses inw and outw to 'whiten' the encryption. Now listen β inw and outw are secret (unlike the iv) and they are, as such, part of the key. So the key is sort of 24 bytes. This is much better than CBC DES, no cap. π₯DES_ede3_cbc_encrypt()β implements outer triple CBC DES encryption with three keys. Which means each DES operation inside the CBC mode is"C=E(ks3,D(ks2,E(ks1,M)))". This mode is used by SSL. Big leagues. πDES_ede2_cbc_encrypt()β a macro that 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. Yeah. That 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(). Same girl, different mode.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, which is lowkey nice of it. But note: the ivec variable is changed, and that new changed value needs to be passed to the next call to this function. And also β since this function runs a complete DES ECB encryption per numbits, it's only suggested for use when you're sending a small number of characters. Small. Tiny. Don't be sending novels through this. πDES_cfb64_encrypt()β implements CFB mode of DES with 64-bit feedback. "Why is this useful," you ask? Great question bestie. Because this routine will let you encrypt an arbitrary number of bytes without 8 byte padding. Each call to this routine encrypts the input bytes to output and then updates ivec and num. And num contains 'how far' we are through ivec. If that doesn't make much sense to you, go read more about CFB mode of DES. I believe in you. π¦DES_ede3_cfb64_encrypt()andDES_ede2_cfb64_encrypt()β same asDES_cfb64_encrypt()except that Triple-DES is used. That's it, that's the difference. πDES_ofb_encrypt()β encrypts using output feedback mode. Like its CFB cousin, it takes an array of characters as input and outputs an array of characters, and it does not require any padding to 8 character groups. Same warning though: the ivec variable is changed and the new changed value needs to be passed to the next call. And since it runs a complete DES ECB encryption per numbits, it's only suggested for small numbers of characters. πDES_ofb64_encrypt()β the same asDES_cfb64_encrypt()but using Output Feed Back mode. β¨DES_ede3_ofb64_encrypt()andDES_ede2_ofb64_encrypt()β same asDES_ofb64_encrypt(), but with Triple-DES. You see the pattern. π
π― MIT Kerberos compatibility corner
The following functions are included in the DES library for compatibility with the MIT Kerberos library. They're here for the lore, not for your new app. πΈ
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 useEVP_DigestInit(3)etc. instead. Please. For me. π«ΆDES_quad_cksum()β 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. And if output is non-NULL, the 8 bytes generated by each pass are written into output. Quadruple the passes, quadruple the drama. π«
π The DES-based transformations
DES_fcrypt()β a fast version of the Unixcrypt(3)function. This version takes only a small amount of space relative to other fastcrypt()implementations, tiny little icon. How is it different from normalcrypt()? The third parameter is the buffer that the return value gets written into β and it needs to be at least 14 bytes long. Oh, and this function is thread safe, unlike the normalcrypt(). Slay. π₯DES_crypt()β a faster replacement for the normal systemcrypt(). This function callsDES_fcrypt()with a static array passed as the third parameter. It mostly emulates the normal non-thread-safe semantics ofcrypt(3), so, you know, the chaos is intentional. The salt must be two ASCII characters.- The values returned by
DES_fcrypt()andDES_crypt()are terminated by a NUL character. Done, finished,\0. β¨ DES_enc_write()β writes len bytes to file descriptor fd from buffer buf. The data gets encrypted viapcbc_encrypt(default) using sched for the key and iv as a starting vector. The actual data sent down fd consists 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β so if you want the iv to actually update, useDES_ncbc_encrypt()instead. That's the whole bug. Surprise! πDES_cfb_encrypt()andDES_ofb_encrypt()operate on input of 8 bits. Let me explain what that actually means, cause it's feral. If you set numbits to12and length to2, then 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. And the same holds for output. Why is it implemented like this?? Because most people will be using a multiple of 8, and because β and I quote the energy here β 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 instead. The same applies forDES_string_to_2key(). Both of them. Yeah. π
π NOTES π
- The des library was written to be source code compatible with the MIT Kerberos library. Old friends. π€
- Applications should use the higher level functions
EVP_EncryptInit(3)etc. instead of calling these functions directly. (Yes, I'm repeating myself, but so is the manpage, so.) β¨ - Single-key DES is insecure due to its short key size. π¨ Not a vibe. And ECB mode is not suitable for most applications β go look at
des_modes(7)for the full tea on that.
π«Ά 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's ok. β¨DES_cbc_cksum()andDES_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, andDES_crypt()returns a pointer to a static buffer, on success. Otherwise they returnNULL. π
π SEE ALSO π
des_modes(7), EVP_EncryptInit(3)
Go read those. Tell them I said hi. π
π HISTORY π
- All of these functions were deprecated in OpenSSL 3.0. All of them. The whole squad. π°οΈ
- The requirement that the salt parameter to
DES_crypt()andDES_fcrypt()be two ASCII characters was first enforced in OpenSSL 1.1.0. Before that? Previous versions tried to use the letter uppercaseAif both characters were not present, and β bestie, brace yourself β could crash when given non-ASCII on some platforms. π Yeah. We do not miss that era.
βοΈ 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>.
And that's the whole page, bestie!! π This one was DES_RANDOM_KEY(3SSL) from OpenSSL 4.0.3, dated 2026-09-29. Now go call EVP_EncryptInit_ex(3) like a good main character and leave these deprecated girlies alone. Mwah. πβ¨π
π¦ where this page comes from
- ubuntu stonking: libssl-doc 4.0.3-1ubuntu1 Β· Apache-2.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms