Coverage Report

Created: 2026-01-09 06:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libtorrent/src/ed25519/hasher512.cpp
Line
Count
Source
1
/*
2
3
Copyright (c) 2003-2016, Arvid Norberg, Alden Torres
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
10
    * Redistributions of source code must retain the above copyright
11
      notice, this list of conditions and the following disclaimer.
12
    * Redistributions in binary form must reproduce the above copyright
13
      notice, this list of conditions and the following disclaimer in
14
      the documentation and/or other materials provided with the distribution.
15
    * Neither the name of the author nor the names of its
16
      contributors may be used to endorse or promote products derived
17
      from this software without specific prior written permission.
18
19
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
POSSIBILITY OF SUCH DAMAGE.
30
31
*/
32
33
#include "libtorrent/aux_/hasher512.hpp"
34
#include "libtorrent/error_code.hpp"
35
#include "libtorrent/assert.hpp"
36
#include "libtorrent/ssl.hpp"
37
38
#include "libtorrent/aux_/disable_deprecation_warnings_push.hpp"
39
40
namespace libtorrent {
41
namespace aux {
42
43
  hasher512::hasher512()
44
0
  {
45
#ifdef TORRENT_USE_LIBGCRYPT
46
    gcry_md_open(&m_context, GCRY_MD_SHA512, 0);
47
#elif TORRENT_USE_COMMONCRYPTO
48
    CC_SHA512_Init(&m_context);
49
#elif TORRENT_USE_CNG
50
#elif TORRENT_USE_CRYPTOAPI_SHA_512
51
#elif defined TORRENT_USE_LIBCRYPTO
52
    SHA512_Init(&m_context);
53
#else
54
    SHA512_init(&m_context);
55
#endif
56
0
  }
57
58
  hasher512::hasher512(span<char const> data)
59
0
    : hasher512()
60
0
  {
61
0
    update(data);
62
0
  }
63
64
#ifdef TORRENT_USE_LIBGCRYPT
65
  hasher512::hasher512(hasher512 const& h)
66
  {
67
    gcry_md_copy(&m_context, h.m_context);
68
  }
69
70
  hasher512& hasher512::operator=(hasher512 const& h) &
71
  {
72
    if (this == &h) return *this;
73
    gcry_md_close(m_context);
74
    gcry_md_copy(&m_context, h.m_context);
75
    return *this;
76
  }
77
#else
78
0
  hasher512::hasher512(hasher512 const&) = default;
79
0
  hasher512& hasher512::operator=(hasher512 const&) & = default;
80
#endif
81
82
  hasher512& hasher512::update(span<char const> data)
83
0
  {
84
0
    TORRENT_ASSERT(data.size() > 0);
85
#ifdef TORRENT_USE_LIBGCRYPT
86
    gcry_md_write(m_context, data.data(), static_cast<std::size_t>(data.size()));
87
#elif TORRENT_USE_COMMONCRYPTO
88
    CC_SHA512_Update(&m_context, reinterpret_cast<unsigned char const*>(data.data()), CC_LONG(data.size()));
89
#elif TORRENT_USE_CNG
90
    m_context.update(data);
91
#elif TORRENT_USE_CRYPTOAPI_SHA_512
92
    m_context.update(data);
93
#elif defined TORRENT_USE_LIBCRYPTO
94
    SHA512_Update(&m_context, reinterpret_cast<unsigned char const*>(data.data())
95
0
      , static_cast<std::size_t>(data.size()));
96
#else
97
    SHA512_update(&m_context, reinterpret_cast<unsigned char const*>(data.data())
98
      , static_cast<std::size_t>(data.size()));
99
#endif
100
0
    return *this;
101
0
  }
102
103
  sha512_hash hasher512::final()
104
0
  {
105
0
    sha512_hash digest;
106
#ifdef TORRENT_USE_LIBGCRYPT
107
    gcry_md_final(m_context);
108
    digest.assign(reinterpret_cast<char const*>(gcry_md_read(m_context, 0)));
109
#elif TORRENT_USE_COMMONCRYPTO
110
    CC_SHA512_Final(reinterpret_cast<unsigned char*>(digest.data()), &m_context);
111
#elif TORRENT_USE_CNG
112
    m_context.get_hash(digest.data(), digest.size());
113
#elif TORRENT_USE_CRYPTOAPI_SHA_512
114
    m_context.get_hash(digest.data(), digest.size());
115
#elif defined TORRENT_USE_LIBCRYPTO
116
    SHA512_Final(reinterpret_cast<unsigned char*>(digest.data()), &m_context);
117
#else
118
    SHA512_final(reinterpret_cast<unsigned char*>(digest.data()), &m_context);
119
#endif
120
0
    return digest;
121
0
  }
122
123
  void hasher512::reset()
124
0
  {
125
#ifdef TORRENT_USE_LIBGCRYPT
126
    gcry_md_reset(m_context);
127
#elif TORRENT_USE_COMMONCRYPTO
128
    CC_SHA512_Init(&m_context);
129
#elif TORRENT_USE_CNG
130
    m_context.reset();
131
#elif TORRENT_USE_CRYPTOAPI_SHA_512
132
    m_context.reset();
133
#elif defined TORRENT_USE_LIBCRYPTO
134
    SHA512_Init(&m_context);
135
#else
136
    SHA512_init(&m_context);
137
#endif
138
0
  }
139
140
#if defined TORRENT_USE_LIBGCRYPT
141
  hasher512::~hasher512()
142
  {
143
    gcry_md_close(m_context);
144
  }
145
#else
146
0
  hasher512::~hasher512() = default;
147
#endif
148
149
}
150
}
151
152
#include "libtorrent/aux_/disable_warnings_pop.hpp"