/src/libzip/lib/zip_winzip_aes.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | zip_winzip_aes.c -- Winzip AES de/encryption backend routines |
3 | | Copyright (C) 2017-2022 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 | | static bool |
54 | 11.0k | aes_crypt(zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length) { |
55 | 11.0k | zip_uint64_t i, j; |
56 | | |
57 | 85.6M | for (i = 0; i < length; i++) { |
58 | 85.6M | if (ctx->pad_offset == AES_BLOCK_SIZE) { |
59 | 5.37M | for (j = 0; j < 8; j++) { |
60 | 5.37M | ctx->counter[j]++; |
61 | 5.37M | if (ctx->counter[j] != 0) { |
62 | 5.35M | break; |
63 | 5.35M | } |
64 | 5.37M | } |
65 | 5.35M | if (!_zip_crypto_aes_encrypt_block(ctx->aes, ctx->counter, ctx->pad)) { |
66 | 0 | return false; |
67 | 0 | } |
68 | 5.35M | ctx->pad_offset = 0; |
69 | 5.35M | } |
70 | 85.6M | data[i] ^= ctx->pad[ctx->pad_offset++]; |
71 | 85.6M | } |
72 | | |
73 | 11.0k | return true; |
74 | 11.0k | } |
75 | | |
76 | | |
77 | | zip_winzip_aes_t * |
78 | 921 | _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) { |
79 | 921 | zip_winzip_aes_t *ctx; |
80 | 921 | zip_uint8_t buffer[2 * (MAX_KEY_LENGTH / 8) + WINZIP_AES_PASSWORD_VERIFY_LENGTH]; |
81 | 921 | zip_uint16_t key_size = 0; /* in bits */ |
82 | 921 | zip_uint16_t key_length; /* in bytes */ |
83 | | |
84 | 921 | switch (encryption_method) { |
85 | 290 | case ZIP_EM_AES_128: |
86 | 290 | key_size = 128; |
87 | 290 | break; |
88 | 30 | case ZIP_EM_AES_192: |
89 | 30 | key_size = 192; |
90 | 30 | break; |
91 | 601 | case ZIP_EM_AES_256: |
92 | 601 | key_size = 256; |
93 | 601 | break; |
94 | 921 | } |
95 | | |
96 | 921 | if (key_size == 0 || salt == NULL || password == NULL || password_length == 0) { |
97 | 0 | zip_error_set(error, ZIP_ER_INVAL, 0); |
98 | 0 | return NULL; |
99 | 0 | } |
100 | | |
101 | 921 | key_length = key_size / 8; |
102 | | |
103 | 921 | if ((ctx = (zip_winzip_aes_t *)malloc(sizeof(*ctx))) == NULL) { |
104 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
105 | 0 | return NULL; |
106 | 0 | } |
107 | | |
108 | 921 | memset(ctx->counter, 0, sizeof(ctx->counter)); |
109 | 921 | ctx->pad_offset = ZIP_CRYPTO_AES_BLOCK_LENGTH; |
110 | | |
111 | 921 | if (!_zip_crypto_pbkdf2(password, password_length, salt, key_length / 2, PBKDF2_ITERATIONS, buffer, 2 * key_length + WINZIP_AES_PASSWORD_VERIFY_LENGTH)) { |
112 | 0 | free(ctx); |
113 | 0 | return NULL; |
114 | 0 | } |
115 | | |
116 | 921 | if ((ctx->aes = _zip_crypto_aes_new(buffer, key_size, error)) == NULL) { |
117 | 0 | _zip_crypto_clear(ctx, sizeof(*ctx)); |
118 | 0 | free(ctx); |
119 | 0 | return NULL; |
120 | 0 | } |
121 | 921 | if ((ctx->hmac = _zip_crypto_hmac_new(buffer + key_length, key_length, error)) == NULL) { |
122 | 0 | _zip_crypto_aes_free(ctx->aes); |
123 | 0 | free(ctx); |
124 | 0 | return NULL; |
125 | 0 | } |
126 | | |
127 | 921 | if (password_verify) { |
128 | 921 | (void)memcpy_s(password_verify, WINZIP_AES_PASSWORD_VERIFY_LENGTH, buffer + (2 * key_size / 8), WINZIP_AES_PASSWORD_VERIFY_LENGTH); |
129 | 921 | } |
130 | | |
131 | 921 | return ctx; |
132 | 921 | } |
133 | | |
134 | | |
135 | | bool |
136 | 9.54k | _zip_winzip_aes_encrypt(zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length) { |
137 | 9.54k | return aes_crypt(ctx, data, length) && _zip_crypto_hmac(ctx->hmac, data, length); |
138 | 9.54k | } |
139 | | |
140 | | |
141 | | bool |
142 | 1.50k | _zip_winzip_aes_decrypt(zip_winzip_aes_t *ctx, zip_uint8_t *data, zip_uint64_t length) { |
143 | 1.50k | return _zip_crypto_hmac(ctx->hmac, data, length) && aes_crypt(ctx, data, length); |
144 | 1.50k | } |
145 | | |
146 | | |
147 | | bool |
148 | 619 | _zip_winzip_aes_finish(zip_winzip_aes_t *ctx, zip_uint8_t *hmac) { |
149 | 619 | return _zip_crypto_hmac_output(ctx->hmac, hmac); |
150 | 619 | } |
151 | | |
152 | | |
153 | | void |
154 | 1.66k | _zip_winzip_aes_free(zip_winzip_aes_t *ctx) { |
155 | 1.66k | if (ctx == NULL) { |
156 | 743 | return; |
157 | 743 | } |
158 | | |
159 | 921 | _zip_crypto_aes_free(ctx->aes); |
160 | 921 | _zip_crypto_hmac_free(ctx->hmac); |
161 | 921 | free(ctx); |
162 | 921 | } |