crypto_s2k.c 13 KB

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