Coverage Report

Created: 2024-02-25 07:20

/src/libluksde/libfcrypto/libfcrypto_blowfish_context.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Blowfish (de/en)crypt functions
3
 *
4
 * Copyright (C) 2017-2024, Joachim Metz <joachim.metz@gmail.com>
5
 *
6
 * Refer to AUTHORS for acknowledgements.
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Lesser General Public License as published by
10
 * the Free Software Foundation, either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
#include <common.h>
23
#include <byte_stream.h>
24
#include <memory.h>
25
#include <types.h>
26
27
#include "libfcrypto_blowfish_context.h"
28
#include "libfcrypto_definitions.h"
29
#include "libfcrypto_libcerror.h"
30
31
/* The initial permutation values (P-array)
32
 */
33
static uint32_t libfcrypto_blowfish_initial_permutation_values[ 18 ] = {
34
  0x243f6a88L, 0x85a308d3L, 0x13198a2eL, 0x03707344L,
35
  0xa4093822L, 0x299f31d0L, 0x082efa98L, 0xec4e6c89L,
36
  0x452821e6L, 0x38d01377L, 0xbe5466cfL, 0x34e90c6cL,
37
  0xc0ac29b7L, 0xc97c50ddL, 0x3f84d5b5L, 0xb5470917L,
38
  0x9216d5d9L, 0x8979fb1bL };
39
40
/* The initial substitution-boxes (S-boxes)
41
 */
