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 "archive_openssl_evp_private.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 <windows.h> 147 #include <wincrypt.h> 148 typedef struct { 149 int valid; 150 HCRYPTPROV cryptProv; 151 HCRYPTHASH hash; 152 } Digest_CTX; 153 #endif 154 155 /* typedefs */ 156 #if defined(ARCHIVE_CRYPTO_MD5_LIBC) 157 typedef MD5_CTX archive_md5_ctx; 158 #elif defined(ARCHIVE_CRYPTO_MD5_LIBMD) 159 typedef MD5_CTX archive_md5_ctx; 160 #elif defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM) 161 typedef CC_MD5_CTX archive_md5_ctx; 162 #elif defined(ARCHIVE_CRYPTO_MD5_NETTLE) 163 typedef struct md5_ctx archive_md5_ctx; 164 #elif defined(ARCHIVE_CRYPTO_MD5_OPENSSL) 165 typedef EVP_MD_CTX *archive_md5_ctx; 166 #elif defined(ARCHIVE_CRYPTO_MD5_WIN) 167 typedef Digest_CTX archive_md5_ctx; 168 #else 169 typedef unsigned char archive_md5_ctx; 170 #endif 171 172 #if defined(ARCHIVE_CRYPTO_RMD160_LIBC) 173 typedef RMD160_CTX archive_rmd160_ctx; 174 #elif defined(ARCHIVE_CRYPTO_RMD160_LIBMD) 175 typedef RIPEMD160_CTX archive_rmd160_ctx; 176 #elif defined(ARCHIVE_CRYPTO_RMD160_NETTLE) 177 typedef struct ripemd160_ctx archive_rmd160_ctx; 178 #elif defined(ARCHIVE_CRYPTO_RMD160_OPENSSL) 179 typedef EVP_MD_CTX *archive_rmd160_ctx; 180 #else 181 typedef unsigned char archive_rmd160_ctx; 182 #endif 183 184 #if defined(ARCHIVE_CRYPTO_SHA1_LIBC) 185 typedef SHA1_CTX archive_sha1_ctx; 186 #elif defined(ARCHIVE_CRYPTO_SHA1_LIBMD) 187 typedef SHA1_CTX archive_sha1_ctx; 188 #elif defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM) 189 typedef CC_SHA1_CTX archive_sha1_ctx; 190 #elif defined(ARCHIVE_CRYPTO_SHA1_NETTLE) 191 typedef struct sha1_ctx archive_sha1_ctx; 192 #elif defined(ARCHIVE_CRYPTO_SHA1_OPENSSL) 193 typedef EVP_MD_CTX *archive_sha1_ctx; 194 #elif defined(ARCHIVE_CRYPTO_SHA1_WIN) 195 typedef Digest_CTX archive_sha1_ctx; 196 #else 197 typedef unsigned char archive_sha1_ctx; 198 #endif 199 200 #if defined(ARCHIVE_CRYPTO_SHA256_LIBC) 201 typedef SHA256_CTX archive_sha256_ctx; 202 #elif defined(ARCHIVE_CRYPTO_SHA256_LIBC2) 203 typedef SHA256_CTX archive_sha256_ctx; 204 #elif defined(ARCHIVE_CRYPTO_SHA256_LIBC3) 205 typedef SHA2_CTX archive_sha256_ctx; 206 #elif defined(ARCHIVE_CRYPTO_SHA256_LIBMD) 207 typedef SHA256_CTX archive_sha256_ctx; 208 #elif defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM) 209 typedef CC_SHA256_CTX archive_sha256_ctx; 210 #elif defined(ARCHIVE_CRYPTO_SHA256_NETTLE) 211 typedef struct sha256_ctx archive_sha256_ctx; 212 #elif defined(ARCHIVE_CRYPTO_SHA256_OPENSSL) 213 typedef EVP_MD_CTX *archive_sha256_ctx; 214 #elif defined(ARCHIVE_CRYPTO_SHA256_WIN) 215 typedef Digest_CTX archive_sha256_ctx; 216 #else 217 typedef unsigned char archive_sha256_ctx; 218 #endif 219 220 #if defined(ARCHIVE_CRYPTO_SHA384_LIBC) 221 typedef SHA384_CTX archive_sha384_ctx; 222 #elif defined(ARCHIVE_CRYPTO_SHA384_LIBC2) 223 typedef SHA384_CTX archive_sha384_ctx; 224 #elif defined(ARCHIVE_CRYPTO_SHA384_LIBC3) 225 typedef SHA2_CTX archive_sha384_ctx; 226 #elif defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM) 227 typedef CC_SHA512_CTX archive_sha384_ctx; 228 #elif defined(ARCHIVE_CRYPTO_SHA384_NETTLE) 229 typedef struct sha384_ctx archive_sha384_ctx; 230 #elif defined(ARCHIVE_CRYPTO_SHA384_OPENSSL) 231 typedef EVP_MD_CTX *archive_sha384_ctx; 232 #elif defined(ARCHIVE_CRYPTO_SHA384_WIN) 233 typedef Digest_CTX archive_sha384_ctx; 234 #else 235 typedef unsigned char archive_sha384_ctx; 236 #endif 237 238 #if defined(ARCHIVE_CRYPTO_SHA512_LIBC) 239 typedef SHA512_CTX archive_sha512_ctx; 240 #elif defined(ARCHIVE_CRYPTO_SHA512_LIBC2) 241 typedef SHA512_CTX archive_sha512_ctx; 242 #elif defined(ARCHIVE_CRYPTO_SHA512_LIBC3) 243 typedef SHA2_CTX archive_sha512_ctx; 244 #elif defined(ARCHIVE_CRYPTO_SHA512_LIBMD) 245 typedef SHA512_CTX archive_sha512_ctx; 246 #elif defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM) 247 typedef CC_SHA512_CTX archive_sha512_ctx; 248 #elif defined(ARCHIVE_CRYPTO_SHA512_NETTLE) 249 typedef struct sha512_ctx archive_sha512_ctx; 250 #elif defined(ARCHIVE_CRYPTO_SHA512_OPENSSL) 251 typedef EVP_MD_CTX *archive_sha512_ctx; 252 #elif defined(ARCHIVE_CRYPTO_SHA512_WIN) 253 typedef Digest_CTX archive_sha512_ctx; 254 #else 255 typedef unsigned char archive_sha512_ctx; 256 #endif 257 258 /* defines */ 259 #if defined(ARCHIVE_CRYPTO_MD5_LIBC) ||\ 260 defined(ARCHIVE_CRYPTO_MD5_LIBMD) || \ 261 defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM) ||\ 262 defined(ARCHIVE_CRYPTO_MD5_NETTLE) ||\ 263 defined(ARCHIVE_CRYPTO_MD5_OPENSSL) ||\ 264 defined(ARCHIVE_CRYPTO_MD5_WIN) 265 #define ARCHIVE_HAS_MD5 266 #endif 267 #define archive_md5_init(ctx)\ 268 __archive_digest.md5init(ctx) 269 #define archive_md5_final(ctx, md)\ 270 __archive_digest.md5final(ctx, md) 271 #define archive_md5_update(ctx, buf, n)\ 272 __archive_digest.md5update(ctx, buf, n) 273 274 #if defined(ARCHIVE_CRYPTO_RMD160_LIBC) ||\ 275 defined(ARCHIVE_CRYPTO_RMD160_NETTLE) ||\ 276 defined(ARCHIVE_CRYPTO_RMD160_OPENSSL) 277 #define ARCHIVE_HAS_RMD160 278 #endif 279 #define archive_rmd160_init(ctx)\ 280 __archive_digest.rmd160init(ctx) 281 #define archive_rmd160_final(ctx, md)\ 282 __archive_digest.rmd160final(ctx, md) 283 #define archive_rmd160_update(ctx, buf, n)\ 284 __archive_digest.rmd160update(ctx, buf, n) 285 286 #if defined(ARCHIVE_CRYPTO_SHA1_LIBC) ||\ 287 defined(ARCHIVE_CRYPTO_SHA1_LIBMD) || \ 288 defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM) ||\ 289 defined(ARCHIVE_CRYPTO_SHA1_NETTLE) ||\ 290 defined(ARCHIVE_CRYPTO_SHA1_OPENSSL) ||\ 291 defined(ARCHIVE_CRYPTO_SHA1_WIN) 292 #define ARCHIVE_HAS_SHA1 293 #endif 294 #define archive_sha1_init(ctx)\ 295 __archive_digest.sha1init(ctx) 296 #define archive_sha1_final(ctx, md)\ 297 __archive_digest.sha1final(ctx, md) 298 #define archive_sha1_update(ctx, buf, n)\ 299 __archive_digest.sha1update(ctx, buf, n) 300 301 #if defined(ARCHIVE_CRYPTO_SHA256_LIBC) ||\ 302 defined(ARCHIVE_CRYPTO_SHA256_LIBC2) ||\ 303 defined(ARCHIVE_CRYPTO_SHA256_LIBC3) ||\ 304 defined(ARCHIVE_CRYPTO_SHA256_LIBMD) ||\ 305 defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM) ||\ 306 defined(ARCHIVE_CRYPTO_SHA256_NETTLE) ||\ 307 defined(ARCHIVE_CRYPTO_SHA256_OPENSSL) ||\ 308 defined(ARCHIVE_CRYPTO_SHA256_WIN) 309 #define ARCHIVE_HAS_SHA256 310 #endif 311 #define archive_sha256_init(ctx)\ 312 __archive_digest.sha256init(ctx) 313 #define archive_sha256_final(ctx, md)\ 314 __archive_digest.sha256final(ctx, md) 315 #define archive_sha256_update(ctx, buf, n)\ 316 __archive_digest.sha256update(ctx, buf, n) 317 318 #if defined(ARCHIVE_CRYPTO_SHA384_LIBC) ||\ 319 defined(ARCHIVE_CRYPTO_SHA384_LIBC2) ||\ 320 defined(ARCHIVE_CRYPTO_SHA384_LIBC3) ||\ 321 defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM) ||\ 322 defined(ARCHIVE_CRYPTO_SHA384_NETTLE) ||\ 323 defined(ARCHIVE_CRYPTO_SHA384_OPENSSL) ||\ 324 defined(ARCHIVE_CRYPTO_SHA384_WIN) 325 #define ARCHIVE_HAS_SHA384 326 #endif 327 #define archive_sha384_init(ctx)\ 328 __archive_digest.sha384init(ctx) 329 #define archive_sha384_final(ctx, md)\ 330 __archive_digest.sha384final(ctx, md) 331 #define archive_sha384_update(ctx, buf, n)\ 332 __archive_digest.sha384update(ctx, buf, n) 333 334 #if defined(ARCHIVE_CRYPTO_SHA512_LIBC) ||\ 335 defined(ARCHIVE_CRYPTO_SHA512_LIBC2) ||\ 336 defined(ARCHIVE_CRYPTO_SHA512_LIBC3) ||\ 337 defined(ARCHIVE_CRYPTO_SHA512_LIBMD) ||\ 338 defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM) ||\ 339 defined(ARCHIVE_CRYPTO_SHA512_NETTLE) ||\ 340 defined(ARCHIVE_CRYPTO_SHA512_OPENSSL) ||\ 341 defined(ARCHIVE_CRYPTO_SHA512_WIN) 342 #define ARCHIVE_HAS_SHA512 343 #endif 344 #define archive_sha512_init(ctx)\ 345 __archive_digest.sha512init(ctx) 346 #define archive_sha512_final(ctx, md)\ 347 __archive_digest.sha512final(ctx, md) 348 #define archive_sha512_update(ctx, buf, n)\ 349 __archive_digest.sha512update(ctx, buf, n) 350 351 /* Minimal interface to digest functionality for internal use in libarchive */ 352 struct archive_digest 353 { 354 /* Message Digest */ 355 int (*md5init)(archive_md5_ctx *ctx); 356 int (*md5update)(archive_md5_ctx *, const void *, size_t); 357 int (*md5final)(archive_md5_ctx *, void *); 358 int (*rmd160init)(archive_rmd160_ctx *); 359 int (*rmd160update)(archive_rmd160_ctx *, const void *, size_t); 360 int (*rmd160final)(archive_rmd160_ctx *, void *); 361 int (*sha1init)(archive_sha1_ctx *); 362 int (*sha1update)(archive_sha1_ctx *, const void *, size_t); 363 int (*sha1final)(archive_sha1_ctx *, void *); 364 int (*sha256init)(archive_sha256_ctx *); 365 int (*sha256update)(archive_sha256_ctx *, const void *, size_t); 366 int (*sha256final)(archive_sha256_ctx *, void *); 367 int (*sha384init)(archive_sha384_ctx *); 368 int (*sha384update)(archive_sha384_ctx *, const void *, size_t); 369 int (*sha384final)(archive_sha384_ctx *, void *); 370 int (*sha512init)(archive_sha512_ctx *); 371 int (*sha512update)(archive_sha512_ctx *, const void *, size_t); 372 int (*sha512final)(archive_sha512_ctx *, void *); 373 }; 374 375 extern const struct archive_digest __archive_digest; 376 377 #endif 378