crypto_s2k.c 12 KB

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