crypto_s2k.c 14 KB

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