crypt.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* High-level libcrypt interfaces.
  2. Copyright (C) 1991-2017 Free Software Foundation, Inc.
  3. This library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public License
  5. as published by the Free Software Foundation; either version 2.1 of
  6. the License, or (at your option) any later version.
  7. This library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with this library; if not, see
  13. <https://www.gnu.org/licenses/>. */
  14. #ifndef _CRYPT_H
  15. #define _CRYPT_H 1
  16. #include <sys/cdefs.h>
  17. __BEGIN_DECLS
  18. /* The strings returned by crypt, crypt_r, crypt_rn, and crypt_ra will
  19. be no longer than this, counting the terminating NUL. (Existing
  20. algorithms all produce much shorter strings, but we have reserved
  21. generous space for future expansion.) This is NOT the appropriate
  22. size to use in allocating the buffer supplied to crypt_rn; use
  23. sizeof (struct crypt_data) instead. */
  24. #define CRYPT_OUTPUT_SIZE 384
  25. /* Passphrases longer than this (counting the terminating NUL) are not
  26. supported. Note that some hash algorithms have lower limits. */
  27. #define CRYPT_MAX_PASSPHRASE_SIZE 512
  28. /* The strings returned by crypt_gensalt, crypt_gensalt_rn, and
  29. crypt_gensalt_ra will be no longer than this. This IS the
  30. appropriate size to use when allocating the buffer supplied to
  31. crypt_gensalt_rn. (Again, existing algorithms all produce
  32. much shorter strings, but we have reserved generous space for
  33. future expansion.) */
  34. #define CRYPT_GENSALT_OUTPUT_SIZE 192
  35. /* One-way hash the passphrase PHRASE as specified by SETTING, and
  36. return a string suitable for storage in a Unix-style "passwd" file.
  37. If SETTING is a previously hashed passphrase, the string returned
  38. will be equal to SETTING if and only if PHRASE is the same as the
  39. passphrase that was previously hashed. See the documentation for
  40. other ways to use this function.
  41. The string returned by this function is stored in a statically-
  42. allocated buffer, and will be overwritten if the function is called
  43. again. It is not safe to call this function from multiple threads
  44. concurrently.
  45. If an error occurs (such as SETTING being nonsense or unsupported)
  46. the string returned will begin with '*', and will not be equal to
  47. SETTING nor to any valid hashed passphrase. Otherwise, the string
  48. will not begin with '*'. */
  49. extern char *crypt (const char *__phrase, const char *__setting)
  50. __THROW;
  51. /* These sizes are chosen to make sizeof (struct crypt_data) add up to
  52. exactly 32768 bytes. */
  53. #define CRYPT_DATA_RESERVED_SIZE 767
  54. #define CRYPT_DATA_INTERNAL_SIZE 30720
  55. /* Memory area used by crypt_r. */
  56. struct crypt_data
  57. {
  58. /* crypt_r writes the hashed password to this field of its 'data'
  59. argument. crypt_rn and crypt_ra do the same, treating the
  60. untyped data area they are supplied with as this struct. */
  61. char output[CRYPT_OUTPUT_SIZE];
  62. /* Applications are encouraged, but not required, to use this field
  63. to store the "setting" string that must be passed to crypt_*.
  64. Future extensions to the API may make this more ergonomic.
  65. A valid "setting" is either previously hashed password or the
  66. string produced by one of the crypt_gensalt functions; see the
  67. crypt_gensalt documentation for further details. */
  68. char setting[CRYPT_OUTPUT_SIZE];
  69. /* Applications are encouraged, but not required, to use this field
  70. to store the unhashed passphrase they will pass to crypt_*.
  71. Future extensions to the API may make this more ergonomic. */
  72. char input[CRYPT_MAX_PASSPHRASE_SIZE];
  73. /* Reserved for future application-visible fields. For maximum
  74. forward compatibility, applications should set this field to all
  75. bytes zero before calling crypt_r, crypt_rn, or crypt_ra for the
  76. first time with a just-allocated 'struct crypt_data'. Future
  77. extensions to the API may make this more ergonomic. */
  78. char reserved[CRYPT_DATA_RESERVED_SIZE];
  79. /* This field should be set to 0 before calling crypt_r, crypt_rn,
  80. or crypt_ra for the first time with a just-allocated
  81. 'struct crypt_data'. This is not required if crypt_ra is allowed
  82. to do the allocation itself (i.e. if the *DATA argument is a null
  83. pointer). Future extensions to the API may make this more ergonomic. */
  84. char initialized;
  85. /* Scratch space used internally. Applications should not read or
  86. write this field. All data written to this area is erased before
  87. returning from the library. */
  88. char internal[CRYPT_DATA_INTERNAL_SIZE];
  89. };
  90. /* Thread-safe version of crypt. Instead of writing to a static
  91. storage area, the string returned by this function will be within
  92. DATA->output. Otherwise, behaves exactly the same as crypt. */
  93. extern char *crypt_r (const char *__phrase, const char *__setting,
  94. struct crypt_data *__restrict __data)
  95. __THROW;
  96. /* Another thread-safe version of crypt. Instead of writing to a
  97. static storage area, the string returned by this function will be
  98. somewhere within the space provided at DATA, which is of length SIZE
  99. bytes. SIZE must be at least sizeof (struct crypt_data).
  100. Also, if an error occurs, this function returns a null pointer,
  101. not a special string. (However, the string returned on success
  102. still will never begin with '*'.) */
  103. extern char *crypt_rn (const char *__phrase, const char *__setting,
  104. void *__data, int __size)
  105. __THROW;
  106. /* Yet a third thread-safe version of crypt; this one works like
  107. getline(3). *DATA must be either 0 or a pointer to memory
  108. allocated by malloc, and *SIZE must be the size of the allocation.
  109. This space will be allocated or reallocated as necessary and the
  110. values updated. The string returned by this function will be
  111. somewhere within the space at *DATA. It is safe to deallocate
  112. this space with free when it is no longer needed.
  113. Like crypt_rn, this function returns a null pointer on failure, not
  114. a special string. */
  115. extern char *crypt_ra (const char *__phrase, const char *__setting,
  116. void **__data, int *__size)
  117. __THROW;
  118. /* Generate a string suitable for use as the setting when hashing a
  119. new passphrase. PREFIX controls which hash function will be used,
  120. COUNT controls the computational cost of the hash (for functions
  121. where this is tunable), and RBYTES should point to NRBYTES bytes of
  122. random data. If PREFIX is a null pointer, the current best default
  123. is used; if RBYTES is a null pointer, random data will be retrieved
  124. from the operating system if possible. (Caution: setting PREFIX to
  125. an *empty string* selects the use of the oldest and least secure
  126. hash in the library. Don't do that.)
  127. The string returned is stored in a statically-allocated buffer, and
  128. will be overwritten if the function is called again. It is not
  129. safe to call this function from multiple threads concurrently.
  130. However, within a single thread, it is safe to pass the string as
  131. the SETTING argument to crypt without copying it first; the two
  132. functions use separate buffers.
  133. If an error occurs (e.g. a prefix that does not correspond to a
  134. supported hash function, or an inadequate amount of random data),
  135. this function returns a null pointer. */
  136. extern char *crypt_gensalt (const char *__prefix, unsigned long __count,
  137. const char *__rbytes, int __nrbytes)
  138. __THROW;
  139. /* Thread-safe version of crypt_gensalt; instead of a
  140. statically-allocated buffer, the generated setting string is
  141. written to OUTPUT, which is OUTPUT_SIZE bytes long. OUTPUT_SIZE
  142. must be at least CRYPT_GENSALT_OUTPUT_SIZE (see above).
  143. If an error occurs, this function returns a null pointer and writes
  144. a string that does not correspond to any valid setting into OUTPUT. */
  145. extern char *crypt_gensalt_rn (const char *__prefix, unsigned long __count,
  146. const char *__rbytes, int __nrbytes,
  147. char *__output, int __output_size)
  148. __THROW;
  149. /* Kept for code compatibility with libxcrypt (v3.1.1 and earlier).
  150. We intentionally declare the function using a macro here, since
  151. we actually want to link compiled applications against the
  152. identical crypt_gensalt_rn function. */
  153. #ifndef IN_LIBCRYPT /* Defined when building libxcrypt. */
  154. # ifdef __REDIRECT_NTH
  155. extern char * __REDIRECT_NTH (crypt_gensalt_r, (const char *__prefix,
  156. unsigned long __count, const char *__rbytes,
  157. int __nrbytes, char *__output,
  158. int __output_size), crypt_gensalt_rn);
  159. # else
  160. # define crypt_gensalt_r crypt_gensalt_rn
  161. # endif
  162. #endif
  163. /* Another thread-safe version of crypt_gensalt; the generated setting
  164. string is in storage allocated by malloc, and should be deallocated
  165. with free when it is no longer needed. */
  166. extern char *crypt_gensalt_ra (const char *__prefix, unsigned long __count,
  167. const char *__rbytes, int __nrbytes)
  168. __THROW;
  169. /* Checks whether the given setting is a supported method.
  170. The return value is 0 if there is nothing wrong with this setting.
  171. Otherwise, it is one of the following constants. */
  172. extern int crypt_checksalt (const char *__setting);
  173. /* Constants for checking the return value of the
  174. crypt_checksalt function. */
  175. #define CRYPT_SALT_OK 0
  176. #define CRYPT_SALT_INVALID 1
  177. #define CRYPT_SALT_METHOD_DISABLED 2 /* NOT implemented, yet. */
  178. #define CRYPT_SALT_METHOD_LEGACY 3 /* NOT implemented, yet. */
  179. #define CRYPT_SALT_TOO_CHEAP 4 /* NOT implemented, yet. */
  180. /* Convenience function to get the prefix of the preferred hash method,
  181. which is also used by the crypt_gensalt functions, if their given
  182. prefix parameter is NULL.
  183. The return value is string that equals the prefix of the preferred
  184. hash method. Otherwise, it is NULL. */
  185. extern const char *crypt_preferred_method (void);
  186. /* These macros could be checked by portable users of crypt_gensalt*
  187. functions to find out whether null pointers could be specified
  188. as PREFIX and RBYTES arguments. */
  189. #define CRYPT_GENSALT_IMPLEMENTS_DEFAULT_PREFIX 1
  190. #define CRYPT_GENSALT_IMPLEMENTS_AUTO_ENTROPY 1
  191. /* These macros can be checked by portable users of libxcrypt
  192. to find out whether the function is implemented. */
  193. #define CRYPT_CHECKSALT_AVAILABLE 1
  194. #define CRYPT_PREFERRED_METHOD_AVAILABLE 1
  195. /* Version number split in single integers. */
  196. #define XCRYPT_VERSION_MAJOR 4
  197. #define XCRYPT_VERSION_MINOR 4
  198. /* Version number coded into an integer. */
  199. #define XCRYPT_VERSION_NUM ((XCRYPT_VERSION_MAJOR << 16) | \
  200. XCRYPT_VERSION_MINOR)
  201. /* Version number as a string constant. */
  202. #define XCRYPT_VERSION_STR "4.4.10"
  203. __END_DECLS
  204. #endif /* crypt.h */