/src/libzip/lib/zip_winzip_aes.c
Line | Count | Source |
1 | | /* |
2 | | zip_winzip_aes.c -- Winzip AES de/encryption backend routines |
3 | | Copyright (C) 2017-2024 Dieter Baron and Thomas Klausner |
4 | | |
5 | | This file is part of libzip, a library to manipulate ZIP archives. |
6 | | The authors can be contacted at <info@libzip.org> |
7 | | |
8 | | Redistribution and use in source and binary forms, with or without |
9 | | modification, are permitted provided that the following conditions |
10 | | are met: |
11 | | 1. Redistributions of source code must retain the above copyright |
12 | | notice, this list of conditions and the following disclaimer. |
13 | | 2. Redistributions in binary form must reproduce the above copyright |
14 | | notice, this list of conditions and the following disclaimer in |
15 | | the documentation and/or other materials provided with the |
16 | | distribution. |
17 | | 3. The names of the authors may not be used to endorse or promote |
18 | | products derived from this software without specific prior |
19 | | written permission. |
20 | | |
21 | | THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS |
22 | | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 | | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
25 | | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
26 | | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
27 | | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
28 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
29 | | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
30 | | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
31 | | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | | */ |
33 | | |
34 | | #include "zipint.h" |
35 | | |
36 | | #include "zip_crypto.h" |
37 | | |
38 | | #include <stdlib.h> |
39 | | #include <string.h> |
40 | | |
41 | | |
42 | | #define MAX_KEY_LENGTH 256 |
43 | | #define PBKDF2_ITERATIONS 1000 |
44 | | |
45 | | struct _zip_winzip_aes { |
46 | | _zip_crypto_aes_t *aes; |
47 | | _zip_crypto_hmac_t *hmac; |
48 | | zip_uint8_t counter[ZIP_CRYPTO_AES_BLOCK_LENGTH]; |
49 | | zip_uint8_t pad[ZIP_CRYPTO_AES_BLOCK_LENGTH]; |
50 | | int pad_offset; |
51 | | }; |
52 | | |
53 | 0 | static bool aes_crypt(zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length) { |
54 | 0 | zip_uint64_t i, j; |
55 | |
|
56 | 0 | for (i = 0; i < length; i++) { |
57 | 0 | if (ctx->pad_offset == AES_BLOCK_SIZE) { |
58 | 0 | for (j = 0; j < 8; j++) { |
59 | 0 | ctx->counter[j]++; |
60 | 0 | if (ctx->counter[j] != 0) { |
61 | 0 | break; |
62 | 0 | } |
63 | 0 | } |
64 | 0 | if (!_zip_crypto_aes_encrypt_block(ctx->aes, ctx->counter, ctx->pad)) { |
65 | 0 | return false; |
66 | 0 | } |
67 | 0 | ctx->pad_offset = 0; |
68 | 0 | } |
69 | 0 | data[i] ^= ctx->pad[ctx->pad_offset++]; |
70 | 0 | } |
71 | | |
72 | 0 | return true; |
73 | 0 | } |
74 | | |
75 | | |
76 | 0 | 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) { |
77 | 0 | zip_winzip_aes_t *ctx; |
78 | 0 | zip_uint8_t buffer[2 * (MAX_KEY_LENGTH / 8) + WINZIP_AES_PASSWORD_VERIFY_LENGTH]; |
79 | 0 | zip_uint16_t key_size = 0; /* in bits */ |
80 | 0 | zip_uint16_t key_length; /* in bytes */ |
81 | |
|
82 | 0 | switch (encryption_method) { |
83 | 0 | case ZIP_EM_AES_128: |
84 | 0 | key_size = 128; |
85 | 0 | break; |
86 | 0 | case ZIP_EM_AES_192: |
87 | 0 | key_size = 192; |
88 | 0 | break; |
89 | 0 | case ZIP_EM_AES_256: |
90 | 0 | key_size = 256; |
91 | 0 | break; |
92 | 0 | } |
93 | | |
94 | 0 | if (key_size == 0 || salt == NULL || password == NULL || password_length == 0) { |
95 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
96 | 0 | return NULL; |
97 | 0 | } |
98 | | |
99 | 0 | key_length = key_size / 8; |
100 | |
|
101 | 0 | if ((ctx = (zip_winzip_aes_t *)malloc(sizeof(*ctx))) == NULL) { |
102 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
103 | 0 | return NULL; |
104 | 0 | } |
105 | | |
106 | 0 | memset(ctx->counter, 0, sizeof(ctx->counter)); |
107 | 0 | ctx->pad_offset = ZIP_CRYPTO_AES_BLOCK_LENGTH; |
108 | |
|
109 | 0 | if (!_zip_crypto_pbkdf2(password, password_length, salt, key_length / 2, PBKDF2_ITERATIONS, buffer, 2 * key_length + WINZIP_AES_PASSWORD_VERIFY_LENGTH)) { |
110 | 0 | free(ctx); |
111 | 0 | return NULL; |
112 | 0 | } |
113 | | |
114 | 0 | if ((ctx->aes = _zip_crypto_aes_new(buffer, key_size, error)) == NULL) { |
115 | 0 | _zip_crypto_clear(buffer, sizeof(buffer)); |
116 | 0 | _zip_crypto_clear(ctx, sizeof(*ctx)); |
117 | 0 | free(ctx); |
118 | 0 | return NULL; |
119 | 0 | } |
120 | 0 | if ((ctx->hmac = _zip_crypto_hmac_new(buffer + key_length, key_length, error)) == NULL) { |
121 | 0 | _zip_crypto_clear(buffer, sizeof(buffer)); |
122 | 0 | _zip_crypto_aes_free(ctx->aes); |
123 | 0 | free(ctx); |
124 | 0 | return NULL; |
125 | 0 | } |
126 | | |
127 | 0 | if (password_verify) { |
128 | 0 | (void)memcpy_s(password_verify, WINZIP_AES_PASSWORD_VERIFY_LENGTH, buffer + (2 * key_size / 8), WINZIP_AES_PASSWORD_VERIFY_LENGTH); |
129 | 0 | } |
130 | |
|
131 | 0 | _zip_crypto_clear(buffer, sizeof(buffer)); |
132 | |
|
133 | 0 | return ctx; |
134 | 0 | } |
135 | | |
136 | | |
137 | 0 | bool _zip_winzip_aes_encrypt(zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length) { |
138 | 0 | return aes_crypt(ctx, data, length) && _zip_crypto_hmac(ctx->hmac, data, length); |
139 | 0 | } |
140 | | |
141 | | |
142 | 0 | bool _zip_winzip_aes_decrypt(zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length) { |
143 | 0 | return _zip_crypto_hmac(ctx->hmac, data, length) && aes_crypt(ctx, data, length); |
144 | 0 | } |
145 | | |
146 | | |
147 | 0 | bool _zip_winzip_aes_finish(zip_winzip_aes_t *ctx, zip_uint8_t *hmac) { |
148 | 0 | return _zip_crypto_hmac_output(ctx->hmac, hmac); |
149 | 0 | } |
150 | | |
151 | | |
152 | 0 | void _zip_winzip_aes_free(zip_winzip_aes_t *ctx) { |
153 | 0 | if (ctx == NULL) { |
154 | 0 | return; |
155 | 0 | } |
156 | | |
157 | 0 | _zip_crypto_aes_free(ctx->aes); |
158 | 0 | _zip_crypto_hmac_free(ctx->hmac); |
159 | 0 | _zip_crypto_clear(ctx, sizeof(*ctx)); |
160 | 0 | free(ctx); |
161 | 0 | } |