crypto_s2k.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* Copyright (c) 2001, Matej Pfajfar.
  2. * Copyright (c) 2001-2004, Roger Dingledine.
  3. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  4. * Copyright (c) 2007-2016, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. #ifndef TOR_CRYPTO_S2K_H_INCLUDED
  7. #define TOR_CRYPTO_S2K_H_INCLUDED
  8. #include <stdio.h>
  9. #include "torint.h"
  10. /** Length of RFC2440-style S2K specifier: the first 8 bytes are a salt, the
  11. * 9th describes how much iteration to do. */
  12. #define S2K_RFC2440_SPECIFIER_LEN 9
  13. void secret_to_key_rfc2440(
  14. char *key_out, size_t key_out_len, const char *secret,
  15. size_t secret_len, const char *s2k_specifier);
  16. /** Flag for secret-to-key function: do not use scrypt. */
  17. #define S2K_FLAG_NO_SCRYPT (1u<<0)
  18. /** Flag for secret-to-key functions: if using a memory-tuned s2k function,
  19. * assume that we have limited memory. */
  20. #define S2K_FLAG_LOW_MEM (1u<<1)
  21. /** Flag for secret-to-key functions: force use of pbkdf2. Without this, we
  22. * default to scrypt, then RFC2440. */
  23. #define S2K_FLAG_USE_PBKDF2 (1u<<2)
  24. /** Maximum possible output length from secret_to_key_new. */
  25. #define S2K_MAXLEN 64
  26. /** Error code from secret-to-key functions: all is well */
  27. #define S2K_OKAY 0
  28. /** Error code from secret-to-key functions: generic failure */
  29. #define S2K_FAILED -1
  30. /** Error code from secret-to-key functions: provided secret didn't match */
  31. #define S2K_BAD_SECRET -2
  32. /** Error code from secret-to-key functions: didn't recognize the algorithm */
  33. #define S2K_BAD_ALGORITHM -3
  34. /** Error code from secret-to-key functions: specifier wasn't valid */
  35. #define S2K_BAD_PARAMS -4
  36. /** Error code from secret-to-key functions: compiled without scrypt */
  37. #define S2K_NO_SCRYPT_SUPPORT -5
  38. /** Error code from secret-to-key functions: not enough space to write output.
  39. */
  40. #define S2K_TRUNCATED -6
  41. /** Error code from secret-to-key functions: Wrong length for specifier. */
  42. #define S2K_BAD_LEN -7
  43. int secret_to_key_new(uint8_t *buf,
  44. size_t buf_len,
  45. size_t *len_out,
  46. const char *secret, size_t secret_len,
  47. unsigned flags);
  48. int secret_to_key_make_specifier(uint8_t *buf, size_t buf_len, unsigned flags);
  49. int secret_to_key_check(const uint8_t *spec_and_key, size_t spec_and_key_len,
  50. const char *secret, size_t secret_len);
  51. int secret_to_key_derivekey(uint8_t *key_out, size_t key_out_len,
  52. const uint8_t *spec, size_t spec_len,
  53. const char *secret, size_t secret_len);
  54. #ifdef CRYPTO_S2K_PRIVATE
  55. STATIC int secret_to_key_compute_key(uint8_t *key_out, size_t key_out_len,
  56. const uint8_t *spec, size_t spec_len,
  57. const char *secret, size_t secret_len,
  58. int type);
  59. #endif
  60. #endif