Line | Branch | Exec | Source |
---|---|---|---|
1 | /* GATE PROJECT LICENSE: | ||
2 | +----------------------------------------------------------------------------+ | ||
3 | | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> | | ||
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 are met:| | ||
8 | | | | ||
9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
10 | | this list of conditions and the following disclaimer. | | ||
11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
12 | | notice, this list of conditions and the following disclaimer in the | | ||
13 | | documentation and/or other materials provided with the distribution. | | ||
14 | | | | ||
15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
26 | +----------------------------------------------------------------------------+ | ||
27 | */ | ||
28 | #include "gate/encode/blockciphers.hpp" | ||
29 | #include "gate/exceptions.hpp" | ||
30 | #include "gate/results.hpp" | ||
31 | #include "gate/memalloc.hpp" | ||
32 | |||
33 | namespace gate | ||
34 | { | ||
35 | namespace enc | ||
36 | { | ||
37 | |||
38 | 9 | Blowfish::Block::Block(uint32_t l, uint32_t r) | |
39 | { | ||
40 | 9 | gate_blowfish_block_init(this, l, r); | |
41 | 9 | } | |
42 | ✗ | Blowfish::Block::Block(unsigned char const l[4], unsigned char const r[4]) | |
43 | { | ||
44 | ✗ | Mem::copy(&this->L[0], &l[0], sizeof(this->L)); | |
45 | ✗ | Mem::copy(&this->R[0], &r[0], sizeof(this->R)); | |
46 | ✗ | } | |
47 | |||
48 | |||
49 | 4 | Blowfish::Blowfish(void const* key, size_t keylength) | |
50 | 4 | : cbc(false), cbcBlock(Block()) | |
51 | { | ||
52 | 4 | result_t result = gate_blowfish_init(&this->impl, key, keylength); | |
53 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | GATEXX_CHECK_ERROR(result); |
54 | 4 | } | |
55 | |||
56 | 4 | Blowfish::Blowfish(void const* key, size_t keylength, Block const& cbcIvBlock) | |
57 | 4 | : cbc(true) | |
58 | { | ||
59 | 4 | result_t result = gate_blowfish_block_load(&this->cbcBlock, &cbcIvBlock, sizeof(cbcIvBlock)); | |
60 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | GATEXX_CHECK_ERROR(result); |
61 | 4 | result = gate_blowfish_init(&this->impl, key, keylength); | |
62 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | GATEXX_CHECK_ERROR(result); |
63 | 4 | } | |
64 | |||
65 | 2 | size_t Blowfish::encrypt(char const* srcbuffer, char* dstbuffer, size_t length) | |
66 | { | ||
67 | 2 | size_t ret = length - (length % GATE_BLOWFISH_BLOCKLEN); | |
68 | result_t result; | ||
69 | |||
70 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (this->cbc) |
71 | { | ||
72 | 1 | result = gate_blowfish_encrypt_cbc(&this->impl, srcbuffer, dstbuffer, ret, &this->cbcBlock); | |
73 | } | ||
74 | else | ||
75 | { | ||
76 | 1 | result = gate_blowfish_encrypt(&this->impl, srcbuffer, dstbuffer, ret); | |
77 | } | ||
78 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | GATEXX_CHECK_ERROR(result); |
79 | 2 | return ret; | |
80 | } | ||
81 | 2 | size_t Blowfish::encrypt(char* buffer, size_t length) | |
82 | { | ||
83 | 2 | return this->encrypt(buffer, buffer, length); | |
84 | } | ||
85 | 2 | void Blowfish::encrypt(Stream& input, Stream& output) | |
86 | { | ||
87 | gate_result_t result; | ||
88 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (this->cbc) |
89 | { | ||
90 | 1 | result = gate_blowfish_encrypt_cbc_stream(&this->impl, input.c_impl(), output.c_impl(), &this->cbcBlock); | |
91 | } | ||
92 | else | ||
93 | { | ||
94 | 1 | result = gate_blowfish_encrypt_stream(&this->impl, input.c_impl(), output.c_impl()); | |
95 | } | ||
96 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | GATEXX_CHECK_ERROR(result); |
97 | 2 | } | |
98 | |||
99 | |||
100 | 2 | size_t Blowfish::decrypt(char const* srcbuffer, char* dstbuffer, size_t length) | |
101 | { | ||
102 | 2 | size_t ret = length - (length % GATE_BLOWFISH_BLOCKLEN); | |
103 | result_t result; | ||
104 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (this->cbc) |
105 | { | ||
106 | 1 | result = gate_blowfish_decrypt_cbc(&this->impl, srcbuffer, dstbuffer, ret, &this->cbcBlock); | |
107 | } | ||
108 | else | ||
109 | { | ||
110 | 1 | result = gate_blowfish_decrypt(&this->impl, srcbuffer, dstbuffer, ret); | |
111 | } | ||
112 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | GATEXX_CHECK_EXCEPTION(result); |
113 | 2 | return ret; | |
114 | } | ||
115 | 2 | size_t Blowfish::decrypt(char* buffer, size_t length) | |
116 | { | ||
117 | 2 | return this->decrypt(buffer, buffer, length); | |
118 | } | ||
119 | 2 | void Blowfish::decrypt(Stream& input, Stream& output) | |
120 | { | ||
121 | gate_result_t result; | ||
122 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (this->cbc) |
123 | { | ||
124 | 1 | result = gate_blowfish_decrypt_cbc_stream(&this->impl, input.c_impl(), output.c_impl(), &this->cbcBlock); | |
125 | } | ||
126 | else | ||
127 | { | ||
128 | 1 | result = gate_blowfish_decrypt_stream(&this->impl, input.c_impl(), output.c_impl()); | |
129 | } | ||
130 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | GATEXX_CHECK_ERROR(result); |
131 | 2 | } | |
132 | |||
133 | |||
134 | |||
135 | ✗ | Xtea::Xtea(uint32_t const key[4]) | |
136 | { | ||
137 | ✗ | for (size_t n = 0; n != 4; ++n) | |
138 | { | ||
139 | ✗ | this->keyblock[n] = key[n]; | |
140 | } | ||
141 | ✗ | Mem::clear(this->keyblockbuffer, sizeof(this->keyblockbuffer)); | |
142 | ✗ | } | |
143 | 3 | Xtea::Xtea(char const* key, size_t keylength) | |
144 | { | ||
145 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (keylength < sizeof(this->keyblockbuffer)) |
146 | { | ||
147 | ✗ | GATEXX_RAISE_ERROR(results::InvalidArg); | |
148 | } | ||
149 | 3 | Mem::copy(&this->keyblockbuffer[0], key, sizeof(this->keyblock)); | |
150 | 3 | } | |
151 | |||
152 | ✗ | void Xtea::encrypt(uint32_t* blocks, size_t block_count) | |
153 | { | ||
154 | ✗ | result_t result = gate_xtea_block_encrypt(blocks, block_count, this->keyblock); | |
155 | ✗ | GATEXX_CHECK_ERROR(result); | |
156 | ✗ | } | |
157 | 1 | size_t Xtea::encrypt(void const* srcbuffer, void* dstbuffer, size_t length) | |
158 | { | ||
159 | 1 | size_t ret = length - (length % GATE_XTEA_BLOCKLEN); | |
160 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (srcbuffer != dstbuffer) |
161 | { | ||
162 | ✗ | gate_mem_copy(dstbuffer, srcbuffer, ret); | |
163 | } | ||
164 | 1 | unsigned char* ptr = (unsigned char*)dstbuffer; | |
165 | |||
166 | 1 | result_t result = gate_xtea_encrypt(ptr, ret, this->keyblockbuffer); | |
167 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_ERROR(result); |
168 | 1 | return ret; | |
169 | } | ||
170 | 1 | void Xtea::encrypt(Stream& instream, Stream& outstream) | |
171 | { | ||
172 | 1 | gate_result_t result = gate_xtea_encrypt_stream(instream.c_impl(), outstream.c_impl(), this->keyblockbuffer); | |
173 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_ERROR(result); |
174 | 1 | } | |
175 | |||
176 | ✗ | void Xtea::decrypt(uint32_t* blocks, size_t block_count) | |
177 | { | ||
178 | ✗ | result_t result = gate_xtea_block_decrypt(blocks, block_count, this->keyblock); | |
179 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
180 | ✗ | } | |
181 | 1 | size_t Xtea::decrypt(void const* srcbuffer, void* dstbuffer, size_t length) | |
182 | { | ||
183 | 1 | size_t ret = length - (length % GATE_XTEA_BLOCKLEN); | |
184 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (srcbuffer != dstbuffer) |
185 | { | ||
186 | ✗ | gate_mem_copy(dstbuffer, srcbuffer, ret); | |
187 | } | ||
188 | 1 | unsigned char* ptr = (unsigned char*)dstbuffer; | |
189 | |||
190 | 1 | result_t result = gate_xtea_decrypt(ptr, ret, this->keyblockbuffer); | |
191 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
192 | 1 | return ret; | |
193 | } | ||
194 | 1 | void Xtea::decrypt(Stream& instream, Stream& outstream) | |
195 | { | ||
196 | 1 | gate_result_t result = gate_xtea_decrypt_stream(instream.c_impl(), outstream.c_impl(), this->keyblockbuffer); | |
197 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_ERROR(result); |
198 | 1 | } | |
199 | |||
200 | |||
201 | |||
202 | ✗ | Xxtea::Xxtea(uint32_t const key[4]) | |
203 | { | ||
204 | ✗ | for (size_t n = 0; n != 4; ++n) | |
205 | { | ||
206 | ✗ | this->keyblock[n] = key[n]; | |
207 | } | ||
208 | ✗ | Mem::clear(this->keyblockbuffer, sizeof(this->keyblockbuffer)); | |
209 | ✗ | } | |
210 | 3 | Xxtea::Xxtea(char const* key, size_t keylength) | |
211 | { | ||
212 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (keylength < sizeof(this->keyblockbuffer)) |
213 | { | ||
214 | ✗ | GATEXX_RAISE_ERROR(results::InvalidArg); | |
215 | } | ||
216 | 3 | Mem::copy(&this->keyblockbuffer[0], key, sizeof(this->keyblock)); | |
217 | 3 | } | |
218 | |||
219 | ✗ | void Xxtea::encrypt(uint32_t* blocks, size_t block_count) | |
220 | { | ||
221 | ✗ | result_t result = gate_xxtea_block_encrypt(blocks, block_count, this->keyblock); | |
222 | ✗ | GATEXX_CHECK_ERROR(result); | |
223 | ✗ | } | |
224 | 1 | size_t Xxtea::encrypt(void const* srcbuffer, void* dstbuffer, size_t length) | |
225 | { | ||
226 | 1 | size_t ret = length - (length % GATE_XTEA_BLOCKLEN); | |
227 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (srcbuffer != dstbuffer) |
228 | { | ||
229 | ✗ | gate_mem_copy(dstbuffer, srcbuffer, ret); | |
230 | } | ||
231 | 1 | unsigned char* ptr = (unsigned char*)dstbuffer; | |
232 | |||
233 | 1 | result_t result = gate_xxtea_encrypt(ptr, ret, this->keyblockbuffer); | |
234 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_ERROR(result); |
235 | 1 | return ret; | |
236 | } | ||
237 | 1 | void Xxtea::encrypt(Stream& instream, Stream& outstream) | |
238 | { | ||
239 | 1 | gate_result_t result = gate_xxtea_encrypt_stream(instream.c_impl(), outstream.c_impl(), this->keyblockbuffer); | |
240 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_ERROR(result); |
241 | 1 | } | |
242 | |||
243 | ✗ | void Xxtea::decrypt(uint32_t* blocks, size_t block_count) | |
244 | { | ||
245 | ✗ | result_t result = gate_xxtea_block_decrypt(blocks, block_count, this->keyblock); | |
246 | ✗ | GATEXX_CHECK_EXCEPTION(result); | |
247 | ✗ | } | |
248 | 1 | size_t Xxtea::decrypt(void const* srcbuffer, void* dstbuffer, size_t length) | |
249 | { | ||
250 | 1 | size_t ret = length - (length % GATE_XTEA_BLOCKLEN); | |
251 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (srcbuffer != dstbuffer) |
252 | { | ||
253 | ✗ | gate_mem_copy(dstbuffer, srcbuffer, ret); | |
254 | } | ||
255 | 1 | unsigned char* ptr = (unsigned char*)dstbuffer; | |
256 | |||
257 | 1 | result_t result = gate_xxtea_decrypt(ptr, ret, this->keyblockbuffer); | |
258 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_EXCEPTION(result); |
259 | 1 | return ret; | |
260 | } | ||
261 | 1 | void Xxtea::decrypt(Stream& instream, Stream& outstream) | |
262 | { | ||
263 | 1 | gate_result_t result = gate_xxtea_decrypt_stream(instream.c_impl(), outstream.c_impl(), this->keyblockbuffer); | |
264 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATEXX_CHECK_ERROR(result); |
265 | 1 | } | |
266 | |||
267 | |||
268 | } // end of namespace enc | ||
269 | } // end of namespace gate | ||
270 |