crypto_s2k.c 13 KB

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