PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
password.c
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# Copyright 2001-2004, ps2dev - http://www.ps2dev.org
7# Licenced under Academic Free License version 2.0
8# Review ps2sdk README & LICENSE files for further details.
9#
10# Password-handling routines
11*/
12
13#include <errno.h>
14#include <iomanX.h>
15#ifdef _IOP
16#include <sysclib.h>
17#else
18#include <string.h>
19#endif
20#include <stdio.h>
21#include <hdd-ioctl.h>
22
23#include "libapa.h"
24
25int apaPassCmp(const char *pw1, const char *pw2)
26{
27#ifdef APA_ENABLE_PASSWORDS
28 return memcmp(pw1, pw2, APA_PASSMAX) ? -EACCES : 0;
29#else
30 (void)pw1;
31 (void)pw2;
32 //Passwords are not supported, hence this check should always pass.
33 return 0;
34#endif
35}
36
37static void DESEncryptPassword(u32 id_lo, u32 id_hi, char *password_out, const char *password);
38
39void apaEncryptPassword(const char *id, char *password_out, const char *password_in)
40{
41 char password[APA_PASSMAX];
42 memcpy(password, password_in, APA_PASSMAX);
43 DESEncryptPassword(*(u32*)(id), *(u32*)(id + 4), password_out, password);
44}
45
46struct KeyPair{
47 u32 lo, hi;
48};
49
50//This is a standard DES-ECB implementation. It encrypts the partition ID with the password.
51static void DESEncryptPassword(u32 id_lo, u32 id_hi, char *password_out, const char *password)
52{
53 //Left
54 static const u8 PC1[]={ 0x39, 0x31, 0x29, 0x21, 0x19, 0x11, 0x09,
55 0x01, 0x3a, 0x32, 0x2a, 0x22, 0x1a, 0x12,
56 0x0a, 0x02, 0x3b, 0x33, 0x2b, 0x23, 0x1b,
57 0x13, 0x0b, 0x03, 0x3c, 0x34, 0x2c, 0x24,
58 //Right
59 0x3f, 0x37, 0x2f, 0x27, 0x1f, 0x17, 0x0f,
60 0x07, 0x3e, 0x36, 0x2e, 0x26, 0x1e, 0x16,
61 0x0e, 0x06, 0x3d, 0x35, 0x2d, 0x25, 0x1d,
62 0x15, 0x0d, 0x05, 0x1c, 0x14, 0x0c, 0x04 };
63 static const u8 Rotations[]={ 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
64 static const u8 PC2[]={ 0x0e, 0x11, 0x0b, 0x18, 0x01, 0x05,
65 0x03, 0x1c, 0x0f, 0x06, 0x15, 0x0a,
66 0x17, 0x13, 0x0c, 0x04, 0x1a, 0x08,
67 0x10, 0x07, 0x1b, 0x14, 0x0d, 0x02,
68 0x29, 0x34, 0x1f, 0x25, 0x2f, 0x37,
69 0x1e, 0x28, 0x33, 0x2d, 0x21, 0x30,
70 0x2c, 0x31, 0x27, 0x38, 0x22, 0x35,
71 0x2e, 0x2a, 0x32, 0x24, 0x1d, 0x20 };
72 static const u8 IP[]={ 0x3a, 0x32, 0x2a, 0x22, 0x1a, 0x12, 0x0a, 0x02, 0x3c, 0x34, 0x2c, 0x24, 0x1c, 0x14, 0x0c, 0x04,
73 0x3e, 0x36, 0x2e, 0x26, 0x1e, 0x16, 0x0e, 0x06, 0x40, 0x38, 0x30, 0x28, 0x20, 0x18, 0x10, 0x08,
74
75 0x39, 0x31, 0x29, 0x21, 0x19, 0x11, 0x09, 0x01, 0x3b, 0x33, 0x2b, 0x23, 0x1b, 0x13, 0x0b, 0x03,
76 0x3d, 0x35, 0x2d, 0x25, 0x1d, 0x15, 0x0d, 0x05, 0x3f, 0x37, 0x2f, 0x27, 0x1f, 0x17, 0x0f, 0x07 };
77 static const u8 Expansion[]={ 0x20, 0x01, 0x02, 0x03, 0x04, 0x05, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x08, 0x09, 0x0a, 0x0b,
78 0x0c, 0x0d, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x14, 0x15,
79 0x16, 0x17, 0x18, 0x19, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x01 };
80 static const u8 sbox[][64]={ { 0x0e, 0x04, 0x0d, 0x01, 0x02, 0x0f, 0x0b, 0x08, 0x03, 0x0a, 0x06, 0x0c, 0x05, 0x09, 0x00, 0x07,
81 0x00, 0x0f, 0x07, 0x04, 0x0e, 0x02, 0x0d, 0x01, 0x0a, 0x06, 0x0c, 0x0b, 0x09, 0x05, 0x03, 0x08,
82 0x04, 0x01, 0x0e, 0x08, 0x0d, 0x06, 0x02, 0x0b, 0x0f, 0x0c, 0x09, 0x07, 0x03, 0x0a, 0x05, 0x00,
83 0x0f, 0x0c, 0x08, 0x02, 0x04, 0x09, 0x01, 0x07, 0x05, 0x0b, 0x03, 0x0e, 0x0a, 0x00, 0x06, 0x0d },
84 { 0x0f, 0x01, 0x08, 0x0e, 0x06, 0x0b, 0x03, 0x04, 0x09, 0x07, 0x02, 0x0d, 0x0c, 0x00, 0x05, 0x0a,
85 0x03, 0x0d, 0x04, 0x07, 0x0f, 0x02, 0x08, 0x0e, 0x0c, 0x00, 0x01, 0x0a, 0x06, 0x09, 0x0b, 0x05,
86 0x00, 0x0e, 0x07, 0x0b, 0x0a, 0x04, 0x0d, 0x01, 0x05, 0x08, 0x0c, 0x06, 0x09, 0x03, 0x02, 0x0f,
87 0x0d, 0x08, 0x0a, 0x01, 0x03, 0x0f, 0x04, 0x02, 0x0b, 0x06, 0x07, 0x0c, 0x00, 0x05, 0x0e, 0x09 },
88 { 0x0a, 0x00, 0x09, 0x0e, 0x06, 0x03, 0x0f, 0x05, 0x01, 0x0d, 0x0c, 0x07, 0x0b, 0x04, 0x02, 0x08,
89 0x0d, 0x07, 0x00, 0x09, 0x03, 0x04, 0x06, 0x0a, 0x02, 0x08, 0x05, 0x0e, 0x0c, 0x0b, 0x0f, 0x01,
90 0x0d, 0x06, 0x04, 0x09, 0x08, 0x0f, 0x03, 0x00, 0x0b, 0x01, 0x02, 0x0c, 0x05, 0x0a, 0x0e, 0x07,
91 0x01, 0x0a, 0x0d, 0x00, 0x06, 0x09, 0x08, 0x07, 0x04, 0x0f, 0x0e, 0x03, 0x0b, 0x05, 0x02, 0x0c },
92 { 0x07, 0x0d, 0x0e, 0x03, 0x00, 0x06, 0x09, 0x0a, 0x01, 0x02, 0x08, 0x05, 0x0b, 0x0c, 0x04, 0x0f,
93 0x0d, 0x08, 0x0b, 0x05, 0x06, 0x0f, 0x00, 0x03, 0x04, 0x07, 0x02, 0x0c, 0x01, 0x0a, 0x0e, 0x09,
94 0x0a, 0x06, 0x09, 0x00, 0x0c, 0x0b, 0x07, 0x0d, 0x0f, 0x01, 0x03, 0x0e, 0x05, 0x02, 0x08, 0x04,
95 0x03, 0x0f, 0x00, 0x06, 0x0a, 0x01, 0x0d, 0x08, 0x09, 0x04, 0x05, 0x0b, 0x0c, 0x07, 0x02, 0x0e },
96 { 0x02, 0x0c, 0x04, 0x01, 0x07, 0x0a, 0x0b, 0x06, 0x08, 0x05, 0x03, 0x0f, 0x0d, 0x00, 0x0e, 0x09,
97 0x0e, 0x0b, 0x02, 0x0c, 0x04, 0x07, 0x0d, 0x01, 0x05, 0x00, 0x0f, 0x0a, 0x03, 0x09, 0x08, 0x06,
98 0x04, 0x02, 0x01, 0x0b, 0x0a, 0x0d, 0x07, 0x08, 0x0f, 0x09, 0x0c, 0x05, 0x06, 0x03, 0x00, 0x0e,
99 0x0b, 0x08, 0x0c, 0x07, 0x01, 0x0e, 0x02, 0x0d, 0x06, 0x0f, 0x00, 0x09, 0x0a, 0x04, 0x05, 0x03 },
100 { 0x0c, 0x01, 0x0a, 0x0f, 0x09, 0x02, 0x06, 0x08, 0x00, 0x0d, 0x03, 0x04, 0x0e, 0x07, 0x05, 0x0b,
101 0x0a, 0x0f, 0x04, 0x02, 0x07, 0x0c, 0x09, 0x05, 0x06, 0x01, 0x0d, 0x0e, 0x00, 0x0b, 0x03, 0x08,
102 0x09, 0x0e, 0x0f, 0x05, 0x02, 0x08, 0x0c, 0x03, 0x07, 0x00, 0x04, 0x0a, 0x01, 0x0d, 0x0b, 0x06,
103 0x04, 0x03, 0x02, 0x0c, 0x09, 0x05, 0x0f, 0x0a, 0x0b, 0x0e, 0x01, 0x07, 0x06, 0x00, 0x08, 0x0d },
104 { 0x04, 0x0b, 0x02, 0x0e, 0x0f, 0x00, 0x08, 0x0d, 0x03, 0x0c, 0x09, 0x07, 0x05, 0x0a, 0x06, 0x01,
105 0x0d, 0x00, 0x0b, 0x07, 0x04, 0x09, 0x01, 0x0a, 0x0e, 0x03, 0x05, 0x0c, 0x02, 0x0f, 0x08, 0x06,
106 0x01, 0x04, 0x0b, 0x0d, 0x0c, 0x03, 0x07, 0x0e, 0x0a, 0x0f, 0x06, 0x08, 0x00, 0x05, 0x09, 0x02,
107 0x06, 0x0b, 0x0d, 0x08, 0x01, 0x04, 0x0a, 0x07, 0x09, 0x05, 0x00, 0x0f, 0x0e, 0x02, 0x03, 0x0c },
108 { 0x0d, 0x02, 0x08, 0x04, 0x06, 0x0f, 0x0b, 0x01, 0x0a, 0x09, 0x03, 0x0e, 0x05, 0x00, 0x0c, 0x07,
109 0x01, 0x0f, 0x0d, 0x08, 0x0a, 0x03, 0x07, 0x04, 0x0c, 0x05, 0x06, 0x0b, 0x00, 0x0e, 0x09, 0x02,
110 0x07, 0x0b, 0x04, 0x01, 0x09, 0x0c, 0x0e, 0x02, 0x00, 0x06, 0x0a, 0x0d, 0x0f, 0x03, 0x05, 0x08,
111 0x02, 0x01, 0x0e, 0x07, 0x04, 0x0a, 0x08, 0x0d, 0x0f, 0x0c, 0x09, 0x00, 0x03, 0x05, 0x06, 0x0b }};
112 static const u8 Permutation[]={ 0x10, 0x07, 0x14, 0x15, 0x1d, 0x0c, 0x1c, 0x11, 0x01, 0x0f, 0x17, 0x1a, 0x05, 0x12, 0x1f, 0x0a,
113 0x02, 0x08, 0x18, 0x0e, 0x20, 0x1b, 0x03, 0x09, 0x13, 0x0d, 0x1e, 0x06, 0x16, 0x0b, 0x04, 0x19 };
114 static const u8 FP[]={ 0x28, 0x08, 0x30, 0x10, 0x38, 0x18, 0x40, 0x20, 0x27, 0x07, 0x2f, 0x0f, 0x37, 0x17, 0x3f, 0x1f,
115 0x26, 0x06, 0x2e, 0x0e, 0x36, 0x16, 0x3e, 0x1e, 0x25, 0x05, 0x2d, 0x0d, 0x35, 0x15, 0x3d, 0x1d,
116 0x24, 0x04, 0x2c, 0x0c, 0x34, 0x14, 0x3c, 0x1c, 0x23, 0x03, 0x2b, 0x0b, 0x33, 0x13, 0x3b, 0x1b,
117 0x22, 0x02, 0x2a, 0x0a, 0x32, 0x12, 0x3a, 0x1a, 0x21, 0x01, 0x29, 0x09, 0x31, 0x11, 0x39, 0x19 };
118 u32 BitMask_hi, BitMask_lo;
119 u32 PermutedKeyMask_lo, PermutedKeyMask_hi, Rot2Mask_lo, Rot1Mask_lo, Rot2Mask_hi, Rot1Mask_hi;
120 u32 BitPerm_lo, BitPerm_hi;
121 u32 password_lo, password_hi;
122 u32 key1, key2, key3, key4, input_lo, input_hi;
123 u32 PermutedKey_lo, PermutedKey_hi, kVal_lo, kVal_hi, PermutedInput_lo, PermutedInput_hi, eVal_lo, eVal_hi, sVal_lo, sVal_hi, pVal_lo, pVal_hi, output_lo, output_hi;
124 struct KeyPair pairC[17], pairD[17], pairK[17], pairL[17], pairR[17];
125 struct KeyPair *pPairC, *pPairD, *pPairK, *pPairL, *pPairR;
126 unsigned int i, j, k;
127 int shift;
128
129 //Phase 1 (Permute KEY with Permuted Choice 1)
130 PermutedKey_lo = 0;
131 PermutedKey_hi = 0;
132 BitMask_lo = 0;
133 BitMask_hi = 0x80000000;
134 BitPerm_lo = 0;
135 BitPerm_hi = 0x80000000;
136
137 password_lo = *(const u32*)password;
138 password_hi = *(const u32*)(password + 4);
139
140 for(i = 0; i < 56; i++)
141 {
142 shift = PC1[i] - 1;
143
144 if((shift << 26) >= 0)
145 { //0 to 31-bit shift
146 if((shift << 26) > 0) //Shift all bits left by shift (hi,lo >> shift)
147 key1 = (BitMask_lo >> shift) | (BitMask_hi << (-shift));
148 else //0-bit shift, which should not happen.
149 key1 = BitMask_lo >> shift;
150
151 key2 = BitMask_hi >> shift;
152 } else { //>31-bit shift
153 key2 = 0;
154 key1 = BitMask_hi >> shift;
155 }
156
157 //If bit (shift) is set, set the current bit.
158 if(((password_lo & key1) | (password_hi & key2)) != 0)
159 {
160 PermutedKey_lo |= BitPerm_lo;
161 PermutedKey_hi |= BitPerm_hi;
162 }
163
164 //Shift all bits left by 1 (hi,lo >> 1)
165 BitPerm_lo = (BitPerm_lo >> 1) | (BitPerm_hi << 31);
166 BitPerm_hi >>= 1;
167 }
168
169 //Phase 2 (Key Schedule Calculation)
170 //This mask is used to extract the lower 28-bits of a key.
171 PermutedKeyMask_lo = 0x0FFFFFFF;
172 PermutedKeyMask_hi = 0x00000000;
173
174 Rot2Mask_lo = 3;
175 Rot2Mask_hi = 0x00000000;
176 Rot1Mask_lo = 1;
177 Rot1Mask_hi = 0x00000000;
178
179 //C-bits, upper 28-bits of Permuted Key
180 pairC[0].lo = PermutedKey_hi >> 4;
181 pairC[0].hi = 0;
182
183 //D-bits, lower 28-bits of Permuted Key
184 pairD[0].lo = ((PermutedKey_lo >> 8) | (PermutedKey_hi << 24)) & PermutedKeyMask_lo;
185 pairD[0].hi = ((PermutedKey_hi >> 8) & PermutedKeyMask_hi);
186
187 //Calculate all Cn and Dn.
188 for(i = 0; i < 16; i++)
189 {
190 if(Rotations[i] != 1)
191 { //Rotate left twice
192 //hi 26:0 | lo 31:26
193 key1 = ((pairC[i].hi << 6) | (pairC[i].lo >> 26)) & Rot2Mask_lo;
194 //lo 29:0
195 //key1|key 4 results in: LLLLLLLLLLLLLLLLLLLLLLLLLLLLLXX, where X is from key1
196 key4 = pairC[i].lo << 2;
197 //hi 31:26
198 //This part is discarded.
199 key2 = (pairC[i].hi >> 26) & Rot2Mask_hi;
200 //hi 29:2 | lo 31:30
201 //key2|key3 results in: HHHHHHHHHHHHHHHHHHHHHHHHHHHHHLL
202 key3 = (pairC[i].hi << 2) | (pairC[i].lo >> 30);
203 } else { //Rotate left once
204 //hi 27:0 | lo 31:27
205 key1 = ((pairC[i].hi << 5) | (pairC[i].lo >> 27)) & Rot1Mask_lo;
206 //lo 30:0
207 //key1|key4 results in: LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLX, where X is from key1
208 key4 = pairC[i].lo << 1;
209 //hi 31:27
210 //This part is discarded.
211 key2 = (pairC[i].hi >> 27) & Rot1Mask_hi;
212 //hi 30:0 | lo 31:31
213 //key2:key3 results in: HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHL
214 key3 = (pairC[i].hi << 1) | (pairC[i].lo >> 31);
215 }
216
217 //Merge the two rotated parts together, for the hi and low pair.
218 //Note: hi contains nothing.
219 pairC[i + 1].lo = (key1 | key4) & PermutedKeyMask_lo;
220 pairC[i + 1].hi = (key2 | key3) & PermutedKeyMask_hi;
221
222 if(Rotations[i] != 1)
223 { //Rotate left twice
224 //hi 26:0 | lo 31:26
225 key1 = ((pairD[i].hi << 6) | (pairD[i].lo >> 26)) & Rot2Mask_lo;
226 //lo 29:0
227 //key1|key 4 results in: LLLLLLLLLLLLLLLLLLLLLLLLLLLLLXX, where X is from key1
228 key4 = pairD[i].lo << 2;
229 //hi 31:26
230 //This part is discarded.
231 key2 = (pairD[i].hi >> 26) & Rot2Mask_hi;
232 //hi 29:2 | lo 31:30
233 //key2|key3 results in: HHHHHHHHHHHHHHHHHHHHHHHHHHHHHLL
234 key3 = (pairD[i].hi << 2) | (pairD[i].lo >> 30);
235 } else { //Rotate left once
236 //hi 27:0 | lo 31:27
237 key1 = ((pairD[i].hi << 5) | (pairD[i].lo >> 27)) & Rot1Mask_lo;
238 //lo 30:0
239 //key1|key4 results in: LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLX, where X is from key1
240 key4 = pairD[i].lo << 1;
241 //hi 31:27
242 //This part is discarded.
243 key2 = (pairD[i].hi >> 27) & Rot1Mask_hi;
244 //hi 30:0 | lo 31:31
245 //key2:key3 results in: HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHL
246 key3 = (pairD[i].hi << 1) | (pairD[i].lo >> 31);
247 }
248
249 //Merge the two rotated parts together, for the hi and low pair.
250 //Note: hi contains nothing.
251 pairD[i + 1].lo = (key1 | key4) & PermutedKeyMask_lo;
252 pairD[i + 1].hi = (key2 | key3) & PermutedKeyMask_hi;
253 }
254
255 //Phase 3
256 BitMask_lo = 0x00000000;
257 BitMask_hi = 0x80000000;
258
259 // Determine all K, through the Permutation of CnDn by PC-2
260 for(pPairC = &pairC[1],pPairD = &pairD[1],pPairK = &pairK[1]; pPairK < &pairK[17]; pPairC++,pPairD++,pPairK++)
261 {
262 kVal_lo = 0;
263 kVal_hi = 0;
264 BitPerm_lo = 0x00000000;
265 BitPerm_hi = 0x80000000;
266
267 /* Calculate CnDn:
268 Note: hi of both Cn and Dn are assumed to, and actually contain nothing.
269 lo: (D lo 23:0) | (0 7:0)
270 D lo 23:0 LLLLLLLLLLLLLLLLLLLLLLLL00000000
271 0 7:0 00000000000000000000000000000000
272 hi: (C hi 27:0) | (D hi 23:0) | (D lo 31:24)
273 C lo 27:0 llllllllllllllllllllllllllll0000
274 D hi 23:0 HHHHHHHHHHHHHHHHHHHHHHHH00000000 All zero
275 D lo 31:24 000000000000000000000000XXXXLLLL
276
277 l = C lo L = D lo
278 h = C hi H = D hi X = unused bits (28/32-bit value) */
279 input_lo = 0 | (pPairD->lo << 8);
280 input_hi = (pPairC->lo << 4) | (pPairD->hi << 8) | (pPairD->lo >> 24);
281
282 for(i = 0; i < 48; i++)
283 {
284 shift = PC2[i] - 1;
285
286 if((shift << 26) >= 0)
287 { //0 to 31-bit shift
288 if((shift << 26) > 0) //Shift all bits left by shift (hi,lo >> shift)
289 key1 = (BitMask_lo >> shift) | (BitMask_hi << (-shift));
290 else //0-bit shift, which should not happen.
291 key1 = BitMask_lo >> shift;
292
293 key2 = BitMask_hi >> shift;
294 } else { //>31-bit shift
295 key2 = 0;
296 key1 = BitMask_hi >> shift;
297 }
298
299 //If bit (shift) is set, set the current bit.
300 if(((input_lo & key1) | (input_hi & key2)) != 0)
301 {
302 kVal_lo |= BitPerm_lo;
303 kVal_hi |= BitPerm_hi;
304 }
305
306 //Shift all bits left by 1 (hi,lo >> 1)
307 BitPerm_lo = (BitPerm_hi << 31) | (BitPerm_lo >> 1);
308 BitPerm_hi >>= 1;
309 }
310
311 pPairK->lo = kVal_lo;
312 pPairK->hi = kVal_hi;
313 }
314
315 //Phase 4 (Enciphering)
316 BitMask_lo = 0x00000000;
317 BitMask_hi = 0x80000000;
318 BitPerm_lo = 0x00000000;
319 BitPerm_hi = 0x80000000;
320 PermutedInput_lo = 0;
321 PermutedInput_hi = 0;
322
323 //Initial Permutation (IP)
324 for(i = 0; i < 64; i++)
325 {
326 shift = IP[i] - 1;
327
328 if((shift << 26) >= 0)
329 { //0 to 31-bit shift
330 if((shift << 26) > 0) //Shift all bits left by shift (hi,lo >> shift)
331 key1 = (BitMask_lo >> shift) | (BitMask_hi << (-shift));
332 else //0-bit shift, which should not happen.
333 key1 = BitMask_lo >> shift;
334
335 key2 = BitMask_hi >> shift;
336 } else { //>31-bit shift
337 key2 = 0;
338 key1 = BitMask_hi >> shift;
339 }
340
341 //If bit (shift) is set, set the current bit.
342 if(((id_lo & key1) | (id_hi & key2)) != 0)
343 {
344 PermutedInput_lo |= BitPerm_lo;
345 PermutedInput_hi |= BitPerm_hi;
346 }
347
348 //Shift all bits left by 1 (hi,lo >> 1)
349 BitPerm_lo = (BitPerm_lo >> 1) | (BitPerm_hi << 31);
350 BitPerm_hi >>= 1;
351 }
352
353 //Phase 5 (Key-dependent Computation)
354 BitMask_lo = 0x00000000;
355 BitMask_hi = 0x80000000;
356
357 //L0 and R0 make up the permuted input block.
358 //The lo values are not used.
359 pairL[0].lo = PermutedInput_lo & 0x00000000;
360 pairL[0].hi = PermutedInput_hi & 0xFFFFFFFF;
361 pairR[0].lo = 0x00000000;
362 pairR[0].hi = PermutedInput_lo << 0;
363
364 for (j = 0, k = 1, pPairL = &pairL[1], pPairR = &pairR[1]; pPairR < &pairR[17]; j++, k++, pPairR++, pPairL++)
365 {
366 eVal_lo = 0;
367 eVal_hi = 0;
368 BitPerm_lo = 0x00000000;
369 BitPerm_hi = 0x80000000;
370
371 //Ln' = Rn
372 pPairL->lo = input_lo = pairR[j].lo;
373 pPairL->hi = input_hi = pairR[j].hi;
374
375 //Calculate E(Rn)
376 for(i = 0; i < 48; i++)
377 {
378 shift = Expansion[i] - 1;
379 if((shift << 26) >= 0)
380 { //0 to 31-bit shift
381 if((shift << 26) > 0) //Shift all bits left by shift (hi,lo >> shift)
382 key1 = (BitMask_lo >> shift) | (BitMask_hi << (-shift));
383 else //0-bit shift, which should not happen.
384 key1 = BitMask_lo >> shift;
385
386 key2 = BitMask_hi >> shift;
387 } else { //>31-bit shift
388 key2 = 0;
389 key1 = BitMask_hi >> shift;
390 }
391
392 //If bit (shift) is set, set the current bit.
393 if(((input_lo & key1) | (input_hi & key2)) != 0)
394 {
395 eVal_lo |= BitPerm_lo;
396 eVal_hi |= BitPerm_hi;
397 }
398
399 //Shift all bits left by 1 (hi,lo >> 1)
400 BitPerm_lo = (BitPerm_hi << 31) | (BitPerm_lo >> 1);
401 BitPerm_hi >>= 1;
402 }
403
404 //Phase 6 (Substitution Boxes)
405 sVal_lo = 0;
406 sVal_hi = 0;
407 //Calculate Bn = Kn ^ E(Rn)
408 //Also, shift the 48-bit Expansion value forward towards position 0.
409 eVal_lo = (eVal_lo ^ pairK[k].lo) >> 16;
410 eVal_hi = eVal_hi ^ pairK[k].hi;
411 eVal_lo |= (eVal_hi << 16);
412 eVal_hi >>= 16;
413
414 //Calculate Sn(Bn), which is stored in the upper 32-bits of the result.
415 for(i = 0, shift = 32; i < 8; i++, shift += 4)
416 {
417 input_lo = sbox[7 - i][((eVal_lo & 0x20) | ((eVal_lo & 0x3F) >> 1 & 0xF) | ((eVal_lo & 0x3F) << 4 & 0x10))];
418 //Shift to the next 6-bit B-block
419 eVal_lo = (eVal_lo >> 6) | (eVal_hi << 26);
420 eVal_hi >>= 6;
421
422 input_hi = 0;
423 if((s32)((u32)shift << 26) >= 0)
424 { //0 to 31-bit shift
425 if((shift << 26) > 0) //Shift all bits right by shift (hi,lo >> shift)
426 key3 = (input_hi << shift) | (input_lo >> (-shift));
427 else //0-bit shift, which should not happen.
428 key3 = input_hi << shift;
429
430 key4 = input_lo << shift;
431 } else { //>31-bit shift
432 key3 = input_lo << shift;
433 key4 = 0;
434 }
435
436 sVal_lo |= key4;
437 sVal_hi |= key3;
438 }
439
440 //Phase 7 (Permutation with function P)
441 pVal_lo = 0;
442 pVal_hi = 0;
443 BitPerm_lo = 0x00000000;
444 BitPerm_hi = 0x80000000;
445
446 //Calculate P(Sn(K ^ E(Rn)))
447 for(i = 0; i < 32; i++)
448 {
449 shift = Permutation[i] - 1;
450 if((shift << 26) >= 0)
451 { //0 to 31-bit shift
452 if((shift << 26) > 0) //Shift all bits left by shift (hi,lo >> shift)
453 key1 = (BitMask_lo >> shift) | (BitMask_hi << (-shift));
454 else //0-bit shift, which should not happen.
455 key1 = BitMask_lo >> shift;
456
457 key2 = BitMask_hi >> shift;
458 } else { //>31-bit shift
459 key1 = BitMask_hi >> shift;
460 key2 = 0;
461 }
462
463 //If bit (shift) is set, set the current bit.
464 if(((sVal_lo & key1) | (sVal_hi & key2)) != 0)
465 {
466 pVal_lo |= BitPerm_lo;
467 pVal_hi |= BitPerm_hi;
468 }
469
470 //Shift all bits left by 1 (hi,lo >> 1)
471 BitPerm_lo = (BitPerm_lo >> 1) | (BitPerm_hi << 31);
472 BitPerm_hi >>= 1;
473 }
474
475 //0x00003bc4 - Rn' = Ln ^ f(Rn,Kn)
476 pPairR->lo = pairL[j].lo ^ pVal_lo;
477 pPairR->hi = pairL[j].hi ^ pVal_hi;
478 }
479
480 //Phase 8 (Final Permutation)
481 output_lo = 0;
482 output_hi = 0;
483 BitMask_lo = BitPerm_lo = 0x00000000;
484 BitMask_hi = BitPerm_hi = 0x80000000;
485 //Retrieve preoutput blocks
486 //Ln and Rn lo do not contain anything, hence they are discarded.
487 input_lo = pairR[16].lo | (pairL[16].hi >> 0);
488 input_hi = pairR[16].hi | 0;
489
490 //Subject the preoutput to the Final Permutation
491 for(i = 0; i < 64; i++)
492 {
493 shift = FP[i] - 1;
494 if((shift << 26) >= 0)
495 { //0 to 31-bit shift
496 if((shift << 26) > 0) //Shift all bits left by shift (hi,lo >> shift)
497 key1 = (BitMask_lo >> shift) | (BitMask_hi << (-shift));
498 else //0-bit shift, which should not happen.
499 key1 = BitMask_lo >> shift;
500
501 key2 = BitMask_hi >> shift;
502 } else { //>31-bit shift
503 key1 = BitMask_hi >> shift;
504 key2 = 0;
505 }
506
507 //If bit (shift) is set, set the current bit.
508 if(((input_lo & key1) | (input_hi & key2)) != 0)
509 {
510 output_lo |= BitPerm_lo;
511 output_hi |= BitPerm_hi;
512 }
513
514 //Shift all bits left by 1 (hi,lo >> 1)
515 BitPerm_lo = (BitPerm_lo >> 1) | (BitPerm_hi << 31);
516 BitPerm_hi >>= 1;
517 }
518
519 //Encrypted output
520 *(u32*)password_out = output_lo;
521 *(u32*)(password_out + 4) = output_hi;
522}
#define EACCES
Definition errno.h:45