archive_cryptor.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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_LIBNETTLE) && defined(HAVE_NETTLE_PBKDF2_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. pbkdf2_hmac_sha1((unsigned)pw_len, (const uint8_t *)pw, rounds,
  82. salt_len, salt, derived_key_len, derived_key);
  83. return 0;
  84. }
  85. #elif defined(HAVE_LIBCRYPTO) && defined(HAVE_PKCS5_PBKDF2_HMAC_SHA1)
  86. static int
  87. pbkdf2_sha1(const char *pw, size_t pw_len, const uint8_t *salt,
  88. size_t salt_len, unsigned rounds, uint8_t *derived_key,
  89. size_t derived_key_len) {
  90. PKCS5_PBKDF2_HMAC_SHA1(pw, pw_len, salt, salt_len, rounds,
  91. derived_key_len, derived_key);
  92. return 0;
  93. }
  94. #else
  95. /* Stub */
  96. static int
  97. pbkdf2_sha1(const char *pw, size_t pw_len, const uint8_t *salt,
  98. size_t salt_len, unsigned rounds, uint8_t *derived_key,
  99. size_t derived_key_len) {
  100. (void)pw; /* UNUSED */
  101. (void)pw_len; /* UNUSED */
  102. (void)salt; /* UNUSED */
  103. (void)salt_len; /* UNUSED */
  104. (void)rounds; /* UNUSED */
  105. (void)derived_key; /* UNUSED */
  106. (void)derived_key_len; /* UNUSED */
  107. return -1; /* UNSUPPORTED */
  108. }
  109. #endif
  110. #ifdef ARCHIVE_CRYPTOR_USE_Apple_CommonCrypto
  111. # if MAC_OS_X_VERSION_MAX_ALLOWED < 1090
  112. # define kCCAlgorithmAES kCCAlgorithmAES128
  113. # endif
  114. static int
  115. aes_ctr_init(archive_crypto_ctx *ctx, const uint8_t *key, size_t key_len)
  116. {
  117. CCCryptorStatus r;
  118. ctx->key_len = key_len;
  119. memcpy(ctx->key, key, key_len);
  120. memset(ctx->nonce, 0, sizeof(ctx->nonce));
  121. ctx->encr_pos = AES_BLOCK_SIZE;
  122. r = CCCryptorCreateWithMode(kCCEncrypt, kCCModeECB, kCCAlgorithmAES,
  123. ccNoPadding, NULL, key, key_len, NULL, 0, 0, 0, &ctx->ctx);
  124. return (r == kCCSuccess)? 0: -1;
  125. }
  126. static int
  127. aes_ctr_encrypt_counter(archive_crypto_ctx *ctx)
  128. {
  129. CCCryptorRef ref = ctx->ctx;
  130. CCCryptorStatus r;
  131. r = CCCryptorReset(ref, NULL);
  132. if (r != kCCSuccess)
  133. return -1;
  134. r = CCCryptorUpdate(ref, ctx->nonce, AES_BLOCK_SIZE, ctx->encr_buf,
  135. AES_BLOCK_SIZE, NULL);
  136. return (r == kCCSuccess)? 0: -1;
  137. }
  138. static int
  139. aes_ctr_release(archive_crypto_ctx *ctx)
  140. {
  141. memset(ctx->key, 0, ctx->key_len);
  142. memset(ctx->nonce, 0, sizeof(ctx->nonce));
  143. return 0;
  144. }
  145. #elif defined(_WIN32) && !defined(__CYGWIN__) && defined(HAVE_BCRYPT_H)
  146. static int
  147. aes_ctr_init(archive_crypto_ctx *ctx, const uint8_t *key, size_t key_len)
  148. {
  149. BCRYPT_ALG_HANDLE hAlg;
  150. BCRYPT_KEY_HANDLE hKey;
  151. DWORD keyObj_len, aes_key_len;
  152. PBYTE keyObj;
  153. ULONG result;
  154. NTSTATUS status;
  155. BCRYPT_KEY_LENGTHS_STRUCT key_lengths;
  156. ctx->hAlg = NULL;
  157. ctx->hKey = NULL;
  158. ctx->keyObj = NULL;
  159. switch (key_len) {
  160. case 16: aes_key_len = 128; break;
  161. case 24: aes_key_len = 192; break;
  162. case 32: aes_key_len = 256; break;
  163. default: return -1;
  164. }
  165. status = BCryptOpenAlgorithmProvider(&hAlg, BCRYPT_AES_ALGORITHM,
  166. MS_PRIMITIVE_PROVIDER, 0);
  167. if (!BCRYPT_SUCCESS(status))
  168. return -1;
  169. status = BCryptGetProperty(hAlg, BCRYPT_KEY_LENGTHS, (PUCHAR)&key_lengths,
  170. sizeof(key_lengths), &result, 0);
  171. if (!BCRYPT_SUCCESS(status)) {
  172. BCryptCloseAlgorithmProvider(hAlg, 0);
  173. return -1;
  174. }
  175. if (key_lengths.dwMinLength > aes_key_len
  176. || key_lengths.dwMaxLength < aes_key_len) {
  177. BCryptCloseAlgorithmProvider(hAlg, 0);
  178. return -1;
  179. }
  180. status = BCryptGetProperty(hAlg, BCRYPT_OBJECT_LENGTH, (PUCHAR)&keyObj_len,
  181. sizeof(keyObj_len), &result, 0);
  182. if (!BCRYPT_SUCCESS(status)) {
  183. BCryptCloseAlgorithmProvider(hAlg, 0);
  184. return -1;
  185. }
  186. keyObj = (PBYTE)HeapAlloc(GetProcessHeap(), 0, keyObj_len);
  187. if (keyObj == NULL) {
  188. BCryptCloseAlgorithmProvider(hAlg, 0);
  189. return -1;
  190. }
  191. status = BCryptSetProperty(hAlg, BCRYPT_CHAINING_MODE,
  192. (PUCHAR)BCRYPT_CHAIN_MODE_ECB, sizeof(BCRYPT_CHAIN_MODE_ECB), 0);
  193. if (!BCRYPT_SUCCESS(status)) {
  194. BCryptCloseAlgorithmProvider(hAlg, 0);
  195. HeapFree(GetProcessHeap(), 0, keyObj);
  196. return -1;
  197. }
  198. status = BCryptGenerateSymmetricKey(hAlg, &hKey,
  199. keyObj, keyObj_len,
  200. (PUCHAR)(uintptr_t)key, (ULONG)key_len, 0);
  201. if (!BCRYPT_SUCCESS(status)) {
  202. BCryptCloseAlgorithmProvider(hAlg, 0);
  203. HeapFree(GetProcessHeap(), 0, keyObj);
  204. return -1;
  205. }
  206. ctx->hAlg = hAlg;
  207. ctx->hKey = hKey;
  208. ctx->keyObj = keyObj;
  209. ctx->keyObj_len = keyObj_len;
  210. ctx->encr_pos = AES_BLOCK_SIZE;
  211. return 0;
  212. }
  213. static int
  214. aes_ctr_encrypt_counter(archive_crypto_ctx *ctx)
  215. {
  216. NTSTATUS status;
  217. ULONG result;
  218. status = BCryptEncrypt(ctx->hKey, (PUCHAR)ctx->nonce, AES_BLOCK_SIZE,
  219. NULL, NULL, 0, (PUCHAR)ctx->encr_buf, AES_BLOCK_SIZE,
  220. &result, 0);
  221. return BCRYPT_SUCCESS(status) ? 0 : -1;
  222. }
  223. static int
  224. aes_ctr_release(archive_crypto_ctx *ctx)
  225. {
  226. if (ctx->hAlg != NULL) {
  227. BCryptCloseAlgorithmProvider(ctx->hAlg, 0);
  228. ctx->hAlg = NULL;
  229. BCryptDestroyKey(ctx->hKey);
  230. ctx->hKey = NULL;
  231. HeapFree(GetProcessHeap(), 0, ctx->keyObj);
  232. ctx->keyObj = NULL;
  233. }
  234. memset(ctx, 0, sizeof(*ctx));
  235. return 0;
  236. }
  237. #elif defined(HAVE_LIBNETTLE) && defined(HAVE_NETTLE_AES_H)
  238. static int
  239. aes_ctr_init(archive_crypto_ctx *ctx, const uint8_t *key, size_t key_len)
  240. {
  241. ctx->key_len = key_len;
  242. memcpy(ctx->key, key, key_len);
  243. memset(ctx->nonce, 0, sizeof(ctx->nonce));
  244. ctx->encr_pos = AES_BLOCK_SIZE;
  245. memset(&ctx->ctx, 0, sizeof(ctx->ctx));
  246. return 0;
  247. }
  248. static int
  249. aes_ctr_encrypt_counter(archive_crypto_ctx *ctx)
  250. {
  251. aes_set_encrypt_key(&ctx->ctx, ctx->key_len, ctx->key);
  252. aes_encrypt(&ctx->ctx, AES_BLOCK_SIZE, ctx->encr_buf, ctx->nonce);
  253. return 0;
  254. }
  255. static int
  256. aes_ctr_release(archive_crypto_ctx *ctx)
  257. {
  258. memset(ctx, 0, sizeof(*ctx));
  259. return 0;
  260. }
  261. #elif defined(HAVE_LIBCRYPTO)
  262. static int
  263. aes_ctr_init(archive_crypto_ctx *ctx, const uint8_t *key, size_t key_len)
  264. {
  265. if ((ctx->ctx = EVP_CIPHER_CTX_new()) == NULL)
  266. return -1;
  267. switch (key_len) {
  268. case 16: ctx->type = EVP_aes_128_ecb(); break;
  269. case 24: ctx->type = EVP_aes_192_ecb(); break;
  270. case 32: ctx->type = EVP_aes_256_ecb(); break;
  271. default: ctx->type = NULL; return -1;
  272. }
  273. ctx->key_len = key_len;
  274. memcpy(ctx->key, key, key_len);
  275. memset(ctx->nonce, 0, sizeof(ctx->nonce));
  276. ctx->encr_pos = AES_BLOCK_SIZE;
  277. EVP_CIPHER_CTX_init(ctx->ctx);
  278. return 0;
  279. }
  280. static int
  281. aes_ctr_encrypt_counter(archive_crypto_ctx *ctx)
  282. {
  283. int outl = 0;
  284. int r;
  285. r = EVP_EncryptInit_ex(ctx->ctx, ctx->type, NULL, ctx->key, NULL);
  286. if (r == 0)
  287. return -1;
  288. r = EVP_EncryptUpdate(ctx->ctx, ctx->encr_buf, &outl, ctx->nonce,
  289. AES_BLOCK_SIZE);
  290. if (r == 0 || outl != AES_BLOCK_SIZE)
  291. return -1;
  292. return 0;
  293. }
  294. static int
  295. aes_ctr_release(archive_crypto_ctx *ctx)
  296. {
  297. EVP_CIPHER_CTX_free(ctx->ctx);
  298. memset(ctx->key, 0, ctx->key_len);
  299. memset(ctx->nonce, 0, sizeof(ctx->nonce));
  300. return 0;
  301. }
  302. #else
  303. #define ARCHIVE_CRYPTOR_STUB
  304. /* Stub */
  305. static int
  306. aes_ctr_init(archive_crypto_ctx *ctx, const uint8_t *key, size_t key_len)
  307. {
  308. (void)ctx; /* UNUSED */
  309. (void)key; /* UNUSED */
  310. (void)key_len; /* UNUSED */
  311. return -1;
  312. }
  313. static int
  314. aes_ctr_encrypt_counter(archive_crypto_ctx *ctx)
  315. {
  316. (void)ctx; /* UNUSED */
  317. return -1;
  318. }
  319. static int
  320. aes_ctr_release(archive_crypto_ctx *ctx)
  321. {
  322. (void)ctx; /* UNUSED */
  323. return 0;
  324. }
  325. #endif
  326. #ifdef ARCHIVE_CRYPTOR_STUB
  327. static int
  328. aes_ctr_update(archive_crypto_ctx *ctx, const uint8_t * const in,
  329. size_t in_len, uint8_t * const out, size_t *out_len)
  330. {
  331. (void)ctx; /* UNUSED */
  332. (void)in; /* UNUSED */
  333. (void)in_len; /* UNUSED */
  334. (void)out; /* UNUSED */
  335. (void)out_len; /* UNUSED */
  336. aes_ctr_encrypt_counter(ctx); /* UNUSED */ /* Fix unused function warning */
  337. return -1;
  338. }
  339. #else
  340. static void
  341. aes_ctr_increase_counter(archive_crypto_ctx *ctx)
  342. {
  343. uint8_t *const nonce = ctx->nonce;
  344. int j;
  345. for (j = 0; j < 8; j++) {
  346. if (++nonce[j])
  347. break;
  348. }
  349. }
  350. static int
  351. aes_ctr_update(archive_crypto_ctx *ctx, const uint8_t * const in,
  352. size_t in_len, uint8_t * const out, size_t *out_len)
  353. {
  354. uint8_t *const ebuf = ctx->encr_buf;
  355. unsigned pos = ctx->encr_pos;
  356. unsigned max = (unsigned)((in_len < *out_len)? in_len: *out_len);
  357. unsigned i;
  358. for (i = 0; i < max; ) {
  359. if (pos == AES_BLOCK_SIZE) {
  360. aes_ctr_increase_counter(ctx);
  361. if (aes_ctr_encrypt_counter(ctx) != 0)
  362. return -1;
  363. while (max -i >= AES_BLOCK_SIZE) {
  364. for (pos = 0; pos < AES_BLOCK_SIZE; pos++)
  365. out[i+pos] = in[i+pos] ^ ebuf[pos];
  366. i += AES_BLOCK_SIZE;
  367. aes_ctr_increase_counter(ctx);
  368. if (aes_ctr_encrypt_counter(ctx) != 0)
  369. return -1;
  370. }
  371. pos = 0;
  372. if (i >= max)
  373. break;
  374. }
  375. out[i] = in[i] ^ ebuf[pos++];
  376. i++;
  377. }
  378. ctx->encr_pos = pos;
  379. *out_len = i;
  380. return 0;
  381. }
  382. #endif /* ARCHIVE_CRYPTOR_STUB */
  383. const struct archive_cryptor __archive_cryptor =
  384. {
  385. &pbkdf2_sha1,
  386. &aes_ctr_init,
  387. &aes_ctr_update,
  388. &aes_ctr_release,
  389. &aes_ctr_init,
  390. &aes_ctr_update,
  391. &aes_ctr_release,
  392. };