Rizin
unix-like reverse engineering framework and cli tools
zip_crypto_win.c File Reference
#include <stdlib.h>
#include <limits.h>
#include "zipint.h"
#include "zip_crypto.h"
#include <windows.h>
#include <bcrypt.h>

Go to the source code of this file.

Classes

struct  _zip_crypto_aes_s
 
struct  _zip_crypto_hmac_s
 

Macros

#define WIN32_LEAN_AND_MEAN
 
#define NOCRYPT
 
#define HAS_BCRYPTDERIVEKEYPBKDF2
 

Functions

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_aes_t_zip_crypto_aes_new (const zip_uint8_t *key, zip_uint16_t key_size, zip_error_t *error)
 
void _zip_crypto_aes_free (_zip_crypto_aes_t *aes)
 
bool _zip_crypto_aes_encrypt_block (_zip_crypto_aes_t *aes, const zip_uint8_t *in, zip_uint8_t *out)
 
_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)
 
bool _zip_crypto_hmac (_zip_crypto_hmac_t *hmac, zip_uint8_t *data, zip_uint64_t length)
 
bool _zip_crypto_hmac_output (_zip_crypto_hmac_t *hmac, zip_uint8_t *data)
 
ZIP_EXTERN bool zip_secure_random (zip_uint8_t *buffer, zip_uint16_t length)
 

Macro Definition Documentation

◆ HAS_BCRYPTDERIVEKEYPBKDF2

#define HAS_BCRYPTDERIVEKEYPBKDF2

Definition at line 85 of file zip_crypto_win.c.

◆ NOCRYPT

#define NOCRYPT

Definition at line 41 of file zip_crypto_win.c.

◆ WIN32_LEAN_AND_MEAN

#define WIN32_LEAN_AND_MEAN

Definition at line 40 of file zip_crypto_win.c.

Function Documentation

◆ _zip_crypto_aes_encrypt_block()

bool _zip_crypto_aes_encrypt_block ( _zip_crypto_aes_t aes,
const zip_uint8_t in,
zip_uint8_t out 
)

Definition at line 373 of file zip_crypto_win.c.

373  {
374  ULONG cbResult;
375  NTSTATUS status = BCryptEncrypt(aes->hKey, (PUCHAR)in, ZIP_CRYPTO_AES_BLOCK_LENGTH, NULL, NULL, 0, (PUCHAR)out, ZIP_CRYPTO_AES_BLOCK_LENGTH, &cbResult, 0);
376  return BCRYPT_SUCCESS(status);
377 }
const lzma_allocator const uint8_t * in
Definition: block.h:527
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
#define NULL
Definition: cris-opc.c:27
static const char struct stat static buf struct stat static buf static vhangup int status
Definition: sflib.h:145
LONG NTSTATUS
Definition: win.h:198
ULONG
#define ZIP_CRYPTO_AES_BLOCK_LENGTH
Definition: zip_crypto.h:38

References in, NULL, out, status, ULONG, and ZIP_CRYPTO_AES_BLOCK_LENGTH.

◆ _zip_crypto_aes_free()

void _zip_crypto_aes_free ( _zip_crypto_aes_t aes)

Definition at line 352 of file zip_crypto_win.c.

