1 /*
2 zip_crypto_mbedtls.c -- mbed TLS wrapper
3 Copyright (C) 2018-2019 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 <libzip@nih.at>
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 <stdlib.h>
35
36 #include "zipint.h"
37
38 #include "zip_crypto.h"
39
40 #include <mbedtls/ctr_drbg.h>
41 #include <mbedtls/entropy.h>
42 #include <mbedtls/pkcs5.h>
43
44
45 _zip_crypto_aes_t *
_zip_crypto_aes_new(const zip_uint8_t * key,zip_uint16_t key_size,zip_error_t * error)46 _zip_crypto_aes_new(const zip_uint8_t *key, zip_uint16_t key_size, zip_error_t *error) {
47 _zip_crypto_aes_t *aes;
48
49 if ((aes = (_zip_crypto_aes_t *)malloc(sizeof(*aes))) == NULL) {
50 zip_error_set(error, ZIP_ER_MEMORY, 0);
51 return NULL;
52 }
53
54 mbedtls_aes_init(aes);
55 mbedtls_aes_setkey_enc(aes, (const unsigned char *)key, (unsigned int)key_size);
56
57 return aes;
58 }
59
60 void
_zip_crypto_aes_free(_zip_crypto_aes_t * aes)61 _zip_crypto_aes_free(_zip_crypto_aes_t *aes) {
62 if (aes == NULL) {
63 return;
64 }
65
66 mbedtls_aes_free(aes);
67 free(aes);
68 }
69
70
71 _zip_crypto_hmac_t *
_zip_crypto_hmac_new(const zip_uint8_t * secret,zip_uint64_t secret_length,zip_error_t * error)72 _zip_crypto_hmac_new(const zip_uint8_t *secret, zip_uint64_t secret_length, zip_error_t *error) {
73 _zip_crypto_hmac_t *hmac;
74
75 if (secret_length > INT_MAX) {
76 zip_error_set(error, ZIP_ER_INVAL, 0);
77 return NULL;
78 }
79
80 if ((hmac = (_zip_crypto_hmac_t *)malloc(sizeof(*hmac))) == NULL) {
81 zip_error_set(error, ZIP_ER_MEMORY, 0);
82 return NULL;
83 }
84
85 mbedtls_md_init(hmac);
86
87 if (mbedtls_md_setup(hmac, mbedtls_md_info_from_type(MBEDTLS_MD_SHA1), 1) != 0) {
88 zip_error_set(error, ZIP_ER_INTERNAL, 0);
89 free(hmac);
90 return NULL;
91 }
92
93 if (mbedtls_md_hmac_starts(hmac, (const unsigned char *)secret, (size_t)secret_length) != 0) {
94 zip_error_set(error, ZIP_ER_INTERNAL, 0);
95 free(hmac);
96 return NULL;
97 }
98
99 return hmac;
100 }
101
102
103 void
_zip_crypto_hmac_free(_zip_crypto_hmac_t * hmac)104 _zip_crypto_hmac_free(_zip_crypto_hmac_t *hmac) {
105 if (hmac == NULL) {
106 return;
107 }
108
109 mbedtls_md_free(hmac);
110 free(hmac);
111 }
112
113
114 bool
_zip_crypto_pbkdf2(const zip_uint8_t * key,zip_uint64_t key_length,const zip_uint8_t * salt,zip_uint16_t salt_length,int iterations,zip_uint8_t * output,zip_uint64_t output_length)115 _zip_crypto_pbkdf2(const zip_uint8_t *key, zip_uint64_t key_length, const zip_uint8_t *salt, zip_uint16_t salt_length, int iterations, zip_uint8_t *output, zip_uint64_t output_length) {
116 mbedtls_md_context_t sha1_ctx;
117 bool ok = true;
118
119 mbedtls_md_init(&sha1_ctx);
120
121 if (mbedtls_md_setup(&sha1_ctx, mbedtls_md_info_from_type(MBEDTLS_MD_SHA1), 1) != 0) {
122 ok = false;
123 }
124
125 if (ok && mbedtls_pkcs5_pbkdf2_hmac(&sha1_ctx, (const unsigned char *)key, (size_t)key_length, (const unsigned char *)salt, (size_t)salt_length, (unsigned int)iterations, (uint32_t)output_length, (unsigned char *)output) != 0) {
126 ok = false;
127 }
128
129 mbedtls_md_free(&sha1_ctx);
130 return ok;
131 }
132
133
134 typedef struct {
135 mbedtls_entropy_context entropy;
136 mbedtls_ctr_drbg_context ctr_drbg;
137 } zip_random_context_t;
138
139 ZIP_EXTERN bool
zip_secure_random(zip_uint8_t * buffer,zip_uint16_t length)140 zip_secure_random(zip_uint8_t *buffer, zip_uint16_t length) {
141 static zip_random_context_t *ctx = NULL;
142 const unsigned char *pers = "zip_crypto_mbedtls";
143
144 if (!ctx) {
145 ctx = (zip_random_context_t *)malloc(sizeof(zip_random_context_t));
146 if (!ctx) {
147 return false;
148 }
149 mbedtls_entropy_init(&ctx->entropy);
150 mbedtls_ctr_drbg_init(&ctx->ctr_drbg);
151 if (mbedtls_ctr_drbg_seed(&ctx->ctr_drbg, mbedtls_entropy_func, &ctx->entropy, pers, strlen(pers)) != 0) {
152 mbedtls_ctr_drbg_free(&ctx->ctr_drbg);
153 mbedtls_entropy_free(&ctx->entropy);
154 free(ctx);
155 ctx = NULL;
156 return false;
157 }
158 }
159
160 return mbedtls_ctr_drbg_random(&ctx->ctr_drbg, (unsigned char *)buffer, (size_t)length) == 0;
161 }
162