crypt.texi 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. @node Cryptographic Functions, Debugging Support, System Configuration, Top
  2. @chapter Cryptographic Functions
  3. @c %MENU% Passphrase storage and strongly unpredictable bytes.
  4. @Theglibc{} includes only a few special-purpose cryptographic
  5. functions: one-way hash functions for passphrase storage, and access
  6. to a cryptographic randomness source, if one is provided by the
  7. operating system. Programs that need general-purpose cryptography
  8. should use a dedicated cryptography library, such as
  9. @uref{https://www.gnu.org/software/libgcrypt/,,libgcrypt}.
  10. Many countries place legal restrictions on the import, export,
  11. possession, or use of cryptographic software. We deplore these
  12. restrictions, but we must still warn you that @theglibc{} may be
  13. subject to them, even if you do not use the functions in this chapter
  14. yourself. The restrictions vary from place to place and are changed
  15. often, so we cannot give any more specific advice than this warning.
  16. @menu
  17. * Passphrase Storage:: One-way hashing for passphrases.
  18. * Unpredictable Bytes:: Randomness for cryptographic purposes.
  19. @end menu
  20. @node Passphrase Storage
  21. @section Passphrase Storage
  22. @cindex passphrase hashing
  23. @cindex one-way hashing
  24. @cindex hashing, passphrase
  25. Sometimes it is necessary to be sure that a user is authorized
  26. to use some service a machine provides---for instance, to log in as a
  27. particular user id (@pxref{Users and Groups}). One traditional way of
  28. doing this is for each user to choose a secret @dfn{passphrase}; then, the
  29. system can ask someone claiming to be a user what the user's passphrase
  30. is, and if the person gives the correct passphrase then the system can
  31. grant the appropriate privileges. (Traditionally, these were called
  32. ``passwords,'' but nowadays a single word is too easy to guess.)
  33. Programs that handle passphrases must take special care not to reveal
  34. them to anyone, no matter what. It is not enough to keep them in a
  35. file that is only accessible with special privileges. The file might
  36. be ``leaked'' via a bug or misconfiguration, and system administrators
  37. shouldn't learn everyone's passphrase even if they have to edit that
  38. file for some reason. To avoid this, passphrases should also be
  39. converted into @dfn{one-way hashes}, using a @dfn{one-way function},
  40. before they are stored.
  41. A one-way function is easy to compute, but there is no known way to
  42. compute its inverse. This means the system can easily check
  43. passphrases, by hashing them and comparing the result with the stored
  44. hash. But an attacker who discovers someone's passphrase hash can
  45. only discover the passphrase it corresponds to by guessing and
  46. checking. The one-way functions are designed to make this process
  47. impractically slow, for all but the most obvious guesses. (Do not use
  48. a word from the dictionary as your passphrase.)
  49. @Theglibc{} provides an interface to four one-way functions, based on
  50. the SHA-2-512, SHA-2-256, MD5, and DES cryptographic primitives. New
  51. passphrases should be hashed with either of the SHA-based functions.
  52. The others are too weak for newly set passphrases, but we continue to
  53. support them for verifying old passphrases. The DES-based hash is
  54. especially weak, because it ignores all but the first eight characters
  55. of its input.
  56. @deftypefun {char *} crypt (const char *@var{phrase}, const char *@var{salt})
  57. @standards{X/Open, unistd.h}
  58. @standards{GNU, crypt.h}
  59. @safety{@prelim{}@mtunsafe{@mtasurace{:crypt}}@asunsafe{@asucorrupt{} @asulock{} @ascuheap{} @ascudlopen{}}@acunsafe{@aculock{} @acsmem{}}}
  60. @c Besides the obvious problem of returning a pointer into static
  61. @c storage, the DES initializer takes an internal lock with the usual
  62. @c set of problems for AS- and AC-Safety.
  63. @c The NSS implementations may leak file descriptors if cancelled.
  64. @c The MD5, SHA256 and SHA512 implementations will malloc on long keys,
  65. @c and NSS relies on dlopening, which brings about another can of worms.
  66. The function @code{crypt} converts a passphrase string, @var{phrase},
  67. into a one-way hash suitable for storage in the user database. The
  68. string that it returns will consist entirely of printable ASCII
  69. characters. It will not contain whitespace, nor any of the characters
  70. @samp{:}, @samp{;}, @samp{*}, @samp{!}, or @samp{\}.
  71. The @var{salt} parameter controls which one-way function is used, and
  72. it also ensures that the output of the one-way function is different
  73. for every user, even if they have the same passphrase. This makes it
  74. harder to guess passphrases from a large user database. Without salt,
  75. the attacker could make a guess, run @code{crypt} on it once, and
  76. compare the result with all the hashes. Salt forces the attacker to
  77. make separate calls to @code{crypt} for each user.
  78. To verify a passphrase, pass the previously hashed passphrase as the
  79. @var{salt}. To hash a new passphrase for storage, set @var{salt} to a
  80. string consisting of a prefix plus a sequence of randomly chosen
  81. characters, according to this table:
  82. @multitable @columnfractions .2 .1 .3
  83. @headitem One-way function @tab Prefix @tab Random sequence
  84. @item SHA-2-512
  85. @tab @samp{$6$}
  86. @tab 16 characters
  87. @item SHA-2-256
  88. @tab @samp{$5$}
  89. @tab 16 characters
  90. @item MD5
  91. @tab @samp{$1$}
  92. @tab 8 characters
  93. @item DES
  94. @tab @samp{}
  95. @tab 2 characters
  96. @end multitable
  97. In all cases, the random characters should be chosen from the alphabet
  98. @code{./0-9A-Za-z}.
  99. With all of the hash functions @emph{except} DES, @var{phrase} can be
  100. arbitrarily long, and all eight bits of each byte are significant.
  101. With DES, only the first eight characters of @var{phrase} affect the
  102. output, and the eighth bit of each byte is also ignored.
  103. @code{crypt} can fail. Some implementations return @code{NULL} on
  104. failure, and others return an @emph{invalid} hashed passphrase, which
  105. will begin with a @samp{*} and will not be the same as @var{salt}. In
  106. either case, @code{errno} will be set to indicate the problem. Some
  107. of the possible error codes are:
  108. @table @code
  109. @item EINVAL
  110. @var{salt} is invalid; neither a previously hashed passphrase, nor a
  111. well-formed new salt for any of the supported hash functions.
  112. @item EPERM
  113. The system configuration forbids use of the hash function selected by
  114. @var{salt}.
  115. @item ENOMEM
  116. Failed to allocate internal scratch storage.
  117. @item ENOSYS
  118. @itemx EOPNOTSUPP
  119. Hashing passphrases is not supported at all, or the hash function
  120. selected by @var{salt} is not supported. @Theglibc{} does not use
  121. these error codes, but they may be encountered on other operating
  122. systems.
  123. @end table
  124. @code{crypt} uses static storage for both internal scratchwork and the
  125. string it returns. It is not safe to call @code{crypt} from multiple
  126. threads simultaneously, and the string it returns will be overwritten
  127. by any subsequent call to @code{crypt}.
  128. @code{crypt} is specified in the X/Open Portability Guide and is
  129. present on nearly all historical Unix systems. However, the XPG does
  130. not specify any one-way functions.
  131. @code{crypt} is declared in @file{unistd.h}. @Theglibc{} also
  132. declares this function in @file{crypt.h}.
  133. @end deftypefun
  134. @deftypefun {char *} crypt_r (const char *@var{phrase}, const char *@var{salt}, struct crypt_data *@var{data})
  135. @standards{GNU, crypt.h}
  136. @safety{@prelim{}@mtsafe{}@asunsafe{@asucorrupt{} @asulock{} @ascuheap{} @ascudlopen{}}@acunsafe{@aculock{} @acsmem{}}}
  137. @tindex struct crypt_data
  138. @c Compared with crypt, this function fixes the @mtasurace:crypt
  139. @c problem, but nothing else.
  140. The function @code{crypt_r} is a thread-safe version of @code{crypt}.
  141. Instead of static storage, it uses the memory pointed to by its
  142. @var{data} argument for both scratchwork and the string it returns.
  143. It can safely be used from multiple threads, as long as different
  144. @var{data} objects are used in each thread. The string it returns
  145. will still be overwritten by another call with the same @var{data}.
  146. @var{data} must point to a @code{struct crypt_data} object allocated
  147. by the caller. All of the fields of @code{struct crypt_data} are
  148. private, but before one of these objects is used for the first time,
  149. it must be initialized to all zeroes, using @code{memset} or similar.
  150. After that, it can be reused for many calls to @code{crypt_r} without
  151. erasing it again. @code{struct crypt_data} is very large, so it is
  152. best to allocate it with @code{malloc} rather than as a local
  153. variable. @xref{Memory Allocation}.
  154. @code{crypt_r} is a GNU extension. It is declared in @file{crypt.h},
  155. as is @code{struct crypt_data}.
  156. @end deftypefun
  157. The following program shows how to use @code{crypt} the first time a
  158. passphrase is entered. It uses @code{getentropy} to make the salt as
  159. unpredictable as possible; @pxref{Unpredictable Bytes}.
  160. @smallexample
  161. @include genpass.c.texi
  162. @end smallexample
  163. The next program demonstrates how to verify a passphrase. It checks a
  164. hash hardcoded into the program, because looking up real users' hashed
  165. passphrases may require special privileges (@pxref{User Database}).
  166. It also shows that different one-way functions produce different
  167. hashes for the same passphrase.
  168. @smallexample
  169. @include testpass.c.texi
  170. @end smallexample
  171. @node Unpredictable Bytes
  172. @section Generating Unpredictable Bytes
  173. @cindex randomness source
  174. @cindex random numbers, cryptographic
  175. @cindex pseudo-random numbers, cryptographic
  176. @cindex cryptographic random number generator
  177. @cindex deterministic random bit generator
  178. @cindex CRNG
  179. @cindex CSPRNG
  180. @cindex DRBG
  181. Cryptographic applications often need some random data that will be as
  182. difficult as possible for a hostile eavesdropper to guess. For
  183. instance, encryption keys should be chosen at random, and the ``salt''
  184. strings used by @code{crypt} (@pxref{Passphrase Storage}) should also
  185. be chosen at random.
  186. Some pseudo-random number generators do not provide unpredictable-enough
  187. output for cryptographic applications; @pxref{Pseudo-Random Numbers}.
  188. Such applications need to use a @dfn{cryptographic random number
  189. generator} (CRNG), also sometimes called a @dfn{cryptographically strong
  190. pseudo-random number generator} (CSPRNG) or @dfn{deterministic random
  191. bit generator} (DRBG).
  192. Currently, @theglibc{} does not provide a cryptographic random number
  193. generator, but it does provide functions that read random data from a
  194. @dfn{randomness source} supplied by the operating system. The
  195. randomness source is a CRNG at heart, but it also continually
  196. ``re-seeds'' itself from physical sources of randomness, such as
  197. electronic noise and clock jitter. This means applications do not need
  198. to do anything to ensure that the random numbers it produces are
  199. different on each run.
  200. The catch, however, is that these functions will only produce
  201. relatively short random strings in any one call. Often this is not a
  202. problem, but applications that need more than a few kilobytes of
  203. cryptographically strong random data should call these functions once
  204. and use their output to seed a CRNG.
  205. Most applications should use @code{getentropy}. The @code{getrandom}
  206. function is intended for low-level applications which need additional
  207. control over blocking behavior.
  208. @deftypefun int getentropy (void *@var{buffer}, size_t @var{length})
  209. @standards{GNU, sys/random.h}
  210. @safety{@mtsafe{}@assafe{}@acsafe{}}
  211. This function writes exactly @var{length} bytes of random data to the
  212. array starting at @var{buffer}. @var{length} can be no more than 256.
  213. On success, it returns zero. On failure, it returns @math{-1}, and
  214. @code{errno} is set to indicate the problem. Some of the possible
  215. errors are listed below.
  216. @table @code
  217. @item ENOSYS
  218. The operating system does not implement a randomness source, or does
  219. not support this way of accessing it. (For instance, the system call
  220. used by this function was added to the Linux kernel in version 3.17.)
  221. @item EFAULT
  222. The combination of @var{buffer} and @var{length} arguments specifies
  223. an invalid memory range.
  224. @item EIO
  225. @var{length} is larger than 256, or the kernel entropy pool has
  226. suffered a catastrophic failure.
  227. @end table
  228. A call to @code{getentropy} can only block when the system has just
  229. booted and the randomness source has not yet been initialized.
  230. However, if it does block, it cannot be interrupted by signals or
  231. thread cancellation. Programs intended to run in very early stages of
  232. the boot process may need to use @code{getrandom} in non-blocking mode
  233. instead, and be prepared to cope with random data not being available
  234. at all.
  235. The @code{getentropy} function is declared in the header file
  236. @file{sys/random.h}. It is derived from OpenBSD.
  237. @end deftypefun
  238. @deftypefun ssize_t getrandom (void *@var{buffer}, size_t @var{length}, unsigned int @var{flags})
  239. @standards{GNU, sys/random.h}
  240. @safety{@mtsafe{}@assafe{}@acsafe{}}
  241. This function writes up to @var{length} bytes of random data to the
  242. array starting at @var{buffer}. The @var{flags} argument should be
  243. either zero, or the bitwise OR of some of the following flags:
  244. @table @code
  245. @item GRND_RANDOM
  246. Use the @file{/dev/random} (blocking) source instead of the
  247. @file{/dev/urandom} (non-blocking) source to obtain randomness.
  248. If this flag is specified, the call may block, potentially for quite
  249. some time, even after the randomness source has been initialized. If it
  250. is not specified, the call can only block when the system has just
  251. booted and the randomness source has not yet been initialized.
  252. @item GRND_NONBLOCK
  253. Instead of blocking, return to the caller immediately if no data is
  254. available.
  255. @end table
  256. Unlike @code{getentropy}, the @code{getrandom} function is a
  257. cancellation point, and if it blocks, it can be interrupted by
  258. signals.
  259. On success, @code{getrandom} returns the number of bytes which have
  260. been written to the buffer, which may be less than @var{length}. On
  261. error, it returns @math{-1}, and @code{errno} is set to indicate the
  262. problem. Some of the possible errors are:
  263. @table @code
  264. @item ENOSYS
  265. The operating system does not implement a randomness source, or does
  266. not support this way of accessing it. (For instance, the system call
  267. used by this function was added to the Linux kernel in version 3.17.)
  268. @item EAGAIN
  269. No random data was available and @code{GRND_NONBLOCK} was specified in
  270. @var{flags}.
  271. @item EFAULT
  272. The combination of @var{buffer} and @var{length} arguments specifies
  273. an invalid memory range.
  274. @item EINTR
  275. The system call was interrupted. During the system boot process, before
  276. the kernel randomness pool is initialized, this can happen even if
  277. @var{flags} is zero.
  278. @item EINVAL
  279. The @var{flags} argument contains an invalid combination of flags.
  280. @end table
  281. The @code{getrandom} function is declared in the header file
  282. @file{sys/random.h}. It is a GNU extension.
  283. @end deftypefun