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 __LIBARCHIVE_BUILD 28 #error This header is only to be used internally to libarchive. 29 #endif 30 31 #ifndef ARCHIVE_CRYPTO_PRIVATE_H_INCLUDED 32 #define ARCHIVE_CRYPTO_PRIVATE_H_INCLUDED 33 34 /* 35 * Crypto support in various Operating Systems: 36 * 37 * NetBSD: 38 * - MD5 and SHA1 in libc: without _ after algorithm name 39 * - SHA2 in libc: with _ after algorithm name 40 * 41 * OpenBSD: 42 * - MD5, SHA1 and SHA2 in libc: without _ after algorithm name 43 * - OpenBSD 4.4 and earlier have SHA2 in libc with _ after algorithm name 44 * 45 * DragonFly and FreeBSD: 46 * - MD5 libmd: without _ after algorithm name 47 * - SHA1, SHA256 and SHA512 in libmd: with _ after algorithm name 48 * 49 * Mac OS X (10.4 and later): 50 * - MD5, SHA1 and SHA2 in libSystem: with CC_ prefix and _ after algorithm name 51 * 52 * OpenSSL: 53 * - MD5, SHA1 and SHA2 in libcrypto: with _ after algorithm name 54 * 55 * Windows: 56 * - MD5, SHA1 and SHA2 in archive_crypto.c using Windows crypto API 57 */ 58 59 /* libc crypto headers */ 60 #if defined(ARCHIVE_CRYPTO_MD5_LIBC) 61 #include <md5.h> 62 #endif 63 #if defined(ARCHIVE_CRYPTO_RMD160_LIBC) 64 #include <rmd160.h> 65 #endif 66 #if defined(ARCHIVE_CRYPTO_SHA1_LIBC) 67 #include <sha1.h> 68 #endif 69 #if defined(ARCHIVE_CRYPTO_SHA256_LIBC) ||\ 70 defined(ARCHIVE_CRYPTO_SHA256_LIBC2) ||\ 71 defined(ARCHIVE_CRYPTO_SHA256_LIBC3) ||\ 72 defined(ARCHIVE_CRYPTO_SHA384_LIBC) ||\ 73 defined(ARCHIVE_CRYPTO_SHA384_LIBC2) ||\ 74 defined(ARCHIVE_CRYPTO_SHA384_LIBC3) ||\ 75 defined(ARCHIVE_CRYPTO_SHA512_LIBC) ||\ 76 defined(ARCHIVE_CRYPTO_SHA512_LIBC2) ||\ 77 defined(ARCHIVE_CRYPTO_SHA512_LIBC3) 78 #include <sha2.h> 79 #endif 80 81 /* libmd crypto headers */ 82 #if defined(ARCHIVE_CRYPTO_MD5_LIBMD) ||\ 83 defined(ARCHIVE_CRYPTO_RMD160_LIBMD) ||\ 84 defined(ARCHIVE_CRYPTO_SHA1_LIBMD) ||\ 85 defined(ARCHIVE_CRYPTO_SHA256_LIBMD) ||\ 86 defined(ARCHIVE_CRYPTO_SHA512_LIBMD) 87 #define ARCHIVE_CRYPTO_LIBMD 1 88 #endif 89 90 #if defined(ARCHIVE_CRYPTO_MD5_LIBMD) 91 #include <md5.h> 92 #endif 93 #if defined(ARCHIVE_CRYPTO_RMD160_LIBMD) 94 #include <ripemd.h> 95 #endif 96 #if defined(ARCHIVE_CRYPTO_SHA1_LIBMD) 97 #include <sha.h> 98 #endif 99 #if defined(ARCHIVE_CRYPTO_SHA256_LIBMD) 100 #include <sha256.h> 101 #endif 102 #if defined(ARCHIVE_CRYPTO_SHA512_LIBMD) 103 #include <sha512.h> 104 #endif 105 106 /* libSystem crypto headers */ 107 #if defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM) ||\ 108 defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM) ||\ 109 defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM) ||\ 110 defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM) ||\ 111 defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM) 112 #include <CommonCrypto/CommonDigest.h> 113 #endif 114 115 /* Nettle crypto headers */ 116 #if defined(ARCHIVE_CRYPTO_MD5_NETTLE) 117 #include <nettle/md5.h> 118 #endif 119 #if defined(ARCHIVE_CRYPTO_RMD160_NETTLE) 120 #include <nettle/ripemd160.h> 121 #endif 122 #if defined(ARCHIVE_CRYPTO_SHA1_NETTLE) ||\ 123 defined(ARCHIVE_CRYPTO_SHA256_NETTLE) ||\ 124 defined(ARCHIVE_CRYPTO_SHA384_NETTLE) ||\ 125 defined(ARCHIVE_CRYPTO_SHA512_NETTLE) 126 #include <nettle/sha.h> 127 #endif 128 129 /* OpenSSL crypto headers */ 130 #if defined(ARCHIVE_CRYPTO_MD5_OPENSSL) ||\ 131 defined(ARCHIVE_CRYPTO_RMD160_OPENSSL) ||\ 132 defined(ARCHIVE_CRYPTO_SHA1_OPENSSL) ||\ 133 defined(ARCHIVE_CRYPTO_SHA256_OPENSSL) ||\ 134 defined(ARCHIVE_CRYPTO_SHA384_OPENSSL) ||\ 135 defined(ARCHIVE_CRYPTO_SHA512_OPENSSL) 136 #define ARCHIVE_CRYPTO_OPENSSL 1 137 #include <openssl/evp.h> 138 #endif 139 140 /* Windows crypto headers */ 141 #if defined(ARCHIVE_CRYPTO_MD5_WIN) ||\ 142 defined(ARCHIVE_CRYPTO_SHA1_WIN) ||\ 143 defined(ARCHIVE_CRYPTO_SHA256_WIN) ||\ 144 defined(ARCHIVE_CRYPTO_SHA384_WIN) ||\ 145 defined(ARCHIVE_CRYPTO_SHA512_WIN) 146 #include <wincrypt.h> 147 typedef struct { 148 int valid; 149 HCRYPTPROV cryptProv; 150 HCRYPTHASH hash; 151 } Digest_CTX; 152 #endif 153 154 /* typedefs */ 155 #if defined(ARCHIVE_CRYPTO_MD5_LIBC) 156 typedef MD5_CTX archive_md5_ctx; 157 #elif defined(ARCHIVE_CRYPTO_MD5_LIBMD) 158 typedef MD5_CTX archive_md5_ctx; 159 #elif defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM) 160 typedef CC_MD5_CTX archive_md5_ctx; 161 #elif defined(ARCHIVE_CRYPTO_MD5_NETTLE) 162 typedef struct md5_ctx archive_md5_ctx; 163 #elif defined(ARCHIVE_CRYPTO_MD5_OPENSSL) 164 typedef EVP_MD_CTX archive_md5_ctx; 165 #elif defined(ARCHIVE_CRYPTO_MD5_WIN) 166 typedef Digest_CTX archive_md5_ctx; 167 #else 168 typedef unsigned char archive_md5_ctx; 169 #endif 170 171 #if defined(ARCHIVE_CRYPTO_RMD160_LIBC) 172 typedef RMD160_CTX archive_rmd160_ctx; 173 #elif defined(ARCHIVE_CRYPTO_RMD160_LIBMD) 174 typedef RIPEMD160_CTX archive_rmd160_ctx; 175 #elif defined(ARCHIVE_CRYPTO_RMD160_NETTLE) 176 typedef struct ripemd160_ctx archive_rmd160_ctx; 177 #elif defined(ARCHIVE_CRYPTO_RMD160_OPENSSL) 178 typedef EVP_MD_CTX archive_rmd160_ctx; 179 #else 180 typedef unsigned char archive_rmd160_ctx; 181 #endif 182 183 #if defined(ARCHIVE_CRYPTO_SHA1_LIBC) 184 typedef SHA1_CTX archive_sha1_ctx; 185 #elif defined(ARCHIVE_CRYPTO_SHA1_LIBMD) 186 typedef SHA1_CTX archive_sha1_ctx; 187 #elif defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM) 188 typedef CC_SHA1_CTX archive_sha1_ctx; 189 #elif defined(ARCHIVE_CRYPTO_SHA1_NETTLE) 190 typedef struct sha1_ctx archive_sha1_ctx; 191 #elif defined(ARCHIVE_CRYPTO_SHA1_OPENSSL) 192 typedef EVP_MD_CTX archive_sha1_ctx; 193 #elif defined(ARCHIVE_CRYPTO_SHA1_WIN) 194 typedef Digest_CTX archive_sha1_ctx; 195 #else 196 typedef unsigned char archive_sha1_ctx; 197 #endif 198 199 #if defined(ARCHIVE_CRYPTO_SHA256_LIBC) 200 typedef SHA256_CTX archive_sha256_ctx; 201 #elif defined(ARCHIVE_CRYPTO_SHA256_LIBC2) 202 typedef SHA256_CTX archive_sha256_ctx; 203 #elif defined(ARCHIVE_CRYPTO_SHA256_LIBC3) 204 typedef SHA2_CTX archive_sha256_ctx; 205 #elif defined(ARCHIVE_CRYPTO_SHA256_LIBMD) 206 typedef SHA256_CTX archive_sha256_ctx; 207 #elif defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM) 208 typedef CC_SHA256_CTX archive_sha256_ctx; 209 #elif defined(ARCHIVE_CRYPTO_SHA256_NETTLE) 210 typedef struct sha256_ctx archive_sha256_ctx; 211 #elif defined(ARCHIVE_CRYPTO_SHA256_OPENSSL) 212 typedef EVP_MD_CTX archive_sha256_ctx; 213 #elif defined(ARCHIVE_CRYPTO_SHA256_WIN) 214 typedef Digest_CTX archive_sha256_ctx; 215 #else 216 typedef unsigned char archive_sha256_ctx; 217 #endif 218 219 #if defined(ARCHIVE_CRYPTO_SHA384_LIBC) 220 typedef SHA384_CTX archive_sha384_ctx; 221 #elif defined(ARCHIVE_CRYPTO_SHA384_LIBC2) 222 typedef SHA384_CTX archive_sha384_ctx; 223 #elif defined(ARCHIVE_CRYPTO_SHA384_LIBC3) 224 typedef SHA2_CTX archive_sha384_ctx; 225 #elif defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM) 226 typedef CC_SHA512_CTX archive_sha384_ctx; 227 #elif defined(ARCHIVE_CRYPTO_SHA384_NETTLE) 228 typedef struct sha384_ctx archive_sha384_ctx; 229 #elif defined(ARCHIVE_CRYPTO_SHA384_OPENSSL) 230 typedef EVP_MD_CTX archive_sha384_ctx; 231 #elif defined(ARCHIVE_CRYPTO_SHA384_WIN) 232 typedef Digest_CTX archive_sha384_ctx; 233 #else 234 typedef unsigned char archive_sha384_ctx; 235 #endif 236 237 #if defined(ARCHIVE_CRYPTO_SHA512_LIBC) 238 typedef SHA512_CTX archive_sha512_ctx; 239 #elif defined(ARCHIVE_CRYPTO_SHA512_LIBC2) 240 typedef SHA512_CTX archive_sha512_ctx; 241 #elif defined(ARCHIVE_CRYPTO_SHA512_LIBC3) 242 typedef SHA2_CTX archive_sha512_ctx; 243 #elif defined(ARCHIVE_CRYPTO_SHA512_LIBMD) 244 typedef SHA512_CTX archive_sha512_ctx; 245 #elif defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM) 246 typedef CC_SHA512_CTX archive_sha512_ctx; 247 #elif defined(ARCHIVE_CRYPTO_SHA512_NETTLE) 248 typedef struct sha512_ctx archive_sha512_ctx; 249 #elif defined(ARCHIVE_CRYPTO_SHA512_OPENSSL) 250 typedef EVP_MD_CTX archive_sha512_ctx; 251 #elif defined(ARCHIVE_CRYPTO_SHA512_WIN) 252 typedef Digest_CTX archive_sha512_ctx; 253 #else 254 typedef unsigned char archive_sha512_ctx; 255 #endif 256 257 /* defines */ 258 #if defined(ARCHIVE_CRYPTO_MD5_LIBC) ||\ 259 defined(ARCHIVE_CRYPTO_MD5_LIBMD) || \ 260 defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM) ||\ 261 defined(ARCHIVE_CRYPTO_MD5_NETTLE) ||\ 262 defined(ARCHIVE_CRYPTO_MD5_OPENSSL) ||\ 263 defined(ARCHIVE_CRYPTO_MD5_WIN) 264 #define ARCHIVE_HAS_MD5 265 #endif 266 #define archive_md5_init(ctx)\ 267 __archive_digest.md5init(ctx) 268 #define archive_md5_final(ctx, md)\ 269 __archive_digest.md5final(ctx, md) 270 #define archive_md5_update(ctx, buf, n)\ 271 __archive_digest.md5update(ctx, buf, n) 272 273 #if defined(ARCHIVE_CRYPTO_RMD160_LIBC) ||\ 274 defined(ARCHIVE_CRYPTO_RMD160_NETTLE) ||\ 275 defined(ARCHIVE_CRYPTO_RMD160_OPENSSL) 276 #define ARCHIVE_HAS_RMD160 277 #endif 278 #define archive_rmd160_init(ctx)\ 279 __archive_digest.rmd160init(ctx) 280 #define archive_rmd160_final(ctx, md)\ 281 __archive_digest.rmd160final(ctx, md) 282 #define archive_rmd160_update(ctx, buf, n)\ 283 __archive_digest.rmd160update(ctx, buf, n) 284 285 #if defined(ARCHIVE_CRYPTO_SHA1_LIBC) ||\ 286 defined(ARCHIVE_CRYPTO_SHA1_LIBMD) || \ 287 defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM) ||\ 288 defined(ARCHIVE_CRYPTO_SHA1_NETTLE) ||\ 289 defined(ARCHIVE_CRYPTO_SHA1_OPENSSL) ||\ 290 defined(ARCHIVE_CRYPTO_SHA1_WIN) 291 #define ARCHIVE_HAS_SHA1 292 #endif 293 #define archive_sha1_init(ctx)\ 294 __archive_digest.sha1init(ctx) 295 #define archive_sha1_final(ctx, md)\ 296 __archive_digest.sha1final(ctx, md) 297 #define archive_sha1_update(ctx, buf, n)\ 298 __archive_digest.sha1update(ctx, buf, n) 299 300 #if defined(ARCHIVE_CRYPTO_SHA256_LIBC) ||\ 301 defined(ARCHIVE_CRYPTO_SHA256_LIBC2) ||\ 302 defined(ARCHIVE_CRYPTO_SHA256_LIBC3) ||\ 303 defined(ARCHIVE_CRYPTO_SHA256_LIBMD) ||\ 304 defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM) ||\ 305 defined(ARCHIVE_CRYPTO_SHA256_NETTLE) ||\ 306 defined(ARCHIVE_CRYPTO_SHA256_OPENSSL) ||\ 307 defined(ARCHIVE_CRYPTO_SHA256_WIN) 308 #define ARCHIVE_HAS_SHA256 309 #endif 310 #define archive_sha256_init(ctx)\ 311 __archive_digest.sha256init(ctx) 312 #define archive_sha256_final(ctx, md)\ 313 __archive_digest.sha256final(ctx, md) 314 #define archive_sha256_update(ctx, buf, n)\ 315 __archive_digest.sha256update(ctx, buf, n) 316 317 #if defined(ARCHIVE_CRYPTO_SHA384_LIBC) ||\ 318 defined(ARCHIVE_CRYPTO_SHA384_LIBC2) ||\ 319 defined(ARCHIVE_CRYPTO_SHA384_LIBC3) ||\ 320 defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM) ||\ 321 defined(ARCHIVE_CRYPTO_SHA384_NETTLE) ||\ 322 defined(ARCHIVE_CRYPTO_SHA384_OPENSSL) ||\ 323 defined(ARCHIVE_CRYPTO_SHA384_WIN) 324 #define ARCHIVE_HAS_SHA384 325 #endif 326 #define archive_sha384_init(ctx)\ 327 __archive_digest.sha384init(ctx) 328 #define archive_sha384_final(ctx, md)\ 329 __archive_digest.sha384final(ctx, md) 330 #define archive_sha384_update(ctx, buf, n)\ 331 __archive_digest.sha384update(ctx, buf, n) 332 333 #if defined(ARCHIVE_CRYPTO_SHA512_LIBC) ||\ 334 defined(ARCHIVE_CRYPTO_SHA512_LIBC2) ||\ 335 defined(ARCHIVE_CRYPTO_SHA512_LIBC3) ||\ 336 defined(ARCHIVE_CRYPTO_SHA512_LIBMD) ||\ 337 defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM) ||\ 338 defined(ARCHIVE_CRYPTO_SHA512_NETTLE) ||\ 339 defined(ARCHIVE_CRYPTO_SHA512_OPENSSL) ||\ 340 defined(ARCHIVE_CRYPTO_SHA512_WIN) 341 #define ARCHIVE_HAS_SHA512 342 #endif 343 #define archive_sha512_init(ctx)\ 344 __archive_digest.sha512init(ctx) 345 #define archive_sha512_final(ctx, md)\ 346 __archive_digest.sha512final(ctx, md) 347 #define archive_sha512_update(ctx, buf, n)\ 348 __archive_digest.sha512update(ctx, buf, n) 349 350 /* Minimal interface to digest functionality for internal use in libarchive */ 351 struct archive_digest 352 { 353 /* Message Digest */ 354 int (*md5init)(archive_md5_ctx *ctx); 355 int (*md5update)(archive_md5_ctx *, const void *, size_t); 356 int (*md5final)(archive_md5_ctx *, void *); 357 int (*rmd160init)(archive_rmd160_ctx *); 358 int (*rmd160update)(archive_rmd160_ctx *, const void *, size_t); 359 int (*rmd160final)(archive_rmd160_ctx *, void *); 360 int (*sha1init)(archive_sha1_ctx *); 361 int (*sha1update)(archive_sha1_ctx *, const void *, size_t); 362 int (*sha1final)(archive_sha1_ctx *, void *); 363 int (*sha256init)(archive_sha256_ctx *); 364 int (*sha256update)(archive_sha256_ctx *, const void *, size_t); 365 int (*sha256final)(archive_sha256_ctx *, void *); 366 int (*sha384init)(archive_sha384_ctx *); 367 int (*sha384update)(archive_sha384_ctx *, const void *, size_t); 368 int (*sha384final)(archive_sha384_ctx *, void *); 369 int (*sha512init)(archive_sha512_ctx *); 370 int (*sha512update)(archive_sha512_ctx *, const void *, size_t); 371 int (*sha512final)(archive_sha512_ctx *, void *); 372 }; 373 374 extern const struct archive_digest __archive_digest; 375 376 #endif 377