Rizin
unix-like reverse engineering framework and cli tools
zip_winzip_aes.c File Reference
#include "zipint.h"
#include "zip_crypto.h"
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Classes

struct  _zip_winzip_aes
 

Macros

#define MAX_KEY_LENGTH   256
 
#define PBKDF2_ITERATIONS   1000
 

Functions

static bool aes_crypt (zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length)
 
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)
 
bool _zip_winzip_aes_encrypt (zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length)
 
bool _zip_winzip_aes_decrypt (zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length)
 
bool _zip_winzip_aes_finish (zip_winzip_aes_t *ctx, zip_uint8_t *hmac)
 
void _zip_winzip_aes_free (zip_winzip_aes_t *ctx)
 

Macro Definition Documentation

◆ MAX_KEY_LENGTH

#define MAX_KEY_LENGTH   256

Definition at line 42 of file zip_winzip_aes.c.

◆ PBKDF2_ITERATIONS

#define PBKDF2_ITERATIONS   1000

Definition at line 43 of file zip_winzip_aes.c.

Function Documentation

◆ _zip_winzip_aes_decrypt()

bool _zip_winzip_aes_decrypt ( zip_winzip_aes_t ctx,
zip_uint8_t data,
zip_uint64_t  length 
)

Definition at line 142 of file zip_winzip_aes.c.

142  {
143  return _zip_crypto_hmac(ctx->hmac, data, length) && aes_crypt(ctx, data, length);
144 }
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
#define _zip_crypto_hmac(hmac, data, length)
static bool aes_crypt(zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length)

References _zip_crypto_hmac, aes_crypt(), and length.

Referenced by winzip_aes_decrypt().

◆ _zip_winzip_aes_encrypt()

bool _zip_winzip_aes_encrypt ( zip_winzip_aes_t ctx,
zip_uint8_t data,
zip_uint64_t  length 
)

Definition at line 136 of file zip_winzip_aes.c.

136  {
137  return aes_crypt(ctx, data, length) && _zip_crypto_hmac(ctx->hmac, data, length);
138 }

References _zip_crypto_hmac, aes_crypt(), and length.

Referenced by winzip_aes_encrypt().

◆ _zip_winzip_aes_finish()

bool _zip_winzip_aes_finish ( zip_winzip_aes_t ctx,
zip_uint8_t hmac 
)

Definition at line 148 of file zip_winzip_aes.c.

148  {
149  return _zip_crypto_hmac_output(ctx->hmac, hmac);
150 }
#define _zip_crypto_hmac_output(hmac, data)

References _zip_crypto_hmac_output.

Referenced by verify_hmac(), and winzip_aes_encrypt().

◆ _zip_winzip_aes_free()

void _zip_winzip_aes_free ( zip_winzip_aes_t ctx)

Definition at line 154 of file zip_winzip_aes.c.

154  {
155  if (ctx == NULL) {
156  return;
157  }
158 
160  _zip_crypto_hmac_free(ctx->hmac);
161  free(ctx);
162 }
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void _zip_crypto_hmac_free(_zip_crypto_hmac_t *hmac)
void _zip_crypto_aes_free(_zip_crypto_aes_t *aes)

References _zip_crypto_aes_free(), _zip_crypto_hmac_free(), free(), and NULL.

Referenced by decrypt_header(), encrypt_header(), verify_hmac(), winzip_aes_encrypt(), and winzip_aes_free().

◆ _zip_winzip_aes_new()

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 
)

Definition at line 78 of file zip_winzip_aes.c.

