Branch data TLA Line data Source code
1 : : // Copyright (c) 2017-2022 Thomas Fussell
2 : : // Copyright (c) 2024-2025 xlnt-community
3 : : //
4 : : // Permission is hereby granted, free of charge, to any person obtaining a copy
5 : : // of this software and associated documentation files (the "Software"), to deal
6 : : // in the Software without restriction, including without limitation the rights
7 : : // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 : : // copies of the Software, and to permit persons to whom the Software is
9 : : // furnished to do so, subject to the following conditions:
10 : : //
11 : : // The above copyright notice and this permission notice shall be included in
12 : : // all copies or substantial portions of the Software.
13 : : //
14 : : // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 : : // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 : : // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 : : // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 : : // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 : : // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 : : // THE SOFTWARE
21 : : //
22 : : // @license: http://www.opensource.org/licenses/mit-license.php
23 : : // @author: see AUTHORS file
24 : :
25 : : #include <cstring>
26 : :
27 : : #include <detail/cryptography/sha.hpp>
28 : :
29 : : extern "C" {
30 : :
31 : : extern void sha1_hash(const uint8_t *message, size_t len, uint32_t hash[5]);
32 : : extern void sha512_hash(const uint8_t *message, size_t len, uint64_t hash[8]);
33 : : }
34 : :
35 : : namespace {
36 : :
37 : : #ifdef _MSC_VER
38 : : inline std::uint32_t byteswap32(std::uint32_t i)
39 : : {
40 : : return _byteswap_ulong(i);
41 : : }
42 : :
43 : : inline std::uint64_t byteswap64(std::uint64_t i)
44 : : {
45 : : return _byteswap_uint64(i);
46 : : }
47 : : #else
48 :CBC 5251655 : inline std::uint32_t byteswap32(std::uint32_t i)
49 : : {
50 : 5251655 : return __builtin_bswap32(i);
51 : : }
52 : :
53 : 5601344 : inline std::uint64_t byteswap64(std::uint64_t i)
54 : : {
55 : 5601344 : return __builtin_bswap64(i);
56 : : }
57 : : #endif
58 : :
59 : 1050331 : void byteswap(std::uint32_t *arr, std::size_t len)
60 : : {
61 [ + + ]: 6301986 : for (auto i = std::size_t(0); i < len; ++i)
62 : : {
63 : 5251655 : arr[i] = byteswap32(arr[i]);
64 : : }
65 : 1050331 : }
66 : :
67 : 700168 : void byteswap(std::uint64_t *arr, std::size_t len)
68 : : {
69 [ + + ]: 6301512 : for (auto i = std::size_t(0); i < len; ++i)
70 : : {
71 : 5601344 : arr[i] = byteswap64(arr[i]);
72 : : }
73 : 700168 : }
74 : :
75 : : } // namespace
76 : :
77 : : namespace xlnt {
78 : : namespace detail {
79 : :
80 : 1050331 : void sha1(const std::vector<std::uint8_t> &input, std::vector<std::uint8_t> &output)
81 : : {
82 : : static const auto sha1_bytes = 20;
83 : :
84 : 1050331 : output.resize(sha1_bytes);
85 : 1050331 : auto output_pointer_u32 = reinterpret_cast<std::uint32_t *>(output.data());
86 : :
87 : 1050331 : sha1_hash(input.data(), input.size(), output_pointer_u32);
88 : :
89 : 1050331 : byteswap(output_pointer_u32, sha1_bytes / sizeof(std::uint32_t));
90 : 1050331 : }
91 : :
92 : 700168 : void sha512(const std::vector<std::uint8_t> &input, std::vector<std::uint8_t> &output)
93 : : {
94 : : static const auto sha512_bytes = 64;
95 : :
96 : 700168 : output.resize(sha512_bytes);
97 : 700168 : auto output_pointer_u64 = reinterpret_cast<std::uint64_t *>(output.data());
98 : :
99 : 700168 : sha512_hash(input.data(), input.size(), output_pointer_u64);
100 : :
101 : 700168 : byteswap(output_pointer_u64, sha512_bytes / sizeof(std::uint64_t));
102 : 700168 : }
103 : :
104 : : } // namespace detail
105 : : } // namespace xlnt
|