352  {
353  if (aes == NULL) {
354  return;
355  }
356 
357  if (aes->hKey != NULL) {
358  BCryptDestroyKey(aes->hKey);
359  }
360 
361  if (aes->pbKeyObject != NULL) {
362  free(aes->pbKeyObject);
363  }
364 
365  if (aes->hAlgorithm != NULL) {
366  BCryptCloseAlgorithmProvider(aes->hAlgorithm, 0);
367  }
368 
369  free(aes);
370 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References free(), and NULL.

Referenced by _zip_crypto_aes_new().

◆ _zip_crypto_aes_new()

_zip_crypto_aes_t* _zip_crypto_aes_new ( const zip_uint8_t key,
zip_uint16_t  key_size,
zip_error_t error 
)

Definition at line 310 of file zip_crypto_win.c.

310  {
311  _zip_crypto_aes_t *aes = (_zip_crypto_aes_t *)calloc(1, sizeof(*aes));
312 
313  ULONG cbResult;
314  ULONG key_length = key_size / 8;
315 
316  if (aes == NULL) {
318  return NULL;
319  }
320 
321  if (!BCRYPT_SUCCESS(BCryptOpenAlgorithmProvider(&aes->hAlgorithm, BCRYPT_AES_ALGORITHM, NULL, 0))) {
323  return NULL;
324  }
325 
326  if (!BCRYPT_SUCCESS(BCryptSetProperty(aes->hAlgorithm, BCRYPT_CHAINING_MODE, (PUCHAR)BCRYPT_CHAIN_MODE_ECB, sizeof(BCRYPT_CHAIN_MODE_ECB), 0))) {
328  return NULL;
329  }
330 
331  if (!BCRYPT_SUCCESS(BCryptGetProperty(aes->hAlgorithm, BCRYPT_OBJECT_LENGTH, (PUCHAR)&aes->cbKeyObject, sizeof(aes->cbKeyObject), &cbResult, 0))) {
333  return NULL;
334  }
335 
336  aes->pbKeyObject = malloc(aes->cbKeyObject);
337  if (aes->pbKeyObject == NULL) {
340  return NULL;
341  }
342 
343  if (!BCRYPT_SUCCESS(BCryptGenerateSymmetricKey(aes->hAlgorithm, &aes->hKey, aes->pbKeyObject, aes->cbKeyObject, (PUCHAR)key, key_length, 0))) {
345  return NULL;
346  }
347 
348  return aes;
349 }
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len key
Definition: sflib.h:118
ZIP_EXTERN void zip_error_set(zip_error_t *_Nullable, int, int)
Definition: zip_error.c:126
#define ZIP_ER_MEMORY
Definition: zip.h:119
void * malloc(size_t size)
Definition: malloc.c:123
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
void error(const char *msg)
Definition: untgz.c:593
void _zip_crypto_aes_free(_zip_crypto_aes_t *aes)

References _zip_crypto_aes_free(), calloc(), error(), key, malloc(), NULL, ULONG, ZIP_ER_MEMORY, and zip_error_set().

◆ _zip_crypto_hmac()

bool _zip_crypto_hmac ( _zip_crypto_hmac_t hmac,
zip_uint8_t data,
zip_uint64_t  length 
)

Definition at line 475 of file zip_crypto_win.c.

475  {
476  if (hmac == NULL || length > ULONG_MAX) {
477  return false;
478  }
479 
480  return BCRYPT_SUCCESS(BCryptHashData(hmac->hHash, data, (ULONG)length, 0));
481 }
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec struct timespec static rem const char static group const void length
Definition: sflib.h:133
BCRYPT_HASH_HANDLE hHash

References _zip_crypto_hmac_s::hHash, length, NULL, and ULONG.

◆ _zip_crypto_hmac_free()

void _zip_crypto_hmac_free ( _zip_crypto_hmac_t hmac)

Definition at line 450 of file zip_crypto_win.c.

450  {
451  if (hmac == NULL) {
452  return;
453  }
454 
455  if (hmac->hHash != NULL) {
456  BCryptDestroyHash(hmac->hHash);
457  }
458 
459  if (hmac->pbHash != NULL) {
460  free(hmac->pbHash);
461  }
462 
463  if (hmac->pbHashObject != NULL) {
464  free(hmac->pbHashObject);
465  }
466 
467  if (hmac->hAlgorithm) {
468  BCryptCloseAlgorithmProvider(hmac->hAlgorithm, 0);
469  }
470 
471  free(hmac);
472 }
BCRYPT_ALG_HANDLE hAlgorithm

References free(), _zip_crypto_hmac_s::hAlgorithm, _zip_crypto_hmac_s::hHash, NULL, _zip_crypto_hmac_s::pbHash, and _zip_crypto_hmac_s::pbHashObject.

Referenced by _zip_crypto_hmac_new().

◆ _zip_crypto_hmac_new()

_zip_crypto_hmac_t* _zip_crypto_hmac_new ( const zip_uint8_t secret,
zip_uint64_t  secret_length,
zip_error_t error 
)

Definition at line 391 of file zip_crypto_win.c.

391  {
393  ULONG cbResult;
394  _zip_crypto_hmac_t *hmac;
395 
396  if (secret_length > INT_MAX) {
398  return NULL;
399  }
400 
401  hmac = (_zip_crypto_hmac_t *)calloc(1, sizeof(*hmac));
402 
403  if (hmac == NULL) {
405  return NULL;
406  }
407 
408  status = BCryptOpenAlgorithmProvider(&hmac->hAlgorithm, BCRYPT_SHA1_ALGORITHM, NULL, BCRYPT_ALG_HANDLE_HMAC_FLAG);
409  if (!BCRYPT_SUCCESS(status)) {
410  _zip_crypto_hmac_free(hmac);
411  return NULL;
412  }
413 
414  status = BCryptGetProperty(hmac->hAlgorithm, BCRYPT_OBJECT_LENGTH, (PUCHAR)&hmac->cbHashObject, sizeof(hmac->cbHashObject), &cbResult, 0);
415  if (!BCRYPT_SUCCESS(status)) {
416  _zip_crypto_hmac_free(hmac);
417  return NULL;
418  }
419 
420  hmac->pbHashObject = malloc(hmac->cbHashObject);
421  if (hmac->pbHashObject == NULL) {
422  _zip_crypto_hmac_free(hmac);
424  return NULL;
425  }
426 
427  status = BCryptGetProperty(hmac->hAlgorithm, BCRYPT_HASH_LENGTH, (PUCHAR)&hmac->cbHash, sizeof(hmac->cbHash), &cbResult, 0);
428  if (!BCRYPT_SUCCESS(status)) {
429  _zip_crypto_hmac_free(hmac);
430  return NULL;
431  }
432 
433  hmac->pbHash = malloc(hmac->cbHash);
434  if (hmac->pbHash == NULL) {
435  _zip_crypto_hmac_free(hmac);
437  return NULL;
438  }
439 
440  status = BCryptCreateHash(hmac->hAlgorithm, &hmac->hHash, hmac->pbHashObject, hmac->cbHashObject, (PUCHAR)secret, (ULONG)secret_length, 0);
441  if (!BCRYPT_SUCCESS(status)) {
442  _zip_crypto_hmac_free(hmac);
443  return NULL;
444  }
445 
446  return hmac;
447 }
#define INT_MAX
Definition: cp-demangle.c:131
#define ZIP_ER_INVAL
Definition: zip.h:123
void _zip_crypto_hmac_free(_zip_crypto_hmac_t *hmac)

References _zip_crypto_hmac_free(), calloc(), _zip_crypto_hmac_s::cbHash, _zip_crypto_hmac_s::cbHashObject, error(), _zip_crypto_hmac_s::hAlgorithm, _zip_crypto_hmac_s::hHash, INT_MAX, malloc(), NULL, _zip_crypto_hmac_s::pbHash, _zip_crypto_hmac_s::pbHashObject, status, ULONG, ZIP_ER_INVAL, ZIP_ER_MEMORY, and zip_error_set().

◆ _zip_crypto_hmac_output()

bool _zip_crypto_hmac_output ( _zip_crypto_hmac_t hmac,
zip_uint8_t data 
)

Definition at line 484 of file zip_crypto_win.c.

484  {
485  if (hmac == NULL) {
486  return false;
487  }
488 
489  return BCRYPT_SUCCESS(BCryptFinishHash(hmac->hHash, data, hmac->cbHash, 0));
490 }

References _zip_crypto_hmac_s::cbHash, _zip_crypto_hmac_s::hHash, and NULL.

◆ _zip_crypto_pbkdf2()

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 
)

