archive_cryptor.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. /*-
  2. * Copyright (c) 2014 Michihiro NAKAJIMA
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
  15. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  16. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  17. * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
  18. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  19. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  20. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  21. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  23. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include "archive_platform.h"
  26. #ifdef HAVE_STRING_H
  27. #include <string.h>
  28. #endif
  29. #include "archive.h"
  30. #include "archive_cryptor_private.h"
  31. /*
  32. * On systems that do not support any recognized crypto libraries,
  33. * this file will normally define no usable symbols.
  34. *
  35. * But some compilers and linkers choke on empty object files, so
  36. * define a public symbol that will always exist. This could
  37. * be removed someday if this file gains another always-present
  38. * symbol definition.
  39. */
  40. int __libarchive_cryptor_build_hack(void) {
  41. return 0;
  42. }
  43. #ifdef ARCHIVE_CRYPTOR_USE_Apple_CommonCrypto
  44. static int
  45. pbkdf2_sha1(const char *pw, size_t pw_len, const uint8_t *salt,
  46. size_t salt_len, unsigned rounds, uint8_t *derived_key,
  47. size_t derived_key_len)
  48. {
  49. CCKeyDerivationPBKDF(kCCPBKDF2, (const char *)pw,
  50. pw_len, salt, salt_len, kCCPRFHmacAlgSHA1, rounds,
  51. derived_key, derived_key_len);
  52. return 0;
  53. }
  54. #elif defined(_WIN32) && !defined(__CYGWIN__) && defined(HAVE_BCRYPT_H)
  55. #ifdef _MSC_VER
  56. #pragma comment(lib, "Bcrypt.lib")
  57. #endif
  58. static int
  59. pbkdf2_sha1(const char *pw, size_t pw_len, const uint8_t *salt,
  60. size_t salt_len, unsigned rounds, uint8_t *derived_key,
  61. size_t derived_key_len)
  62. {
  63. NTSTATUS status;
  64. BCRYPT_ALG_HANDLE hAlg;
  65. status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_SHA1_ALGORITHM,
  66. MS_PRIMITIVE_PROVIDER, BCRYPT_ALG_HANDLE_HMAC_FLAG);
  67. if (!BCRYPT_SUCCESS(status))
  68. return -1;
  69. status = BCryptDeriveKeyPBKDF2(hAlg,
  70. (PUCHAR)(uintptr_t)pw, (ULONG)pw_len,
  71. (PUCHAR)(uintptr_t)salt, (ULONG)salt_len, rounds,
  72. (PUCHAR)derived_key, (ULONG)derived_key_len, 0);
  73. BCryptCloseAlgorithmProvider(hAlg, 0);
  74. return (BCRYPT_SUCCESS(status)) ? 0: -1;
  75. }
  76. #elif defined(HAVE_LIBMBEDCRYPTO) && defined(HAVE_MBEDTLS_PKCS5_H)
  77. static int
  78. pbkdf2_sha1(const char *pw, size_t pw_len, const uint8_t *salt,
  79. size_t salt_len, unsigned rounds, uint8_t *derived_key,
  80. size_t derived_key_len)
  81. {
  82. mbedtls_md_context_t ctx;
  83. const mbedtls_md_info_t *info;
  84. int ret;
  85. mbedtls_md_init(&ctx);
  86. info = mbedtls_md_info_from_type(MBEDTLS_MD_SHA1);
  87. if (info == NULL) {
  88. mbedtls_md_free(&ctx);
  89. return (-1);
  90. }
  91. ret = mbedtls_md_setup(&ctx, info, 1);
  92. if (ret != 0) {
  93. mbedtls_md_free(&ctx);
  94. return (-1);
  95. }
  96. ret = mbedtls_pkcs5_pbkdf2_hmac(&ctx, (const unsigned char *)pw,
  97. pw_len, salt, salt_len, rounds, derived_key_len, derived_key);
  98. mbedtls_md_free(&ctx);
  99. return (ret);
  100. }
  101. #elif defined(HAVE_LIBNETTLE) && defined(HAVE_NETTLE_PBKDF2_H)
  102. static int
  103. pbkdf2_sha1(const char *pw, size_t pw_len, const uint8_t *salt,
  104. size_t salt_len, unsigned rounds, uint8_t *derived_key,
  105. size_t derived_key_len) {
  106. pbkdf2_hmac_sha1((unsigned)pw_len, (const uint8_t *)pw, rounds,
  107. salt_len, salt, derived_key_len, derived_key);
  108. return 0;
  109. }
  110. #elif defined(HAVE_LIBCRYPTO) && defined(HAVE_PKCS5_PBKDF2_HMAC_SHA1)
  111. static int
  112. pbkdf2_sha1(const char *pw, size_t pw_len, const uint8_t *salt,
  113. size_t salt_len, unsigned rounds, uint8_t *derived_key,
  114. size_t derived_key_len) {
  115. PKCS5_PBKDF2_HMAC_SHA1(pw, pw_len, salt, salt_len, rounds,
  116. derived_key_len, derived_key);
  117. return 0;
  118. }
  119. #else
  120. /* Stub */
  121. static int
  122. pbkdf2_sha1(const char *pw, size_t pw_len, const uint8_t *salt,
  123. size_t salt_len, unsigned rounds, uint8_t *derived_key,
  124. size_t derived_key_len) {
  125. (void)pw; /* UNUSED */
  126. (void)pw_len; /* UNUSED */
  127. (void)salt; /* UNUSED */
  128. (void)salt_len; /* UNUSED */
  129. (void)rounds; /* UNUSED */
  130. (void)derived_key; /* UNUSED */
  131. (void)derived_key_len; /* UNUSED */
  132. return -1; /* UNSUPPORTED */
  133. }
  134. #endif
  135. #ifdef ARCHIVE_CRYPTOR_USE_Apple_CommonCrypto
  136. # if MAC_OS_X_VERSION_MAX_ALLOWED < 1090
  137. # define kCCAlgorithmAES kCCAlgorithmAES128
  138. # endif
  139. static int
  140. aes_ctr_init(archive_crypto_ctx *ctx, const uint8_t *key, size_t key_len)
  141. {
  142. CCCryptorStatus r;
  143. ctx->key_len = key_len;
  144. memcpy(ctx->key, key, key_len);
  145. memset(ctx->nonce, 0, sizeof(ctx->nonce));
  146. ctx->encr_pos = AES_BLOCK_SIZE;
  147. r = CCCryptorCreateWithMode(kCCEncrypt, kCCModeECB, kCCAlgorithmAES,
  148. ccNoPadding, NULL, key, key_len, NULL, 0, 0, 0, &ctx->ctx);
  149. return (r == kCCSuccess)? 0: -1;
  150. }
  151. static int
  152. aes_ctr_encrypt_counter(archive_crypto_ctx *ctx)
  153. {
  154. CCCryptorRef ref = ctx->ctx;
  155. CCCryptorStatus r;
  156. r = CCCryptorReset(ref, NULL);
  157. if (r != kCCSuccess && r != kCCUnimplemented)
  158. return -1;
  159. r = CCCryptorUpdate(ref, ctx->nonce, AES_BLOCK_SIZE, ctx->encr_buf,
  160. AES_BLOCK_SIZE, NULL);
  161. return (r == kCCSuccess)? 0: -1;
  162. }
  163. static int
  164. aes_ctr_release(archive_crypto_ctx *ctx)
  165. {
  166. memset(ctx->key, 0, ctx->key_len);
  167. memset(ctx->nonce, 0, sizeof(ctx->nonce));
  168. return 0;
  169. }
  170. #elif defined(_WIN32) && !defined(__CYGWIN__) && defined(HAVE_BCRYPT_H)
  171. static int
  172. aes_ctr_init(archive_crypto_ctx *ctx, const uint8_t *key, size_t key_len)
  173. {
  174. BCRYPT_ALG_HANDLE hAlg;
  175. BCRYPT_KEY_HANDLE hKey;
  176. DWORD keyObj_len, aes_key_len;
  177. PBYTE keyObj;
  178. ULONG result;
  179. NTSTATUS status;
  180. BCRYPT_KEY_LENGTHS_STRUCT key_lengths;
  181. ctx->hAlg = NULL;
  182. ctx->hKey = NULL;
  183. ctx->keyObj = NULL;
  184. switch (key_len) {
  185. case 16: aes_key_len = 128; break;
  186. case 24: aes_key_len = 192; break;
  187. case 32: aes_key_len = 256; break;
  188. default: return -1;
  189. }
  190. status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_AES_ALGORITHM,
  191. MS_PRIMITIVE_PROVIDER, 0);
  192. if (!BCRYPT_SUCCESS(status))
  193. return -1;
  194. status = BCryptGetProperty(hAlg, BCRYPT_KEY_LENGTHS, (PUCHAR)&key_lengths,
  195. sizeof(key_lengths), &result, 0);
  196. if (!BCRYPT_SUCCESS(status)) {
  197. BCryptCloseAlgorithmProvider(hAlg, 0);
  198. return -1;
  199. }
  200. if (key_lengths.dwMinLength > aes_key_len
  201. || key_lengths.dwMaxLength < aes_key_len) {
  202. BCryptCloseAlgorithmProvider(hAlg, 0);
  203. return -1;
  204. }
  205. status = BCryptGetProperty(hAlg, BCRYPT_OBJECT_LENGTH, (PUCHAR)&keyObj_len,
  206. sizeof(keyObj_len), &result, 0);
  207. if (!BCRYPT_SUCCESS(status)) {
  208. BCryptCloseAlgorithmProvider(hAlg, 0);
  209. return -1;
  210. }
  211. keyObj = (PBYTE)HeapAlloc(GetProcessHeap(), 0, keyObj_len);
  212. if (keyObj == NULL) {
  213. BCryptCloseAlgorithmProvider(hAlg, 0);
  214. return -1;
  215. }
  216. status = BCryptSetProperty(hAlg, BCRYPT_CHAINING_MODE,
  217. (PUCHAR)BCRYPT_CHAIN_MODE_ECB, sizeof(BCRYPT_CHAIN_MODE_ECB), 0);
  218. if (!BCRYPT_SUCCESS(status)) {
  219. BCryptCloseAlgorithmProvider(hAlg, 0);
  220. HeapFree(GetProcessHeap(), 0, keyObj);
  221. return -1;
  222. }
  223. status = BCryptGenerateSymmetricKey(hAlg, &hKey,
  224. keyObj, keyObj_len,
  225. (PUCHAR)(uintptr_t)key, (ULONG)key_len, 0);
  226. if (!BCRYPT_SUCCESS(status)) {
  227. BCryptCloseAlgorithmProvider(hAlg, 0);
  228. HeapFree(GetProcessHeap(), 0, keyObj);
  229. return -1;
  230. }
  231. ctx->hAlg = hAlg;
  232. ctx->hKey = hKey;
  233. ctx->keyObj = keyObj;
  234. ctx->keyObj_len = keyObj_len;
  235. ctx->encr_pos = AES_BLOCK_SIZE;
  236. return 0;
  237. }
  238. static int
  239. aes_ctr_encrypt_counter(archive_crypto_ctx *ctx)
  240. {
  241. NTSTATUS status;
  242. ULONG result;
  243. status = BCryptEncrypt(ctx->hKey, (PUCHAR)ctx->nonce, AES_BLOCK_SIZE,
  244. NULL, NULL, 0, (PUCHAR)ctx->encr_buf, AES_BLOCK_SIZE,
  245. &result, 0);
  246. return BCRYPT_SUCCESS(status) ? 0 : -1;
  247. }
  248. static int
  249. aes_ctr_release(archive_crypto_ctx *ctx)
  250. {
  251. if (ctx->hAlg != NULL) {
  252. BCryptCloseAlgorithmProvider(ctx->hAlg, 0);
  253. ctx->hAlg = NULL;
  254. BCryptDestroyKey(ctx->hKey);
  255. ctx->hKey = NULL;
  256. HeapFree(GetProcessHeap(), 0, ctx->keyObj);
  257. ctx->keyObj = NULL;
  258. }
  259. memset(ctx, 0, sizeof(*ctx));
  260. return 0;
  261. }
  262. #elif defined(HAVE_LIBMBEDCRYPTO) && defined(HAVE_MBEDTLS_AES_H)
  263. static int
  264. aes_ctr_init(archive_crypto_ctx *ctx, const uint8_t *key, size_t key_len)
  265. {
  266. mbedtls_aes_init(&ctx->ctx);
  267. ctx->key_len = key_len;
  268. memcpy(ctx->key, key, key_len);
  269. memset(ctx->nonce, 0, sizeof(ctx->nonce));
  270. ctx->encr_pos = AES_BLOCK_SIZE;
  271. return 0;
  272. }
  273. static int
  274. aes_ctr_encrypt_counter(archive_crypto_ctx *ctx)
  275. {
  276. if (mbedtls_aes_setkey_enc(&ctx->ctx, ctx->key,
  277. ctx->key_len * 8) != 0)
  278. return (-1);
  279. if (mbedtls_aes_crypt_ecb(&ctx->ctx, MBEDTLS_AES_ENCRYPT, ctx->nonce,
  280. ctx->encr_buf) != 0)
  281. return (-1);
  282. return 0;
  283. }
  284. static int
  285. aes_ctr_release(archive_crypto_ctx *ctx)
  286. {
  287. mbedtls_aes_free(&ctx->ctx);
  288. memset(ctx, 0, sizeof(*ctx));
  289. return 0;
  290. }
  291. #elif defined(HAVE_LIBNETTLE) && defined(HAVE_NETTLE_AES_H)
  292. static int
  293. aes_ctr_init(archive_crypto_ctx *ctx, const uint8_t *key, size_t key_len)
  294. {
  295. ctx->key_len = key_len;
  296. memcpy(ctx->key, key, key_len);
  297. memset(ctx->nonce, 0, sizeof(ctx->nonce));
  298. ctx->encr_pos = AES_BLOCK_SIZE;
  299. memset(&ctx->ctx, 0, sizeof(ctx->ctx));
  300. return 0;
  301. }
  302. static int
  303. aes_ctr_encrypt_counter(archive_crypto_ctx *ctx)
  304. {
  305. #if NETTLE_VERSION_MAJOR < 3
  306. aes_set_encrypt_key(&ctx->ctx, ctx->key_len, ctx->key);
  307. aes_encrypt(&ctx->ctx, AES_BLOCK_SIZE, ctx->encr_buf, ctx->nonce);
  308. #else
  309. switch(ctx->key_len) {
  310. case AES128_KEY_SIZE:
  311. aes128_set_encrypt_key(&ctx->ctx.c128, ctx->key);
  312. aes128_encrypt(&ctx->ctx.c128, AES_BLOCK_SIZE, ctx->encr_buf,
  313. ctx->nonce);
  314. break;
  315. case AES192_KEY_SIZE:
  316. aes192_set_encrypt_key(&ctx->ctx.c192, ctx->key);
  317. aes192_encrypt(&ctx->ctx.c192, AES_BLOCK_SIZE, ctx->encr_buf,
  318. ctx->nonce);
  319. break;
  320. case AES256_KEY_SIZE:
  321. aes256_set_encrypt_key(&ctx->ctx.c256, ctx->key);
  322. aes256_encrypt(&ctx->ctx.c256, AES_BLOCK_SIZE, ctx->encr_buf,
  323. ctx->nonce);
  324. break;
  325. default:
  326. return -1;
  327. break;
  328. }
  329. #endif
  330. return 0;
  331. }
  332. static int
  333. aes_ctr_release(archive_crypto_ctx *ctx)
  334. {
  335. memset(ctx, 0, sizeof(*ctx));
  336. return 0;
  337. }
  338. #elif defined(HAVE_LIBCRYPTO)
  339. static int
  340. aes_ctr_init(archive_crypto_ctx *ctx, const uint8_t *key, size_t key_len)
  341. {
  342. if ((ctx->ctx = EVP_CIPHER_CTX_new()) == NULL)
  343. return -1;
  344. switch (key_len) {
  345. case 16: ctx->type = EVP_aes_128_ecb(); break;
  346. case 24: ctx->type = EVP_aes_192_ecb(); break;
  347. case 32: ctx->type = EVP_aes_256_ecb(); break;
  348. default: ctx->type = NULL; return -1;
  349. }
  350. ctx->key_len = key_len;
  351. memcpy(ctx->key, key, key_len);
  352. memset(ctx->nonce, 0, sizeof(ctx->nonce));
  353. ctx->encr_pos = AES_BLOCK_SIZE;
  354. return 0;
  355. }
  356. static int
  357. aes_ctr_encrypt_counter(archive_crypto_ctx *ctx)
  358. {
  359. int outl = 0;
  360. int r;
  361. r = EVP_EncryptInit_ex(ctx->ctx, ctx->type, NULL, ctx->key, NULL);
  362. if (r == 0)
  363. return -1;
  364. r = EVP_EncryptUpdate(ctx->ctx, ctx->encr_buf, &outl, ctx->nonce,
  365. AES_BLOCK_SIZE);
  366. if (r == 0 || outl != AES_BLOCK_SIZE)
  367. return -1;
  368. return 0;
  369. }
  370. static int
  371. aes_ctr_release(archive_crypto_ctx *ctx)
  372. {
  373. EVP_CIPHER_CTX_free(ctx->ctx);
  374. OPENSSL_cleanse(ctx->key, ctx->key_len);
  375. OPENSSL_cleanse(ctx->nonce, sizeof(ctx->nonce));
  376. return 0;
  377. }
  378. #else
  379. #define ARCHIVE_CRYPTOR_STUB
  380. /* Stub */
  381. static int
  382. aes_ctr_init(archive_crypto_ctx *ctx, const uint8_t *key, size_t key_len)
  383. {
  384. (void)ctx; /* UNUSED */
  385. (void)key; /* UNUSED */
  386. (void)key_len; /* UNUSED */
  387. return -1;
  388. }
  389. static int
  390. aes_ctr_encrypt_counter(archive_crypto_ctx *ctx)
  391. {
  392. (void)ctx; /* UNUSED */
  393. return -1;
  394. }
  395. static int
  396. aes_ctr_release(archive_crypto_ctx *ctx)
  397. {
  398. (void)ctx; /* UNUSED */
  399. return 0;
  400. }
  401. #endif
  402. #ifdef ARCHIVE_CRYPTOR_STUB
  403. static int
  404. aes_ctr_update(archive_crypto_ctx *ctx, const uint8_t * const in,
  405. size_t in_len, uint8_t * const out, size_t *out_len)
  406. {
  407. (void)ctx; /* UNUSED */
  408. (void)in; /* UNUSED */
  409. (void)in_len; /* UNUSED */
  410. (void)out; /* UNUSED */
  411. (void)out_len; /* UNUSED */
  412. aes_ctr_encrypt_counter(ctx); /* UNUSED */ /* Fix unused function warning */
  413. return -1;
  414. }
  415. #else
  416. static void
  417. aes_ctr_increase_counter(archive_crypto_ctx *ctx)
  418. {
  419. uint8_t *const nonce = ctx->nonce;
  420. int j;
  421. for (j = 0; j < 8; j++) {
  422. if (++nonce[j])
  423. break;
  424. }
  425. }
  426. static int
  427. aes_ctr_update(archive_crypto_ctx *ctx, const uint8_t * const in,
  428. size_t in_len, uint8_t * const out, size_t *out_len)
  429. {
  430. uint8_t *const ebuf = ctx->encr_buf;
  431. unsigned pos = ctx->encr_pos;
  432. unsigned max = (unsigned)((in_len < *out_len)? in_len: *out_len);
  433. unsigned i;
  434. for (i = 0; i < max; ) {
  435. if (pos == AES_BLOCK_SIZE) {
  436. aes_ctr_increase_counter(ctx);
  437. if (aes_ctr_encrypt_counter(ctx) != 0)
  438. return -1;
  439. while (max -i >= AES_BLOCK_SIZE) {
  440. for (pos = 0; pos < AES_BLOCK_SIZE; pos++)
  441. out[i+pos] = in[i+pos] ^ ebuf[pos];
  442. i += AES_BLOCK_SIZE;
  443. aes_ctr_increase_counter(ctx);
  444. if (aes_ctr_encrypt_counter(ctx) != 0)
  445. return -1;
  446. }
  447. pos = 0;
  448. if (i >= max)
  449. break;
  450. }
  451. out[i] = in[i] ^ ebuf[pos++];
  452. i++;
  453. }
  454. ctx->encr_pos = pos;
  455. *out_len = i;
  456. return 0;
  457. }
  458. #endif /* ARCHIVE_CRYPTOR_STUB */
  459. const struct archive_cryptor __archive_cryptor =
  460. {
  461. &pbkdf2_sha1,
  462. &aes_ctr_init,
  463. &aes_ctr_update,
  464. &aes_ctr_release,
  465. &aes_ctr_init,
  466. &aes_ctr_update,
  467. &aes_ctr_release,
  468. };