42
static uint32_t libfcrypto_blowfish_initial_sboxes[ 4 ][ 256 ] = {
43
  { 0xd1310ba6L, 0x98dfb5acL, 0x2ffd72dbL, 0xd01adfb7L,
44
    0xb8e1afedL, 0x6a267e96L, 0xba7c9045L, 0xf12c7f99L,
45
    0x24a19947L, 0xb3916cf7L, 0x0801f2e2L, 0x858efc16L,
46
    0x636920d8L, 0x71574e69L, 0xa458fea3L, 0xf4933d7eL,
47
    0x0d95748fL, 0x728eb658L, 0x718bcd58L, 0x82154aeeL,
48
    0x7b54a41dL, 0xc25a59b5L, 0x9c30d539L, 0x2af26013L,
49
    0xc5d1b023L, 0x286085f0L, 0xca417918L, 0xb8db38efL,
50
    0x8e79dcb0L, 0x603a180eL, 0x6c9e0e8bL, 0xb01e8a3eL,
51
    0xd71577c1L, 0xbd314b27L, 0x78af2fdaL, 0x55605c60L,
52
    0xe65525f3L, 0xaa55ab94L, 0x57489862L, 0x63e81440L,
53
    0x55ca396aL, 0x2aab10b6L, 0xb4cc5c34L, 0x1141e8ceL,
54
    0xa15486afL, 0x7c72e993L, 0xb3ee1411L, 0x636fbc2aL,
55
    0x2ba9c55dL, 0x741831f6L, 0xce5c3e16L, 0x9b87931eL,
56
    0xafd6ba33L, 0x6c24cf5cL, 0x7a325381L, 0x28958677L,
57
    0x3b8f4898L, 0x6b4bb9afL, 0xc4bfe81bL, 0x66282193L,
58
    0x61d809ccL, 0xfb21a991L, 0x487cac60L, 0x5dec8032L,
59
    0xef845d5dL, 0xe98575b1L, 0xdc262302L, 0xeb651b88L,
60
    0x23893e81L, 0xd396acc5L, 0x0f6d6ff3L, 0x83f44239L,
61
    0x2e0b4482L, 0xa4842004L, 0x69c8f04aL, 0x9e1f9b5eL,
62
    0x21c66842L, 0xf6e96c9aL, 0x670c9c61L, 0xabd388f0L,
63
    0x6a51a0d2L, 0xd8542f68L, 0x960fa728L, 0xab5133a3L,
64
    0x6eef0b6cL, 0x137a3be4L, 0xba3bf050L, 0x7efb2a98L,
65
    0xa1f1651dL, 0x39af0176L, 0x66ca593eL, 0x82430e88L,
66
    0x8cee8619L, 0x456f9fb4L, 0x7d84a5c3L, 0x3b8b5ebeL,
67
    0xe06f75d8L, 0x85c12073L, 0x401a449fL, 0x56c16aa6L,
68
    0x4ed3aa62L, 0x363f7706L, 0x1bfedf72L, 0x429b023dL,
69
    0x37d0d724L, 0xd00a1248L, 0xdb0fead3L, 0x49f1c09bL,
70
    0x075372c9L, 0x80991b7bL, 0x25d479d8L, 0xf6e8def7L,
71
    0xe3fe501aL, 0xb6794c3bL, 0x976ce0bdL, 0x04c006baL,
72
    0xc1a94fb6L, 0x409f60c4L, 0x5e5c9ec2L, 0x196a2463L,
73
    0x68fb6fafL, 0x3e6c53b5L, 0x1339b2ebL, 0x3b52ec6fL,
74
    0x6dfc511fL, 0x9b30952cL, 0xcc814544L, 0xaf5ebd09L,
75
    0xbee3d004L, 0xde334afdL, 0x660f2807L, 0x192e4bb3L,
76
    0xc0cba857L, 0x45c8740fL, 0xd20b5f39L, 0xb9d3fbdbL,
77
    0x5579c0bdL, 0x1a60320aL, 0xd6a100c6L, 0x402c7279L,
78
    0x679f25feL, 0xfb1fa3ccL, 0x8ea5e9f8L, 0xdb3222f8L,
79
    0x3c7516dfL, 0xfd616b15L, 0x2f501ec8L, 0xad0552abL,
80
    0x323db5faL, 0xfd238760L, 0x53317b48L, 0x3e00df82L,
81
    0x9e5c57bbL, 0xca6f8ca0L, 0x1a87562eL, 0xdf1769dbL,
82
    0xd542a8f6L, 0x287effc3L, 0xac6732c6L, 0x8c4f5573L,
83
    0x695b27b0L, 0xbbca58c8L, 0xe1ffa35dL, 0xb8f011a0L,
84
    0x10fa3d98L, 0xfd2183b8L, 0x4afcb56cL, 0x2dd1d35bL,
85
    0x9a53e479L, 0xb6f84565L, 0xd28e49bcL, 0x4bfb9790L,
86
    0xe1ddf2daL, 0xa4cb7e33L, 0x62fb1341L, 0xcee4c6e8L,
87
    0xef20cadaL, 0x36774c01L, 0xd07e9efeL, 0x2bf11fb4L,
88
    0x95dbda4dL, 0xae909198L, 0xeaad8e71L, 0x6b93d5a0L,
89
    0xd08ed1d0L, 0xafc725e0L, 0x8e3c5b2fL, 0x8e7594b7L,
90
    0x8ff6e2fbL, 0xf2122b64L, 0x8888b812L, 0x900df01cL,
91
    0x4fad5ea0L, 0x688fc31cL, 0xd1cff191L, 0xb3a8c1adL,
92
    0x2f2f2218L, 0xbe0e1777L, 0xea752dfeL, 0x8b021fa1L,
93
    0xe5a0cc0fL, 0xb56f74e8L, 0x18acf3d6L, 0xce89e299L,
94
    0xb4a84fe0L, 0xfd13e0b7L, 0x7cc43b81L, 0xd2ada8d9L,
95
    0x165fa266L, 0x80957705L, 0x93cc7314L, 0x211a1477L,
96
    0xe6ad2065L, 0x77b5fa86L, 0xc75442f5L, 0xfb9d35cfL,
97
    0xebcdaf0cL, 0x7b3e89a0L, 0xd6411bd3L, 0xae1e7e49L,
98
    0x00250e2dL, 0x2071b35eL, 0x226800bbL, 0x57b8e0afL,
99
    0x2464369bL, 0xf009b91eL, 0x5563911dL, 0x59dfa6aaL,
100
    0x78c14389L, 0xd95a537fL, 0x207d5ba2L, 0x02e5b9c5L,
101
    0x83260376L, 0x6295cfa9L, 0x11c81968L, 0x4e734a41L,
102
    0xb3472dcaL, 0x7b14a94aL, 0x1b510052L, 0x9a532915L,
103
    0xd60f573fL, 0xbc9bc6e4L, 0x2b60a476L, 0x81e67400L,
104
    0x08ba6fb5L, 0x571be91fL, 0xf296ec6bL, 0x2a0dd915L,
105
    0xb6636521L, 0xe7b9f9b6L, 0xff34052eL, 0xc5855664L,
106
    0x53b02d5dL, 0xa99f8fa1L, 0x08ba4799L, 0x6e85076aL },
107
  { 0x4b7a70e9L, 0xb5b32944L, 0xdb75092eL, 0xc4192623L,
108
    0xad6ea6b0L, 0x49a7df7dL, 0x9cee60b8L, 0x8fedb266L,
109
    0xecaa8c71L, 0x699a17ffL, 0x5664526cL, 0xc2b19ee1L,
110
    0x193602a5L, 0x75094c29L, 0xa0591340L, 0xe4183a3eL,
111
    0x3f54989aL, 0x5b429d65L, 0x6b8fe4d6L, 0x99f73fd6L,
112
    0xa1d29c07L, 0xefe830f5L, 0x4d2d38e6L, 0xf0255dc1L,
113
    0x4cdd2086L, 0x8470eb26L, 0x6382e9c6L, 0x021ecc5eL,
114
    0x09686b3fL, 0x3ebaefc9L, 0x3c971814L, 0x6b6a70a1L,
115
    0x687f3584L, 0x52a0e286L, 0xb79c5305L, 0xaa500737L,
116
    0x3e07841cL, 0x7fdeae5cL, 0x8e7d44ecL, 0x5716f2b8L,
117
    0xb03ada37L, 0xf0500c0dL, 0xf01c1f04L, 0x0200b3ffL,
118
    0xae0cf51aL, 0x3cb574b2L, 0x25837a58L, 0xdc0921bdL,
119
    0xd19113f9L, 0x7ca92ff6L, 0x94324773L, 0x22f54701L,
120
    0x3ae5e581L, 0x37c2dadcL, 0xc8b57634L, 0x9af3dda7L,
121
    0xa9446146L, 0x0fd0030eL, 0xecc8c73eL, 0xa4751e41L,
122
    0xe238cd99L, 0x3bea0e2fL, 0x3280bba1L, 0x183eb331L,
123
    0x4e548b38L, 0x4f6db908L, 0x6f420d03L, 0xf60a04bfL,
124
    0x2cb81290L, 0x24977c79L, 0x5679b072L, 0xbcaf89afL,
125
    0xde9a771fL, 0xd9930810L, 0xb38bae12L, 0xdccf3f2eL,
126
    0x5512721fL, 0x2e6b7124L, 0x501adde6L, 0x9f84cd87L,
127
    0x7a584718L, 0x7408da17L, 0xbc9f9abcL, 0xe94b7d8cL,
128
    0xec7aec3aL, 0xdb851dfaL, 0x63094366L, 0xc464c3d2L,
129
    0xef1c1847L, 0x3215d908L, 0xdd433b37L, 0x24c2ba16L,
130
    0x12a14d43L, 0x2a65c451L, 0x50940002L, 0x133ae4ddL,
131
    0x71dff89eL, 0x10314e55L, 0x81ac77d6L, 0x5f11199bL,
132
    0x043556f1L, 0xd7a3c76bL, 0x3c11183bL, 0x5924a509L,
133
    0xf28fe6edL, 0x97f1fbfaL, 0x9ebabf2cL, 0x1e153c6eL,
134
    0x86e34570L, 0xeae96fb1L, 0x860e5e0aL, 0x5a3e2ab3L,
135
    0x771fe71cL, 0x4e3d06faL, 0x2965dcb9L, 0x99e71d0fL,
136
    0x803e89d6L, 0x5266c825L, 0x2e4cc978L, 0x9c10b36aL,
137
    0xc6150ebaL, 0x94e2ea78L, 0xa5fc3c53L, 0x1e0a2df4L,
138
    0xf2f74ea7L, 0x361d2b3dL, 0x1939260fL, 0x19c27960L,
139
    0x5223a708L, 0xf71312b6L, 0xebadfe6eL, 0xeac31f66L,
140
    0xe3bc4595L, 0xa67bc883L, 0xb17f37d1L, 0x018cff28L,
141
    0xc332ddefL, 0xbe6c5aa5L, 0x65582185L, 0x68ab9802L,
142
    0xeecea50fL, 0xdb2f953bL, 0x2aef7dadL, 0x5b6e2f84L,
143
    0x1521b628L, 0x29076170L, 0xecdd4775L, 0x619f1510L,
144
    0x13cca830L, 0xeb61bd96L, 0x0334fe1eL, 0xaa0363cfL,
145
    0xb5735c90L, 0x4c70a239L, 0xd59e9e0bL, 0xcbaade14L,
146
    0xeecc86bcL, 0x60622ca7L, 0x9cab5cabL, 0xb2f3846eL,
147
    0x648b1eafL, 0x19bdf0caL, 0xa02369b9L, 0x655abb50L,
148
    0x40685a32L, 0x3c2ab4b3L, 0x319ee9d5L, 0xc021b8f7L,
149
    0x9b540b19L, 0x875fa099L, 0x95f7997eL, 0x623d7da8L,
150
    0xf837889aL, 0x97e32d77L, 0x11ed935fL, 0x16681281L,
151
    0x0e358829L, 0xc7e61fd6L, 0x96dedfa1L, 0x7858ba99L,
152
    0x57f584a5L, 0x1b227263L, 0x9b83c3ffL, 0x1ac24696L,
153
    0xcdb30aebL, 0x532e3054L, 0x8fd948e4L, 0x6dbc3128L,
154
    0x58ebf2efL, 0x34c6ffeaL, 0xfe28ed61L, 0xee7c3c73L,
155
    0x5d4a14d9L, 0xe864b7e3L, 0x42105d14L, 0x203e13e0L,
156
    0x45eee2b6L, 0xa3aaabeaL, 0xdb6c4f15L, 0xfacb4fd0L,
157
    0xc742f442L, 0xef6abbb5L, 0x654f3b1dL, 0x41cd2105L,
158
    0xd81e799eL, 0x86854dc7L, 0xe44b476aL, 0x3d816250L,
159
    0xcf62a1f2L, 0x5b8d2646L, 0xfc8883a0L, 0xc1c7b6a3L,
160
    0x7f1524c3L, 0x69cb7492L, 0x47848a0bL, 0x5692b285L,
161
    0x095bbf00L, 0xad19489dL, 0x1462b174L, 0x23820e00L,
162
    0x58428d2aL, 0x0c55f5eaL, 0x1dadf43eL, 0x233f7061L,
163
    0x3372f092L, 0x8d937e41L, 0xd65fecf1L, 0x6c223bdbL,
164
    0x7cde3759L, 0xcbee7460L, 0x4085f2a7L, 0xce77326eL,
165
    0xa6078084L, 0x19f8509eL, 0xe8efd855L, 0x61d99735L,
166
    0xa969a7aaL, 0xc50c06c2L, 0x5a04abfcL, 0x800bcadcL,
167
    0x9e447a2eL, 0xc3453484L, 0xfdd56705L, 0x0e1e9ec9L,
168
    0xdb73dbd3L, 0x105588cdL, 0x675fda79L, 0xe3674340L,
169
    0xc5c43465L, 0x713e38d8L, 0x3d28f89eL, 0xf16dff20L,
170
    0x153e21e7L, 0x8fb03d4aL, 0xe6e39f2bL, 0xdb83adf7L },
171
  { 0xe93d5a68L, 0x948140f7L, 0xf64c261cL, 0x94692934L,
172
    0x411520f7L, 0x7602d4f7L, 0xbcf46b2eL, 0xd4a20068L,
173
    0xd4082471L, 0x3320f46aL, 0x43b7d4b7L, 0x500061afL,
174
    0x1e39f62eL, 0x97244546L, 0x14214f74L, 0xbf8b8840L,
175
    0x4d95fc1dL, 0x96b591afL, 0x70f4ddd3L, 0x66a02f45L,
176
    0xbfbc09ecL, 0x03bd9785L, 0x7fac6dd0L, 0x31cb8504L,
177
    0x96eb27b3L, 0x55fd3941L, 0xda2547e6L, 0xabca0a9aL,
178
    0x28507825L, 0x530429f4L, 0x0a2c86daL, 0xe9b66dfbL,
179
    0x68dc1462L, 0xd7486900L, 0x680ec0a4L, 0x27a18deeL,
180
    0x4f3ffea2L, 0xe887ad8cL, 0xb58ce006L, 0x7af4d6b6L,
181
    0xaace1e7cL, 0xd3375fecL, 0xce78a399L, 0x406b2a42L,
182
    0x20fe9e35L, 0xd9f385b9L, 0xee39d7abL, 0x3b124e8bL,
183
    0x1dc9faf7L, 0x4b6d1856L, 0x26a36631L, 0xeae397b2L,
184
    0x3a6efa74L, 0xdd5b4332L, 0x6841e7f7L, 0xca7820fbL,
185
    0xfb0af54eL, 0xd8feb397L, 0x454056acL, 0xba489527L,
186
    0x55533a3aL, 0x20838d87L, 0xfe6ba9b7L, 0xd096954bL,
187
    0x55a867bcL, 0xa1159a58L, 0xcca92963L, 0x99e1db33L,
188
    0xa62a4a56L, 0x3f3125f9L, 0x5ef47e1cL, 0x9029317cL,
189
    0xfdf8e802L, 0x04272f70L, 0x80bb155cL, 0x05282ce3L,
190
    0x95c11548L, 0xe4c66d22L, 0x48c1133fL, 0xc70f86dcL,
191
    0x07f9c9eeL, 0x41041f0fL, 0x404779a4L, 0x5d886e17L,
192
    0x325f51ebL, 0xd59bc0d1L, 0xf2bcc18fL, 0x41113564L,
193
    0x257b7834L, 0x602a9c60L, 0xdff8e8a3L, 0x1f636c1bL,
194
    0x0e12b4c2L, 0x02e1329eL, 0xaf664fd1L, 0xcad18115L,
195
    0x6b2395e0L, 0x333e92e1L, 0x3b240b62L, 0xeebeb922L,
196
    0x85b2a20eL, 0xe6ba0d99L, 0xde720c8cL, 0x2da2f728L,
197
    0xd0127845L, 0x95b794fdL, 0x647d0862L, 0xe7ccf5f0L,
198
    0x5449a36fL, 0x877d48faL, 0xc39dfd27L, 0xf33e8d1eL,
199
    0x0a476341L, 0x992eff74L, 0x3a6f6eabL, 0xf4f8fd37L,
200
    0xa812dc60L, 0xa1ebddf8L, 0x991be14cL, 0xdb6e6b0dL,
201
    0xc67b5510L, 0x6d672c37L, 0x2765d43bL, 0xdcd0e804L,
202
    0xf1290dc7L, 0xcc00ffa3L, 0xb5390f92L, 0x690fed0bL,
203
    0x667b9ffbL, 0xcedb7d9cL, 0xa091cf0bL, 0xd9155ea3L,
204
    0xbb132f88L, 0x515bad24L, 0x7b9479bfL, 0x763bd6ebL,
205
    0x37392eb3L, 0xcc115979L, 0x8026e297L, 0xf42e312dL,
206
    0x6842ada7L, 0xc66a2b3bL, 0x12754cccL, 0x782ef11cL,
207
    0x6a124237L, 0xb79251e7L, 0x06a1bbe6L, 0x4bfb6350L,
208
    0x1a6b1018L, 0x11caedfaL, 0x3d25bdd8L, 0xe2e1c3c9L,
209
    0x44421659L, 0x0a121386L, 0xd90cec6eL, 0xd5abea2aL,
210
    0x64af674eL, 0xda86a85fL, 0xbebfe988L, 0x64e4c3feL,
211
    0x9dbc8057L, 0xf0f7c086L, 0x60787bf8L, 0x6003604dL,
212
    0xd1fd8346L, 0xf6381fb0L, 0x7745ae04L, 0xd736fcccL,
213
    0x83426b33L, 0xf01eab71L, 0xb0804187L, 0x3c005e5fL,
214
    0x77a057beL, 0xbde8ae24L, 0x55464299L, 0xbf582e61L,
215
    0x4e58f48fL, 0xf2ddfda2L, 0xf474ef38L, 0x8789bdc2L,
216
    0x5366f9c3L, 0xc8b38e74L, 0xb475f255L, 0x46fcd9b9L,
217
    0x7aeb2661L, 0x8b1ddf84L, 0x846a0e79L, 0x915f95e2L,
218
    0x466e598eL, 0x20b45770L, 0x8cd55591L, 0xc902de4cL,
219
    0xb90bace1L, 0xbb8205d0L, 0x11a86248L, 0x7574a99eL,
220
    0xb77f19b6L, 0xe0a9dc09L, 0x662d09a1L, 0xc4324633L,
221
    0xe85a1f02L, 0x09f0be8cL, 0x4a99a025L, 0x1d6efe10L,
222
    0x1ab93d1dL, 0x0ba5a4dfL, 0xa186f20fL, 0x2868f169L,
223
    0xdcb7da83L, 0x573906feL, 0xa1e2ce9bL, 0x4fcd7f52L,
224
    0x50115e01L, 0xa70683faL, 0xa002b5c4L, 0x0de6d027L,
225
    0x9af88c27L, 0x773f8641L, 0xc3604c06L, 0x61a806b5L,
226
    0xf0177a28L, 0xc0f586e0L, 0x006058aaL, 0x30dc7d62L,
227
    0x11e69ed7L, 0x2338ea63L, 0x53c2dd94L, 0xc2c21634L,
228
    0xbbcbee56L, 0x90bcb6deL, 0xebfc7da1L, 0xce591d76L,
229
    0x6f05e409L, 0x4b7c0188L, 0x39720a3dL, 0x7c927c24L,
230
    0x86e3725fL, 0x724d9db9L, 0x1ac15bb4L, 0xd39eb8fcL,
231
    0xed545578L, 0x08fca5b5L, 0xd83d7cd3L, 0x4dad0fc4L,
232
    0x1e50ef5eL, 0xb161e6f8L, 0xa28514d9L, 0x6c51133cL,
233
    0x6fd5c7e7L, 0x56e14ec4L, 0x362abfceL, 0xddc6c837L,
234
    0xd79a3234L, 0x92638212L, 0x670efa8eL, 0x406000e0L },
235
  { 0x3a39ce37L, 0xd3faf5cfL, 0xabc27737L, 0x5ac52d1bL,
236
    0x5cb0679eL, 0x4fa33742L, 0xd3822740L, 0x99bc9bbeL,
237
    0xd5118e9dL, 0xbf0f7315L, 0xd62d1c7eL, 0xc700c47bL,
238
    0xb78c1b6bL, 0x21a19045L, 0xb26eb1beL, 0x6a366eb4L,
239
    0x5748ab2fL, 0xbc946e79L, 0xc6a376d2L, 0x6549c2c8L,
240
    0x530ff8eeL, 0x468dde7dL, 0xd5730a1dL, 0x4cd04dc6L,
241
    0x2939bbdbL, 0xa9ba4650L, 0xac9526e8L, 0xbe5ee304L,
242
    0xa1fad5f0L, 0x6a2d519aL, 0x63ef8ce2L, 0x9a86ee22L,
243
    0xc089c2b8L, 0x43242ef6L, 0xa51e03aaL, 0x9cf2d0a4L,
244
    0x83c061baL, 0x9be96a4dL, 0x8fe51550L, 0xba645bd6L,
245
    0x2826a2f9L, 0xa73a3ae1L, 0x4ba99586L, 0xef5562e9L,
246
    0xc72fefd3L, 0xf752f7daL, 0x3f046f69L, 0x77fa0a59L,
247
    0x80e4a915L, 0x87b08601L, 0x9b09e6adL, 0x3b3ee593L,
248
    0xe990fd5aL, 0x9e34d797L, 0x2cf0b7d9L, 0x022b8b51L,
249
    0x96d5ac3aL, 0x017da67dL, 0xd1cf3ed6L, 0x7c7d2d28L,
250
    0x1f9f25cfL, 0xadf2b89bL, 0x5ad6b472L, 0x5a88f54cL,
251
    0xe029ac71L, 0xe019a5e6L, 0x47b0acfdL, 0xed93fa9bL,
252
    0xe8d3c48dL, 0x283b57ccL, 0xf8d56629L, 0x79132e28L,
253
    0x785f0191L, 0xed756055L, 0xf7960e44L, 0xe3d35e8cL,
254
    0x15056dd4L, 0x88f46dbaL, 0x03a16125L, 0x0564f0bdL,
255
    0xc3eb9e15L, 0x3c9057a2L, 0x97271aecL, 0xa93a072aL,
256
    0x1b3f6d9bL, 0x1e6321f5L, 0xf59c66fbL, 0x26dcf319L,
257
    0x7533d928L, 0xb155fdf5L, 0x03563482L, 0x8aba3cbbL,
258
    0x28517711L, 0xc20ad9f8L, 0xabcc5167L, 0xccad925fL,
259
    0x4de81751L, 0x3830dc8eL, 0x379d5862L, 0x9320f991L,
260
    0xea7a90c2L, 0xfb3e7bceL, 0x5121ce64L, 0x774fbe32L,
261
    0xa8b6e37eL, 0xc3293d46L, 0x48de5369L, 0x6413e680L,
262
    0xa2ae0810L, 0xdd6db224L, 0x69852dfdL, 0x09072166L,
263
    0xb39a460aL, 0x6445c0ddL, 0x586cdecfL, 0x1c20c8aeL,
264
    0x5bbef7ddL, 0x1b588d40L, 0xccd2017fL, 0x6bb4e3bbL,
265
    0xdda26a7eL, 0x3a59ff45L, 0x3e350a44L, 0xbcb4cdd5L,
266
    0x72eacea8L, 0xfa6484bbL, 0x8d6612aeL, 0xbf3c6f47L,
267
    0xd29be463L, 0x542f5d9eL, 0xaec2771bL, 0xf64e6370L,
268
    0x740e0d8dL, 0xe75b1357L, 0xf8721671L, 0xaf537d5dL,
269
    0x4040cb08L, 0x4eb4e2ccL, 0x34d2466aL, 0x0115af84L,
270
    0xe1b00428L, 0x95983a1dL, 0x06b89fb4L, 0xce6ea048L,
271
    0x6f3f3b82L, 0x3520ab82L, 0x011a1d4bL, 0x277227f8L,
272
    0x611560b1L, 0xe7933fdcL, 0xbb3a792bL, 0x344525bdL,
273
    0xa08839e1L, 0x51ce794bL, 0x2f32c9b7L, 0xa01fbac9L,
274
    0xe01cc87eL, 0xbcc7d1f6L, 0xcf0111c3L, 0xa1e8aac7L,
275
    0x1a908749L, 0xd44fbd9aL, 0xd0dadecbL, 0xd50ada38L,
276
    0x0339c32aL, 0xc6913667L, 0x8df9317cL, 0xe0b12b4fL,
277
    0xf79e59b7L, 0x43f5bb3aL, 0xf2d519ffL, 0x27d9459cL,
278
    0xbf97222cL, 0x15e6fc2aL, 0x0f91fc71L, 0x9b941525L,
279
    0xfae59361L, 0xceb69cebL, 0xc2a86459L, 0x12baa8d1L,
280
    0xb6c1075eL, 0xe3056a0cL, 0x10d25065L, 0xcb03a442L,
281
    0xe0ec6e0eL, 0x1698db3bL, 0x4c98a0beL, 0x3278e964L,
282
    0x9f1f9532L, 0xe0d392dfL, 0xd3a0342bL, 0x8971f21eL,
283
    0x1b0a7441L, 0x4ba3348cL, 0xc5be7120L, 0xc37632d8L,
284
    0xdf359f8dL, 0x9b992f2eL, 0xe60b6f47L, 0x0fe3f11dL,
285
    0xe54cda54L, 0x1edad891L, 0xce6279cfL, 0xcd3e7e6fL,
286
    0x1618b166L, 0xfd2c1d05L, 0x848fd2c5L, 0xf6fb2299L,
287
    0xf523f357L, 0xa6327623L, 0x93a83531L, 0x56cccd02L,
288
    0xacf08162L, 0x5a75ebb5L, 0x6e163697L, 0x88d273ccL,
289
    0xde966292L, 0x81b949d0L, 0x4c50901bL, 0x71c65614L,
290
    0xe6c6c7bdL, 0x327a140aL, 0x45e1d006L, 0xc3f27b9aL,
291
    0xc9aa53fdL, 0x62a80f00L, 0xbb25bfe2L, 0x35bdd2f6L,
292
    0x71126905L, 0xb2040222L, 0xb6cbcf7cL, 0xcd769c2bL,
293
    0x53113ec0L, 0x1640e3d3L, 0x38abbd60L, 0x2547adf0L,
294
    0xba38209cL, 0xf746ce76L, 0x77afa1c5L, 0x20756060L,
295
    0x85cbfe4eL, 0x8ae88dd8L, 0x7aaaf9b0L, 0x4cf9aa7eL,
296
    0x1948c25cL, 0x02fb8a8cL, 0x01c36ae4L, 0xd6ebe1f9L,
297
    0x90d4f869L, 0xa65cdea0L, 0x3f09252dL, 0xc208e69fL,
298
    0xb74e6132L, 0xce77e25bL, 0x578fdfe3L, 0x3ac372e6L } };
