archive_cryptor.c 11 KB

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