archive_cryptor.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. aes_set_encrypt_key(&ctx->ctx, ctx->key_len, ctx->key);
  306. aes_encrypt(&ctx->ctx, AES_BLOCK_SIZE, ctx->encr_buf, ctx->nonce);
  307. return 0;
  308. }
  309. static int
  310. aes_ctr_release(archive_crypto_ctx *ctx)
  311. {
  312. memset(ctx, 0, sizeof(*ctx));
  313. return 0;
  314. }
  315. #elif defined(HAVE_LIBCRYPTO)
  316. static int
  317. aes_ctr_init(archive_crypto_ctx *ctx, const uint8_t *key, size_t key_len)
  318. {
  319. if ((ctx->ctx = EVP_CIPHER_CTX_new()) == NULL)
  320. return -1;
  321. switch (key_len) {
  322. case 16: ctx->type = EVP_aes_128_ecb(); break;
  323. case 24: ctx->type = EVP_aes_192_ecb(); break;
  324. case 32: ctx->type = EVP_aes_256_ecb(); break;
  325. default: ctx->type = NULL; return -1;
  326. }
  327. ctx->key_len = key_len;
  328. memcpy(ctx->key, key, key_len);
  329. memset(ctx->nonce, 0, sizeof(ctx->nonce));
  330. ctx->encr_pos = AES_BLOCK_SIZE;
  331. #if OPENSSL_VERSION_NUMBER >= 0x10100000L && !defined(LIBRESSL_VERSION_NUMBER)
  332. if (!EVP_CIPHER_CTX_reset(ctx->ctx)) {
  333. EVP_CIPHER_CTX_free(ctx->ctx);
  334. ctx->ctx = NULL;
  335. }
  336. #else
  337. EVP_CIPHER_CTX_init(ctx->ctx);
  338. #endif
  339. return 0;
  340. }
  341. static int
  342. aes_ctr_encrypt_counter(archive_crypto_ctx *ctx)
  343. {
  344. int outl = 0;
  345. int r;
  346. r = EVP_EncryptInit_ex(ctx->ctx, ctx->type, NULL, ctx->key, NULL);
  347. if (r == 0)
  348. return -1;
  349. r = EVP_EncryptUpdate(ctx->ctx, ctx->encr_buf, &outl, ctx->nonce,
  350. AES_BLOCK_SIZE);
  351. if (r == 0 || outl != AES_BLOCK_SIZE)
  352. return -1;
  353. return 0;
  354. }
  355. static int
  356. aes_ctr_release(archive_crypto_ctx *ctx)
  357. {
  358. EVP_CIPHER_CTX_free(ctx->ctx);
  359. memset(ctx->key, 0, ctx->key_len);
  360. memset(ctx->nonce, 0, sizeof(ctx->nonce));
  361. return 0;
  362. }
  363. #else
  364. #define ARCHIVE_CRYPTOR_STUB
  365. /* Stub */
  366. static int
  367. aes_ctr_init(archive_crypto_ctx *ctx, const uint8_t *key, size_t key_len)
  368. {
  369. (void)ctx; /* UNUSED */
  370. (void)key; /* UNUSED */
  371. (void)key_len; /* UNUSED */
  372. return -1;
  373. }
  374. static int
  375. aes_ctr_encrypt_counter(archive_crypto_ctx *ctx)
  376. {
  377. (void)ctx; /* UNUSED */
  378. return -1;
  379. }
  380. static int
  381. aes_ctr_release(archive_crypto_ctx *ctx)
  382. {
  383. (void)ctx; /* UNUSED */
  384. return 0;
  385. }
  386. #endif
  387. #ifdef ARCHIVE_CRYPTOR_STUB
  388. static int
  389. aes_ctr_update(archive_crypto_ctx *ctx, const uint8_t * const in,
  390. size_t in_len, uint8_t * const out, size_t *out_len)
  391. {
  392. (void)ctx; /* UNUSED */
  393. (void)in; /* UNUSED */
  394. (void)in_len; /* UNUSED */
  395. (void)out; /* UNUSED */
  396. (void)out_len; /* UNUSED */
  397. aes_ctr_encrypt_counter(ctx); /* UNUSED */ /* Fix unused function warning */
  398. return -1;
  399. }
  400. #else
  401. static void
  402. aes_ctr_increase_counter(archive_crypto_ctx *ctx)
  403. {
  404. uint8_t *const nonce = ctx->nonce;
  405. int j;
  406. for (j = 0; j < 8; j++) {
  407. if (++nonce[j])
  408. break;
  409. }
  410. }
  411. static int
  412. aes_ctr_update(archive_crypto_ctx *ctx, const uint8_t * const in,
  413. size_t in_len, uint8_t * const out, size_t *out_len)
  414. {
  415. uint8_t *const ebuf = ctx->encr_buf;
  416. unsigned pos = ctx->encr_pos;
  417. unsigned max = (unsigned)((in_len < *out_len)? in_len: *out_len);
  418. unsigned i;
  419. for (i = 0; i < max; ) {
  420. if (pos == AES_BLOCK_SIZE) {
  421. aes_ctr_increase_counter(ctx);
  422. if (aes_ctr_encrypt_counter(ctx) != 0)
  423. return -1;
  424. while (max -i >= AES_BLOCK_SIZE) {
  425. for (pos = 0; pos < AES_BLOCK_SIZE; pos++)
  426. out[i+pos] = in[i+pos] ^ ebuf[pos];
  427. i += AES_BLOCK_SIZE;
  428. aes_ctr_increase_counter(ctx);
  429. if (aes_ctr_encrypt_counter(ctx) != 0)
  430. return -1;
  431. }
  432. pos = 0;
  433. if (i >= max)
  434. break;
  435. }
  436. out[i] = in[i] ^ ebuf[pos++];
  437. i++;
  438. }
  439. ctx->encr_pos = pos;
  440. *out_len = i;
  441. return 0;
  442. }
  443. #endif /* ARCHIVE_CRYPTOR_STUB */
  444. const struct archive_cryptor __archive_cryptor =
  445. {
  446. &pbkdf2_sha1,
  447. &aes_ctr_init,
  448. &aes_ctr_update,
  449. &aes_ctr_release,
  450. &aes_ctr_init,
  451. &aes_ctr_update,
  452. &aes_ctr_release,
  453. };