299
300
/* Creates a Blowfish context
301
 * Make sure the value context is referencing, is set to NULL
302
 * Returns 1 if successful or -1 on error
303
 */
304
int libfcrypto_blowfish_context_initialize(
305
     libfcrypto_blowfish_context_t **context,
306
     libcerror_error_t **error )
307
112
{
308
112
  libfcrypto_internal_blowfish_context_t *internal_context = NULL;
309
112
  static char *function                                    = "libfcrypto_blowfish_context_initialize";
310
311
112
  if( context == NULL )
312
0
  {
313
0
    libcerror_error_set(
314
0
     error,
315
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
316
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
317
0
     "%s: invalid context.",
318
0
     function );
319
320
0
    return( -1 );
321
0
  }
322
112
  if( *context != NULL )
323
0
  {
324
0
    libcerror_error_set(
325
0
     error,
326
0
     LIBCERROR_ERROR_DOMAIN_RUNTIME,
327
0
     LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
328
0
     "%s: invalid context value already set.",
329
0
     function );
330
331
0
    return( -1 );
332
0
  }
333
112
  internal_context = memory_allocate_structure(
334
112
                      libfcrypto_internal_blowfish_context_t );
335
336
112
  if( internal_context == NULL )
337
0
  {
338
0
    libcerror_error_set(
339
0
     error,
340
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
341
0
     LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
342
0
     "%s: unable to create context.",
343
0
     function );
344
345
0
    goto on_error;
346
0
  }
347
112
  if( memory_set(
348
112
       internal_context,
349
112
       0,
350
112
       sizeof( libfcrypto_internal_blowfish_context_t ) ) == NULL )
351
0
  {
352
0
    libcerror_error_set(
353
0
     error,
354
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
355
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
356
0
     "%s: unable to clear context.",
357
0
     function );
358
359
0
    goto on_error;
360
0
  }
361
112
  *context = (libfcrypto_blowfish_context_t *) internal_context;
362
363
112
  return( 1 );
364
365
0
on_error:
366
0
  if( internal_context != NULL )
367
0
  {
368
0
    memory_free(
369
0
     internal_context );
370
0
  }
371
0
  return( -1 );
372
112
}
373
374
/* Frees a Blowfish context
375
 * Returns 1 if successful or -1 on error
376
 */