78  {
81  zip_uint16_t key_size = 0; /* in bits */
82  zip_uint16_t key_length; /* in bytes */
83 
84  switch (encryption_method) {
85  case ZIP_EM_AES_128:
86  key_size = 128;
87  break;
88  case ZIP_EM_AES_192:
89  key_size = 192;
90  break;
91  case ZIP_EM_AES_256:
92  key_size = 256;
93  break;
94  }
95 
96  if (key_size == 0 || salt == NULL || password == NULL || password_length == 0) {
98  return NULL;
99  }
100 
101  key_length = key_size / 8;
102 
103  if ((ctx = (zip_winzip_aes_t *)malloc(sizeof(*ctx))) == NULL) {
105  return NULL;
106  }
107 
108  memset(ctx->counter, 0, sizeof(ctx->counter));
109  ctx->pad_offset = ZIP_CRYPTO_AES_BLOCK_LENGTH;
110 
111  if (!_zip_crypto_pbkdf2(password, password_length, salt, key_length / 2, PBKDF2_ITERATIONS, buffer, 2 * key_length + WINZIP_AES_PASSWORD_VERIFY_LENGTH)) {
112  free(ctx);
113  return NULL;
114  }
115 
116  if ((ctx->aes = _zip_crypto_aes_new(buffer, key_size, error)) == NULL) {
117  _zip_crypto_clear(ctx, sizeof(*ctx));
118  free(ctx);
119  return NULL;
120  }
121  if ((ctx->hmac = _zip_crypto_hmac_new(buffer + key_length, key_length, error)) == NULL) {
123  free(ctx);
124  return NULL;
125  }
126 
127  if (password_verify) {
128  memcpy(password_verify, buffer + (2 * key_size / 8), WINZIP_AES_PASSWORD_VERIFY_LENGTH);
129  }
130 
131  return ctx;
132 }
return memset(p, 0, total)
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
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
#define ZIP_EM_AES_256
Definition: zip.h:192
#define ZIP_EM_AES_192
Definition: zip.h:191
#define ZIP_ER_INVAL
Definition: zip.h:123
#define ZIP_EM_AES_128
Definition: zip.h:190
void * malloc(size_t size)
Definition: malloc.c:123
Definition: buffer.h:15
void error(const char *msg)
Definition: untgz.c:593
#define ZIP_CRYPTO_AES_BLOCK_LENGTH
Definition: zip_crypto.h:38
_zip_crypto_aes_t * _zip_crypto_aes_new(const zip_uint8_t *key, zip_uint16_t key_size, zip_error_t *error)
_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_pbkdf2(key, key_length, salt, salt_length, iterations, output, output_length)
#define MAX_KEY_LENGTH
#define PBKDF2_ITERATIONS
uint8_t zip_uint8_t
Definition: zipconf.h:33
uint16_t zip_uint16_t
Definition: zipconf.h:35
#define _zip_crypto_clear(b, l)
Definition: zipint.h:489
#define WINZIP_AES_PASSWORD_VERIFY_LENGTH
Definition: zipint.h:75

References _zip_crypto_aes_free(), _zip_crypto_aes_new(), _zip_crypto_clear, _zip_crypto_hmac_new(), _zip_crypto_pbkdf2, error(), free(), malloc(), MAX_KEY_LENGTH, memcpy(), memset(), NULL, PBKDF2_ITERATIONS, WINZIP_AES_PASSWORD_VERIFY_LENGTH, ZIP_CRYPTO_AES_BLOCK_LENGTH, ZIP_EM_AES_128, ZIP_EM_AES_192, ZIP_EM_AES_256, ZIP_ER_INVAL, ZIP_ER_MEMORY, and zip_error_set().

Referenced by decrypt_header(), and encrypt_header().

◆ aes_crypt()

static bool aes_crypt ( zip_winzip_aes_t ctx,
zip_uint8_t data,
zip_uint64_t  length 
)
static

Definition at line 54 of file zip_winzip_aes.c.

54  {
55  zip_uint64_t i, j;
56 
57  for (i = 0; i < length; i++) {
58  if (ctx->pad_offset == AES_BLOCK_SIZE) {
59  for (j = 0; j < 8; j++) {
60  ctx->counter[j]++;
61  if (ctx->counter[j] != 0) {
62  break;
63  }
64  }
65  if (!_zip_crypto_aes_encrypt_block(ctx->aes, ctx->counter, ctx->pad)) {
66  return false;
67  }
68  ctx->pad_offset = 0;
69  }
70  data[i] ^= ctx->pad[ctx->pad_offset++];
71  }
72 
73  return true;
74 }
lzma_index ** i
Definition: index.h:629
#define _zip_crypto_aes_encrypt_block(aes, in, out)
uint64_t zip_uint64_t
Definition: zipconf.h:39
#define AES_BLOCK_SIZE
Definition: zipint.h:77

References _zip_crypto_aes_encrypt_block, AES_BLOCK_SIZE, i, and length.

Referenced by _zip_winzip_aes_decrypt(), and _zip_winzip_aes_encrypt().