crypto_s2k.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  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-2018, The Tor Project, Inc. */
  5. /* See LICENSE for licensing information */
  6. /**
  7. * \file crypto_s2k.c
  8. *
  9. * \brief Functions for deriving keys from human-readable passphrases.
  10. */
  11. #define CRYPTO_S2K_PRIVATE
  12. #include "common/compat.h"
  13. #include "common/crypto.h"
  14. #include "common/crypto_digest.h"
  15. #include "common/crypto_hkdf.h"
  16. #include "common/crypto_rand.h"
  17. #include "common/crypto_s2k.h"
  18. #include "common/crypto_util.h"
  19. #include "common/util.h"
  20. #include <openssl/evp.h>
  21. #if defined(HAVE_LIBSCRYPT_H) && defined(HAVE_LIBSCRYPT_SCRYPT)
  22. #define HAVE_SCRYPT
  23. #include <libscrypt.h>
  24. #endif
  25. /* Encoded secrets take the form:
  26. u8 type;
  27. u8 salt_and_parameters[depends on type];
  28. u8 key[depends on type];
  29. As a special case, if the encoded secret is exactly 29 bytes long,
  30. type 0 is understood.
  31. Recognized types are:
  32. 00 -- RFC2440. salt_and_parameters is 9 bytes. key is 20 bytes.
  33. salt_and_parameters is 8 bytes random salt,
  34. 1 byte iteration info.
  35. 01 -- PKBDF2_SHA1. salt_and_parameters is 17 bytes. key is 20 bytes.
  36. salt_and_parameters is 16 bytes random salt,
  37. 1 byte iteration info.
  38. 02 -- SCRYPT_SALSA208_SHA256. salt_and_parameters is 18 bytes. key is
  39. 32 bytes.
  40. salt_and_parameters is 18 bytes random salt, 2 bytes iteration
  41. info.
  42. */
  43. #define S2K_TYPE_RFC2440 0
  44. #define S2K_TYPE_PBKDF2 1
  45. #define S2K_TYPE_SCRYPT 2
  46. #define PBKDF2_SPEC_LEN 17
  47. #define PBKDF2_KEY_LEN 20
  48. #define SCRYPT_SPEC_LEN 18
  49. #define SCRYPT_KEY_LEN 32
  50. /** Given an algorithm ID (one of S2K_TYPE_*), return the length of the
  51. * specifier part of it, without the prefix type byte. Return -1 if it is not
  52. * a valid algorithm ID. */
  53. static int
  54. secret_to_key_spec_len(uint8_t type)
  55. {
  56. switch (type) {
  57. case S2K_TYPE_RFC2440:
  58. return S2K_RFC2440_SPECIFIER_LEN;
  59. case S2K_TYPE_PBKDF2:
  60. return PBKDF2_SPEC_LEN;
  61. case S2K_TYPE_SCRYPT:
  62. return SCRYPT_SPEC_LEN;
  63. default:
  64. return -1;
  65. }
  66. }
  67. /** Given an algorithm ID (one of S2K_TYPE_*), return the length of the
  68. * its preferred output. */
  69. static int
  70. secret_to_key_key_len(uint8_t type)
  71. {
  72. switch (type) {
  73. case S2K_TYPE_RFC2440:
  74. return DIGEST_LEN;
  75. case S2K_TYPE_PBKDF2:
  76. return DIGEST_LEN;
  77. case S2K_TYPE_SCRYPT:
  78. return DIGEST256_LEN;
  79. // LCOV_EXCL_START
  80. default:
  81. tor_fragile_assert();
  82. return -1;
  83. // LCOV_EXCL_STOP
  84. }
  85. }
  86. /** Given a specifier in <b>spec_and_key</b> of length
  87. * <b>spec_and_key_len</b>, along with its prefix algorithm ID byte, and along
  88. * with a key if <b>key_included</b> is true, check whether the whole
  89. * specifier-and-key is of valid length, and return the algorithm type if it
  90. * is. Set *<b>legacy_out</b> to 1 iff this is a legacy password hash or
  91. * legacy specifier. Return an error code on failure.
  92. */
  93. static int
  94. secret_to_key_get_type(const uint8_t *spec_and_key, size_t spec_and_key_len,
  95. int key_included, int *legacy_out)
  96. {
  97. size_t legacy_len = S2K_RFC2440_SPECIFIER_LEN;
  98. uint8_t type;
  99. int total_len;
  100. if (key_included)
  101. legacy_len += DIGEST_LEN;
  102. if (spec_and_key_len == legacy_len) {
  103. *legacy_out = 1;
  104. return S2K_TYPE_RFC2440;
  105. }
  106. *legacy_out = 0;
  107. if (spec_and_key_len == 0)
  108. return S2K_BAD_LEN;
  109. type = spec_and_key[0];
  110. total_len = secret_to_key_spec_len(type);
  111. if (total_len < 0)
  112. return S2K_BAD_ALGORITHM;
  113. if (key_included) {
  114. int keylen = secret_to_key_key_len(type);
  115. if (keylen < 0)
  116. return S2K_BAD_ALGORITHM;
  117. total_len += keylen;
  118. }
  119. if ((size_t)total_len + 1 == spec_and_key_len)
  120. return type;
  121. else
  122. return S2K_BAD_LEN;
  123. }
  124. /**
  125. * Write a new random s2k specifier of type <b>type</b>, without prefixing
  126. * type byte, to <b>spec_out</b>, which must have enough room. May adjust
  127. * parameter choice based on <b>flags</b>.
  128. */
  129. static int
  130. make_specifier(uint8_t *spec_out, uint8_t type, unsigned flags)
  131. {
  132. int speclen = secret_to_key_spec_len(type);
  133. if (speclen < 0)
  134. return S2K_BAD_ALGORITHM;
  135. crypto_rand((char*)spec_out, speclen);
  136. switch (type) {
  137. case S2K_TYPE_RFC2440:
  138. /* Hash 64 k of data. */
  139. spec_out[S2K_RFC2440_SPECIFIER_LEN-1] = 96;
  140. break;
  141. case S2K_TYPE_PBKDF2:
  142. /* 131 K iterations */
  143. spec_out[PBKDF2_SPEC_LEN-1] = 17;
  144. break;
  145. case S2K_TYPE_SCRYPT:
  146. if (flags & S2K_FLAG_LOW_MEM) {
  147. /* N = 1<<12 */
  148. spec_out[SCRYPT_SPEC_LEN-2] = 12;
  149. } else {
  150. /* N = 1<<15 */
  151. spec_out[SCRYPT_SPEC_LEN-2] = 15;
  152. }
  153. /* r = 8; p = 2. */
  154. spec_out[SCRYPT_SPEC_LEN-1] = (3u << 4) | (1u << 0);
  155. break;
  156. // LCOV_EXCL_START - we should have returned above.
  157. default:
  158. tor_fragile_assert();
  159. return S2K_BAD_ALGORITHM;
  160. // LCOV_EXCL_STOP
  161. }
  162. return speclen;
  163. }
  164. /** Implement RFC2440-style iterated-salted S2K conversion: convert the
  165. * <b>secret_len</b>-byte <b>secret</b> into a <b>key_out_len</b> byte
  166. * <b>key_out</b>. As in RFC2440, the first 8 bytes of s2k_specifier
  167. * are a salt; the 9th byte describes how much iteration to do.
  168. * If <b>key_out_len</b> &gt; DIGEST_LEN, use HDKF to expand the result.
  169. */
  170. void
  171. secret_to_key_rfc2440(char *key_out, size_t key_out_len, const char *secret,
  172. size_t secret_len, const char *s2k_specifier)
  173. {
  174. crypto_digest_t *d;
  175. uint8_t c;
  176. size_t count, tmplen;
  177. char *tmp;
  178. uint8_t buf[DIGEST_LEN];
  179. tor_assert(key_out_len < SIZE_T_CEILING);
  180. #define EXPBIAS 6
  181. c = s2k_specifier[8];
  182. count = ((uint32_t)16 + (c & 15)) << ((c >> 4) + EXPBIAS);
  183. #undef EXPBIAS
  184. d = crypto_digest_new();
  185. tmplen = 8+secret_len;
  186. tmp = tor_malloc(tmplen);
  187. memcpy(tmp,s2k_specifier,8);
  188. memcpy(tmp+8,secret,secret_len);
  189. secret_len += 8;
  190. while (count) {
  191. if (count >= secret_len) {
  192. crypto_digest_add_bytes(d, tmp, secret_len);
  193. count -= secret_len;
  194. } else {
  195. crypto_digest_add_bytes(d, tmp, count);
  196. count = 0;
  197. }
  198. }
  199. crypto_digest_get_digest(d, (char*)buf, sizeof(buf));
  200. if (key_out_len <= sizeof(buf)) {
  201. memcpy(key_out, buf, key_out_len);
  202. } else {
  203. crypto_expand_key_material_rfc5869_sha256(buf, DIGEST_LEN,
  204. (const uint8_t*)s2k_specifier, 8,
  205. (const uint8_t*)"EXPAND", 6,
  206. (uint8_t*)key_out, key_out_len);
  207. }
  208. memwipe(tmp, 0, tmplen);
  209. memwipe(buf, 0, sizeof(buf));
  210. tor_free(tmp);
  211. crypto_digest_free(d);
  212. }
  213. /**
  214. * Helper: given a valid specifier without prefix type byte in <b>spec</b>,
  215. * whose length must be correct, and given a secret passphrase <b>secret</b>
  216. * of length <b>secret_len</b>, compute the key and store it into
  217. * <b>key_out</b>, which must have enough room for secret_to_key_key_len(type)
  218. * bytes. Return the number of bytes written on success and an error code
  219. * on failure.
  220. */
  221. STATIC int
  222. secret_to_key_compute_key(uint8_t *key_out, size_t key_out_len,
  223. const uint8_t *spec, size_t spec_len,
  224. const char *secret, size_t secret_len,
  225. int type)
  226. {
  227. int rv;
  228. if (key_out_len > INT_MAX)
  229. return S2K_BAD_LEN;
  230. switch (type) {
  231. case S2K_TYPE_RFC2440:
  232. secret_to_key_rfc2440((char*)key_out, key_out_len, secret, secret_len,
  233. (const char*)spec);
  234. return (int)key_out_len;
  235. case S2K_TYPE_PBKDF2: {
  236. uint8_t log_iters;
  237. if (spec_len < 1 || secret_len > INT_MAX || spec_len > INT_MAX)
  238. return S2K_BAD_LEN;
  239. log_iters = spec[spec_len-1];
  240. if (log_iters > 31)
  241. return S2K_BAD_PARAMS;
  242. rv = PKCS5_PBKDF2_HMAC_SHA1(secret, (int)secret_len,
  243. spec, (int)spec_len-1,
  244. (1<<log_iters),
  245. (int)key_out_len, key_out);
  246. if (rv < 0)
  247. return S2K_FAILED;
  248. return (int)key_out_len;
  249. }
  250. case S2K_TYPE_SCRYPT: {
  251. #ifdef HAVE_SCRYPT
  252. uint8_t log_N, log_r, log_p;
  253. uint64_t N;
  254. uint32_t r, p;
  255. if (spec_len < 2)
  256. return S2K_BAD_LEN;
  257. log_N = spec[spec_len-2];
  258. log_r = (spec[spec_len-1]) >> 4;
  259. log_p = (spec[spec_len-1]) & 15;
  260. if (log_N > 63)
  261. return S2K_BAD_PARAMS;
  262. N = ((uint64_t)1) << log_N;
  263. r = 1u << log_r;
  264. p = 1u << log_p;
  265. rv = libscrypt_scrypt((const uint8_t*)secret, secret_len,
  266. spec, spec_len-2, N, r, p, key_out, key_out_len);
  267. if (rv != 0)
  268. return S2K_FAILED;
  269. return (int)key_out_len;
  270. #else /* !(defined(HAVE_SCRYPT)) */
  271. return S2K_NO_SCRYPT_SUPPORT;
  272. #endif /* defined(HAVE_SCRYPT) */
  273. }
  274. default:
  275. return S2K_BAD_ALGORITHM;
  276. }
  277. }
  278. /**
  279. * Given a specifier previously constructed with secret_to_key_make_specifier
  280. * in <b>spec</b> of length <b>spec_len</b>, and a secret password in
  281. * <b>secret</b> of length <b>secret_len</b>, generate <b>key_out_len</b>
  282. * bytes of cryptographic material in <b>key_out</b>. The native output of
  283. * the secret-to-key function will be truncated if key_out_len is short, and
  284. * expanded with HKDF if key_out_len is long. Returns S2K_OKAY on success,
  285. * and an error code on failure.
  286. */
  287. int
  288. secret_to_key_derivekey(uint8_t *key_out, size_t key_out_len,
  289. const uint8_t *spec, size_t spec_len,
  290. const char *secret, size_t secret_len)
  291. {
  292. int legacy_format = 0;
  293. int type = secret_to_key_get_type(spec, spec_len, 0, &legacy_format);
  294. int r;
  295. if (type < 0)
  296. return type;
  297. #ifndef HAVE_SCRYPT
  298. if (type == S2K_TYPE_SCRYPT)
  299. return S2K_NO_SCRYPT_SUPPORT;
  300. #endif
  301. if (! legacy_format) {
  302. ++spec;
  303. --spec_len;
  304. }
  305. r = secret_to_key_compute_key(key_out, key_out_len, spec, spec_len,
  306. secret, secret_len, type);
  307. if (r < 0)
  308. return r;
  309. else
  310. return S2K_OKAY;
  311. }
  312. /**
  313. * Construct a new s2k algorithm specifier and salt in <b>buf</b>, according
  314. * to the bitwise-or of some S2K_FLAG_* options in <b>flags</b>. Up to
  315. * <b>buf_len</b> bytes of storage may be used in <b>buf</b>. Return the
  316. * number of bytes used on success and an error code on failure.
  317. */
  318. int
  319. secret_to_key_make_specifier(uint8_t *buf, size_t buf_len, unsigned flags)
  320. {
  321. int rv;
  322. int spec_len;
  323. #ifdef HAVE_SCRYPT
  324. uint8_t type = S2K_TYPE_SCRYPT;
  325. #else
  326. uint8_t type = S2K_TYPE_RFC2440;
  327. #endif
  328. if (flags & S2K_FLAG_NO_SCRYPT)
  329. type = S2K_TYPE_RFC2440;
  330. if (flags & S2K_FLAG_USE_PBKDF2)
  331. type = S2K_TYPE_PBKDF2;
  332. spec_len = secret_to_key_spec_len(type);
  333. if ((int)buf_len < spec_len + 1)
  334. return S2K_TRUNCATED;
  335. buf[0] = type;
  336. rv = make_specifier(buf+1, type, flags);
  337. if (rv < 0)
  338. return rv;
  339. else
  340. return rv + 1;
  341. }
  342. /**
  343. * Hash a passphrase from <b>secret</b> of length <b>secret_len</b>, according
  344. * to the bitwise-or of some S2K_FLAG_* options in <b>flags</b>, and store the
  345. * hash along with salt and hashing parameters into <b>buf</b>. Up to
  346. * <b>buf_len</b> bytes of storage may be used in <b>buf</b>. Set
  347. * *<b>len_out</b> to the number of bytes used and return S2K_OKAY on success;
  348. * and return an error code on failure.
  349. */
  350. int
  351. secret_to_key_new(uint8_t *buf,
  352. size_t buf_len,
  353. size_t *len_out,
  354. const char *secret, size_t secret_len,
  355. unsigned flags)
  356. {
  357. int key_len;
  358. int spec_len;
  359. int type;
  360. int rv;
  361. spec_len = secret_to_key_make_specifier(buf, buf_len, flags);
  362. if (spec_len < 0)
  363. return spec_len;
  364. type = buf[0];
  365. key_len = secret_to_key_key_len(type);
  366. if (key_len < 0)
  367. return key_len;
  368. if ((int)buf_len < key_len + spec_len)
  369. return S2K_TRUNCATED;
  370. rv = secret_to_key_compute_key(buf + spec_len, key_len,
  371. buf + 1, spec_len-1,
  372. secret, secret_len, type);
  373. if (rv < 0)
  374. return rv;
  375. *len_out = spec_len + key_len;
  376. return S2K_OKAY;
  377. }
  378. /**
  379. * Given a hashed passphrase in <b>spec_and_key</b> of length
  380. * <b>spec_and_key_len</b> as generated by secret_to_key_new(), verify whether
  381. * it is a hash of the passphrase <b>secret</b> of length <b>secret_len</b>.
  382. * Return S2K_OKAY on a match, S2K_BAD_SECRET on a well-formed hash that
  383. * doesn't match this secret, and another error code on other errors.
  384. */
  385. int
  386. secret_to_key_check(const uint8_t *spec_and_key, size_t spec_and_key_len,
  387. const char *secret, size_t secret_len)
  388. {
  389. int is_legacy = 0;
  390. int type = secret_to_key_get_type(spec_and_key, spec_and_key_len,
  391. 1, &is_legacy);
  392. uint8_t buf[32];
  393. int spec_len;
  394. int key_len;
  395. int rv;
  396. if (type < 0)
  397. return type;
  398. if (! is_legacy) {
  399. spec_and_key++;
  400. spec_and_key_len--;
  401. }
  402. spec_len = secret_to_key_spec_len(type);
  403. key_len = secret_to_key_key_len(type);
  404. tor_assert(spec_len > 0);
  405. tor_assert(key_len > 0);
  406. tor_assert(key_len <= (int) sizeof(buf));
  407. tor_assert((int)spec_and_key_len == spec_len + key_len);
  408. rv = secret_to_key_compute_key(buf, key_len,
  409. spec_and_key, spec_len,
  410. secret, secret_len, type);
  411. if (rv < 0)
  412. goto done;
  413. if (tor_memeq(buf, spec_and_key + spec_len, key_len))
  414. rv = S2K_OKAY;
  415. else
  416. rv = S2K_BAD_SECRET;
  417. done:
  418. memwipe(buf, 0, sizeof(buf));
  419. return rv;
  420. }