377
int libfcrypto_blowfish_context_free(
378
     libfcrypto_blowfish_context_t **context,
379
     libcerror_error_t **error )
380
112
{
381
112
  libfcrypto_internal_blowfish_context_t *internal_context = NULL;
382
112
  static char *function                                    = "libfcrypto_blowfish_context_free";
383
112
  int result                                               = 1;
384
385
112
  if( context == NULL )
386
0
  {
387
0
    libcerror_error_set(
388
0
     error,
389
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
390
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
391
0
     "%s: invalid context.",
392
0
     function );
393
394
0
    return( -1 );
395
0
  }
396
112
  if( *context != NULL )
397
112
  {
398
112
    internal_context = (libfcrypto_internal_blowfish_context_t *) *context;
399
112
    *context         = NULL;
400
401
112
    if( memory_set(
402
112
         internal_context,
403
112
         0,
404
112
         sizeof( libfcrypto_internal_blowfish_context_t ) ) == NULL )
405
0
    {
406
0
      libcerror_error_set(
407
0
       error,
408
0
       LIBCERROR_ERROR_DOMAIN_MEMORY,
409
0
       LIBCERROR_MEMORY_ERROR_SET_FAILED,
410
0
       "%s: unable to clear context.",
411
0
       function );
412
413
0
      result = -1;
414
0
    }
415
112
    memory_free(
416
112
     internal_context );
417
112
  }
418
112
  return( result );
419
112
}
420
421
/* Encrypts values using Blowfish
422
 * Returns 1 if successful or -1 on error
423
 */
