Path: blob/main/contrib/libarchive/libarchive/archive_digest_private.h
39478 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#define ARCHIVE_CRYPTO_CommonCrypto 1116#endif117118/* mbed TLS crypto headers */119#if defined(ARCHIVE_CRYPTO_MD5_MBEDTLS)120#include <mbedtls/md5.h>121#endif122#if defined(ARCHIVE_CRYPTO_RMD160_MBEDTLS)123#include <mbedtls/ripemd160.h>124#endif125#if defined(ARCHIVE_CRYPTO_SHA1_MBEDTLS)126#include <mbedtls/sha1.h>127#endif128#if defined(ARCHIVE_CRYPTO_SHA256_MBEDTLS)129#include <mbedtls/sha256.h>130#endif131#if defined(ARCHIVE_CRYPTO_SHA384_MBEDTLS) ||\132defined(ARCHIVE_CRYPTO_SHA512_MBEDTLS)133#include <mbedtls/sha512.h>134#endif135136/* Nettle crypto headers */137#if defined(ARCHIVE_CRYPTO_MD5_NETTLE)138#include <nettle/md5.h>139#endif140#if defined(ARCHIVE_CRYPTO_RMD160_NETTLE)141#include <nettle/ripemd160.h>142#endif143#if defined(ARCHIVE_CRYPTO_SHA1_NETTLE) ||\144defined(ARCHIVE_CRYPTO_SHA256_NETTLE) ||\145defined(ARCHIVE_CRYPTO_SHA384_NETTLE) ||\146defined(ARCHIVE_CRYPTO_SHA512_NETTLE)147#include <nettle/sha.h>148#endif149150/* OpenSSL crypto headers */151#if defined(ARCHIVE_CRYPTO_MD5_OPENSSL) ||\152defined(ARCHIVE_CRYPTO_RMD160_OPENSSL) ||\153defined(ARCHIVE_CRYPTO_SHA1_OPENSSL) ||\154defined(ARCHIVE_CRYPTO_SHA256_OPENSSL) ||\155defined(ARCHIVE_CRYPTO_SHA384_OPENSSL) ||\156defined(ARCHIVE_CRYPTO_SHA512_OPENSSL)157#define ARCHIVE_CRYPTO_OPENSSL 1158#include "archive_openssl_evp_private.h"159#endif160161/* Windows crypto headers */162#if defined(ARCHIVE_CRYPTO_MD5_WIN) ||\163defined(ARCHIVE_CRYPTO_SHA1_WIN) ||\164defined(ARCHIVE_CRYPTO_SHA256_WIN) ||\165defined(ARCHIVE_CRYPTO_SHA384_WIN) ||\166defined(ARCHIVE_CRYPTO_SHA512_WIN)167#if defined(HAVE_BCRYPT_H) && _WIN32_WINNT >= _WIN32_WINNT_VISTA168/* don't use bcrypt when XP needs to be supported */169#include <bcrypt.h>170#define ARCHIVE_CRYPTO_CNG 1171typedef struct {172int valid;173BCRYPT_ALG_HANDLE hAlg;174BCRYPT_HASH_HANDLE hHash;175} Digest_CTX;176#else177#include <windows.h>178#include <wincrypt.h>179#define ARCHIVE_CRYPTO_WINCRYPT 1180typedef struct {181int valid;182HCRYPTPROV cryptProv;183HCRYPTHASH hash;184} Digest_CTX;185#endif186#endif187188/* typedefs */189#if defined(ARCHIVE_CRYPTO_MD5_LIBC)190typedef MD5_CTX archive_md5_ctx;191#elif defined(ARCHIVE_CRYPTO_MD5_LIBMD)192typedef MD5_CTX archive_md5_ctx;193#elif defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM)194typedef CC_MD5_CTX archive_md5_ctx;195#elif defined(ARCHIVE_CRYPTO_MD5_WIN)196typedef Digest_CTX archive_md5_ctx;197#elif defined(ARCHIVE_CRYPTO_MD5_MBEDTLS)198#define ARCHIVE_CRYPTO_MBED 1199typedef mbedtls_md5_context archive_md5_ctx;200#elif defined(ARCHIVE_CRYPTO_MD5_NETTLE)201#define ARCHIVE_CRYPTO_NETTLE 1202typedef struct md5_ctx archive_md5_ctx;203#elif defined(ARCHIVE_CRYPTO_MD5_OPENSSL)204typedef EVP_MD_CTX *archive_md5_ctx;205#else206typedef unsigned char archive_md5_ctx;207#endif208209#if defined(ARCHIVE_CRYPTO_RMD160_LIBC)210typedef RMD160_CTX archive_rmd160_ctx;211#elif defined(ARCHIVE_CRYPTO_RMD160_LIBMD)212typedef RIPEMD160_CTX archive_rmd160_ctx;213#elif defined(ARCHIVE_CRYPTO_RMD160_MBEDTLS)214#define ARCHIVE_CRYPTO_MBED 1215typedef mbedtls_ripemd160_context archive_rmd160_ctx;216#elif defined(ARCHIVE_CRYPTO_RMD160_NETTLE)217#define ARCHIVE_CRYPTO_NETTLE 1218typedef struct ripemd160_ctx archive_rmd160_ctx;219#elif defined(ARCHIVE_CRYPTO_RMD160_OPENSSL)220typedef EVP_MD_CTX *archive_rmd160_ctx;221#else222typedef unsigned char archive_rmd160_ctx;223#endif224225#if defined(ARCHIVE_CRYPTO_SHA1_LIBC)226typedef SHA1_CTX archive_sha1_ctx;227#elif defined(ARCHIVE_CRYPTO_SHA1_LIBMD)228typedef SHA1_CTX archive_sha1_ctx;229#elif defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM)230typedef CC_SHA1_CTX archive_sha1_ctx;231#elif defined(ARCHIVE_CRYPTO_SHA1_WIN)232typedef Digest_CTX archive_sha1_ctx;233#elif defined(ARCHIVE_CRYPTO_SHA1_MBEDTLS)234#define ARCHIVE_CRYPTO_MBED 1235typedef mbedtls_sha1_context archive_sha1_ctx;236#elif defined(ARCHIVE_CRYPTO_SHA1_NETTLE)237#define ARCHIVE_CRYPTO_NETTLE 1238typedef struct sha1_ctx archive_sha1_ctx;239#elif defined(ARCHIVE_CRYPTO_SHA1_OPENSSL)240typedef EVP_MD_CTX *archive_sha1_ctx;241#else242typedef unsigned char archive_sha1_ctx;243#endif244245#if defined(ARCHIVE_CRYPTO_SHA256_LIBC)246typedef SHA256_CTX archive_sha256_ctx;247#elif defined(ARCHIVE_CRYPTO_SHA256_LIBC2)248typedef SHA256_CTX archive_sha256_ctx;249#elif defined(ARCHIVE_CRYPTO_SHA256_LIBC3)250typedef SHA2_CTX archive_sha256_ctx;251#elif defined(ARCHIVE_CRYPTO_SHA256_LIBMD)252typedef SHA256_CTX archive_sha256_ctx;253#elif defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM)254typedef CC_SHA256_CTX archive_sha256_ctx;255#elif defined(ARCHIVE_CRYPTO_SHA256_WIN)256typedef Digest_CTX archive_sha256_ctx;257#elif defined(ARCHIVE_CRYPTO_SHA256_MBEDTLS)258#define ARCHIVE_CRYPTO_MBED 1259typedef mbedtls_sha256_context archive_sha256_ctx;260#elif defined(ARCHIVE_CRYPTO_SHA256_NETTLE)261#define ARCHIVE_CRYPTO_NETTLE 1262typedef struct sha256_ctx archive_sha256_ctx;263#elif defined(ARCHIVE_CRYPTO_SHA256_OPENSSL)264typedef EVP_MD_CTX *archive_sha256_ctx;265#else266typedef unsigned char archive_sha256_ctx;267#endif268269#if defined(ARCHIVE_CRYPTO_SHA384_LIBC)270typedef SHA384_CTX archive_sha384_ctx;271#elif defined(ARCHIVE_CRYPTO_SHA384_LIBC2)272typedef SHA384_CTX archive_sha384_ctx;273#elif defined(ARCHIVE_CRYPTO_SHA384_LIBC3)274typedef SHA2_CTX archive_sha384_ctx;275#elif defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM)276typedef CC_SHA512_CTX archive_sha384_ctx;277#elif defined(ARCHIVE_CRYPTO_SHA384_WIN)278typedef Digest_CTX archive_sha384_ctx;279#elif defined(ARCHIVE_CRYPTO_SHA384_MBEDTLS)280#define ARCHIVE_CRYPTO_MBED 1281typedef mbedtls_sha512_context archive_sha384_ctx;282#elif defined(ARCHIVE_CRYPTO_SHA384_NETTLE)283#define ARCHIVE_CRYPTO_NETTLE 1284typedef struct sha384_ctx archive_sha384_ctx;285#elif defined(ARCHIVE_CRYPTO_SHA384_OPENSSL)286typedef EVP_MD_CTX *archive_sha384_ctx;287#else288typedef unsigned char archive_sha384_ctx;289#endif290291#if defined(ARCHIVE_CRYPTO_SHA512_LIBC)292typedef SHA512_CTX archive_sha512_ctx;293#elif defined(ARCHIVE_CRYPTO_SHA512_LIBC2)294typedef SHA512_CTX archive_sha512_ctx;295#elif defined(ARCHIVE_CRYPTO_SHA512_LIBC3)296typedef SHA2_CTX archive_sha512_ctx;297#elif defined(ARCHIVE_CRYPTO_SHA512_LIBMD)298typedef SHA512_CTX archive_sha512_ctx;299#elif defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM)300typedef CC_SHA512_CTX archive_sha512_ctx;301#elif defined(ARCHIVE_CRYPTO_SHA512_WIN)302typedef Digest_CTX archive_sha512_ctx;303#elif defined(ARCHIVE_CRYPTO_SHA512_MBEDTLS)304#define ARCHIVE_CRYPTO_MBED 1305typedef mbedtls_sha512_context archive_sha512_ctx;306#elif defined(ARCHIVE_CRYPTO_SHA512_NETTLE)307#define ARCHIVE_CRYPTO_NETTLE 1308typedef struct sha512_ctx archive_sha512_ctx;309#elif defined(ARCHIVE_CRYPTO_SHA512_OPENSSL)310typedef EVP_MD_CTX *archive_sha512_ctx;311#else312typedef unsigned char archive_sha512_ctx;313#endif314315/* defines */316#if defined(ARCHIVE_CRYPTO_MD5_LIBC) ||\317defined(ARCHIVE_CRYPTO_MD5_LIBMD) || \318defined(ARCHIVE_CRYPTO_MD5_LIBSYSTEM) ||\319defined(ARCHIVE_CRYPTO_MD5_MBEDTLS) ||\320defined(ARCHIVE_CRYPTO_MD5_NETTLE) ||\321defined(ARCHIVE_CRYPTO_MD5_OPENSSL) ||\322defined(ARCHIVE_CRYPTO_MD5_WIN)323#define ARCHIVE_HAS_MD5324#endif325#define archive_md5_init(ctx)\326__archive_digest.md5init(ctx)327#define archive_md5_final(ctx, md)\328__archive_digest.md5final(ctx, md)329#define archive_md5_update(ctx, buf, n)\330__archive_digest.md5update(ctx, buf, n)331332#if defined(ARCHIVE_CRYPTO_RMD160_LIBC) ||\333defined(ARCHIVE_CRYPTO_RMD160_MBEDTLS) ||\334defined(ARCHIVE_CRYPTO_RMD160_NETTLE) ||\335defined(ARCHIVE_CRYPTO_RMD160_OPENSSL)336#define ARCHIVE_HAS_RMD160337#endif338#define archive_rmd160_init(ctx)\339__archive_digest.rmd160init(ctx)340#define archive_rmd160_final(ctx, md)\341__archive_digest.rmd160final(ctx, md)342#define archive_rmd160_update(ctx, buf, n)\343__archive_digest.rmd160update(ctx, buf, n)344345#if defined(ARCHIVE_CRYPTO_SHA1_LIBC) ||\346defined(ARCHIVE_CRYPTO_SHA1_LIBMD) || \347defined(ARCHIVE_CRYPTO_SHA1_LIBSYSTEM) ||\348defined(ARCHIVE_CRYPTO_SHA1_MBEDTLS) ||\349defined(ARCHIVE_CRYPTO_SHA1_NETTLE) ||\350defined(ARCHIVE_CRYPTO_SHA1_OPENSSL) ||\351defined(ARCHIVE_CRYPTO_SHA1_WIN)352#define ARCHIVE_HAS_SHA1353#endif354#define archive_sha1_init(ctx)\355__archive_digest.sha1init(ctx)356#define archive_sha1_final(ctx, md)\357__archive_digest.sha1final(ctx, md)358#define archive_sha1_update(ctx, buf, n)\359__archive_digest.sha1update(ctx, buf, n)360361#if defined(ARCHIVE_CRYPTO_SHA256_LIBC) ||\362defined(ARCHIVE_CRYPTO_SHA256_LIBC2) ||\363defined(ARCHIVE_CRYPTO_SHA256_LIBC3) ||\364defined(ARCHIVE_CRYPTO_SHA256_LIBMD) ||\365defined(ARCHIVE_CRYPTO_SHA256_LIBSYSTEM) ||\366defined(ARCHIVE_CRYPTO_SHA256_MBEDTLS) ||\367defined(ARCHIVE_CRYPTO_SHA256_NETTLE) ||\368defined(ARCHIVE_CRYPTO_SHA256_OPENSSL) ||\369defined(ARCHIVE_CRYPTO_SHA256_WIN)370#define ARCHIVE_HAS_SHA256371#endif372#define archive_sha256_init(ctx)\373__archive_digest.sha256init(ctx)374#define archive_sha256_final(ctx, md)\375__archive_digest.sha256final(ctx, md)376#define archive_sha256_update(ctx, buf, n)\377__archive_digest.sha256update(ctx, buf, n)378379#if defined(ARCHIVE_CRYPTO_SHA384_LIBC) ||\380defined(ARCHIVE_CRYPTO_SHA384_LIBC2) ||\381defined(ARCHIVE_CRYPTO_SHA384_LIBC3) ||\382defined(ARCHIVE_CRYPTO_SHA384_LIBSYSTEM) ||\383defined(ARCHIVE_CRYPTO_SHA384_MBEDTLS) ||\384defined(ARCHIVE_CRYPTO_SHA384_NETTLE) ||\385defined(ARCHIVE_CRYPTO_SHA384_OPENSSL) ||\386defined(ARCHIVE_CRYPTO_SHA384_WIN)387#define ARCHIVE_HAS_SHA384388#endif389#define archive_sha384_init(ctx)\390__archive_digest.sha384init(ctx)391#define archive_sha384_final(ctx, md)\392__archive_digest.sha384final(ctx, md)393#define archive_sha384_update(ctx, buf, n)\394__archive_digest.sha384update(ctx, buf, n)395396#if defined(ARCHIVE_CRYPTO_SHA512_LIBC) ||\397defined(ARCHIVE_CRYPTO_SHA512_LIBC2) ||\398defined(ARCHIVE_CRYPTO_SHA512_LIBC3) ||\399defined(ARCHIVE_CRYPTO_SHA512_LIBMD) ||\400defined(ARCHIVE_CRYPTO_SHA512_LIBSYSTEM) ||\401defined(ARCHIVE_CRYPTO_SHA512_MBEDTLS) ||\402defined(ARCHIVE_CRYPTO_SHA512_NETTLE) ||\403defined(ARCHIVE_CRYPTO_SHA512_OPENSSL) ||\404defined(ARCHIVE_CRYPTO_SHA512_WIN)405#define ARCHIVE_HAS_SHA512406#endif407#define archive_sha512_init(ctx)\408__archive_digest.sha512init(ctx)409#define archive_sha512_final(ctx, md)\410__archive_digest.sha512final(ctx, md)411#define archive_sha512_update(ctx, buf, n)\412__archive_digest.sha512update(ctx, buf, n)413414/* Minimal interface to digest functionality for internal use in libarchive */415struct archive_digest416{417/* Message Digest */418int (*md5init)(archive_md5_ctx *ctx);419int (*md5update)(archive_md5_ctx *, const void *, size_t);420int (*md5final)(archive_md5_ctx *, void *);421int (*rmd160init)(archive_rmd160_ctx *);422int (*rmd160update)(archive_rmd160_ctx *, const void *, size_t);423int (*rmd160final)(archive_rmd160_ctx *, void *);424int (*sha1init)(archive_sha1_ctx *);425int (*sha1update)(archive_sha1_ctx *, const void *, size_t);426int (*sha1final)(archive_sha1_ctx *, void *);427int (*sha256init)(archive_sha256_ctx *);428int (*sha256update)(archive_sha256_ctx *, const void *, size_t);429int (*sha256final)(archive_sha256_ctx *, void *);430int (*sha384init)(archive_sha384_ctx *);431int (*sha384update)(archive_sha384_ctx *, const void *, size_t);432int (*sha384final)(archive_sha384_ctx *, void *);433int (*sha512init)(archive_sha512_ctx *);434int (*sha512update)(archive_sha512_ctx *, const void *, size_t);435int (*sha512final)(archive_sha512_ctx *, void *);436};437438extern const struct archive_digest __archive_digest;439440#endif441442443