Blame src/hash/hash_win32.h

Packit ae9e2a
/*
Packit ae9e2a
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit ae9e2a
 *
Packit ae9e2a
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit ae9e2a
 * a Linking Exception. For full terms see the included COPYING file.
Packit ae9e2a
 */
Packit ae9e2a
Packit ae9e2a
#ifndef INCLUDE_hash_win32_h__
Packit ae9e2a
#define INCLUDE_hash_win32_h__
Packit ae9e2a
Packit ae9e2a
#include "common.h"
Packit ae9e2a
#include "hash.h"
Packit ae9e2a
Packit ae9e2a
#include <wincrypt.h>
Packit ae9e2a
#include <strsafe.h>
Packit ae9e2a
Packit ae9e2a
enum hash_win32_prov_type {
Packit ae9e2a
	INVALID = 0,
Packit ae9e2a
	CRYPTOAPI,
Packit ae9e2a
	CNG
Packit ae9e2a
};
Packit ae9e2a
Packit ae9e2a
/*
Packit ae9e2a
 * CryptoAPI is available for hashing on Windows XP and newer.
Packit ae9e2a
 */
Packit ae9e2a
Packit ae9e2a
struct hash_cryptoapi_prov {
Packit ae9e2a
	HCRYPTPROV handle;
Packit ae9e2a
};
Packit ae9e2a
Packit ae9e2a
/*
Packit ae9e2a
 * CNG (bcrypt.dll) is significantly more performant than CryptoAPI and is
Packit ae9e2a
 * preferred, however it is only available on Windows 2008 and newer and
Packit ae9e2a
 * must therefore be dynamically loaded, and we must inline constants that
Packit ae9e2a
 * would not exist when building in pre-Windows 2008 environments.
Packit ae9e2a
 */
Packit ae9e2a
Packit ae9e2a
#define GIT_HASH_CNG_DLL_NAME           "bcrypt.dll"
Packit ae9e2a
Packit ae9e2a
/* BCRYPT_SHA1_ALGORITHM */
Packit ae9e2a
#define GIT_HASH_CNG_HASH_TYPE          L"SHA1"
Packit ae9e2a
Packit ae9e2a
/* BCRYPT_OBJECT_LENGTH */
Packit ae9e2a
#define GIT_HASH_CNG_HASH_OBJECT_LEN    L"ObjectLength"
Packit ae9e2a
Packit ae9e2a
/* BCRYPT_HASH_REUSEABLE_FLAGS */
Packit ae9e2a
#define GIT_HASH_CNG_HASH_REUSABLE      0x00000020
Packit ae9e2a
Packit ae9e2a
/* Function declarations for CNG */
Packit ae9e2a
typedef NTSTATUS (WINAPI *hash_win32_cng_open_algorithm_provider_fn)(
Packit ae9e2a
	HANDLE /* BCRYPT_ALG_HANDLE */ *phAlgorithm,
Packit ae9e2a
	LPCWSTR pszAlgId,
Packit ae9e2a
	LPCWSTR pszImplementation,
Packit ae9e2a
	DWORD dwFlags);
Packit ae9e2a
Packit ae9e2a
typedef NTSTATUS (WINAPI *hash_win32_cng_get_property_fn)(
Packit ae9e2a
	HANDLE /* BCRYPT_HANDLE */ hObject,
Packit ae9e2a
	LPCWSTR pszProperty,
Packit ae9e2a
	PUCHAR pbOutput,
Packit ae9e2a
	ULONG cbOutput,
Packit ae9e2a
	ULONG *pcbResult,
Packit ae9e2a
	ULONG dwFlags);
Packit ae9e2a
Packit ae9e2a
typedef NTSTATUS (WINAPI *hash_win32_cng_create_hash_fn)(
Packit ae9e2a
	HANDLE /* BCRYPT_ALG_HANDLE */ hAlgorithm,
Packit ae9e2a
	HANDLE /* BCRYPT_HASH_HANDLE */ *phHash,
Packit ae9e2a
	PUCHAR pbHashObject, ULONG cbHashObject,
Packit ae9e2a
	PUCHAR pbSecret,
Packit ae9e2a
	ULONG cbSecret,
Packit ae9e2a
	ULONG dwFlags);
