Re-format code with clang-format
diff --git a/lib/mkstemp.c b/lib/mkstemp.c
index 01a531f..f18f10b 100644
--- a/lib/mkstemp.c
+++ b/lib/mkstemp.c
@@ -148,6 +148,6 @@
}
}
}
- /*NOTREACHED*/
+ /*NOTREACHED*/
#endif
}
diff --git a/lib/zip_crypto_commoncrypto.c b/lib/zip_crypto_commoncrypto.c
index 79ce7f9..10f7700 100644
--- a/lib/zip_crypto_commoncrypto.c
+++ b/lib/zip_crypto_commoncrypto.c
@@ -41,10 +41,9 @@
#include <unistd.h>
void
-_zip_crypto_aes_free(_zip_crypto_aes_t *aes)
-{
+_zip_crypto_aes_free(_zip_crypto_aes_t *aes) {
if (aes == NULL) {
- return;
+ return;
}
CCCryptorRelease(aes);
@@ -52,8 +51,7 @@
bool
-_zip_crypto_aes_encrypt_block(_zip_crypto_aes_t *aes, const zip_uint8_t *in, zip_uint8_t *out)
-{
+_zip_crypto_aes_encrypt_block(_zip_crypto_aes_t *aes, const zip_uint8_t *in, zip_uint8_t *out) {
size_t len;
CCCryptorUpdate(aes, in, ZIP_CRYPTO_AES_BLOCK_LENGTH, out, ZIP_CRYPTO_AES_BLOCK_LENGTH, &len);
return true;
@@ -61,8 +59,7 @@
_zip_crypto_aes_t *
-_zip_crypto_aes_new(const zip_uint8_t *key, zip_uint16_t key_size, zip_error_t *error)
-{
+_zip_crypto_aes_new(const zip_uint8_t *key, zip_uint16_t key_size, zip_error_t *error) {
_zip_crypto_aes_t *aes;
CCCryptorStatus ret;
@@ -70,28 +67,27 @@
switch (ret) {
case kCCSuccess:
- return aes;
+ return aes;
case kCCMemoryFailure:
- zip_error_set(error, ZIP_ER_MEMORY, 0);
- return NULL;
+ zip_error_set(error, ZIP_ER_MEMORY, 0);
+ return NULL;
case kCCParamError:
- zip_error_set(error, ZIP_ER_INVAL, 0);
- return NULL;
+ zip_error_set(error, ZIP_ER_INVAL, 0);
+ return NULL;
default:
- zip_error_set(error, ZIP_ER_INTERNAL, 0);
- return NULL;
+ zip_error_set(error, ZIP_ER_INTERNAL, 0);
+ return NULL;
}
}
void
-_zip_crypto_hmac_free(_zip_crypto_hmac_t *hmac)
-{
+_zip_crypto_hmac_free(_zip_crypto_hmac_t *hmac) {
if (hmac == NULL) {
- return;
+ return;
}
_zip_crypto_clear(hmac, sizeof(*hmac));
@@ -100,13 +96,12 @@
_zip_crypto_hmac_t *
-_zip_crypto_hmac_new(const zip_uint8_t *secret, zip_uint64_t secret_length, zip_error_t *error)
-{
+_zip_crypto_hmac_new(const zip_uint8_t *secret, zip_uint64_t secret_length, zip_error_t *error) {
_zip_crypto_hmac_t *hmac;
if ((hmac = (_zip_crypto_hmac_t *)malloc(sizeof(*hmac))) == NULL) {
- zip_error_set(error, ZIP_ER_MEMORY, 0);
- return NULL;
+ zip_error_set(error, ZIP_ER_MEMORY, 0);
+ return NULL;
}
CCHmacInit(hmac, kCCHmacAlgSHA1, secret, secret_length);
@@ -120,12 +115,12 @@
int fd;
if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
- return false;
+ return false;
}
if (read(fd, buffer, length) != length) {
- close(fd);
- return false;
+ close(fd);
+ return false;
}
close(fd);
diff --git a/lib/zip_crypto_commoncrypto.h b/lib/zip_crypto_commoncrypto.h
index 778341e..1eae1b7 100644
--- a/lib/zip_crypto_commoncrypto.h
+++ b/lib/zip_crypto_commoncrypto.h
@@ -48,7 +48,6 @@
_zip_crypto_hmac_t *_zip_crypto_hmac_new(const zip_uint8_t *secret, zip_uint64_t secret_length, zip_error_t *error);
#define _zip_crypto_hmac_output(hmac, data) (CCHmacFinal((hmac), (data)), true)
-#define _zip_crypto_pbkdf2(key, key_length, salt, salt_length, iterations, output, output_length) \
- (CCKeyDerivationPBKDF(kCCPBKDF2, (const char *)(key), (key_length), (salt), (salt_length), kCCPRFHmacAlgSHA1, (iterations), (output), (output_length)) == kCCSuccess)
+#define _zip_crypto_pbkdf2(key, key_length, salt, salt_length, iterations, output, output_length) (CCKeyDerivationPBKDF(kCCPBKDF2, (const char *)(key), (key_length), (salt), (salt_length), kCCPRFHmacAlgSHA1, (iterations), (output), (output_length)) == kCCSuccess)
#endif /* HAD_ZIP_CRYPTO_COMMONCRYPTO_H */
diff --git a/lib/zip_crypto_gnutls.c b/lib/zip_crypto_gnutls.c
index f688eae..d08f2f9 100644
--- a/lib/zip_crypto_gnutls.c
+++ b/lib/zip_crypto_gnutls.c
@@ -33,63 +33,60 @@
#include <stdlib.h>
-#include "zipint.h"
#include "zip_crypto.h"
+#include "zipint.h"
_zip_crypto_aes_t *
-_zip_crypto_aes_new(const zip_uint8_t *key, zip_uint16_t key_size, zip_error_t *error)
-{
+_zip_crypto_aes_new(const zip_uint8_t *key, zip_uint16_t key_size, zip_error_t *error) {
_zip_crypto_aes_t *aes;
- if ((aes = (_zip_crypto_aes_t *)malloc(sizeof(*aes))) == NULL) {
- zip_error_set(error, ZIP_ER_MEMORY, 0);
- return NULL;
+ if ((aes = (_zip_crypto_aes_t *)malloc(sizeof(*aes))) == NULL) {
+ zip_error_set(error, ZIP_ER_MEMORY, 0);
+ return NULL;
}
aes->key_size = key_size;
switch (aes->key_size) {
case 128:
- nettle_aes128_set_encrypt_key(&aes->ctx.ctx_128, key);
- break;
+ nettle_aes128_set_encrypt_key(&aes->ctx.ctx_128, key);
+ break;
case 192:
- nettle_aes192_set_encrypt_key(&aes->ctx.ctx_192, key);
- break;
+ nettle_aes192_set_encrypt_key(&aes->ctx.ctx_192, key);
+ break;
case 256:
- nettle_aes256_set_encrypt_key(&aes->ctx.ctx_256, key);
- break;
+ nettle_aes256_set_encrypt_key(&aes->ctx.ctx_256, key);
+ break;
default:
- zip_error_set(error, ZIP_ER_INVAL, 0);
- free(aes);
- return NULL;
+ zip_error_set(error, ZIP_ER_INVAL, 0);
+ free(aes);
+ return NULL;
}
return aes;
}
bool
-_zip_crypto_aes_encrypt_block(_zip_crypto_aes_t *aes, const zip_uint8_t *in, zip_uint8_t *out)
-{
+_zip_crypto_aes_encrypt_block(_zip_crypto_aes_t *aes, const zip_uint8_t *in, zip_uint8_t *out) {
switch (aes->key_size) {
case 128:
- nettle_aes128_encrypt(&aes->ctx.ctx_128, ZIP_CRYPTO_AES_BLOCK_LENGTH, out, in);
- break;
+ nettle_aes128_encrypt(&aes->ctx.ctx_128, ZIP_CRYPTO_AES_BLOCK_LENGTH, out, in);
+ break;
case 192:
- nettle_aes192_encrypt(&aes->ctx.ctx_192, ZIP_CRYPTO_AES_BLOCK_LENGTH, out, in);
- break;
+ nettle_aes192_encrypt(&aes->ctx.ctx_192, ZIP_CRYPTO_AES_BLOCK_LENGTH, out, in);
+ break;
case 256:
- nettle_aes256_encrypt(&aes->ctx.ctx_256, ZIP_CRYPTO_AES_BLOCK_LENGTH, out, in);
- break;
+ nettle_aes256_encrypt(&aes->ctx.ctx_256, ZIP_CRYPTO_AES_BLOCK_LENGTH, out, in);
+ break;
}
return true;
}
void
-_zip_crypto_aes_free(_zip_crypto_aes_t *aes)
-{
+_zip_crypto_aes_free(_zip_crypto_aes_t *aes) {
if (aes == NULL) {
- return;
+ return;
}
_zip_crypto_clear(aes, sizeof(*aes));
@@ -98,20 +95,19 @@
_zip_crypto_hmac_t *
-_zip_crypto_hmac_new(const zip_uint8_t *secret, zip_uint64_t secret_length, zip_error_t *error)
-{
+_zip_crypto_hmac_new(const zip_uint8_t *secret, zip_uint64_t secret_length, zip_error_t *error) {
_zip_crypto_hmac_t *hmac;
int ret;
- if ((hmac = (_zip_crypto_hmac_t *)malloc(sizeof(*hmac))) == NULL) {
- zip_error_set(error, ZIP_ER_MEMORY, 0);
- return NULL;
+ if ((hmac = (_zip_crypto_hmac_t *)malloc(sizeof(*hmac))) == NULL) {
+ zip_error_set(error, ZIP_ER_MEMORY, 0);
+ return NULL;
}
if ((ret = gnutls_hmac_init(hmac, GNUTLS_MAC_SHA1, secret, secret_length)) < 0) {
- // TODO: set error
- free(hmac);
- return NULL;
+ // TODO: set error
+ free(hmac);
+ return NULL;
}
return hmac;
@@ -119,12 +115,11 @@
void
-_zip_crypto_hmac_free(_zip_crypto_hmac_t *hmac)
-{
+_zip_crypto_hmac_free(_zip_crypto_hmac_t *hmac) {
zip_uint8_t buf[ZIP_CRYPTO_SHA1_LENGTH];
if (hmac == NULL) {
- return;
+ return;
}
gnutls_hmac_deinit(*hmac, buf);
@@ -137,4 +132,3 @@
zip_random(zip_uint8_t *buffer, zip_uint16_t length) {
return gnutls_rnd(GNUTLS_RND_KEY, buffer, length) == 0;
}
-
diff --git a/lib/zip_crypto_gnutls.h b/lib/zip_crypto_gnutls.h
index c3d6a0e..40d34a5 100644
--- a/lib/zip_crypto_gnutls.h
+++ b/lib/zip_crypto_gnutls.h
@@ -37,17 +37,17 @@
#include <nettle/aes.h>
#include <nettle/pbkdf2.h>
-#include <gnutls/gnutls.h>
#include <gnutls/crypto.h>
+#include <gnutls/gnutls.h>
typedef struct {
union {
- struct aes128_ctx ctx_128;
- struct aes192_ctx ctx_192;
- struct aes256_ctx ctx_256;
+ struct aes128_ctx ctx_128;
+ struct aes192_ctx ctx_192;
+ struct aes256_ctx ctx_256;
} ctx;
zip_uint16_t key_size;
-} _zip_crypto_aes_t;
+} _zip_crypto_aes_t;
#define _zip_crypto_hmac_t gnutls_hmac_hd_t
@@ -55,12 +55,11 @@
bool _zip_crypto_aes_encrypt_block(_zip_crypto_aes_t *aes, const zip_uint8_t *in, zip_uint8_t *out);
_zip_crypto_aes_t *_zip_crypto_aes_new(const zip_uint8_t *key, zip_uint16_t key_size, zip_error_t *error);
-#define _zip_crypto_hmac(hmac, data, length) (gnutls_hmac(*(hmac), (data), (length)) == 0)
+#define _zip_crypto_hmac(hmac, data, length) (gnutls_hmac(*(hmac), (data), (length)) == 0)
void _zip_crypto_hmac_free(_zip_crypto_hmac_t *hmac);
_zip_crypto_hmac_t *_zip_crypto_hmac_new(const zip_uint8_t *secret, zip_uint64_t secret_length, zip_error_t *error);
#define _zip_crypto_hmac_output(hmac, data) (gnutls_hmac_output(*(hmac), (data)), true)
-#define _zip_crypto_pbkdf2(key, key_length, salt, salt_length, iterations, output, output_length) \
- (pbkdf2_hmac_sha1((key_length), (key), (iterations), (salt_length), (salt), (output_length), (output)), true)
+#define _zip_crypto_pbkdf2(key, key_length, salt, salt_length, iterations, output, output_length) (pbkdf2_hmac_sha1((key_length), (key), (iterations), (salt_length), (salt), (output_length), (output)), true)
#endif /* HAD_ZIP_CRYPTO_GNUTLS_H */
diff --git a/lib/zip_crypto_openssl.c b/lib/zip_crypto_openssl.c
index db32d55..1d256cd 100644
--- a/lib/zip_crypto_openssl.c
+++ b/lib/zip_crypto_openssl.c
@@ -33,8 +33,8 @@
#include <stdlib.h>
-#include "zipint.h"
#include "zip_crypto.h"
+#include "zipint.h"
#include <openssl/rand.h>
@@ -44,13 +44,12 @@
_zip_crypto_aes_t *
-_zip_crypto_aes_new(const zip_uint8_t *key, zip_uint16_t key_size, zip_error_t *error)
-{
+_zip_crypto_aes_new(const zip_uint8_t *key, zip_uint16_t key_size, zip_error_t *error) {
_zip_crypto_aes_t *aes;
- if ((aes = (_zip_crypto_aes_t *)malloc(sizeof(*aes))) == NULL) {
- zip_error_set(error, ZIP_ER_MEMORY, 0);
- return NULL;
+ if ((aes = (_zip_crypto_aes_t *)malloc(sizeof(*aes))) == NULL) {
+ zip_error_set(error, ZIP_ER_MEMORY, 0);
+ return NULL;
}
AES_set_encrypt_key(key, key_size, aes);
@@ -59,10 +58,9 @@
}
void
-_zip_crypto_aes_free(_zip_crypto_aes_t *aes)
-{
+_zip_crypto_aes_free(_zip_crypto_aes_t *aes) {
if (aes == NULL) {
- return;
+ return;
}
_zip_crypto_clear(aes, sizeof(*aes));
@@ -71,37 +69,36 @@
_zip_crypto_hmac_t *
-_zip_crypto_hmac_new(const zip_uint8_t *secret, zip_uint64_t secret_length, zip_error_t *error)
-{
+_zip_crypto_hmac_new(const zip_uint8_t *secret, zip_uint64_t secret_length, zip_error_t *error) {
_zip_crypto_hmac_t *hmac;
if (secret_length > INT_MAX) {
- zip_error_set(error, ZIP_ER_INVAL, 0);
- return NULL;
+ zip_error_set(error, ZIP_ER_INVAL, 0);
+ return NULL;
}
#ifdef USE_OPENSSL_1_0_API
- if ((hmac = (_zip_crypto_hmac_t *)malloc(sizeof(*hmac))) == NULL) {
- zip_error_set(error, ZIP_ER_MEMORY, 0);
- return NULL;
+ if ((hmac = (_zip_crypto_hmac_t *)malloc(sizeof(*hmac))) == NULL) {
+ zip_error_set(error, ZIP_ER_MEMORY, 0);
+ return NULL;
}
HMAC_CTX_init(hmac);
#else
if ((hmac = HMAC_CTX_new()) == NULL) {
- zip_error_set(error, ZIP_ER_MEMORY, 0);
- return NULL;
+ zip_error_set(error, ZIP_ER_MEMORY, 0);
+ return NULL;
}
#endif
if (HMAC_Init_ex(hmac, secret, (int)secret_length, EVP_sha1(), NULL) != 1) {
- zip_error_set(error, ZIP_ER_INTERNAL, 0);
+ zip_error_set(error, ZIP_ER_INTERNAL, 0);
#ifdef USE_OPENSSL_1_0_API
- free(hmac);
+ free(hmac);
#else
- HMAC_CTX_free(hmac);
+ HMAC_CTX_free(hmac);
#endif
- return NULL;
+ return NULL;
}
return hmac;
@@ -109,10 +106,9 @@
void
-_zip_crypto_hmac_free(_zip_crypto_hmac_t *hmac)
-{
+_zip_crypto_hmac_free(_zip_crypto_hmac_t *hmac) {
if (hmac == NULL) {
- return;
+ return;
}
#ifdef USE_OPENSSL_1_0_API
@@ -126,8 +122,7 @@
bool
-_zip_crypto_hmac_output(_zip_crypto_hmac_t *hmac, zip_uint8_t *data)
-{
+_zip_crypto_hmac_output(_zip_crypto_hmac_t *hmac, zip_uint8_t *data) {
unsigned int length;
return HMAC_Final(hmac, data, &length) == 1;
@@ -138,5 +133,3 @@
zip_random(zip_uint8_t *buffer, zip_uint16_t length) {
return RAND_bytes(buffer, length) == 1;
}
-
-
diff --git a/lib/zip_crypto_openssl.h b/lib/zip_crypto_openssl.h
index 937635c..906890e 100644
--- a/lib/zip_crypto_openssl.h
+++ b/lib/zip_crypto_openssl.h
@@ -41,7 +41,7 @@
#define _zip_crypto_hmac_t HMAC_CTX
void _zip_crypto_aes_free(_zip_crypto_aes_t *aes);
-#define _zip_crypto_aes_encrypt_block(aes, in, out) (AES_encrypt((in), (out), (aes)), true)
+#define _zip_crypto_aes_encrypt_block(aes, in, out) (AES_encrypt((in), (out), (aes)), true)
_zip_crypto_aes_t *_zip_crypto_aes_new(const zip_uint8_t *key, zip_uint16_t key_size, zip_error_t *error);
#define _zip_crypto_hmac(hmac, data, length) (HMAC_Update((hmac), (data), (length)) == 1)
@@ -49,7 +49,6 @@
_zip_crypto_hmac_t *_zip_crypto_hmac_new(const zip_uint8_t *secret, zip_uint64_t secret_length, zip_error_t *error);
bool _zip_crypto_hmac_output(_zip_crypto_hmac_t *hmac, zip_uint8_t *data);
-#define _zip_crypto_pbkdf2(key, key_length, salt, salt_length, iterations, output, output_length) \
- (PKCS5_PBKDF2_HMAC_SHA1((const char *)(key), (key_length), (salt), (salt_length), (iterations), (output_length), (output)))
+#define _zip_crypto_pbkdf2(key, key_length, salt, salt_length, iterations, output, output_length) (PKCS5_PBKDF2_HMAC_SHA1((const char *)(key), (key_length), (salt), (salt_length), (iterations), (output_length), (output)))
#endif /* HAD_ZIP_CRYPTO_OPENSSL_H */
diff --git a/lib/zip_crypto_win.c b/lib/zip_crypto_win.c
index 7e50ea4..4d428e2 100644
--- a/lib/zip_crypto_win.c
+++ b/lib/zip_crypto_win.c
@@ -32,8 +32,8 @@
*/
#include <stdlib.h>
-#include "zipint.h"
#include "zip_crypto.h"
+#include "zipint.h"
#define WIN32_LEAN_AND_MEAN
#define NOCRYPT
@@ -84,20 +84,19 @@
#ifdef HAS_BCRYPTDERIVEKEYPBKDF2
-bool _zip_crypto_pbkdf2(const zip_uint8_t *key, zip_uint64_t key_length, const zip_uint8_t *salt, zip_uint16_t salt_length, zip_uint16_t iterations, zip_uint8_t* output, zip_uint16_t output_length)
-{
- BCRYPT_ALG_HANDLE hAlgorithm = NULL;
+bool
+_zip_crypto_pbkdf2(const zip_uint8_t *key, zip_uint64_t key_length, const zip_uint8_t *salt, zip_uint16_t salt_length, zip_uint16_t iterations, zip_uint8_t *output, zip_uint16_t output_length) {
+ BCRYPT_ALG_HANDLE hAlgorithm = NULL;
- if (!BCRYPT_SUCCESS(BCryptOpenAlgorithmProvider(&hAlgorithm, BCRYPT_SHA1_ALGORITHM, NULL, BCRYPT_ALG_HANDLE_HMAC_FLAG)))
- {
- return false;
- }
+ if (!BCRYPT_SUCCESS(BCryptOpenAlgorithmProvider(&hAlgorithm, BCRYPT_SHA1_ALGORITHM, NULL, BCRYPT_ALG_HANDLE_HMAC_FLAG))) {
+ return false;
+ }
- bool result = BCRYPT_SUCCESS(BCryptDeriveKeyPBKDF2(hAlgorithm, (PUCHAR)key, (ULONG)key_length, (PUCHAR)salt, salt_length, iterations, output, output_length, 0));
+ bool result = BCRYPT_SUCCESS(BCryptDeriveKeyPBKDF2(hAlgorithm, (PUCHAR)key, (ULONG)key_length, (PUCHAR)salt, salt_length, iterations, output, output_length, 0));
- BCryptCloseAlgorithmProvider(hAlgorithm, 0);
+ BCryptCloseAlgorithmProvider(hAlgorithm, 0);
- return result;
+ return result;
}
#else
@@ -107,421 +106,359 @@
#define DIGEST_SIZE 20
#define BLOCK_SIZE 64
-typedef struct
-{
- BCRYPT_ALG_HANDLE hAlgorithm;
- BCRYPT_HASH_HANDLE hInnerHash;
- BCRYPT_HASH_HANDLE hOuterHash;
- ULONG cbHashObject;
- PUCHAR pbInnerHash;
- PUCHAR pbOuterHash;
+typedef struct {
+ BCRYPT_ALG_HANDLE hAlgorithm;
+ BCRYPT_HASH_HANDLE hInnerHash;
+ BCRYPT_HASH_HANDLE hOuterHash;
+ ULONG cbHashObject;
+ PUCHAR pbInnerHash;
+ PUCHAR pbOuterHash;
} PRF_CTX;
-static void hmacFree(PRF_CTX* pContext)
-{
- if (pContext->hOuterHash) BCryptDestroyHash(pContext->hOuterHash);
- if (pContext->hInnerHash) BCryptDestroyHash(pContext->hInnerHash);
- free(pContext->pbOuterHash);
- free(pContext->pbInnerHash);
- if (pContext->hAlgorithm) BCryptCloseAlgorithmProvider(pContext->hAlgorithm, 0);
+static void
+hmacFree(PRF_CTX *pContext) {
+ if (pContext->hOuterHash)
+ BCryptDestroyHash(pContext->hOuterHash);
+ if (pContext->hInnerHash)
+ BCryptDestroyHash(pContext->hInnerHash);
+ free(pContext->pbOuterHash);
+ free(pContext->pbInnerHash);
+ if (pContext->hAlgorithm)
+ BCryptCloseAlgorithmProvider(pContext->hAlgorithm, 0);
}
-static BOOL hmacPrecomputeDigest(BCRYPT_HASH_HANDLE hHash, PUCHAR pbPassword, DWORD cbPassword, BYTE mask)
-{
- BYTE buffer[BLOCK_SIZE];
- DWORD i;
+static BOOL
+hmacPrecomputeDigest(BCRYPT_HASH_HANDLE hHash, PUCHAR pbPassword, DWORD cbPassword, BYTE mask) {
+ BYTE buffer[BLOCK_SIZE];
+ DWORD i;
- if (cbPassword > BLOCK_SIZE)
- {
- return FALSE;
- }
+ if (cbPassword > BLOCK_SIZE) {
+ return FALSE;
+ }
- memset(buffer, mask, sizeof(buffer));
+ memset(buffer, mask, sizeof(buffer));
- for (i = 0; i < cbPassword; ++i)
- {
- buffer[i] = (char)(pbPassword[i] ^ mask);
- }
+ for (i = 0; i < cbPassword; ++i) {
+ buffer[i] = (char)(pbPassword[i] ^ mask);
+ }
- return BCRYPT_SUCCESS(BCryptHashData(hHash, buffer, sizeof(buffer), 0));
+ return BCRYPT_SUCCESS(BCryptHashData(hHash, buffer, sizeof(buffer), 0));
}
-static BOOL hmacInit(PRF_CTX* pContext, PUCHAR pbPassword, DWORD cbPassword)
-{
- BOOL bStatus = FALSE;
- ULONG cbResult;
- BYTE key[DIGEST_SIZE];
+static BOOL
+hmacInit(PRF_CTX *pContext, PUCHAR pbPassword, DWORD cbPassword) {
+ BOOL bStatus = FALSE;
+ ULONG cbResult;
+ BYTE key[DIGEST_SIZE];
- if (!BCRYPT_SUCCESS(BCryptOpenAlgorithmProvider(&pContext->hAlgorithm, BCRYPT_SHA1_ALGORITHM, NULL, 0)) ||
- !BCRYPT_SUCCESS(BCryptGetProperty(pContext->hAlgorithm, BCRYPT_OBJECT_LENGTH, (PUCHAR)&pContext->cbHashObject, sizeof(pContext->cbHashObject), &cbResult, 0)) ||
- ((pContext->pbInnerHash = malloc(pContext->cbHashObject)) == NULL) ||
- ((pContext->pbOuterHash = malloc(pContext->cbHashObject)) == NULL) ||
- !BCRYPT_SUCCESS(BCryptCreateHash(pContext->hAlgorithm, &pContext->hInnerHash, pContext->pbInnerHash, pContext->cbHashObject, NULL, 0, 0)) ||
- !BCRYPT_SUCCESS(BCryptCreateHash(pContext->hAlgorithm, &pContext->hOuterHash, pContext->pbOuterHash, pContext->cbHashObject, NULL, 0, 0)))
- {
- goto hmacInit_end;
+ if (!BCRYPT_SUCCESS(BCryptOpenAlgorithmProvider(&pContext->hAlgorithm, BCRYPT_SHA1_ALGORITHM, NULL, 0)) || !BCRYPT_SUCCESS(BCryptGetProperty(pContext->hAlgorithm, BCRYPT_OBJECT_LENGTH, (PUCHAR)&pContext->cbHashObject, sizeof(pContext->cbHashObject), &cbResult, 0)) || ((pContext->pbInnerHash = malloc(pContext->cbHashObject)) == NULL) || ((pContext->pbOuterHash = malloc(pContext->cbHashObject)) == NULL) || !BCRYPT_SUCCESS(BCryptCreateHash(pContext->hAlgorithm, &pContext->hInnerHash, pContext->pbInnerHash, pContext->cbHashObject, NULL, 0, 0)) || !BCRYPT_SUCCESS(BCryptCreateHash(pContext->hAlgorithm, &pContext->hOuterHash, pContext->pbOuterHash, pContext->cbHashObject, NULL, 0, 0))) {
+ goto hmacInit_end;
+ }
+
+ if (cbPassword > BLOCK_SIZE) {
+ BCRYPT_HASH_HANDLE hHash = NULL;
+ PUCHAR pbHashObject = malloc(pContext->cbHashObject);
+
+ bStatus = BCRYPT_SUCCESS(BCryptCreateHash(pContext->hAlgorithm, &hHash, pbHashObject, pContext->cbHashObject, NULL, 0, 0)) && BCRYPT_SUCCESS(BCryptHashData(hHash, pbPassword, cbPassword, 0)) && BCRYPT_SUCCESS(BCryptGetProperty(hHash, BCRYPT_HASH_LENGTH, (PUCHAR)&cbPassword, sizeof(cbPassword), &cbResult, 0)) && BCRYPT_SUCCESS(BCryptFinishHash(hHash, key, cbPassword, 0));
+
+ if (hHash)
+ BCryptDestroyHash(hHash);
+ free(pbHashObject);
+
+ if (!bStatus) {
+ goto hmacInit_end;
}
- if (cbPassword > BLOCK_SIZE)
- {
- BCRYPT_HASH_HANDLE hHash = NULL;
- PUCHAR pbHashObject = malloc(pContext->cbHashObject);
+ pbPassword = key;
+ }
- bStatus =
- BCRYPT_SUCCESS(BCryptCreateHash(pContext->hAlgorithm, &hHash, pbHashObject, pContext->cbHashObject, NULL, 0, 0)) &&
- BCRYPT_SUCCESS(BCryptHashData(hHash, pbPassword, cbPassword, 0)) &&
- BCRYPT_SUCCESS(BCryptGetProperty(hHash, BCRYPT_HASH_LENGTH, (PUCHAR)&cbPassword, sizeof(cbPassword), &cbResult, 0)) &&
- BCRYPT_SUCCESS(BCryptFinishHash(hHash, key, cbPassword, 0));
-
- if (hHash) BCryptDestroyHash(hHash);
- free(pbHashObject);
-
- if (!bStatus)
- {
- goto hmacInit_end;
- }
-
- pbPassword = key;
- }
-
- bStatus =
- hmacPrecomputeDigest(pContext->hInnerHash, pbPassword, cbPassword, 0x36) &&
- hmacPrecomputeDigest(pContext->hOuterHash, pbPassword, cbPassword, 0x5C);
+ bStatus = hmacPrecomputeDigest(pContext->hInnerHash, pbPassword, cbPassword, 0x36) && hmacPrecomputeDigest(pContext->hOuterHash, pbPassword, cbPassword, 0x5C);
hmacInit_end:
- if (bStatus == FALSE) hmacFree(pContext);
+ if (bStatus == FALSE)
+ hmacFree(pContext);
- return bStatus;
+ return bStatus;
}
-static BOOL hmacCalculateInternal(BCRYPT_HASH_HANDLE hHashTemplate, PUCHAR pbData, DWORD cbData, PUCHAR pbOutput, DWORD cbOutput, DWORD cbHashObject)
-{
- BOOL success = FALSE;
- BCRYPT_HASH_HANDLE hHash = NULL;
- PUCHAR pbHashObject = malloc(cbHashObject);
+static BOOL
+hmacCalculateInternal(BCRYPT_HASH_HANDLE hHashTemplate, PUCHAR pbData, DWORD cbData, PUCHAR pbOutput, DWORD cbOutput, DWORD cbHashObject) {
+ BOOL success = FALSE;
+ BCRYPT_HASH_HANDLE hHash = NULL;
+ PUCHAR pbHashObject = malloc(cbHashObject);
- if (BCRYPT_SUCCESS(BCryptDuplicateHash(hHashTemplate, &hHash, pbHashObject, cbHashObject, 0)))
- {
- success =
- BCRYPT_SUCCESS(BCryptHashData(hHash, pbData, cbData, 0)) &&
- BCRYPT_SUCCESS(BCryptFinishHash(hHash, pbOutput, cbOutput, 0));
+ if (BCRYPT_SUCCESS(BCryptDuplicateHash(hHashTemplate, &hHash, pbHashObject, cbHashObject, 0))) {
+ success = BCRYPT_SUCCESS(BCryptHashData(hHash, pbData, cbData, 0)) && BCRYPT_SUCCESS(BCryptFinishHash(hHash, pbOutput, cbOutput, 0));
- BCryptDestroyHash(hHash);
- }
+ BCryptDestroyHash(hHash);
+ }
- free(pbHashObject);
+ free(pbHashObject);
- return success;
+ return success;
}
-static BOOL hmacCalculate(PRF_CTX* pContext, PUCHAR pbData, DWORD cbData, PUCHAR pbDigest)
-{
- DWORD cbResult;
- DWORD cbHashObject;
+static BOOL
+hmacCalculate(PRF_CTX *pContext, PUCHAR pbData, DWORD cbData, PUCHAR pbDigest) {
+ DWORD cbResult;
+ DWORD cbHashObject;
- return
- BCRYPT_SUCCESS(BCryptGetProperty(pContext->hAlgorithm, BCRYPT_OBJECT_LENGTH, (PUCHAR)&cbHashObject, sizeof(cbHashObject), &cbResult, 0)) &&
- hmacCalculateInternal(pContext->hInnerHash, pbData, cbData, pbDigest, DIGEST_SIZE, cbHashObject) &&
- hmacCalculateInternal(pContext->hOuterHash, pbDigest, DIGEST_SIZE, pbDigest, DIGEST_SIZE, cbHashObject);
+ return BCRYPT_SUCCESS(BCryptGetProperty(pContext->hAlgorithm, BCRYPT_OBJECT_LENGTH, (PUCHAR)&cbHashObject, sizeof(cbHashObject), &cbResult, 0)) && hmacCalculateInternal(pContext->hInnerHash, pbData, cbData, pbDigest, DIGEST_SIZE, cbHashObject) && hmacCalculateInternal(pContext->hOuterHash, pbDigest, DIGEST_SIZE, pbDigest, DIGEST_SIZE, cbHashObject);
}
-static void xor(LPBYTE ptr1, LPBYTE ptr2, DWORD dwLen)
-{
+static void xor
+ (LPBYTE ptr1, LPBYTE ptr2, DWORD dwLen) {
while (dwLen--)
- *ptr1++ ^= *ptr2++;
-}
+ *ptr1++ ^= *ptr2++;
+ }
-BOOL pbkdf2(
- PUCHAR pbPassword,
- ULONG cbPassword,
- PUCHAR pbSalt,
- ULONG cbSalt,
- DWORD cIterations,
- PUCHAR pbDerivedKey,
- ULONG cbDerivedKey)
-{
- BOOL bStatus = FALSE;
- DWORD l, r, dwULen, i, j;
- BYTE Ti[DIGEST_SIZE];
- BYTE V[DIGEST_SIZE];
- LPBYTE U = malloc(max((cbSalt + 4), DIGEST_SIZE));
- PRF_CTX prfCtx = { 0 };
+ BOOL pbkdf2(PUCHAR pbPassword, ULONG cbPassword, PUCHAR pbSalt, ULONG cbSalt, DWORD cIterations, PUCHAR pbDerivedKey, ULONG cbDerivedKey) {
+ BOOL bStatus = FALSE;
+ DWORD l, r, dwULen, i, j;
+ BYTE Ti[DIGEST_SIZE];
+ BYTE V[DIGEST_SIZE];
+ LPBYTE U = malloc(max((cbSalt + 4), DIGEST_SIZE));
+ PRF_CTX prfCtx = {0};
- if (pbPassword == NULL || cbPassword == 0 || pbSalt == NULL || cbSalt == 0 || cIterations == 0 || pbDerivedKey == NULL || cbDerivedKey == 0)
- {
- free(U);
- return FALSE;
- }
+ if (pbPassword == NULL || cbPassword == 0 || pbSalt == NULL || cbSalt == 0 || cIterations == 0 || pbDerivedKey == NULL || cbDerivedKey == 0) {
+ free(U);
+ return FALSE;
+ }
- if (!hmacInit(&prfCtx, pbPassword, cbPassword))
- {
+ if (!hmacInit(&prfCtx, pbPassword, cbPassword)) {
+ goto PBKDF2_end;
+ }
+
+ l = (DWORD)ceil((double)cbDerivedKey / (double)DIGEST_SIZE);
+ r = cbDerivedKey - (l - 1) * DIGEST_SIZE;
+
+ for (i = 1; i <= l; i++) {
+ ZeroMemory(Ti, DIGEST_SIZE);
+ for (j = 0; j < cIterations; j++) {
+ if (j == 0) {
+ // construct first input for PRF
+ memcpy(U, pbSalt, cbSalt);
+ U[cbSalt] = (BYTE)((i & 0xFF000000) >> 24);
+ U[cbSalt + 1] = (BYTE)((i & 0x00FF0000) >> 16);
+ U[cbSalt + 2] = (BYTE)((i & 0x0000FF00) >> 8);
+ U[cbSalt + 3] = (BYTE)((i & 0x000000FF));
+ dwULen = cbSalt + 4;
+ }
+ else {
+ memcpy(U, V, DIGEST_SIZE);
+ dwULen = DIGEST_SIZE;
+ }
+
+ if (!hmacCalculate(&prfCtx, U, dwULen, V)) {
goto PBKDF2_end;
+ }
+
+ xor(Ti, V, DIGEST_SIZE);
}
- l = (DWORD)ceil((double)cbDerivedKey / (double)DIGEST_SIZE);
- r = cbDerivedKey - (l - 1) * DIGEST_SIZE;
-
- for (i = 1; i <= l; i++)
- {
- ZeroMemory(Ti, DIGEST_SIZE);
- for (j = 0; j < cIterations; j++)
- {
- if (j == 0)
- {
- // construct first input for PRF
- memcpy(U, pbSalt, cbSalt);
- U[cbSalt] = (BYTE)((i & 0xFF000000) >> 24);
- U[cbSalt + 1] = (BYTE)((i & 0x00FF0000) >> 16);
- U[cbSalt + 2] = (BYTE)((i & 0x0000FF00) >> 8);
- U[cbSalt + 3] = (BYTE)((i & 0x000000FF));
- dwULen = cbSalt + 4;
- }
- else
- {
- memcpy(U, V, DIGEST_SIZE);
- dwULen = DIGEST_SIZE;
- }
-
- if (!hmacCalculate(&prfCtx, U, dwULen, V))
- {
- goto PBKDF2_end;
- }
-
- xor (Ti, V, DIGEST_SIZE);
- }
-
- if (i != l)
- {
- memcpy(&pbDerivedKey[(i - 1) * DIGEST_SIZE], Ti, DIGEST_SIZE);
- }
- else
- {
- // Take only the first r bytes
- memcpy(&pbDerivedKey[(i - 1) * DIGEST_SIZE], Ti, r);
- }
+ if (i != l) {
+ memcpy(&pbDerivedKey[(i - 1) * DIGEST_SIZE], Ti, DIGEST_SIZE);
}
+ else {
+ // Take only the first r bytes
+ memcpy(&pbDerivedKey[(i - 1) * DIGEST_SIZE], Ti, r);
+ }
+ }
- bStatus = TRUE;
+ bStatus = TRUE;
PBKDF2_end:
- hmacFree(&prfCtx);
- free(U);
- return bStatus;
+ hmacFree(&prfCtx);
+ free(U);
+ return bStatus;
}
-bool _zip_crypto_pbkdf2(const zip_uint8_t *key, zip_uint64_t key_length, const zip_uint8_t *salt, zip_uint16_t salt_length, zip_uint16_t iterations, zip_uint8_t* output, zip_uint16_t output_length)
-{
- return
- (key_length <= ZIP_UINT32_MAX) &&
- pbkdf2((PUCHAR)key, (ULONG)key_length, (PUCHAR)salt, salt_length, iterations, output, output_length);
+bool
+_zip_crypto_pbkdf2(const zip_uint8_t *key, zip_uint64_t key_length, const zip_uint8_t *salt, zip_uint16_t salt_length, zip_uint16_t iterations, zip_uint8_t *output, zip_uint16_t output_length) {
+ return (key_length <= ZIP_UINT32_MAX) && pbkdf2((PUCHAR)key, (ULONG)key_length, (PUCHAR)salt, salt_length, iterations, output, output_length);
}
#endif
-struct _zip_crypto_aes_s
-{
- BCRYPT_ALG_HANDLE hAlgorithm;
- BCRYPT_KEY_HANDLE hKey;
- ULONG cbKeyObject;
- PUCHAR pbKeyObject;
+struct _zip_crypto_aes_s {
+ BCRYPT_ALG_HANDLE hAlgorithm;
+ BCRYPT_KEY_HANDLE hKey;
+ ULONG cbKeyObject;
+ PUCHAR pbKeyObject;
};
-_zip_crypto_aes_t *_zip_crypto_aes_new(const zip_uint8_t *key, zip_uint16_t key_size, zip_error_t *error)
-{
- _zip_crypto_aes_t *aes = (_zip_crypto_aes_t *)calloc(1, sizeof(*aes));
+_zip_crypto_aes_t *
+_zip_crypto_aes_new(const zip_uint8_t *key, zip_uint16_t key_size, zip_error_t *error) {
+ _zip_crypto_aes_t *aes = (_zip_crypto_aes_t *)calloc(1, sizeof(*aes));
- ULONG cbResult;
- ULONG key_length = key_size / 8;
+ ULONG cbResult;
+ ULONG key_length = key_size / 8;
- if (aes == NULL)
- {
- zip_error_set(error, ZIP_ER_MEMORY, 0);
- return NULL;
- }
+ if (aes == NULL) {
+ zip_error_set(error, ZIP_ER_MEMORY, 0);
+ return NULL;
+ }
- if (!BCRYPT_SUCCESS(BCryptOpenAlgorithmProvider(&aes->hAlgorithm, BCRYPT_AES_ALGORITHM, NULL, 0)))
- {
- _zip_crypto_aes_free(aes);
- return NULL;
- }
+ if (!BCRYPT_SUCCESS(BCryptOpenAlgorithmProvider(&aes->hAlgorithm, BCRYPT_AES_ALGORITHM, NULL, 0))) {
+ _zip_crypto_aes_free(aes);
+ return NULL;
+ }
- if (!BCRYPT_SUCCESS(BCryptSetProperty(aes->hAlgorithm, BCRYPT_CHAINING_MODE, (PUCHAR)BCRYPT_CHAIN_MODE_ECB, sizeof(BCRYPT_CHAIN_MODE_ECB), 0)))
- {
- _zip_crypto_aes_free(aes);
- return NULL;
- }
+ if (!BCRYPT_SUCCESS(BCryptSetProperty(aes->hAlgorithm, BCRYPT_CHAINING_MODE, (PUCHAR)BCRYPT_CHAIN_MODE_ECB, sizeof(BCRYPT_CHAIN_MODE_ECB), 0))) {
+ _zip_crypto_aes_free(aes);
+ return NULL;
+ }
- if (!BCRYPT_SUCCESS(BCryptGetProperty(aes->hAlgorithm, BCRYPT_OBJECT_LENGTH, (PUCHAR)&aes->cbKeyObject, sizeof(aes->cbKeyObject), &cbResult, 0)))
- {
- _zip_crypto_aes_free(aes);
- return NULL;
- }
+ if (!BCRYPT_SUCCESS(BCryptGetProperty(aes->hAlgorithm, BCRYPT_OBJECT_LENGTH, (PUCHAR)&aes->cbKeyObject, sizeof(aes->cbKeyObject), &cbResult, 0))) {
+ _zip_crypto_aes_free(aes);
+ return NULL;
+ }
- aes->pbKeyObject = malloc(aes->cbKeyObject);
+ aes->pbKeyObject = malloc(aes->cbKeyObject);
- if (!BCRYPT_SUCCESS(BCryptGenerateSymmetricKey(aes->hAlgorithm, &aes->hKey, aes->pbKeyObject, aes->cbKeyObject, (PUCHAR)key, key_length, 0)))
- {
- _zip_crypto_aes_free(aes);
- return NULL;
- }
+ if (!BCRYPT_SUCCESS(BCryptGenerateSymmetricKey(aes->hAlgorithm, &aes->hKey, aes->pbKeyObject, aes->cbKeyObject, (PUCHAR)key, key_length, 0))) {
+ _zip_crypto_aes_free(aes);
+ return NULL;
+ }
- return aes;
+ return aes;
}
-void _zip_crypto_aes_free(_zip_crypto_aes_t *aes)
-{
- if (aes == NULL)
- {
- return;
- }
+void
+_zip_crypto_aes_free(_zip_crypto_aes_t *aes) {
+ if (aes == NULL) {
+ return;
+ }
- if (aes->hKey != NULL)
- {
- BCryptDestroyKey(aes->hKey);
- }
+ if (aes->hKey != NULL) {
+ BCryptDestroyKey(aes->hKey);
+ }
- if (aes->pbKeyObject != NULL)
- {
- free(aes->pbKeyObject);
- }
+ if (aes->pbKeyObject != NULL) {
+ free(aes->pbKeyObject);
+ }
- if (aes->hAlgorithm != NULL)
- {
- BCryptCloseAlgorithmProvider(aes->hAlgorithm, 0);
- }
+ if (aes->hAlgorithm != NULL) {
+ BCryptCloseAlgorithmProvider(aes->hAlgorithm, 0);
+ }
- free(aes);
+ free(aes);
}
-bool _zip_crypto_aes_encrypt_block(_zip_crypto_aes_t *aes, const zip_uint8_t *in, zip_uint8_t *out)
-{
- ULONG cbResult;
- NTSTATUS status = BCryptEncrypt(aes->hKey, (PUCHAR)in, ZIP_CRYPTO_AES_BLOCK_LENGTH, NULL, NULL, 0, (PUCHAR)out, ZIP_CRYPTO_AES_BLOCK_LENGTH, &cbResult, 0);
- return BCRYPT_SUCCESS(status);
+bool
+_zip_crypto_aes_encrypt_block(_zip_crypto_aes_t *aes, const zip_uint8_t *in, zip_uint8_t *out) {
+ ULONG cbResult;
+ NTSTATUS status = BCryptEncrypt(aes->hKey, (PUCHAR)in, ZIP_CRYPTO_AES_BLOCK_LENGTH, NULL, NULL, 0, (PUCHAR)out, ZIP_CRYPTO_AES_BLOCK_LENGTH, &cbResult, 0);
+ return BCRYPT_SUCCESS(status);
}
-struct _zip_crypto_hmac_s
-{
- BCRYPT_ALG_HANDLE hAlgorithm;
- BCRYPT_HASH_HANDLE hHash;
- DWORD cbHashObject;
- PUCHAR pbHashObject;
- DWORD cbHash;
- PUCHAR pbHash;
+struct _zip_crypto_hmac_s {
+ BCRYPT_ALG_HANDLE hAlgorithm;
+ BCRYPT_HASH_HANDLE hHash;
+ DWORD cbHashObject;
+ PUCHAR pbHashObject;
+ DWORD cbHash;
+ PUCHAR pbHash;
};
// https://code.msdn.microsoft.com/windowsdesktop/Hmac-Computation-Sample-11fe8ec1/sourcecode?fileId=42820&pathId=283874677
-_zip_crypto_hmac_t *_zip_crypto_hmac_new(const zip_uint8_t *secret, zip_uint64_t secret_length, zip_error_t *error)
-{
- NTSTATUS status;
- ULONG cbResult;
- _zip_crypto_hmac_t *hmac;
+_zip_crypto_hmac_t *
+_zip_crypto_hmac_new(const zip_uint8_t *secret, zip_uint64_t secret_length, zip_error_t *error) {
+ NTSTATUS status;
+ ULONG cbResult;
+ _zip_crypto_hmac_t *hmac;
- if (secret_length > INT_MAX)
- {
- zip_error_set(error, ZIP_ER_INVAL, 0);
- return NULL;
- }
+ if (secret_length > INT_MAX) {
+ zip_error_set(error, ZIP_ER_INVAL, 0);
+ return NULL;
+ }
- hmac = (_zip_crypto_hmac_t *)calloc(1, sizeof(*hmac));
+ hmac = (_zip_crypto_hmac_t *)calloc(1, sizeof(*hmac));
- if (hmac == NULL)
- {
- zip_error_set(error, ZIP_ER_MEMORY, 0);
- return NULL;
- }
+ if (hmac == NULL) {
+ zip_error_set(error, ZIP_ER_MEMORY, 0);
+ return NULL;
+ }
- status = BCryptOpenAlgorithmProvider(&hmac->hAlgorithm, BCRYPT_SHA1_ALGORITHM, NULL, BCRYPT_ALG_HANDLE_HMAC_FLAG);
- if (!BCRYPT_SUCCESS(status))
- {
- _zip_crypto_hmac_free(hmac);
- return NULL;
- }
+ status = BCryptOpenAlgorithmProvider(&hmac->hAlgorithm, BCRYPT_SHA1_ALGORITHM, NULL, BCRYPT_ALG_HANDLE_HMAC_FLAG);
+ if (!BCRYPT_SUCCESS(status)) {
+ _zip_crypto_hmac_free(hmac);
+ return NULL;
+ }
- status = BCryptGetProperty(hmac->hAlgorithm, BCRYPT_OBJECT_LENGTH, (PUCHAR)&hmac->cbHashObject, sizeof(hmac->cbHashObject), &cbResult, 0);
- if (!BCRYPT_SUCCESS(status))
- {
- _zip_crypto_hmac_free(hmac);
- return NULL;
- }
+ status = BCryptGetProperty(hmac->hAlgorithm, BCRYPT_OBJECT_LENGTH, (PUCHAR)&hmac->cbHashObject, sizeof(hmac->cbHashObject), &cbResult, 0);
+ if (!BCRYPT_SUCCESS(status)) {
+ _zip_crypto_hmac_free(hmac);
+ return NULL;
+ }
- hmac->pbHashObject = malloc(hmac->cbHashObject);
+ hmac->pbHashObject = malloc(hmac->cbHashObject);
- status = BCryptGetProperty(hmac->hAlgorithm, BCRYPT_HASH_LENGTH, (PUCHAR)&hmac->cbHash, sizeof(hmac->cbHash), &cbResult, 0);
- if (!BCRYPT_SUCCESS(status))
- {
- _zip_crypto_hmac_free(hmac);
- return NULL;
- }
+ status = BCryptGetProperty(hmac->hAlgorithm, BCRYPT_HASH_LENGTH, (PUCHAR)&hmac->cbHash, sizeof(hmac->cbHash), &cbResult, 0);
+ if (!BCRYPT_SUCCESS(status)) {
+ _zip_crypto_hmac_free(hmac);
+ return NULL;
+ }
- hmac->pbHash = malloc(hmac->cbHash);
+ hmac->pbHash = malloc(hmac->cbHash);
- status = BCryptCreateHash(hmac->hAlgorithm, &hmac->hHash, hmac->pbHashObject, hmac->cbHashObject, (PUCHAR)secret, (ULONG)secret_length, 0);
- if (!BCRYPT_SUCCESS(status))
- {
- _zip_crypto_hmac_free(hmac);
- return NULL;
- }
+ status = BCryptCreateHash(hmac->hAlgorithm, &hmac->hHash, hmac->pbHashObject, hmac->cbHashObject, (PUCHAR)secret, (ULONG)secret_length, 0);
+ if (!BCRYPT_SUCCESS(status)) {
+ _zip_crypto_hmac_free(hmac);
+ return NULL;
+ }
- return hmac;
+ return hmac;
}
-void _zip_crypto_hmac_free(_zip_crypto_hmac_t *hmac)
-{
- if (hmac == NULL)
- {
- return;
- }
+void
+_zip_crypto_hmac_free(_zip_crypto_hmac_t *hmac) {
+ if (hmac == NULL) {
+ return;
+ }
- if (hmac->hHash != NULL)
- {
- BCryptDestroyHash(hmac->hHash);
- }
+ if (hmac->hHash != NULL) {
+ BCryptDestroyHash(hmac->hHash);
+ }
- if (hmac->pbHash != NULL)
- {
- free(hmac->pbHash);
- }
+ if (hmac->pbHash != NULL) {
+ free(hmac->pbHash);
+ }
- if (hmac->pbHashObject != NULL)
- {
- free(hmac->pbHashObject);
- }
+ if (hmac->pbHashObject != NULL) {
+ free(hmac->pbHashObject);
+ }
- if (hmac->hAlgorithm)
- {
- BCryptCloseAlgorithmProvider(hmac->hAlgorithm, 0);
- }
+ if (hmac->hAlgorithm) {
+ BCryptCloseAlgorithmProvider(hmac->hAlgorithm, 0);
+ }
- free(hmac);
+ free(hmac);
}
-bool _zip_crypto_hmac(_zip_crypto_hmac_t *hmac, zip_uint8_t *data, zip_uint64_t length)
-{
- if (hmac == NULL || length > ULONG_MAX)
- {
- return false;
- }
+bool
+_zip_crypto_hmac(_zip_crypto_hmac_t *hmac, zip_uint8_t *data, zip_uint64_t length) {
+ if (hmac == NULL || length > ULONG_MAX) {
+ return false;
+ }
- return BCRYPT_SUCCESS(BCryptHashData(hmac->hHash, data, (ULONG)length, 0));
+ return BCRYPT_SUCCESS(BCryptHashData(hmac->hHash, data, (ULONG)length, 0));
}
-bool _zip_crypto_hmac_output(_zip_crypto_hmac_t *hmac, zip_uint8_t *data)
-{
- if (hmac == NULL)
- {
- return false;
- }
+bool
+_zip_crypto_hmac_output(_zip_crypto_hmac_t *hmac, zip_uint8_t *data) {
+ if (hmac == NULL) {
+ return false;
+ }
- return BCRYPT_SUCCESS(BCryptFinishHash(hmac->hHash, data, hmac->cbHash, 0));
+ return BCRYPT_SUCCESS(BCryptFinishHash(hmac->hHash, data, hmac->cbHash, 0));
}
ZIP_EXTERN bool
-zip_random(zip_uint8_t *buffer, zip_uint16_t length)
-{
- return BCRYPT_SUCCESS(BCryptGenRandom(NULL, buffer, length, BCRYPT_USE_SYSTEM_PREFERRED_RNG));
+zip_random(zip_uint8_t *buffer, zip_uint16_t length) {
+ return BCRYPT_SUCCESS(BCryptGenRandom(NULL, buffer, length, BCRYPT_USE_SYSTEM_PREFERRED_RNG));
}
diff --git a/lib/zip_crypto_win.h b/lib/zip_crypto_win.h
index 15a2d46..9a239af 100644
--- a/lib/zip_crypto_win.h
+++ b/lib/zip_crypto_win.h
@@ -41,14 +41,7 @@
_zip_crypto_aes_t *_zip_crypto_aes_new(const zip_uint8_t *key, zip_uint16_t key_size, zip_error_t *error);
bool _zip_crypto_aes_encrypt_block(_zip_crypto_aes_t *aes, const zip_uint8_t *in, zip_uint8_t *out);
-bool _zip_crypto_pbkdf2(
- const zip_uint8_t *key,
- zip_uint64_t key_length,
- const zip_uint8_t *salt,
- zip_uint16_t salt_length,
- zip_uint16_t iterations,
- zip_uint8_t* output,
- zip_uint16_t output_length);
+bool _zip_crypto_pbkdf2(const zip_uint8_t *key, zip_uint64_t key_length, const zip_uint8_t *salt, zip_uint16_t salt_length, zip_uint16_t iterations, zip_uint8_t *output, zip_uint16_t output_length);
_zip_crypto_hmac_t *_zip_crypto_hmac_new(const zip_uint8_t *secret, zip_uint64_t secret_length, zip_error_t *error);
void _zip_crypto_hmac_free(_zip_crypto_hmac_t *hmac);
diff --git a/lib/zip_source_buffer.c b/lib/zip_source_buffer.c
index d2bc8ad..32e2fba 100644
--- a/lib/zip_source_buffer.c
+++ b/lib/zip_source_buffer.c
@@ -389,8 +389,8 @@
}
if ((fragments = realloc(buffer->fragments, sizeof(buffer->fragments[0]) * capacity)) == NULL) {
- zip_error_set(error, ZIP_ER_MEMORY, 0);
- return false;
+ zip_error_set(error, ZIP_ER_MEMORY, 0);
+ return false;
}
buffer->fragments = fragments;
if ((offsets = realloc(buffer->fragment_offsets, sizeof(buffer->fragment_offsets[0]) * (capacity + 1))) == NULL) {
diff --git a/lib/zip_source_read.c b/lib/zip_source_read.c
index 83514c6..b3ea57f 100644
--- a/lib/zip_source_read.c
+++ b/lib/zip_source_read.c
@@ -57,7 +57,7 @@
}
if (len == 0) {
- return 0;
+ return 0;
}
bytes_read = 0;
diff --git a/lib/zip_source_winzip_aes_decode.c b/lib/zip_source_winzip_aes_decode.c
index cd76593..c9a88df 100644
--- a/lib/zip_source_winzip_aes_decode.c
+++ b/lib/zip_source_winzip_aes_decode.c
@@ -138,8 +138,8 @@
}
if (!_zip_winzip_aes_finish(ctx->aes_ctx, computed)) {
- zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
- return false;
+ zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
+ return false;
}
_zip_winzip_aes_free(ctx->aes_ctx);
ctx->aes_ctx = NULL;
@@ -186,10 +186,10 @@
}
ctx->current_position += (zip_uint64_t)n;
- if (!_zip_winzip_aes_decrypt(ctx->aes_ctx, (zip_uint8_t *)data, (zip_uint64_t)n)) {
- zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
- return -1;
- }
+ if (!_zip_winzip_aes_decrypt(ctx->aes_ctx, (zip_uint8_t *)data, (zip_uint64_t)n)) {
+ zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
+ return -1;
+ }
return n;
diff --git a/lib/zip_source_winzip_aes_encode.c b/lib/zip_source_winzip_aes_encode.c
index 12ed1b2..cf66da4 100644
--- a/lib/zip_source_winzip_aes_encode.c
+++ b/lib/zip_source_winzip_aes_encode.c
@@ -143,19 +143,19 @@
return -1;
}
- if (!_zip_winzip_aes_encrypt(ctx->aes_ctx, data, (zip_uint64_t)ret)) {
- zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
- /* TODO: return partial read? */
- return -1;
- }
+ if (!_zip_winzip_aes_encrypt(ctx->aes_ctx, data, (zip_uint64_t)ret)) {
+ zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
+ /* TODO: return partial read? */
+ return -1;
+ }
if ((zip_uint64_t)ret < length) {
ctx->eof = true;
- if (!_zip_winzip_aes_finish(ctx->aes_ctx, ctx->data)) {
- zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
- /* TODO: return partial read? */
- return -1;
- }
+ if (!_zip_winzip_aes_finish(ctx->aes_ctx, ctx->data)) {
+ zip_error_set(&ctx->error, ZIP_ER_INTERNAL, 0);
+ /* TODO: return partial read? */
+ return -1;
+ }
_zip_winzip_aes_free(ctx->aes_ctx);
ctx->aes_ctx = NULL;
if ((ctx->buffer = _zip_buffer_new(ctx->data, HMAC_LENGTH)) == NULL) {
diff --git a/lib/zip_winzip_aes.c b/lib/zip_winzip_aes.c
index 4f98064..ad2282c 100644
--- a/lib/zip_winzip_aes.c
+++ b/lib/zip_winzip_aes.c
@@ -40,7 +40,6 @@
#include <string.h>
-
#define MAX_KEY_LENGTH 256
#define PBKDF2_ITERATIONS 1000
@@ -53,11 +52,10 @@
};
static bool
-aes_crypt(zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length)
-{
+aes_crypt(zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length) {
zip_uint64_t i, j;
- for (i=0; i < length; i++) {
+ for (i = 0; i < length; i++) {
if (ctx->pad_offset == AES_BLOCK_SIZE) {
for (j = 0; j < 8; j++) {
ctx->counter[j]++;
@@ -65,9 +63,9 @@
break;
}
}
- if (!_zip_crypto_aes_encrypt_block(ctx->aes, ctx->counter, ctx->pad)) {
- return false;
- }
+ if (!_zip_crypto_aes_encrypt_block(ctx->aes, ctx->counter, ctx->pad)) {
+ return false;
+ }
ctx->pad_offset = 0;
}
data[i] ^= ctx->pad[ctx->pad_offset++];
@@ -78,12 +76,11 @@
zip_winzip_aes_t *
-_zip_winzip_aes_new(const zip_uint8_t *password, zip_uint64_t password_length, const zip_uint8_t *salt, zip_uint16_t encryption_method, zip_uint8_t *password_verify, zip_error_t *error)
-{
+_zip_winzip_aes_new(const zip_uint8_t *password, zip_uint64_t password_length, const zip_uint8_t *salt, zip_uint16_t encryption_method, zip_uint8_t *password_verify, zip_error_t *error) {
zip_winzip_aes_t *ctx;
zip_uint8_t buffer[2 * (MAX_KEY_LENGTH / 8) + WINZIP_AES_PASSWORD_VERIFY_LENGTH];
zip_uint16_t key_size = 0; /* in bits */
- zip_uint16_t key_length; /* in bytes */
+ zip_uint16_t key_length; /* in bytes */
switch (encryption_method) {
case ZIP_EM_AES_128:
@@ -111,21 +108,21 @@
memset(ctx->counter, 0, sizeof(ctx->counter));
ctx->pad_offset = ZIP_CRYPTO_AES_BLOCK_LENGTH;
-
+
if (!_zip_crypto_pbkdf2(password, password_length, salt, key_length / 2, PBKDF2_ITERATIONS, buffer, 2 * key_length + WINZIP_AES_PASSWORD_VERIFY_LENGTH)) {
- free(ctx);
- return NULL;
+ free(ctx);
+ return NULL;
}
if ((ctx->aes = _zip_crypto_aes_new(buffer, key_size, error)) == NULL) {
- _zip_crypto_clear(ctx, sizeof(*ctx));
- free(ctx);
- return NULL;
+ _zip_crypto_clear(ctx, sizeof(*ctx));
+ free(ctx);
+ return NULL;
}
if ((ctx->hmac = _zip_crypto_hmac_new(buffer + key_length, key_length, error)) == NULL) {
- _zip_crypto_aes_free(ctx->aes);
- free(ctx);
- return NULL;
+ _zip_crypto_aes_free(ctx->aes);
+ free(ctx);
+ return NULL;
}
if (password_verify) {
@@ -137,29 +134,25 @@
bool
-_zip_winzip_aes_encrypt(zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length)
-{
+_zip_winzip_aes_encrypt(zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length) {
return aes_crypt(ctx, data, length) && _zip_crypto_hmac(ctx->hmac, data, length);
}
bool
-_zip_winzip_aes_decrypt(zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length)
-{
+_zip_winzip_aes_decrypt(zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length) {
return _zip_crypto_hmac(ctx->hmac, data, length) && aes_crypt(ctx, data, length);
}
bool
-_zip_winzip_aes_finish(zip_winzip_aes_t *ctx, zip_uint8_t *hmac)
-{
+_zip_winzip_aes_finish(zip_winzip_aes_t *ctx, zip_uint8_t *hmac) {
return _zip_crypto_hmac_output(ctx->hmac, hmac);
}
void
-_zip_winzip_aes_free(zip_winzip_aes_t *ctx)
-{
+_zip_winzip_aes_free(zip_winzip_aes_t *ctx) {
if (ctx == NULL) {
return;
}