2014-08-25 10:18:34 +00:00
|
|
|
/*
|
2014-05-06 14:23:05 +00:00
|
|
|
* s3fs - FUSE-based file system backed by Amazon S3
|
|
|
|
*
|
2017-05-07 11:24:17 +00:00
|
|
|
* Copyright(C) 2007 Randy Rizun <rrizun@gmail.com>
|
2014-05-06 14:23:05 +00:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2023-11-19 01:00:16 +00:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstring>
|
2014-05-06 14:23:05 +00:00
|
|
|
#include <pthread.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <gcrypt.h>
|
|
|
|
#include <gnutls/gnutls.h>
|
|
|
|
#include <gnutls/crypto.h>
|
2020-08-22 12:40:53 +00:00
|
|
|
#ifdef USE_GNUTLS_NETTLE
|
2014-05-06 14:23:05 +00:00
|
|
|
#include <nettle/md5.h>
|
|
|
|
#include <nettle/sha1.h>
|
|
|
|
#include <nettle/hmac.h>
|
|
|
|
#endif
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "common.h"
|
2020-08-22 12:40:53 +00:00
|
|
|
#include "s3fs.h"
|
2014-05-06 14:23:05 +00:00
|
|
|
#include "s3fs_auth.h"
|
2022-07-30 03:06:47 +00:00
|
|
|
#include "s3fs_logger.h"
|
2014-05-06 14:23:05 +00:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
// Utility Function for version
|
|
|
|
//-------------------------------------------------------------------
|
2020-08-22 12:40:53 +00:00
|
|
|
#ifdef USE_GNUTLS_NETTLE
|
2014-05-06 14:23:05 +00:00
|
|
|
|
|
|
|
const char* s3fs_crypt_lib_name(void)
|
|
|
|
{
|
2023-11-14 13:15:17 +00:00
|
|
|
static constexpr char version[] = "GnuTLS(nettle)";
|
2014-05-06 14:23:05 +00:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
return version;
|
2014-05-06 14:23:05 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
#else // USE_GNUTLS_NETTLE
|
2014-05-06 14:23:05 +00:00
|
|
|
|
2019-01-23 23:44:50 +00:00
|
|
|
const char* s3fs_crypt_lib_name()
|
2014-05-06 14:23:05 +00:00
|
|
|
{
|
2023-11-14 13:15:17 +00:00
|
|
|
static constexpr char version[] = "GnuTLS(gcrypt)";
|
2014-05-06 14:23:05 +00:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
return version;
|
2014-05-06 14:23:05 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
#endif // USE_GNUTLS_NETTLE
|
2014-05-06 14:23:05 +00:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
// Utility Function for global init
|
|
|
|
//-------------------------------------------------------------------
|
2019-01-23 23:44:50 +00:00
|
|
|
bool s3fs_init_global_ssl()
|
2014-05-06 14:23:05 +00:00
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
if(GNUTLS_E_SUCCESS != gnutls_global_init()){
|
|
|
|
return false;
|
|
|
|
}
|
2018-06-21 17:40:10 +00:00
|
|
|
#ifndef USE_GNUTLS_NETTLE
|
2023-07-27 12:56:58 +00:00
|
|
|
if(nullptr == gcry_check_version(nullptr)){
|
2020-08-22 12:40:53 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif // USE_GNUTLS_NETTLE
|
|
|
|
return true;
|
2014-05-06 14:23:05 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 23:44:50 +00:00
|
|
|
bool s3fs_destroy_global_ssl()
|
2014-05-06 14:23:05 +00:00
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
gnutls_global_deinit();
|
|
|
|
return true;
|
2014-05-06 14:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
// Utility Function for crypt lock
|
|
|
|
//-------------------------------------------------------------------
|
2019-01-23 23:44:50 +00:00
|
|
|
bool s3fs_init_crypt_mutex()
|
2014-05-06 14:23:05 +00:00
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
return true;
|
2014-05-06 14:23:05 +00:00
|
|
|
}
|
|
|
|
|
2019-01-23 23:44:50 +00:00
|
|
|
bool s3fs_destroy_crypt_mutex()
|
2014-05-06 14:23:05 +00:00
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
return true;
|
2014-05-06 14:23:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
// Utility Function for HMAC
|
|
|
|
//-------------------------------------------------------------------
|
2020-08-22 12:40:53 +00:00
|
|
|
#ifdef USE_GNUTLS_NETTLE
|
2014-05-06 14:23:05 +00:00
|
|
|
|
2023-08-19 14:29:00 +00:00
|
|
|
std::unique_ptr<unsigned char[]> s3fs_HMAC(const void* key, size_t keylen, const unsigned char* data, size_t datalen, unsigned int* digestlen)
|
2014-05-06 14:23:05 +00:00
|
|
|
{
|
2023-08-19 14:29:00 +00:00
|
|
|
if(!key || !data || !digestlen){
|
|
|
|
return nullptr;
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
2014-05-06 14:23:05 +00:00
|
|
|
|
2023-08-19 14:29:00 +00:00
|
|
|
std::unique_ptr<unsigned char[]> digest(new unsigned char[SHA1_DIGEST_SIZE]);
|
2014-05-06 14:23:05 +00:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
struct hmac_sha1_ctx ctx_hmac;
|
|
|
|
hmac_sha1_set_key(&ctx_hmac, keylen, reinterpret_cast<const uint8_t*>(key));
|
|
|
|
hmac_sha1_update(&ctx_hmac, datalen, reinterpret_cast<const uint8_t*>(data));
|
2023-08-19 14:29:00 +00:00
|
|
|
hmac_sha1_digest(&ctx_hmac, SHA1_DIGEST_SIZE, reinterpret_cast<uint8_t*>(digest.get()));
|
2020-08-22 12:40:53 +00:00
|
|
|
*digestlen = SHA1_DIGEST_SIZE;
|
2014-05-06 14:23:05 +00:00
|
|
|
|
2023-08-19 14:29:00 +00:00
|
|
|
return digest;
|
2014-05-06 14:23:05 +00:00
|
|
|
}
|
|
|
|
|
2023-08-19 14:29:00 +00:00
|
|
|
std::unique_ptr<unsigned char[]> s3fs_HMAC256(const void* key, size_t keylen, const unsigned char* data, size_t datalen, unsigned int* digestlen)
|
2015-02-02 16:36:08 +00:00
|
|
|
{
|
2023-08-19 14:29:00 +00:00
|
|
|
if(!key || !data || !digestlen){
|
|
|
|
return nullptr;
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
2015-02-02 16:36:08 +00:00
|
|
|
|
2023-08-19 14:29:00 +00:00
|
|
|
std::unique_ptr<unsigned char[]> digest(new unsigned char[SHA256_DIGEST_SIZE]);
|
2015-02-02 16:36:08 +00:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
struct hmac_sha256_ctx ctx_hmac;
|
|
|
|
hmac_sha256_set_key(&ctx_hmac, keylen, reinterpret_cast<const uint8_t*>(key));
|
|
|
|
hmac_sha256_update(&ctx_hmac, datalen, reinterpret_cast<const uint8_t*>(data));
|
2023-08-19 14:29:00 +00:00
|
|
|
hmac_sha256_digest(&ctx_hmac, SHA256_DIGEST_SIZE, reinterpret_cast<uint8_t*>(digest.get()));
|
2020-08-22 12:40:53 +00:00
|
|
|
*digestlen = SHA256_DIGEST_SIZE;
|
2015-02-02 16:36:08 +00:00
|
|
|
|
2023-08-19 14:29:00 +00:00
|
|
|
return digest;
|
2015-02-02 16:36:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
#else // USE_GNUTLS_NETTLE
|
2014-05-06 14:23:05 +00:00
|
|
|
|
2023-08-19 14:29:00 +00:00
|
|
|
std::unique_ptr<unsigned char[]> s3fs_HMAC(const void* key, size_t keylen, const unsigned char* data, size_t datalen, unsigned int* digestlen)
|
2014-05-06 14:23:05 +00:00
|
|
|
{
|
2023-08-19 14:29:00 +00:00
|
|
|
if(!key || !data || !digestlen){
|
|
|
|
return nullptr;
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(0 == (*digestlen = gnutls_hmac_get_len(GNUTLS_MAC_SHA1))){
|
2023-08-19 14:29:00 +00:00
|
|
|
return nullptr;
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
2023-08-19 14:29:00 +00:00
|
|
|
std::unique_ptr<unsigned char[]> digest(new unsigned char[*digestlen + 1]);
|
|
|
|
if(0 > gnutls_hmac_fast(GNUTLS_MAC_SHA1, key, keylen, data, datalen, digest.get())){
|
|
|
|
return nullptr;
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
2023-08-19 14:29:00 +00:00
|
|
|
return digest;
|
2014-05-06 14:23:05 +00:00
|
|
|
}
|
|
|
|
|
2023-08-19 14:29:00 +00:00
|
|
|
std::unique_ptr<unsigned char[]> s3fs_HMAC256(const void* key, size_t keylen, const unsigned char* data, size_t datalen, unsigned int* digestlen)
|
2015-02-02 16:36:08 +00:00
|
|
|
{
|
2023-08-19 14:29:00 +00:00
|
|
|
if(!key || !data || !digestlen){
|
|
|
|
return nullptr;
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(0 == (*digestlen = gnutls_hmac_get_len(GNUTLS_MAC_SHA256))){
|
2023-08-19 14:29:00 +00:00
|
|
|
return nullptr;
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
2023-08-19 14:29:00 +00:00
|
|
|
std::unique_ptr<unsigned char[]> digest(new unsigned char[*digestlen + 1]);
|
|
|
|
if(0 > gnutls_hmac_fast(GNUTLS_MAC_SHA256, key, keylen, data, datalen, digest.get())){
|
|
|
|
return nullptr;
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
2023-08-19 14:29:00 +00:00
|
|
|
return digest;
|
2015-02-02 16:36:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
#endif // USE_GNUTLS_NETTLE
|
2014-05-06 14:23:05 +00:00
|
|
|
|
|
|
|
//-------------------------------------------------------------------
|
|
|
|
// Utility Function for MD5
|
|
|
|
//-------------------------------------------------------------------
|
2020-08-22 12:40:53 +00:00
|
|
|
#ifdef USE_GNUTLS_NETTLE
|
2023-08-06 15:17:15 +00:00
|
|
|
bool s3fs_md5(const unsigned char* data, size_t datalen, md5_t* result)
|
|
|
|
{
|
|
|
|
struct md5_ctx ctx_md5;
|
|
|
|
md5_init(&ctx_md5);
|
|
|
|
md5_update(&ctx_md5, datalen, data);
|
|
|
|
md5_digest(&ctx_md5, result->size(), result->data());
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-05 00:36:22 +00:00
|
|
|
bool s3fs_md5_fd(int fd, off_t start, off_t size, md5_t* result)
|
2014-05-06 14:23:05 +00:00
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
struct md5_ctx ctx_md5;
|
2020-09-12 06:00:23 +00:00
|
|
|
off_t bytes;
|
2020-08-22 12:40:53 +00:00
|
|
|
|
2020-09-25 14:05:42 +00:00
|
|
|
if(-1 == size){
|
|
|
|
struct stat st;
|
|
|
|
if(-1 == fstat(fd, &st)){
|
2023-08-05 00:36:22 +00:00
|
|
|
return false;
|
2020-09-25 14:05:42 +00:00
|
|
|
}
|
|
|
|
size = st.st_size;
|
|
|
|
}
|
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
md5_init(&ctx_md5);
|
|
|
|
|
2020-09-12 06:00:23 +00:00
|
|
|
for(off_t total = 0; total < size; total += bytes){
|
2020-09-14 10:49:45 +00:00
|
|
|
off_t len = 512;
|
|
|
|
unsigned char buf[len];
|
|
|
|
bytes = len < (size - total) ? len : (size - total);
|
2020-08-22 12:40:53 +00:00
|
|
|
bytes = pread(fd, buf, bytes, start + total);
|
|
|
|
if(0 == bytes){
|
|
|
|
// end of file
|
|
|
|
break;
|
|
|
|
}else if(-1 == bytes){
|
|
|
|
// error
|
|
|
|
S3FS_PRN_ERR("file read error(%d)", errno);
|
2023-08-05 00:36:22 +00:00
|
|
|
return false;
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
|
|
|
md5_update(&ctx_md5, bytes, buf);
|
|
|
|
}
|
2023-08-05 00:36:22 +00:00
|
|
|
md5_digest(&ctx_md5, result->size(), result->data());
|
2014-05-06 14:23:05 +00:00
|
|
|
|
2023-08-05 00:36:22 +00:00
|
|
|
return true;
|
2014-05-06 14:23:05 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
#else // USE_GNUTLS_NETTLE
|
2014-05-06 14:23:05 +00:00
|
|
|
|
2023-08-06 15:17:15 +00:00
|
|
|
bool s3fs_md5(const unsigned char* data, size_t datalen, md5_t* digest)
|
|
|
|
{
|
|
|
|
gcry_md_hd_t ctx_md5;
|
|
|
|
gcry_error_t err;
|
|
|
|
if(GPG_ERR_NO_ERROR != (err = gcry_md_open(&ctx_md5, GCRY_MD_MD5, 0))){
|
|
|
|
S3FS_PRN_ERR("MD5 context creation failure: %s/%s", gcry_strsource(err), gcry_strerror(err));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
gcry_md_write(ctx_md5, digest->data(), digest->size());
|
|
|
|
gcry_md_close(ctx_md5);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-08-05 00:36:22 +00:00
|
|
|
bool s3fs_md5_fd(int fd, off_t start, off_t size, md5_t* result)
|
2014-05-06 14:23:05 +00:00
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
gcry_md_hd_t ctx_md5;
|
|
|
|
gcry_error_t err;
|
2020-09-12 06:00:23 +00:00
|
|
|
off_t bytes;
|
2020-08-22 12:40:53 +00:00
|
|
|
|
|
|
|
if(-1 == size){
|
|
|
|
struct stat st;
|
|
|
|
if(-1 == fstat(fd, &st)){
|
2023-08-05 00:36:22 +00:00
|
|
|
return false;
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
2020-09-12 06:00:23 +00:00
|
|
|
size = st.st_size;
|
2014-05-06 14:23:05 +00:00
|
|
|
}
|
2020-08-22 12:40:53 +00:00
|
|
|
|
|
|
|
if(GPG_ERR_NO_ERROR != (err = gcry_md_open(&ctx_md5, GCRY_MD_MD5, 0))){
|
|
|
|
S3FS_PRN_ERR("MD5 context creation failure: %s/%s", gcry_strsource(err), gcry_strerror(err));
|
2023-08-05 00:36:22 +00:00
|
|
|
return false;
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 06:00:23 +00:00
|
|
|
for(off_t total = 0; total < size; total += bytes){
|
2020-09-14 10:49:45 +00:00
|
|
|
off_t len = 512;
|
|
|
|
char buf[len];
|
|
|
|
bytes = len < (size - total) ? len : (size - total);
|
2020-08-22 12:40:53 +00:00
|
|
|
bytes = pread(fd, buf, bytes, start + total);
|
|
|
|
if(0 == bytes){
|
|
|
|
// end of file
|
|
|
|
break;
|
|
|
|
}else if(-1 == bytes){
|
|
|
|
// error
|
|
|
|
S3FS_PRN_ERR("file read error(%d)", errno);
|
|
|
|
gcry_md_close(ctx_md5);
|
2023-08-05 00:36:22 +00:00
|
|
|
return false;
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
|
|
|
gcry_md_write(ctx_md5, buf, bytes);
|
|
|
|
}
|
2023-08-05 00:36:22 +00:00
|
|
|
memcpy(result->data(), gcry_md_read(ctx_md5, 0), result->size());
|
2020-08-22 12:40:53 +00:00
|
|
|
gcry_md_close(ctx_md5);
|
2014-05-06 14:23:05 +00:00
|
|
|
|
2023-08-05 00:36:22 +00:00
|
|
|
return true;
|
2014-05-06 14:23:05 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
#endif // USE_GNUTLS_NETTLE
|
2014-05-06 14:23:05 +00:00
|
|
|
|
2015-02-02 16:36:08 +00:00
|
|
|
//-------------------------------------------------------------------
|
|
|
|
// Utility Function for SHA256
|
|
|
|
//-------------------------------------------------------------------
|
2020-08-22 12:40:53 +00:00
|
|
|
#ifdef USE_GNUTLS_NETTLE
|
2023-08-05 00:36:22 +00:00
|
|
|
bool s3fs_sha256(const unsigned char* data, size_t datalen, sha256_t* digest)
|
2015-02-02 16:36:08 +00:00
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
struct sha256_ctx ctx_sha256;
|
|
|
|
sha256_init(&ctx_sha256);
|
|
|
|
sha256_update(&ctx_sha256, datalen, data);
|
2023-08-05 00:36:22 +00:00
|
|
|
sha256_digest(&ctx_sha256, digest->size(), digest->data());
|
2015-02-02 16:36:08 +00:00
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
return true;
|
2015-02-02 16:36:08 +00:00
|
|
|
}
|
|
|
|
|
2023-08-05 00:36:22 +00:00
|
|
|
bool s3fs_sha256_fd(int fd, off_t start, off_t size, sha256_t* result)
|
2015-02-02 16:36:08 +00:00
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
struct sha256_ctx ctx_sha256;
|
2020-09-12 06:00:23 +00:00
|
|
|
off_t bytes;
|
2020-08-22 12:40:53 +00:00
|
|
|
|
|
|
|
sha256_init(&ctx_sha256);
|
|
|
|
|
2020-09-12 06:00:23 +00:00
|
|
|
for(off_t total = 0; total < size; total += bytes){
|
2020-09-14 10:49:45 +00:00
|
|
|
off_t len = 512;
|
|
|
|
unsigned char buf[len];
|
|
|
|
bytes = len < (size - total) ? len : (size - total);
|
2020-08-22 12:40:53 +00:00
|
|
|
bytes = pread(fd, buf, bytes, start + total);
|
|
|
|
if(0 == bytes){
|
|
|
|
// end of file
|
|
|
|
break;
|
|
|
|
}else if(-1 == bytes){
|
|
|
|
// error
|
|
|
|
S3FS_PRN_ERR("file read error(%d)", errno);
|
2023-08-05 00:36:22 +00:00
|
|
|
return false;
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
|
|
|
sha256_update(&ctx_sha256, bytes, buf);
|
|
|
|
}
|
2023-08-05 00:36:22 +00:00
|
|
|
sha256_digest(&ctx_sha256, result->size(), result->data());
|
2015-02-02 16:36:08 +00:00
|
|
|
|
2023-08-05 00:36:22 +00:00
|
|
|
return true;
|
2015-02-02 16:36:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
#else // USE_GNUTLS_NETTLE
|
2015-02-02 16:36:08 +00:00
|
|
|
|
2023-08-05 00:36:22 +00:00
|
|
|
bool s3fs_sha256(const unsigned char* data, size_t datalen, sha256_t* digest)
|
2015-02-02 16:36:08 +00:00
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
gcry_md_hd_t ctx_sha256;
|
|
|
|
gcry_error_t err;
|
|
|
|
if(GPG_ERR_NO_ERROR != (err = gcry_md_open(&ctx_sha256, GCRY_MD_SHA256, 0))){
|
|
|
|
S3FS_PRN_ERR("SHA256 context creation failure: %s/%s", gcry_strsource(err), gcry_strerror(err));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
gcry_md_write(ctx_sha256, data, datalen);
|
2023-08-05 00:36:22 +00:00
|
|
|
memcpy(digest->data(), gcry_md_read(ctx_sha256, 0), digest->size());
|
2020-08-22 12:40:53 +00:00
|
|
|
gcry_md_close(ctx_sha256);
|
|
|
|
|
|
|
|
return true;
|
2015-02-02 16:36:08 +00:00
|
|
|
}
|
|
|
|
|
2023-08-05 00:36:22 +00:00
|
|
|
bool s3fs_sha256_fd(int fd, off_t start, off_t size, sha256_t* result)
|
2015-02-02 16:36:08 +00:00
|
|
|
{
|
2020-08-22 12:40:53 +00:00
|
|
|
gcry_md_hd_t ctx_sha256;
|
|
|
|
gcry_error_t err;
|
2020-09-12 06:00:23 +00:00
|
|
|
off_t bytes;
|
2020-08-22 12:40:53 +00:00
|
|
|
|
|
|
|
if(-1 == size){
|
|
|
|
struct stat st;
|
|
|
|
if(-1 == fstat(fd, &st)){
|
2023-08-05 00:36:22 +00:00
|
|
|
return false;
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
2020-09-12 06:00:23 +00:00
|
|
|
size = st.st_size;
|
2015-02-02 16:36:08 +00:00
|
|
|
}
|
2020-08-22 12:40:53 +00:00
|
|
|
|
|
|
|
if(GPG_ERR_NO_ERROR != (err = gcry_md_open(&ctx_sha256, GCRY_MD_SHA256, 0))){
|
|
|
|
S3FS_PRN_ERR("SHA256 context creation failure: %s/%s", gcry_strsource(err), gcry_strerror(err));
|
2023-08-05 00:36:22 +00:00
|
|
|
return false;
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
|
|
|
|
2020-09-12 06:00:23 +00:00
|
|
|
for(off_t total = 0; total < size; total += bytes){
|
2020-09-14 10:49:45 +00:00
|
|
|
off_t len = 512;
|
|
|
|
char buf[len];
|
|
|
|
bytes = len < (size - total) ? len : (size - total);
|
2020-08-22 12:40:53 +00:00
|
|
|
bytes = pread(fd, buf, bytes, start + total);
|
|
|
|
if(0 == bytes){
|
|
|
|
// end of file
|
|
|
|
break;
|
|
|
|
}else if(-1 == bytes){
|
|
|
|
// error
|
|
|
|
S3FS_PRN_ERR("file read error(%d)", errno);
|
|
|
|
gcry_md_close(ctx_sha256);
|
2023-08-05 00:36:22 +00:00
|
|
|
return false;
|
2020-08-22 12:40:53 +00:00
|
|
|
}
|
|
|
|
gcry_md_write(ctx_sha256, buf, bytes);
|
|
|
|
}
|
2023-08-05 00:36:22 +00:00
|
|
|
memcpy(result->data(), gcry_md_read(ctx_sha256, 0), result->size());
|
2020-08-22 12:40:53 +00:00
|
|
|
gcry_md_close(ctx_sha256);
|
2015-02-02 16:36:08 +00:00
|
|
|
|
2023-08-05 00:36:22 +00:00
|
|
|
return true;
|
2015-02-02 16:36:08 +00:00
|
|
|
}
|
|
|
|
|
2020-08-22 12:40:53 +00:00
|
|
|
#endif // USE_GNUTLS_NETTLE
|
2015-02-02 16:36:08 +00:00
|
|
|
|
2014-09-07 15:08:27 +00:00
|
|
|
/*
|
|
|
|
* Local variables:
|
2020-08-22 12:40:53 +00:00
|
|
|
* tab-width: 4
|
|
|
|
* c-basic-offset: 4
|
2014-09-07 15:08:27 +00:00
|
|
|
* End:
|
2020-08-22 12:40:53 +00:00
|
|
|
* vim600: expandtab sw=4 ts=4 fdm=marker
|
|
|
|
* vim<600: expandtab sw=4 ts=4
|
2014-09-07 15:08:27 +00:00
|
|
|
*/
|