Packit ae9e2a
Packit ae9e2a
typedef NTSTATUS (WINAPI *hash_win32_cng_finish_hash_fn)(
Packit ae9e2a
	HANDLE /* BCRYPT_HASH_HANDLE */ hHash,
Packit ae9e2a
	PUCHAR pbOutput,
Packit ae9e2a
	ULONG cbOutput,
Packit ae9e2a
	ULONG dwFlags);
Packit ae9e2a
Packit ae9e2a
typedef NTSTATUS (WINAPI *hash_win32_cng_hash_data_fn)(
Packit ae9e2a
	HANDLE /* BCRYPT_HASH_HANDLE */ hHash,
Packit ae9e2a
	PUCHAR pbInput,
Packit ae9e2a
	ULONG cbInput,
Packit ae9e2a
	ULONG dwFlags);
Packit ae9e2a
Packit ae9e2a
typedef NTSTATUS (WINAPI *hash_win32_cng_destroy_hash_fn)(
Packit ae9e2a
	HANDLE /* BCRYPT_HASH_HANDLE */ hHash);
Packit ae9e2a
Packit ae9e2a
typedef NTSTATUS (WINAPI *hash_win32_cng_close_algorithm_provider_fn)(
Packit ae9e2a
	HANDLE /* BCRYPT_ALG_HANDLE */ hAlgorithm,
Packit ae9e2a
	ULONG dwFlags);
Packit ae9e2a
Packit ae9e2a
struct hash_cng_prov {
Packit ae9e2a
	/* DLL for CNG */
Packit ae9e2a
	HINSTANCE dll;
Packit ae9e2a
Packit ae9e2a
	/* Function pointers for CNG */
Packit ae9e2a
	hash_win32_cng_open_algorithm_provider_fn open_algorithm_provider;
Packit ae9e2a
	hash_win32_cng_get_property_fn get_property;
Packit ae9e2a
	hash_win32_cng_create_hash_fn create_hash;
Packit ae9e2a
	hash_win32_cng_finish_hash_fn finish_hash;
Packit ae9e2a
	hash_win32_cng_hash_data_fn hash_data;
Packit ae9e2a
	hash_win32_cng_destroy_hash_fn destroy_hash;
Packit ae9e2a
	hash_win32_cng_close_algorithm_provider_fn close_algorithm_provider;
Packit ae9e2a
Packit ae9e2a
	HANDLE /* BCRYPT_ALG_HANDLE */ handle;
Packit ae9e2a
	DWORD hash_object_size;
Packit ae9e2a
};
Packit ae9e2a
Packit ae9e2a
struct git_hash_prov {
Packit ae9e2a
	enum hash_win32_prov_type type;
Packit ae9e2a
Packit ae9e2a
	union {
Packit ae9e2a
		struct hash_cryptoapi_prov cryptoapi;
Packit ae9e2a
		struct hash_cng_prov cng;
Packit ae9e2a
	} prov;
Packit ae9e2a
};
Packit ae9e2a
Packit ae9e2a
/* Hash contexts */
Packit ae9e2a
Packit ae9e2a
struct hash_cryptoapi_ctx {
Packit ae9e2a
	bool valid;
Packit ae9e2a
	HCRYPTHASH hash_handle;
Packit ae9e2a
};
Packit ae9e2a
Packit ae9e2a
struct hash_cng_ctx {
Packit ae9e2a
	bool updated;
Packit ae9e2a
	HANDLE /* BCRYPT_HASH_HANDLE */ hash_handle;
Packit ae9e2a
	PBYTE hash_object;
Packit ae9e2a
};
Packit ae9e2a
Packit ae9e2a
struct git_hash_ctx {
Packit ae9e2a
	enum hash_win32_prov_type type;
Packit ae9e2a
	git_hash_prov *prov;
Packit ae9e2a
Packit ae9e2a
	union {
Packit ae9e2a
		struct hash_cryptoapi_ctx cryptoapi;
Packit ae9e2a
		struct hash_cng_ctx cng;
Packit ae9e2a
	} ctx;
Packit ae9e2a
};
Packit ae9e2a
Packit ae9e2a
#endif /* INCLUDE_hash_openssl_h__ */