424
int libfcrypto_internal_blowfish_context_encrypt_values(
425
     libfcrypto_internal_blowfish_context_t *internal_context,
426
     uint32_t *value_left,
427
     uint32_t *value_right,
428
     libcerror_error_t **error )
429
58.3k
{
430
58.3k
  static char *function     = "libfcrypto_internal_blowfish_context_encrypt_values";
431
58.3k
  uint32_t value_32bit      = 0;
432
58.3k
  uint32_t safe_value_left  = 0;
433
58.3k
  uint32_t safe_value_right = 0;
434
58.3k
  uint8_t byte_value1       = 0;
435
58.3k
  uint8_t byte_value2       = 0;
436
58.3k
  uint8_t byte_value3       = 0;
437
58.3k
  uint8_t byte_value4       = 0;
438
58.3k
  uint8_t encryption_round  = 0;
439
440
58.3k
  if( internal_context == NULL )
441
0
  {
442
0
    libcerror_error_set(
443
0
     error,
444
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
445
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
446
0
     "%s: invalid context.",
447
0
     function );
448
449
0
    return( -1 );
450
0
  }
451
58.3k
  if( value_left == NULL )
452
0
  {
453
0
    libcerror_error_set(
454
0
     error,
455
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
456
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
457
0
     "%s: invalid value left.",
458
0
     function );
459
460
0
    return( -1 );
461
0
  }
462
58.3k
  if( value_right == NULL )
463
0
  {
464
0
    libcerror_error_set(
465
0
     error,
466
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
467
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
468
0
     "%s: invalid value right.",
469
0
     function );
470
471
0
    return( -1 );
472
0
  }
473
58.3k
  safe_value_left  = *value_left;
474
58.3k
  safe_value_right = *value_right;
475
476
58.3k
  for( encryption_round = 0;
477
991k
       encryption_round < 16;
478
933k
       encryption_round++ )
479
933k
  {
480
933k
    safe_value_left ^= internal_context->permutation_values[ encryption_round ];
481
482
933k
    byte_value4 = (uint8_t) ( safe_value_left & 0x000000ffUL );
483
933k
    byte_value3 = (uint8_t) ( ( safe_value_left >> 8 ) & 0x000000ffUL );
484
933k
    byte_value2 = (uint8_t) ( ( safe_value_left >> 16 ) & 0x000000ffUL );
485
933k
    byte_value1 = (uint8_t) ( ( safe_value_left >> 24 ) & 0x000000ffUL );
486
487
933k
    value_32bit  = internal_context->sboxes[ 0 ][ byte_value1 ];
488
933k
    value_32bit += internal_context->sboxes[ 1 ][ byte_value2 ];
489
933k
    value_32bit ^= internal_context->sboxes[ 2 ][ byte_value3 ];
490
933k
    value_32bit += internal_context->sboxes[ 3 ][ byte_value4 ];
491
492
933k
    safe_value_right ^= value_32bit;
493
494
933k
    value_32bit      = safe_value_left;
495
933k
    safe_value_left  = safe_value_right;
496
933k
    safe_value_right = value_32bit;
497
933k
  }
498
58.3k
  value_32bit      = safe_value_left;
499
58.3k
  safe_value_left  = safe_value_right;
500
58.3k
  safe_value_right = value_32bit;
501
502
58.3k
  safe_value_right ^= internal_context->permutation_values[ 16 ];
503
58.3k
  safe_value_left  ^= internal_context->permutation_values[ 17 ];
504
505
58.3k
  *value_left  = safe_value_left;
506
58.3k
  *value_right = safe_value_right;
507
508
58.3k
  return( 1 );
509
58.3k
}
510
511
/* Sets the key
512
 * Returns 1 if successful or -1 on error
513
 */
514
int libfcrypto_blowfish_context_set_key(
515
     libfcrypto_blowfish_context_t *context,
516
     const uint8_t *key,
517
     size_t key_bit_size,
518
     libcerror_error_t **error )
519
112
{
520
112
  libfcrypto_internal_blowfish_context_t *internal_context = NULL;
521
112
  static char *function                                    = "libfcrypto_blowfish_context_set_key";
522
112
  size_t key_byte_index                                    = 0;
523
112
  size_t key_byte_size                                     = 0;
524
112
  uint32_t permutation_value                               = 0;
525
112
  uint32_t value_left                                      = 0;
526
112
  uint32_t value_right                                     = 0;
527
112
  uint16_t byte_index                                      = 0;
528
112
  uint8_t permutation_value_index                          = 0;
529
112
  uint8_t sbox_index                                       = 0;
530
531
112
  if( context == NULL )
532
0
  {
533
0
    libcerror_error_set(
534
0
     error,
535
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
536
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
537
0
     "%s: invalid context.",
538
0
     function );
539
540
0
    return( -1 );
541
0
  }
542
112
  internal_context = (libfcrypto_internal_blowfish_context_t *) context;
543
544
112
  if( key == NULL )
545
0
  {
546
0
    libcerror_error_set(
547
0
     error,
548
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
549
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
550
0
     "%s: invalid key.",
551
0
     function );
552
553
0
    return( -1 );
554
0
  }
555
112
  if( ( key_bit_size < 32 )
556
112
   || ( key_bit_size > 448 )
557
112
   || ( ( key_bit_size % 8 ) != 0 ) )
558
0
  {
559
0
    libcerror_error_set(
560
0
     error,
561
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
562
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
563
0
     "%s: unsupported key bit size.",
564
0
     function );
565
566
0
    return( -1 );
567
0
  }
568
112
  key_byte_size = key_bit_size / 8;
569
570
/* TODO replace iteration by memory_copy */
571
112
  for( sbox_index = 0;
572
560
       sbox_index < 4;
573
448
       sbox_index++ )
574
448
  {
575
448
    for( byte_index = 0;
576
115k
         byte_index < 256;
577
114k
         byte_index++ )
578
114k
    {
579
114k
      internal_context->sboxes[ sbox_index ][ byte_index ] = libfcrypto_blowfish_initial_sboxes[ sbox_index ][ byte_index ];
580
114k
    }
581
448
  }
582
112
  key_byte_index = 0;
583
584
112
  for( permutation_value_index = 0;
585
2.12k
       permutation_value_index < 18;
586
2.01k
       permutation_value_index++ )
587
2.01k
  {
588
2.01k
    permutation_value = 0;
589
590
2.01k
    for( sbox_index = 0;
591
10.0k
         sbox_index < 4;
592
8.06k
         sbox_index++ )
593
8.06k
    {
594
8.06k
      permutation_value <<= 8;
595
8.06k
      permutation_value  |= key[ key_byte_index++ ];
596
597
8.06k
      if( key_byte_index >= key_byte_size )
598
2.01k
      {
599
2.01k
        key_byte_index = 0;
600
2.01k
      }
601
8.06k
    }
602
2.01k
    internal_context->permutation_values[ permutation_value_index ] = libfcrypto_blowfish_initial_permutation_values[ permutation_value_index ] ^ permutation_value;
603
2.01k
  }
604
112
  permutation_value_index = 0;
605
606
1.12k
  while( permutation_value_index < 18 )
607
1.00k
  {
608
1.00k
    if( libfcrypto_internal_blowfish_context_encrypt_values(
609
1.00k
         internal_context,
610
1.00k
         &value_left,
611
1.00k
         &value_right,
612
1.00k
         error ) != 1 )
613
0
    {
614
0
      libcerror_error_set(
615
0
       error,
616
0
       LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
617
0
       LIBCERROR_ENCRYPTION_ERROR_GENERIC,
618
0
       "%s: unable to encrypt permutation values.",
619
0
       function );
620
621
0
      goto on_error;
622
0
    }
623
1.00k
    internal_context->permutation_values[ permutation_value_index++ ] = value_left;
624
1.00k
    internal_context->permutation_values[ permutation_value_index++ ] = value_right;
625
1.00k
  }
626
112
  for( sbox_index = 0;
627
560
       sbox_index < 4;
628
448
       sbox_index++ )
629
448
  {
630
448
    byte_index = 0;
631
632
57.7k
    while( byte_index < 256 )
633
57.3k
    {
634
57.3k
      if( libfcrypto_internal_blowfish_context_encrypt_values(
635
57.3k
           internal_context,
636
57.3k
           &value_left,
637
57.3k
           &value_right,
638
57.3k
           error ) != 1 )
639
0
      {
640
0
        libcerror_error_set(
641
0
         error,
642
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
643
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
644
0
         "%s: unable to encrypt permutation values.",
645
0
         function );
646
647
0
        goto on_error;
648
0
      }
649
57.3k
      internal_context->sboxes[ sbox_index ][ byte_index++ ] = value_left;
650
57.3k
      internal_context->sboxes[ sbox_index ][ byte_index++ ] = value_right;
651
57.3k
    }
652
448
  }
653
112
  return( 1 );
654
655
0
on_error:
656
0
  memory_set(
657
0
   internal_context->permutation_values,
658
0
   0,
659
0
   sizeof( uint32_t ) * 18 );
660
661
0
  memory_set(
662
0
   internal_context->sboxes,
663
0
   0,
664
0
   sizeof( uint8_t ) * 4 * 256 );
665
666
0
  return( -1 );
667
112
}
668
669
/* Encrypts a block of data using Blowfish
670
 * The size must be a multitude of the Blowfish block size (8 byte)
671
 * Returns 1 if successful or -1 on error
672
 */
