Path: blob/master/Utilities/cmlibarchive/libarchive/archive_digest_private.h
3153 views
/*-1* Copyright (c) 2003-2007 Tim Kientzle2* Copyright (c) 2011 Andres Mejia3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR15* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES16* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.17* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,18* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT19* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,20* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY21* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT22* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF23* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.24*/2526#ifndef ARCHIVE_DIGEST_PRIVATE_H_INCLUDED27#define ARCHIVE_DIGEST_PRIVATE_H_INCLUDED2829#ifndef __LIBARCHIVE_BUILD30#error This header is only to be used internally to libarchive.31#endif32#ifndef __LIBARCHIVE_CONFIG_H_INCLUDED33#error "Should have include config.h first!"34#endif3536/*37* Crypto support in various Operating Systems:38*39* NetBSD:40* - MD5 and SHA1 in libc: without _ after algorithm name41* - SHA2 in libc: with _ after algorithm name42*43* OpenBSD:44* - MD5, SHA1 and SHA2 in libc: without _ after algorithm name45* - OpenBSD 4.4 and earlier have SHA2 in libc with _ after algorithm name46*47* DragonFly and FreeBSD:48* - MD5 libmd: without _ after algorithm name49* - SHA1, SHA256 and SHA512 in libmd: with _ after algorithm name50*51* Mac OS X (10.4 and later):52* - MD5, SHA1 and SHA2 in libSystem: with CC_ prefix and _ after algorithm name53*54* OpenSSL:55* - MD5, SHA1 and SHA2 in libcrypto: with _ after algorithm name56*57* Windows:58* - MD5, SHA1 and SHA2 in archive_crypto.c using Windows crypto API59*/6061/* libc crypto headers */62#if defined(ARCHIVE_CRYPTO_MD5_LIBC)63#include <md5.h>64#endif65#if defined(ARCHIVE_CRYPTO_RMD160_LIBC)66#include <rmd160.h>67#endif68#if defined(ARCHIVE_CRYPTO_SHA1_LIBC)69#include <sha1.h>70#endif71#if defined(ARCHIVE_CRYPTO_SHA256_LIBC) ||\72defined(ARCHIVE_CRYPTO_SHA256_LIBC2) ||\73defined(ARCHIVE_CRYPTO_SHA256_LIBC3) ||\74defined(ARCHIVE_CRYPTO_SHA384_LIBC) ||\75defined(ARCHIVE_CRYPTO_SHA384_LIBC2) ||\76defined(ARCHIVE_CRYPTO_SHA384_LIBC3) ||\77defined(ARCHIVE_CRYPTO_SHA512_LIBC) ||\78defined(ARCHIVE_CRYPTO_SHA512_LIBC2) ||\79defined(ARCHIVE_CRYPTO_SHA512_LIBC3)80#include <sha2.h>81#endif8283/* libmd crypto headers */84#if defined(ARCHIVE_CRYPTO_MD5_LIBMD) ||\85defined(ARCHIVE_CRYPTO_RMD160_LIBMD) ||\86defined(ARCHIVE_CRYPTO_SHA1_LIBMD) ||\87defined(ARCHIVE_CRYPTO_SHA256_LIBMD) ||\88defined(ARCHIVE_CRYPTO_SHA512_LIBMD)89#define ARCHIVE_CRYPTO_LIBMD 190#endif9192#if defined(ARCHIVE_CRYPTO_MD5_LIBMD)93#include <md5.h>94#endif95#if defined(ARCHIVE_CRYPTO_RMD160_LIBMD)96#include <ripemd.h>97#endif98#if defined(ARCHIVE_CRYPTO_SHA1_LIBMD)99#include <sha.h>100#endif101#if defined(ARCHIVE_CRYPTO_SHA256_LIBMD)102#include <sha256.h>103#endif104#if defined(ARCHIVE_CRYPTO_SHA512_LIBMD)105#include <sha512.h>106#endif107108/* libSystem crypto headers */109#if defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM) ||\110defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM) ||\111defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM) ||\112defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM) ||\113defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM)114#include <CommonCrypto/CommonDigest.h>115#endif116117/* mbed TLS crypto headers */118#if defined(ARCHIVE_CRYPTO_MD5_MBEDTLS)119#include <mbedtls/md5.h>120#endif121#if defined(ARCHIVE_CRYPTO_RMD160_MBEDTLS)122#include <mbedtls/ripemd160.h>123#endif124#if defined(ARCHIVE_CRYPTO_SHA1_MBEDTLS)125#include <mbedtls/sha1.h>126#endif127#if defined(ARCHIVE_CRYPTO_SHA256_MBEDTLS)128#include <mbedtls/sha256.h>129#endif130#if defined(ARCHIVE_CRYPTO_SHA384_MBEDTLS) ||\131defined(ARCHIVE_CRYPTO_SHA512_MBEDTLS)132#include <mbedtls/sha512.h>133#endif134135/* Nettle crypto headers */136#if defined(ARCHIVE_CRYPTO_MD5_NETTLE)137#include <nettle/md5.h>138#endif139#if defined(ARCHIVE_CRYPTO_RMD160_NETTLE)140#include <nettle/ripemd160.h>141#endif142#if defined(ARCHIVE_CRYPTO_SHA1_NETTLE) ||\143defined(ARCHIVE_CRYPTO_SHA256_NETTLE) ||\144defined(ARCHIVE_CRYPTO_SHA384_NETTLE) ||\145defined(ARCHIVE_CRYPTO_SHA512_NETTLE)146#include <nettle/sha.h>147#endif148149/* OpenSSL crypto headers */150#if defined(ARCHIVE_CRYPTO_MD5_OPENSSL) ||\151defined(ARCHIVE_CRYPTO_RMD160_OPENSSL) ||\152defined(ARCHIVE_CRYPTO_SHA1_OPENSSL) ||\153defined(ARCHIVE_CRYPTO_SHA256_OPENSSL) ||\154defined(ARCHIVE_CRYPTO_SHA384_OPENSSL) ||\155defined(ARCHIVE_CRYPTO_SHA512_OPENSSL)156#define ARCHIVE_CRYPTO_OPENSSL 1157#include "archive_openssl_evp_private.h"158#endif159160/* Windows crypto headers */161#if defined(ARCHIVE_CRYPTO_MD5_WIN) ||\162defined(ARCHIVE_CRYPTO_SHA1_WIN) ||\163defined(ARCHIVE_CRYPTO_SHA256_WIN) ||\164defined(ARCHIVE_CRYPTO_SHA384_WIN) ||\165defined(ARCHIVE_CRYPTO_SHA512_WIN)166#if defined(HAVE_BCRYPT_H) && _WIN32_WINNT >= _WIN32_WINNT_VISTA167/* don't use bcrypt when XP needs to be supported */168#include <bcrypt.h>169typedef struct {170int valid;171BCRYPT_ALG_HANDLE hAlg;172BCRYPT_HASH_HANDLE hHash;173} Digest_CTX;174#else175#include <windows.h>176#include <wincrypt.h>177typedef struct {178int valid;179HCRYPTPROV cryptProv;180HCRYPTHASH hash;181} Digest_CTX;182#endif183#endif184185/* typedefs */186#if defined(ARCHIVE_CRYPTO_MD5_LIBC)187typedef MD5_CTX archive_md5_ctx;188#elif defined(ARCHIVE_CRYPTO_MD5_LIBMD)189typedef MD5_CTX archive_md5_ctx;190#elif defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM)191typedef CC_MD5_CTX archive_md5_ctx;192#elif defined(ARCHIVE_CRYPTO_MD5_MBEDTLS)193typedef mbedtls_md5_context archive_md5_ctx;194#elif defined(ARCHIVE_CRYPTO_MD5_NETTLE)195typedef struct md5_ctx archive_md5_ctx;196#elif defined(ARCHIVE_CRYPTO_MD5_OPENSSL)197typedef EVP_MD_CTX *archive_md5_ctx;198#elif defined(ARCHIVE_CRYPTO_MD5_WIN)199typedef Digest_CTX archive_md5_ctx;200#else201typedef unsigned char archive_md5_ctx;202#endif203204#if defined(ARCHIVE_CRYPTO_RMD160_LIBC)205typedef RMD160_CTX archive_rmd160_ctx;206#elif defined(ARCHIVE_CRYPTO_RMD160_LIBMD)207typedef RIPEMD160_CTX archive_rmd160_ctx;208#elif defined(ARCHIVE_CRYPTO_RMD160_MBEDTLS)209typedef mbedtls_ripemd160_context archive_rmd160_ctx;210#elif defined(ARCHIVE_CRYPTO_RMD160_NETTLE)211typedef struct ripemd160_ctx archive_rmd160_ctx;212#elif defined(ARCHIVE_CRYPTO_RMD160_OPENSSL)213typedef EVP_MD_CTX *archive_rmd160_ctx;214#else215typedef unsigned char archive_rmd160_ctx;216#endif217218#if defined(ARCHIVE_CRYPTO_SHA1_LIBC)219typedef SHA1_CTX archive_sha1_ctx;220#elif defined(ARCHIVE_CRYPTO_SHA1_LIBMD)221typedef SHA1_CTX archive_sha1_ctx;222#elif defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM)223typedef CC_SHA1_CTX archive_sha1_ctx;224#elif defined(ARCHIVE_CRYPTO_SHA1_MBEDTLS)225typedef mbedtls_sha1_context archive_sha1_ctx;226#elif defined(ARCHIVE_CRYPTO_SHA1_NETTLE)227typedef struct sha1_ctx archive_sha1_ctx;228#elif defined(ARCHIVE_CRYPTO_SHA1_OPENSSL)229typedef EVP_MD_CTX *archive_sha1_ctx;230#elif defined(ARCHIVE_CRYPTO_SHA1_WIN)231typedef Digest_CTX archive_sha1_ctx;232#else233typedef unsigned char archive_sha1_ctx;234#endif235236#if defined(ARCHIVE_CRYPTO_SHA256_LIBC)237typedef SHA256_CTX archive_sha256_ctx;238#elif defined(ARCHIVE_CRYPTO_SHA256_LIBC2)239typedef SHA256_CTX archive_sha256_ctx;240#elif defined(ARCHIVE_CRYPTO_SHA256_LIBC3)241typedef SHA2_CTX archive_sha256_ctx;242#elif defined(ARCHIVE_CRYPTO_SHA256_LIBMD)243typedef SHA256_CTX archive_sha256_ctx;244#elif defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM)245typedef CC_SHA256_CTX archive_sha256_ctx;246#elif defined(ARCHIVE_CRYPTO_SHA256_MBEDTLS)247typedef mbedtls_sha256_context archive_sha256_ctx;248#elif defined(ARCHIVE_CRYPTO_SHA256_NETTLE)249typedef struct sha256_ctx archive_sha256_ctx;250#elif defined(ARCHIVE_CRYPTO_SHA256_OPENSSL)251typedef EVP_MD_CTX *archive_sha256_ctx;252#elif defined(ARCHIVE_CRYPTO_SHA256_WIN)253typedef Digest_CTX archive_sha256_ctx;254#else255typedef unsigned char archive_sha256_ctx;256#endif257258#if defined(ARCHIVE_CRYPTO_SHA384_LIBC)259typedef SHA384_CTX archive_sha384_ctx;260#elif defined(ARCHIVE_CRYPTO_SHA384_LIBC2)261typedef SHA384_CTX archive_sha384_ctx;262#elif defined(ARCHIVE_CRYPTO_SHA384_LIBC3)263typedef SHA2_CTX archive_sha384_ctx;264#elif defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM)265typedef CC_SHA512_CTX archive_sha384_ctx;266#elif defined(ARCHIVE_CRYPTO_SHA384_MBEDTLS)267typedef mbedtls_sha512_context archive_sha384_ctx;268#elif defined(ARCHIVE_CRYPTO_SHA384_NETTLE)269typedef struct sha384_ctx archive_sha384_ctx;270#elif defined(ARCHIVE_CRYPTO_SHA384_OPENSSL)271typedef EVP_MD_CTX *archive_sha384_ctx;272#elif defined(ARCHIVE_CRYPTO_SHA384_WIN)273typedef Digest_CTX archive_sha384_ctx;274#else275typedef unsigned char archive_sha384_ctx;276#endif277278#if defined(ARCHIVE_CRYPTO_SHA512_LIBC)279typedef SHA512_CTX archive_sha512_ctx;280#elif defined(ARCHIVE_CRYPTO_SHA512_LIBC2)281typedef SHA512_CTX archive_sha512_ctx;282#elif defined(ARCHIVE_CRYPTO_SHA512_LIBC3)283typedef SHA2_CTX archive_sha512_ctx;284#elif defined(ARCHIVE_CRYPTO_SHA512_LIBMD)285typedef SHA512_CTX archive_sha512_ctx;286#elif defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM)287typedef CC_SHA512_CTX archive_sha512_ctx;288#elif defined(ARCHIVE_CRYPTO_SHA512_MBEDTLS)289typedef mbedtls_sha512_context archive_sha512_ctx;290#elif defined(ARCHIVE_CRYPTO_SHA512_NETTLE)291typedef struct sha512_ctx archive_sha512_ctx;292#elif defined(ARCHIVE_CRYPTO_SHA512_OPENSSL)293typedef EVP_MD_CTX *archive_sha512_ctx;294#elif defined(ARCHIVE_CRYPTO_SHA512_WIN)295typedef Digest_CTX archive_sha512_ctx;296#else297typedef unsigned char archive_sha512_ctx;298#endif299300/* defines */301#if defined(ARCHIVE_CRYPTO_MD5_LIBC) ||\302defined(ARCHIVE_CRYPTO_MD5_LIBMD) || \303defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM) ||\304defined(ARCHIVE_CRYPTO_MD5_MBEDTLS) ||\305defined(ARCHIVE_CRYPTO_MD5_NETTLE) ||\306defined(ARCHIVE_CRYPTO_MD5_OPENSSL) ||\307defined(ARCHIVE_CRYPTO_MD5_WIN)308#define ARCHIVE_HAS_MD5309#endif310#define archive_md5_init(ctx)\311__archive_digest.md5init(ctx)312#define archive_md5_final(ctx, md)\313__archive_digest.md5final(ctx, md)314#define archive_md5_update(ctx, buf, n)\315__archive_digest.md5update(ctx, buf, n)316317#if defined(ARCHIVE_CRYPTO_RMD160_LIBC) ||\318defined(ARCHIVE_CRYPTO_RMD160_MBEDTLS) ||\319defined(ARCHIVE_CRYPTO_RMD160_NETTLE) ||\320defined(ARCHIVE_CRYPTO_RMD160_OPENSSL)321#define ARCHIVE_HAS_RMD160322#endif323#define archive_rmd160_init(ctx)\324__archive_digest.rmd160init(ctx)325#define archive_rmd160_final(ctx, md)\326__archive_digest.rmd160final(ctx, md)327#define archive_rmd160_update(ctx, buf, n)\328__archive_digest.rmd160update(ctx, buf, n)329330#if defined(ARCHIVE_CRYPTO_SHA1_LIBC) ||\331defined(ARCHIVE_CRYPTO_SHA1_LIBMD) || \332defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM) ||\333defined(ARCHIVE_CRYPTO_SHA1_MBEDTLS) ||\334defined(ARCHIVE_CRYPTO_SHA1_NETTLE) ||\335defined(ARCHIVE_CRYPTO_SHA1_OPENSSL) ||\336defined(ARCHIVE_CRYPTO_SHA1_WIN)337#define ARCHIVE_HAS_SHA1338#endif339#define archive_sha1_init(ctx)\340__archive_digest.sha1init(ctx)341#define archive_sha1_final(ctx, md)\342__archive_digest.sha1final(ctx, md)343#define archive_sha1_update(ctx, buf, n)\344__archive_digest.sha1update(ctx, buf, n)345346#if defined(ARCHIVE_CRYPTO_SHA256_LIBC) ||\347defined(ARCHIVE_CRYPTO_SHA256_LIBC2) ||\348defined(ARCHIVE_CRYPTO_SHA256_LIBC3) ||\349defined(ARCHIVE_CRYPTO_SHA256_LIBMD) ||\350defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM) ||\351defined(ARCHIVE_CRYPTO_SHA256_MBEDTLS) ||\352defined(ARCHIVE_CRYPTO_SHA256_NETTLE) ||\353defined(ARCHIVE_CRYPTO_SHA256_OPENSSL) ||\354defined(ARCHIVE_CRYPTO_SHA256_WIN)355#define ARCHIVE_HAS_SHA256356#endif357#define archive_sha256_init(ctx)\358__archive_digest.sha256init(ctx)359#define archive_sha256_final(ctx, md)\360__archive_digest.sha256final(ctx, md)361#define archive_sha256_update(ctx, buf, n)\362__archive_digest.sha256update(ctx, buf, n)363364#if defined(ARCHIVE_CRYPTO_SHA384_LIBC) ||\365defined(ARCHIVE_CRYPTO_SHA384_LIBC2) ||\366defined(ARCHIVE_CRYPTO_SHA384_LIBC3) ||\367defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM) ||\368defined(ARCHIVE_CRYPTO_SHA384_MBEDTLS) ||\369defined(ARCHIVE_CRYPTO_SHA384_NETTLE) ||\370defined(ARCHIVE_CRYPTO_SHA384_OPENSSL) ||\371defined(ARCHIVE_CRYPTO_SHA384_WIN)372#define ARCHIVE_HAS_SHA384373#endif374#define archive_sha384_init(ctx)\375__archive_digest.sha384init(ctx)376#define archive_sha384_final(ctx, md)\377__archive_digest.sha384final(ctx, md)378#define archive_sha384_update(ctx, buf, n)\379__archive_digest.sha384update(ctx, buf, n)380381#if defined(ARCHIVE_CRYPTO_SHA512_LIBC) ||\382defined(ARCHIVE_CRYPTO_SHA512_LIBC2) ||\383defined(ARCHIVE_CRYPTO_SHA512_LIBC3) ||\384defined(ARCHIVE_CRYPTO_SHA512_LIBMD) ||\385defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM) ||\386defined(ARCHIVE_CRYPTO_SHA512_MBEDTLS) ||\387defined(ARCHIVE_CRYPTO_SHA512_NETTLE) ||\388defined(ARCHIVE_CRYPTO_SHA512_OPENSSL) ||\389defined(ARCHIVE_CRYPTO_SHA512_WIN)390#define ARCHIVE_HAS_SHA512391#endif392#define archive_sha512_init(ctx)\393__archive_digest.sha512init(ctx)394#define archive_sha512_final(ctx, md)\395__archive_digest.sha512final(ctx, md)396#define archive_sha512_update(ctx, buf, n)\397__archive_digest.sha512update(ctx, buf, n)398399/* Minimal interface to digest functionality for internal use in libarchive */400struct archive_digest401{402/* Message Digest */403int (*md5init)(archive_md5_ctx *ctx);404int (*md5update)(archive_md5_ctx *, const void *, size_t);405int (*md5final)(archive_md5_ctx *, void *);406int (*rmd160init)(archive_rmd160_ctx *);407int (*rmd160update)(archive_rmd160_ctx *, const void *, size_t);408int (*rmd160final)(archive_rmd160_ctx *, void *);409int (*sha1init)(archive_sha1_ctx *);410int (*sha1update)(archive_sha1_ctx *, const void *, size_t);411int (*sha1final)(archive_sha1_ctx *, void *);412int (*sha256init)(archive_sha256_ctx *);413int (*sha256update)(archive_sha256_ctx *, const void *, size_t);414int (*sha256final)(archive_sha256_ctx *, void *);415int (*sha384init)(archive_sha384_ctx *);416int (*sha384update)(archive_sha384_ctx *, const void *, size_t);417int (*sha384final)(archive_sha384_ctx *, void *);418int (*sha512init)(archive_sha512_ctx *);419int (*sha512update)(archive_sha512_ctx *, const void *, size_t);420int (*sha512final)(archive_sha512_ctx *, void *);421};422423extern const struct archive_digest __archive_digest;424425#endif426427428