Definition at line 91 of file zip_crypto_win.c.

91  {
92  BCRYPT_ALG_HANDLE hAlgorithm = NULL;
93  bool result;
94 
95  if (!BCRYPT_SUCCESS(BCryptOpenAlgorithmProvider(&hAlgorithm, BCRYPT_SHA1_ALGORITHM, NULL, BCRYPT_ALG_HANDLE_HMAC_FLAG))) {
96  return false;
97  }
98 
99  result = BCRYPT_SUCCESS(BCryptDeriveKeyPBKDF2(hAlgorithm, (PUCHAR)key, (ULONG)key_length, (PUCHAR)salt, salt_length, iterations, output, output_length, 0));
100 
101  BCryptCloseAlgorithmProvider(hAlgorithm, 0);
102 
103  return result;
104 }
diff_output_t output
Definition: zipcmp.c:237

References key, NULL, output, and ULONG.

◆ zip_secure_random()

ZIP_EXTERN bool zip_secure_random ( zip_uint8_t buffer,
zip_uint16_t  length 
)

Definition at line 493 of file zip_crypto_win.c.

493  {
494  return BCRYPT_SUCCESS(BCryptGenRandom(NULL, buffer, length, BCRYPT_USE_SYSTEM_PREFERRED_RNG));
495 }
Definition: buffer.h:15

References length, and NULL.