673
int libfcrypto_internal_blowfish_context_encrypt_block(
674
     libfcrypto_internal_blowfish_context_t *internal_context,
675
     const uint8_t *input_data,
676
     size_t input_data_size,
677
     uint8_t *output_data,
678
     size_t output_data_size,
679
     libcerror_error_t **error )
680
72
{
681
72
  static char *function    = "libfcrypto_internal_blowfish_context_encrypt_block";
682
72
  uint32_t value_32bit     = 0;
683
72
  uint32_t value_left      = 0;
684
72
  uint32_t value_right     = 0;
685
72
  uint8_t byte_value1      = 0;
686
72
  uint8_t byte_value2      = 0;
687
72
  uint8_t byte_value3      = 0;
688
72
  uint8_t byte_value4      = 0;
689
72
  uint8_t encryption_round = 0;
690
691
72
  if( internal_context == NULL )
692
0
  {
693
0
    libcerror_error_set(
694
0
     error,
695
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
696
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
697
0
     "%s: invalid context.",
698
0
     function );
699
700
0
    return( -1 );
701
0
  }
702
72
  if( input_data == NULL )
703
0
  {
704
0
    libcerror_error_set(
705
0
     error,
706
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
707
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
708
0
     "%s: invalid input data.",
709
0
     function );
710
711
0
    return( -1 );
712
0
  }
713
72
  if( ( input_data_size < 8 )
714
72
   || ( input_data_size > (size_t) SSIZE_MAX ) )
715
0
  {
716
0
    libcerror_error_set(
717
0
     error,
718
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
719
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
720
0
     "%s: invalid input data size value out of bounds.",
721
0
     function );
722
723
0
    return( -1 );
724
0
  }
725
72
  if( output_data == NULL )
726
0
  {
727
0
    libcerror_error_set(
728
0
     error,
729
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
730
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
731
0
     "%s: invalid output data.",
732
0
     function );
733
734
0
    return( -1 );
735
0
  }
736
72
  if( ( output_data_size < 8 )
737
72
   || ( output_data_size > (size_t) SSIZE_MAX ) )
738
0
  {
739
0
    libcerror_error_set(
740
0
     error,
741
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
742
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
743
0
     "%s: invalid output data size value out of bounds.",
744
0
     function );
745
746
0
    return( -1 );
747
0
  }
748
72
  byte_stream_copy_to_uint32_big_endian(
749
72
   &( input_data[ 0 ] ),
750
72
   value_left );
751
752
72
  byte_stream_copy_to_uint32_big_endian(
753
72
   &( input_data[ 4 ] ),
754
72
   value_right );
755
756
72
  for( encryption_round = 0;
757
1.22k
       encryption_round < 16;
758
1.15k
       encryption_round++ )
759
1.15k
  {
760
1.15k
    value_left ^= internal_context->permutation_values[ encryption_round ];
761
762
1.15k
    byte_value4 = (uint8_t) ( value_left & 0x000000ffUL );
763
1.15k
    byte_value3 = (uint8_t) ( ( value_left >> 8 ) & 0x000000ffUL );
764
1.15k
    byte_value2 = (uint8_t) ( ( value_left >> 16 ) & 0x000000ffUL );
765
1.15k
    byte_value1 = (uint8_t) ( ( value_left >> 24 ) & 0x000000ffUL );
766
767
1.15k
    value_32bit  = internal_context->sboxes[ 0 ][ byte_value1 ];
768
1.15k
    value_32bit += internal_context->sboxes[ 1 ][ byte_value2 ];
769
1.15k
    value_32bit ^= internal_context->sboxes[ 2 ][ byte_value3 ];
770
1.15k
    value_32bit += internal_context->sboxes[ 3 ][ byte_value4 ];
771
772
1.15k
    value_right ^= value_32bit;
773
774
1.15k
    value_32bit = value_left;
775
1.15k
    value_left  = value_right;
776
1.15k
    value_right = value_32bit;
777
1.15k
  }
778
72
  value_32bit = value_left;
779
72
  value_left  = value_right;
780
72
  value_right = value_32bit;
781
782
72
  value_right ^= internal_context->permutation_values[ 16 ];
783
72
  value_left  ^= internal_context->permutation_values[ 17 ];
784
785
72
  byte_stream_copy_from_uint32_big_endian(
786
72
   &( output_data[ 0 ] ),
787
72
   value_left );
788
789
72
  byte_stream_copy_from_uint32_big_endian(
790
72
   &( output_data[ 4 ] ),
791
72
   value_right );
792
793
72
  return( 1 );
794
72
}
795
796
/* Decrypt a block of data using Blowfish
797
 * The size must be a multitude of the Blowfish block size (8 byte)
798
 * Returns 1 if successful or -1 on error
799
 */
800
int libfcrypto_internal_blowfish_context_decrypt_block(
801
     libfcrypto_internal_blowfish_context_t *internal_context,
802
     const uint8_t *input_data,
803
     size_t input_data_size,
804
     uint8_t *output_data,
805
     size_t output_data_size,
806
     libcerror_error_t **error )
