CRYPT(3)

๐Ÿ“ฆ version 15.1, in freebsd 14.5, freebsd 15.1

๐Ÿ’… CRYPT(3) ๐Ÿ’…

okay so bestie, welcome to the crypt manual page from the Crypt Library (libcrypt, -lcrypt) โ€” she's iconic, she's doing the MOST, and we're gonna get into all of it ๐ŸŽ€


โœจ SYNOPSIS โœจ

like, here's the whole friend group:

#include <unistd.h>

char *
crypt(const char *key, const char *salt);

char *
crypt_r(const char *key, const char *salt, struct crypt_data *data);

const char *
crypt_get_format(void);

int
crypt_set_format(const char *string);

๐ŸŒธ DESCRIPTION ๐ŸŒธ

so crypt() is literally out here performing password hashing with extra code sprinkled in specifically to deter key search attempts ๐Ÿ”ฅ different algorithms can be used in the hash, and rn the lineup includes the NBS Data Encryption Standard (DES), MD5 hash, NT-Hash (compatible with Microsoft's NT scheme) and Blowfish. which one you get depends on the format of the salt (following the Modular Crypt Format (MCF)), whether DES and/or Blowfish is installed or not, and whether crypt_set_format() has been called to change the default. no cap.

the first argument to crypt is the data to hash (usually a password), in a NUL-terminated string. the second is the salt, in one of three forms:

  • Extended โ€” if it begins with an underscore (_) then the DES Extended Format is used in interpreting both the key and the salt, as outlined below ๐Ÿ’–
  • Modular โ€” if it begins with the string $digit$ then the Modular Crypt Format is used, as outlined below โœจ
  • Traditional โ€” if neither of the above is true, it assumes the Traditional Format, using the entire string as the salt (or the first portion) ๐Ÿซถ

all routines are designed to be time-consuming. she takes her time, okay? it's giving main character energy ๐Ÿ’…


๐Ÿฆ‹ DES Extended Format ๐Ÿฆ‹

the key is divided into groups of 8 characters (the last group is NUL-padded) and the low-order 7 bits of each character (56 bits per group) are used to form the DES key as follows: the first group of 56 bits becomes the initial DES key. for each additional group, the XOR of the encryption of the current DES key with itself and the group bits becomes the next DES key. literally so real.

the salt is a 9-character array consisting of an underscore followed by 4 bytes of iteration count and 4 bytes of salt. these are encoded as printable characters, 6 bits per character, least significant character first. the values 0 to 63 are encoded as ./0-9A-Za-z. this allows 24 bits for both count and salt ๐ŸŽ€

the salt introduces disorder in the DES algorithm in one of 16777216 or 4096 possible ways (i.e., with 24 or 12 bits: if bit i of the salt is set, then bits i and i+24 are swapped in the DES E-box output). the way that math just hits ๐Ÿงš

the DES key is used to encrypt a 64-bit constant using count iterations of DES. the value returned is a NUL-terminated string, 20 or 13 bytes (plus NUL) in length, consisting of the salt followed by the encoded 64-bit encryption. obsessed with her consistency ๐Ÿ’–


๐ŸŒˆ Modular crypt ๐ŸŒˆ

if the salt begins with the string $digit$ then the Modular Crypt Format is used. the digit represents which algorithm is used in encryption. following the token is the actual salt to use in the encryption. the maximum length of the salt used depends upon the module. the salt must be terminated with the end of the string character (NUL) or a dollar sign. any characters after the dollar sign are ignored. bye bestie, we don't see you ๐Ÿ‘‹

currently supported algorithms are:

  1. MD5
  2. Blowfish
  3. NT-Hash
  4. (unused)
  5. SHA-256
  6. SHA-512

other crypt formats may be easily added. an example salt would be:

$4$thesalt$rest

โญ Traditional crypt โญ

the algorithm used will depend upon whether crypt_set_format() has been called and whether a global default format has been specified. unless a global default has been specified or crypt_set_format() has set the format to something else, the built-in default format is used. this is currently DES if it is available, or SHA-512 if not. slay ๐Ÿซถ

how the salt is used will depend upon the algorithm for the hash. for best results, specify at least eight characters of salt. eight is the magic number bestie, don't skip it ๐ŸŽ€

the crypt_get_format() function returns a constant string that represents the name of the algorithm currently used. valid values are 'des', 'blf', 'md5', 'sha256', 'sha512' and 'nth'. iconic lineup tbh ๐Ÿ’–

the crypt_set_format() function sets the default encoding format according to the supplied string โœจ

the crypt_r() function behaves identically to crypt(), except that the resulting string is stored in data, making it thread-safe. she's the responsible one of the group, we love her ๐Ÿงš


๐Ÿ’œ RETURN VALUES ๐Ÿ’œ

the crypt() and crypt_r() functions return a pointer to the encrypted value on success, and NULL on failure. note: this is not a standard behaviour โ€” AT&T crypt() will always return a pointer to a string. so like, don't come for her, she's just built different ๐Ÿ˜ญ

the crypt_set_format() function will return 1 if the supplied encoding format was valid. otherwise, a value of 0 is returned. simple as that ๐ŸŽ€


๐Ÿ‘‘ SEE ALSO ๐Ÿ‘‘

login(1), passwd(1), getpass(3), passwd(5) โ€” go read those next, iykyk ๐Ÿ“š


๐Ÿ•ฐ๏ธ HISTORY ๐Ÿ•ฐ๏ธ

a rotor-based crypt() function appeared in Version 6 AT&T UNIX. the current style crypt() first appeared in Version 7 AT&T UNIX. she's been serving looks since the 70s fr fr ๐Ÿ’…

the DES section of the code (FreeSec 1.0) was developed outside the United States of America as an unencumbered replacement for the U.S.-only NetBSD libcrypt encryption library. international queen behavior ๐ŸŒ๐Ÿ‘‘

the crypt_r() function was added in FreeBSD 12.0. new-ish but already iconic โœจ


๐ŸŒธ AUTHORS ๐ŸŒธ

originally written by David Burren <davidb@werj.com.au>, later additions and changes by Poul-Henning Kamp, Mark R V Murray, Michael Bretterklieber, Kris Kennaway, Brian Feldman, Paul Herman and Niels Provos. the whole squad contributed, we stan collaborative effort ๐Ÿซถ๐Ÿ’–


๐Ÿ’€ BUGS ๐Ÿ’€

okay so listen up because this part matters:

the crypt() function returns a pointer to static data, and subsequent calls to crypt() will modify the same data. likewise, crypt_set_format() modifies static data. so like, don't be surprised when things change under you, that's just how she operates ๐Ÿ˜ญ๐Ÿ™ˆ

the NT-hash scheme does not use a salt, and is not hard for a competent attacker to break. its use is not recommended. this is a real warning bestie, please take it seriously ๐Ÿšจ๐Ÿ’€

kisses, this page is from Debian, dated May 26, 2019 โ€” stay crypt-ic, stay safe, and remember: salt your hashes, not just your fries ๐ŸŸ๐Ÿ’–โœจ

๐Ÿ“ฆ where this page comes from