crypto_rsa.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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_rsa.c
  8. * \brief Block of functions related with RSA utilities and operations.
  9. **/
  10. #include "lib/crypt_ops/crypto_cipher.h"
  11. #include "lib/crypt_ops/crypto_curve25519.h"
  12. #include "lib/crypt_ops/crypto_digest.h"
  13. #include "lib/crypt_ops/crypto_format.h"
  14. #include "lib/crypt_ops/compat_openssl.h"
  15. #include "lib/crypt_ops/crypto_rand.h"
  16. #include "lib/crypt_ops/crypto_rsa.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 "lib/fs/files.h"
  21. #include "lib/log/log.h"
  22. #include "lib/encoding/binascii.h"
  23. #include <string.h>
  24. /** Return the number of bytes added by padding method <b>padding</b>.
  25. */
  26. int
  27. crypto_get_rsa_padding_overhead(int padding)
  28. {
  29. switch (padding)
  30. {
  31. case RSA_PKCS1_OAEP_PADDING: return PKCS1_OAEP_PADDING_OVERHEAD;
  32. default: tor_assert(0); return -1; // LCOV_EXCL_LINE
  33. }
  34. }
  35. /** Given a padding method <b>padding</b>, return the correct OpenSSL constant.
  36. */
  37. int
  38. crypto_get_rsa_padding(int padding)
  39. {
  40. switch (padding)
  41. {
  42. case PK_PKCS1_OAEP_PADDING: return RSA_PKCS1_OAEP_PADDING;
  43. default: tor_assert(0); return -1; // LCOV_EXCL_LINE
  44. }
  45. }
  46. /** Compare the public-key components of a and b. Return non-zero iff
  47. * a==b. A NULL key is considered to be distinct from all non-NULL
  48. * keys, and equal to itself.
  49. *
  50. * Note that this may leak information about the keys through timing.
  51. */
  52. int
  53. crypto_pk_eq_keys(const crypto_pk_t *a, const crypto_pk_t *b)
  54. {
  55. return (crypto_pk_cmp_keys(a, b) == 0);
  56. }
  57. /** Perform a hybrid (public/secret) encryption on <b>fromlen</b>
  58. * bytes of data from <b>from</b>, with padding type 'padding',
  59. * storing the results on <b>to</b>.
  60. *
  61. * Returns the number of bytes written on success, -1 on failure.
  62. *
  63. * The encrypted data consists of:
  64. * - The source data, padded and encrypted with the public key, if the
  65. * padded source data is no longer than the public key, and <b>force</b>
  66. * is false, OR
  67. * - The beginning of the source data prefixed with a 16-byte symmetric key,
  68. * padded and encrypted with the public key; followed by the rest of
  69. * the source data encrypted in AES-CTR mode with the symmetric key.
  70. *
  71. * NOTE that this format does not authenticate the symmetrically encrypted
  72. * part of the data, and SHOULD NOT BE USED for new protocols.
  73. */
  74. int
  75. crypto_pk_obsolete_public_hybrid_encrypt(crypto_pk_t *env,
  76. char *to, size_t tolen,
  77. const char *from,
  78. size_t fromlen,
  79. int padding, int force)
  80. {
  81. int overhead, outlen, r;
  82. size_t pkeylen, symlen;
  83. crypto_cipher_t *cipher = NULL;
  84. char *buf = NULL;
  85. tor_assert(env);
  86. tor_assert(from);
  87. tor_assert(to);
  88. tor_assert(fromlen < SIZE_T_CEILING);
  89. overhead = crypto_get_rsa_padding_overhead(crypto_get_rsa_padding(padding));
  90. pkeylen = crypto_pk_keysize(env);
  91. if (!force && fromlen+overhead <= pkeylen) {
  92. /* It all fits in a single encrypt. */
  93. return crypto_pk_public_encrypt(env,to,
  94. tolen,
  95. from,fromlen,padding);
  96. }
  97. tor_assert(tolen >= fromlen + overhead + CIPHER_KEY_LEN);
  98. tor_assert(tolen >= pkeylen);
  99. char key[CIPHER_KEY_LEN];
  100. crypto_rand(key, sizeof(key)); /* generate a new key. */
  101. cipher = crypto_cipher_new(key);
  102. buf = tor_malloc(pkeylen+1);
  103. memcpy(buf, key, CIPHER_KEY_LEN);
  104. memcpy(buf+CIPHER_KEY_LEN, from, pkeylen-overhead-CIPHER_KEY_LEN);
  105. /* Length of symmetrically encrypted data. */
  106. symlen = fromlen-(pkeylen-overhead-CIPHER_KEY_LEN);
  107. outlen = crypto_pk_public_encrypt(env,to,tolen,buf,pkeylen-overhead,padding);
  108. if (outlen!=(int)pkeylen) {
  109. goto err;
  110. }
  111. r = crypto_cipher_encrypt(cipher, to+outlen,
  112. from+pkeylen-overhead-CIPHER_KEY_LEN, symlen);
  113. if (r<0) goto err;
  114. memwipe(buf, 0, pkeylen);
  115. memwipe(key, 0, sizeof(key));
  116. tor_free(buf);
  117. crypto_cipher_free(cipher);
  118. tor_assert(outlen+symlen < INT_MAX);
  119. return (int)(outlen + symlen);
  120. err:
  121. memwipe(buf, 0, pkeylen);
  122. memwipe(key, 0, sizeof(key));
  123. tor_free(buf);
  124. crypto_cipher_free(cipher);
  125. return -1;
  126. }
  127. /** Invert crypto_pk_obsolete_public_hybrid_encrypt. Returns the number of
  128. * bytes written on success, -1 on failure.
  129. *
  130. * NOTE that this format does not authenticate the symmetrically encrypted
  131. * part of the data, and SHOULD NOT BE USED for new protocols.
  132. */
  133. int
  134. crypto_pk_obsolete_private_hybrid_decrypt(crypto_pk_t *env,
  135. char *to,
  136. size_t tolen,
  137. const char *from,
  138. size_t fromlen,
  139. int padding, int warnOnFailure)
  140. {
  141. int outlen, r;
  142. size_t pkeylen;
  143. crypto_cipher_t *cipher = NULL;
  144. char *buf = NULL;
  145. tor_assert(fromlen < SIZE_T_CEILING);
  146. pkeylen = crypto_pk_keysize(env);
  147. if (fromlen <= pkeylen) {
  148. return crypto_pk_private_decrypt(env,to,tolen,from,fromlen,padding,
  149. warnOnFailure);
  150. }
  151. buf = tor_malloc(pkeylen);
  152. outlen = crypto_pk_private_decrypt(env,buf,pkeylen,from,pkeylen,padding,
  153. warnOnFailure);
  154. if (outlen<0) {
  155. log_fn(warnOnFailure?LOG_WARN:LOG_DEBUG, LD_CRYPTO,
  156. "Error decrypting public-key data");
  157. goto err;
  158. }
  159. if (outlen < CIPHER_KEY_LEN) {
  160. log_fn(warnOnFailure?LOG_WARN:LOG_INFO, LD_CRYPTO,
  161. "No room for a symmetric key");
  162. goto err;
  163. }
  164. cipher = crypto_cipher_new(buf);
  165. if (!cipher) {
  166. goto err;
  167. }
  168. memcpy(to,buf+CIPHER_KEY_LEN,outlen-CIPHER_KEY_LEN);
  169. outlen -= CIPHER_KEY_LEN;
  170. tor_assert(tolen - outlen >= fromlen - pkeylen);
  171. r = crypto_cipher_decrypt(cipher, to+outlen, from+pkeylen, fromlen-pkeylen);
  172. if (r<0)
  173. goto err;
  174. memwipe(buf,0,pkeylen);
  175. tor_free(buf);
  176. crypto_cipher_free(cipher);
  177. tor_assert(outlen + fromlen < INT_MAX);
  178. return (int)(outlen + (fromlen-pkeylen));
  179. err:
  180. memwipe(buf,0,pkeylen);
  181. tor_free(buf);
  182. crypto_cipher_free(cipher);
  183. return -1;
  184. }
  185. /** Given a private or public key <b>pk</b>, put a fingerprint of the
  186. * public key into <b>fp_out</b> (must have at least FINGERPRINT_LEN+1 bytes of
  187. * space). Return 0 on success, -1 on failure.
  188. *
  189. * Fingerprints are computed as the SHA1 digest of the ASN.1 encoding
  190. * of the public key, converted to hexadecimal, in upper case, with a
  191. * space after every four digits.
  192. *
  193. * If <b>add_space</b> is false, omit the spaces.
  194. */
  195. int
  196. crypto_pk_get_fingerprint(crypto_pk_t *pk, char *fp_out, int add_space)
  197. {
  198. char digest[DIGEST_LEN];
  199. char hexdigest[HEX_DIGEST_LEN+1];
  200. if (crypto_pk_get_digest(pk, digest)) {
  201. return -1;
  202. }
  203. base16_encode(hexdigest,sizeof(hexdigest),digest,DIGEST_LEN);
  204. if (add_space) {
  205. crypto_add_spaces_to_fp(fp_out, FINGERPRINT_LEN+1, hexdigest);
  206. } else {
  207. strncpy(fp_out, hexdigest, HEX_DIGEST_LEN+1);
  208. }
  209. return 0;
  210. }
  211. /** Given a private or public key <b>pk</b>, put a hashed fingerprint of
  212. * the public key into <b>fp_out</b> (must have at least FINGERPRINT_LEN+1
  213. * bytes of space). Return 0 on success, -1 on failure.
  214. *
  215. * Hashed fingerprints are computed as the SHA1 digest of the SHA1 digest
  216. * of the ASN.1 encoding of the public key, converted to hexadecimal, in
  217. * upper case.
  218. */
  219. int
  220. crypto_pk_get_hashed_fingerprint(crypto_pk_t *pk, char *fp_out)
  221. {
  222. char digest[DIGEST_LEN], hashed_digest[DIGEST_LEN];
  223. if (crypto_pk_get_digest(pk, digest)) {
  224. return -1;
  225. }
  226. if (crypto_digest(hashed_digest, digest, DIGEST_LEN) < 0) {
  227. return -1;
  228. }
  229. base16_encode(fp_out, FINGERPRINT_LEN + 1, hashed_digest, DIGEST_LEN);
  230. return 0;
  231. }
  232. /** Copy <b>in</b> to the <b>outlen</b>-byte buffer <b>out</b>, adding spaces
  233. * every four characters. */
  234. void
  235. crypto_add_spaces_to_fp(char *out, size_t outlen, const char *in)
  236. {
  237. int n = 0;
  238. char *end = out+outlen;
  239. tor_assert(outlen < SIZE_T_CEILING);
  240. while (*in && out<end) {
  241. *out++ = *in++;
  242. if (++n == 4 && *in && out<end) {
  243. n = 0;
  244. *out++ = ' ';
  245. }
  246. }
  247. tor_assert(out<end);
  248. *out = '\0';
  249. }
  250. /** Check a siglen-byte long signature at <b>sig</b> against
  251. * <b>datalen</b> bytes of data at <b>data</b>, using the public key
  252. * in <b>env</b>. Return 0 if <b>sig</b> is a correct signature for
  253. * SHA1(data). Else return -1.
  254. */
  255. MOCK_IMPL(int,
  256. crypto_pk_public_checksig_digest,(crypto_pk_t *env, const char *data,
  257. size_t datalen, const char *sig,
  258. size_t siglen))
  259. {
  260. char digest[DIGEST_LEN];
  261. char *buf;
  262. size_t buflen;
  263. int r;
  264. tor_assert(env);
  265. tor_assert(data);
  266. tor_assert(sig);
  267. tor_assert(datalen < SIZE_T_CEILING);
  268. tor_assert(siglen < SIZE_T_CEILING);
  269. if (crypto_digest(digest,data,datalen)<0) {
  270. log_warn(LD_BUG, "couldn't compute digest");
  271. return -1;
  272. }
  273. buflen = crypto_pk_keysize(env);
  274. buf = tor_malloc(buflen);
  275. r = crypto_pk_public_checksig(env,buf,buflen,sig,siglen);
  276. if (r != DIGEST_LEN) {
  277. log_warn(LD_CRYPTO, "Invalid signature");
  278. tor_free(buf);
  279. return -1;
  280. }
  281. if (tor_memneq(buf, digest, DIGEST_LEN)) {
  282. log_warn(LD_CRYPTO, "Signature mismatched with digest.");
  283. tor_free(buf);
  284. return -1;
  285. }
  286. tor_free(buf);
  287. return 0;
  288. }
  289. /** Compute a SHA1 digest of <b>fromlen</b> bytes of data stored at
  290. * <b>from</b>; sign the data with the private key in <b>env</b>, and
  291. * store it in <b>to</b>. Return the number of bytes written on
  292. * success, and -1 on failure.
  293. *
  294. * <b>tolen</b> is the number of writable bytes in <b>to</b>, and must be
  295. * at least the length of the modulus of <b>env</b>.
  296. */
  297. int
  298. crypto_pk_private_sign_digest(crypto_pk_t *env, char *to, size_t tolen,
  299. const char *from, size_t fromlen)
  300. {
  301. int r;
  302. char digest[DIGEST_LEN];
  303. if (crypto_digest(digest,from,fromlen)<0)
  304. return -1;
  305. r = crypto_pk_private_sign(env,to,tolen,digest,DIGEST_LEN);
  306. memwipe(digest, 0, sizeof(digest));
  307. return r;
  308. }
  309. /** Given a private or public key <b>pk</b>, put a SHA1 hash of the
  310. * public key into <b>digest_out</b> (must have DIGEST_LEN bytes of space).
  311. * Return 0 on success, -1 on failure.
  312. */
  313. int
  314. crypto_pk_get_digest(const crypto_pk_t *pk, char *digest_out)
  315. {
  316. char *buf;
  317. size_t buflen;
  318. int len;
  319. int rv = -1;
  320. buflen = crypto_pk_keysize(pk)*2;
  321. buf = tor_malloc(buflen);
  322. len = crypto_pk_asn1_encode(pk, buf, buflen);
  323. if (len < 0)
  324. goto done;
  325. if (crypto_digest(digest_out, buf, len) < 0)
  326. goto done;
  327. rv = 0;
  328. done:
  329. tor_free(buf);
  330. return rv;
  331. }
  332. /** Compute all digests of the DER encoding of <b>pk</b>, and store them
  333. * in <b>digests_out</b>. Return 0 on success, -1 on failure. */
  334. int
  335. crypto_pk_get_common_digests(crypto_pk_t *pk, common_digests_t *digests_out)
  336. {
  337. char *buf;
  338. size_t buflen;
  339. int len;
  340. int rv = -1;
  341. buflen = crypto_pk_keysize(pk)*2;
  342. buf = tor_malloc(buflen);
  343. len = crypto_pk_asn1_encode(pk, buf, buflen);
  344. if (len < 0)
  345. goto done;
  346. if (crypto_common_digests(digests_out, (char*)buf, len) < 0)
  347. goto done;
  348. rv = 0;
  349. done:
  350. tor_free(buf);
  351. return rv;
  352. }