1 /*- 2 * Copyright (c) 2003-2007 Tim Kientzle 3 * Copyright (c) 2011 Andres Mejia 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #ifndef ARCHIVE_DIGEST_PRIVATE_H_INCLUDED 28 #define ARCHIVE_DIGEST_PRIVATE_H_INCLUDED 29 30 #ifndef __LIBARCHIVE_BUILD 31 #error This header is only to be used internally to libarchive. 32 #endif 33 /* 34 * Crypto support in various Operating Systems: 35 * 36 * NetBSD: 37 * - MD5 and SHA1 in libc: without _ after algorithm name 38 * - SHA2 in libc: with _ after algorithm name 39 * 40 * OpenBSD: 41 * - MD5, SHA1 and SHA2 in libc: without _ after algorithm name 42 * - OpenBSD 4.4 and earlier have SHA2 in libc with _ after algorithm name 43 * 44 * DragonFly and FreeBSD: 45 * - MD5 libmd: without _ after algorithm name 46 * - SHA1, SHA256 and SHA512 in libmd: with _ after algorithm name 47 * 48 * Mac OS X (10.4 and later): 49 * - MD5, SHA1 and SHA2 in libSystem: with CC_ prefix and _ after algorithm name 50 * 51 * OpenSSL: 52 * - MD5, SHA1 and SHA2 in libcrypto: with _ after algorithm name 53 * 54 * Windows: 55 * - MD5, SHA1 and SHA2 in archive_crypto.c using Windows crypto API 56 */ 57 58 /* libc crypto headers */ 59 #if defined(ARCHIVE_CRYPTO_MD5_LIBC) 60 #include <md5.h> 61 #endif 62 #if defined(ARCHIVE_CRYPTO_RMD160_LIBC) 63 #include <rmd160.h> 64 #endif 65 #if defined(ARCHIVE_CRYPTO_SHA1_LIBC) 66 #include <sha1.h> 67 #endif 68 #if defined(ARCHIVE_CRYPTO_SHA256_LIBC) ||\ 69 defined(ARCHIVE_CRYPTO_SHA256_LIBC2) ||\ 70 defined(ARCHIVE_CRYPTO_SHA256_LIBC3) ||\ 71 defined(ARCHIVE_CRYPTO_SHA384_LIBC) ||\ 72 defined(ARCHIVE_CRYPTO_SHA384_LIBC2) ||\ 73 defined(ARCHIVE_CRYPTO_SHA384_LIBC3) ||\ 74 defined(ARCHIVE_CRYPTO_SHA512_LIBC) ||\ 75 defined(ARCHIVE_CRYPTO_SHA512_LIBC2) ||\ 76 defined(ARCHIVE_CRYPTO_SHA512_LIBC3) 77 #include <sha2.h> 78 #endif 79 80 /* libmd crypto headers */ 81 #if defined(ARCHIVE_CRYPTO_MD5_LIBMD) ||\ 82 defined(ARCHIVE_CRYPTO_RMD160_LIBMD) ||\ 83 defined(ARCHIVE_CRYPTO_SHA1_LIBMD) ||\ 84 defined(ARCHIVE_CRYPTO_SHA256_LIBMD) ||\ 85 defined(ARCHIVE_CRYPTO_SHA512_LIBMD) 86 #define ARCHIVE_CRYPTO_LIBMD 1 87 #endif 88 89 #if defined(ARCHIVE_CRYPTO_MD5_LIBMD) 90 #include <md5.h> 91 #endif 92 #if defined(ARCHIVE_CRYPTO_RMD160_LIBMD) 93 #include <ripemd.h> 94 #endif 95 #if defined(ARCHIVE_CRYPTO_SHA1_LIBMD) 96 #include <sha.h> 97 #endif 98 #if defined(ARCHIVE_CRYPTO_SHA256_LIBMD) 99 #include <sha256.h> 100 #endif 101 #if defined(ARCHIVE_CRYPTO_SHA512_LIBMD) 102 #include <sha512.h> 103 #endif 104 105 /* libSystem crypto headers */ 106 #if defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM) ||\ 107 defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM) ||\ 108 defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM) ||\ 109 defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM) ||\ 110 defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM) 111 #include <CommonCrypto/CommonDigest.h> 112 #endif 113 114 /* mbed TLS crypto headers */ 115 #if defined(ARCHIVE_CRYPTO_MD5_MBEDTLS) 116 #include <mbedtls/md5.h> 117 #endif 118 #if defined(ARCHIVE_CRYPTO_RMD160_MBEDTLS) 119 #include <mbedtls/ripemd160.h> 120 #endif 121 #if defined(ARCHIVE_CRYPTO_SHA1_MBEDTLS) 122 #include <mbedtls/sha1.h> 123 #endif 124 #if defined(ARCHIVE_CRYPTO_SHA256_MBEDTLS) 125 #include <mbedtls/sha256.h> 126 #endif 127 #if defined(ARCHIVE_CRYPTO_SHA384_MBEDTLS) ||\ 128 defined(ARCHIVE_CRYPTO_SHA512_MBEDTLS) 129 #include <mbedtls/sha512.h> 130 #endif 131 132 /* Nettle crypto headers */ 133 #if defined(ARCHIVE_CRYPTO_MD5_NETTLE) 134 #include <nettle/md5.h> 135 #endif 136 #if defined(ARCHIVE_CRYPTO_RMD160_NETTLE) 137 #include <nettle/ripemd160.h> 138 #endif 139 #if defined(ARCHIVE_CRYPTO_SHA1_NETTLE) ||\ 140 defined(ARCHIVE_CRYPTO_SHA256_NETTLE) ||\ 141 defined(ARCHIVE_CRYPTO_SHA384_NETTLE) ||\ 142 defined(ARCHIVE_CRYPTO_SHA512_NETTLE) 143 #include <nettle/sha.h> 144 #endif 145 146 /* OpenSSL crypto headers */ 147 #if defined(ARCHIVE_CRYPTO_MD5_OPENSSL) ||\ 148 defined(ARCHIVE_CRYPTO_RMD160_OPENSSL) ||\ 149 defined(ARCHIVE_CRYPTO_SHA1_OPENSSL) ||\ 150 defined(ARCHIVE_CRYPTO_SHA256_OPENSSL) ||\ 151 defined(ARCHIVE_CRYPTO_SHA384_OPENSSL) ||\ 152 defined(ARCHIVE_CRYPTO_SHA512_OPENSSL) 153 #define ARCHIVE_CRYPTO_OPENSSL 1 154 #include "archive_openssl_evp_private.h" 155 #endif 156 157 /* Windows crypto headers */ 158 #if defined(ARCHIVE_CRYPTO_MD5_WIN) ||\ 159 defined(ARCHIVE_CRYPTO_SHA1_WIN) ||\ 160 defined(ARCHIVE_CRYPTO_SHA256_WIN) ||\ 161 defined(ARCHIVE_CRYPTO_SHA384_WIN) ||\ 162 defined(ARCHIVE_CRYPTO_SHA512_WIN) 163 #include <windows.h> 164 #include <wincrypt.h> 165 typedef struct { 166 int valid; 167 HCRYPTPROV cryptProv; 168 HCRYPTHASH hash; 169 } Digest_CTX; 170 #endif 171 172 /* typedefs */ 173 #if defined(ARCHIVE_CRYPTO_MD5_LIBC) 174 typedef MD5_CTX archive_md5_ctx; 175 #elif defined(ARCHIVE_CRYPTO_MD5_LIBMD) 176 typedef MD5_CTX archive_md5_ctx; 177 #elif defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM) 178 typedef CC_MD5_CTX archive_md5_ctx; 179 #elif defined(ARCHIVE_CRYPTO_MD5_MBEDTLS) 180 typedef mbedtls_md5_context archive_md5_ctx; 181 #elif defined(ARCHIVE_CRYPTO_MD5_NETTLE) 182 typedef struct md5_ctx archive_md5_ctx; 183 #elif defined(ARCHIVE_CRYPTO_MD5_OPENSSL) 184 typedef EVP_MD_CTX *archive_md5_ctx; 185 #elif defined(ARCHIVE_CRYPTO_MD5_WIN) 186 typedef Digest_CTX archive_md5_ctx; 187 #else 188 typedef unsigned char archive_md5_ctx; 189 #endif 190 191 #if defined(ARCHIVE_CRYPTO_RMD160_LIBC) 192 typedef RMD160_CTX archive_rmd160_ctx; 193 #elif defined(ARCHIVE_CRYPTO_RMD160_LIBMD) 194 typedef RIPEMD160_CTX archive_rmd160_ctx; 195 #elif defined(ARCHIVE_CRYPTO_RMD160_MBEDTLS) 196 typedef mbedtls_ripemd160_context archive_rmd160_ctx; 197 #elif defined(ARCHIVE_CRYPTO_RMD160_NETTLE) 198 typedef struct ripemd160_ctx archive_rmd160_ctx; 199 #elif defined(ARCHIVE_CRYPTO_RMD160_OPENSSL) 200 typedef EVP_MD_CTX *archive_rmd160_ctx; 201 #else 202 typedef unsigned char archive_rmd160_ctx; 203 #endif 204 205 #if defined(ARCHIVE_CRYPTO_SHA1_LIBC) 206 typedef SHA1_CTX archive_sha1_ctx; 207 #elif defined(ARCHIVE_CRYPTO_SHA1_LIBMD) 208 typedef SHA1_CTX archive_sha1_ctx; 209 #elif defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM) 210 typedef CC_SHA1_CTX archive_sha1_ctx; 211 #elif defined(ARCHIVE_CRYPTO_SHA1_MBEDTLS) 212 typedef mbedtls_sha1_context archive_sha1_ctx; 213 #elif defined(ARCHIVE_CRYPTO_SHA1_NETTLE) 214 typedef struct sha1_ctx archive_sha1_ctx; 215 #elif defined(ARCHIVE_CRYPTO_SHA1_OPENSSL) 216 typedef EVP_MD_CTX *archive_sha1_ctx; 217 #elif defined(ARCHIVE_CRYPTO_SHA1_WIN) 218 typedef Digest_CTX archive_sha1_ctx; 219 #else 220 typedef unsigned char archive_sha1_ctx; 221 #endif 222 223 #if defined(ARCHIVE_CRYPTO_SHA256_LIBC) 224 typedef SHA256_CTX archive_sha256_ctx; 225 #elif defined(ARCHIVE_CRYPTO_SHA256_LIBC2) 226 typedef SHA256_CTX archive_sha256_ctx; 227 #elif defined(ARCHIVE_CRYPTO_SHA256_LIBC3) 228 typedef SHA2_CTX archive_sha256_ctx; 229 #elif defined(ARCHIVE_CRYPTO_SHA256_LIBMD) 230 typedef SHA256_CTX archive_sha256_ctx; 231 #elif defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM) 232 typedef CC_SHA256_CTX archive_sha256_ctx; 233 #elif defined(ARCHIVE_CRYPTO_SHA256_MBEDTLS) 234 typedef mbedtls_sha256_context archive_sha256_ctx; 235 #elif defined(ARCHIVE_CRYPTO_SHA256_NETTLE) 236 typedef struct sha256_ctx archive_sha256_ctx; 237 #elif defined(ARCHIVE_CRYPTO_SHA256_OPENSSL) 238 typedef EVP_MD_CTX *archive_sha256_ctx; 239 #elif defined(ARCHIVE_CRYPTO_SHA256_WIN) 240 typedef Digest_CTX archive_sha256_ctx; 241 #else 242 typedef unsigned char archive_sha256_ctx; 243 #endif 244 245 #if defined(ARCHIVE_CRYPTO_SHA384_LIBC) 246 typedef SHA384_CTX archive_sha384_ctx; 247 #elif defined(ARCHIVE_CRYPTO_SHA384_LIBC2) 248 typedef SHA384_CTX archive_sha384_ctx; 249 #elif defined(ARCHIVE_CRYPTO_SHA384_LIBC3) 250 typedef SHA2_CTX archive_sha384_ctx; 251 #elif defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM) 252 typedef CC_SHA512_CTX archive_sha384_ctx; 253 #elif defined(ARCHIVE_CRYPTO_SHA384_MBEDTLS) 254 typedef mbedtls_sha512_context archive_sha384_ctx; 255 #elif defined(ARCHIVE_CRYPTO_SHA384_NETTLE) 256 typedef struct sha384_ctx archive_sha384_ctx; 257 #elif defined(ARCHIVE_CRYPTO_SHA384_OPENSSL) 258 typedef EVP_MD_CTX *archive_sha384_ctx; 259 #elif defined(ARCHIVE_CRYPTO_SHA384_WIN) 260 typedef Digest_CTX archive_sha384_ctx; 261 #else 262 typedef unsigned char archive_sha384_ctx; 263 #endif 264 265 #if defined(ARCHIVE_CRYPTO_SHA512_LIBC) 266 typedef SHA512_CTX archive_sha512_ctx; 267 #elif defined(ARCHIVE_CRYPTO_SHA512_LIBC2) 268 typedef SHA512_CTX archive_sha512_ctx; 269 #elif defined(ARCHIVE_CRYPTO_SHA512_LIBC3) 270 typedef SHA2_CTX archive_sha512_ctx; 271 #elif defined(ARCHIVE_CRYPTO_SHA512_LIBMD) 272 typedef SHA512_CTX archive_sha512_ctx; 273 #elif defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM) 274 typedef CC_SHA512_CTX archive_sha512_ctx; 275 #elif defined(ARCHIVE_CRYPTO_SHA512_MBEDTLS) 276 typedef mbedtls_sha512_context archive_sha512_ctx; 277 #elif defined(ARCHIVE_CRYPTO_SHA512_NETTLE) 278 typedef struct sha512_ctx archive_sha512_ctx; 279 #elif defined(ARCHIVE_CRYPTO_SHA512_OPENSSL) 280 typedef EVP_MD_CTX *archive_sha512_ctx; 281 #elif defined(ARCHIVE_CRYPTO_SHA512_WIN) 282 typedef Digest_CTX archive_sha512_ctx; 283 #else 284 typedef unsigned char archive_sha512_ctx; 285 #endif 286 287 /* defines */ 288 #if defined(ARCHIVE_CRYPTO_MD5_LIBC) ||\ 289 defined(ARCHIVE_CRYPTO_MD5_LIBMD) || \ 290 defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM) ||\ 291 defined(ARCHIVE_CRYPTO_MD5_MBEDTLS) ||\ 292 defined(ARCHIVE_CRYPTO_MD5_NETTLE) ||\ 293 defined(ARCHIVE_CRYPTO_MD5_OPENSSL) ||\ 294 defined(ARCHIVE_CRYPTO_MD5_WIN) 295 #define ARCHIVE_HAS_MD5 296 #endif 297 #define archive_md5_init(ctx)\ 298 __archive_digest.md5init(ctx) 299 #define archive_md5_final(ctx, md)\ 300 __archive_digest.md5final(ctx, md) 301 #define archive_md5_update(ctx, buf, n)\ 302 __archive_digest.md5update(ctx, buf, n) 303 304 #if defined(ARCHIVE_CRYPTO_RMD160_LIBC) ||\ 305 defined(ARCHIVE_CRYPTO_RMD160_MBEDTLS) ||\ 306 defined(ARCHIVE_CRYPTO_RMD160_NETTLE) ||\ 307 defined(ARCHIVE_CRYPTO_RMD160_OPENSSL) 308 #define ARCHIVE_HAS_RMD160 309 #endif 310 #define archive_rmd160_init(ctx)\ 311 __archive_digest.rmd160init(ctx) 312 #define archive_rmd160_final(ctx, md)\ 313 __archive_digest.rmd160final(ctx, md) 314 #define archive_rmd160_update(ctx, buf, n)\ 315 __archive_digest.rmd160update(ctx, buf, n) 316 317 #if defined(ARCHIVE_CRYPTO_SHA1_LIBC) ||\ 318 defined(ARCHIVE_CRYPTO_SHA1_LIBMD) || \ 319 defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM) ||\ 320 defined(ARCHIVE_CRYPTO_SHA1_MBEDTLS) ||\ 321 defined(ARCHIVE_CRYPTO_SHA1_NETTLE) ||\ 322 defined(ARCHIVE_CRYPTO_SHA1_OPENSSL) ||\ 323 defined(ARCHIVE_CRYPTO_SHA1_WIN) 324 #define ARCHIVE_HAS_SHA1 325 #endif 326 #define archive_sha1_init(ctx)\ 327 __archive_digest.sha1init(ctx) 328 #define archive_sha1_final(ctx, md)\ 329 __archive_digest.sha1final(ctx, md) 330 #define archive_sha1_update(ctx, buf, n)\ 331 __archive_digest.sha1update(ctx, buf, n) 332 333 #if defined(ARCHIVE_CRYPTO_SHA256_LIBC) ||\ 334 defined(ARCHIVE_CRYPTO_SHA256_LIBC2) ||\ 335 defined(ARCHIVE_CRYPTO_SHA256_LIBC3) ||\ 336 defined(ARCHIVE_CRYPTO_SHA256_LIBMD) ||\ 337 defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM) ||\ 338 defined(ARCHIVE_CRYPTO_SHA256_MBEDTLS) ||\ 339 defined(ARCHIVE_CRYPTO_SHA256_NETTLE) ||\ 340 defined(ARCHIVE_CRYPTO_SHA256_OPENSSL) ||\ 341 defined(ARCHIVE_CRYPTO_SHA256_WIN) 342 #define ARCHIVE_HAS_SHA256 343 #endif 344 #define archive_sha256_init(ctx)\ 345 __archive_digest.sha256init(ctx) 346 #define archive_sha256_final(ctx, md)\ 347 __archive_digest.sha256final(ctx, md) 348 #define archive_sha256_update(ctx, buf, n)\ 349 __archive_digest.sha256update(ctx, buf, n) 350 351 #if defined(ARCHIVE_CRYPTO_SHA384_LIBC) ||\ 352 defined(ARCHIVE_CRYPTO_SHA384_LIBC2) ||\ 353 defined(ARCHIVE_CRYPTO_SHA384_LIBC3) ||\ 354 defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM) ||\ 355 defined(ARCHIVE_CRYPTO_SHA384_MBEDTLS) ||\ 356 defined(ARCHIVE_CRYPTO_SHA384_NETTLE) ||\ 357 defined(ARCHIVE_CRYPTO_SHA384_OPENSSL) ||\ 358 defined(ARCHIVE_CRYPTO_SHA384_WIN) 359 #define ARCHIVE_HAS_SHA384 360 #endif 361 #define archive_sha384_init(ctx)\ 362 __archive_digest.sha384init(ctx) 363 #define archive_sha384_final(ctx, md)\ 364 __archive_digest.sha384final(ctx, md) 365 #define archive_sha384_update(ctx, buf, n)\ 366 __archive_digest.sha384update(ctx, buf, n) 367 368 #if defined(ARCHIVE_CRYPTO_SHA512_LIBC) ||\ 369 defined(ARCHIVE_CRYPTO_SHA512_LIBC2) ||\ 370 defined(ARCHIVE_CRYPTO_SHA512_LIBC3) ||\ 371 defined(ARCHIVE_CRYPTO_SHA512_LIBMD) ||\ 372 defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM) ||\ 373 defined(ARCHIVE_CRYPTO_SHA512_MBEDTLS) ||\ 374 defined(ARCHIVE_CRYPTO_SHA512_NETTLE) ||\ 375 defined(ARCHIVE_CRYPTO_SHA512_OPENSSL) ||\ 376 defined(ARCHIVE_CRYPTO_SHA512_WIN) 377 #define ARCHIVE_HAS_SHA512 378 #endif 379 #define archive_sha512_init(ctx)\ 380 __archive_digest.sha512init(ctx) 381 #define archive_sha512_final(ctx, md)\ 382 __archive_digest.sha512final(ctx, md) 383 #define archive_sha512_update(ctx, buf, n)\ 384 __archive_digest.sha512update(ctx, buf, n) 385 386 /* Minimal interface to digest functionality for internal use in libarchive */ 387 struct archive_digest 388 { 389 /* Message Digest */ 390 int (*md5init)(archive_md5_ctx *ctx); 391 int (*md5update)(archive_md5_ctx *, const void *, size_t); 392 int (*md5final)(archive_md5_ctx *, void *); 393 int (*rmd160init)(archive_rmd160_ctx *); 394 int (*rmd160update)(archive_rmd160_ctx *, const void *, size_t); 395 int (*rmd160final)(archive_rmd160_ctx *, void *); 396 int (*sha1init)(archive_sha1_ctx *); 397 int (*sha1update)(archive_sha1_ctx *, const void *, size_t); 398 int (*sha1final)(archive_sha1_ctx *, void *); 399 int (*sha256init)(archive_sha256_ctx *); 400 int (*sha256update)(archive_sha256_ctx *, const void *, size_t); 401 int (*sha256final)(archive_sha256_ctx *, void *); 402 int (*sha384init)(archive_sha384_ctx *); 403 int (*sha384update)(archive_sha384_ctx *, const void *, size_t); 404 int (*sha384final)(archive_sha384_ctx *, void *); 405 int (*sha512init)(archive_sha512_ctx *); 406 int (*sha512update)(archive_sha512_ctx *, const void *, size_t); 407 int (*sha512final)(archive_sha512_ctx *, void *); 408 }; 409 410 extern const struct archive_digest __archive_digest; 411 412 #endif 413