crypto_s2k.c 13 KB

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