807
0
{
808
0
  static char *function    = "libfcrypto_internal_blowfish_context_decrypt_block";
809
0
  uint32_t value_32bit     = 0;
810
0
  uint32_t value_left      = 0;
811
0
  uint32_t value_right     = 0;
812
0
  uint8_t byte_value1      = 0;
813
0
  uint8_t byte_value2      = 0;
814
0
  uint8_t byte_value3      = 0;
815
0
  uint8_t byte_value4      = 0;
816
0
  uint8_t encryption_round = 0;
817
818
0
  if( internal_context == NULL )
819
0
  {
820
0
    libcerror_error_set(
821
0
     error,
822
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
823
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
824
0
     "%s: invalid context.",
825
0
     function );
826
827
0
    return( -1 );
828
0
  }
829
0
  if( input_data == NULL )
830
0
  {
831
0
    libcerror_error_set(
832
0
     error,
833
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
834
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
835
0
     "%s: invalid input data.",
836
0
     function );
837
838
0
    return( -1 );
839
0
  }
840
0
  if( ( input_data_size < 8 )
841
0
   || ( input_data_size > (size_t) SSIZE_MAX ) )
842
0
  {
843
0
    libcerror_error_set(
844
0
     error,
845
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
846
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
847
0
     "%s: invalid input data size value out of bounds.",
848
0
     function );
849
850
0
    return( -1 );
851
0
  }
852
0
  if( output_data == NULL )
853
0
  {
854
0
    libcerror_error_set(
855
0
     error,
856
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
857
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
858
0
     "%s: invalid output data.",
859
0
     function );
860
861
0
    return( -1 );
862
0
  }
863
0
  if( ( output_data_size < 8 )
864
0
   || ( output_data_size > (size_t) SSIZE_MAX ) )
865
0
  {
866
0
    libcerror_error_set(
867
0
     error,
868
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
869
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
870
0
     "%s: invalid output data size value out of bounds.",
871
0
     function );
872
873
0
    return( -1 );
874
0
  }
875
0
  byte_stream_copy_to_uint32_big_endian(
876
0
   &( input_data[ 0 ] ),
877
0
   value_left );
878
879
0
  byte_stream_copy_to_uint32_big_endian(
880
0
   &( input_data[ 4 ] ),
881
0
   value_right );
882
883
0
  for( encryption_round = 17;
884
0
       encryption_round > 1;
885
0
       encryption_round-- )
886
0
  {
887
0
    value_left ^= internal_context->permutation_values[ encryption_round ];
888
889
0
    byte_value4 = (uint8_t) ( value_left & 0x000000ffUL );
890
0
    byte_value3 = (uint8_t) ( ( value_left >> 8 ) & 0x000000ffUL );
891
0
    byte_value2 = (uint8_t) ( ( value_left >> 16 ) & 0x000000ffUL );
892
0
    byte_value1 = (uint8_t) ( ( value_left >> 24 ) & 0x000000ffUL );
893
894
0
    value_32bit  = internal_context->sboxes[ 0 ][ byte_value1 ];
895
0
    value_32bit += internal_context->sboxes[ 1 ][ byte_value2 ];
896
0
    value_32bit ^= internal_context->sboxes[ 2 ][ byte_value3 ];
897
0
    value_32bit += internal_context->sboxes[ 3 ][ byte_value4 ];
898
899
0
    value_right ^= value_32bit;
900
901
0
    value_32bit = value_left;
902
0
    value_left  = value_right;
903
0
    value_right = value_32bit;
904
0
  }
905
0
  value_32bit = value_left;
906
0
  value_left  = value_right;
907
0
  value_right = value_32bit;
908
909
0
  value_right ^= internal_context->permutation_values[ 1 ];
910
0
  value_left  ^= internal_context->permutation_values[ 0 ];
911
912
0
  byte_stream_copy_from_uint32_big_endian(
913
0
   &( output_data[ 0 ] ),
914
0
   value_left );
915
916
0
  byte_stream_copy_from_uint32_big_endian(
917
0
   &( output_data[ 4 ] ),
918
0
   value_right );
919
920
0
  return( 1 );
921
0
}
922
923
/* De- or encrypts a block of data using Blowfish-CBC (Cipher Block Chaining)
924
 * The size must be a multitude of the Blowfish block size (8 byte)
925
 * Returns 1 if successful or -1 on error
926
 */
927
int libfcrypto_blowfish_crypt_cbc(
928
     libfcrypto_blowfish_context_t *context,
929
     int mode,
930
     const uint8_t *initialization_vector,
931
     size_t initialization_vector_size,
932
     const uint8_t *input_data,
933
     size_t input_data_size,
934
     uint8_t *output_data,
935
     size_t output_data_size,
936
     libcerror_error_t **error )
937
56
{
938
56
  uint8_t block_data[ 8 ];
939
56
  uint8_t internal_initialization_vector[ 8 ];
940
941
56
  static char *function = "libfcrypto_blowfish_context_crypt_cbc";
942
56
  size_t data_offset    = 0;
943
944
56
#if !defined( LIBFCRYPTO_UNFOLLED_LOOPS )
945
56
  uint8_t block_index   = 0;
946
56
#endif
947
948
56
  if( context == NULL )
949
0
  {
950
0
    libcerror_error_set(
951
0
     error,
952
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
953
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
954
0
     "%s: invalid context.",
955
0
     function );
956
957
0
    return( -1 );
958
0
  }
959
56
  if( initialization_vector == NULL )
960
0
  {
961
0
    libcerror_error_set(
962
0
     error,
963
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
964
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
965
0
     "%s: invalid initialization vector.",
966
0
     function );
967
968
0
    return( -1 );
969
0
  }
970
56
  if( initialization_vector_size != 8 )
971
0
  {
972
0
    libcerror_error_set(
973
0
     error,
974
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
975
0
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
976
0
     "%s: invalid initialization vector size value out of bounds.",
977
0
     function );
978
979
0
    return( -1 );
980
0
  }
981
56
  if( ( mode != LIBFCRYPTO_BLOWFISH_CRYPT_MODE_ENCRYPT )
982
56
   && ( mode != LIBFCRYPTO_BLOWFISH_CRYPT_MODE_DECRYPT ) )
983
0
  {
984
0
    libcerror_error_set(
985
0
     error,
986
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
987
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
988
0
     "%s: unsupported mode.",
989
0
     function );
990
991
0
    return( -1 );
992
0
  }
993
56
  if( input_data == NULL )
994
0
  {
995
0
    libcerror_error_set(
996
0
     error,
997
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
998
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
999
0
     "%s: invalid input data.",
1000
0
     function );
1001
1002
0
    return( -1 );
1003
0
  }
1004
  /* Check if the input data size is a multitude of 8-byte
1005
   */
1006
56
  if( ( ( input_data_size & (size_t) 0x07 ) != 0 )
1007
56
   || ( input_data_size < 8 )
1008
56
   || ( input_data_size > (size_t) SSIZE_MAX ) )
1009
3
  {
1010
3
    libcerror_error_set(
1011
3
     error,
1012
3
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1013
3
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1014
3
     "%s: invalid input data size value out of bounds.",
1015
3
     function );
1016
1017
3
    return( -1 );
1018
3
  }
1019
53
  if( output_data == NULL )
1020
0
  {
1021
0
    libcerror_error_set(
1022
0
     error,
1023
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1024
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1025
0
     "%s: invalid output data.",
1026
0
     function );
1027
1028
0
    return( -1 );
1029
0
  }
1030
53
  if( ( output_data_size < input_data_size )
1031
53
   || ( output_data_size > (size_t) SSIZE_MAX ) )
1032
45
  {
1033
45
    libcerror_error_set(
1034
45
     error,
1035
45
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1036
45
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1037
45
     "%s: invalid output data size value out of bounds.",
1038
45
     function );
1039
1040
45
    return( -1 );
1041
45
  }
1042
8
  if( memory_copy(
1043
8
       internal_initialization_vector,
1044
8
       initialization_vector,
1045
8
       8 ) == NULL )
1046
0
  {
1047
0
    libcerror_error_set(
1048
0
     error,
1049
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
1050
0
     LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1051
0
     "%s: unable to copy initialization vector.",
1052
0
     function );
1053
1054
0
    goto on_error;
1055
0
  }
1056
44
  while( data_offset < input_data_size )
1057
36
  {
1058
36
    if( mode == LIBFCRYPTO_BLOWFISH_CRYPT_MODE_ENCRYPT )
1059
36
    {
1060
#if defined( LIBFCRYPTO_UNFOLLED_LOOPS )
1061
      block_data[ 0 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 0 ];
1062
      block_data[ 1 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 1 ];
1063
      block_data[ 2 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 2 ];
1064
      block_data[ 3 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 3 ];
1065
      block_data[ 4 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 4 ];
1066
      block_data[ 5 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 5 ];
1067
      block_data[ 6 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 6 ];
1068
      block_data[ 7 ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ 7 ];
1069
#else
1070
36
      for( block_index = 0;
1071
324
           block_index < 8;
1072
288
           block_index++ )
1073
288
      {
1074
288
        block_data[ block_index ] = input_data[ data_offset++ ] ^ internal_initialization_vector[ block_index ];
1075
288
      }
1076
36
#endif
1077
36
      data_offset -= 8;
1078
1079
36
      if( libfcrypto_internal_blowfish_context_encrypt_block(
1080
36
           (libfcrypto_internal_blowfish_context_t *) context,
1081
36
           block_data,
1082
36
           8,
1083
36
           &( output_data[ data_offset ] ),
1084
36
           8,
1085
36
           error ) != 1 )
1086
0
      {
1087
0
        libcerror_error_set(
1088
0
         error,
1089
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
1090
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
1091
0
         "%s: unable to encrypt input data.",
1092
0
         function );
1093
1094
0
        goto on_error;
1095
0
      }
1096
36
      if( memory_copy(
1097
36
           internal_initialization_vector,
1098
36
           &( output_data[ data_offset ] ),
1099
36
           8 ) == NULL )
1100
0
      {
1101
0
        libcerror_error_set(
1102
0
         error,
1103
0
         LIBCERROR_ERROR_DOMAIN_MEMORY,
1104
0
         LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1105
0
         "%s: unable to copy encrypted output data to initialization vector.",
1106
0
         function );
1107
1108
0
        goto on_error;
1109
0
      }
1110
36
    }
1111
0
    else
1112
0
    {
1113
0
      if( libfcrypto_internal_blowfish_context_decrypt_block(
1114
0
           (libfcrypto_internal_blowfish_context_t *) context,
1115
0
           &( input_data[ data_offset ] ),
1116
0
           8,
1117
0
           &( output_data[ data_offset ] ),
1118
0
           8,
1119
0
           error ) != 1 )
1120
0
      {
1121
0
        libcerror_error_set(
1122
0
         error,
1123
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
1124
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
1125
0
         "%s: unable to decrypt input data.",
1126
0
         function );
1127
1128
0
        goto on_error;
1129
0
      }
1130
#if defined( LIBFCRYPTO_UNFOLLED_LOOPS )
1131
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 0 ];
1132
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 1 ];
1133
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 2 ];
1134
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 3 ];
1135
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 4 ];
1136
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 5 ];
1137
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 6 ];
1138
      output_data[ data_offset++ ] ^= internal_initialization_vector[ 7 ];
1139
#else
1140
0
      for( block_index = 0;
1141
0
           block_index < 8;
1142
0
           block_index++ )
1143
0
      {
1144
0
        output_data[ data_offset++ ] ^= internal_initialization_vector[ block_index ];
1145
0
      }
1146
0
#endif
1147
0
      data_offset -= 8;
1148
1149
0
      if( memory_copy(
1150
0
           internal_initialization_vector,
1151
0
           &( input_data[ data_offset ] ),
1152
0
           8 ) == NULL )
1153
0
      {
1154
0
        libcerror_error_set(
1155
0
         error,
1156
0
         LIBCERROR_ERROR_DOMAIN_MEMORY,
1157
0
         LIBCERROR_MEMORY_ERROR_COPY_FAILED,
1158
0
         "%s: unable to copy encrypted input data to initialization vector.",
1159
0
         function );
1160
1161
0
        goto on_error;
1162
0
      }
1163
0
    }
1164
36
    data_offset += 8;
1165
36
  }
1166
8
  if( memory_set(
1167
8
       internal_initialization_vector,
1168
8
       0,
1169
8
       8 ) == NULL )
1170
0
  {
1171
0
    libcerror_error_set(
1172
0
     error,
1173
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
1174
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
1175
0
     "%s: unable to clear initialization vector.",
1176
0
     function );
1177
1178
0
    goto on_error;
1179
0
  }
1180
8
  if( memory_set(
1181
8
       block_data,
1182
8
       0,
1183
8
       8 ) == NULL )
1184
0
  {
1185
0
    libcerror_error_set(
1186
0
     error,
1187
0
     LIBCERROR_ERROR_DOMAIN_MEMORY,
1188
0
     LIBCERROR_MEMORY_ERROR_SET_FAILED,
1189
0
     "%s: unable to clear block data.",
1190
0
     function );
1191
1192
0
    goto on_error;
1193
0
  }
1194
8
  return( 1 );
1195
1196
0
on_error:
1197
0
  memory_set(
1198
0
   internal_initialization_vector,
1199
0
   0,
1200
0
   8 );
1201
1202
0
  memory_set(
1203
0
   block_data,
1204
0
   0,
1205
0
   8 );
1206
1207
0
  return( -1 );
1208
8
}
1209
1210
/* De- or encrypts a block of data using Blowfish-ECB (Electronic CodeBook)
1211
 * The size must be a multitude of the Blowfish block size (8 byte)
1212
 * Returns 1 if successful or -1 on error
1213
 */
1214
int libfcrypto_blowfish_crypt_ecb(
1215
     libfcrypto_blowfish_context_t *context,
1216
     int mode,
1217
     const uint8_t *input_data,
1218
     size_t input_data_size,
1219
     uint8_t *output_data,
1220
     size_t output_data_size,
1221
     libcerror_error_t **error )
1222
56
{
1223
56
  static char *function = "libfcrypto_blowfish_context_crypt_ecb";
1224
56
  size_t data_offset    = 0;
1225
1226
56
  if( context == NULL )
1227
0
  {
1228
0
    libcerror_error_set(
1229
0
     error,
1230
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1231
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1232
0
     "%s: invalid context.",
1233
0
     function );
1234
1235
0
    return( -1 );
1236
0
  }
1237
56
  if( ( mode != LIBFCRYPTO_BLOWFISH_CRYPT_MODE_ENCRYPT )
1238
56
   && ( mode != LIBFCRYPTO_BLOWFISH_CRYPT_MODE_DECRYPT ) )
1239
0
  {
1240
0
    libcerror_error_set(
1241
0
     error,
1242
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1243
0
     LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
1244
0
     "%s: unsupported mode.",
1245
0
     function );
1246
1247
0
    return( -1 );
1248
0
  }
1249
56
  if( input_data == NULL )
1250
0
  {
1251
0
    libcerror_error_set(
1252
0
     error,
1253
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1254
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1255
0
     "%s: invalid input data.",
1256
0
     function );
1257
1258
0
    return( -1 );
1259
0
  }
1260
  /* Check if the input data size is a multitude of 8-byte
1261
   */
1262
56
  if( ( ( input_data_size & (size_t) 0x07 ) != 0 )
1263
56
   || ( input_data_size < 8 )
1264
56
   || ( input_data_size > (size_t) SSIZE_MAX ) )
1265
3
  {
1266
3
    libcerror_error_set(
1267
3
     error,
1268
3
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1269
3
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1270
3
     "%s: invalid input data size value out of bounds.",
1271
3
     function );
1272
1273
3
    return( -1 );
1274
3
  }
1275
53
  if( output_data == NULL )
1276
0
  {
1277
0
    libcerror_error_set(
1278
0
     error,
1279
0
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1280
0
     LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
1281
0
     "%s: invalid output data.",
1282
0
     function );
1283
1284
0
    return( -1 );
1285
0
  }
1286
53
  if( ( output_data_size < input_data_size )
1287
53
   || ( output_data_size > (size_t) SSIZE_MAX ) )
1288
45
  {
1289
45
    libcerror_error_set(
1290
45
     error,
1291
45
     LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
1292
45
     LIBCERROR_ARGUMENT_ERROR_VALUE_OUT_OF_BOUNDS,
1293
45
     "%s: invalid output data size value out of bounds.",
1294
45
     function );
1295
1296
45
    return( -1 );
1297
45
  }
1298
44
  while( data_offset < input_data_size )
1299
36
  {
1300
36
    if( mode == LIBFCRYPTO_BLOWFISH_CRYPT_MODE_ENCRYPT )
1301
36
    {
1302
36
      if( libfcrypto_internal_blowfish_context_encrypt_block(
1303
36
           (libfcrypto_internal_blowfish_context_t *) context,
1304
36
           &( input_data[ data_offset ] ),
1305
36
           8,
1306
36
           &( output_data[ data_offset ] ),
1307
36
           8,
1308
36
           error ) != 1 )
1309
0
      {
1310
0
        libcerror_error_set(
1311
0
         error,
1312
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
1313
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
1314
0
         "%s: unable to encrypt input data.",
1315
0
         function );
1316
1317
0
        return( -1 );
1318
0
      }
1319
36
    }
1320
0
    else
1321
0
    {
1322
0
      if( libfcrypto_internal_blowfish_context_decrypt_block(
1323
0
           (libfcrypto_internal_blowfish_context_t *) context,
1324
0
           &( input_data[ data_offset ] ),
1325
0
           8,
1326
0
           &( output_data[ data_offset ] ),
1327
0
           8,
1328
0
           error ) != 1 )
1329
0
      {
1330
0
        libcerror_error_set(
1331
0
         error,
1332
0
         LIBCERROR_ERROR_DOMAIN_ENCRYPTION,
1333
0
         LIBCERROR_ENCRYPTION_ERROR_GENERIC,
1334
0
         "%s: unable to decrypt input data.",
1335
0
         function );
1336
1337
0
        return( -1 );
1338
0
      }
1339
0
    }
1340
36
    data_offset += 8;
1341
36
  }
1342
8
  return( 1 );
1343
8
}
1344